From 5e6d74dfee3343e1f58efaaad9aa146acbf809b5 Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 2 Jul 2026 23:09:49 +0900 Subject: [PATCH] =?UTF-8?q?=20=E3=82=B7=E3=83=BC=E3=83=B3=E5=8D=98?= =?UTF-8?q?=E4=BD=8D=E3=81=A7=E5=80=A4=E3=82=92=E5=85=B1=E6=9C=89=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=B9=E3=83=88=E3=82=A2=20SceneSharedValues=20?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript/SceneSharedValues.cs | 101 +++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 LuaScript/SceneSharedValues.cs diff --git a/LuaScript/SceneSharedValues.cs b/LuaScript/SceneSharedValues.cs new file mode 100644 index 0000000..85c66dd --- /dev/null +++ b/LuaScript/SceneSharedValues.cs @@ -0,0 +1,101 @@ +using System.Collections.Concurrent; +using System.Text; + +namespace LuaScript +{ + internal enum SceneValueKind + { + Nil = 0, + Number = 1, + String = 2, + Boolean = 3, + } + + internal readonly record struct SceneValue(SceneValueKind Kind, double Number, string? Text) + { + public static readonly SceneValue Nil = new(SceneValueKind.Nil, 0d, null); + + public static SceneValue FromNumber(double value) => new(SceneValueKind.Number, value, null); + + public static SceneValue FromBoolean(bool value) => new(SceneValueKind.Boolean, value ? 1d : 0d, null); + + public static SceneValue FromString(string value) => new(SceneValueKind.String, 0d, value); + } + + internal readonly record struct SceneValueQuery(string Name, SceneValue Result); + + internal sealed class SceneSharedValues + { + public const int MaxNameBytes = NativeTagCapacity - 1; + public const int MaxTextBytes = NativeTagCapacity - 1; + + private const int NativeTagCapacity = 4096; + + private static readonly ConcurrentDictionary s_scenes = new(StringComparer.Ordinal); + + private readonly Dictionary _values = new(StringComparer.Ordinal); + private readonly object _gate = new(); + + public static SceneSharedValues ForScene(string sceneId) => + s_scenes.GetOrAdd(sceneId, static _ => new SceneSharedValues()); + + public void Set(string name, SceneValue value) + { + name = TruncateUtf8(name, MaxNameBytes); + if (value.Kind == SceneValueKind.String) + value = SceneValue.FromString(TruncateUtf8(value.Text ?? string.Empty, MaxTextBytes)); + + lock (_gate) + { + if (value.Kind == SceneValueKind.Nil) + _values.Remove(name); + else + _values[name] = value; + } + } + + public SceneValue Get(string name) + { + name = TruncateUtf8(name, MaxNameBytes); + lock (_gate) + return _values.TryGetValue(name, out var value) ? value : SceneValue.Nil; + } + + private static string TruncateUtf8(string text, int maxBytes) + { + if (text.Length <= maxBytes / 3 || Encoding.UTF8.GetByteCount(text) <= maxBytes) + return text; + + int bytes = 0; + int length = 0; + while (length < text.Length) + { + char c = text[length]; + int size; + int step = 1; + if (char.IsHighSurrogate(c) && length + 1 < text.Length && char.IsLowSurrogate(text[length + 1])) + { + size = 4; + step = 2; + } + else if (c < 0x80) + { + size = 1; + } + else if (c < 0x800) + { + size = 2; + } + else + { + size = 3; + } + if (bytes + size > maxBytes) + break; + bytes += size; + length += step; + } + return text[..length]; + } + } +}