api stufz

This commit is contained in:
_nebula 2023-07-29 01:26:12 +01:00
parent d76495e4b5
commit 4ed9ea62ac
6 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QSB.Utility;
using UnityEngine;
namespace QSB.API;
public static class AddonDataManager
{
private static Dictionary<string, (Type objectType, dynamic action)> _handlerDict = new();
public static void OnReceiveDataMessage(string messageType, object data)
{
DebugLog.DebugWrite($"Received data message of message type \"{messageType}\"!");
if (!_handlerDict.ContainsKey(messageType))
{
return;
}
_handlerDict[messageType].action(Convert.ChangeType(data, _handlerDict[messageType].objectType));
}
public static void RegisterHandler<T>(string messageType, Action<T> handler)
{
DebugLog.DebugWrite($"Registering handler for \"{messageType}\" with type of {typeof(T).Name}");
_handlerDict.Add(messageType, (typeof(T), handler));
}
}

14
QSB/API/IQSBAPI.cs Normal file
View File

@ -0,0 +1,14 @@
using System;
namespace QSB.API;
internal interface IQSBAPI
{
uint GetLocalPlayerID();
void SetCustomData<T>(uint playerId, string key, T data);
T GetCustomData<T>(uint playerId, string key);
void SendMessage<T>(string messageType, T data);
void RegisterHandler<T>(string messageType, Action<T> action);
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using QSB.Messaging;
namespace QSB.API.Messages;
public class AddonDataMessage : QSBMessage<(string messageType, byte[] data)>
{
public AddonDataMessage(string messageType, object data) : base((messageType, ObjectToByteArray(data))) {}
private static byte[] ObjectToByteArray(object obj)
{
var bf = new BinaryFormatter();
using var ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
private static object ByteArrayToObject(byte[] arrBytes)
{
using var memStream = new MemoryStream();
var binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
var obj = binForm.Deserialize(memStream);
return obj;
}
public override void OnReceiveRemote()
{
var obj = ByteArrayToObject(Data.data);
AddonDataManager.OnReceiveDataMessage(Data.messageType, obj);
}
}

28
QSB/API/QSBAPI.cs Normal file
View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QSB.API.Messages;
using QSB.Messaging;
using QSB.Player;
namespace QSB.API;
internal class QSBAPI : IQSBAPI
{
public uint GetLocalPlayerID() => QSBPlayerManager.LocalPlayerId;
public void SetCustomData<T>(uint playerId, string key, T data) => QSBPlayerManager.GetPlayer(playerId).SetCustomData(key, data);
public T GetCustomData<T>(uint playerId, string key) => QSBPlayerManager.GetPlayer(playerId).GetCustomData<T>(key);
public void SendMessage<T>(string messageType, T data)
{
new AddonDataMessage(messageType, data).Send();
}
public void RegisterHandler<T>(string messageType, Action<T> action)
{
AddonDataManager.RegisterHandler(messageType, action);
}
}

View File

@ -68,6 +68,7 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="OuterWildsGameLibs" Version="1.1.13.457" IncludeAssets="compile" />
<PackageReference Include="OWML" Version="2.9.3" IncludeAssets="compile" />
<Reference Include="..\Mirror\*.dll" />

View File

@ -16,6 +16,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using QSB.API;
using UnityEngine;
using UnityEngine.InputSystem;
@ -86,6 +87,8 @@ public class QSBCore : ModBehaviour
"PacificEngine.OW_Randomizer",
};
public override object GetApi() => new QSBAPI();
private static void DetermineGameVendor()
{
var gameAssemblyTypes = typeof(AstroObject).Assembly.GetTypes();