mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-29 18:32:45 +00:00
slightly tweak
This commit is contained in:
parent
2f37664057
commit
4d545d12f3
@ -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>
|
||||
|
@ -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>
|
||||
|
@ -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)
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace QSB.API;
|
||||
|
||||
// TODO: document
|
||||
public interface IQSBAPI
|
||||
{
|
||||
uint GetLocalPlayerID();
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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})";
|
||||
|
@ -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" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user