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

@ -4,139 +4,131 @@ using System.Collections.Generic;
namespace Server.Commands.Generic
{
public delegate BaseExtension ExtensionConstructor();
public delegate BaseExtension ExtensionConstructor();
public sealed class ExtensionInfo
{
public static Dictionary<string, ExtensionInfo> Table { get; } = new Dictionary<string, ExtensionInfo>( StringComparer.InvariantCultureIgnoreCase );
public sealed class ExtensionInfo
{
public ExtensionInfo(int order, string name, int size, ExtensionConstructor constructor)
{
Name = name;
Size = size;
public static void Register( ExtensionInfo ext )
{
Table[ext.Name] = ext;
}
Order = order;
public int Order { get; }
Constructor = constructor;
}
public string Name { get; }
public static Dictionary<string, ExtensionInfo> Table{ get; } =
new Dictionary<string, ExtensionInfo>(StringComparer.InvariantCultureIgnoreCase);
public int Size { get; }
public int Order{ get; }
public bool IsFixedSize => ( Size >= 0 );
public string Name{ get; }
public ExtensionConstructor Constructor { get; }
public int Size{ get; }
public ExtensionInfo( int order, string name, int size, ExtensionConstructor constructor )
{
Name = name;
Size = size;
public bool IsFixedSize => Size >= 0;
Order = order;
public ExtensionConstructor Constructor{ get; }
Constructor = constructor;
}
}
public static void Register(ExtensionInfo ext)
{
Table[ext.Name] = ext;
}
}
public sealed class Extensions : List<BaseExtension>
{
public Extensions()
{
}
public sealed class Extensions : List<BaseExtension>
{
public bool IsValid(object obj)
{
for (int i = 0; i < Count; ++i)
if (!this[i].IsValid(obj))
return false;
public bool IsValid( object obj )
{
for ( int i = 0; i < Count; ++i )
{
if ( !this[i].IsValid( obj ) )
return false;
}
return true;
}
return true;
}
public void Filter(ArrayList list)
{
for (int i = 0; i < Count; ++i)
this[i].Filter(list);
}
public void Filter( ArrayList list )
{
for ( int i = 0; i < Count; ++i )
this[i].Filter( list );
}
public static Extensions Parse(Mobile from, ref string[] args)
{
Extensions parsed = new Extensions();
public static Extensions Parse( Mobile from, ref string[] args )
{
Extensions parsed = new Extensions();
int size = args.Length;
int size = args.Length;
Type baseType = null;
Type baseType = null;
for (int i = args.Length - 1; i >= 0; --i)
{
if (!ExtensionInfo.Table.TryGetValue(args[i], out ExtensionInfo extInfo))
continue;
for ( int i = args.Length - 1; i >= 0; --i )
{
if ( !ExtensionInfo.Table.TryGetValue( args[i], out ExtensionInfo extInfo ) )
continue;
if (extInfo.IsFixedSize && i != size - extInfo.Size - 1)
throw new Exception("Invalid extended argument count.");
if ( extInfo.IsFixedSize && i != ( size - extInfo.Size - 1 ) )
throw new Exception( "Invalid extended argument count." );
BaseExtension ext = extInfo.Constructor();
BaseExtension ext = extInfo.Constructor();
ext.Parse(from, args, i + 1, size - i - 1);
ext.Parse( from, args, i + 1, size - i - 1 );
if (ext is WhereExtension extension)
baseType = extension.Conditional.Type;
if ( ext is WhereExtension extension )
baseType = extension.Conditional.Type;
parsed.Add(ext);
parsed.Add( ext );
size = i;
}
size = i;
}
parsed.Sort(delegate(BaseExtension a, BaseExtension b) { return a.Order - b.Order; });
parsed.Sort( delegate( BaseExtension a, BaseExtension b )
{
return ( a.Order - b.Order );
} );
AssemblyEmitter emitter = null;
AssemblyEmitter emitter = null;
foreach (BaseExtension update in parsed)
update.Optimize(from, baseType, ref emitter);
foreach ( BaseExtension update in parsed )
update.Optimize( from, baseType, ref emitter );
if (size != args.Length)
{
string[] old = args;
args = new string[size];
if ( size != args.Length )
{
string[] old = args;
args = new string[size];
for (int i = 0; i < args.Length; ++i)
args[i] = old[i];
}
for ( int i = 0; i < args.Length; ++i )
args[i] = old[i];
}
return parsed;
}
}
return parsed;
}
}
public abstract class BaseExtension
{
public abstract ExtensionInfo Info{ get; }
public abstract class BaseExtension
{
public abstract ExtensionInfo Info { get; }
public string Name => Info.Name;
public string Name => Info.Name;
public int Size => Info.Size;
public int Size => Info.Size;
public bool IsFixedSize => Info.IsFixedSize;
public bool IsFixedSize => Info.IsFixedSize;
public int Order => Info.Order;
public int Order => Info.Order;
public virtual void Optimize(Mobile from, Type baseType, ref AssemblyEmitter assembly)
{
}
public virtual void Optimize( Mobile from, Type baseType, ref AssemblyEmitter assembly )
{
}
public virtual void Parse(Mobile from, string[] arguments, int offset, int size)
{
}
public virtual void Parse( Mobile from, string[] arguments, int offset, int size )
{
}
public virtual bool IsValid(object obj)
{
return true;
}
public virtual bool IsValid( object obj )
{
return true;
}
public virtual void Filter( ArrayList list )
{
}
}
}
public virtual void Filter(ArrayList list)
{
}
}
}

View file

@ -6,242 +6,254 @@ using System.Reflection.Emit;
namespace Server.Commands.Generic
{
public static class DistinctCompiler
{
public static IComparer Compile( AssemblyEmitter assembly, Type objectType, Property[] props )
{
TypeBuilder typeBuilder = assembly.DefineType(
"__distinct",
TypeAttributes.Public,
typeof( object )
);
public static class DistinctCompiler
{
public static IComparer Compile(AssemblyEmitter assembly, Type objectType, Property[] props)
{
TypeBuilder typeBuilder = assembly.DefineType(
"__distinct",
TypeAttributes.Public,
typeof(object)
);
#region Constructor
{
ConstructorBuilder ctor = typeBuilder.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
Type.EmptyTypes
);
#region Constructor
ILGenerator il = ctor.GetILGenerator();
{
ConstructorBuilder ctor = typeBuilder.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
Type.EmptyTypes
);
// : base()
il.Emit( OpCodes.Ldarg_0 );
il.Emit( OpCodes.Call, typeof( object ).GetConstructor( Type.EmptyTypes ) );
ILGenerator il = ctor.GetILGenerator();
// return;
il.Emit( OpCodes.Ret );
}
#endregion
// : base()
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
#region IComparer
typeBuilder.AddInterfaceImplementation( typeof( IComparer ) );
// return;
il.Emit(OpCodes.Ret);
}
MethodBuilder compareMethod;
#endregion
#region Compare
{
MethodEmitter emitter = new MethodEmitter( typeBuilder );
#region IComparer
emitter.Define(
/* name */ "Compare",
/* attr */ MethodAttributes.Public | MethodAttributes.Virtual,
/* return */ typeof( int ),
/* params */ new[] { typeof( object ), typeof( object ) } );
typeBuilder.AddInterfaceImplementation(typeof(IComparer));
LocalBuilder a = emitter.CreateLocal( objectType );
LocalBuilder b = emitter.CreateLocal( objectType );
MethodBuilder compareMethod;
LocalBuilder v = emitter.CreateLocal( typeof( int ) );
#region Compare
emitter.LoadArgument( 1 );
emitter.CastAs( objectType );
emitter.StoreLocal( a );
{
MethodEmitter emitter = new MethodEmitter(typeBuilder);
emitter.LoadArgument( 2 );
emitter.CastAs( objectType );
emitter.StoreLocal( b );
emitter.Define(
/* name */ "Compare",
/* attr */ MethodAttributes.Public | MethodAttributes.Virtual,
/* return */ typeof(int),
/* params */ new[] { typeof(object), typeof(object) });
emitter.Load( 0 );
emitter.StoreLocal( v );
LocalBuilder a = emitter.CreateLocal(objectType);
LocalBuilder b = emitter.CreateLocal(objectType);
Label end = emitter.CreateLabel();
LocalBuilder v = emitter.CreateLocal(typeof(int));
for ( int i = 0; i < props.Length; ++i )
{
if ( i > 0 )
{
emitter.LoadLocal( v );
emitter.BranchIfTrue( end ); // if ( v != 0 ) return v;
}
emitter.LoadArgument(1);
emitter.CastAs(objectType);
emitter.StoreLocal(a);
Property prop = props[i];
emitter.LoadArgument(2);
emitter.CastAs(objectType);
emitter.StoreLocal(b);
emitter.LoadLocal( a );
emitter.Chain( prop );
emitter.Load(0);
emitter.StoreLocal(v);
bool couldCompare =
emitter.CompareTo( 1, delegate
{
emitter.LoadLocal( b );
emitter.Chain( prop );
} );
Label end = emitter.CreateLabel();
if ( !couldCompare )
throw new InvalidOperationException( "Property is not comparable." );
for (int i = 0; i < props.Length; ++i)
{
if (i > 0)
{
emitter.LoadLocal(v);
emitter.BranchIfTrue(end); // if ( v != 0 ) return v;
}
emitter.StoreLocal( v );
}
Property prop = props[i];
emitter.MarkLabel( end );
emitter.LoadLocal(a);
emitter.Chain(prop);
emitter.LoadLocal( v );
emitter.Return();
bool couldCompare =
emitter.CompareTo(1, delegate
{
emitter.LoadLocal(b);
emitter.Chain(prop);
});
typeBuilder.DefineMethodOverride(
emitter.Method,
typeof( IComparer ).GetMethod(
"Compare",
new[]
{
typeof( object ),
typeof( object )
}
)
);
if (!couldCompare)
throw new InvalidOperationException("Property is not comparable.");
compareMethod = emitter.Method;
}
#endregion
#endregion
emitter.StoreLocal(v);
}
#region IEqualityComparer
typeBuilder.AddInterfaceImplementation( typeof( IEqualityComparer<object> ) );
emitter.MarkLabel(end);
#region Equals
{
MethodEmitter emitter = new MethodEmitter( typeBuilder );
emitter.LoadLocal(v);
emitter.Return();
emitter.Define(
/* name */ "Equals",
/* attr */ MethodAttributes.Public | MethodAttributes.Virtual,
/* return */ typeof( bool ),
/* params */ new[] { typeof( object ), typeof( object ) } );
typeBuilder.DefineMethodOverride(
emitter.Method,
typeof(IComparer).GetMethod(
"Compare",
new[]
{
typeof(object),
typeof(object)
}
)
);
emitter.Generator.Emit( OpCodes.Ldarg_0 );
emitter.Generator.Emit( OpCodes.Ldarg_1 );
emitter.Generator.Emit( OpCodes.Ldarg_2 );
compareMethod = emitter.Method;
}
emitter.Generator.Emit( OpCodes.Call, compareMethod );
#endregion
emitter.Generator.Emit( OpCodes.Ldc_I4_0 );
#endregion
emitter.Generator.Emit( OpCodes.Ceq );
#region IEqualityComparer
emitter.Generator.Emit( OpCodes.Ret );
typeBuilder.AddInterfaceImplementation(typeof(IEqualityComparer<object>));
typeBuilder.DefineMethodOverride(
emitter.Method,
typeof( IEqualityComparer<object> ).GetMethod(
"Equals",
new[]
{
typeof( object ),
typeof( object )
}
)
);
}
#endregion
#region Equals
#region GetHashCode
{
MethodEmitter emitter = new MethodEmitter( typeBuilder );
{
MethodEmitter emitter = new MethodEmitter(typeBuilder);
emitter.Define(
/* name */ "GetHashCode",
/* attr */ MethodAttributes.Public | MethodAttributes.Virtual,
/* return */ typeof( int ),
/* params */ new[] { typeof( object ) } );
emitter.Define(
/* name */ "Equals",
/* attr */ MethodAttributes.Public | MethodAttributes.Virtual,
/* return */ typeof(bool),
/* params */ new[] { typeof(object), typeof(object) });
LocalBuilder obj = emitter.CreateLocal( objectType );
emitter.Generator.Emit(OpCodes.Ldarg_0);
emitter.Generator.Emit(OpCodes.Ldarg_1);
emitter.Generator.Emit(OpCodes.Ldarg_2);
emitter.LoadArgument( 1 );
emitter.CastAs( objectType );
emitter.StoreLocal( obj );
emitter.Generator.Emit(OpCodes.Call, compareMethod);
for ( int i = 0; i < props.Length; ++i )
{
Property prop = props[i];
emitter.Generator.Emit(OpCodes.Ldc_I4_0);
emitter.LoadLocal( obj );
emitter.Chain( prop );
emitter.Generator.Emit(OpCodes.Ceq);
Type active = emitter.Active;
emitter.Generator.Emit(OpCodes.Ret);
MethodInfo getHashCode = active.GetMethod( "GetHashCode", Type.EmptyTypes );
typeBuilder.DefineMethodOverride(
emitter.Method,
typeof(IEqualityComparer<object>).GetMethod(
"Equals",
new[]
{
typeof(object),
typeof(object)
}
)
);
}
if ( getHashCode == null )
getHashCode = typeof( object ).GetMethod( "GetHashCode", Type.EmptyTypes );
#endregion
if ( active != typeof( int ) )
{
if ( !active.IsValueType )
{
LocalBuilder value = emitter.AcquireTemp( active );
#region GetHashCode
Label valueNotNull = emitter.CreateLabel();
Label done = emitter.CreateLabel();
{
MethodEmitter emitter = new MethodEmitter(typeBuilder);
emitter.StoreLocal( value );
emitter.LoadLocal( value );
emitter.Define(
/* name */ "GetHashCode",
/* attr */ MethodAttributes.Public | MethodAttributes.Virtual,
/* return */ typeof(int),
/* params */ new[] { typeof(object) });
emitter.BranchIfTrue( valueNotNull );
LocalBuilder obj = emitter.CreateLocal(objectType);
emitter.Load( 0 );
emitter.Pop( typeof( int ) );
emitter.LoadArgument(1);
emitter.CastAs(objectType);
emitter.StoreLocal(obj);
emitter.Branch( done );
for (int i = 0; i < props.Length; ++i)
{
Property prop = props[i];
emitter.MarkLabel( valueNotNull );
emitter.LoadLocal(obj);
emitter.Chain(prop);
emitter.LoadLocal( value );
emitter.Call( getHashCode );
Type active = emitter.Active;
emitter.ReleaseTemp( value );
MethodInfo getHashCode = active.GetMethod("GetHashCode", Type.EmptyTypes);
emitter.MarkLabel( done );
}
else
{
emitter.Call( getHashCode );
}
}
if (getHashCode == null)
getHashCode = typeof(object).GetMethod("GetHashCode", Type.EmptyTypes);
if ( i > 0 )
emitter.Xor();
}
if (active != typeof(int))
{
if (!active.IsValueType)
{
LocalBuilder value = emitter.AcquireTemp(active);
emitter.Return();
Label valueNotNull = emitter.CreateLabel();
Label done = emitter.CreateLabel();
typeBuilder.DefineMethodOverride(
emitter.Method,
typeof( IEqualityComparer<object> ).GetMethod(
"GetHashCode",
new[]
{
typeof( object )
}
)
);
}
#endregion
#endregion
emitter.StoreLocal(value);
emitter.LoadLocal(value);
Type comparerType = typeBuilder.CreateType();
emitter.BranchIfTrue(valueNotNull);
return (IComparer) Activator.CreateInstance( comparerType );
}
}
}
emitter.Load(0);
emitter.Pop(typeof(int));
emitter.Branch(done);
emitter.MarkLabel(valueNotNull);
emitter.LoadLocal(value);
emitter.Call(getHashCode);
emitter.ReleaseTemp(value);
emitter.MarkLabel(done);
}
else
{
emitter.Call(getHashCode);
}
}
if (i > 0)
emitter.Xor();
}
emitter.Return();
typeBuilder.DefineMethodOverride(
emitter.Method,
typeof(IEqualityComparer<object>).GetMethod(
"GetHashCode",
new[]
{
typeof(object)
}
)
);
}
#endregion
#endregion
Type comparerType = typeBuilder.CreateType();
return (IComparer)Activator.CreateInstance(comparerType);
}
}
}

View file

@ -5,160 +5,166 @@ using System.Reflection.Emit;
namespace Server.Commands.Generic
{
public sealed class OrderInfo
{
private int m_Order;
public sealed class OrderInfo
{
private int m_Order;
public Property Property { get; set; }
public OrderInfo(Property property, bool isAscending)
{
Property = property;
public bool IsAscending
{
get => ( m_Order > 0 );
set => m_Order = ( value ? +1 : -1 );
}
IsAscending = isAscending;
}
public bool IsDescending
{
get => ( m_Order < 0 );
set => m_Order = ( value ? -1 : +1 );
}
public Property Property{ get; set; }
public int Sign
{
get => Math.Sign( m_Order );
set
{
m_Order = Math.Sign( value );
public bool IsAscending
{
get => m_Order > 0;
set => m_Order = value ? +1 : -1;
}
if ( m_Order == 0 )
throw new InvalidOperationException( "Sign cannot be zero." );
}
}
public bool IsDescending
{
get => m_Order < 0;
set => m_Order = value ? -1 : +1;
}
public OrderInfo( Property property, bool isAscending )
{
Property = property;
public int Sign
{
get => Math.Sign(m_Order);
set
{
m_Order = Math.Sign(value);
IsAscending = isAscending;
}
}
if (m_Order == 0)
throw new InvalidOperationException("Sign cannot be zero.");
}
}
}
public static class SortCompiler
{
public static IComparer Compile( AssemblyEmitter assembly, Type objectType, OrderInfo[] orders )
{
TypeBuilder typeBuilder = assembly.DefineType(
"__sort",
TypeAttributes.Public,
typeof( object )
);
public static class SortCompiler
{
public static IComparer Compile(AssemblyEmitter assembly, Type objectType, OrderInfo[] orders)
{
TypeBuilder typeBuilder = assembly.DefineType(
"__sort",
TypeAttributes.Public,
typeof(object)
);
#region Constructor
{
ConstructorBuilder ctor = typeBuilder.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
Type.EmptyTypes
);
#region Constructor
ILGenerator il = ctor.GetILGenerator();
{
ConstructorBuilder ctor = typeBuilder.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
Type.EmptyTypes
);
// : base()
il.Emit( OpCodes.Ldarg_0 );
il.Emit( OpCodes.Call, typeof( object ).GetConstructor( Type.EmptyTypes ) );
ILGenerator il = ctor.GetILGenerator();
// return;
il.Emit( OpCodes.Ret );
}
#endregion
// : base()
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
#region IComparer
typeBuilder.AddInterfaceImplementation( typeof( IComparer ) );
// return;
il.Emit(OpCodes.Ret);
}
MethodBuilder compareMethod;
#endregion
#region Compare
{
MethodEmitter emitter = new MethodEmitter( typeBuilder );
#region IComparer
emitter.Define(
/* name */ "Compare",
/* attr */ MethodAttributes.Public | MethodAttributes.Virtual,
/* return */ typeof( int ),
/* params */ new[] { typeof( object ), typeof( object ) } );
typeBuilder.AddInterfaceImplementation(typeof(IComparer));
LocalBuilder a = emitter.CreateLocal( objectType );
LocalBuilder b = emitter.CreateLocal( objectType );
MethodBuilder compareMethod;
LocalBuilder v = emitter.CreateLocal( typeof( int ) );
#region Compare
emitter.LoadArgument( 1 );
emitter.CastAs( objectType );
emitter.StoreLocal( a );
{
MethodEmitter emitter = new MethodEmitter(typeBuilder);
emitter.LoadArgument( 2 );
emitter.CastAs( objectType );
emitter.StoreLocal( b );
emitter.Define(
/* name */ "Compare",
/* attr */ MethodAttributes.Public | MethodAttributes.Virtual,
/* return */ typeof(int),
/* params */ new[] { typeof(object), typeof(object) });
emitter.Load( 0 );
emitter.StoreLocal( v );
LocalBuilder a = emitter.CreateLocal(objectType);
LocalBuilder b = emitter.CreateLocal(objectType);
Label end = emitter.CreateLabel();
LocalBuilder v = emitter.CreateLocal(typeof(int));
for ( int i = 0; i < orders.Length; ++i )
{
if ( i > 0 )
{
emitter.LoadLocal( v );
emitter.BranchIfTrue( end ); // if ( v != 0 ) return v;
}
emitter.LoadArgument(1);
emitter.CastAs(objectType);
emitter.StoreLocal(a);
OrderInfo orderInfo = orders[i];
emitter.LoadArgument(2);
emitter.CastAs(objectType);
emitter.StoreLocal(b);
Property prop = orderInfo.Property;
int sign = orderInfo.Sign;
emitter.Load(0);
emitter.StoreLocal(v);
emitter.LoadLocal( a );
emitter.Chain( prop );
Label end = emitter.CreateLabel();
bool couldCompare =
emitter.CompareTo( sign, delegate
{
emitter.LoadLocal( b );
emitter.Chain( prop );
} );
for (int i = 0; i < orders.Length; ++i)
{
if (i > 0)
{
emitter.LoadLocal(v);
emitter.BranchIfTrue(end); // if ( v != 0 ) return v;
}
if ( !couldCompare )
throw new InvalidOperationException( "Property is not comparable." );
OrderInfo orderInfo = orders[i];
emitter.StoreLocal( v );
}
Property prop = orderInfo.Property;
int sign = orderInfo.Sign;
emitter.MarkLabel( end );
emitter.LoadLocal(a);
emitter.Chain(prop);
emitter.LoadLocal( v );
emitter.Return();
bool couldCompare =
emitter.CompareTo(sign, delegate
{
emitter.LoadLocal(b);
emitter.Chain(prop);
});
typeBuilder.DefineMethodOverride(
emitter.Method,
typeof( IComparer ).GetMethod(
"Compare",
new[]
{
typeof( object ),
typeof( object )
}
)
);
if (!couldCompare)
throw new InvalidOperationException("Property is not comparable.");
compareMethod = emitter.Method;
}
#endregion
#endregion
emitter.StoreLocal(v);
}
Type comparerType = typeBuilder.CreateType();
emitter.MarkLabel(end);
return (IComparer) Activator.CreateInstance( comparerType );
}
}
}
emitter.LoadLocal(v);
emitter.Return();
typeBuilder.DefineMethodOverride(
emitter.Method,
typeof(IComparer).GetMethod(
"Compare",
new[]
{
typeof(object),
typeof(object)
}
)
);
compareMethod = emitter.Method;
}
#endregion
#endregion
Type comparerType = typeBuilder.CreateType();
return (IComparer)Activator.CreateInstance(comparerType);
}
}
}

View file

@ -4,81 +4,82 @@ using System.Collections.Generic;
namespace Server.Commands.Generic
{
public sealed class DistinctExtension : BaseExtension
{
public static ExtensionInfo ExtInfo = new ExtensionInfo( 30, "Distinct", -1, delegate { return new DistinctExtension(); } );
public sealed class DistinctExtension : BaseExtension
{
public static ExtensionInfo ExtInfo =
new ExtensionInfo(30, "Distinct", -1, delegate { return new DistinctExtension(); });
public static void Initialize()
{
ExtensionInfo.Register( ExtInfo );
}
private IComparer m_Comparer;
public override ExtensionInfo Info => ExtInfo;
private List<Property> m_Properties;
private List<Property> m_Properties;
public DistinctExtension()
{
m_Properties = new List<Property>();
}
private IComparer m_Comparer;
public override ExtensionInfo Info => ExtInfo;
public DistinctExtension()
{
m_Properties = new List<Property>();
}
public static void Initialize()
{
ExtensionInfo.Register(ExtInfo);
}
public override void Optimize( Mobile from, Type baseType, ref AssemblyEmitter assembly )
{
if ( baseType == null )
throw new Exception( "Distinct extension may only be used in combination with an object conditional." );
public override void Optimize(Mobile from, Type baseType, ref AssemblyEmitter assembly)
{
if (baseType == null)
throw new Exception("Distinct extension may only be used in combination with an object conditional.");
foreach ( Property prop in m_Properties )
{
prop.BindTo( baseType, PropertyAccess.Read );
prop.CheckAccess( from );
}
foreach (Property prop in m_Properties)
{
prop.BindTo(baseType, PropertyAccess.Read);
prop.CheckAccess(from);
}
if ( assembly == null )
assembly = new AssemblyEmitter( "__dynamic", false );
if (assembly == null)
assembly = new AssemblyEmitter("__dynamic", false);
m_Comparer = DistinctCompiler.Compile( assembly, baseType, m_Properties.ToArray() );
}
m_Comparer = DistinctCompiler.Compile(assembly, baseType, m_Properties.ToArray());
}
public override void Parse( Mobile from, string[] arguments, int offset, int size )
{
if ( size < 1 )
throw new Exception( "Invalid distinction syntax." );
public override void Parse(Mobile from, string[] arguments, int offset, int size)
{
if (size < 1)
throw new Exception("Invalid distinction syntax.");
int end = offset + size;
int end = offset + size;
while ( offset < end )
{
string binding = arguments[offset++];
while (offset < end)
{
string binding = arguments[offset++];
m_Properties.Add( new Property( binding ) );
}
}
m_Properties.Add(new Property(binding));
}
}
public override void Filter( ArrayList list )
{
if ( m_Comparer == null )
throw new InvalidOperationException( "The extension must first be optimized." );
public override void Filter(ArrayList list)
{
if (m_Comparer == null)
throw new InvalidOperationException("The extension must first be optimized.");
ArrayList copy = new ArrayList( list );
ArrayList copy = new ArrayList(list);
copy.Sort( m_Comparer );
copy.Sort(m_Comparer);
list.Clear();
list.Clear();
object last = null;
object last = null;
for ( int i = 0; i < copy.Count; ++i )
{
object obj = copy[i];
for (int i = 0; i < copy.Count; ++i)
{
object obj = copy[i];
if ( last == null || m_Comparer.Compare( obj, last ) != 0 )
{
list.Add( obj );
last = obj;
}
}
}
}
}
if (last == null || m_Comparer.Compare(obj, last) != 0)
{
list.Add(obj);
last = obj;
}
}
}
}
}

View file

@ -3,35 +3,31 @@ using System.Collections;
namespace Server.Commands.Generic
{
public sealed class LimitExtension : BaseExtension
{
public static ExtensionInfo ExtInfo = new ExtensionInfo( 80, "Limit", 1, delegate { return new LimitExtension(); } );
public sealed class LimitExtension : BaseExtension
{
public static ExtensionInfo ExtInfo = new ExtensionInfo(80, "Limit", 1, delegate { return new LimitExtension(); });
public static void Initialize()
{
ExtensionInfo.Register( ExtInfo );
}
public override ExtensionInfo Info => ExtInfo;
public override ExtensionInfo Info => ExtInfo;
public int Limit{ get; private set; }
public int Limit { get; private set; }
public static void Initialize()
{
ExtensionInfo.Register(ExtInfo);
}
public LimitExtension()
{
}
public override void Parse(Mobile from, string[] arguments, int offset, int size)
{
Limit = Utility.ToInt32(arguments[offset]);
public override void Parse( Mobile from, string[] arguments, int offset, int size )
{
Limit = Utility.ToInt32( arguments[offset] );
if (Limit < 0)
throw new Exception("Limit cannot be less than zero.");
}
if ( Limit < 0 )
throw new Exception( "Limit cannot be less than zero." );
}
public override void Filter( ArrayList list )
{
if ( list.Count > Limit )
list.RemoveRange( Limit, list.Count - Limit );
}
}
}
public override void Filter(ArrayList list)
{
if (list.Count > Limit)
list.RemoveRange(Limit, list.Count - Limit);
}
}
}

View file

@ -4,101 +4,101 @@ using System.Collections.Generic;
namespace Server.Commands.Generic
{
public sealed class SortExtension : BaseExtension
{
public static ExtensionInfo ExtInfo = new ExtensionInfo( 40, "Order", -1, delegate { return new SortExtension(); } );
public sealed class SortExtension : BaseExtension
{
public static ExtensionInfo ExtInfo = new ExtensionInfo(40, "Order", -1, delegate { return new SortExtension(); });
public static void Initialize()
{
ExtensionInfo.Register( ExtInfo );
}
private IComparer m_Comparer;
public override ExtensionInfo Info => ExtInfo;
private List<OrderInfo> m_Orders;
private List<OrderInfo> m_Orders;
public SortExtension()
{
m_Orders = new List<OrderInfo>();
}
private IComparer m_Comparer;
public override ExtensionInfo Info => ExtInfo;
public SortExtension()
{
m_Orders = new List<OrderInfo>();
}
public static void Initialize()
{
ExtensionInfo.Register(ExtInfo);
}
public override void Optimize( Mobile from, Type baseType, ref AssemblyEmitter assembly )
{
if ( baseType == null )
throw new Exception( "The ordering extension may only be used in combination with an object conditional." );
public override void Optimize(Mobile from, Type baseType, ref AssemblyEmitter assembly)
{
if (baseType == null)
throw new Exception("The ordering extension may only be used in combination with an object conditional.");
foreach ( OrderInfo order in m_Orders )
{
order.Property.BindTo( baseType, PropertyAccess.Read );
order.Property.CheckAccess( from );
}
foreach (OrderInfo order in m_Orders)
{
order.Property.BindTo(baseType, PropertyAccess.Read);
order.Property.CheckAccess(from);
}
if ( assembly == null )
assembly = new AssemblyEmitter( "__dynamic", false );
if (assembly == null)
assembly = new AssemblyEmitter("__dynamic", false);
m_Comparer = SortCompiler.Compile( assembly, baseType, m_Orders.ToArray() );
}
m_Comparer = SortCompiler.Compile(assembly, baseType, m_Orders.ToArray());
}
public override void Parse( Mobile from, string[] arguments, int offset, int size )
{
if ( size < 1 )
throw new Exception( "Invalid ordering syntax." );
public override void Parse(Mobile from, string[] arguments, int offset, int size)
{
if (size < 1)
throw new Exception("Invalid ordering syntax.");
if ( Insensitive.Equals( arguments[offset], "by" ) )
{
++offset;
--size;
if (Insensitive.Equals(arguments[offset], "by"))
{
++offset;
--size;
if ( size < 1 )
throw new Exception( "Invalid ordering syntax." );
}
if (size < 1)
throw new Exception("Invalid ordering syntax.");
}
int end = offset + size;
int end = offset + size;
while ( offset < end )
{
string binding = arguments[offset++];
while (offset < end)
{
string binding = arguments[offset++];
bool isAscending = true;
bool isAscending = true;
if ( offset < end )
{
string next = arguments[offset];
if (offset < end)
{
string next = arguments[offset];
switch ( next.ToLower() )
{
case "+":
case "up":
case "asc":
case "ascending":
isAscending = true;
++offset;
break;
switch (next.ToLower())
{
case "+":
case "up":
case "asc":
case "ascending":
isAscending = true;
++offset;
break;
case "-":
case "down":
case "desc":
case "descending":
isAscending = false;
++offset;
break;
}
}
case "-":
case "down":
case "desc":
case "descending":
isAscending = false;
++offset;
break;
}
}
Property property = new Property( binding );
Property property = new Property(binding);
m_Orders.Add( new OrderInfo( property, isAscending ) );
}
}
m_Orders.Add(new OrderInfo(property, isAscending));
}
}
public override void Filter( ArrayList list )
{
if ( m_Comparer == null )
throw new InvalidOperationException( "The extension must first be optimized." );
public override void Filter(ArrayList list)
{
if (m_Comparer == null)
throw new InvalidOperationException("The extension must first be optimized.");
list.Sort( m_Comparer );
}
}
}
list.Sort(m_Comparer);
}
}
}

View file

@ -2,42 +2,38 @@ using System;
namespace Server.Commands.Generic
{
public sealed class WhereExtension : BaseExtension
{
public static ExtensionInfo ExtInfo = new ExtensionInfo( 20, "Where", -1, delegate { return new WhereExtension(); } );
public sealed class WhereExtension : BaseExtension
{
public static ExtensionInfo ExtInfo = new ExtensionInfo(20, "Where", -1, delegate { return new WhereExtension(); });
public static void Initialize()
{
ExtensionInfo.Register( ExtInfo );
}
public override ExtensionInfo Info => ExtInfo;
public override ExtensionInfo Info => ExtInfo;
public ObjectConditional Conditional{ get; private set; }
public ObjectConditional Conditional { get; private set; }
public static void Initialize()
{
ExtensionInfo.Register(ExtInfo);
}
public WhereExtension()
{
}
public override void Optimize(Mobile from, Type baseType, ref AssemblyEmitter assembly)
{
if (baseType == null)
throw new InvalidOperationException("Insanity.");
public override void Optimize( Mobile from, Type baseType, ref AssemblyEmitter assembly )
{
if ( baseType == null )
throw new InvalidOperationException( "Insanity." );
Conditional.Compile(ref assembly);
}
Conditional.Compile( ref assembly );
}
public override void Parse(Mobile from, string[] arguments, int offset, int size)
{
if (size < 1)
throw new Exception("Invalid condition syntax.");
public override void Parse( Mobile from, string[] arguments, int offset, int size )
{
if ( size < 1 )
throw new Exception( "Invalid condition syntax." );
Conditional = ObjectConditional.ParseDirect(from, arguments, offset, size);
}
Conditional = ObjectConditional.ParseDirect( from, arguments, offset, size );
}
public override bool IsValid( object obj )
{
return Conditional.CheckCondition( obj );
}
}
}
public override bool IsValid(object obj)
{
return Conditional.CheckCondition(obj);
}
}
}