Fixes code according to modern code style. Fixes a few expression bugs.

This commit is contained in:
Kamron Batman 2018-09-15 09:58:51 -07:00
parent 89eea25e5f
commit 970fd563b2
3324 changed files with 441118 additions and 433755 deletions

View file

@ -1,413 +1,410 @@
using System;
using System.IO;
using System.Xml;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Server.Items;
namespace Server.Commands
{
public class Categorization
{
private static CategoryEntry m_RootItems, m_RootMobiles;
public class Categorization
{
private static CategoryEntry m_RootItems, m_RootMobiles;
public static CategoryEntry Items
{
get
{
if ( m_RootItems == null )
Load();
private static Type typeofItem = typeof(Item);
private static Type typeofMobile = typeof(Mobile);
private static Type typeofConstructible = typeof(ConstructibleAttribute);
return m_RootItems;
}
}
public static CategoryEntry Items
{
get
{
if (m_RootItems == null)
Load();
public static CategoryEntry Mobiles
{
get
{
if ( m_RootMobiles == null )
Load();
return m_RootItems;
}
}
return m_RootMobiles;
}
}
public static CategoryEntry Mobiles
{
get
{
if (m_RootMobiles == null)
Load();
public static void Initialize()
{
CommandSystem.Register( "RebuildCategorization", AccessLevel.Administrator, RebuildCategorization_OnCommand );
}
return m_RootMobiles;
}
}
[Usage( "RebuildCategorization" )]
[Description( "Rebuilds the categorization data file used by the Add command." )]
public static void RebuildCategorization_OnCommand( CommandEventArgs e )
{
CategoryEntry root = new CategoryEntry( null, "Add Menu", new[]{ Items, Mobiles } );
public static void Initialize()
{
CommandSystem.Register("RebuildCategorization", AccessLevel.Administrator, RebuildCategorization_OnCommand);
}
Export( root, "Data/objects.xml", "Objects" );
[Usage("RebuildCategorization")]
[Description("Rebuilds the categorization data file used by the Add command.")]
public static void RebuildCategorization_OnCommand(CommandEventArgs e)
{
CategoryEntry root = new CategoryEntry(null, "Add Menu", new[] { Items, Mobiles });
e.Mobile.SendMessage( "Categorization menu rebuilt." );
}
Export(root, "Data/objects.xml", "Objects");
public static void RecurseFindCategories( CategoryEntry ce, ArrayList list )
{
list.Add( ce );
e.Mobile.SendMessage("Categorization menu rebuilt.");
}
for ( int i = 0; i < ce.SubCategories.Length; ++i )
RecurseFindCategories( ce.SubCategories[i], list );
}
public static void RecurseFindCategories(CategoryEntry ce, ArrayList list)
{
list.Add(ce);
public static void Export( CategoryEntry ce, string fileName, string title )
{
XmlTextWriter xml = new XmlTextWriter( fileName, System.Text.Encoding.UTF8 );
for (int i = 0; i < ce.SubCategories.Length; ++i)
RecurseFindCategories(ce.SubCategories[i], list);
}
xml.Indentation = 1;
xml.IndentChar = '\t';
xml.Formatting = Formatting.Indented;
public static void Export(CategoryEntry ce, string fileName, string title)
{
XmlTextWriter xml = new XmlTextWriter(fileName, Encoding.UTF8);
xml.WriteStartDocument( true );
xml.Indentation = 1;
xml.IndentChar = '\t';
xml.Formatting = Formatting.Indented;
RecurseExport( xml, ce );
xml.WriteStartDocument(true);
xml.Flush();
xml.Close();
}
RecurseExport(xml, ce);
public static void RecurseExport( XmlTextWriter xml, CategoryEntry ce )
{
xml.WriteStartElement( "category" );
xml.Flush();
xml.Close();
}
xml.WriteAttributeString( "title", ce.Title );
public static void RecurseExport(XmlTextWriter xml, CategoryEntry ce)
{
xml.WriteStartElement("category");
ArrayList subCats = new ArrayList( ce.SubCategories );
xml.WriteAttributeString("title", ce.Title);
subCats.Sort( new CategorySorter() );
ArrayList subCats = new ArrayList(ce.SubCategories);
for ( int i = 0; i < subCats.Count; ++i )
RecurseExport( xml, (CategoryEntry)subCats[i] );
subCats.Sort(new CategorySorter());
ce.Matched.Sort( new CategorySorter() );
for (int i = 0; i < subCats.Count; ++i)
RecurseExport(xml, (CategoryEntry)subCats[i]);
for ( int i = 0; i < ce.Matched.Count; ++i )
{
CategoryTypeEntry cte = (CategoryTypeEntry)ce.Matched[i];
ce.Matched.Sort(new CategorySorter());
xml.WriteStartElement( "object" );
for (int i = 0; i < ce.Matched.Count; ++i)
{
CategoryTypeEntry cte = (CategoryTypeEntry)ce.Matched[i];
xml.WriteAttributeString( "type", cte.Type.ToString() );
xml.WriteStartElement("object");
if ( cte.Object is Item item )
{
int itemID = item.ItemID;
xml.WriteAttributeString("type", cte.Type.ToString());
if ( item is BaseAddon addon && addon.Components.Count == 1 )
itemID = addon.Components[0].ItemID;
if (cte.Object is Item item)
{
int itemID = item.ItemID;
if ( itemID > TileData.MaxItemValue )
itemID = 1;
if (item is BaseAddon addon && addon.Components.Count == 1)
itemID = addon.Components[0].ItemID;
xml.WriteAttributeString( "gfx", XmlConvert.ToString( itemID ) );
if (itemID > TileData.MaxItemValue)
itemID = 1;
int hue = item.Hue & 0x7FFF;
xml.WriteAttributeString("gfx", XmlConvert.ToString(itemID));
if ( (hue & 0x4000) != 0 )
hue = 0;
int hue = item.Hue & 0x7FFF;
if ( hue != 0 )
xml.WriteAttributeString( "hue", XmlConvert.ToString( hue ) );
if ((hue & 0x4000) != 0)
hue = 0;
item.Delete();
}
else if ( cte.Object is Mobile mob )
{
int itemID = ShrinkTable.Lookup( mob, 1 );
if (hue != 0)
xml.WriteAttributeString("hue", XmlConvert.ToString(hue));
xml.WriteAttributeString( "gfx", XmlConvert.ToString( itemID ) );
item.Delete();
}
else if (cte.Object is Mobile mob)
{
int itemID = ShrinkTable.Lookup(mob, 1);
int hue = mob.Hue & 0x7FFF;
xml.WriteAttributeString("gfx", XmlConvert.ToString(itemID));
if ( (hue & 0x4000) != 0 )
hue = 0;
int hue = mob.Hue & 0x7FFF;
if ( hue != 0 )
xml.WriteAttributeString( "hue", XmlConvert.ToString( hue ) );
if ((hue & 0x4000) != 0)
hue = 0;
mob.Delete();
}
if (hue != 0)
xml.WriteAttributeString("hue", XmlConvert.ToString(hue));
xml.WriteEndElement();
}
mob.Delete();
}
xml.WriteEndElement();
}
xml.WriteEndElement();
}
public static void Load()
{
ArrayList types = new ArrayList();
xml.WriteEndElement();
}
AddTypes( Core.Assembly, types );
public static void Load()
{
ArrayList types = new ArrayList();
for ( int i = 0; i < ScriptCompiler.Assemblies.Length; ++i )
AddTypes( ScriptCompiler.Assemblies[i], types );
AddTypes(Core.Assembly, types);
m_RootItems = Load( types, "Data/items.cfg" );
m_RootMobiles = Load( types, "Data/mobiles.cfg" );
}
for (int i = 0; i < ScriptCompiler.Assemblies.Length; ++i)
AddTypes(ScriptCompiler.Assemblies[i], types);
private static CategoryEntry Load( ArrayList types, string config )
{
CategoryLine[] lines = CategoryLine.Load( config );
m_RootItems = Load(types, "Data/items.cfg");
m_RootMobiles = Load(types, "Data/mobiles.cfg");
}
if ( lines.Length > 0 )
{
int index = 0;
CategoryEntry root = new CategoryEntry( null, lines, ref index );
private static CategoryEntry Load(ArrayList types, string config)
{
CategoryLine[] lines = CategoryLine.Load(config);
Fill( root, types );
if (lines.Length > 0)
{
int index = 0;
CategoryEntry root = new CategoryEntry(null, lines, ref index);
return root;
}
Fill(root, types);
return new CategoryEntry();
}
return root;
}
private static Type typeofItem = typeof( Item );
private static Type typeofMobile = typeof( Mobile );
private static Type typeofConstructible = typeof( ConstructibleAttribute );
return new CategoryEntry();
}
private static bool IsConstructible( Type type )
{
if ( !type.IsSubclassOf( typeofItem ) && !type.IsSubclassOf( typeofMobile ) )
return false;
private static bool IsConstructible(Type type)
{
if (!type.IsSubclassOf(typeofItem) && !type.IsSubclassOf(typeofMobile))
return false;
ConstructorInfo ctor = type.GetConstructor( Type.EmptyTypes );
ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
return ( ctor != null && ctor.IsDefined( typeofConstructible, false ) );
}
return ctor != null && ctor.IsDefined(typeofConstructible, false);
}
private static void AddTypes( Assembly asm, ArrayList types )
{
Type[] allTypes = asm.GetTypes();
private static void AddTypes(Assembly asm, ArrayList types)
{
Type[] allTypes = asm.GetTypes();
for ( int i = 0; i < allTypes.Length; ++i )
{
Type type = allTypes[i];
for (int i = 0; i < allTypes.Length; ++i)
{
Type type = allTypes[i];
if ( type.IsAbstract )
continue;
if (type.IsAbstract)
continue;
if ( IsConstructible( type ) )
types.Add( type );
}
}
if (IsConstructible(type))
types.Add(type);
}
}
private static void Fill( CategoryEntry root, ArrayList list )
{
for ( int i = 0; i < list.Count; ++i )
{
Type type = (Type)list[i];
CategoryEntry match = GetDeepestMatch( root, type );
private static void Fill(CategoryEntry root, ArrayList list)
{
for (int i = 0; i < list.Count; ++i)
{
Type type = (Type)list[i];
CategoryEntry match = GetDeepestMatch(root, type);
if ( match == null )
continue;
if (match == null)
continue;
try
{
match.Matched.Add( new CategoryTypeEntry( type ) );
}
catch
{
}
}
}
try
{
match.Matched.Add(new CategoryTypeEntry(type));
}
catch
{
}
}
}
private static CategoryEntry GetDeepestMatch( CategoryEntry root, Type type )
{
if ( !root.IsMatch( type ) )
return null;
private static CategoryEntry GetDeepestMatch(CategoryEntry root, Type type)
{
if (!root.IsMatch(type))
return null;
for ( int i = 0; i < root.SubCategories.Length; ++i )
{
CategoryEntry check = GetDeepestMatch( root.SubCategories[i], type );
for (int i = 0; i < root.SubCategories.Length; ++i)
{
CategoryEntry check = GetDeepestMatch(root.SubCategories[i], type);
if ( check != null )
return check;
}
if (check != null)
return check;
}
return root;
}
}
return root;
}
}
public class CategorySorter : IComparer
{
public int Compare( object x, object y )
{
string a = null, b = null;
public class CategorySorter : IComparer
{
public int Compare(object x, object y)
{
string a = null, b = null;
if ( x is CategoryEntry entry )
a = entry.Title;
else if ( x is CategoryTypeEntry xTypeEntry )
a = xTypeEntry.Type.Name;
if (x is CategoryEntry entry)
a = entry.Title;
else if (x is CategoryTypeEntry xTypeEntry)
a = xTypeEntry.Type.Name;
if ( y is CategoryEntry categoryEntry )
b = categoryEntry.Title;
else if ( y is CategoryTypeEntry yTypeEntry )
b = yTypeEntry.Type.Name;
if (y is CategoryEntry categoryEntry)
b = categoryEntry.Title;
else if (y is CategoryTypeEntry yTypeEntry)
b = yTypeEntry.Type.Name;
if ( a == null && b == null )
return 0;
if (a == null && b == null)
return 0;
if ( a == null )
return 1;
if (a == null)
return 1;
if ( b == null )
return -1;
if (b == null)
return -1;
return a.CompareTo( b );
}
}
return a.CompareTo(b);
}
}
public class CategoryTypeEntry
{
public Type Type { get; }
public class CategoryTypeEntry
{
public CategoryTypeEntry(Type type)
{
Type = type;
Object = Activator.CreateInstance(type);
}
public object Object { get; }
public Type Type{ get; }
public CategoryTypeEntry( Type type )
{
Type = type;
Object = Activator.CreateInstance( type );
}
}
public object Object{ get; }
}
public class CategoryEntry
{
public string Title { get; }
public class CategoryEntry
{
public CategoryEntry()
{
Title = "(empty)";
Matches = new Type[0];
SubCategories = new CategoryEntry[0];
Matched = new ArrayList();
}
public Type[] Matches { get; }
public CategoryEntry(CategoryEntry parent, string title, CategoryEntry[] subCats)
{
Parent = parent;
Title = title;
SubCategories = subCats;
Matches = new Type[0];
Matched = new ArrayList();
}
public CategoryEntry Parent { get; }
public CategoryEntry(CategoryEntry parent, CategoryLine[] lines, ref int index)
{
Parent = parent;
public CategoryEntry[] SubCategories { get; }
string text = lines[index].Text;
public ArrayList Matched { get; }
int start = text.IndexOf('(');
public CategoryEntry()
{
Title = "(empty)";
Matches = new Type[0];
SubCategories = new CategoryEntry[0];
Matched = new ArrayList();
}
if (start < 0)
throw new FormatException($"Input string not correctly formatted ('{text}')");
public CategoryEntry( CategoryEntry parent, string title, CategoryEntry[] subCats )
{
Parent = parent;
Title = title;
SubCategories = subCats;
Matches = new Type[0];
Matched = new ArrayList();
}
Title = text.Substring(0, start).Trim();
public bool IsMatch( Type type )
{
bool isMatch = false;
int end = text.IndexOf(')', ++start);
for ( int i = 0; !isMatch && i < Matches.Length; ++i )
isMatch = ( type == Matches[i] || type.IsSubclassOf( Matches[i] ) );
if (end < start)
throw new FormatException($"Input string not correctly formatted ('{text}')");
return isMatch;
}
text = text.Substring(start, end - start);
string[] split = text.Split(';');
public CategoryEntry( CategoryEntry parent, CategoryLine[] lines, ref int index )
{
Parent = parent;
ArrayList list = new ArrayList();
string text = lines[index].Text;
for (int i = 0; i < split.Length; ++i)
{
Type type = ScriptCompiler.FindTypeByName(split[i].Trim());
int start = text.IndexOf( '(' );
if (type == null)
Console.WriteLine("Match type not found ('{0}')", split[i].Trim());
else
list.Add(type);
}
if ( start < 0 )
throw new FormatException($"Input string not correctly formatted ('{text}')");
Matches = (Type[])list.ToArray(typeof(Type));
list.Clear();
Title = text.Substring( 0, start ).Trim();
int ourIndentation = lines[index].Indentation;
int end = text.IndexOf( ')', ++start );
++index;
if ( end < start )
throw new FormatException($"Input string not correctly formatted ('{text}')");
while (index < lines.Length && lines[index].Indentation > ourIndentation)
list.Add(new CategoryEntry(this, lines, ref index));
text = text.Substring( start, end-start );
string[] split = text.Split( ';' );
SubCategories = (CategoryEntry[])list.ToArray(typeof(CategoryEntry));
list.Clear();
ArrayList list = new ArrayList();
Matched = list;
}
for ( int i = 0; i < split.Length; ++i )
{
Type type = ScriptCompiler.FindTypeByName( split[i].Trim() );
public string Title{ get; }
if ( type == null )
Console.WriteLine( "Match type not found ('{0}')", split[i].Trim() );
else
list.Add( type );
}
public Type[] Matches{ get; }
Matches = (Type[])list.ToArray( typeof( Type ) );
list.Clear();
public CategoryEntry Parent{ get; }
int ourIndentation = lines[index].Indentation;
public CategoryEntry[] SubCategories{ get; }
++index;
public ArrayList Matched{ get; }
while ( index < lines.Length && lines[index].Indentation > ourIndentation )
list.Add( new CategoryEntry( this, lines, ref index ) );
public bool IsMatch(Type type)
{
bool isMatch = false;
SubCategories = (CategoryEntry[])list.ToArray( typeof( CategoryEntry ) );
list.Clear();
for (int i = 0; !isMatch && i < Matches.Length; ++i)
isMatch = type == Matches[i] || type.IsSubclassOf(Matches[i]);
Matched = list;
}
}
return isMatch;
}
}
public class CategoryLine
{
public int Indentation { get; }
public class CategoryLine
{
public CategoryLine(string input)
{
int index;
public string Text { get; }
for (index = 0; index < input.Length; ++index)
if (char.IsLetter(input, index))
break;
public CategoryLine( string input )
{
int index;
if (index >= input.Length)
throw new FormatException($"Input string not correctly formatted ('{input}')");
for ( index = 0; index < input.Length; ++index )
{
if ( char.IsLetter( input, index ) )
break;
}
Indentation = index;
Text = input.Substring(index);
}
if ( index >= input.Length )
throw new FormatException($"Input string not correctly formatted ('{input}')");
public int Indentation{ get; }
Indentation = index;
Text = input.Substring( index );
}
public string Text{ get; }
public static CategoryLine[] Load( string path )
{
ArrayList list = new ArrayList();
public static CategoryLine[] Load(string path)
{
ArrayList list = new ArrayList();
if ( File.Exists( path ) )
{
using ( StreamReader ip = new StreamReader( path ) )
{
string line;
if (File.Exists(path))
using (StreamReader ip = new StreamReader(path))
{
string line;
while ( (line = ip.ReadLine()) != null )
list.Add( new CategoryLine( line ) );
}
}
while ((line = ip.ReadLine()) != null)
list.Add(new CategoryLine(line));
}
return (CategoryLine[])list.ToArray( typeof( CategoryLine ) );
}
}
}
return (CategoryLine[])list.ToArray(typeof(CategoryLine));
}
}
}