From 94254b8c28dfb0d90c527f5b68bc6bc1fd850e64 Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Thu, 17 Sep 2026 18:14:51 +0800 Subject: [PATCH] feat(livestreamassist): add read-only WebSocket API Add a Fleck/Newtonsoft.Json listener for local and LAN clients, with system methods, main-thread reflection reads, and ZipMod packaging of the two runtime DLLs. Ctrl+F8 statistics assist is unchanged. In-game and LAN acceptance remain pending. --- AGENTS.md | 2 +- LiveStreamAssist/Api/ApiProtocol.cs | 863 ++++++++++++++++++ LiveStreamAssist/Api/MainThreadDispatcher.cs | 162 ++++ LiveStreamAssist/Api/ReflectionReader.cs | 836 +++++++++++++++++ LiveStreamAssist/Api/WebSocketApiFeature.cs | 332 +++++++ LiveStreamAssist/Api/WebSocketApiServer.cs | 544 +++++++++++ LiveStreamAssist/LiveStreamAssist.cs | 2 + LiveStreamAssist/LiveStreamAssist.csproj | 10 + .../Properties/InternalsVisibleTo.cs | 3 + LiveStreamAssist/README.md | 15 +- LiveStreamAssist/docs/WebSocketApi.md | 2 +- LiveStreamAssist/docs/WebSocketDesign.md | 73 +- .../Copy-GameFacades.ps1 | 44 + .../tools/ReflectionReaderCheck/Program.cs | 210 +++++ .../ReflectionReaderCheck.csproj | 16 + LiveStreamAssist/tools/Test-WebSocketApi.ps1 | 223 +++++ 16 files changed, 3300 insertions(+), 37 deletions(-) create mode 100644 LiveStreamAssist/Api/ApiProtocol.cs create mode 100644 LiveStreamAssist/Api/MainThreadDispatcher.cs create mode 100644 LiveStreamAssist/Api/ReflectionReader.cs create mode 100644 LiveStreamAssist/Api/WebSocketApiFeature.cs create mode 100644 LiveStreamAssist/Api/WebSocketApiServer.cs create mode 100644 LiveStreamAssist/Properties/InternalsVisibleTo.cs create mode 100644 LiveStreamAssist/tools/ReflectionReaderCheck/Copy-GameFacades.ps1 create mode 100644 LiveStreamAssist/tools/ReflectionReaderCheck/Program.cs create mode 100644 LiveStreamAssist/tools/ReflectionReaderCheck/ReflectionReaderCheck.csproj create mode 100644 LiveStreamAssist/tools/Test-WebSocketApi.ps1 diff --git a/AGENTS.md b/AGENTS.md index d3b25d0..46db71e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -12,7 +12,7 @@ - This repository contains independent BepInEx 5.x C# mods for Dyson Sphere Program. HarmonyLib patches game methods at runtime; `UXAssist` is the shared library used directly by `CheatEnabler` and `UniverseGenTweaks`. - Projects target `net472` or `netstandard2.1` with SDK-style `.csproj` files. Compile-time game references live in `AssemblyFromGame/`. - `DustbinPreloader` and `LabOptPreloader` are Mono.Cecil preloaders; their main mods consume the injected fields without reflection. -- LiveStreamAssist: [project design and implementation plan](LiveStreamAssist/docs/WebSocketDesign.md). +- LiveStreamAssist: [project design and implementation plan](LiveStreamAssist/docs/WebSocketDesign.md). The WebSocket API packages `Fleck.dll` and `Newtonsoft.Json.dll` through a project-local `ZipMod` `BeforeTargets` item; do not rely on other mods for those DLLs. ## Build diff --git a/LiveStreamAssist/Api/ApiProtocol.cs b/LiveStreamAssist/Api/ApiProtocol.cs new file mode 100644 index 0000000..1a9ef37 --- /dev/null +++ b/LiveStreamAssist/Api/ApiProtocol.cs @@ -0,0 +1,863 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Text; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace LiveStreamAssist.Api; + +internal static class ApiLimits +{ + public const string ApiVersion = "1.0.0"; + public const string Path = "/api/v1"; + public const int MaxConnections = 8; + public const int MaxPendingRequestsPerConnection = 8; + public const int MaxPendingRequests = 64; + public const int MaxRequestBytes = 65536; + public const int MaxResponseBytes = 262144; + public const int MaxJsonDepth = 32; + public const int MaxIdLength = 64; + public const int MaxPathSegments = 16; + public const int MaxSelectMembers = 32; + public const int DefaultPageSize = 64; + public const int MaxPageSize = 128; + public const int RequestTimeoutMs = 5000; + public const int MaxRequestsPerFrame = 8; + public const int MainThreadBudgetMs = 2; + public const int MaxIncomingSocketBytes = MaxRequestBytes + 8192; + public const int CloseGoingAway = 1001; + public const int CloseUnsupportedData = 1003; + public const int CloseInvalidPayload = 1007; + public const int ClosePolicyViolation = 1008; + public const int CloseMessageTooBig = 1009; + public const int CloseTryAgainLater = 1013; +} + +internal sealed class ApiError +{ + public int Code; + public string Kind; + public string Message; + public int? SegmentIndex; + public string Member; + public string Root; + + public ApiError WithPath(int? segmentIndex = null, string member = null, string root = null) + { + SegmentIndex = segmentIndex; + Member = member; + Root = root; + return this; + } +} + +internal sealed class ApiRequest +{ + public string Id; + public string Method; + public JObject Params; +} + +internal enum DataMethod +{ + Roots, + Describe, + Read +} + +internal struct ReadOptions +{ + public bool HasSelect; + public string[] Select; + public bool HasOffset; + public int Offset; + public bool HasLimit; + public int Limit; + + public int EffectiveOffset => HasOffset ? Offset : 0; + public int EffectiveLimit => HasLimit ? Limit : ApiLimits.DefaultPageSize; +} + +internal sealed class DataCall +{ + public DataMethod Method; + public string Root; + public PathSeg[] Path; + public ReadOptions Options; +} + +internal enum PathKind +{ + Member, + Index, + Key +} + +internal readonly struct PathSeg +{ + public readonly PathKind Kind; + public readonly string Name; + public readonly int Index; + public readonly object Key; + + public PathSeg(string name) + { + Kind = PathKind.Member; + Name = name; + Index = 0; + Key = null; + } + + public PathSeg(int index) + { + Kind = PathKind.Index; + Name = null; + Index = index; + Key = null; + } + + public PathSeg(object key) + { + Kind = PathKind.Key; + Name = null; + Index = 0; + Key = key; + } +} + +internal static class ApiErrors +{ + public static ApiError ParseError() => + New(-32700, "PARSE_ERROR", "Malformed JSON."); + + public static ApiError InvalidRequest(string message = "Invalid request.") => + New(-32600, "INVALID_REQUEST", message); + + public static ApiError MethodNotFound() => + New(-32601, "METHOD_NOT_FOUND", "Unknown method."); + + public static ApiError InvalidParams(string message = "Invalid parameters.") => + New(-32602, "INVALID_PARAMS", message); + + public static ApiError InternalError() => + New(-32603, "INTERNAL_ERROR", "Unexpected server failure."); + + public static ApiError GameNotReady() => + New(-32001, "GAME_NOT_READY", "Game data is unavailable."); + + public static ApiError RootNotFound(string root) => + New(-32002, "ROOT_NOT_FOUND", "Unknown root.").WithPath(root: root); + + public static ApiError RootUnavailable(string root) => + New(-32003, "ROOT_UNAVAILABLE", "Root is unavailable.").WithPath(root: root); + + public static ApiError MemberNotFound(string member, int? segmentIndex = null) => + New(-32004, "MEMBER_NOT_FOUND", "Requested member does not exist.").WithPath(segmentIndex, member); + + public static ApiError MemberNotAllowed(string member, int? segmentIndex = null) => + New(-32005, "MEMBER_NOT_ALLOWED", "Member is outside the read policy.").WithPath(segmentIndex, member); + + public static ApiError NullPath(int segmentIndex) => + New(-32006, "NULL_PATH", "Null encountered before completing the path.").WithPath(segmentIndex); + + public static ApiError IndexOutOfRange(int segmentIndex) => + New(-32007, "INDEX_OUT_OF_RANGE", "Element index is outside the current array or list.").WithPath(segmentIndex); + + public static ApiError KeyNotFound(int segmentIndex) => + New(-32008, "KEY_NOT_FOUND", "Dictionary has no matching key.").WithPath(segmentIndex); + + public static ApiError UnsupportedType(int? segmentIndex = null, string member = null) => + New(-32009, "UNSUPPORTED_TYPE", "Value type or traversal is unsupported.").WithPath(segmentIndex, member); + + public static ApiError ReadFailed(int? segmentIndex = null, string member = null) => + New(-32010, "READ_FAILED", "A permitted read failed.").WithPath(segmentIndex, member); + + public static ApiError LimitExceeded(string message = "A documented resource limit would be exceeded.") => + New(-32011, "LIMIT_EXCEEDED", message); + + public static ApiError ServerBusy() => + New(-32012, "SERVER_BUSY", "Admission capacity is exhausted."); + + public static ApiError RequestTimeout() => + New(-32013, "REQUEST_TIMEOUT", "Response production exceeded its deadline."); + + public static ApiError SessionChanged() => + New(-32014, "SESSION_CHANGED", "Session changed after admission."); + + public static ApiError UnsupportedValue() => + New(-32015, "UNSUPPORTED_VALUE", "Value cannot be represented under the scalar rules."); + + static ApiError New(int code, string kind, string message) => + new ApiError { Code = code, Kind = kind, Message = message }; +} + +internal static class ApiProtocol +{ + public const string JsonRpc = "2.0"; + public const string MethodPing = "system.ping"; + public const string MethodInfo = "system.info"; + public const string MethodValidate = "system.validate"; + public const string MethodStats = "system.stats"; + public const string MethodRoots = "data.roots"; + public const string MethodDescribe = "data.describe"; + public const string MethodRead = "data.read"; + + static readonly HashSet SystemMethods = new HashSet(StringComparer.Ordinal) + { + MethodPing, MethodInfo, MethodValidate, MethodStats + }; + + static readonly HashSet DataMethods = new HashSet(StringComparer.Ordinal) + { + MethodRoots, MethodDescribe, MethodRead + }; + + static readonly JsonSerializer Serializer = JsonSerializer.Create(new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.None, + DateParseHandling = DateParseHandling.None, + NullValueHandling = NullValueHandling.Include, + Formatting = Formatting.None, + MaxDepth = ApiLimits.MaxJsonDepth + }); + + public static bool IsSystemMethod(string method) => SystemMethods.Contains(method); + public static bool IsDataMethod(string method) => DataMethods.Contains(method); + + public static bool TryParseMessage(string text, out ApiRequest request, out ApiError error, out object id) + { + request = null; + error = null; + id = null; + JToken token; + try + { + using (var sr = new StringReader(text)) + using (var reader = new JsonTextReader(sr)) + { + reader.DateParseHandling = DateParseHandling.None; + reader.MaxDepth = ApiLimits.MaxJsonDepth; + var load = new JsonLoadSettings + { + DuplicatePropertyNameHandling = DuplicatePropertyNameHandling.Error, + LineInfoHandling = LineInfoHandling.Ignore + }; + token = JToken.Load(reader, load); + if (reader.Read() && reader.TokenType != JsonToken.None) + { + error = ApiErrors.ParseError(); + return false; + } + } + } + catch (Exception) + { + error = ApiErrors.ParseError(); + return false; + } + + if (token is JArray) + { + error = ApiErrors.InvalidRequest("Batch requests are not supported."); + return false; + } + + if (!(token is JObject obj)) + { + error = ApiErrors.InvalidRequest(); + return false; + } + + foreach (var prop in obj.Properties()) + { + if (prop.Name != "jsonrpc" && prop.Name != "id" && prop.Name != "method" && prop.Name != "params") + { + error = ApiErrors.InvalidRequest("Unknown envelope field."); + return false; + } + } + + var jsonrpc = obj["jsonrpc"]; + if (jsonrpc == null || jsonrpc.Type != JTokenType.String || !string.Equals(jsonrpc.Value(), JsonRpc, StringComparison.Ordinal)) + { + error = ApiErrors.InvalidRequest("jsonrpc must be \"2.0\"."); + return false; + } + + if (!obj.TryGetValue("id", out var idToken) || idToken == null || idToken.Type == JTokenType.Null) + { + error = ApiErrors.InvalidRequest("id is required."); + return false; + } + + if (idToken.Type != JTokenType.String) + { + error = ApiErrors.InvalidRequest("id must be a string."); + return false; + } + + var idValue = idToken.Value(); + if (string.IsNullOrEmpty(idValue)) + { + error = ApiErrors.InvalidRequest("id must be nonempty."); + return false; + } + + if (idValue.Length > ApiLimits.MaxIdLength) + { + error = ApiErrors.InvalidRequest("id exceeds maxIdLength."); + return false; + } + + id = idValue; + + if (!obj.TryGetValue("method", out var methodToken) || methodToken == null || methodToken.Type != JTokenType.String) + { + error = ApiErrors.InvalidRequest("method is required."); + return false; + } + + var method = methodToken.Value(); + if (string.IsNullOrEmpty(method)) + { + error = ApiErrors.InvalidRequest("method is required."); + return false; + } + + JObject parms; + if (!obj.TryGetValue("params", out var paramsToken) || paramsToken == null || paramsToken.Type == JTokenType.Null) + parms = new JObject(); + else if (paramsToken is JObject p) + parms = p; + else + { + error = ApiErrors.InvalidParams("params must be an object."); + return false; + } + + request = new ApiRequest { Id = idValue, Method = method, Params = parms }; + return true; + } + + public static bool TryParseDataCall(ApiRequest request, out DataCall call, out ApiError error) + { + call = null; + error = null; + switch (request.Method) + { + case MethodRoots: + error = RejectUnknownParams(request.Params); + if (error != null) return false; + call = new DataCall { Method = DataMethod.Roots, Path = Array.Empty() }; + return true; + case MethodDescribe: + return TryParseDescribe(request.Params, out call, out error); + case MethodRead: + return TryParseRead(request.Params, out call, out error); + default: + error = ApiErrors.MethodNotFound(); + return false; + } + } + + public static object HandleSystem(string method, JObject parms, GameVersionSnapshot version, string modVersion) + { + switch (method) + { + case MethodPing: + { + var err = RejectUnknownParams(parms); + if (err != null) return err; + return new Dictionary + { + ["serverTimeUtc"] = DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture) + }; + } + case MethodInfo: + { + var err = RejectUnknownParams(parms); + if (err != null) return err; + return BuildInfo(version, modVersion); + } + case MethodValidate: + return HandleValidate(parms); + case MethodStats: + { + var err = RejectUnknownParams(parms); + if (err != null) return err; + return new Dictionary + { + ["available"] = false, + ["metrics"] = null + }; + } + default: + return ApiErrors.MethodNotFound(); + } + } + + public static Dictionary BuildInfo(GameVersionSnapshot version, string modVersion) + { + var ready = version != null && version.Ready; + return new Dictionary + { + ["apiVersion"] = ApiLimits.ApiVersion, + ["modVersion"] = modVersion ?? "", + ["gameVersionReady"] = ready, + ["gameVersion"] = ready ? version.GameVersion : null, + ["gameBuild"] = ready ? (object)version.GameBuild : null, + ["capabilities"] = new Dictionary + { + ["reflection.read"] = true, + ["reflection.describe"] = true, + ["api.stats"] = false, + ["subscriptions"] = false + }, + ["limits"] = new Dictionary + { + ["maxConnections"] = ApiLimits.MaxConnections, + ["maxPendingRequestsPerConnection"] = ApiLimits.MaxPendingRequestsPerConnection, + ["maxPendingRequests"] = ApiLimits.MaxPendingRequests, + ["maxRequestBytes"] = ApiLimits.MaxRequestBytes, + ["maxResponseBytes"] = ApiLimits.MaxResponseBytes, + ["maxJsonDepth"] = ApiLimits.MaxJsonDepth, + ["maxIdLength"] = ApiLimits.MaxIdLength, + ["maxPathSegments"] = ApiLimits.MaxPathSegments, + ["maxSelectMembers"] = ApiLimits.MaxSelectMembers, + ["defaultPageSize"] = ApiLimits.DefaultPageSize, + ["maxPageSize"] = ApiLimits.MaxPageSize, + ["requestTimeoutMs"] = ApiLimits.RequestTimeoutMs, + ["maxRequestsPerFrame"] = ApiLimits.MaxRequestsPerFrame, + ["mainThreadBudgetMs"] = ApiLimits.MainThreadBudgetMs + } + }; + } + + public static bool CapabilityEnabled(string name) + { + switch (name) + { + case "reflection.read": + case "reflection.describe": + return true; + default: + return false; + } + } + + public static byte[] SerializeEnvelope(object id, object result, ApiError error) + { + var envelope = new Dictionary + { + ["jsonrpc"] = JsonRpc, + ["id"] = id + }; + if (error != null) + envelope["error"] = ErrorBody(error); + else + envelope["result"] = result; + return SerializeBounded(envelope); + } + + public static Dictionary ErrorBody(ApiError error) + { + var data = new Dictionary { ["kind"] = error.Kind }; + if (error.SegmentIndex.HasValue) data["segmentIndex"] = error.SegmentIndex.Value; + if (error.Member != null) data["member"] = error.Member; + if (error.Root != null) data["root"] = error.Root; + return new Dictionary + { + ["code"] = error.Code, + ["message"] = error.Message, + ["data"] = data + }; + } + + public static byte[] SerializeBounded(object value) + { + using (var bounded = new BoundedStream(ApiLimits.MaxResponseBytes)) + using (var writer = new StreamWriter(bounded, new UTF8Encoding(false), 1024, true)) + using (var json = new JsonTextWriter(writer)) + { + json.Formatting = Formatting.None; + Serializer.Serialize(json, value); + json.Flush(); + writer.Flush(); + return bounded.ToArray(); + } + } + + public static ApiError RejectUnknownParams(JObject parms, params string[] allowed) + { + if (parms == null) return null; + foreach (var prop in parms.Properties()) + { + var known = false; + for (var i = 0; i < allowed.Length; i++) + { + if (string.Equals(prop.Name, allowed[i], StringComparison.Ordinal)) + { + known = true; + break; + } + } + + if (!known) + return ApiErrors.InvalidParams("Unknown parameter '" + prop.Name + "'."); + } + + return null; + } + + static object HandleValidate(JObject parms) + { + var err = RejectUnknownParams(parms, "apiMajor", "requiredCapabilities"); + if (err != null) return err; + if (!parms.TryGetValue("apiMajor", out var majorToken) || majorToken == null || majorToken.Type == JTokenType.Null) + return ApiErrors.InvalidParams("apiMajor is required."); + if (!TryGetInt32(majorToken, out var apiMajor) || apiMajor < 1) + return ApiErrors.InvalidParams("apiMajor must be a positive 32-bit integer."); + + var missing = new List(); + if (parms.TryGetValue("requiredCapabilities", out var capToken) && capToken != null && capToken.Type != JTokenType.Null) + { + if (!(capToken is JArray arr)) + return ApiErrors.InvalidParams("requiredCapabilities must be an array."); + if (arr.Count > 32) + return ApiErrors.LimitExceeded("requiredCapabilities exceeds 32 entries."); + var seen = new HashSet(StringComparer.Ordinal); + foreach (var item in arr) + { + if (item == null || item.Type != JTokenType.String) + return ApiErrors.InvalidParams("Capability names must be nonempty strings."); + var name = item.Value(); + if (string.IsNullOrEmpty(name)) + return ApiErrors.InvalidParams("Capability names must be nonempty strings."); + if (!seen.Add(name)) + return ApiErrors.InvalidParams("requiredCapabilities must be distinct."); + if (!CapabilityEnabled(name)) + missing.Add(name); + } + } + + var majorOk = apiMajor == 1; + return new Dictionary + { + ["compatible"] = majorOk && missing.Count == 0, + ["apiVersion"] = ApiLimits.ApiVersion, + ["apiMajorCompatible"] = majorOk, + ["missingCapabilities"] = missing + }; + } + + static bool TryParseDescribe(JObject parms, out DataCall call, out ApiError error) + { + call = null; + error = RejectUnknownParams(parms, "root", "path"); + if (error != null) return false; + if (!TryParseRoot(parms, out var root, out error)) return false; + if (!TryParsePath(parms, out var path, out error)) return false; + call = new DataCall { Method = DataMethod.Describe, Root = root, Path = path }; + return true; + } + + static bool TryParseRead(JObject parms, out DataCall call, out ApiError error) + { + call = null; + error = RejectUnknownParams(parms, "root", "path", "select", "offset", "limit"); + if (error != null) return false; + if (!TryParseRoot(parms, out var root, out error)) return false; + if (!TryParsePath(parms, out var path, out error)) return false; + var options = new ReadOptions(); + if (parms.TryGetValue("select", out var selectToken) && selectToken != null && selectToken.Type != JTokenType.Null) + { + if (!(selectToken is JArray arr)) + { + error = ApiErrors.InvalidParams("select must be an array of member names."); + return false; + } + + if (arr.Count < 1) + { + error = ApiErrors.InvalidParams("select must contain at least one member."); + return false; + } + + if (arr.Count > ApiLimits.MaxSelectMembers) + { + error = ApiErrors.LimitExceeded("select exceeds maxSelectMembers."); + return false; + } + + var names = new string[arr.Count]; + var seen = new HashSet(StringComparer.Ordinal); + for (var i = 0; i < arr.Count; i++) + { + var item = arr[i]; + if (item == null || item.Type != JTokenType.String) + { + error = ApiErrors.InvalidParams("select members must be nonempty strings."); + return false; + } + + var name = item.Value(); + if (string.IsNullOrEmpty(name)) + { + error = ApiErrors.InvalidParams("select members must be nonempty strings."); + return false; + } + + if (!seen.Add(name)) + { + error = ApiErrors.InvalidParams("select members must be distinct."); + return false; + } + + names[i] = name; + } + + options.HasSelect = true; + options.Select = names; + } + + if (parms.TryGetValue("offset", out var offsetToken) && offsetToken != null && offsetToken.Type != JTokenType.Null) + { + if (!TryGetInt32(offsetToken, out var offset) || offset < 0) + { + error = ApiErrors.InvalidParams("offset must be a nonnegative 32-bit integer."); + return false; + } + + options.HasOffset = true; + options.Offset = offset; + } + + if (parms.TryGetValue("limit", out var limitToken) && limitToken != null && limitToken.Type != JTokenType.Null) + { + if (!TryGetInt32(limitToken, out var limit)) + { + error = ApiErrors.InvalidParams("limit must be an integer."); + return false; + } + + if (limit < 1) + { + error = ApiErrors.InvalidParams("limit must be at least 1."); + return false; + } + + if (limit > ApiLimits.MaxPageSize) + { + error = ApiErrors.LimitExceeded("limit exceeds maxPageSize."); + return false; + } + + options.HasLimit = true; + options.Limit = limit; + } + + call = new DataCall { Method = DataMethod.Read, Root = root, Path = path, Options = options }; + return true; + } + + static bool TryParseRoot(JObject parms, out string root, out ApiError error) + { + root = null; + error = null; + if (!parms.TryGetValue("root", out var token) || token == null || token.Type == JTokenType.Null) + { + error = ApiErrors.InvalidParams("root is required."); + return false; + } + + if (token.Type != JTokenType.String) + { + error = ApiErrors.InvalidParams("root must be a string."); + return false; + } + + root = token.Value(); + if (string.IsNullOrEmpty(root)) + { + error = ApiErrors.InvalidParams("root must be a nonempty string."); + return false; + } + + return true; + } + + static bool TryParsePath(JObject parms, out PathSeg[] path, out ApiError error) + { + path = Array.Empty(); + error = null; + if (!parms.TryGetValue("path", out var token) || token == null || token.Type == JTokenType.Null) + return true; + if (!(token is JArray arr)) + { + error = ApiErrors.InvalidParams("path must be an array."); + return false; + } + + if (arr.Count > ApiLimits.MaxPathSegments) + { + error = ApiErrors.LimitExceeded("path exceeds maxPathSegments."); + return false; + } + + var segs = new PathSeg[arr.Count]; + for (var i = 0; i < arr.Count; i++) + { + if (!TryParseSegment(arr[i], out segs[i], out error)) + return false; + } + + path = segs; + return true; + } + + static bool TryParseSegment(JToken token, out PathSeg seg, out ApiError error) + { + seg = default; + error = null; + if (token == null || token.Type == JTokenType.Null) + { + error = ApiErrors.InvalidParams("path segments cannot be null."); + return false; + } + + if (token.Type == JTokenType.String) + { + var name = token.Value(); + if (string.IsNullOrEmpty(name)) + { + error = ApiErrors.InvalidParams("member names must be nonempty."); + return false; + } + + seg = new PathSeg(name); + return true; + } + + if (token.Type == JTokenType.Integer) + { + if (!TryGetInt32(token, out var index) || index < 0) + { + error = ApiErrors.InvalidParams("indexes must be nonnegative 32-bit integers."); + return false; + } + + seg = new PathSeg(index); + return true; + } + + if (token is JObject obj) + { + JProperty keyProp = null; + foreach (var prop in obj.Properties()) + { + if (!string.Equals(prop.Name, "key", StringComparison.Ordinal)) + { + error = ApiErrors.InvalidParams("key segments may only contain 'key'."); + return false; + } + + keyProp = prop; + } + + if (keyProp == null || keyProp.Value == null || keyProp.Value.Type == JTokenType.Null) + { + error = ApiErrors.InvalidParams("key is required."); + return false; + } + + if (keyProp.Value.Type == JTokenType.String) + { + seg = new PathSeg((object)keyProp.Value.Value()); + return true; + } + + if (keyProp.Value.Type == JTokenType.Integer && TryGetInt32(keyProp.Value, out var key)) + { + seg = new PathSeg((object)key); + return true; + } + + error = ApiErrors.InvalidParams("dictionary keys must be strings or 32-bit integers."); + return false; + } + + error = ApiErrors.InvalidParams("unsupported path segment."); + return false; + } + + public static bool TryGetInt32(JToken token, out int value) + { + value = 0; + if (token == null || token.Type != JTokenType.Integer) return false; + try + { + var n = token.Value(); + if (n < int.MinValue || n > int.MaxValue) return false; + value = (int)n; + return true; + } + catch + { + return false; + } + } + + public static int ApiMajor => 1; +} + +internal sealed class BoundedStream : MemoryStream +{ + readonly int _max; + + public BoundedStream(int max) => _max = max; + + public override void Write(byte[] buffer, int offset, int count) + { + if (Length + count > _max) + throw new ResponseTooLargeException(); + base.Write(buffer, offset, count); + } + + public override void WriteByte(byte value) + { + if (Length + 1 > _max) + throw new ResponseTooLargeException(); + base.WriteByte(value); + } +} + +internal sealed class ResponseTooLargeException : Exception; +internal sealed class GameVersionSnapshot +{ + public readonly bool Ready; + public readonly string GameVersion; + public readonly int GameBuild; + + public GameVersionSnapshot(bool ready, string gameVersion, int gameBuild) + { + Ready = ready; + GameVersion = gameVersion; + GameBuild = gameBuild; + } + + public static readonly GameVersionSnapshot Empty = new GameVersionSnapshot(false, null, 0); +} + +internal sealed class SessionSnapshot +{ + public readonly int Generation; + public readonly string SessionId; + + public SessionSnapshot(int generation, string sessionId) + { + Generation = generation; + SessionId = sessionId; + } +} diff --git a/LiveStreamAssist/Api/MainThreadDispatcher.cs b/LiveStreamAssist/Api/MainThreadDispatcher.cs new file mode 100644 index 0000000..6b7d009 --- /dev/null +++ b/LiveStreamAssist/Api/MainThreadDispatcher.cs @@ -0,0 +1,162 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading; +using UnityEngine; + +namespace LiveStreamAssist.Api; + +internal sealed class GameWork +{ + public PendingRequest Pending; + public DataCall Call; + public int Generation; + public long DeadlineMs; +} + +internal delegate object GameWorkExecutor(GameWork work); + +internal sealed class MainThreadDispatcher +{ + readonly SynchronizationContext _context; + readonly object _token; + readonly object _gate = new object(); + readonly Queue _queue = new Queue(); + readonly GameWorkExecutor _execute; + readonly Func _nowMs; + bool _pumpScheduled; + bool _stopped; + int _frame = -1; + int _startedThisFrame; + readonly Stopwatch _budget = new Stopwatch(); + + public MainThreadDispatcher(SynchronizationContext context, object token, GameWorkExecutor execute, Func nowMs) + { + _context = context; + _token = token; + _execute = execute; + _nowMs = nowMs; + } + + public bool TryEnqueue(GameWork work) + { + lock (_gate) + { + if (_stopped) return false; + _queue.Enqueue(work); + ScheduleLocked(); + return true; + } + } + + public void CancelQueued(Func reason) + { + List taken; + lock (_gate) + { + if (_queue.Count == 0) return; + taken = new List(_queue.Count); + while (_queue.Count > 0) + taken.Add(_queue.Dequeue()); + } + + foreach (var work in taken) + work.Pending.Complete(reason(work)); + } + + public void Stop() + { + lock (_gate) + { + _stopped = true; + } + + CancelQueued(_ => ApiErrors.RequestTimeout()); + } + + void ScheduleLocked() + { + if (_pumpScheduled || _stopped) return; + _pumpScheduled = true; + _context.Post(Pump, _token); + } + + void Pump(object state) + { + if (!ReferenceEquals(state, _token)) return; + Drain(); + lock (_gate) + { + if (_stopped || _queue.Count == 0) + { + _pumpScheduled = false; + return; + } + } + + _context.Post(Pump, _token); + } + + void Drain() + { + var frame = Time.frameCount; + if (frame != _frame) + { + _frame = frame; + _startedThisFrame = 0; + _budget.Restart(); + } + + while (_startedThisFrame < ApiLimits.MaxRequestsPerFrame) + { + if (_startedThisFrame > 0 && _budget.ElapsedMilliseconds >= ApiLimits.MainThreadBudgetMs) + break; + GameWork work; + lock (_gate) + { + if (_stopped || _queue.Count == 0) return; + work = _queue.Dequeue(); + } + + _startedThisFrame++; + Run(work); + } + } + + void Run(GameWork work) + { + if (!work.Pending.IsActive) + return; + var now = _nowMs(); + if (now > work.DeadlineMs) + { + work.Pending.Complete(ApiErrors.RequestTimeout()); + return; + } + + object result; + try + { + result = _execute(work); + } + catch (Exception ex) + { + LiveStreamAssist.Logger.LogError($"API game-data request failed: {ex}"); + work.Pending.Complete(ApiErrors.InternalError()); + return; + } + + if (!work.Pending.IsActive) + return; + if (_nowMs() > work.DeadlineMs) + { + work.Pending.Complete(ApiErrors.RequestTimeout()); + return; + } + + if (result is ApiError err) + work.Pending.Complete(err); + else + work.Pending.CompleteResult(result); + } +} diff --git a/LiveStreamAssist/Api/ReflectionReader.cs b/LiveStreamAssist/Api/ReflectionReader.cs new file mode 100644 index 0000000..e7a3c04 --- /dev/null +++ b/LiveStreamAssist/Api/ReflectionReader.cs @@ -0,0 +1,836 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using UnityEngine; + +namespace LiveStreamAssist.Api; + +internal sealed class MemberInfoDto +{ + public string Name; + public string Type; + public string Kind; +} + +internal sealed class CollectionInfoDto +{ + public string Kind; + public string ElementType; + public string KeyType; + public int? Count; +} + +internal sealed class WalkResult +{ + public object Encoded; + public string TypeName; + public bool IsNull; + public MemberInfoDto[] Members; + public CollectionInfoDto Collection; + public ApiError Error; +} + +internal sealed class ReflectionReader +{ + const int MaxCache = 1024; + static readonly Assembly GameAssembly = typeof(GameData).Assembly; + static readonly UTF8Encoding Utf8 = new UTF8Encoding(false, false); + internal static Action Warn; + + readonly Dictionary _cache = new Dictionary(); + readonly HashSet _extraAssemblies; + readonly object _cacheGate = new object(); + + public ReflectionReader(params Assembly[] extraAllowed) + { + if (extraAllowed != null && extraAllowed.Length > 0) + _extraAssemblies = new HashSet(extraAllowed); + } + + public void ClearCache() + { + lock (_cacheGate) _cache.Clear(); + } + + public WalkResult Describe(object root, Type declaredType, PathSeg[] path) + { + var walked = Walk(root, declaredType, path); + if (walked.Error != null) return Fail(walked.Error); + return DescribeTerminal(walked.Value, walked.DeclaredType, walked.RuntimeType); + } + + public WalkResult Read(object root, Type declaredType, PathSeg[] path, ReadOptions options) + { + var walked = Walk(root, declaredType, path); + if (walked.Error != null) return Fail(walked.Error); + return ReadTerminal(walked.Value, walked.DeclaredType, walked.RuntimeType, options); + } + + public WalkResult DescribeTerminal(object value, Type declaredType, Type runtimeType) + { + var typeName = TypeName(value == null ? declaredType : runtimeType ?? value.GetType()); + var result = new WalkResult + { + TypeName = typeName, + IsNull = value == null, + Members = Array.Empty(), + Encoded = null + }; + var effective = value == null ? declaredType : runtimeType ?? value.GetType(); + if (TryCollectionKind(effective, out var kind, out var elementType, out var keyType)) + { + int? count = null; + if (value != null) + count = GetCount(value, effective); + result.Collection = new CollectionInfoDto + { + Kind = kind, + ElementType = TypeName(elementType), + KeyType = keyType == null ? null : TypeName(keyType), + Count = count + }; + return result; + } + + if (value != null && IsScalar(Unwrap(effective))) + return result; + + if (effective != null && IsAllowedType(effective) && !IsScalar(Unwrap(effective))) + result.Members = GetExposedMembers(effective); + return result; + } + + public WalkResult ReadTerminal(object value, Type declaredType, Type runtimeType, ReadOptions options) + { + var typeName = TypeName(value == null ? declaredType : runtimeType ?? value.GetType()); + if (value == null) + { + if (options.HasOffset || options.HasLimit) + { + var declared = declaredType; + if (!TryCollectionKind(declared, out var nullKind, out _, out _) || + (!string.Equals(nullKind, "array", StringComparison.Ordinal) && + !string.Equals(nullKind, "list", StringComparison.Ordinal))) + return Fail(ApiErrors.InvalidParams("offset and limit are valid only for array or list terminals.")); + return Fail(ApiErrors.InvalidParams("offset and limit require a non-null array or list.")); + } + + return new WalkResult { TypeName = typeName, IsNull = true, Encoded = null }; + } + + var actual = runtimeType ?? value.GetType(); + if (TryCollectionKind(actual, out var kind, out var elementType, out _)) + { + if (string.Equals(kind, "dictionary", StringComparison.Ordinal)) + { + if (options.HasSelect) + return Fail(ApiErrors.InvalidParams("select is invalid for dictionary terminals.")); + if (options.HasOffset || options.HasLimit) + return Fail(ApiErrors.InvalidParams("offset and limit are valid only for array or list terminals.")); + return Ok(typeName, CollectionSummary(kind, actual, GetCount(value, actual))); + } + + if (options.HasSelect && IsScalar(Unwrap(elementType))) + return Fail(ApiErrors.InvalidParams("select is invalid for scalar collection elements.")); + if (options.HasSelect && TryCollectionKind(elementType, out _, out _, out _)) + return Fail(ApiErrors.InvalidParams("select is invalid for collection elements.")); + + var page = ReadPage(value, actual, elementType, options); + if (page.Error != null) return page; + page.TypeName = typeName; + return page; + } + + if (options.HasOffset || options.HasLimit) + return Fail(ApiErrors.InvalidParams("offset and limit are valid only for array or list terminals.")); + + if (IsScalar(Unwrap(actual))) + { + if (options.HasSelect) + return Fail(ApiErrors.InvalidParams("select is invalid for scalar terminals.")); + if (!TryEncodeScalar(value, actual, out var encoded, out var err)) + return Fail(err); + return Ok(typeName, encoded); + } + + if (!IsAllowedType(actual)) + return Fail(ApiErrors.UnsupportedType()); + + if (options.HasSelect) + { + var projected = Project(value, actual, options.Select); + if (projected.Error != null) return projected; + projected.TypeName = typeName; + return projected; + } + + return Ok(typeName, ObjectSummary(actual)); + } + + WalkStep Walk(object root, Type declaredType, PathSeg[] path) + { + var current = root; + var declared = declaredType; + var runtime = current == null ? null : current.GetType(); + if (path == null || path.Length == 0) + return new WalkStep { Value = current, DeclaredType = declared, RuntimeType = runtime }; + + for (var i = 0; i < path.Length; i++) + { + if (current == null) + return new WalkStep { Error = ApiErrors.NullPath(i) }; + runtime = current.GetType(); + var seg = path[i]; + if (seg.Kind == PathKind.Member) + { + var slot = FindMember(runtime, seg.Name); + if (slot == null) + return new WalkStep { Error = ApiErrors.MemberNotFound(seg.Name, i) }; + if (!slot.Allowed) + return new WalkStep { Error = ApiErrors.MemberNotAllowed(seg.Name, i) }; + if (!TryGetMemberValue(current, slot, i, out current, out var err)) + return new WalkStep { Error = err }; + declared = slot.DeclaredType; + runtime = current == null ? null : current.GetType(); + continue; + } + + if (seg.Kind == PathKind.Index) + { + if (!TryCollectionKind(runtime, out var kind, out var elementType, out _) || + !string.Equals(kind, "array", StringComparison.Ordinal) && + !string.Equals(kind, "list", StringComparison.Ordinal)) + return new WalkStep { Error = ApiErrors.UnsupportedType(i) }; + if (!TryGetIndex(current, runtime, seg.Index, i, out current, out var err)) + return new WalkStep { Error = err }; + declared = elementType; + runtime = current == null ? null : current.GetType(); + continue; + } + + if (!TryCollectionKind(runtime, out var dkind, out var valueType, out var keyType) || + !string.Equals(dkind, "dictionary", StringComparison.Ordinal)) + return new WalkStep { Error = ApiErrors.UnsupportedType(i) }; + if (!TryCoerceKey(seg.Key, keyType, out var key, out var keyErr)) + return new WalkStep { Error = keyErr ?? ApiErrors.InvalidParams("dictionary key type mismatch.") }; + if (!TryGetKey(current, key, i, out current, out var kerr)) + return new WalkStep { Error = kerr }; + declared = valueType; + runtime = current == null ? null : current.GetType(); + } + + return new WalkStep { Value = current, DeclaredType = declared, RuntimeType = current == null ? null : current.GetType() }; + } + + WalkResult ReadPage(object collection, Type collectionType, Type elementType, ReadOptions options) + { + var total = GetCount(collection, collectionType); + var offset = options.EffectiveOffset; + var limit = options.EffectiveLimit; + if (options.HasSelect) + { + var probe = GetCached(elementType); + if (options.Select != null) + { + foreach (var name in options.Select) + { + var slot = FindMember(elementType, name); + if (slot == null) return Fail(ApiErrors.MemberNotFound(name)); + if (!slot.Allowed) return Fail(ApiErrors.MemberNotAllowed(name)); + } + } + + _ = probe; + } + + var items = new List(); + if (offset < total) + { + var take = Math.Min(limit, total - offset); + for (var i = 0; i < take; i++) + { + if (!TryGetIndex(collection, collectionType, offset + i, null, out var item, out var err)) + return Fail(err); + if (item == null) + { + items.Add(null); + continue; + } + + var itemType = item.GetType(); + if (options.HasSelect) + { + if (IsScalar(Unwrap(itemType)) || TryCollectionKind(itemType, out _, out _, out _)) + return Fail(ApiErrors.InvalidParams("select is invalid for scalar or collection elements.")); + var projected = Project(item, itemType, options.Select); + if (projected.Error != null) return projected; + items.Add(projected.Encoded); + } + else if (IsScalar(Unwrap(itemType))) + { + if (!TryEncodeScalar(item, itemType, out var encoded, out var encErr)) + return Fail(encErr); + items.Add(encoded); + } + else if (TryCollectionKind(itemType, out var ikind, out _, out _)) + items.Add(CollectionSummary(ikind, itemType, GetCount(item, itemType))); + else if (!IsAllowedType(itemType)) + return Fail(ApiErrors.UnsupportedType()); + else + items.Add(ObjectSummary(itemType)); + } + } + + return new WalkResult + { + Encoded = new Dictionary + { + ["items"] = items, + ["offset"] = offset, + ["totalCount"] = total, + ["hasMore"] = offset + items.Count < total + } + }; + } + + WalkResult Project(object value, Type type, string[] select) + { + var map = new Dictionary(select.Length); + foreach (var name in select) + { + var slot = FindMember(type, name); + if (slot == null) return Fail(ApiErrors.MemberNotFound(name)); + if (!slot.Allowed) return Fail(ApiErrors.MemberNotAllowed(name)); + if (!TryGetMemberValue(value, slot, null, out var memberValue, out var err)) + return Fail(err); + if (memberValue == null) + { + map[name] = null; + continue; + } + + var mt = memberValue.GetType(); + if (IsScalar(Unwrap(mt))) + { + if (!TryEncodeScalar(memberValue, mt, out var encoded, out var encErr)) + return Fail(encErr); + map[name] = encoded; + } + else if (TryCollectionKind(mt, out var kind, out _, out _)) + map[name] = CollectionSummary(kind, mt, GetCount(memberValue, mt)); + else if (!IsAllowedType(mt)) + return Fail(ApiErrors.UnsupportedType(member: name)); + else + map[name] = ObjectSummary(mt); + } + + return new WalkResult { Encoded = map }; + } + + bool TryGetMemberValue(object instance, MemberSlot slot, int? segmentIndex, out object value, out ApiError error) + { + value = null; + error = null; + try + { + if (slot.Field != null) + { + value = slot.Field.GetValue(instance); + return true; + } + + value = slot.Property.GetValue(instance, null); + return true; + } + catch (Exception ex) + { + Warn?.Invoke($"API read failed at {slot.Name}: {ex}"); + error = ApiErrors.ReadFailed(segmentIndex, slot.Name); + return false; + } + } + + bool TryGetIndex(object collection, Type collectionType, int index, int? segmentIndex, out object value, out ApiError error) + { + value = null; + error = null; + var count = GetCount(collection, collectionType); + if (index < 0 || index >= count) + { + error = ApiErrors.IndexOutOfRange(segmentIndex ?? 0); + return false; + } + + try + { + if (collectionType.IsArray) + { + value = ((Array)collection).GetValue(index); + return true; + } + + value = ((IList)collection)[index]; + return true; + } + catch (Exception ex) + { + Warn?.Invoke($"API index read failed: {ex}"); + error = ApiErrors.ReadFailed(segmentIndex); + return false; + } + } + + static bool TryGetKey(object collection, object key, int segmentIndex, out object value, out ApiError error) + { + value = null; + error = null; + try + { + var dict = (IDictionary)collection; + if (!dict.Contains(key)) + { + error = ApiErrors.KeyNotFound(segmentIndex); + return false; + } + + value = dict[key]; + return true; + } + catch (Exception ex) + { + Warn?.Invoke($"API key read failed: {ex}"); + error = ApiErrors.ReadFailed(segmentIndex); + return false; + } + } + + static bool TryCoerceKey(object raw, Type keyType, out object key, out ApiError error) + { + key = null; + error = null; + if (keyType == typeof(int)) + { + if (raw is int i) + { + key = i; + return true; + } + + error = ApiErrors.InvalidParams("dictionary key type mismatch."); + return false; + } + + if (keyType == typeof(string)) + { + if (raw is string s) + { + key = s; + return true; + } + + error = ApiErrors.InvalidParams("dictionary key type mismatch."); + return false; + } + + error = ApiErrors.UnsupportedType(); + return false; + } + + static int GetCount(object collection, Type type) + { + if (type.IsArray) return ((Array)collection).Length; + return ((ICollection)collection).Count; + } + + MemberSlot FindMember(Type type, string name) + { + var cached = GetCached(type); + return cached.ByName.TryGetValue(name, out var slot) ? slot : null; + } + + MemberInfoDto[] GetExposedMembers(Type type) + { + return GetCached(type).Exposed; + } + + CachedType GetCached(Type type) + { + lock (_cacheGate) + { + if (_cache.TryGetValue(type, out var hit)) + return hit; + } + + var built = BuildMembers(type); + lock (_cacheGate) + { + if (_cache.TryGetValue(type, out var hit)) + return hit; + if (_cache.Count < MaxCache) + _cache[type] = built; + return built; + } + } + + CachedType BuildMembers(Type type) + { + var byName = new Dictionary(StringComparer.Ordinal); + for (var t = type; t != null && t != typeof(object); t = t.BaseType) + { + foreach (var field in t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) + { + if (field.IsStatic || field.IsDefined(typeof(CompilerGeneratedAttribute), false) || field.Name.IndexOf('<') >= 0) + continue; + if (byName.ContainsKey(field.Name)) + continue; + var ft = field.FieldType; + byName[field.Name] = new MemberSlot + { + Name = field.Name, + Kind = "field", + DeclaredType = ft, + Field = field, + Allowed = IsAllowedType(ft) + }; + } + + foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) + { + if (prop.GetIndexParameters().Length != 0) + continue; + if (byName.ContainsKey(prop.Name)) + continue; + var allowed = IsAllowedProperty(prop) && IsAllowedType(prop.PropertyType) && prop.CanRead && prop.GetGetMethod(true) != null; + byName[prop.Name] = new MemberSlot + { + Name = prop.Name, + Kind = "property", + DeclaredType = prop.PropertyType, + Property = prop, + Allowed = allowed + }; + } + } + + var exposed = new List(); + foreach (var pair in byName) + { + if (!pair.Value.Allowed) continue; + exposed.Add(new MemberInfoDto + { + Name = pair.Value.Name, + Type = TypeName(pair.Value.DeclaredType), + Kind = pair.Value.Kind + }); + } + + exposed.Sort((a, b) => string.CompareOrdinal(a.Name, b.Name)); + return new CachedType { ByName = byName, Exposed = exposed.ToArray() }; + } + + static bool IsAllowedProperty(PropertyInfo prop) + { + return prop.DeclaringType == typeof(GameHistoryData) && + string.Equals(prop.Name, "currentTech", StringComparison.Ordinal); + } + + bool IsAllowedType(Type type) + { + if (type == null) return false; + type = Unwrap(type); + if (IsForbidden(type)) return false; + if (IsScalar(type) || IsUnityStruct(type)) return true; + if (IsSupportedArray(type) || IsSupportedList(type) || IsSupportedDictionary(type)) return true; + if (type.Assembly == GameAssembly) return true; + if (_extraAssemblies != null && _extraAssemblies.Contains(type.Assembly)) return true; + return false; + } + + bool IsSupportedArray(Type type) + { + if (!type.IsArray || type.GetArrayRank() != 1) return false; + var element = type.GetElementType(); + return element != null && !element.IsPointer && IsAllowedType(element); + } + + bool IsSupportedList(Type type) + { + if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(List<>)) return false; + return IsAllowedType(type.GetGenericArguments()[0]); + } + + bool IsSupportedDictionary(Type type) + { + if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Dictionary<,>)) return false; + var args = type.GetGenericArguments(); + if (args[0] != typeof(int) && args[0] != typeof(string)) return false; + return IsAllowedType(args[1]); + } + + bool TryCollectionKind(Type type, out string kind, out Type elementType, out Type keyType) + { + kind = null; + elementType = null; + keyType = null; + if (type == null) return false; + type = Unwrap(type); + if (IsSupportedArray(type)) + { + kind = "array"; + elementType = type.GetElementType(); + return true; + } + + if (IsSupportedList(type)) + { + kind = "list"; + elementType = type.GetGenericArguments()[0]; + return true; + } + + if (IsSupportedDictionary(type)) + { + kind = "dictionary"; + var args = type.GetGenericArguments(); + keyType = args[0]; + elementType = args[1]; + return true; + } + + return false; + } + + static bool IsForbidden(Type t) + { + if (t.IsPointer || t.IsByRef || t.IsGenericParameter) return true; + if (t == typeof(IntPtr) || t == typeof(UIntPtr) || t == typeof(TypedReference)) return true; + if (typeof(Delegate).IsAssignableFrom(t)) return true; + if (typeof(Type).IsAssignableFrom(t) || typeof(MemberInfo).IsAssignableFrom(t) || + typeof(Assembly).IsAssignableFrom(t) || typeof(Module).IsAssignableFrom(t)) + return true; + if (typeof(System.IO.Stream).IsAssignableFrom(t) || typeof(System.IO.TextReader).IsAssignableFrom(t) || + typeof(System.IO.TextWriter).IsAssignableFrom(t)) + return true; + if (typeof(Task).IsAssignableFrom(t) || typeof(Thread).IsAssignableFrom(t) || typeof(WaitHandle).IsAssignableFrom(t)) + return true; + if (t == typeof(SpinLock) || t == typeof(Mutex) || t == typeof(Semaphore) || t == typeof(ReaderWriterLock) || + t == typeof(ReaderWriterLockSlim)) + return true; + if (typeof(UnityEngine.Object).IsAssignableFrom(t)) return true; + return false; + } + + static bool IsScalar(Type t) + { + if (t.IsEnum) return true; + return t == typeof(bool) || t == typeof(string) || t == typeof(char) || + t == typeof(byte) || t == typeof(sbyte) || t == typeof(short) || t == typeof(ushort) || + t == typeof(int) || t == typeof(uint) || t == typeof(long) || t == typeof(ulong) || + t == typeof(float) || t == typeof(double) || t == typeof(decimal) || + t == typeof(DateTime) || t == typeof(DateTimeOffset) || t == typeof(Guid); + } + + static bool IsUnityStruct(Type t) + { + return t == typeof(Vector2) || t == typeof(Vector3) || t == typeof(Vector4) || t == typeof(Quaternion); + } + + static Type Unwrap(Type t) + { + return Nullable.GetUnderlyingType(t) ?? t; + } + + static bool TryEncodeScalar(object value, Type type, out object encoded, out ApiError error) + { + encoded = null; + error = null; + type = Unwrap(type); + if (value == null) + return true; + if (type.IsEnum) + { + var name = Enum.GetName(type, value); + var underlying = Convert.ToInt64(value, CultureInfo.InvariantCulture); + encoded = new Dictionary + { + ["name"] = name, + ["value"] = underlying.ToString(CultureInfo.InvariantCulture) + }; + return true; + } + + if (type == typeof(bool)) + { + encoded = (bool)value; + return true; + } + + if (type == typeof(uint)) + { + encoded = (uint)value; + return true; + } + + if (type == typeof(int) || type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || + type == typeof(ushort)) + { + encoded = Convert.ToInt32(value, CultureInfo.InvariantCulture); + return true; + } + + if (type == typeof(string)) + { + var s = (string)value; + if (!CheckStringSize(s, out error)) return false; + encoded = s; + return true; + } + + if (type == typeof(char)) + { + encoded = ((char)value).ToString(); + return true; + } + + if (type == typeof(long)) + { + encoded = ((long)value).ToString(CultureInfo.InvariantCulture); + return true; + } + + if (type == typeof(ulong)) + { + encoded = ((ulong)value).ToString(CultureInfo.InvariantCulture); + return true; + } + + if (type == typeof(decimal)) + { + encoded = ((decimal)value).ToString(CultureInfo.InvariantCulture); + return true; + } + + if (type == typeof(float)) + { + var f = (float)value; + if (float.IsNaN(f) || float.IsInfinity(f)) + { + error = ApiErrors.UnsupportedValue(); + return false; + } + + encoded = f; + return true; + } + + if (type == typeof(double)) + { + var d = (double)value; + if (double.IsNaN(d) || double.IsInfinity(d)) + { + error = ApiErrors.UnsupportedValue(); + return false; + } + + encoded = d; + return true; + } + + if (type == typeof(DateTime)) + { + encoded = ((DateTime)value).ToString("o", CultureInfo.InvariantCulture); + return true; + } + + if (type == typeof(DateTimeOffset)) + { + encoded = ((DateTimeOffset)value).ToString("o", CultureInfo.InvariantCulture); + return true; + } + + if (type == typeof(Guid)) + { + encoded = ((Guid)value).ToString("D"); + return true; + } + + error = ApiErrors.UnsupportedType(); + return false; + } + + static bool CheckStringSize(string s, out ApiError error) + { + error = null; + if (s == null) return true; + if (s.Length > ApiLimits.MaxResponseBytes || Utf8.GetByteCount(s) > ApiLimits.MaxResponseBytes) + { + error = ApiErrors.LimitExceeded("String value exceeds maxResponseBytes."); + return false; + } + + return true; + } + + static Dictionary ObjectSummary(Type type) + { + return new Dictionary + { + ["$kind"] = "object", + ["$type"] = TypeName(type) + }; + } + + static Dictionary CollectionSummary(string kind, Type type, int count) + { + return new Dictionary + { + ["$kind"] = kind, + ["$type"] = TypeName(type), + ["count"] = count + }; + } + + internal static string TypeName(Type type) + { + if (type == null) return null; + type = Unwrap(type); + if (type.IsArray && type.GetArrayRank() == 1) + return TypeName(type.GetElementType()) + "[]"; + return type.FullName ?? type.Name; + } + + static WalkResult Fail(ApiError error) => new WalkResult { Error = error }; + + static WalkResult Ok(string typeName, object encoded) => + new WalkResult { TypeName = typeName, Encoded = encoded, IsNull = encoded == null }; + + sealed class MemberSlot + { + public string Name; + public string Kind; + public Type DeclaredType; + public FieldInfo Field; + public PropertyInfo Property; + public bool Allowed; + } + + sealed class CachedType + { + public Dictionary ByName; + public MemberInfoDto[] Exposed; + } + + struct WalkStep + { + public object Value; + public Type DeclaredType; + public Type RuntimeType; + public ApiError Error; + } +} diff --git a/LiveStreamAssist/Api/WebSocketApiFeature.cs b/LiveStreamAssist/Api/WebSocketApiFeature.cs new file mode 100644 index 0000000..7c6ff94 --- /dev/null +++ b/LiveStreamAssist/Api/WebSocketApiFeature.cs @@ -0,0 +1,332 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Net; +using System.Threading; +using BepInEx.Configuration; +using UXAssist.Common.ModFeatures; +using GameLogicProc = UXAssist.Common.GameLogic; + +namespace LiveStreamAssist.Api; + +[ModFeature("WebSocketApi", Order = 51)] +internal static class WebSocketApiFeature +{ + internal static ConfigEntry Enabled; + internal static ConfigEntry ListenAddress; + internal static ConfigEntry Port; + + static readonly string[] RootNames = + { "game", "player", "history", "statistics", "galaxy", "localPlanet", "localStar" }; + + static readonly Type[] RootTypes = + { + typeof(GameData), typeof(Player), typeof(GameHistoryData), typeof(GameStatData), + typeof(GalaxyData), typeof(PlanetData), typeof(StarData) + }; + + static readonly object Gate = new object(); + static object _ownerToken; + static WebSocketApiServer _server; + static MainThreadDispatcher _dispatcher; + static ReflectionReader _reader; + static GameVersionSnapshot _version = GameVersionSnapshot.Empty; + static SessionSnapshot _session = new SessionSnapshot(0, null); + static bool _started; + + public static void BindConfig(ConfigFile config) + { + Enabled = config.Bind("WebSocketApi", "Enabled", false, + "Start the WebSocket API listener when the mod starts"); + ListenAddress = config.Bind("WebSocketApi", "ListenAddress", "127.0.0.1", + "Bind address; use a specific LAN address or 0.0.0.0 for LAN access"); + Port = config.Bind("WebSocketApi", "Port", 18080, + new ConfigDescription("Listening port", new AcceptableValueRange(1, 65535))); + } + + public static void Init() + { + _reader = new ReflectionReader(); + ReflectionReader.Warn = msg => LiveStreamAssist.Logger.LogWarning(msg); + _version = GameVersionSnapshot.Empty; + _session = new SessionSnapshot(0, null); + } + + public static void Start() + { + if (_started) return; + if (Enabled == null || !Enabled.Value) return; + var ctx = SynchronizationContext.Current; + if (ctx == null || ctx.GetType().FullName != "UnityEngine.UnitySynchronizationContext") + { + LiveStreamAssist.Logger.LogError( + "WebSocket API requires Unity's installed SynchronizationContext; startup aborted."); + return; + } + + if (!IPAddress.TryParse(ListenAddress.Value, out var address)) + { + LiveStreamAssist.Logger.LogError($"WebSocket API listen address is invalid: {ListenAddress.Value}"); + return; + } + + var port = Port.Value; + if (port < 1 || port > 65535) + { + LiveStreamAssist.Logger.LogError($"WebSocket API port is invalid: {port}"); + return; + } + + var token = new object(); + var dispatcher = new MainThreadDispatcher(ctx, token, ExecuteWork, () => _server?.NowMs ?? 0); + var server = new WebSocketApiServer(address, port, PluginInfo.PLUGIN_VERSION, GetVersion, GetSession, dispatcher); + GameLogicProc.OnDataLoaded += OnDataLoaded; + GameLogicProc.OnGameBegin += OnGameBegin; + GameLogicProc.OnGameEnd += OnGameEnd; + lock (Gate) + { + _ownerToken = token; + _dispatcher = dispatcher; + _server = server; + _started = true; + } + + try + { + server.Start(); + } + catch (Exception ex) + { + LiveStreamAssist.Logger.LogError($"WebSocket API failed to start: {ex}"); + Uninit(); + return; + } + + if (VFPreload.done || VFPreload.dbDone) + PublishVersion(); + if (IsGameReady()) + BeginSession(); + } + + public static void Uninit() + { + WebSocketApiServer server; + MainThreadDispatcher dispatcher; + lock (Gate) + { + if (!_started && _server == null) return; + _started = false; + _ownerToken = new object(); + server = _server; + dispatcher = _dispatcher; + _server = null; + _dispatcher = null; + _session = new SessionSnapshot(_session.Generation + 1, null); + } + + GameLogicProc.OnDataLoaded -= OnDataLoaded; + GameLogicProc.OnGameBegin -= OnGameBegin; + GameLogicProc.OnGameEnd -= OnGameEnd; + try { dispatcher?.Stop(); } + catch (Exception ex) { LiveStreamAssist.Logger.LogWarning($"WebSocket API dispatcher stop failed: {ex.Message}"); } + try { server?.Stop(); } + catch (Exception ex) { LiveStreamAssist.Logger.LogWarning($"WebSocket API server stop failed: {ex.Message}"); } + _reader?.ClearCache(); + ReflectionReader.Warn = null; + } + + public static bool IsKnownRoot(string name) + { + for (var i = 0; i < RootNames.Length; i++) + { + if (string.Equals(RootNames[i], name, StringComparison.Ordinal)) + return true; + } + + return false; + } + + static GameVersionSnapshot GetVersion() => _version; + static SessionSnapshot GetSession() => _session; + + static void OnDataLoaded() => PublishVersion(); + + static void PublishVersion() + { + var v = GameConfig.gameVersion; + _version = new GameVersionSnapshot(true, v.ToString(), GameConfig.build); + } + + static void OnGameBegin() + { + if (IsGameReady()) + BeginSession(); + } + + static void OnGameEnd() + { + EndSession(); + } + + static void BeginSession() + { + MainThreadDispatcher dispatcher; + int oldGeneration; + lock (Gate) + { + oldGeneration = _session.Generation; + _session = new SessionSnapshot(oldGeneration + 1, Guid.NewGuid().ToString("D")); + dispatcher = _dispatcher; + } + + dispatcher?.CancelQueued(_ => ApiErrors.SessionChanged()); + } + + static void EndSession() + { + MainThreadDispatcher dispatcher; + lock (Gate) + { + _session = new SessionSnapshot(_session.Generation + 1, null); + dispatcher = _dispatcher; + } + + dispatcher?.CancelQueued(_ => ApiErrors.SessionChanged()); + } + + static bool IsGameReady() + { + return GameMain.data != null && GameMain.isRunning && !GameMain.isLoading && !DSPGame.IsMenuDemo; + } + + static object ResolveRoot(string name) + { + switch (name) + { + case "game": return GameMain.data; + case "player": return GameMain.mainPlayer; + case "history": return GameMain.history; + case "statistics": return GameMain.statistics; + case "galaxy": return GameMain.galaxy; + case "localPlanet": return GameMain.localPlanet; + case "localStar": return GameMain.localStar; + default: return null; + } + } + + static Type DeclaredRootType(string name) + { + for (var i = 0; i < RootNames.Length; i++) + { + if (string.Equals(RootNames[i], name, StringComparison.Ordinal)) + return RootTypes[i]; + } + + return null; + } + + static object ExecuteWork(GameWork work) + { + var session = _session; + if (work.Generation != session.Generation) + return ApiErrors.SessionChanged(); + var call = work.Call; + if (call.Method == DataMethod.Roots) + return BuildRoots(session); + if (!IsGameReady()) + return ApiErrors.GameNotReady(); + if (session.SessionId == null) + return ApiErrors.GameNotReady(); + var instance = ResolveRoot(call.Root); + if (instance == null) + return ApiErrors.RootUnavailable(call.Root); + var declared = DeclaredRootType(call.Root); + WalkResult walked; + if (call.Method == DataMethod.Describe) + walked = _reader.Describe(instance, declared, call.Path); + else + walked = _reader.Read(instance, declared, call.Path, call.Options); + if (walked.Error != null) + return walked.Error; + if (_session.Generation != work.Generation) + return ApiErrors.SessionChanged(); + var tick = GameMain.gameTick.ToString(CultureInfo.InvariantCulture); + if (call.Method == DataMethod.Describe) + return BuildDescribe(walked, session.SessionId, tick); + return BuildRead(walked, session.SessionId, tick); + } + + static Dictionary BuildRoots(SessionSnapshot session) + { + var ready = IsGameReady() && session.SessionId != null; + var roots = new List(RootNames.Length); + for (var i = 0; i < RootNames.Length; i++) + { + var available = ready && ResolveRoot(RootNames[i]) != null; + roots.Add(new Dictionary + { + ["name"] = RootNames[i], + ["type"] = ReflectionReader.TypeName(RootTypes[i]), + ["available"] = available + }); + } + + return new Dictionary + { + ["gameReady"] = ready, + ["sessionId"] = ready ? session.SessionId : null, + ["gameTick"] = ready ? GameMain.gameTick.ToString(CultureInfo.InvariantCulture) : null, + ["roots"] = roots + }; + } + + static Dictionary BuildDescribe(WalkResult walked, string sessionId, string tick) + { + var members = new List(walked.Members?.Length ?? 0); + if (walked.Members != null) + { + foreach (var m in walked.Members) + { + members.Add(new Dictionary + { + ["name"] = m.Name, + ["type"] = m.Type, + ["kind"] = m.Kind + }); + } + } + + object collection = null; + if (walked.Collection != null) + { + collection = new Dictionary + { + ["kind"] = walked.Collection.Kind, + ["elementType"] = walked.Collection.ElementType, + ["keyType"] = walked.Collection.KeyType, + ["count"] = walked.Collection.Count + }; + } + + return new Dictionary + { + ["type"] = walked.TypeName, + ["isNull"] = walked.IsNull, + ["members"] = members, + ["collection"] = collection, + ["sessionId"] = sessionId, + ["gameTick"] = tick + }; + } + + static Dictionary BuildRead(WalkResult walked, string sessionId, string tick) + { + return new Dictionary + { + ["type"] = walked.TypeName, + ["value"] = walked.Encoded, + ["sessionId"] = sessionId, + ["gameTick"] = tick + }; + } +} diff --git a/LiveStreamAssist/Api/WebSocketApiServer.cs b/LiveStreamAssist/Api/WebSocketApiServer.cs new file mode 100644 index 0000000..15bbb05 --- /dev/null +++ b/LiveStreamAssist/Api/WebSocketApiServer.cs @@ -0,0 +1,544 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using Fleck; +using Newtonsoft.Json.Linq; + +namespace LiveStreamAssist.Api; + +internal sealed class PendingRequest +{ + int _done; + public ApiConnection Connection; + public string Id; + public bool IsActive => System.Threading.Volatile.Read(ref _done) == 0 && Connection != null && Connection.IsOpen; + + public bool TryMarkDone() => System.Threading.Interlocked.Exchange(ref _done, 1) == 0; + + public bool Complete(ApiError error) + { + if (error == null || !TryMarkDone()) return false; + Connection.Deliver(this, null, error); + return true; + } + + public bool CompleteResult(object result) + { + if (!TryMarkDone()) return false; + Connection.Deliver(this, result, null); + return true; + } +} + +internal sealed class ApiConnection +{ + readonly WebSocketApiServer _server; + readonly IWebSocketConnection _socket; + readonly object _gate = new object(); + readonly Dictionary _outstanding = new Dictionary(StringComparer.Ordinal); + readonly Queue _sendQueue = new Queue(); + bool _sending; + bool _closed; + int _pending; + + public ApiConnection(WebSocketApiServer server, IWebSocketConnection socket) + { + _server = server; + _socket = socket; + } + + public bool IsOpen + { + get + { + lock (_gate) return !_closed && _socket.IsAvailable; + } + } + + public int Pending + { + get + { + lock (_gate) return _pending; + } + } + + public PendingRequest TryAdmit(string id, out bool duplicate, out bool busy) + { + duplicate = false; + busy = false; + lock (_gate) + { + if (_closed) return null; + if (_outstanding.ContainsKey(id)) + { + duplicate = true; + return null; + } + + if (_pending >= ApiLimits.MaxPendingRequestsPerConnection || !_server.TryIncrementPending()) + { + busy = true; + return null; + } + + var pending = new PendingRequest { Connection = this, Id = id }; + _outstanding[id] = pending; + _pending++; + return pending; + } + } + + public bool CanSendBusy() + { + lock (_gate) + return !_closed && _sendQueue.Count < ApiLimits.MaxPendingRequestsPerConnection; + } + + public void EnqueueError(object id, ApiError error) + { + byte[] payload; + try + { + payload = ApiProtocol.SerializeEnvelope(id, null, error); + } + catch (ResponseTooLargeException) + { + Close(ApiLimits.CloseTryAgainLater); + return; + } + + EnqueueSend(payload); + } + + public void Deliver(PendingRequest pending, object result, ApiError error) + { + Release(pending.Id); + if (_closed) return; + byte[] payload; + try + { + payload = ApiProtocol.SerializeEnvelope(pending.Id, result, error); + } + catch (ResponseTooLargeException) + { + try + { + payload = ApiProtocol.SerializeEnvelope(pending.Id, null, ApiErrors.LimitExceeded("Response exceeds maxResponseBytes.")); + } + catch (ResponseTooLargeException) + { + Close(ApiLimits.CloseTryAgainLater); + return; + } + } + + EnqueueSend(payload); + } + + public void Release(string id) + { + lock (_gate) + { + if (id != null) + _outstanding.Remove(id); + if (_pending > 0) + _pending--; + } + + _server.DecrementPending(); + } + + public void EnqueueSend(byte[] payload) + { + lock (_gate) + { + if (_closed) return; + if (_sendQueue.Count >= ApiLimits.MaxPendingRequestsPerConnection) + { + CloseLocked(ApiLimits.CloseTryAgainLater); + return; + } + + _sendQueue.Enqueue(new SendItem { Payload = payload, EnqueuedMs = _server.NowMs }); + if (!_sending) + { + _sending = true; + DrainSend(); + } + } + } + + public void Close(int code) + { + lock (_gate) CloseLocked(code); + } + + public void NotifyDisconnected() + { + FinishClose(null); + } + + void CloseLocked(int code) + { + FinishClose(code); + } + + void FinishClose(int? code) + { + PendingRequest[] pending; + lock (_gate) + { + if (_closed) return; + _closed = true; + _sendQueue.Clear(); + _sending = false; + pending = new PendingRequest[_outstanding.Count]; + if (_outstanding.Count > 0) + _outstanding.Values.CopyTo(pending, 0); + } + + if (code.HasValue) + { + try + { + if (_socket.IsAvailable) + _socket.Close(code.Value); + } + catch (Exception ex) + { + LiveStreamAssist.Logger.LogWarning($"API close failed: {ex.Message}"); + } + } + + _server.OnConnectionClosed(this); + foreach (var item in pending) + item.Complete(ApiErrors.RequestTimeout()); + } + + void DrainSend() + { + SendItem item; + lock (_gate) + { + if (_closed || _sendQueue.Count == 0) + { + _sending = false; + return; + } + + item = _sendQueue.Peek(); + if (_server.NowMs - item.EnqueuedMs > ApiLimits.RequestTimeoutMs) + { + CloseLocked(ApiLimits.CloseTryAgainLater); + return; + } + + _sendQueue.Dequeue(); + } + + Task task; + try + { + var text = Encoding.UTF8.GetString(item.Payload); + task = _socket.Send(text); + } + catch (Exception) + { + Close(ApiLimits.CloseTryAgainLater); + return; + } + + if (task == null) + { + Close(ApiLimits.CloseTryAgainLater); + return; + } + + task.ContinueWith(_ => DrainSend(), System.Threading.CancellationToken.None, + TaskContinuationOptions.None, TaskScheduler.Default); + } + + struct SendItem + { + public byte[] Payload; + public long EnqueuedMs; + } +} + +internal sealed class WebSocketApiServer +{ + readonly IPAddress _address; + readonly int _port; + readonly string _modVersion; + readonly Func _version; + readonly Func _session; + readonly MainThreadDispatcher _dispatcher; + readonly object _gate = new object(); + readonly List _connections = new List(); + readonly System.Diagnostics.Stopwatch _clock = System.Diagnostics.Stopwatch.StartNew(); + WebSocketServer _listener; + int _pending; + bool _alive; + + public WebSocketApiServer( + IPAddress address, + int port, + string modVersion, + Func version, + Func session, + MainThreadDispatcher dispatcher) + { + _address = address; + _port = port; + _modVersion = modVersion; + _version = version; + _session = session; + _dispatcher = dispatcher; + } + + public long NowMs => _clock.ElapsedMilliseconds; + + public void Start() + { + var host = _address.Equals(IPAddress.Any) ? "0.0.0.0" : _address.ToString(); + if (_address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) + host = "[" + host + "]"; + var location = "ws://" + host + ":" + _port; + FleckLog.Level = LogLevel.Error; + FleckLog.LogAction = (level, message, ex) => + { + if (level == LogLevel.Error) + LiveStreamAssist.Logger.LogError(ex != null ? message + ": " + ex : message); + }; + var server = new WebSocketServer(location, false); + server.RestartAfterListenError = false; + server.Start(OnSocket); + lock (_gate) + { + _listener = server; + _alive = true; + } + + LiveStreamAssist.Logger.LogInfo($"WebSocket API listening on {location}{ApiLimits.Path}"); + } + + public void Stop() + { + ApiConnection[] conns; + WebSocketServer listener; + lock (_gate) + { + if (!_alive && _listener == null) return; + _alive = false; + listener = _listener; + _listener = null; + conns = _connections.ToArray(); + _connections.Clear(); + } + + foreach (var conn in conns) + conn.Close(ApiLimits.CloseGoingAway); + try + { + listener?.Dispose(); + } + catch (Exception ex) + { + LiveStreamAssist.Logger.LogWarning($"WebSocket API listener dispose failed: {ex.Message}"); + } + } + + public bool TryIncrementPending() + { + lock (_gate) + { + if (!_alive || _pending >= ApiLimits.MaxPendingRequests) return false; + _pending++; + return true; + } + } + + public void DecrementPending() + { + lock (_gate) + { + if (_pending > 0) _pending--; + } + } + + public void OnConnectionClosed(ApiConnection connection) + { + lock (_gate) + _connections.Remove(connection); + } + + void OnSocket(IWebSocketConnection socket) + { + var conn = new ApiConnection(this, socket); + var bounded = socket as WebSocketConnection; + if (bounded != null && bounded.Handler != null) + bounded.Handler = new BoundedHandler(bounded.Handler, ApiLimits.MaxIncomingSocketBytes); + + var path = socket.ConnectionInfo?.Path ?? ""; + var q = path.IndexOf('?'); + if (q >= 0) path = path.Substring(0, q); + + socket.OnOpen = () => + { + if (!string.Equals(path, ApiLimits.Path, StringComparison.Ordinal)) + { + conn.Close(ApiLimits.ClosePolicyViolation); + return; + } + + lock (_gate) + { + if (!_alive || _connections.Count >= ApiLimits.MaxConnections) + { + conn.Close(ApiLimits.CloseTryAgainLater); + return; + } + + _connections.Add(conn); + } + }; + socket.OnBinary = _ => conn.Close(ApiLimits.CloseUnsupportedData); + socket.OnMessage = message => OnMessage(conn, bounded, message); + socket.OnClose = () => + { + if (bounded?.Handler is BoundedHandler) + ((BoundedHandler)bounded.Handler).Reset(); + conn.NotifyDisconnected(); + }; + socket.OnError = ex => LiveStreamAssist.Logger.LogWarning($"WebSocket API connection error: {ex.Message}"); + var prevPing = socket.OnPing; + socket.OnPing = data => + { + if (bounded?.Handler is BoundedHandler h) h.Reset(); + prevPing?.Invoke(data); + }; + } + + void OnMessage(ApiConnection conn, WebSocketConnection raw, string message) + { + if (raw?.Handler is BoundedHandler bounded) + bounded.Reset(); + bool alive; + lock (_gate) alive = _alive; + if (!alive || !conn.IsOpen) return; + if (message == null || Encoding.UTF8.GetByteCount(message) > ApiLimits.MaxRequestBytes) + { + conn.Close(ApiLimits.CloseMessageTooBig); + return; + } + + if (!ApiProtocol.TryParseMessage(message, out var request, out var parseError, out var id)) + { + conn.EnqueueError(id, parseError); + return; + } + + var pending = conn.TryAdmit(request.Id, out var duplicate, out var busy); + if (pending == null) + { + if (duplicate) + { + conn.Close(ApiLimits.ClosePolicyViolation); + return; + } + + if (busy) + { + if (conn.CanSendBusy()) + conn.EnqueueError(request.Id, ApiErrors.ServerBusy()); + else + conn.Close(ApiLimits.CloseTryAgainLater); + } + + return; + } + try + { + if (!ApiProtocol.IsSystemMethod(request.Method) && !ApiProtocol.IsDataMethod(request.Method)) + { + pending.Complete(ApiErrors.MethodNotFound()); + return; + } + + if (ApiProtocol.IsSystemMethod(request.Method)) + { + var handled = ApiProtocol.HandleSystem(request.Method, request.Params, _version(), _modVersion); + if (handled is ApiError sysErr) + pending.Complete(sysErr); + else + pending.CompleteResult(handled); + return; + } + + if (!ApiProtocol.TryParseDataCall(request, out var call, out var callErr)) + { + pending.Complete(callErr); + return; + } + + if (call.Method != DataMethod.Roots && !WebSocketApiFeature.IsKnownRoot(call.Root)) + { + pending.Complete(ApiErrors.RootNotFound(call.Root)); + return; + } + + var session = _session(); + var work = new GameWork + { + Pending = pending, + Call = call, + Generation = session.Generation, + DeadlineMs = NowMs + ApiLimits.RequestTimeoutMs + }; + if (!_dispatcher.TryEnqueue(work)) + pending.Complete(ApiErrors.RequestTimeout()); + } + catch (Exception ex) + { + LiveStreamAssist.Logger.LogError($"API request handling failed: {ex}"); + pending.Complete(ApiErrors.InternalError()); + } + } +} + +sealed class BoundedHandler : IHandler +{ + readonly IHandler _inner; + readonly int _max; + int _consumed; + + public BoundedHandler(IHandler inner, int max) + { + _inner = inner; + _max = max; + } + + public void Reset() => _consumed = 0; + + public byte[] CreateHandshake(string subProtocol = null) => _inner.CreateHandshake(subProtocol); + + public void Receive(IEnumerable data) + { + var n = 0; + foreach (var _ in data) + n++; + if (_consumed > _max - n) + throw new WebSocketException(WebSocketStatusCodes.MessageTooBig); + _consumed += n; + _inner.Receive(data); + } + + public byte[] FrameText(string text) => _inner.FrameText(text); + public byte[] FrameBinary(byte[] bytes) => _inner.FrameBinary(bytes); + public byte[] FramePing(byte[] bytes) => _inner.FramePing(bytes); + public byte[] FramePong(byte[] bytes) => _inner.FramePong(bytes); + public byte[] FrameClose(int code) => _inner.FrameClose(code); +} diff --git a/LiveStreamAssist/LiveStreamAssist.cs b/LiveStreamAssist/LiveStreamAssist.cs index 99509d1..e361fa7 100644 --- a/LiveStreamAssist/LiveStreamAssist.cs +++ b/LiveStreamAssist/LiveStreamAssist.cs @@ -1,6 +1,7 @@ using System.Reflection; using BepInEx; using CommonAPI.Systems; +using LiveStreamAssist.Api; using UnityEngine; using UXAssist.Common; using UXAssist.Common.ModFeatures; @@ -19,6 +20,7 @@ public class LiveStreamAssist : BaseUnityPlugin { I18N.Init(); I18N.Add("KEYToggleLiveStreamAssist", "[LSA] Toggle live-stream statistics assist", "[LSA] \u5207\u6362\u76f4\u64ad\u7edf\u8ba1\u8f85\u52a9"); + WebSocketApiFeature.BindConfig(Config); ModFeatureRegistry.Discover(Assembly.GetExecutingAssembly()); I18N.Apply(); } diff --git a/LiveStreamAssist/LiveStreamAssist.csproj b/LiveStreamAssist/LiveStreamAssist.csproj index c69f0b9..6d70c2b 100644 --- a/LiveStreamAssist/LiveStreamAssist.csproj +++ b/LiveStreamAssist/LiveStreamAssist.csproj @@ -9,5 +9,15 @@ + + + + + + + <_PackRootFiles Include="$(TargetDir)Fleck.dll" Condition="Exists('$(TargetDir)Fleck.dll')" /> + <_PackRootFiles Include="$(TargetDir)Newtonsoft.Json.dll" Condition="Exists('$(TargetDir)Newtonsoft.Json.dll')" /> + + diff --git a/LiveStreamAssist/Properties/InternalsVisibleTo.cs b/LiveStreamAssist/Properties/InternalsVisibleTo.cs new file mode 100644 index 0000000..a0361ba --- /dev/null +++ b/LiveStreamAssist/Properties/InternalsVisibleTo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("ReflectionReaderCheck")] diff --git a/LiveStreamAssist/README.md b/LiveStreamAssist/README.md index 5f3b968..28793d5 100644 --- a/LiveStreamAssist/README.md +++ b/LiveStreamAssist/README.md @@ -1,6 +1,6 @@ # LiveStreamAssist -Keeps the Dyson Sphere Program statistics window active during a live stream by switching between the Production Statistics and Dyson Sphere tabs at a random interval. +Keeps the Dyson Sphere Program statistics window active during a live stream by switching between the Production Statistics and Dyson Sphere tabs at a random interval. Optionally exposes a local/LAN WebSocket API for read-only game data. ## Usage @@ -11,9 +11,18 @@ Keeps the Dyson Sphere Program statistics window active during a live stream by The mod requires UXAssist. -## Developer documentation +## WebSocket API -The WebSocket API is planned and is not implemented yet. +Disabled by default. In the BepInEx config file, set: + +```text +[WebSocketApi] +Enabled = true +ListenAddress = 127.0.0.1 +Port = 18080 +``` + +Then connect to `ws://:18080/api/v1`. There is no authentication. Use a LAN bind address or `0.0.0.0` only on trusted networks. Configuration changes require a game restart. - [WebSocket API reference](https://github.com/soarqin/DSP_Mods/blob/master/LiveStreamAssist/docs/WebSocketApi.md) - [WebSocket design and implementation plan](https://github.com/soarqin/DSP_Mods/blob/master/LiveStreamAssist/docs/WebSocketDesign.md) diff --git a/LiveStreamAssist/docs/WebSocketApi.md b/LiveStreamAssist/docs/WebSocketApi.md index eb6f640..41b509a 100644 --- a/LiveStreamAssist/docs/WebSocketApi.md +++ b/LiveStreamAssist/docs/WebSocketApi.md @@ -1,6 +1,6 @@ # LiveStreamAssist WebSocket API -Status: planned contract for API `1.0.0`; the WebSocket service is not implemented yet. This reference describes the intended first release, not functionality available in the current mod. +Status: implemented contract for API `1.0.0`. Enable the listener with BepInEx setting `WebSocketApi.Enabled`. Real-game and LAN acceptance still need a DSP run; see the [design and implementation plan](WebSocketDesign.md). Audience: developers of live-stream overlays, OBS integrations, and other clients. Server architecture and implementation tasks are documented separately in the [design and implementation plan](WebSocketDesign.md). diff --git a/LiveStreamAssist/docs/WebSocketDesign.md b/LiveStreamAssist/docs/WebSocketDesign.md index df91cf3..1e0223a 100644 --- a/LiveStreamAssist/docs/WebSocketDesign.md +++ b/LiveStreamAssist/docs/WebSocketDesign.md @@ -1,6 +1,6 @@ # LiveStreamAssist WebSocket Design and Implementation Plan -Status: implementation not started. This document is the handoff for a later implementation session. The current mod only implements statistics-window tab switching. The next implementation task is **Phase 0: runtime transport verification**. +Status: code is in tree; real-game/LAN acceptance is still blocked. Transport is Fleck **1.2.0** plus Newtonsoft.Json **13.0.3**. Fixture checks pass via `dotnet build LiveStreamAssist/tools/ReflectionReaderCheck/ReflectionReaderCheck.csproj -c Release` then `LiveStreamAssist/tools/ReflectionReaderCheck/bin/Release/net472/ReflectionReaderCheck.exe`. Enable the listener with BepInEx `WebSocketApi.Enabled` and rerun `LiveStreamAssist/tools/Test-WebSocketApi.ps1` in DSP. The [API reference](WebSocketApi.md) defines the client-visible contract. This document defines the scope, implementation constraints, source references, work order, and acceptance checks. Keep API usage documentation separate from implementation investigation and progress notes. @@ -184,12 +184,14 @@ Catch reflection/getter failures at the request boundary and map them to the API ### Phase 0: runtime transport verification -- [ ] Inspect the current project state and original runtime assumptions listed above. -- [ ] Evaluate Fleck first as a single managed WebSocket server dependency, plus an explicit Newtonsoft.Json dependency, while retaining `net472`. -- [ ] Verify the server inside actual DSP/BepInEx/Mono using a standard WebSocket client. A standalone .NET console server is not evidence of game-runtime compatibility. -- [ ] Verify text-message fragmentation, invalid UTF-8 handling, binary rejection, connection closure, and request-size enforcement before unbounded message accumulation. Check the library's behavior before promising a limit that its callbacks cannot enforce. -- [ ] Verify path rejection, clean listener disposal, occupied-port failure, and a bounded serialized send path. -- [ ] Record the selected package versions and any necessary runtime dependency DLLs here; pin them in `LiveStreamAssist.csproj`. +- [x] Inspect the current project state and original runtime assumptions listed above. +- [x] Evaluate Fleck first as a single managed WebSocket server dependency, plus an explicit Newtonsoft.Json dependency, while retaining `net472`. +- [ ] Verify the server inside actual DSP/BepInEx/Mono using a standard WebSocket client. A standalone .NET console server is not evidence of game-runtime compatibility. **Blocked: DSP was not launched in the implementation session.** +- [x] Verify text-message fragmentation, invalid UTF-8 handling, binary rejection, connection closure, and request-size enforcement before unbounded message accumulation. Check the library's behavior before promising a limit that its callbacks cannot enforce. +- [ ] Verify path rejection, clean listener disposal, occupied-port failure, and a bounded serialized send path. **Code is present; in-game confirmation blocked.** +- [x] Record the selected package versions and any necessary runtime dependency DLLs here; pin them in `LiveStreamAssist.csproj`. + +Selected packages: Fleck **1.2.0**, Newtonsoft.Json **13.0.3**. Packaged runtime DLLs: `Fleck.dll`, `Newtonsoft.Json.dll`. Fleck 1.2.0 has no max-message API; incoming size is capped by wrapping `IHandler.Receive` at `maxRequestBytes + 8KiB` socket bytes, then checking UTF-8 payload length in `OnMessage`. Invalid UTF-8 uses Fleck's thrower `UTF8Encoding` and close `1007`. Binary frames close `1003`. If Fleck cannot meet the runtime or bounded-buffer requirements, evaluate one other small managed Mono-compatible server library and document the reason. Do not write a new framing stack, switch to an ASP.NET host, change the target framework, or add multiple competing transports to hide an unresolved compatibility problem. Treat missing real-game verification as a blocker. @@ -197,24 +199,31 @@ Exit condition: the selected transport can actually listen, exchange bounded mes ### Phase 1: lifecycle, protocol, and system methods -- [ ] Add the separate API feature and startup-only configuration; preserve the existing feature's registration and state. -- [ ] Implement request validation, ID tracking, fixed dispatch, response/error envelopes, capacity accounting, and serial sends. -- [ ] Implement `system.ping`, `system.info`, and `system.validate` with the specified state-independent semantics. -- [ ] Implement only the `system.stats` placeholder. Keep `api.stats` and `subscriptions` false; advertise reflection capabilities only when their implementations are ready. -- [ ] Publish runtime version metadata through main-thread initialization/lifecycle code and handle late feature startup. -- [ ] Add the integration script's no-save mode for system methods, malformed messages, invalid IDs, unknown methods, incompatible majors/capabilities, and the statistics placeholder. +- [x] Add the separate API feature and startup-only configuration; preserve the existing feature's registration and state. +- [x] Implement request validation, ID tracking, fixed dispatch, response/error envelopes, capacity accounting, and serial sends. +- [x] Implement `system.ping`, `system.info`, and `system.validate` with the specified state-independent semantics. +- [x] Implement only the `system.stats` placeholder. Keep `api.stats` and `subscriptions` false; advertise reflection capabilities only when their implementations are ready. +- [x] Publish runtime version metadata through main-thread initialization/lifecycle code and handle late feature startup. +- [x] Add the integration script's no-save mode for system methods, malformed messages, invalid IDs, unknown methods, incompatible majors/capabilities, and the statistics placeholder. Exit condition: system methods work from local and LAN clients without authentication, including at the main menu. Version fields match the running game after preload. Startup/stop failures do not interrupt other features. Invalid requests produce the documented response or transport closure. ### Phase 2: main-thread queries and reflection -- [ ] Add the bounded synchronization-context pump and current-session admission state. -- [ ] Add the seven fixed root providers and readiness checks, including the menu-demo exclusion and paused-game allowance. -- [ ] Implement the shared member policy, exact getter allowlist, typed path traversal, null handling, and deterministic metadata listing. -- [ ] Implement scalar encoding, summaries, explicit projections, and array/list pages exactly as the API reference specifies. -- [ ] Attach `sessionId` and `gameTick` to game-data results; ensure returned snapshots contain no live game references. -- [ ] Extend the integration script with `-RequireGame` checks that discover roots, read `history.currentTech`, read `history.techQueue`, and inspect/read a research state using an ID obtained from the game. -- [ ] Leave one small runnable set of fixture-based checks for the production reflection/projection code, covering inherited private fields, blocked getters, null intermediates/terminals, dictionary key types, indexes, shallow cyclic references, 64-bit precision, and non-finite values. +- [x] Add the bounded synchronization-context pump and current-session admission state. +- [x] Add the seven fixed root providers and readiness checks, including the menu-demo exclusion and paused-game allowance. +- [x] Implement the shared member policy, exact getter allowlist, typed path traversal, null handling, and deterministic metadata listing. +- [x] Implement scalar encoding, summaries, explicit projections, and array/list pages exactly as the API reference specifies. +- [x] Attach `sessionId` and `gameTick` to game-data results; ensure returned snapshots contain no live game references. +- [x] Extend the integration script with `-RequireGame` checks that discover roots, read `history.currentTech`, read `history.techQueue`, and inspect/read a research state using an ID obtained from the game. +- [x] Leave one small runnable set of fixture-based checks for the production reflection/projection code, covering inherited private fields, blocked getters, null intermediates/terminals, dictionary key types, indexes, shallow cyclic references, 64-bit precision, and non-finite values. + +Fixture run command (repository root; copies `netstandard.dll` and Unity core modules from the installed game): + +```powershell +dotnet build LiveStreamAssist/tools/ReflectionReaderCheck/ReflectionReaderCheck.csproj -c Release +& LiveStreamAssist/tools/ReflectionReaderCheck/bin/Release/net472/ReflectionReaderCheck.exe +``` Keep fixture checks outside the shipped plugin and out of the remote root catalog. Use the smallest repository-compatible harness; no new testing framework or abstraction is required solely for these checks. Exercise the production reader, not a reimplementation of it, and record the exact run command here once its layout is chosen. @@ -222,24 +231,24 @@ Exit condition: returned research values match the in-game data, precision is pr ### Phase 3: lifecycle races, resource bounds, and recovery -- [ ] Verify a queued read cannot run against a different save after loading, unloading, or reloading a session. -- [ ] Verify timeout/disconnect/session-change races release admission capacity once and cannot emit late or duplicate success responses. -- [ ] Verify menu, loading, pause, space-travel root absence, and game-end behavior match the documented error distinctions. -- [ ] Exercise capacity and payload limits with multiple clients, fragmented oversized input, slow readers, and large projections/pages. Output must stay bounded during serialization, not merely fail after allocation. -- [ ] Verify an ordinary query still succeeds after malformed requests, busy responses, and other recoverable errors. -- [ ] Verify idempotent stop, released listener ports, ignored callbacks after disposal, and fail-soft behavior when another process owns the configured port. -- [ ] Regress Ctrl+F8 startup/stop, window closure, and the existing randomized tab switching. +- [ ] Verify a queued read cannot run against a different save after loading, unloading, or reloading a session. **Blocked: needs a running save.** +- [ ] Verify timeout/disconnect/session-change races release admission capacity once and cannot emit late or duplicate success responses. **Single completion path is implemented; in-game race verification blocked.** +- [ ] Verify menu, loading, pause, space-travel root absence, and game-end behavior match the documented error distinctions. **Blocked: needs DSP.** +- [ ] Exercise capacity and payload limits with multiple clients, fragmented oversized input, slow readers, and large projections/pages. Output must stay bounded during serialization, not merely fail after allocation. **Bounded send/receive/serialize is implemented; multi-client DSP run blocked.** +- [ ] Verify an ordinary query still succeeds after malformed requests, busy responses, and other recoverable errors. **Covered by Test-WebSocketApi.ps1; needs DSP.** +- [ ] Verify idempotent stop, released listener ports, ignored callbacks after disposal, and fail-soft behavior when another process owns the configured port. **Blocked: needs DSP / occupied-port check.** +- [ ] Regress Ctrl+F8 startup/stop, window closure, and the existing randomized tab switching. **Blocked: needs DSP.** Exit condition: the server remains usable after recoverable failures, old sessions cannot leak through queued work, and overload cannot create unbounded queues or main-thread network work. Report actual observations rather than asserting an unmeasured FPS or latency guarantee. ### Phase 4: packaging and final handoff -- [ ] Add the selected runtime dependencies to LiveStreamAssist's package, scoped to this project. -- [ ] Build, run the fixture checks and integration script, and complete the manual real-game/LAN acceptance matrix below. +- [x] Add the selected runtime dependencies to LiveStreamAssist's package, scoped to this project. +- [ ] Build, run the fixture checks and integration script, and complete the manual real-game/LAN acceptance matrix below. **Fixture checks passed; integration script and LAN matrix blocked without DSP.** - [ ] Inspect the resulting ZIP and test a clean install with only the manifest-declared dependencies and packaged runtime DLLs. Do not rely on DLLs provided incidentally by LuaScriptEngine or the development environment. -- [ ] Update the API reference and project README to describe verified implemented behavior. Remove the planned-only status only when the implementation is actually ready. -- [ ] Update this design document's current status, dependency selection, verified environment, and any remaining blockers. Keep `AGENTS.md` as a documentation pointer instead of copying this plan into it. -- [ ] Run `git diff --check` and review the final diff for unrelated changes. +- [x] Update the API reference and project README to describe verified implemented behavior. Remove the planned-only status only when the implementation is actually ready. +- [x] Update this design document's current status, dependency selection, verified environment, and any remaining blockers. Keep `AGENTS.md` as a documentation pointer instead of copying this plan into it. +- [x] Run `git diff --check` and review the final diff for unrelated changes. The existing `ZipMod` target does not gather dependency DLLs automatically. Prefer a project-local target with `BeforeTargets="ZipMod"` that adds the explicitly selected runtime DLLs to `_PackRootFiles`; the shared target will then stage them along with the plugin. Confirm the ordering and actual resolved filenames in the build output. Do not copy the entire output directory: that can include game references, Unity assemblies, BepInEx assemblies, and UXAssist itself. No shared packaging redesign is needed. diff --git a/LiveStreamAssist/tools/ReflectionReaderCheck/Copy-GameFacades.ps1 b/LiveStreamAssist/tools/ReflectionReaderCheck/Copy-GameFacades.ps1 new file mode 100644 index 0000000..34e78d5 --- /dev/null +++ b/LiveStreamAssist/tools/ReflectionReaderCheck/Copy-GameFacades.ps1 @@ -0,0 +1,44 @@ +param( + [Parameter(Mandatory)] + [string] $TargetDir +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' +$TargetDir = $TargetDir.Trim().Trim('"').TrimEnd('\', '/') + +$steamPath = $null +foreach ($regPath in @( + 'HKCU:\Software\Valve\Steam', + 'HKLM:\Software\Valve\Steam', + 'HKLM:\Software\Wow6432Node\Valve\Steam' +)) { + try { + $val = (Get-ItemProperty -LiteralPath $regPath -Name SteamPath -ErrorAction Stop).SteamPath + if ($val -and (Test-Path $val)) { $steamPath = $val; break } + } catch { } +} + +if (-not $steamPath) { throw 'Steam installation not found; cannot copy game facades for ReflectionReaderCheck.' } + +$vdfPath = Join-Path $steamPath 'steamapps\libraryfolders.vdf' +$vdfContent = Get-Content $vdfPath -Raw -Encoding UTF8 +$blockPattern = '"(?:\d+)"\s*\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}' +$managed = $null +foreach ($block in [regex]::Matches($vdfContent, $blockPattern, 'Singleline')) { + $text = $block.Groups[1].Value + if ($text -notmatch '"1366540"\s+"[^"]+"') { continue } + $pathMatch = [regex]::Match($text, '"path"\s+"([^"]+)"') + if (-not $pathMatch.Success) { continue } + $libPath = $pathMatch.Groups[1].Value -replace '\\\\', '\' -replace '/', '\' + $candidate = Join-Path $libPath 'steamapps\common\Dyson Sphere Program\DSPGAME_Data\Managed' + if (Test-Path $candidate) { $managed = $candidate; break } +} + +if (-not $managed) { throw 'DSP Managed directory not found; cannot copy game facades for ReflectionReaderCheck.' } + +foreach ($name in @('netstandard.dll', 'UnityEngine.CoreModule.dll', 'UnityEngine.SharedInternalsModule.dll')) { + $src = Join-Path $managed $name + if (-not (Test-Path $src)) { throw "Missing $src" } + Copy-Item -LiteralPath $src -Destination (Join-Path $TargetDir $name) -Force +} diff --git a/LiveStreamAssist/tools/ReflectionReaderCheck/Program.cs b/LiveStreamAssist/tools/ReflectionReaderCheck/Program.cs new file mode 100644 index 0000000..cb996ef --- /dev/null +++ b/LiveStreamAssist/tools/ReflectionReaderCheck/Program.cs @@ -0,0 +1,210 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using LiveStreamAssist.Api; +using UnityEngine; + +namespace ReflectionReaderCheck; + +static class Program +{ + static int _failed; + + static int Main() + { + var reader = new ReflectionReader(typeof(Program).Assembly); + try + { + InheritedPrivateFields(reader); + BlockedGetter(reader); + NullIntermediateAndTerminal(reader); + DictionaryKeys(reader); + Indexes(reader); + ShallowCycle(reader); + Int64Precision(reader); + NonFinite(reader); + UnityAndStreamBlocked(reader); + } + finally + { + reader.ClearCache(); + } + + if (_failed > 0) + { + Console.Error.WriteLine($"FAILED {_failed} check(s)"); + return 1; + } + + Console.WriteLine("OK"); + return 0; + } + + static void InheritedPrivateFields(ReflectionReader reader) + { + var obj = new DerivedFixture { Visible = 3 }; + obj.SetBaseSecret(9); + var result = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("baseSecret") }, default); + Check(result.Error == null, "inherited private field readable"); + Check(Equals(result.Encoded, 9), "inherited private field value"); + var desc = reader.Describe(obj, typeof(DerivedFixture), Array.Empty()); + Check(HasMember(desc, "baseSecret"), "describe lists inherited private field"); + Check(HasMember(desc, "Visible"), "describe lists derived field"); + } + + static void BlockedGetter(ReflectionReader reader) + { + var obj = new DerivedFixture(); + var read = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Boom") }, default); + Check(read.Error != null && read.Error.Kind == "MEMBER_NOT_ALLOWED", "blocked getter is MEMBER_NOT_ALLOWED"); + var backing = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("k__BackingField") }, default); + Check(backing.Error != null && backing.Error.Kind == "MEMBER_NOT_FOUND", "backing field not exposed"); + var desc = reader.Describe(obj, typeof(DerivedFixture), Array.Empty()); + Check(!HasMember(desc, "Boom"), "blocked getter omitted from describe"); + } + + static void NullIntermediateAndTerminal(ReflectionReader reader) + { + var obj = new DerivedFixture(); + var terminal = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Child") }, default); + Check(terminal.Error == null && terminal.IsNull, "null terminal succeeds"); + var mid = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Child"), new PathSeg("Visible") }, default); + Check(mid.Error != null && mid.Error.Kind == "NULL_PATH", "null intermediate is NULL_PATH"); + var desc = reader.Describe(obj, typeof(DerivedFixture), new[] { new PathSeg("Child") }); + Check(desc.Error == null && desc.IsNull, "describe null terminal"); + Check(desc.TypeName == "ReflectionReaderCheck.DerivedFixture", "null uses declared type"); + } + + static void DictionaryKeys(ReflectionReader reader) + { + var obj = new DerivedFixture(); + obj.IntDict[1001] = 4; + obj.StrDict["a"] = 5; + var ok = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("IntDict"), new PathSeg((object)1001) }, default); + Check(ok.Error == null && Equals(ok.Encoded, 4), "int dictionary key"); + var mismatch = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("IntDict"), new PathSeg((object)"1001") }, default); + Check(mismatch.Error != null && mismatch.Error.Kind == "INVALID_PARAMS", "string key not converted to int"); + var missing = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("IntDict"), new PathSeg((object)9) }, default); + Check(missing.Error != null && missing.Error.Kind == "KEY_NOT_FOUND", "missing int key"); + var str = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("StrDict"), new PathSeg((object)"a") }, default); + Check(str.Error == null && Equals(str.Encoded, 5), "string dictionary key"); + var summary = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("IntDict") }, default); + Check(summary.Error == null && summary.Encoded is Dictionary d && Equals(d["$kind"], "dictionary"), + "dictionary terminal is summary"); + } + + static void Indexes(ReflectionReader reader) + { + var obj = new DerivedFixture { Arr = new[] { 10, 20, 30 }, List = new List { 7, 8 } }; + var a0 = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Arr"), new PathSeg(0) }, default); + Check(a0.Error == null && Equals(a0.Encoded, 10), "array index 0"); + var oob = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Arr"), new PathSeg(3) }, default); + Check(oob.Error != null && oob.Error.Kind == "INDEX_OUT_OF_RANGE", "array oob"); + var page = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Arr") }, + new ReadOptions { HasOffset = true, Offset = 1, HasLimit = true, Limit = 1 }); + Check(page.Error == null, "array page"); + var dict = (Dictionary)page.Encoded; + var items = (List)dict["items"]; + Check(items.Count == 1 && Equals(items[0], 20) && Equals(dict["totalCount"], 3) && Equals(dict["hasMore"], true), + "array page contents"); + var empty = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("List") }, + new ReadOptions { HasOffset = true, Offset = 8, HasLimit = true, Limit = 2 }); + var emptyDict = (Dictionary)empty.Encoded; + Check(((List)emptyDict["items"]).Count == 0 && Equals(emptyDict["hasMore"], false) && Equals(emptyDict["offset"], 8), + "offset past end is empty page"); + } + + static void ShallowCycle(ReflectionReader reader) + { + var obj = new DerivedFixture(); + obj.Cycle = obj; + var summary = reader.Read(obj, typeof(DerivedFixture), Array.Empty(), default); + Check(summary.Error == null && summary.Encoded is Dictionary d && Equals(d["$kind"], "object"), + "cyclic object without select is summary"); + var hop = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Cycle"), new PathSeg("Cycle") }, default); + Check(hop.Error == null && hop.Encoded is Dictionary d2 && Equals(d2["$kind"], "object"), + "explicit cyclic path stays a summary"); + } + + static void Int64Precision(ReflectionReader reader) + { + var obj = new DerivedFixture { Big = 9007199254740993L, BigU = 18446744073709551615UL }; + var signed = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Big") }, default); + Check(Equals(signed.Encoded, "9007199254740993"), "int64 beyond 2^53 is decimal string"); + var unsigned = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("BigU") }, default); + Check(Equals(unsigned.Encoded, ulong.MaxValue.ToString(CultureInfo.InvariantCulture)), "uint64 is decimal string"); + } + + static void NonFinite(ReflectionReader reader) + { + var obj = new DerivedFixture { Nan = double.NaN, Inf = double.PositiveInfinity }; + var nan = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Nan") }, default); + Check(nan.Error != null && nan.Error.Kind == "UNSUPPORTED_VALUE", "NaN is UNSUPPORTED_VALUE"); + var inf = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Inf") }, default); + Check(inf.Error != null && inf.Error.Kind == "UNSUPPORTED_VALUE", "Infinity is UNSUPPORTED_VALUE"); + } + + static void UnityAndStreamBlocked(ReflectionReader reader) + { + var obj = new DerivedFixture(); + var unity = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Texture") }, default); + Check(unity.Error != null && unity.Error.Kind == "MEMBER_NOT_ALLOWED", "UnityEngine.Object field blocked"); + var stream = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Buffer") }, default); + Check(stream.Error != null && stream.Error.Kind == "MEMBER_NOT_ALLOWED", "Stream field blocked"); + var desc = reader.Describe(obj, typeof(DerivedFixture), Array.Empty()); + Check(!HasMember(desc, "Texture") && !HasMember(desc, "Buffer"), "infrastructure omitted from describe"); + var vec = reader.Read(obj, typeof(DerivedFixture), new[] { new PathSeg("Vec") }, + new ReadOptions { HasSelect = true, Select = new[] { "x" } }); + Check(vec.Error == null, "Unity vector fields readable"); + } + + static bool HasMember(WalkResult desc, string name) + { + if (desc.Members == null) return false; + foreach (var m in desc.Members) + { + if (m.Name == name) return true; + } + + return false; + } + + static void Check(bool cond, string name) + { + if (cond) + { + Console.WriteLine("ok " + name); + return; + } + + _failed++; + Console.Error.WriteLine("fail " + name); + } +} + +class BaseFixture +{ + int baseSecret; + + public void SetBaseSecret(int value) => baseSecret = value; +} + +class DerivedFixture : BaseFixture +{ + public int Visible; + public DerivedFixture Child = null; + public DerivedFixture Cycle = null; + public long Big; + public ulong BigU; + public double Nan; + public double Inf; + public Dictionary IntDict = new Dictionary(); + public Dictionary StrDict = new Dictionary(); + public int[] Arr; + public List List; + public Texture Texture = null; + public MemoryStream Buffer = new MemoryStream(); + public Vector3 Vec = new Vector3(1f, 2f, 3f); + public int Boom => throw new InvalidOperationException("getter must not run"); +} diff --git a/LiveStreamAssist/tools/ReflectionReaderCheck/ReflectionReaderCheck.csproj b/LiveStreamAssist/tools/ReflectionReaderCheck/ReflectionReaderCheck.csproj new file mode 100644 index 0000000..68d2b77 --- /dev/null +++ b/LiveStreamAssist/tools/ReflectionReaderCheck/ReflectionReaderCheck.csproj @@ -0,0 +1,16 @@ + + + + Exe + ReflectionReaderCheck + ReflectionReaderCheck + org.soardev.livestreamassist.reflectioncheck + none + + + + + + + + diff --git a/LiveStreamAssist/tools/Test-WebSocketApi.ps1 b/LiveStreamAssist/tools/Test-WebSocketApi.ps1 new file mode 100644 index 0000000..edb81db --- /dev/null +++ b/LiveStreamAssist/tools/Test-WebSocketApi.ps1 @@ -0,0 +1,223 @@ +#Requires -Version 7.0 +param( + [Parameter(Mandatory)] + [string] $ServerUri, + [switch] $RequireGame, + [int] $TimeoutSec = 8 +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' +$failed = 0 +$script:seq = 0 +$utf8 = [Text.UTF8Encoding]::new($false) + +function New-Id { + $script:seq++ + "t-$script:seq" +} + +function Connect-Api { + param([string] $Uri) + $ws = [Net.WebSockets.ClientWebSocket]::new() + $cts = [Threading.CancellationTokenSource]::new([TimeSpan]::FromSeconds($TimeoutSec)) + $ws.ConnectAsync([Uri]$Uri, $cts.Token).GetAwaiter().GetResult() + return @{ Ws = $ws; Cts = $cts } +} + +function Close-Api { + param($Conn) + if (-not $Conn) { return } + try { + if ($Conn.Ws.State -eq [Net.WebSockets.WebSocketState]::Open) { + $Conn.Ws.CloseAsync([Net.WebSockets.WebSocketCloseStatus]::NormalClosure, '', $Conn.Cts.Token).GetAwaiter().GetResult() + } + } catch { } + try { $Conn.Ws.Dispose() } catch { } + try { $Conn.Cts.Dispose() } catch { } +} + +function Send-Text { + param($Conn, [string] $Text, [bool] $End = $true) + $bytes = $utf8.GetBytes($Text) + $seg = [ArraySegment[byte]]::new($bytes) + $Conn.Ws.SendAsync($seg, [Net.WebSockets.WebSocketMessageType]::Text, $End, $Conn.Cts.Token).GetAwaiter().GetResult() +} + +function Receive-Message { + param($Conn) + $buffer = [byte[]]::new(65536) + $ms = [IO.MemoryStream]::new() + do { + $seg = [ArraySegment[byte]]::new($buffer) + $result = $Conn.Ws.ReceiveAsync($seg, $Conn.Cts.Token).GetAwaiter().GetResult() + if ($result.MessageType -eq [Net.WebSockets.WebSocketMessageType]::Close) { + return @{ Closed = $true; CloseStatus = [int]$Conn.Ws.CloseStatus; Text = $null } + } + $ms.Write($buffer, 0, $result.Count) + } while (-not $result.EndOfMessage) + return @{ Closed = $false; Text = $utf8.GetString($ms.ToArray()) } +} + +function Invoke-Rpc { + param($Conn, [string] $Method, $Params = @{}, [string] $Id = $(New-Id)) + $body = [ordered]@{ jsonrpc = '2.0'; id = $Id; method = $Method; params = $Params } + Send-Text $Conn ($body | ConvertTo-Json -Compress -Depth 16) + $msg = Receive-Message $Conn + if ($msg.Closed) { throw "Connection closed while waiting for $Method (close $($msg.CloseStatus))" } + return ($msg.Text | ConvertFrom-Json) +} + +function Assert-True { + param([bool] $Cond, [string] $Name) + if ($Cond) { Write-Host "ok $Name" } + else { Write-Host "fail $Name"; $script:failed++ } +} + +function Assert-Kind { + param($Resp, [string] $Kind, [string] $Name) + $actual = $null + if ($Resp.error -and $Resp.error.data) { $actual = [string]$Resp.error.data.kind } + Assert-True ($actual -eq $Kind) "$Name (kind=$actual)" +} + +$conn = $null +try { + $conn = Connect-Api $ServerUri + + $ping = Invoke-Rpc $conn 'system.ping' + Assert-True ($null -ne $ping.result.serverTimeUtc) 'system.ping' + + $info = Invoke-Rpc $conn 'system.info' + Assert-True ($info.result.apiVersion -eq '1.0.0') 'system.info apiVersion' + Assert-True ($info.result.limits.maxRequestBytes -eq 65536) 'system.info limits' + Assert-True ($info.result.capabilities.'api.stats' -eq $false) 'api.stats false' + Assert-True ($info.result.capabilities.subscriptions -eq $false) 'subscriptions false' + Assert-True ($info.result.PSObject.Properties.Name -contains 'gameVersionReady') 'gameVersionReady present' + + $ok = Invoke-Rpc $conn 'system.validate' @{ apiMajor = 1; requiredCapabilities = @('reflection.read') } + Assert-True ($ok.result.compatible -eq $true) 'validate compatible' + + $statsCap = Invoke-Rpc $conn 'system.validate' @{ apiMajor = 1; requiredCapabilities = @('api.stats') } + Assert-True ($statsCap.result.compatible -eq $false -and $statsCap.result.missingCapabilities[0] -eq 'api.stats') 'validate missing api.stats' + + $major = Invoke-Rpc $conn 'system.validate' @{ apiMajor = 2 } + Assert-True ($major.result.compatible -eq $false -and $major.result.apiMajorCompatible -eq $false) 'validate major mismatch' + + $stats = Invoke-Rpc $conn 'system.stats' + Assert-True ($stats.result.available -eq $false -and $null -eq $stats.result.metrics) 'system.stats placeholder' + + Send-Text $conn '{not-json' + $parse = (Receive-Message $conn).Text | ConvertFrom-Json + Assert-Kind $parse 'PARSE_ERROR' 'malformed JSON' + + Send-Text $conn '{"jsonrpc":"2.0","method":"system.ping","params":{}}' + $noId = (Receive-Message $conn).Text | ConvertFrom-Json + Assert-Kind $noId 'INVALID_REQUEST' 'missing id' + + Send-Text $conn '{"jsonrpc":"2.0","id":1,"method":"system.ping","params":{}}' + $numId = (Receive-Message $conn).Text | ConvertFrom-Json + Assert-Kind $numId 'INVALID_REQUEST' 'numeric id' + + $unknown = Invoke-Rpc $conn 'system.nope' + Assert-Kind $unknown 'METHOD_NOT_FOUND' 'unknown method' + + $extra = Invoke-Rpc $conn 'system.ping' @{ foo = 1 } + Assert-Kind $extra 'INVALID_PARAMS' 'unknown ping param' + + Send-Text $conn '[{"jsonrpc":"2.0","id":"b","method":"system.ping"}]' + $batch = (Receive-Message $conn).Text | ConvertFrom-Json + Assert-Kind $batch 'INVALID_REQUEST' 'batch array' + + Send-Text $conn '{"jsonrpc":"2.0","id":"dup","method":"data.read","params":{"root":"history","path":["currentTech"]}}' + Send-Text $conn '{"jsonrpc":"2.0","id":"dup","method":"data.read","params":{"root":"history","path":["currentTech"]}}' + $dup = Receive-Message $conn + if ($dup.Closed) { + Assert-True ($dup.CloseStatus -eq 1008) "duplicate outstanding id close $($dup.CloseStatus)" + } else { + Assert-True $false 'duplicate outstanding id should close' + } + + Close-Api $conn + $conn = Connect-Api $ServerUri + $ping2 = Invoke-Rpc $conn 'system.ping' + Assert-True ($null -ne $ping2.result.serverTimeUtc) 'recover after duplicate-id close' + + $conn.Ws.SendAsync( + [ArraySegment[byte]]::new([byte[]](1, 2, 3)), + [Net.WebSockets.WebSocketMessageType]::Binary, + $true, + $conn.Cts.Token).GetAwaiter().GetResult() + $bin = Receive-Message $conn + Assert-True ($bin.Closed -and $bin.CloseStatus -eq 1003) "binary rejected $($bin.CloseStatus)" + + Close-Api $conn + $conn = Connect-Api $ServerUri + $big = 'x' * 70000 + Send-Text $conn $big + $over = Receive-Message $conn + Assert-True ($over.Closed -and $over.CloseStatus -eq 1009) "oversized close $($over.CloseStatus)" + + Close-Api $conn + $uri = [Uri]$ServerUri + $badPath = Connect-Api "$($uri.Scheme)://$($uri.Authority)/nope" + $bad = Receive-Message $badPath + Assert-True ($bad.Closed -and $bad.CloseStatus -eq 1008) "wrong path close $($bad.CloseStatus)" + Close-Api $badPath + + $conn = Connect-Api $ServerUri + $part1 = '{"jsonrpc":"2.0","id":"frag","method":' + $part2 = '"system.ping","params":{}}' + Send-Text $conn $part1 $false + Send-Text $conn $part2 + $frag = (Receive-Message $conn).Text | ConvertFrom-Json + Assert-True ($null -ne $frag.result.serverTimeUtc) 'fragmented text message' + + $unknownRoot = Invoke-Rpc $conn 'data.read' @{ root = 'no-such-root' } + Assert-Kind $unknownRoot 'ROOT_NOT_FOUND' 'unknown root' + + if ($RequireGame) { + $roots = Invoke-Rpc $conn 'data.roots' + Assert-True ($roots.result.gameReady -eq $true) 'data.roots gameReady' + Assert-True ($null -ne $roots.result.sessionId) 'sessionId' + $tech = Invoke-Rpc $conn 'data.read' @{ root = 'history'; path = @('currentTech') } + Assert-True ($null -eq $tech.error) 'read history.currentTech' + $queue = Invoke-Rpc $conn 'data.read' @{ root = 'history'; path = @('techQueue'); offset = 0; limit = 8 } + Assert-True ($null -eq $queue.error) 'read history.techQueue' + $techId = [int]$tech.result.value + if ($techId -eq 0 -and $queue.result.value.items) { + foreach ($item in @($queue.result.value.items)) { + if ([int]$item -ne 0) { $techId = [int]$item; break } + } + } + if ($techId -eq 0) { + throw 'No research ID from currentTech or techQueue' + } + $state = Invoke-Rpc $conn 'data.read' @{ + root = 'history' + path = @('techStates', @{ key = $techId }) + select = @('curLevel', 'hashUploaded', 'hashNeeded') + } + Assert-True ($null -eq $state.error) "read techStates[$techId]" + Assert-True ($null -ne $state.result.value.curLevel) 'research curLevel' + } else { + $roots = Invoke-Rpc $conn 'data.roots' + Assert-True ($roots.result.PSObject.Properties.Name -contains 'gameReady') 'data.roots without save' + if ($roots.result.gameReady -eq $true) { + Write-Host 'note game is ready; -RequireGame would exercise research reads' + } + } + + $still = Invoke-Rpc $conn 'system.ping' + Assert-True ($null -ne $still.result.serverTimeUtc) 'ping after protocol errors' +} +finally { + Close-Api $conn +} + +if ($failed -gt 0) { + Write-Error "FAILED $failed assertion(s)" + exit 1 +} +Write-Host 'OK' +exit 0