コード例 #1
0
 public static object ParseLong(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!long.TryParse(value, out var longValue))
     {
         return(null);
     }
     return(longValue);
 }
コード例 #2
0
 public static object ParseSByte(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!sbyte.TryParse(value, out var sbyteValue))
     {
         return(null);
     }
     return(sbyteValue);
 }
コード例 #3
0
 public static object ParseInt(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!int.TryParse(value, out var intValue))
     {
         return(null);
     }
     return(intValue);
 }
コード例 #4
0
 public static object ParseBool(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!bool.TryParse(value, out var boolValue))
     {
         return(null);
     }
     return(boolValue);
 }
コード例 #5
0
 public static object ParseDouble(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!double.TryParse(value, out var doubleValue))
     {
         return(null);
     }
     return(doubleValue);
 }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
 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
         });
コード例 #9
0
        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));
        }
コード例 #10
0
 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);
         }
     }
 }
コード例 #11
0
        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;
                    }
                }
コード例 #12
0
        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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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;
        }
コード例 #15
0
 internal NativeResource(ISharedCore core, IntPtr nativePointer) : base(core, nativePointer)
 {
 }
コード例 #16
0
 public MValueWriter2(ISharedCore core)
 {
     this.core = core;
 }
コード例 #17
0
 public MValueObject(ISharedCore core, List <string> names, List <object> values)
 {
     this.core = core;
     Names     = names;
     Values    = values;
 }
コード例 #18
0
 public static object ParseByteArray(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     return(null);
 }
コード例 #19
0
 public MValueArray(ISharedCore core, List <object> values)
 {
     this.core = core;
     Values    = values;
 }
コード例 #20
0
 public void ExecuteOn(ISharedCore core)
 {
 }
コード例 #21
0
 public MValueArrayReader(ISharedCore core, MValueConst[] mValueArray)
 {
     this.mValueArray  = mValueArray;
     mValueArrayBuffer = new MValueBuffer2(core, mValueArray);
 }
コード例 #22
0
 public static object ParseEnum(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     return(!Enum.TryParse(type, value, true, out var enumObject) ? null : enumObject);
 }
コード例 #23
0
 public MValueConstFunctionWrapper(ISharedCore core, IntPtr nativePointer)
 {
     this.core          = core;
     this.nativePointer = nativePointer;
 }
コード例 #24
0
 internal SharedNativeResource(ISharedCore core, IntPtr nativePointer)
 {
     this.core     = core;
     NativePointer = nativePointer;
 }
コード例 #25
0
 public MValueConst(ISharedCore core, Type type, IntPtr nativePointer)
 {
     this.core          = core;
     this.nativePointer = nativePointer;
     this.type          = type;
 }
コード例 #26
0
 public FunctionWrapper(ISharedCore core, MValueFunctionCallback function)
 {
     this.core     = core;
     this.function = function;
 }
コード例 #27
0
 public IllegalThreadException(ISharedCore baseObject) : base(
         $"API from {baseObject} called in wrong thread with name {Thread.CurrentThread.Name} and id {Thread.CurrentThread.ManagedThreadId}")
 {
 }
コード例 #28
0
 public Config(ISharedCore core, IntPtr pointer) : base(core, Marshal.PtrToStructure <ConfigNodeData>(pointer))
 {
     this.pointer = pointer;
 }
コード例 #29
0
 internal ConfigNode(ISharedCore core, ConfigNodeData data)
 {
     this.core = core;
     this.data = data;
 }
コード例 #30
0
        //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));
        }