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,245 +4,247 @@ using CPA = Server.CommandPropertyAttribute;
namespace Server.Commands.Generic
{
public sealed class ObjectConditional
{
private static readonly Type typeofItem = typeof( Item );
private static readonly Type typeofMobile = typeof( Mobile );
public sealed class ObjectConditional
{
private static readonly Type typeofItem = typeof(Item);
private static readonly Type typeofMobile = typeof(Mobile);
private ICondition[][] m_Conditions;
public static readonly ObjectConditional Empty = new ObjectConditional(null, null);
private IConditional[] m_Conditionals;
private IConditional[] m_Conditionals;
public Type Type { get; }
private ICondition[][] m_Conditions;
public bool IsItem => ( Type == null || Type == typeofItem || Type.IsSubclassOf( typeofItem ) );
public ObjectConditional(Type objectType, ICondition[][] conditions)
{
Type = objectType;
m_Conditions = conditions;
}
public bool IsMobile => ( Type == null || Type == typeofMobile || Type.IsSubclassOf( typeofMobile ) );
public Type Type{ get; }
public static readonly ObjectConditional Empty = new ObjectConditional( null, null );
public bool IsItem => Type == null || Type == typeofItem || Type.IsSubclassOf(typeofItem);
public bool HasCompiled => ( m_Conditionals != null );
public bool IsMobile => Type == null || Type == typeofMobile || Type.IsSubclassOf(typeofMobile);
public void Compile( ref AssemblyEmitter emitter )
{
if ( emitter == null )
emitter = new AssemblyEmitter( "__dynamic", false );
public bool HasCompiled => m_Conditionals != null;
m_Conditionals = new IConditional[m_Conditions.Length];
public void Compile(ref AssemblyEmitter emitter)
{
if (emitter == null)
emitter = new AssemblyEmitter("__dynamic", false);
for ( int i = 0; i < m_Conditionals.Length; ++i )
m_Conditionals[i] = ConditionalCompiler.Compile( emitter, Type, m_Conditions[i], i );
}
m_Conditionals = new IConditional[m_Conditions.Length];
public bool CheckCondition( object obj )
{
if ( Type == null )
return true; // null type means no condition
for (int i = 0; i < m_Conditionals.Length; ++i)
m_Conditionals[i] = ConditionalCompiler.Compile(emitter, Type, m_Conditions[i], i);
}
if ( !HasCompiled )
{
AssemblyEmitter emitter = null;
public bool CheckCondition(object obj)
{
if (Type == null)
return true; // null type means no condition
Compile( ref emitter );
}
if (!HasCompiled)
{
AssemblyEmitter emitter = null;
for ( int i = 0; i < m_Conditionals.Length; ++i )
{
if ( m_Conditionals[i].Verify( obj ) )
return true;
}
Compile(ref emitter);
}
return false; // all conditions false
}
for (int i = 0; i < m_Conditionals.Length; ++i)
if (m_Conditionals[i].Verify(obj))
return true;
public static ObjectConditional Parse( Mobile from, ref string[] args )
{
string[] conditionArgs = null;
return false; // all conditions false
}
for ( int i = 0; i < args.Length; ++i )
{
if ( Insensitive.Equals( args[i], "where" ) )
{
string[] origArgs = args;
public static ObjectConditional Parse(Mobile from, ref string[] args)
{
string[] conditionArgs = null;
args = new string[i];
for (int i = 0; i < args.Length; ++i)
if (Insensitive.Equals(args[i], "where"))
{
string[] origArgs = args;
for ( int j = 0; j < args.Length; ++j )
args[j] = origArgs[j];
args = new string[i];
conditionArgs = new string[origArgs.Length - i - 1];
for (int j = 0; j < args.Length; ++j)
args[j] = origArgs[j];
for ( int j = 0; j < conditionArgs.Length; ++j )
conditionArgs[j] = origArgs[i + j + 1];
conditionArgs = new string[origArgs.Length - i - 1];
break;
}
}
for (int j = 0; j < conditionArgs.Length; ++j)
conditionArgs[j] = origArgs[i + j + 1];
return ParseDirect( from, conditionArgs, 0, conditionArgs.Length );
}
break;
}
public static ObjectConditional ParseDirect( Mobile from, string[] args, int offset, int size )
{
if ( args == null || size == 0 )
return Empty;
return ParseDirect(from, conditionArgs, 0, conditionArgs.Length);
}
int index = 0;
public static ObjectConditional ParseDirect(Mobile from, string[] args, int offset, int size)
{
if (args == null || size == 0)
return Empty;
Type objectType = ScriptCompiler.FindTypeByName( args[offset + index], true );
int index = 0;
if ( objectType == null )
throw new Exception($"No type with that name ({args[offset + index]}) was found.");
Type objectType = ScriptCompiler.FindTypeByName(args[offset + index], true);
++index;
if (objectType == null)
throw new Exception($"No type with that name ({args[offset + index]}) was found.");
List<ICondition[]> conditions = new List<ICondition[]>();
List<ICondition> current = new List<ICondition>();
++index;
current.Add( TypeCondition.Default );
List<ICondition[]> conditions = new List<ICondition[]>();
List<ICondition> current = new List<ICondition>();
while ( index < size )
{
string cur = args[offset + index];
current.Add(TypeCondition.Default);
bool inverse = false;
while (index < size)
{
string cur = args[offset + index];
if ( Insensitive.Equals( cur, "not" ) || cur == "!" )
{
inverse = true;
++index;
bool inverse = false;
if ( index >= size )
throw new Exception( "Improperly formatted object conditional." );
}
else if ( Insensitive.Equals( cur, "or" ) || cur == "||" )
{
if ( current.Count > 1 )
{
conditions.Add( current.ToArray() );
if (Insensitive.Equals(cur, "not") || cur == "!")
{
inverse = true;
++index;
current.Clear();
current.Add( TypeCondition.Default );
}
if (index >= size)
throw new Exception("Improperly formatted object conditional.");
}
else if (Insensitive.Equals(cur, "or") || cur == "||")
{
if (current.Count > 1)
{
conditions.Add(current.ToArray());
++index;
current.Clear();
current.Add(TypeCondition.Default);
}
continue;
}
++index;
string binding = args[offset + index];
index++;
continue;
}
if ( index >= size )
throw new Exception( "Improperly formatted object conditional." );
string binding = args[offset + index];
index++;
string oper = args[offset + index];
index++;
if (index >= size)
throw new Exception("Improperly formatted object conditional.");
if ( index >= size )
throw new Exception( "Improperly formatted object conditional." );
string oper = args[offset + index];
index++;
string val = args[offset + index];
index++;
if (index >= size)
throw new Exception("Improperly formatted object conditional.");
Property prop = new Property( binding );
string val = args[offset + index];
index++;
prop.BindTo( objectType, PropertyAccess.Read );
prop.CheckAccess( from );
Property prop = new Property(binding);
ICondition condition = null;
prop.BindTo(objectType, PropertyAccess.Read);
prop.CheckAccess(from);
switch ( oper )
{
#region Equality
case "=":
case "==":
case "is":
condition = new ComparisonCondition( prop, inverse, ComparisonOperator.Equal, val );
break;
ICondition condition = null;
case "!=":
condition = new ComparisonCondition( prop, inverse, ComparisonOperator.NotEqual, val );
break;
#endregion
switch (oper)
{
#region Equality
#region Relational
case ">":
condition = new ComparisonCondition( prop, inverse, ComparisonOperator.Greater, val );
break;
case "=":
case "==":
case "is":
condition = new ComparisonCondition(prop, inverse, ComparisonOperator.Equal, val);
break;
case "<":
condition = new ComparisonCondition( prop, inverse, ComparisonOperator.Lesser, val );
break;
case "!=":
condition = new ComparisonCondition(prop, inverse, ComparisonOperator.NotEqual, val);
break;
case ">=":
condition = new ComparisonCondition( prop, inverse, ComparisonOperator.GreaterEqual, val );
break;
#endregion
case "<=":
condition = new ComparisonCondition( prop, inverse, ComparisonOperator.LesserEqual, val );
break;
#endregion
#region Relational
#region Strings
case "==~":
case "~==":
case "=~":
case "~=":
case "is~":
case "~is":
condition = new StringCondition( prop, inverse, StringOperator.Equal, val, true );
break;
case ">":
condition = new ComparisonCondition(prop, inverse, ComparisonOperator.Greater, val);
break;
case "!=~":
case "~!=":
condition = new StringCondition( prop, inverse, StringOperator.NotEqual, val, true );
break;
case "<":
condition = new ComparisonCondition(prop, inverse, ComparisonOperator.Lesser, val);
break;
case "starts":
condition = new StringCondition( prop, inverse, StringOperator.StartsWith, val, false );
break;
case ">=":
condition = new ComparisonCondition(prop, inverse, ComparisonOperator.GreaterEqual, val);
break;
case "starts~":
case "~starts":
condition = new StringCondition( prop, inverse, StringOperator.StartsWith, val, true );
break;
case "<=":
condition = new ComparisonCondition(prop, inverse, ComparisonOperator.LesserEqual, val);
break;
case "ends":
condition = new StringCondition( prop, inverse, StringOperator.EndsWith, val, false );
break;
#endregion
case "ends~":
case "~ends":
condition = new StringCondition( prop, inverse, StringOperator.EndsWith, val, true );
break;
#region Strings
case "contains":
condition = new StringCondition( prop, inverse, StringOperator.Contains, val, false );
break;
case "==~":
case "~==":
case "=~":
case "~=":
case "is~":
case "~is":
condition = new StringCondition(prop, inverse, StringOperator.Equal, val, true);
break;
case "contains~":
case "~contains":
condition = new StringCondition( prop, inverse, StringOperator.Contains, val, true );
break;
#endregion
}
case "!=~":
case "~!=":
condition = new StringCondition(prop, inverse, StringOperator.NotEqual, val, true);
break;
if ( condition == null )
throw new InvalidOperationException($"Unrecognized operator (\"{oper}\").");
case "starts":
condition = new StringCondition(prop, inverse, StringOperator.StartsWith, val, false);
break;
current.Add( condition );
}
case "starts~":
case "~starts":
condition = new StringCondition(prop, inverse, StringOperator.StartsWith, val, true);
break;
conditions.Add( current.ToArray() );
case "ends":
condition = new StringCondition(prop, inverse, StringOperator.EndsWith, val, false);
break;
return new ObjectConditional( objectType, conditions.ToArray() );
}
case "ends~":
case "~ends":
condition = new StringCondition(prop, inverse, StringOperator.EndsWith, val, true);
break;
public ObjectConditional( Type objectType, ICondition[][] conditions )
{
Type = objectType;
m_Conditions = conditions;
}
}
case "contains":
condition = new StringCondition(prop, inverse, StringOperator.Contains, val, false);
break;
case "contains~":
case "~contains":
condition = new StringCondition(prop, inverse, StringOperator.Contains, val, true);
break;
#endregion
}
if (condition == null)
throw new InvalidOperationException($"Unrecognized operator (\"{oper}\").");
current.Add(condition);
}
conditions.Add(current.ToArray());
return new ObjectConditional(objectType, conditions.ToArray());
}
}
}