Exemplo n.º 1
0
        public FunctionTypeInfo(Type type)
        {
            IsList = type.BaseType == FunctionTypes.Array;
            IsDict = type.Name.StartsWith("Dictionary");
            if (IsDict)
            {
                GenericArguments = type.GetGenericArguments();
                //TODO: dont create this for primitive type dictionaries
                DictType         = typeof(Dictionary <,>).MakeGenericType(GenericArguments[0], GenericArguments[1]);
                DictionaryValue  = GenericArguments.Length == 2 ? new FunctionTypeInfo(GenericArguments[1]) : null;
                CreateDictionary = Expression.Lambda <Func <System.Collections.IDictionary> >(
                    Expression.New(DictType)
                    ).Compile();
            }
            else
            {
                GenericArguments = null;
                DictType         = null;
                DictionaryValue  = null;
                CreateDictionary = null;
            }

            if (type.IsValueType && type != FunctionTypes.String)
            {
                DefaultValue = Activator.CreateInstance(type);
            }
            else
            {
                DefaultValue = null;
            }

            var interfaces = type.GetInterfaces();

            if (interfaces.Contains(FunctionTypes.Entity))
            {
                IsEntity     = true;
                IsVehicle    = type == FunctionTypes.Vehicle || interfaces.Contains(FunctionTypes.Vehicle);
                IsPlayer     = type == FunctionTypes.Player || interfaces.Contains(FunctionTypes.Player);
                IsBlip       = type == FunctionTypes.Blip || interfaces.Contains(FunctionTypes.Blip);
                IsCheckpoint = type == FunctionTypes.Checkpoint || interfaces.Contains(FunctionTypes.Checkpoint);
            }
            else
            {
                IsEntity     = false;
                IsVehicle    = false;
                IsPlayer     = false;
                IsBlip       = false;
                IsCheckpoint = false;
            }

            IsMValueConvertible = interfaces.Contains(FunctionTypes.MValueConvertible);

            var elementType = type.GetElementType();

            if (elementType != null)
            {
                ElementType = elementType;
                Element     = new FunctionTypeInfo(elementType);
            }
        }
Exemplo n.º 2
0
 public static object ParseBool(string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!bool.TryParse(value, out var boolValue))
     {
         return(null);
     }
     return(boolValue);
 }
Exemplo n.º 3
0
 public static object ParseFloat(string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!float.TryParse(value, out var floatValue))
     {
         return(null);
     }
     return(floatValue);
 }
 public static object ParseUShort(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!ushort.TryParse(value, out var ushortValue))
     {
         return(null);
     }
     return(ushortValue);
 }
Exemplo n.º 5
0
 public static object ParseDouble(string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!double.TryParse(value, out var doubleValue))
     {
         return(null);
     }
     return(doubleValue);
 }
 public static object ParseByte(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!byte.TryParse(value, out var byteValue))
     {
         return(null);
     }
     return(byteValue);
 }
 public static object ParseLong(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!long.TryParse(value, out var longValue))
     {
         return(null);
     }
     return(longValue);
 }
Exemplo n.º 8
0
 public static object ParseULong(string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!ulong.TryParse(value, out var ulongValue))
     {
         return(null);
     }
     return(ulongValue);
 }
Exemplo n.º 9
0
 public static object ParseUInt(string value, Type type, FunctionTypeInfo typeInfo)
 {
     if (!uint.TryParse(value, out var uintValue))
     {
         return(null);
     }
     return(uintValue);
 }
        public static object ParseFunction(object value, Type type, FunctionTypeInfo typeInfo)
        {
            if (value is MValue.Function function)
            {
                return((Function.Func) new FunctionWrapper(function).Call);
            }

            return(null);
        }
 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
         });
Exemplo n.º 12
0
 public static object ParseInt(object value, Type type, FunctionTypeInfo typeInfo)
 {
     unchecked
     {
         return(value switch
         {
             long longValue => (int)longValue,
             ulong ulongValue => (int)ulongValue,
             double doubleValue => (int)doubleValue,
             string stringValue when int.TryParse(stringValue, out var intValue) => intValue,
             bool boolValue => boolValue ? 1 : 0,
             _ => default
         });
        public static object ParseObject(object value, Type type, FunctionTypeInfo typeInfo)
        {
            if (MValueAdapters.FromObject(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 static object CreateArray(Type type, MValue[] mValues, IBaseBaseObjectPool baseBaseObjectPool,
     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(ref currMValue, type, baseBaseObjectPool, typeInfo?.Element, out var obj))
             {
                 array[i] = obj;
                 //return null;
             }
             else
             {
                 array[i] = obj;
             }
         }
Exemplo n.º 15
0
        private static object CreateArray(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(in currMValue, type, typeInfo?.Element, out var obj))
                    {
                        array[i] = obj;
                    }
                    else
                    {
                        array[i] = obj;
                    }
                }
        public static object ParseEntity(string value, Type type, FunctionTypeInfo typeInfo)
        {
            if (typeInfo.IsPlayer)
            {
                foreach (var player in Alt.Server.GetPlayers())
                {
                    if (!player.Exists)
                    {
                        continue;
                    }
                    if (player.Name.Equals(value))
                    {
                        return(player);
                    }
                }

                if (!ushort.TryParse(value, out var playerId))
                {
                    return(null);
                }
                var entity = Alt.Server.GetEntityById(playerId);
                if (entity is IPlayer playerEntity)
                {
                    return(playerEntity);
                }
            }
            else if (typeInfo.IsVehicle)
            {
                if (!ushort.TryParse(value, out var vehicleId))
                {
                    return(null);
                }
                var entity = Alt.Server.GetEntityById(vehicleId);
                if (entity is IVehicle 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);
        }
 public static object ParseConvertible(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     return(null);
 }
 public static object ParseInt(object value, Type type, FunctionTypeInfo typeInfo)
 {
     return(value is long l ? (int)l : default);
 public static object ParseBool(object value, Type type, FunctionTypeInfo typeInfo)
 {
     return(value is bool?value : null);
 }
 public static object ParseEnum(string value, Type type, FunctionTypeInfo typeInfo)
 {
     return(!Enum.TryParse(type, value, true, out var enumObject) ? null : enumObject);
 }
Exemplo n.º 22
0
        public FunctionTypeInfo(Type paramType, ParameterInfo paramInfo = null)
        {
            var param = Expression.Parameter(typeof(int));

            CreateArrayOfElementType = Expression.Lambda <Func <int, Array> >(
                Expression.NewArrayBounds(paramType, param), param).Compile();
            IsList = paramType.BaseType == FunctionTypes.Array;
            IsDict = paramType.Name.StartsWith("Dictionary") || paramType.Name.StartsWith("IDictionary");

            if (IsDict)
            {
                GenericArguments = paramType.GetGenericArguments();
                //TODO: dont create this for primitive type dictionaries
                DictType         = typeof(Dictionary <,>).MakeGenericType(GenericArguments[0], GenericArguments[1]);
                DictionaryValue  = GenericArguments.Length == 2 ? new FunctionTypeInfo(GenericArguments[1]) : null;
                CreateDictionary = Expression.Lambda <Func <IDictionary> >(
                    Expression.New(DictType)
                    ).Compile();
            }
            else
            {
                GenericArguments = null;
                DictType         = null;
                DictionaryValue  = null;
                CreateDictionary = null;
            }

            if (paramInfo != null && paramInfo.HasDefaultValue)
            {
                DefaultValue = paramInfo.DefaultValue;
            }
            else
            {
                if (paramType.IsValueType && paramType != FunctionTypes.String)
                {
                    DefaultValue = Activator.CreateInstance(paramType);
                }
                else
                {
                    DefaultValue = null;
                }
            }

            IsPosition  = paramType == FunctionTypes.Position;
            IsRotation  = paramType == FunctionTypes.Rotation;
            IsRgba      = paramType == FunctionTypes.Rgba;
            IsVector3   = paramType == FunctionTypes.Vector3;
            IsByteArray = paramType == FunctionTypes.ByteArray;

            var interfaces = paramType.GetInterfaces();

            if (interfaces.Contains(FunctionTypes.BaseObject))
            {
                IsBaseObject = true;
                IsVehicle    = paramType == FunctionTypes.Vehicle || interfaces.Contains(FunctionTypes.Vehicle);
                IsPlayer     = paramType == FunctionTypes.Player || interfaces.Contains(FunctionTypes.Player);
            }
            else
            {
                IsBaseObject = false;
                IsVehicle    = false;
                IsPlayer     = false;
            }

            IsMValueConvertible = interfaces.Contains(FunctionTypes.MValueConvertible);

            var elementType = paramType.GetElementType();

            if (elementType != null)
            {
                ElementType = elementType;
                Element     = new FunctionTypeInfo(elementType);

                var arraySizeParam = Expression.Parameter(typeof(int));
                CreateArrayOfTypeExp = Expression.Lambda <Func <int, Array> >(
                    Expression.NewArrayBounds(ElementType, arraySizeParam),
                    new[] { arraySizeParam }
                    ).Compile();
                EmptyArrayOfType = Array.CreateInstance(ElementType, 0);
            }
            else
            {
                CreateArrayOfTypeExp = null;
                EmptyArrayOfType     = null;
            }

            if (paramInfo != null)
            {
                IsParamArray = paramInfo.GetCustomAttribute <ParamArrayAttribute>() != null;
            }

            IsNullable = paramType.Name.StartsWith("Nullable");
            if (IsNullable)
            {
                var genericArguments = paramType.GetGenericArguments();
                if (genericArguments.Length != 1)
                {
                    IsNullable = false;
                }
                else
                {
                    NullableType = genericArguments[0];
                    DefaultValue = Activator.CreateInstance(typeof(Nullable <>).MakeGenericType(NullableType));
                }
            }

            IsEnum = paramType.IsEnum;


            if (IsNullable)
            {
                paramType = NullableType;
            }
            if (paramType == FunctionTypes.Obj)
            {
                ConstParser  = FunctionMValueConstParsers.ParseObject;
                ObjectParser = FunctionObjectParsers.ParseObject;
                StringParser = FunctionStringParsers.ParseObject;
            }
            else if (paramType == FunctionTypes.Bool)
            {
                ConstParser  = FunctionMValueConstParsers.ParseBool;
                ObjectParser = FunctionObjectParsers.ParseBool;
                StringParser = FunctionStringParsers.ParseBool;
            }
            else if (paramType == FunctionTypes.SByte)
            {
                ConstParser  = FunctionMValueConstParsers.ParseSByte;
                ObjectParser = FunctionObjectParsers.ParseSByte;
                StringParser = FunctionStringParsers.ParseSByte;
            }
            else if (paramType == FunctionTypes.Short)
            {
                ConstParser  = FunctionMValueConstParsers.ParseShort;
                ObjectParser = FunctionObjectParsers.ParseShort;
                StringParser = FunctionStringParsers.ParseShort;
            }
            else if (paramType == FunctionTypes.Int)
            {
                ConstParser  = FunctionMValueConstParsers.ParseInt;
                ObjectParser = FunctionObjectParsers.ParseInt;
                StringParser = FunctionStringParsers.ParseInt;
            }
            else if (paramType == FunctionTypes.Long)
            {
                ConstParser  = FunctionMValueConstParsers.ParseLong;
                ObjectParser = FunctionObjectParsers.ParseLong;
                StringParser = FunctionStringParsers.ParseLong;
            }
            else if (paramType == FunctionTypes.Byte)
            {
                ConstParser  = FunctionMValueConstParsers.ParseByte;
                ObjectParser = FunctionObjectParsers.ParseByte;
                StringParser = FunctionStringParsers.ParseByte;
            }
            else if (paramType == FunctionTypes.UShort)
            {
                ConstParser  = FunctionMValueConstParsers.ParseUShort;
                ObjectParser = FunctionObjectParsers.ParseUShort;
                StringParser = FunctionStringParsers.ParseUShort;
            }
            else if (paramType == FunctionTypes.UInt)
            {
                ConstParser  = FunctionMValueConstParsers.ParseUInt;
                ObjectParser = FunctionObjectParsers.ParseUInt;
                StringParser = FunctionStringParsers.ParseUInt;
            }
            else if (paramType == FunctionTypes.ULong)
            {
                ConstParser  = FunctionMValueConstParsers.ParseULong;
                ObjectParser = FunctionObjectParsers.ParseULong;
                StringParser = FunctionStringParsers.ParseULong;
            }
            else if (paramType == FunctionTypes.Float)
            {
                ConstParser  = FunctionMValueConstParsers.ParseFloat;
                ObjectParser = FunctionObjectParsers.ParseFloat;
                StringParser = FunctionStringParsers.ParseFloat;
            }
            else if (paramType == FunctionTypes.Double)
            {
                ConstParser  = FunctionMValueConstParsers.ParseDouble;
                ObjectParser = FunctionObjectParsers.ParseDouble;
                StringParser = FunctionStringParsers.ParseDouble;
            }
            else if (paramType == FunctionTypes.String)
            {
                ConstParser  = FunctionMValueConstParsers.ParseString;
                ObjectParser = FunctionObjectParsers.ParseString;
                StringParser = FunctionStringParsers.ParseString;
            }
            else if (paramType.BaseType == FunctionTypes.Array)
            {
                ConstParser  = FunctionMValueConstParsers.ParseArray;
                ObjectParser = FunctionObjectParsers.ParseArray;
                StringParser = FunctionStringParsers.ParseArray;
            }
            else if (IsBaseObject)
            {
                ConstParser  = FunctionMValueConstParsers.ParseBaseObject;
                ObjectParser = FunctionObjectParsers.ParseBaseObject;
                StringParser = FunctionStringParsers.ParseBaseObject;
            }
            else if (IsDict)
            {
                ConstParser  = FunctionMValueConstParsers.ParseDictionary;
                ObjectParser = FunctionObjectParsers.ParseDictionary;
                StringParser = FunctionStringParsers.ParseDictionary;
            }
            else if (IsMValueConvertible)
            {
                ConstParser  = FunctionMValueConstParsers.ParseConvertible;
                ObjectParser = FunctionObjectParsers.ParseConvertible;
                StringParser = FunctionStringParsers.ParseConvertible;
            }
            else if (paramType == FunctionTypes.FunctionType)
            {
                ConstParser  = FunctionMValueConstParsers.ParseFunction;
                ObjectParser = FunctionObjectParsers.ParseFunction;
                StringParser = FunctionStringParsers.ParseFunction;
            }
            else if (IsEnum)
            {
                ConstParser  = FunctionMValueConstParsers.ParseEnum;
                ObjectParser = FunctionObjectParsers.ParseEnum;
                StringParser = FunctionStringParsers.ParseEnum;
            }
            else if (IsPosition)
            {
                ConstParser  = FunctionMValueConstParsers.ParsePosition;
                ObjectParser = FunctionObjectParsers.ParsePosition;
                StringParser = FunctionStringParsers.ParsePosition;
            }
            else if (IsRotation)
            {
                ConstParser  = FunctionMValueConstParsers.ParseRotation;
                ObjectParser = FunctionObjectParsers.ParseRotation;
                StringParser = FunctionStringParsers.ParseRotation;
            }
            else if (IsVector3)
            {
                ConstParser  = FunctionMValueConstParsers.ParseVector3;
                ObjectParser = FunctionObjectParsers.ParseVector3;
                StringParser = FunctionStringParsers.ParseVector3;
            }
            else if (IsRgba)
            {
                ConstParser  = FunctionMValueConstParsers.ParseRgba;
                ObjectParser = FunctionObjectParsers.ParseRgba;
                StringParser = FunctionStringParsers.ParseRgba;
            }
            else if (IsByteArray)
            {
                ConstParser  = FunctionMValueConstParsers.ParseByteArray;
                ObjectParser = FunctionObjectParsers.ParseByteArray;
                StringParser = FunctionStringParsers.ParseByteArray;
            }
            else if (AltShared.Core.IsMValueConvertible(paramType))
            {
                ConstParser  = FunctionMValueConstParsers.ParseConvertible;
                ObjectParser = FunctionObjectParsers.ParseConvertible;
                StringParser = FunctionStringParsers.ParseConvertible;
            }
            else
            {
                ConstParser  = null;
                ObjectParser = null;
                StringParser = null;
            }
        }
Exemplo n.º 23
0
 public static object ParseArray(object value, Type type, FunctionTypeInfo typeInfo)
 {
     return(null);
 }
Exemplo n.º 24
0
 public static bool ValidateEntityType(BaseObjectType baseObjectType, Type type, FunctionTypeInfo typeInfo)
 {
     return(false);
 }
Exemplo n.º 25
0
 public static object ParseString(string value, Type type, FunctionTypeInfo typeInfo)
 {
     return(value);
 }
        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);
        }
Exemplo n.º 27
0
 public static object ParseConvertible(object value, Type type, FunctionTypeInfo typeInfo)
 {
     return(null);
 }
 public static object ParseByteArray(ISharedCore core, string value, Type type, FunctionTypeInfo typeInfo)
 {
     return(null);
 }
        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);
        }
Exemplo n.º 30
0
 public static object ParseFunction(string value, Type type, FunctionTypeInfo typeInfo)
 {
     return(null);
 }