173 lines
6.5 KiB
C#
Raw Normal View History

2022-06-22 14:18:20 +08:00
using System;
using System.Collections.Generic;
2022-06-23 09:46:38 +08:00
using GTA.Native;
2022-07-02 17:14:56 +08:00
using GTA.Math;
using GTA;
using RageCoop.Core;
2022-06-22 14:18:20 +08:00
using RageCoop.Core.Scripting;
2022-07-02 17:14:56 +08:00
using System.Linq;
2022-06-22 14:18:20 +08:00
namespace RageCoop.Client.Scripting
{
internal class BaseScript : ClientScript
{
public override void OnStart()
{
API.RegisterCustomEventHandler(CustomEvents.SetAutoRespawn,SetAutoRespawn);
2022-06-23 09:46:38 +08:00
API.RegisterCustomEventHandler(CustomEvents.NativeCall,NativeCall);
2022-07-02 17:14:56 +08:00
API.RegisterCustomEventHandler(CustomEvents.ServerPropSync, ServerObjectSync);
API.RegisterCustomEventHandler(CustomEvents.DeleteServerProp, DeleteServerProp);
API.Events.OnPedDeleted+=(s,p) => { API.SendCustomEvent(CustomEvents.OnPedDeleted,p.ID); };
API.Events.OnVehicleDeleted+=(s, p) => { API.SendCustomEvent(CustomEvents.OnVehicleDeleted, p.ID); };
2022-06-22 14:18:20 +08:00
}
public override void OnStop()
{
}
private void SetAutoRespawn(CustomEventReceivedArgs args)
2022-06-22 14:18:20 +08:00
{
API.Config.EnableAutoRespawn=(bool)args.Args[0];
}
2022-07-02 17:14:56 +08:00
private void DeleteServerProp(CustomEventReceivedArgs e)
{
var id = (int)e.Args[0];
if (EntityPool.ServerProps.TryGetValue(id, out var prop))
{
EntityPool.ServerProps.Remove(id);
API.QueueAction(() => { prop?.MainProp?.Delete(); });
}
}
private void ServerObjectSync(CustomEventReceivedArgs e)
{
SyncedProp prop;
var id = (int)e.Args[0];
if (!EntityPool.ServerProps.TryGetValue(id,out prop))
{
EntityPool.ServerProps.Add(id,prop=new SyncedProp(id));
}
prop.LastSynced=Main.Ticked+1;
prop.ModelHash= (Model)e.Args[1];
prop.Position=(Vector3)e.Args[2];
prop.Rotation=(Vector3)e.Args[3];
Main.Logger.Trace($"{Main.Ticked},{(VehicleHash)prop.ModelHash},{prop.Position},{prop.Rotation}");
}
2022-06-23 09:46:38 +08:00
private void NativeCall(CustomEventReceivedArgs e)
{
List<InputArgument> arguments = new List<InputArgument>();
int i;
var ty = (byte)e.Args[0];
TypeCode returnType=(TypeCode)ty;
i = returnType==TypeCode.Empty ? 1 : 2;
var hash = (Hash)e.Args[i++];
for(; i<e.Args.Count;i++)
{
arguments.Add(GetInputArgument(e.Args[i]));
}
Main.QueueAction(() =>
{
if (returnType==TypeCode.Empty)
{
Function.Call(hash, arguments.ToArray());
return;
}
var t = returnType;
int id = (int)e.Args[1];
switch (returnType)
{
case TypeCode.Boolean:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<bool>(hash, arguments.ToArray()) });
break;
case TypeCode.Byte:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<byte>(hash, arguments.ToArray()) });
break;
case TypeCode.Char:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<char>(hash, arguments.ToArray()) });
break;
case TypeCode.Single:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<float>(hash, arguments.ToArray()) });
break;
case TypeCode.Double:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<double>(hash, arguments.ToArray()) });
break;
case TypeCode.Int16:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<short>(hash, arguments.ToArray()) });
break;
case TypeCode.Int32:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<int>(hash, arguments.ToArray()) });
break;
case TypeCode.Int64:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<long>(hash, arguments.ToArray()) });
break;
case TypeCode.String:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<string>(hash, arguments.ToArray()) });
break;
case TypeCode.UInt16:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<ushort>(hash, arguments.ToArray()) });
break;
case TypeCode.UInt32:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<uint>(hash, arguments.ToArray()) });
break;
case TypeCode.UInt64:
API.SendCustomEvent(CustomEvents.NativeResponse,
new List<object> { id, Function.Call<ulong>(hash, arguments.ToArray()) });
break;
}
});
}
private InputArgument GetInputArgument(object obj)
{
// Implicit conversion
switch (obj)
{
case byte _:
return (byte)obj;
case short _:
return (short)obj;
case ushort _:
return (ushort)obj;
case int _:
return (int)obj;
case uint _:
return (uint)obj;
case long _:
return (long)obj;
case ulong _:
return (ulong)obj;
case float _:
return (float)obj;
case bool _:
return (bool)obj;
case string _:
return (obj as string);
default:
return null;
}
}
2022-06-22 14:18:20 +08:00
}
}