Runtime のファイルをドメイン別フォルダへ整理
このコミットが含まれているのは:
26ファイル変更
検索条件に一致するファイルはありません
@@ -0,0 +1,7 @@
|
||||
namespace LuaScript
|
||||
{
|
||||
internal interface ISceneObjectResolver
|
||||
{
|
||||
bool TryResolve(string tag, int timelineFrame, out SceneObjectInfo info);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace LuaScript
|
||||
{
|
||||
internal readonly record struct SceneObjectInfo(
|
||||
string Tag,
|
||||
bool Exist,
|
||||
double X,
|
||||
double Y,
|
||||
double Z,
|
||||
double Zoom,
|
||||
double Rz,
|
||||
double Alpha,
|
||||
int Layer);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace LuaScript
|
||||
{
|
||||
internal readonly record struct SceneObjectQuery(
|
||||
string Tag,
|
||||
int Frame,
|
||||
SceneObjectInfo? Result);
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Runtime.CompilerServices;
|
||||
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 = 4095;
|
||||
public const int MaxTextBytes = 4095;
|
||||
public const int MaxEntries = 4096;
|
||||
public const long MaxHistoryCostBytes = 16 * 1024 * 1024;
|
||||
|
||||
private const long VersionBaseCost = 64;
|
||||
|
||||
private readonly record struct SceneVersion(long Frame, int Layer, SceneValue Value);
|
||||
|
||||
private static readonly ConditionalWeakTable<object, ConcurrentDictionary<string, SceneSharedValues>> s_scopes = new();
|
||||
|
||||
private readonly Dictionary<string, List<SceneVersion>> _names = new(StringComparer.Ordinal);
|
||||
private readonly object _gate = new();
|
||||
private long _historyCost;
|
||||
|
||||
public static SceneSharedValues ForScene(object scope, string sceneId) =>
|
||||
s_scopes.GetOrCreateValue(scope).GetOrAdd(sceneId, static _ => new SceneSharedValues());
|
||||
|
||||
public SceneValue Read(long generation, string name)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (!_names.TryGetValue(name, out var versions))
|
||||
return SceneValue.Nil;
|
||||
int index = LatestBefore(versions, generation);
|
||||
return index >= 0 ? versions[index].Value : SceneValue.Nil;
|
||||
}
|
||||
}
|
||||
|
||||
public void Publish(long generation, int layer, Dictionary<string, SceneValue> writes)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
foreach (var pair in writes)
|
||||
Upsert(generation, layer, pair.Key, pair.Value);
|
||||
EnforceHistoryBudget();
|
||||
}
|
||||
}
|
||||
|
||||
private void Upsert(long generation, int layer, string name, SceneValue value)
|
||||
{
|
||||
if (!_names.TryGetValue(name, out var versions))
|
||||
{
|
||||
if (_names.Count >= MaxEntries)
|
||||
return;
|
||||
if (value == SceneValue.Nil)
|
||||
return;
|
||||
_names[name] = [new SceneVersion(generation, layer, value)];
|
||||
return;
|
||||
}
|
||||
|
||||
int index = LatestBefore(versions, generation + 1);
|
||||
if (index >= 0 && versions[index].Frame == generation)
|
||||
{
|
||||
if (layer < versions[index].Layer)
|
||||
return;
|
||||
if (index < versions.Count - 1)
|
||||
_historyCost += CostOf(value) - CostOf(versions[index].Value);
|
||||
versions[index] = new SceneVersion(generation, layer, value);
|
||||
return;
|
||||
}
|
||||
|
||||
var previous = index >= 0 ? versions[index].Value : SceneValue.Nil;
|
||||
if (value == previous)
|
||||
return;
|
||||
|
||||
versions.Insert(index + 1, new SceneVersion(generation, layer, value));
|
||||
if (index + 1 < versions.Count - 1)
|
||||
_historyCost += CostOf(value);
|
||||
else if (versions.Count > 1)
|
||||
_historyCost += CostOf(versions[^2].Value);
|
||||
}
|
||||
|
||||
private void EnforceHistoryBudget()
|
||||
{
|
||||
while (_historyCost > MaxHistoryCostBytes)
|
||||
{
|
||||
List<SceneVersion>? oldest = null;
|
||||
string oldestName = string.Empty;
|
||||
foreach (var pair in _names)
|
||||
{
|
||||
var versions = pair.Value;
|
||||
if (versions.Count < 2)
|
||||
continue;
|
||||
if (oldest is null ||
|
||||
versions[0].Frame < oldest[0].Frame ||
|
||||
(versions[0].Frame == oldest[0].Frame && string.CompareOrdinal(pair.Key, oldestName) < 0))
|
||||
{
|
||||
oldest = versions;
|
||||
oldestName = pair.Key;
|
||||
}
|
||||
}
|
||||
if (oldest is null)
|
||||
return;
|
||||
_historyCost -= CostOf(oldest[0].Value);
|
||||
oldest.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
private static long CostOf(SceneValue value) =>
|
||||
VersionBaseCost + (value.Text is null ? 0 : value.Text.Length * 2L);
|
||||
|
||||
private static int LatestBefore(List<SceneVersion> versions, long generation)
|
||||
{
|
||||
int low = 0;
|
||||
int high = versions.Count - 1;
|
||||
int result = -1;
|
||||
while (low <= high)
|
||||
{
|
||||
int mid = (low + high) >> 1;
|
||||
if (versions[mid].Frame < generation)
|
||||
{
|
||||
result = mid;
|
||||
low = mid + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string NormalizeName(string name) => TruncateUtf8(name, MaxNameBytes);
|
||||
|
||||
public static SceneValue NormalizeValue(SceneValue value) =>
|
||||
value.Kind == SceneValueKind.String
|
||||
? SceneValue.FromString(TruncateUtf8(value.Text ?? string.Empty, MaxTextBytes))
|
||||
: value;
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
namespace LuaScript
|
||||
{
|
||||
internal sealed class SceneValueSession
|
||||
{
|
||||
private readonly Dictionary<string, SceneValue> _writes = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<string, SceneValue> _reads = new(StringComparer.Ordinal);
|
||||
private readonly List<SceneValueQuery> _queries = [];
|
||||
|
||||
private SceneSharedValues? _store;
|
||||
private object _scope = new();
|
||||
private string _sceneId = string.Empty;
|
||||
private long _generation;
|
||||
private int _layer;
|
||||
|
||||
public bool HasWrites => _writes.Count > 0;
|
||||
|
||||
public IReadOnlyList<SceneValueQuery> Queries => _queries;
|
||||
|
||||
public void Begin(object scope, string sceneId, long generation, int layer)
|
||||
{
|
||||
if (!ReferenceEquals(_scope, scope) || !string.Equals(_sceneId, sceneId, StringComparison.Ordinal))
|
||||
{
|
||||
_store = null;
|
||||
_scope = scope;
|
||||
_sceneId = sceneId;
|
||||
}
|
||||
_generation = generation;
|
||||
_layer = layer;
|
||||
_writes.Clear();
|
||||
_reads.Clear();
|
||||
_queries.Clear();
|
||||
}
|
||||
|
||||
public SceneValue Get(string name)
|
||||
{
|
||||
name = SceneSharedValues.NormalizeName(name);
|
||||
if (_writes.TryGetValue(name, out var written))
|
||||
return written;
|
||||
if (_reads.TryGetValue(name, out var cached))
|
||||
return cached;
|
||||
var value = Store.Read(_generation, name);
|
||||
_reads[name] = value;
|
||||
_queries.Add(new SceneValueQuery(name, value));
|
||||
return value;
|
||||
}
|
||||
|
||||
public void Set(string name, SceneValue value)
|
||||
{
|
||||
name = SceneSharedValues.NormalizeName(name);
|
||||
value = SceneSharedValues.NormalizeValue(value);
|
||||
if (value.Kind != SceneValueKind.Nil &&
|
||||
!_writes.ContainsKey(name) &&
|
||||
_writes.Count >= SceneSharedValues.MaxEntries)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_writes[name] = value;
|
||||
}
|
||||
|
||||
public void Publish()
|
||||
{
|
||||
if (_writes.Count == 0)
|
||||
return;
|
||||
Store.Publish(_generation, _layer, _writes);
|
||||
}
|
||||
|
||||
private SceneSharedValues Store => _store ??= SceneSharedValues.ForScene(_scope, _sceneId);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする