Skip to content

Commit

Permalink
Refactor event handlers, update example resource (#110)
Browse files Browse the repository at this point in the history
* Refactor event handlers, rename attributes, update SampleResource
* Refactor async event handlers, rename async attributes, update EventAttribute, update SampleResource
* Fix warnings in Alt.RegisterEvents & AltAsync.RegisterEvents, fix implicitly called closures in AsyncModule, update example resource
  • Loading branch information
Unnvaldr authored and FabianTerhorst committed Dec 9, 2019
1 parent 7dc3d43 commit dcabd2e
Show file tree
Hide file tree
Showing 22 changed files with 3,341 additions and 1,483 deletions.
493 changes: 493 additions & 0 deletions api/AltV.Net.Async/AltAsync.Off.cs

Large diffs are not rendered by default.

288 changes: 285 additions & 3 deletions api/AltV.Net.Async/AltAsync.On.cs

Large diffs are not rendered by default.

454 changes: 233 additions & 221 deletions api/AltV.Net.Async/AltAsync.RegisterEvents.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace AltV.Net.Async
{
[AttributeUsage(AttributeTargets.Method)]
public class AsyncEvent : Attribute
public sealed class AsyncClientEventAttribute : Attribute
{
public string Name { get; }

public AsyncEvent(string name = null)
public AsyncClientEventAttribute(string name = null)
{
Name = name;
}
Expand Down
16 changes: 16 additions & 0 deletions api/AltV.Net.Async/AsyncEventAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace AltV.Net.Async
{
[Obsolete("This attribute is obsolete. Use AsyncServerEventAttribute or AsyncClientEventAttribute instead.")]
[AttributeUsage(AttributeTargets.Method)]
public sealed class AsyncEventAttribute : Attribute
{
public string Name { get; }

public AsyncEventAttribute(string name = null)
{
Name = name;
}
}
}
300 changes: 246 additions & 54 deletions api/AltV.Net.Async/AsyncModule.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;

namespace AltV.Net.Async
{
[AttributeUsage(AttributeTargets.Method)]
public class AsyncScriptEvent : Attribute
{
public ScriptEventType EventType { get; }

public AsyncScriptEvent(ScriptEventType eventType)
{
EventType = eventType;
}
}
using System;

namespace AltV.Net.Async
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class AsyncScriptEventAttribute : Attribute
{
public ScriptEventType EventType { get; }

public AsyncScriptEventAttribute(ScriptEventType eventType)
{
EventType = eventType;
}
}
}
15 changes: 15 additions & 0 deletions api/AltV.Net.Async/AsyncServerEventAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace AltV.Net.Async
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class AsyncServerEventAttribute : Attribute
{
public string Name { get; }

public AsyncServerEventAttribute(string name = null)
{
Name = name;
}
}
}
93 changes: 44 additions & 49 deletions api/AltV.Net.Example/SampleResource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using AltV.Net.Async;
Expand Down Expand Up @@ -49,7 +48,7 @@ public override void OnStart()
}
};

Alt.On<object[]>("array_test", objects =>
Alt.OnServer<object[]>("array_test", objects =>
{
Console.WriteLine("count:" + objects.Length);
Console.WriteLine("1:" + objects[0]);
Expand All @@ -59,10 +58,10 @@ public override void OnStart()
Console.WriteLine("2:" + ((object[])objects[2])[0]);
});
Alt.Emit("array_test", new object[] {new object[] {"test", "test4", new[] {1337}}});
Alt.On<object[]>("array_test2", objects =>
Alt.OnServer<object[]>("array_test2", objects =>
{
Console.WriteLine("count2:" + objects.Length);
Console.WriteLine("count3:" + ((object[])(((object[])objects)[0]))[0]);
Console.WriteLine("count3:" + ((object[])objects[0])[0]);
});
Alt.Emit("array_test2", new object[] {new object[] {new object[]{1337}}});

Expand All @@ -75,12 +74,12 @@ public override void OnStart()
var mValuesListGet = mValueList.GetList();
for (var i = 0; i < mValuesListGet.Length; i++)
{
Console.WriteLine("val: " + mValuesListGet[i].ToString());
Console.WriteLine("val: " + mValuesListGet[i]);
mValuesListGet[i].Dispose();
}

MValueAdapters.Register(new ConvertibleObject.ConvertibleObjectAdapter());
Alt.On("convertible_test", delegate(ConvertibleObject convertible)
Alt.OnServer("convertible_test", delegate(ConvertibleObject convertible)
{
Console.WriteLine("convertible_test received");
Console.WriteLine(convertible.Test);
Expand All @@ -103,14 +102,14 @@ public override void OnStart()
//mValueFromConvertible.Dispose();
Alt.Emit("convertible_test", convertibleObject);

Alt.On<string>("test", s => { Alt.Log("test=" + s); });
Alt.OnServer("test", args => { Alt.Log("args=" + args[0]); });
Alt.OnServer<string>("test", s => { Alt.Log("test=" + s); });
Alt.OnServer<object[]>("test", args => { Alt.Log("args=" + args[0]); });
Alt.Emit("test", "bla");
Alt.On("bla", bla);
Alt.On<string>("bla2", bla2);
Alt.On<string, bool>("bla3", bla3);
Alt.On<string, string>("bla4", bla4);
Alt.On<IMyVehicle>("vehicleTest", myVehicle =>
Alt.OnServer("bla", bla);
Alt.OnServer<string>("bla2", bla2);
Alt.OnServer<string, bool>("bla3", bla3);
Alt.OnServer<string, string>("bla4", bla4);
Alt.OnServer<IMyVehicle>("vehicleTest", myVehicle =>
{
Console.WriteLine("inside invoke");
Alt.Log("myData: " + myVehicle?.MyData);
Expand All @@ -132,7 +131,7 @@ public override void OnStart()
AltAsync.Log("event:" + name);
return Task.CompletedTask;
};
AltAsync.OnServer("bla",
AltAsync.OnServer<object[]>("bla",
async args => { await AltAsync.Do(() => Alt.Log("bla with no args:" + args.Length)); });
Alt.Emit("bla");

Expand All @@ -143,7 +142,7 @@ public override void OnStart()
Alt.Log(checkpoint.Color.ToString());

var voiceChannel = Alt.CreateVoiceChannel(true, 10f);
Alt.Log(voiceChannel.MaxDistance.ToString());
Alt.Log(voiceChannel.MaxDistance.ToString("R"));

var vehicle = Alt.CreateVehicle(VehicleModel.Apc, new Position(1, 2, 3), new Rotation(1, 2, 3));
vehicle.SetSyncedMetaData("test", 123);
Expand Down Expand Up @@ -186,7 +185,7 @@ public override void OnStart()

Alt.Emit("vehicleTest", vehicle);

Alt.On("event_name",
Alt.OnServer("event_name",
delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVehicle arg5,
Dictionary<string, object> arg6, IMyVehicle[] myVehicles, string probablyNull, string[] nullArray,
Dictionary<string, double> bla)
Expand All @@ -202,7 +201,7 @@ public override void OnStart()
Alt.Log("bla2:" + bla["test"]);
});

Alt.On("entity-array-obj",
Alt.OnServer<object[]>("entity-array-obj",
delegate(object[] myVehicles)
{
Alt.Log("entity-array-obj: " + ((MyVehicle) myVehicles[0]).Position.X + " " +
Expand All @@ -215,8 +214,7 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe
Dictionary<string, double> bla)
{
await Task.Delay(500);
var asyncVehicle =
await AltAsync.CreateVehicle(VehicleModel.Apc, Position.Zero, new Rotation(1, 2, 3));
await AltAsync.CreateVehicle(VehicleModel.Apc, Position.Zero, new Rotation(1, 2, 3));

AltAsync.Log("async-param1:" + s);
AltAsync.Log("async-param2:" + s1);
Expand All @@ -238,15 +236,15 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe
//["test2"] = new Dictionary<string, long> {["test"] = 1},
//["test3"] = new Dictionary<string, long> {["test"] = 42}
},
new IMyVehicle[] {(IMyVehicle) vehicle}, null, new string[] {null},
new[] {(IMyVehicle) vehicle}, null, new string[] {null},
new Dictionary<string, object>
{
["test"] = null
});

Alt.On<string[]>("test_string_array", s => { Alt.Log("string-array-entry-0:" + s[0]); });
Alt.OnServer<string[]>("test_string_array", s => { Alt.Log("string-array-entry-0:" + s[0]); });

Alt.Emit("test_string_array", new object[] {new string[] {"bla"}});
Alt.Emit("test_string_array", new object[] {new[] {"bla"}});

/*Alt.On("function_event", delegate(Function.Func func)
{
Expand All @@ -260,16 +258,17 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe
return 42;
}));*/

foreach (var player in Alt.GetAllPlayers())
foreach (var pl in Alt.GetAllPlayers())
{
Alt.Log("player:" + pl.Position.X + " " + pl.Position.Y + " " + pl.Position.Z);
}

foreach (var veh in Alt.GetAllVehicles())
{
Alt.Log("vehicle:" + veh.Position.X + " " + veh.Position.Y + " " + veh.Position.Z);
}

Alt.On("1337", delegate(int int1) { Alt.Log("int1:" + int1); });
Alt.OnServer("1337", delegate(int int1) { Alt.Log("int1:" + int1); });

AltAsync.On("1337", delegate(int int1) { Alt.Log("int1:" + int1); });

Expand All @@ -281,7 +280,7 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe

Alt.Emit("MyServerEvent3", vehicle);

Alt.Emit("entity-array-obj", new[] {new[] {vehicle}});
Alt.Emit("entity-array-obj", new object[] {new[] {vehicle}});

vehicle.Remove();

Expand All @@ -295,25 +294,21 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe

Alt.Emit("MyServerEvent", "test-custom-parser");

Alt.Emit("MyServerEvent2", new object[] {new string[] {"test-custom-parser-array"}});
Alt.Emit("MyServerEvent2", new object[] {new[] {"test-custom-parser-array"}});

//dynamic dynamic = new ExpandoObject();

//Alt.Emit("dynamic_test", dynamic);

Alt.Export("GetBla", () => { Alt.Log("GetBla called"); });

Action action;

Alt.Import("example", "GetBla", out action);
Alt.Import(Alt.Server.Resource.Name, "GetBla", out Action action);

action();

Alt.Export("functionExport", delegate(string name) { Alt.Log("called with:" + name); });

Action<string> action2;

Alt.Import("example", "functionExport", out action2);
Alt.Import(Alt.Server.Resource.Name, "functionExport", out Action<string> action2);

action2("123");
/*if (Alt.Import("Bla", "GetBla", out Action value))
Expand All @@ -326,14 +321,14 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe
Alt.Emit("none-existing-event", new ConvertibleObject());

// You need to catch this with a exception because its not possible to construct a invalid entity
// Remember not all vehicles you receive from events has to be constructored by this constructor when there got created from different resources ect.
// Remember not all vehicles you receive from events has to be constructed by this constructor when there got created from different resources ect.
// But when you don't use a entity factory you can validate that by checking if the ivehicle is a imyvehicle
try
{
IMyVehicle myConstructedVehicle =
IMyVehicle unused =
new MyVehicle((uint) VehicleModel.Apc, new Position(1, 1, 1), new Rotation(1, 1, 1));
}
catch (BaseObjectRemovedException baseObjectRemovedException)
catch (BaseObjectRemovedException)
{
}

Expand All @@ -353,19 +348,19 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe

var colShapeCylinder = Alt.CreateColShapeCylinder(new Position(1337, 1337, 1337), 10, 10);
colShapeCylinder.SetMetaData("bla", 1);
colShapeCylinder.SetData("bla", (int) 2);
colShapeCylinder.SetData("bla", 2);

var colShapeCircle = Alt.CreateColShapeCircle(new Position(1337, 1337, 1337), 10);
colShapeCircle.SetMetaData("bla", 3);
colShapeCircle.SetData("bla", (int) 4);
colShapeCircle.SetData("bla", 4);

AltChat.SendBroadcast("Test");

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

Alt.On("EmptyParams", TestEmptyParams);
Alt.OnServer("EmptyParams", TestEmptyParams);
Alt.Emit("EmptyParams", 1, 2, 3);

Alt.Emit("chat:message", "/dynamicArgs2 7");
Expand All @@ -380,33 +375,33 @@ async delegate(string s, string s1, long i1, string[] arg3, object[] arg4, IMyVe
Alt.Emit("chat:message", "/invalidCommand");
Alt.Emit("chat:message", "/invalidCommand 3535");

Alt.On<int, object[]>("onOptionalAndParamArray", OnOptionalAndParamArray);
Alt.OnServer<int, object[]>("onOptionalAndParamArray", OnOptionalAndParamArray);

Alt.Emit("onOptionalAndParamArray", 5, 42, "test");
}

public void OnOptionalAndParamArray(int test, params object[] args) {
System.Console.WriteLine($"Event<OnOptionalAndParamArray>({test}, [{string.Join(',', System.Array.ConvertAll(args ?? new object[] {""}, el => el.ToString()))}])");
public static void OnOptionalAndParamArray(int test, params object[] args) {
Console.WriteLine($"Event<OnOptionalAndParamArray>({test}, [{string.Join(',', Array.ConvertAll(args ?? new object[] {""}, el => el.ToString()))}])");
}

public void TestEmptyParams()
public static void TestEmptyParams()
{
Alt.Log("Empty params");
}

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

[Event("bla2")]
[ServerEvent("bla2")]
public void MyServerEventHandler2(string myString)
{
Alt.Log(myString);
}

[AsyncEvent]
[AsyncServerEvent]
public void asyncBla3(string myString)
{
AltAsync.Log(myString);
Expand Down Expand Up @@ -554,9 +549,9 @@ public async void Bla2(IPlayer player)
}

await player.SetPositionAsync(new Position(1, 2, 3));
var position = await player.GetPositionAsync();
var unused = await player.GetPositionAsync();
await AltAsync.Do(() => { });
var vehicle = await AltAsync.Do(() =>
var unused2 = await AltAsync.Do(() =>
Alt.CreateVehicle(VehicleModel.Apc, new Position(1, 2, 3), new Rotation(1, 2, 3)));
}

Expand Down Expand Up @@ -596,7 +591,7 @@ private void OnPlayerDisconnect(IPlayer player, string reason)

private async Task<int> OnPlayerDisconnectAsync(IPlayer player, string reason)
{
if (player is IMyPlayer myPlayer)
if (player is IMyPlayer unused3)
{
}

Expand Down
4 changes: 2 additions & 2 deletions api/AltV.Net.Example/SampleScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public void MyCommandWithDynamicArgs2(IPlayer player, int arg1, int? arg2, param
Console.WriteLine("Command:" + arg1 + " " + arg2 + " remaining args " + string.Join(",", args));
}

[Event("eventName")]
[ServerEvent("eventName")]
public void MyEvent(IPlayer player)
{
}

[Event]
[ServerEvent]
public void MyEventName(string message)
{
Console.WriteLine(message);
Expand Down
Loading

0 comments on commit dcabd2e

Please sign in to comment.