Skip to content

Commit

Permalink
Feature/enum function parser (#77)
Browse files Browse the repository at this point in the history
* Add enum support to function parser
  • Loading branch information
FabianTerhorst authored Oct 6, 2019
1 parent e1ecc0f commit ef3d9c8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion api/AltV.Net.Example/SampleResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,15 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe
colShapeCircle.SetMetaData("bla", 3);
colShapeCircle.SetData("bla", (int) 4);

Alt.CreateVehicle(VehicleModel.Adder, new Position(1337, 1337, 1337), Rotation.Zero);
var vehicle2 = Alt.CreateVehicle(VehicleModel.Adder, new Position(1337, 1337, 1337), Rotation.Zero);
Alt.On<IVehicle, VehicleModel>("onEnum", OnEnum);
Alt.Emit("onEnum", vehicle2, VehicleModel.Adder.ToString());
}

public void OnEnum(IVehicle vehicle, VehicleModel vehicleModel)
{
Console.WriteLine("vehicle:" + vehicle.Id);
Console.WriteLine("vehicle-model:" + vehicleModel);
}

[Event("bla2")]
Expand Down
5 changes: 5 additions & 0 deletions api/AltV.Net/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public static Function Create<T>(T func) where T : Delegate
parsers[i] = FunctionMValueParsers.ParseFunction;
objectParsers[i] = FunctionObjectParsers.ParseFunction;
}
else if (typeInfo.IsEnum)
{
parsers[i] = FunctionMValueParsers.ParseEnum;
objectParsers[i] = FunctionObjectParsers.ParseEnum;
}
else
{
// Unsupported type
Expand Down
5 changes: 5 additions & 0 deletions api/AltV.Net/FunctionParser/FunctionMValueParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,11 @@ public static object ParseConvertible(ref MValue mValue, Type type, IBaseBaseObj
{
return MValueAdapters.FromMValue(ref mValue, type, out var obj) ? obj : null;
}

public static object ParseEnum(ref MValue value, Type type, IBaseBaseObjectPool baseBaseObjectPool, FunctionTypeInfo typeInfo)
{
return !Enum.TryParse(type, value.ToString(), true, out var enumObject) ? null : enumObject;
}
}

internal delegate object FunctionMValueParser(ref MValue mValue, Type type, IBaseBaseObjectPool baseBaseObjectPool,
Expand Down
5 changes: 5 additions & 0 deletions api/AltV.Net/FunctionParser/FunctionObjectParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ public static object ParseConvertible(object value, Type type, FunctionTypeInfo
mValue.Dispose();
return obj;
}

public static object ParseEnum(object value, Type type, FunctionTypeInfo typeInfo)
{
return !Enum.TryParse(type, value.ToString(), true, out var enumObject) ? null : enumObject;
}
}

internal delegate object FunctionObjectParser(object value, Type type, FunctionTypeInfo typeInfo);
Expand Down
4 changes: 4 additions & 0 deletions api/AltV.Net/FunctionParser/FunctionTypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ internal class FunctionTypeInfo

public readonly Type NullableType;

public readonly bool IsEnum;

public FunctionTypeInfo(Type type)
{
IsList = type.BaseType == FunctionTypes.Array;
Expand Down Expand Up @@ -119,6 +121,8 @@ public FunctionTypeInfo(Type type)
DefaultValue = typeof(Nullable<>).MakeGenericType(NullableType);
}
}

IsEnum = type.IsEnum;
}
}
}

0 comments on commit ef3d9c8

Please sign in to comment.