Some works for the new resource system
Rewrite some parts of CustomEvent Expose some API as dll entry
This commit is contained in:
97
Client/Scripts/Scripting/API.Exports.cs
Normal file
97
Client/Scripts/Scripting/API.Exports.cs
Normal file
@ -0,0 +1,97 @@
|
||||
using Lidgren.Network;
|
||||
using RageCoop.Core.Scripting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using static RageCoop.Core.Scripting.CustomEvents;
|
||||
|
||||
namespace RageCoop.Client.Scripting
|
||||
{
|
||||
public static unsafe partial class API
|
||||
{
|
||||
[UnmanagedCallersOnly(EntryPoint = "Connect")]
|
||||
public static void Connect(char* address) => Connect(new string(address));
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "GetLocalPlayerID")]
|
||||
public static int GetLocalPlayerID() => LocalPlayerID;
|
||||
|
||||
/// <summary>
|
||||
/// Get configuration value
|
||||
/// </summary>
|
||||
/// <param name="szName">The name of the config</param>
|
||||
/// <param name="buf">Buffer to store retrived value</param>
|
||||
/// <param name="bufSize">Buffer size</param>
|
||||
/// <returns>The string length of returned value, not including the null terminator</returns>
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "GetConfigValue")]
|
||||
public static int GetConfigValue(char* szName, char* buf, int bufSize)
|
||||
{
|
||||
var name = new string(szName);
|
||||
var value = name switch
|
||||
{
|
||||
nameof(Config.EnableAutoRespawn) => Config.EnableAutoRespawn.ToString(),
|
||||
nameof(Config.Username) => Config.Username.ToString(),
|
||||
nameof(Config.BlipColor) => Config.BlipColor.ToString(),
|
||||
nameof(Config.BlipScale) => Config.BlipScale.ToString(),
|
||||
nameof(Config.BlipSprite) => Config.BlipSprite.ToString(),
|
||||
_ => null
|
||||
};
|
||||
|
||||
if (value == null)
|
||||
return 0;
|
||||
|
||||
fixed (char* p = value)
|
||||
{
|
||||
var cbRequired = (value.Length + 1) * sizeof(char);
|
||||
Buffer.MemoryCopy(p, buf, bufSize, cbRequired);
|
||||
return value.Length;
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "SetConfigValue")]
|
||||
public static void SetConfigValue(char* szName, char* szValue)
|
||||
{
|
||||
var name = new string(szName);
|
||||
var value = new string(szValue);
|
||||
switch (name)
|
||||
{
|
||||
case nameof(Config.EnableAutoRespawn): Config.EnableAutoRespawn = bool.Parse(value); break;
|
||||
case nameof(Config.Username): Config.Username = value; break;
|
||||
case nameof(Config.BlipColor): Config.BlipColor = Enum.Parse<BlipColor>(value); break;
|
||||
case nameof(Config.BlipScale): Config.BlipScale = float.Parse(value); break;
|
||||
case nameof(Config.BlipSprite): Config.BlipSprite = Enum.Parse<BlipSprite>(value); break;
|
||||
};
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "LocalChatMessage")]
|
||||
public static void LocalChatMessage(char* from, char* msg) => LocalChatMessage(new string(from), new string(msg));
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "SendChatMessage")]
|
||||
public static void SendChatMessage(char* msg) => SendChatMessage(new string(msg));
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "GetEventHash")]
|
||||
public static CustomEventHash GetEventHash(char* name)=>new string(name);
|
||||
|
||||
public static void SendCustomEvent(int hash, CustomEventFlags flags, byte* data, int cbData)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert Entity ID to handle
|
||||
/// </summary>
|
||||
[UnmanagedCallersOnly(EntryPoint = "IdToHandle")]
|
||||
public static int IdToHandle(byte type, int id)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
T_ID_PROP => EntityPool.GetPropByID(id)?.MainProp?.Handle ?? 0,
|
||||
T_ID_PED => EntityPool.GetPedByID(id)?.MainPed?.Handle ?? 0,
|
||||
T_ID_VEH => EntityPool.GetVehicleByID(id)?.MainVehicle?.Handle ?? 0,
|
||||
T_ID_BLIP => EntityPool.GetBlipByID(id)?.Handle ?? 0,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using GTA;
|
||||
using Lidgren.Network;
|
||||
@ -30,7 +31,7 @@ namespace RageCoop.Client.Scripting
|
||||
/// <summary>
|
||||
/// Provides vital functionality to interact with RAGECOOP
|
||||
/// </summary>
|
||||
public static class API
|
||||
public static unsafe partial class API
|
||||
{
|
||||
#region INTERNAL
|
||||
|
||||
@ -270,6 +271,7 @@ namespace RageCoop.Client.Scripting
|
||||
/// <summary>
|
||||
/// Disconnect from current server or cancel the connection attempt.
|
||||
/// </summary>
|
||||
[UnmanagedCallersOnly(EntryPoint = "Disconnect")]
|
||||
public static void Disconnect()
|
||||
{
|
||||
if (Networking.IsOnServer || Networking.IsConnecting) Networking.ToggleConnection(null);
|
||||
@ -369,9 +371,9 @@ namespace RageCoop.Client.Scripting
|
||||
};
|
||||
DownloadManager.DownloadCompleted += handler;
|
||||
Networking.GetResponse<Packets.FileTransferResponse>(new Packets.FileTransferRequest
|
||||
{
|
||||
Name = name
|
||||
},
|
||||
{
|
||||
Name = name
|
||||
},
|
||||
p =>
|
||||
{
|
||||
if (p.Response != FileResponse.Loaded)
|
||||
|
Reference in New Issue
Block a user