Files
RAGECOOP-V/Client/Scripts/Scripting/BaseScript.cs

294 lines
11 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
2022-07-20 17:50:01 +08:00
using System.Collections.Generic;
2022-07-10 16:13:08 +08:00
using System.Threading;
2022-07-20 17:50:01 +08:00
using System.Threading.Tasks;
2022-10-23 19:02:39 +08:00
using GTA;
using GTA.Math;
using GTA.Native;
using GTA.UI;
using RageCoop.Core.Scripting;
2022-06-22 14:18:20 +08:00
namespace RageCoop.Client.Scripting
{
2022-10-15 17:06:19 +08:00
internal static class BaseScript
2022-06-22 14:18:20 +08:00
{
2022-10-23 19:02:39 +08:00
private static bool _isHost;
2022-10-15 17:06:19 +08:00
public static void OnStart()
2022-06-22 14:18:20 +08:00
{
2022-09-06 21:46:35 +08:00
API.Events.OnPedDeleted += (s, p) => { API.SendCustomEvent(CustomEvents.OnPedDeleted, p.ID); };
API.Events.OnVehicleDeleted += (s, p) => { API.SendCustomEvent(CustomEvents.OnVehicleDeleted, p.ID); };
API.Events.OnPlayerDied += () => { API.SendCustomEvent(CustomEvents.OnPlayerDied); };
2022-07-10 16:13:08 +08:00
2022-07-20 17:50:01 +08:00
API.RegisterCustomEventHandler(CustomEvents.SetAutoRespawn, SetAutoRespawn);
API.RegisterCustomEventHandler(CustomEvents.SetDisplayNameTag, SetDisplayNameTag);
API.RegisterCustomEventHandler(CustomEvents.NativeCall, NativeCall);
2022-07-02 17:14:56 +08:00
API.RegisterCustomEventHandler(CustomEvents.ServerPropSync, ServerObjectSync);
API.RegisterCustomEventHandler(CustomEvents.DeleteServerProp, DeleteServerProp);
API.RegisterCustomEventHandler(CustomEvents.DeleteEntity, DeleteEntity);
API.RegisterCustomEventHandler(CustomEvents.SetDisplayNameTag, SetNameTag);
2022-07-03 15:28:28 +08:00
API.RegisterCustomEventHandler(CustomEvents.ServerBlipSync, ServerBlipSync);
API.RegisterCustomEventHandler(CustomEvents.DeleteServerBlip, DeleteServerBlip);
2022-07-04 21:29:13 +08:00
API.RegisterCustomEventHandler(CustomEvents.CreateVehicle, CreateVehicle);
2022-07-04 23:59:51 +08:00
API.RegisterCustomEventHandler(CustomEvents.UpdatePedBlip, UpdatePedBlip);
2022-10-23 19:02:39 +08:00
API.RegisterCustomEventHandler(CustomEvents.IsHost, e => { _isHost = (bool)e.Args[0]; });
2022-07-10 16:13:08 +08:00
API.RegisterCustomEventHandler(CustomEvents.WeatherTimeSync, WeatherTimeSync);
2022-10-23 19:02:39 +08:00
API.RegisterCustomEventHandler(CustomEvents.OnPlayerDied,
e => { Notification.Show($"~h~{e.Args[0]}~h~ died."); });
ThreadManager.CreateThread(() =>
{
2023-02-13 20:44:50 +08:00
while (!IsUnloading)
{
2023-02-13 20:44:50 +08:00
if (Networking.IsOnServer && _isHost)
API.QueueAction(() =>
{
unsafe
{
var time = World.CurrentTimeOfDay;
var weather1 = default(int);
var weather2 = default(int);
var percent2 = default(float);
Call(GET_CURR_WEATHER_STATE, &weather1, &weather2, &percent2);
API.SendCustomEvent(CustomEvents.WeatherTimeSync, time.Hours, time.Minutes,
time.Seconds, weather1, weather2, percent2);
}
});
Thread.Sleep(1000);
}
2023-02-13 20:44:50 +08:00
}, "BaseScript");
2022-07-10 16:13:08 +08:00
}
2022-10-15 17:06:19 +08:00
private static void WeatherTimeSync(CustomEventReceivedArgs e)
2022-07-10 16:13:08 +08:00
{
2022-09-06 21:46:35 +08:00
World.CurrentTimeOfDay = new TimeSpan((int)e.Args[0], (int)e.Args[1], (int)e.Args[2]);
Call(SET_CURR_WEATHER_STATE, (int)e.Args[3], (int)e.Args[4], (float)e.Args[5]);
2022-07-03 15:28:28 +08:00
}
2022-10-15 17:06:19 +08:00
private static void SetDisplayNameTag(CustomEventReceivedArgs e)
2022-07-09 19:32:11 +08:00
{
var p = PlayerList.GetPlayer((int)e.Args[0]);
2022-10-23 19:02:39 +08:00
if (p != null) p.DisplayNameTag = (bool)e.Args[1];
2022-07-09 19:32:11 +08:00
}
2022-10-15 17:06:19 +08:00
private static void UpdatePedBlip(CustomEventReceivedArgs e)
2022-07-04 23:59:51 +08:00
{
2022-07-11 11:59:32 +08:00
var p = Entity.FromHandle((int)e.Args[0]);
2022-10-23 19:02:39 +08:00
if (p == null) return;
2022-09-06 21:46:35 +08:00
if (p.Handle == Game.Player.Character.Handle)
2022-07-04 23:59:51 +08:00
{
2022-09-06 21:46:35 +08:00
API.Config.BlipColor = (BlipColor)(byte)e.Args[1];
API.Config.BlipSprite = (BlipSprite)(ushort)e.Args[2];
API.Config.BlipScale = (float)e.Args[3];
2022-07-05 11:18:26 +08:00
}
else
{
var b = p.AttachedBlip;
2022-10-23 19:02:39 +08:00
if (b == null) b = p.AddBlip();
2022-09-06 21:46:35 +08:00
b.Color = (BlipColor)(byte)e.Args[1];
b.Sprite = (BlipSprite)(ushort)e.Args[2];
b.Scale = (float)e.Args[3];
2022-07-05 11:18:26 +08:00
}
2022-07-04 23:59:51 +08:00
}
2022-10-15 17:06:19 +08:00
private static void CreateVehicle(CustomEventReceivedArgs e)
2022-07-04 21:29:13 +08:00
{
2022-07-09 11:21:42 +08:00
var vehicleModel = (Model)e.Args[1];
vehicleModel.Request(1000);
2022-10-08 12:43:24 +08:00
Vehicle veh;
while ((veh = World.CreateVehicle(vehicleModel, (Vector3)e.Args[2], (float)e.Args[3])) == null)
2022-07-11 11:59:32 +08:00
Thread.Sleep(10);
2022-09-06 21:46:35 +08:00
veh.CanPretendOccupants = false;
2022-10-23 19:02:39 +08:00
var v = new SyncedVehicle
2022-07-04 21:29:13 +08:00
{
2022-09-06 21:46:35 +08:00
ID = (int)e.Args[0],
MainVehicle = veh,
2023-02-13 20:44:50 +08:00
OwnerID = LocalPlayerID
2022-07-05 11:18:26 +08:00
};
EntityPool.Add(v);
2022-07-04 21:29:13 +08:00
}
2022-10-15 17:06:19 +08:00
private static void DeleteServerBlip(CustomEventReceivedArgs e)
2022-07-03 15:28:28 +08:00
{
if (EntityPool.ServerBlips.TryGetValue((int)e.Args[0], out var blip))
{
EntityPool.ServerBlips.Remove((int)e.Args[0]);
2022-07-05 11:18:26 +08:00
blip?.Delete();
2022-07-03 15:28:28 +08:00
}
}
2022-10-15 17:06:19 +08:00
private static void ServerBlipSync(CustomEventReceivedArgs obj)
2022-07-03 15:28:28 +08:00
{
2022-10-23 19:02:39 +08:00
var id = (int)obj.Args[0];
2022-07-20 17:50:01 +08:00
var sprite = (BlipSprite)(ushort)obj.Args[1];
2022-07-05 00:18:12 +08:00
var color = (BlipColor)(byte)obj.Args[2];
2022-07-20 17:50:01 +08:00
var scale = (float)obj.Args[3];
var pos = (Vector3)obj.Args[4];
2022-10-23 19:02:39 +08:00
var rot = (int)obj.Args[5];
2022-07-20 17:50:01 +08:00
var name = (string)obj.Args[6];
2022-10-23 19:02:39 +08:00
if (!EntityPool.ServerBlips.TryGetValue(id, out var blip))
2022-09-06 21:46:35 +08:00
EntityPool.ServerBlips.Add(id, blip = World.CreateBlip(pos));
2022-07-05 11:18:26 +08:00
blip.Sprite = sprite;
blip.Color = color;
blip.Scale = scale;
blip.Position = pos;
blip.Rotation = rot;
blip.Name = name;
2022-06-22 14:18:20 +08:00
}
2022-10-15 17:06:19 +08:00
private static void DeleteEntity(CustomEventReceivedArgs e)
{
2022-07-05 11:18:26 +08:00
Entity.FromHandle((int)e.Args[0])?.Delete();
}
2022-10-15 17:06:19 +08:00
private static void SetNameTag(CustomEventReceivedArgs e)
{
2022-07-20 17:50:01 +08:00
var p = PlayerList.GetPlayer((int)e.Args[0]);
2022-10-23 19:02:39 +08:00
if (p != null) p.DisplayNameTag = (bool)e.Args[1];
}
2022-10-23 19:02:39 +08:00
2022-10-15 17:06:19 +08:00
private static void SetAutoRespawn(CustomEventReceivedArgs args)
2022-06-22 14:18:20 +08:00
{
2022-09-06 21:46:35 +08:00
API.Config.EnableAutoRespawn = (bool)args.Args[0];
2022-06-22 14:18:20 +08:00
}
2022-10-23 19:02:39 +08:00
2022-10-15 17:06:19 +08:00
private static void DeleteServerProp(CustomEventReceivedArgs e)
2022-07-02 17:14:56 +08:00
{
var id = (int)e.Args[0];
if (EntityPool.ServerProps.TryGetValue(id, out var prop))
{
EntityPool.ServerProps.Remove(id);
2022-07-05 11:18:26 +08:00
prop?.MainProp?.Delete();
2022-07-02 17:14:56 +08:00
}
}
2022-10-23 19:02:39 +08:00
2022-10-15 17:06:19 +08:00
private static void ServerObjectSync(CustomEventReceivedArgs e)
2022-07-02 17:14:56 +08:00
{
SyncedProp prop;
var id = (int)e.Args[0];
lock (EntityPool.PropsLock)
2022-07-02 17:14:56 +08:00
{
if (!EntityPool.ServerProps.TryGetValue(id, out prop))
2022-09-06 21:46:35 +08:00
EntityPool.ServerProps.Add(id, prop = new SyncedProp(id));
2022-07-02 17:14:56 +08:00
}
2022-10-23 19:02:39 +08:00
2023-02-13 20:44:50 +08:00
prop.LastSynced = Ticked + 1;
2022-09-06 21:46:35 +08:00
prop.Model = (Model)e.Args[1];
prop.Position = (Vector3)e.Args[2];
prop.Rotation = (Vector3)e.Args[3];
2022-10-10 16:45:58 +08:00
prop.Model.Request(1000);
2022-07-05 11:18:26 +08:00
prop.Update();
2022-07-02 17:14:56 +08:00
}
2022-10-23 19:02:39 +08:00
2022-10-15 17:06:19 +08:00
private static void NativeCall(CustomEventReceivedArgs e)
2022-06-23 09:46:38 +08:00
{
2022-10-23 19:02:39 +08:00
var arguments = new List<InputArgument>();
2022-06-23 09:46:38 +08:00
int i;
var ty = (byte)e.Args[0];
2022-10-23 19:02:39 +08:00
var returnType = (TypeCode)ty;
2022-09-06 21:46:35 +08:00
i = returnType == TypeCode.Empty ? 1 : 2;
2022-06-23 09:46:38 +08:00
var hash = (Hash)e.Args[i++];
2022-10-23 19:02:39 +08:00
for (; i < e.Args.Length; i++) arguments.Add(GetInputArgument(e.Args[i]));
2022-06-23 09:46:38 +08:00
2022-09-06 21:46:35 +08:00
if (returnType == TypeCode.Empty)
2022-07-05 11:18:26 +08:00
{
Call(hash, arguments.ToArray());
2022-07-05 11:18:26 +08:00
return;
}
2022-10-23 19:02:39 +08:00
var id = (int)e.Args[1];
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
switch (returnType)
{
case TypeCode.Boolean:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<bool>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
case TypeCode.Byte:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<byte>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
case TypeCode.Char:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<char>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.Single:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<float>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.Double:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<double>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.Int16:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<short>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.Int32:
API.SendCustomEvent(CustomEvents.NativeResponse, id, Call<int>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.Int64:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<long>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.String:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<string>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.UInt16:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<ushort>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.UInt32:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<uint>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
2022-06-23 09:46:38 +08:00
2022-07-05 11:18:26 +08:00
case TypeCode.UInt64:
2022-10-23 19:02:39 +08:00
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Call<ulong>(hash, arguments.ToArray()));
2022-07-05 11:18:26 +08:00
break;
}
2022-06-23 09:46:38 +08:00
}
2022-10-23 19:02:39 +08:00
2022-10-15 17:06:19 +08:00
private static InputArgument GetInputArgument(object obj)
2022-06-23 09:46:38 +08:00
{
2022-11-19 12:54:22 +08:00
// Implicit conversion
switch (obj)
{
case byte stuff:
return stuff;
case short stuff:
return stuff;
case ushort stuff:
return stuff;
case int stuff:
return stuff;
case uint stuff:
return stuff;
case long stuff:
return stuff;
case ulong stuff:
return stuff;
case float stuff:
return stuff;
case bool stuff:
return stuff;
case string stuff:
return stuff;
default:
return default;
2022-11-19 12:54:22 +08:00
}
2022-07-20 17:50:01 +08:00
}
2022-06-22 14:18:20 +08:00
}
2022-10-23 19:02:39 +08:00
}