Merge pull request #638 from misternebula/new-api

API v2
This commit is contained in:
_nebula 2023-07-30 10:49:10 +01:00 committed by GitHub
commit 1ac8fccb96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 120 additions and 33 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

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using QSB.Utility;
namespace QSB.API;
public static class AddonDataManager
{
private static readonly Dictionary<string, Action<object>> _handlers = new();
public static void OnReceiveDataMessage(string messageType, object data)
{
DebugLog.DebugWrite($"Received data message of message type \"{messageType}\"!");
if (!_handlers.TryGetValue(messageType, out var handler))
{
return;
}
handler(data);
}
public static void RegisterHandler<T>(string messageType, Action<T> handler)
{
DebugLog.DebugWrite($"Registering handler for \"{messageType}\" with type of {typeof(T).Name}");
_handlers.Add(messageType, data => handler((T)data));
}
}

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

@ -0,0 +1,15 @@
using System;
namespace QSB.API;
// TODO: document
public 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> handler);
}

View File

@ -0,0 +1,33 @@
using System.IO;
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, Obj2Bytes(data))) { }
private static byte[] Obj2Bytes(object obj)
{
using var ms = new MemoryStream();
var bf = new BinaryFormatter();
bf.Serialize(ms, obj);
var bytes = ms.ToArray();
return bytes;
}
private static object Bytes2Obj(byte[] bytes)
{
using var ms = new MemoryStream(bytes);
var bf = new BinaryFormatter();
var obj = bf.Deserialize(ms);
return obj;
}
public override void OnReceiveRemote()
{
var obj = Bytes2Obj(Data.data);
AddonDataManager.OnReceiveDataMessage(Data.messageType, obj);
}
}

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

@ -0,0 +1,24 @@
using System;
using QSB.API.Messages;
using QSB.Messaging;
using QSB.Player;
namespace QSB.API;
public 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> handler)
{
AddonDataManager.RegisterHandler(messageType, handler);
}
}

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

@ -69,7 +69,7 @@
</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" />
<Reference Include="..\Mirror\*.dll" />
<Reference Include="..\UniTask\*.dll" />
<ProjectReference Include="..\EpicOnlineTransport\EpicOnlineTransport.csproj" />

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();

View File

@ -8,7 +8,7 @@ using UnityEngine;
namespace QSB;
/// <summary>
/// TEMPORARY: this is for trying to solve this stupid fucking bug (gorp)
/// "TEMPORARY": this is for trying to solve this stupid fucking bug (gorp)
/// </summary>
[HarmonyPatch(typeof(OWRigidbody))]
public class TeleportingPlanetsPatch : QSBPatch
@ -21,7 +21,7 @@ public class TeleportingPlanetsPatch : QSBPatch
{
if (__instance.TryGetComponent<AstroObject>(out var astroObject) && astroObject._name != AstroObject.Name.ProbeCannon)
{
DebugLog.ToAll($"AHHHHHHHHH!!!!!!!!!\n{__instance.name}\n{Environment.StackTrace}", MessageType.Error);
DebugLog.ToConsole($"AHHHHHHHHH!!!!!!!!!\nPlanet {__instance.name} teleported! Please screenshot this and contact devs!\n{Environment.StackTrace}", MessageType.Fatal);
}
}
}

View File

@ -21,15 +21,14 @@ public static class DebugLog
message = $"[{ProcessInstanceId}] " + message;
}
if (QSBCore.Helper == null)
// copied from https://github.com/ow-mods/owml/blob/master/src/OWML.Logging/ModSocketOutput.cs#L33
{
// yes i know this is only meant for OWML, but it's useful as a backup
ModConsole.OwmlConsole.WriteLine(message, type, GetCallingType());
}
else
{
var socket = QSBCore.Helper.Console.GetValue<IModSocket>("_socket");
socket.WriteToSocket(new ModSocketMessage
var Logger = ModConsole.OwmlConsole.GetValue<IModLogger>("Logger");
var _socket = ModConsole.OwmlConsole.GetValue<IModSocket>("_socket");
Logger?.Log($"{type}: {message}");
_socket.WriteToSocket(new ModSocketMessage
{
SenderName = "QSB",
SenderType = GetCallingType(),
@ -39,29 +38,12 @@ public static class DebugLog
if (type == MessageType.Fatal)
{
socket.Close();
_socket.Close();
Process.GetCurrentProcess().Kill();
}
}
}
public static void ToHud(string message)
{
if (Locator.GetPlayerBody() == null)
{
return;
}
var data = new NotificationData(NotificationTarget.Player, message.ToUpper());
NotificationManager.SharedInstance.PostNotification(data);
}
public static void ToAll(string message, MessageType type = MessageType.Message)
{
ToConsole(message, type);
ToHud(message);
}
public static void DebugWrite(string message, MessageType type = MessageType.Message)
{
if (QSBCore.DebugSettings.DebugMode)
@ -74,6 +56,7 @@ public static class DebugLog
new StackTrace(2) // skip this function and calling function
.GetFrames()!
.Select(x => x.GetMethod().DeclaringType!)
// BUG: this part doesnt work for some reason
.First(x => x != typeof(DebugLog) && !x.IsDefined(typeof(CompilerGeneratedAttribute), true))
.Name;
}

View File

@ -12,6 +12,8 @@ public abstract class BaseVariableSyncer<T> : QSBNetworkBehaviour
[NonSerialized]
public T Value;
// DO NOT REMOVE THIS METHOD
public bool Bruh() => HasChanged();
protected override bool HasChanged() => !EqualityComparer<T>.Default.Equals(PrevValue, Value);
protected override void UpdatePrevData() => PrevValue = Value;
protected override void Serialize(NetworkWriter writer) => writer.Write(Value);