slightly tweak

This commit is contained in:
JohnCorby 2023-07-29 16:48:58 -07:00
parent 2f37664057
commit 4d545d12f3
7 changed files with 19 additions and 23 deletions

View File

@ -15,6 +15,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="OuterWildsGameLibs" Version="1.1.13.457" IncludeAssets="compile" />
<PackageReference Include="OWML" Version="2.9.3" IncludeAssets="compile" />
<PackageReference Include="OWML" Version="2.9.4" IncludeAssets="compile" />
</ItemGroup>
</Project>

View File

@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OuterWildsGameLibs" Version="1.1.13.457" />
<PackageReference Include="OWML" Version="2.9.3" />
<PackageReference Include="OWML" Version="2.9.4" />
<Reference Include="../Mirror/*.dll" />
</ItemGroup>
<ItemGroup>

View File

@ -1,26 +1,22 @@
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, Action<object> action)> _handlerDict = new();
private static readonly Dictionary<string, (Type objectType, Action<object> action)> _handlerDict = new();
public static void OnReceiveDataMessage(string messageType, object data)
{
DebugLog.DebugWrite($"Received data message of message type \"{messageType}\"!");
if (!_handlerDict.ContainsKey(messageType))
if (!_handlerDict.TryGetValue(messageType, out var handler))
{
return;
}
_handlerDict[messageType].action(Convert.ChangeType(data, _handlerDict[messageType].objectType));
handler.action(Convert.ChangeType(data, handler.objectType));
}
public static void RegisterHandler<T>(string messageType, Action<T> handler)

View File

@ -2,6 +2,7 @@
namespace QSB.API;
// TODO: document
public interface IQSBAPI
{
uint GetLocalPlayerID();

View File

@ -3,31 +3,31 @@ using System.Runtime.Serialization.Formatters.Binary;
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))) {}
public AddonDataMessage(string messageType, object data) : base((messageType, Obj2Bytes(data))) { }
private static byte[] ObjectToByteArray(object obj)
private static byte[] Obj2Bytes(object obj)
{
var bf = new BinaryFormatter();
using var ms = new MemoryStream();
var bf = new BinaryFormatter();
bf.Serialize(ms, obj);
return ms.ToArray();
var bytes = ms.ToArray();
return bytes;
}
private static object ByteArrayToObject(byte[] arrBytes)
private static object Bytes2Obj(byte[] bytes)
{
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);
using var ms = new MemoryStream(bytes);
var bf = new BinaryFormatter();
var obj = bf.Deserialize(ms);
return obj;
}
public override void OnReceiveRemote()
{
var obj = ByteArrayToObject(Data.data);
var obj = Bytes2Obj(Data.data);
AddonDataManager.OnReceiveDataMessage(Data.messageType, obj);
}
}

View File

@ -186,12 +186,12 @@ public partial class PlayerInfo
public T GetCustomData<T>(string key)
{
if (!_customData.ContainsKey(key))
if (!_customData.TryGetValue(key, out var value))
{
return default;
}
return (T)_customData[key];
return (T)value;
}
public override string ToString() => $"{PlayerId}:{GetType().Name} ({Name})";

View File

@ -68,7 +68,6 @@
</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.4" IncludeAssets="compile" />
<Reference Include="..\Mirror\*.dll" />