public static ISpawnable Build(Type type, string[] args)
{
bool isISpawnable = typeof(ISpawnable).IsAssignableFrom(type);
if (!isISpawnable)
{
return null;
}
Add.FixArgs(ref args);
string[, ] props = null;
for (int i = 0; i < args.Length; ++i)
{
if (Insensitive.Equals(args[i], "set"))
{
int remains = args.Length - i - 1;
if (remains >= 2)
{
props = new string[remains / 2, 2];
remains /= 2;
for (int j = 0; j < remains; ++j)
{
props[j, 0] = args[i + (j * 2) + 1];
props[j, 1] = args[i + (j * 2) + 2];
}
Add.FixSetString(ref args, i);
}
break;
}
}
PropertyInfo[] realProps = null;
if (props != null)
{
realProps = new PropertyInfo[props.GetLength(0)];
PropertyInfo[] allProps = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
for (int i = 0; i < realProps.Length; ++i)
{
PropertyInfo thisProp = null;
string propName = props[i, 0];
for (int j = 0; thisProp == null && j < allProps.Length; ++j)
{
if (Insensitive.Equals(propName, allProps[j].Name))
thisProp = allProps[j];
}
if (thisProp != null)
{
CPA attr = Properties.GetCPA(thisProp);
if (attr != null && AccessLevel.GameMaster >= attr.WriteLevel && thisProp.CanWrite && !attr.ReadOnly)
realProps[i] = thisProp;
}
}
}
ConstructorInfo[] ctors = type.GetConstructors();
foreach (var ctor in ctors)
{
if (!Add.IsConstructable(ctor, AccessLevel.GameMaster))
continue;
ParameterInfo[] paramList = ctor.GetParameters();
if (args.Length == paramList.Length)
{
object[] paramValues = Add.ParseValues(paramList, args);
if (paramValues == null)
continue;
object built = ctor.Invoke(paramValues);
if (built != null && realProps != null)
{
for (int j = 0; j < realProps.Length; ++j)
{
if (realProps[j] == null)
continue;
Properties.InternalSetValue(built, realProps[j], props[j, 1]);
}
}
return (ISpawnable)built;
}
}
return null;
}