public static object ParseLong(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo) { if (!long.TryParse(value, out var longValue)) { return(null); } return(longValue); }
public static object ParseSByte(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo) { if (!sbyte.TryParse(value, out var sbyteValue)) { return(null); } return(sbyteValue); }
public static object ParseInt(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo) { if (!int.TryParse(value, out var intValue)) { return(null); } return(intValue); }
public static object ParseBool(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo) { if (!bool.TryParse(value, out var boolValue)) { return(null); } return(boolValue); }
public static object ParseDouble(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo) { if (!double.TryParse(value, out var doubleValue)) { return(null); } return(doubleValue); }
public static object ParseFunction(ISharedCore core, object value, Type type, FunctionTypeInfo typeInfo) { if (value is MValueFunctionCallback function) { return((Function.Func) new FunctionWrapper(core, function).Call); } return(null); }
public static MValueConst[] CreateFrom(ISharedCore core, IntPtr[] pointers) { int length = pointers.Length; var mValues = new MValueConst[length]; for (var i = 0; i < length; i++) { mValues[i] = new MValueConst(core, pointers[i]); } return(mValues); }
public static object ParseSByte(ISharedCore core, object value, Type type, FunctionTypeInfo typeInfo) { unchecked { return(value switch { long longValue => (sbyte)longValue, ulong ulongValue => (sbyte)ulongValue, double doubleValue => (sbyte)doubleValue, string stringValue when sbyte.TryParse(stringValue, out var sbyteValue) => sbyteValue, bool boolValue => boolValue ? (sbyte)1 : (sbyte)0, _ => default });
public static object ParseObject(ISharedCore core, object value, Type type, FunctionTypeInfo typeInfo) { if (core.MValueFromObject(value, type, out var result)) { return(result); } if (!type.IsValueType || type == FunctionTypes.String) { return(value); } var defaultValue = typeInfo?.DefaultValue; return(defaultValue ?? Activator.CreateInstance(type)); }
public MValueConst(ISharedCore core, IntPtr nativePointer) { this.core = core; this.nativePointer = nativePointer; if (nativePointer == IntPtr.Zero) { this.type = Type.Nil; } else { unsafe { this.type = (Type)core.Library.Shared.MValueConst_GetType(nativePointer); } } }
private static object CreateArray(ISharedCore core, Type type, MValueConst[] mValues, FunctionTypeInfo typeInfo) { var length = mValues.Length; if (type == FunctionTypes.Obj) { var array = new object[length]; for (var i = 0; i < length; i++) { var currMValue = mValues[i]; if (!TryParseObject(core, in currMValue, type, typeInfo?.Element, out var obj)) { array[i] = obj; } else { array[i] = obj; } }
public static object ParseBaseObject(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo) { if (typeInfo.IsPlayer) { foreach (var player in core.PlayerPool.GetAllEntities()) { if (!player.Exists) { continue; } if (player.Name.Equals(value)) { return(player); } } if (!ushort.TryParse(value, out var playerId)) { return(null); } var entity = core.GetEntityById(playerId); if (entity is ISharedPlayer playerEntity) { return(playerEntity); } } else if (typeInfo.IsVehicle) { if (!ushort.TryParse(value, out var vehicleId)) { return(null); } var entity = core.GetEntityById(vehicleId); if (entity is ISharedVehicle vehicleEntity) { return(vehicleEntity); } } return(null); }
public static object ParseBool(ISharedCore core, object value, Type type, FunctionTypeInfo typeInfo) { switch (value) { case bool _: return(value); case string stringValue: switch (stringValue) { case "true": return(true); case "false": return(false); } break; } return(null); }
private Function(ISharedCore core, Delegate @delegate, Type returnType, Type[] args, FunctionMValueConstParser[] constParsers, FunctionObjectParser[] objectParsers, FunctionStringParser[] stringParsers, FunctionTypeInfo[] typeInfos, int requiredArgsCount) { this.core = core; this.@delegate = @delegate; this.returnType = returnType; this.args = args; this.constParsers = constParsers; this.objectParsers = objectParsers; this.stringParsers = stringParsers; this.typeInfos = typeInfos; this.requiredArgsCount = requiredArgsCount; var parameterDefaultValues = ParameterDefaultValues .GetParameterDefaultValues(@delegate.Method); objectMethodExecutor = ObjectMethodExecutor.Create( @delegate.Method, @delegate.Target?.GetType().GetTypeInfo(), parameterDefaultValues); target = @delegate.Target; }
internal NativeResource(ISharedCore core, IntPtr nativePointer) : base(core, nativePointer) { }
public MValueWriter2(ISharedCore core) { this.core = core; }
public MValueObject(ISharedCore core, List <string> names, List <object> values) { this.core = core; Names = names; Values = values; }
public static object ParseByteArray(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo) { return(null); }
public MValueArray(ISharedCore core, List <object> values) { this.core = core; Values = values; }
public void ExecuteOn(ISharedCore core) { }
public MValueArrayReader(ISharedCore core, MValueConst[] mValueArray) { this.mValueArray = mValueArray; mValueArrayBuffer = new MValueBuffer2(core, mValueArray); }
public static object ParseEnum(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo) { return(!Enum.TryParse(type, value, true, out var enumObject) ? null : enumObject); }
public MValueConstFunctionWrapper(ISharedCore core, IntPtr nativePointer) { this.core = core; this.nativePointer = nativePointer; }
internal SharedNativeResource(ISharedCore core, IntPtr nativePointer) { this.core = core; NativePointer = nativePointer; }
public MValueConst(ISharedCore core, Type type, IntPtr nativePointer) { this.core = core; this.nativePointer = nativePointer; this.type = type; }
public FunctionWrapper(ISharedCore core, MValueFunctionCallback function) { this.core = core; this.function = function; }
public IllegalThreadException(ISharedCore baseObject) : base( $"API from {baseObject} called in wrong thread with name {Thread.CurrentThread.Name} and id {Thread.CurrentThread.ManagedThreadId}") { }
public Config(ISharedCore core, IntPtr pointer) : base(core, Marshal.PtrToStructure <ConfigNodeData>(pointer)) { this.pointer = pointer; }
internal ConfigNode(ISharedCore core, ConfigNodeData data) { this.core = core; this.data = data; }
//TODO: for high optimization add ParseBoolUnsafe ect. that doesn't contains the mValue type check for scenarios where we already had to check the mValue type // Returns null when function signature isn't supported public static Function Create <T>(ISharedCore core, T func) where T : Delegate { var genericArguments = func.GetType().GetGenericArguments(); var parameters = func.Method.GetParameters(); var returnType = func.Method.ReturnType; if (returnType != FunctionTypes.Void) { genericArguments = genericArguments.SkipLast(1).ToArray(); } //TODO: check for unsupported types var constParsers = new FunctionMValueConstParser[parameters.Length]; var objectParsers = new FunctionObjectParser[parameters.Length]; var stringParsers = new FunctionStringParser[parameters.Length]; var typeInfos = new FunctionTypeInfo[parameters.Length]; var requiredArgsCount = 0; for (int i = 0, length = parameters.Length; i < length; i++) { var parameterInfo = parameters[i]; var arg = parameterInfo.ParameterType; var typeInfo = typeInfos[i] = new FunctionTypeInfo(arg, parameterInfo); if (!typeInfo.IsNullable && !typeInfo.IsParamArray && !parameterInfo.HasDefaultValue) { requiredArgsCount++; } constParsers[i] = typeInfo.ConstParser; objectParsers[i] = typeInfo.ObjectParser; stringParsers[i] = typeInfo.StringParser; if (constParsers[i] == null || objectParsers[i] == null || stringParsers[i] == null) { AltShared.Core.LogWarning("Failed to construct a function because of unsupported argument type " + arg + " at index " + i); return(null); } } for (int i = 0, length = typeInfos.Length; i < length; i++) { if (typeInfos[i].IsParamArray && i + 1 < length) { throw new ArgumentException( "params array needs to be at the end of the method. E.g. (int p1, int p2, params int[] args)"); } if (!typeInfos[i].IsNullable || i + 1 >= length) { continue; } if (!typeInfos[i + 1].IsNullable && !typeInfos[i + 1].IsParamArray) { throw new ArgumentException( "Method nullable needs to be at the end of the method. E.g. (int p1, int? p2, int p3?)."); } } return(new Function(core, func, returnType, genericArguments, constParsers, objectParsers, stringParsers, typeInfos, requiredArgsCount)); }