public override void OnStart()
        {
            AltAsync.OnCheckpoint        += OnCheckpoint;
            AltAsync.OnEntityRemove      += OnEntityRemove;
            AltAsync.OnPlayerConnect     += OnPlayerConnect;
            AltAsync.OnPlayerDamage      += OnPlayerDamage;
            AltAsync.OnPlayerDead        += OnPlayerDead;
            AltAsync.OnPlayerDisconnect  += OnPlayerDisconnect;
            AltAsync.OnVehicleChangeSeat += OnVehicleChangeSeat;
            AltAsync.OnVehicleEnter      += OnVehicleEnter;
            AltAsync.OnVehicleLeave      += OnVehicleLeave;
            AltAsync.OnPlayerEvent       += OnPlayerEvent;

            // Custom Event Triggers can be found at the bottom
            // Custom Event Type 01
            AltAsync.On("customEvent01", async args =>
            {
                await Task.Delay(1000);
                AltAsync.Log("customEvent01 triggered with following arguments: " + args[0]);
            });

            // Custom Event Type 02 - Fixed Argument Type
            AltAsync.On <string>("customEvent02", async str =>
            {
                await Task.Delay(1000);
                AltAsync.Log("customEvent01 triggered with following argument: " + str);
            });

            // Custom Event Type 03 - Delegate Type
            AltAsync.On("customEvent03", async delegate(string str)
            {
                await Task.Delay(1000);
                AltAsync.Log("customEvent03 triggered with following argument: " + str);
            });

            // Custom Event Type 04 - Method Call
            AltAsync.On <string>("customEvent04", CustomEvent04);

            AltAsync.Log("Resource \"CSharp_Examples\" has been started.");
        }
Ejemplo n.º 2
0
        public override void OnStart()
        {
            Alt.On <string>("test", s => { Alt.Log("test=" + s); });
            Alt.OnServer("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.Log("myData: " + myVehicle.MyData); });

            Alt.OnPlayerConnect         += OnPlayerConnect;
            Alt.OnPlayerDisconnect      += OnPlayerDisconnect;
            AltAsync.OnPlayerDisconnect += OnPlayerDisconnectAsync;
            Alt.OnPlayerRemove          += OnPlayerRemove;
            Alt.OnVehicleRemove         += OnVehicleRemove;
            AltAsync.OnPlayerConnect    += OnPlayerConnectAsync;
            Alt.OnConsoleCommand        += (name, args) => { };
            Alt.OnPlayerEvent           += (player, name, args) => { Alt.Log("event:" + name); };
            AltAsync.OnPlayerEvent      += (player, name, args) =>
            {
                AltAsync.Log("event:" + name);
                return(Task.CompletedTask);
            };
            AltAsync.On("bla", async args => { await AltAsync.Do(() => Alt.Log("bla with no args:" + args.Length)); });
            Alt.Emit("bla");

            var vehicle = Alt.CreateVehicle(VehicleModel.Apc, new Position(1, 2, 3), float.MinValue);

            Alt.Log(vehicle.Position.ToString());
            vehicle.PrimaryColor     = 7;
            vehicle.NumberplateText  = "AltV-C#";
            vehicle.NumberplateIndex = 2;
            vehicle.SetMod(0, 0);
            vehicle.GetMod(0);
            vehicle.GetModsCount(0);
            vehicle.Dimension       = 0;
            vehicle.LockState       = VehicleLockState.Locked;
            vehicle.PrimaryColorRgb = new Rgba
            {
                r = 1,
                g = 8,
                b = 7,
                a = 0
            };

            Alt.Log("ptr:" + vehicle.NativePointer);

            Alt.Log("number-plate:" + vehicle.NumberplateText + " " + vehicle.NumberplateIndex);

            Alt.Emit("vehicleTest", vehicle);

            Alt.On("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)
            {
                Alt.Log("param1:" + s);
                Alt.Log("param2:" + s1);
                Alt.Log("bla:" + ((object[])arg4[1])[0]);
                Alt.Log("myData-2: " + arg5.Position.X + " " + arg5.MyData);
                Alt.Log("myData-4-veh-array: " + myVehicles[0].Position.X + " " + myVehicles[0].MyData);
                Alt.Log("myData-3: " + arg6["test"]);
                Alt.Log("null?" + (probablyNull == null ? "y" : "n"));
                Alt.Log("null2?" + (nullArray[0] == null ? "y" : "n"));
                Alt.Log("bla2:" + bla["test"]);
            });

            Alt.On("entity-array-obj",
                   delegate(object[] myVehicles)
            {
                Alt.Log("entity-array-obj: " + ((MyVehicle)myVehicles[0]).Position.X + " " +
                        ((MyVehicle)myVehicles[0]).MyData);
            });

            AltAsync.On("event_name",
                        async 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)
            {
                await Task.Delay(500);
                var asyncVehicle = await AltAsync.CreateVehicle(VehicleModel.Apc, Position.Zero, float.MaxValue);

                AltAsync.Log("async-param1:" + s);
                AltAsync.Log("async-param2:" + s1);
                AltAsync.Log("async-bla:" + ((object[])arg4[1])[0]);
                AltAsync.Log("exists:" + arg5.Exists);
                AltAsync.Log("async-myData-2: " + arg5.Position.X + " " + arg5.MyData);
                AltAsync.Log("async-myData-4: " + myVehicles[0].Position.X + " " + myVehicles[0].MyData);
                AltAsync.Log("async-myData-3: " + arg6["test"]);
                AltAsync.Log("async-null?" + (probablyNull == null ? "y" : "n"));
                AltAsync.Log("async-null2?" + (nullArray[0] == null ? "y" : "n"));
                AltAsync.Log("async-bla2:" + bla["test"]);
            });

            Alt.Emit("event_name", "param_string_1", "param_string_2", 1, new[] { "array_1", "array_2" },
                     new object[] { "test", new[] { 1337 } }, vehicle,
                     new Dictionary <string, object>
            {
                ["test"] = "test" //,
                                  //["test2"] = new Dictionary<string, long> {["test"] = 1},
                                  //["test3"] = new Dictionary<string, long> {["test"] = 42}
            },
                     new IMyVehicle[] { (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.Emit("test_string_array", new object[] { new string[] { "bla" } });

            Alt.On("function_event", delegate(Function.Func func)
            {
                var result = func("parameter1");
                Alt.Log("result:" + result);
            });

            Alt.Emit("function_event", Function.Create(delegate(string bla)
            {
                Alt.Log("parameter=" + bla);
                return(42);
            }));

            foreach (var player in Alt.GetAllPlayers())
            {
            }

            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); });

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

            Alt.Emit("1337", 1);

            Alt.On <IMyVehicle>("MyServerEvent3", MyServerEventHandler2, MyServerEventParser3);

            Alt.On <IMyVehicle>("MyServerEvent3", MyServerEventHandlerAsync, MyServerEventParserAsync);

            Alt.Emit("MyServerEvent3", vehicle);

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

            vehicle.Remove();

            Bla();

            Alt.On <IPlayer, string>("MyEvent", MyEventHandler, MyParser);

            Alt.On <string>("MyServerEvent", MyServerEventHandler, MyServerEventParser);

            Alt.On <string>("MyServerEvent2", MyServerEventHandler, MyServerEventParser2);

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

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

            dynamic dynamic = new ExpandoObject();

            Alt.Emit("dynamic_test", dynamic);

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

            /*if (Alt.Import("Bla", "GetBla", out Action value))
             * {
             *  value();
             * }*/

            Alt.Emit("none-existing-event", new WritableObject());

            Alt.Emit("none-existing-event", new ConvertibleObject());
        }
Ejemplo n.º 3
0
        public override void OnStart()
        {
            MValueAdapters.Register(new ConvertibleObject.ConvertibleObjectAdapter());
            Alt.On("convertible_test", delegate(ConvertibleObject convertible)
            {
                Console.WriteLine("convertible_test received");
                Console.WriteLine(convertible.Test);
                foreach (var t in convertible.List)
                {
                    Console.WriteLine("-" + t.Test);
                }
            });
            var convertibleObject = new ConvertibleObject();

            Alt.Emit("convertible_test", convertibleObject);

            Alt.On <string>("test", s => { Alt.Log("test=" + s); });
            Alt.OnServer("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.Log("myData: " + myVehicle.MyData); });

            Alt.OnPlayerConnect         += OnPlayerConnect;
            Alt.OnPlayerDisconnect      += OnPlayerDisconnect;
            AltAsync.OnPlayerDisconnect += OnPlayerDisconnectAsync;
            Alt.OnPlayerRemove          += OnPlayerRemove;
            Alt.OnVehicleRemove         += OnVehicleRemove;
            AltAsync.OnPlayerConnect    += OnPlayerConnectAsync;
            Alt.OnConsoleCommand        += (name, args) => { };
            Alt.OnPlayerEvent           += (player, name, args) => { Alt.Log("event:" + name); };
            AltAsync.OnPlayerEvent      += (player, name, args) =>
            {
                AltAsync.Log("event:" + name);
                return(Task.CompletedTask);
            };
            AltAsync.OnServer("bla",
                              async args => { await AltAsync.Do(() => Alt.Log("bla with no args:" + args.Length)); });
            Alt.Emit("bla");

            var blip = Alt.CreateBlip(BlipType.Area, Position.Zero);

            blip.Color = 1;

            var checkpoint = Alt.CreateCheckpoint(CheckpointType.Cyclinder, Position.Zero, 1f, 1f, Rgba.Zero);

            Alt.Log(checkpoint.Color.ToString());

            var voiceChannel = Alt.CreateVoiceChannel(true, 10f);

            Alt.Log(voiceChannel.MaxDistance.ToString());

            var vehicle = Alt.CreateVehicle(VehicleModel.Apc, new Position(1, 2, 3), new Rotation(1, 2, 3));

            Alt.Log(vehicle.Position.ToString());
            vehicle.PrimaryColor     = 7;
            vehicle.NumberplateText  = "AltV-C#";
            vehicle.NumberplateIndex = 2;
            vehicle.SetMod(0, 0);
            vehicle.GetMod(0);
            vehicle.GetModsCount(0);
            vehicle.Dimension       = 0;
            vehicle.LockState       = VehicleLockState.Locked;
            vehicle.PrimaryColorRgb = new Rgba
            {
                R = 1,
                G = 8,
                B = 7,
                A = 0
            };

            var(x, y, z) = vehicle.GetPosition();
            vehicle.SetPosition(1, 1, 1);
            vehicle.SetPosition((x, y, z));
            var tuple = vehicle.GetPosition();

            vehicle.SetPosition(tuple);


            Task.Factory.StartNew(() =>
                                  AltAsync.CreateVehicleBuilder(VehicleModel.Apc, new Position(1, 2, 3), new Rotation(1, 2, 3))
                                  .PrimaryColorRgb(Color.GreenYellow)
                                  .SecondaryColor(24)
                                  .NumberplateText("C#")
                                  .LockState(VehicleLockState.Locked)
                                  .Build()
                                  );

            Alt.Log("ptr:" + vehicle.NativePointer);

            Alt.Log("number-plate:" + vehicle.NumberplateText + " " + vehicle.NumberplateIndex);

            Alt.Emit("vehicleTest", vehicle);

            Alt.On("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)
            {
                Alt.Log("param1:" + s);
                Alt.Log("param2:" + s1);
                Alt.Log("bla:" + ((object[])arg4[1])[0]);
                Alt.Log("myData-2: " + arg5.Position.X + " " + arg5.MyData);
                Alt.Log("myData-4-veh-array: " + myVehicles[0].Position.X + " " + myVehicles[0].MyData);
                Alt.Log("myData-3: " + arg6["test"]);
                Alt.Log("null?" + (probablyNull == null ? "y" : "n"));
                Alt.Log("null2?" + (nullArray[0] == null ? "y" : "n"));
                Alt.Log("bla2:" + bla["test"]);
            });

            Alt.On("entity-array-obj",
                   delegate(object[] myVehicles)
            {
                Alt.Log("entity-array-obj: " + ((MyVehicle)myVehicles[0]).Position.X + " " +
                        ((MyVehicle)myVehicles[0]).MyData);
            });

            AltAsync.On("event_name",
                        async 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)
            {
                await Task.Delay(500);
                var asyncVehicle =
                    await AltAsync.CreateVehicle(VehicleModel.Apc, Position.Zero, new Rotation(1, 2, 3));

                AltAsync.Log("async-param1:" + s);
                AltAsync.Log("async-param2:" + s1);
                AltAsync.Log("async-bla:" + ((object[])arg4[1])[0]);
                AltAsync.Log("exists:" + arg5.Exists);
                AltAsync.Log("async-myData-2: " + arg5.Position.X + " " + arg5.MyData);
                AltAsync.Log("async-myData-4: " + myVehicles[0].Position.X + " " + myVehicles[0].MyData);
                AltAsync.Log("async-myData-3: " + arg6["test"]);
                AltAsync.Log("async-null?" + (probablyNull == null ? "y" : "n"));
                AltAsync.Log("async-null2?" + (nullArray[0] == null ? "y" : "n"));
                AltAsync.Log("async-bla2:" + bla["test"]);
            });

            Alt.Emit("event_name", "param_string_1", "param_string_2", 1, new[] { "array_1", "array_2" },
                     new object[] { "test", new[] { 1337 } }, vehicle,
                     new Dictionary <string, object>
            {
                ["test"] = "test" //,
                                  //["test2"] = new Dictionary<string, long> {["test"] = 1},
                                  //["test3"] = new Dictionary<string, long> {["test"] = 42}
            },
                     new IMyVehicle[] { (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.Emit("test_string_array", new object[] { new string[] { "bla" } });

            Alt.On("function_event", delegate(Function.Func func)
            {
                var result = func("parameter1");
                Alt.Log("result:" + result);
            });

            Alt.Emit("function_event", Function.Create(delegate(string bla)
            {
                Alt.Log("parameter=" + bla);
                return(42);
            }));

            foreach (var player in Alt.GetAllPlayers())
            {
            }

            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); });

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

            Alt.Emit("1337", 1);

            Alt.On <IMyVehicle>("MyServerEvent3", MyServerEventHandler2, MyServerEventParser3);

            Alt.On <IMyVehicle>("MyServerEvent3", MyServerEventHandlerAsync, MyServerEventParserAsync);

            Alt.Emit("MyServerEvent3", vehicle);

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

            vehicle.Remove();

            Bla();

            Alt.On <IPlayer, string>("MyEvent", MyEventHandler, MyParser);

            Alt.On <string>("MyServerEvent", MyServerEventHandler, MyServerEventParser);

            Alt.On <string>("MyServerEvent2", MyServerEventHandler, MyServerEventParser2);

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

            Alt.Emit("MyServerEvent2", new object[] { new string[] { "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);

            action();

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

            Action <string> action2;

            Alt.Import("example", "functionExport", out action2);

            action2("123");

            /*if (Alt.Import("Bla", "GetBla", out Action value))
             * {
             *  value();
             * }*/

            Alt.Emit("none-existing-event", new WritableObject());

            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.
            // But when you don't use a entity factory you can validate that by checking if the ivehicle is a imyvehicle
            try
            {
                IMyVehicle myConstructedVehicle =
                    new MyVehicle((uint)VehicleModel.Apc, new Position(1, 1, 1), new Rotation(1, 1, 1));
            }
            catch (BaseObjectRemovedException baseObjectRemovedException)
            {
            }

            Alt.RegisterEvents(this);

            Alt.Emit("bla2", "bla");

            AltAsync.RegisterEvents(this);

            Alt.Emit("asyncBla3", "bla");

            Alt.OnColShape += (shape, entity, state) =>
            {
                Console.WriteLine("collision shape test:" + shape + " " + shape.GetData("bla", out int id1) + " " + id1);
                Console.WriteLine(" " + shape + " " + shape.GetMetaData("bla", out long id2) + " " + id2 + " " + entity + " " + state);
            };

            var colShapeCylinder = Alt.CreateColShapeCylinder(new Position(1337, 1337, 1337), 10, 10);

            colShapeCylinder.SetMetaData("bla", 1);
            colShapeCylinder.SetData("bla", (int)2);

            var colShapeCircle = Alt.CreateColShapeCircle(new Position(1337, 1337, 1337), 10);

            colShapeCircle.SetMetaData("bla", 3);
            colShapeCircle.SetData("bla", (int)4);

            AltChat.SendBroadcast("Test");

            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());
        }