Snapshot for release 2026.10.12.1 (internal 8d252bd)
This commit is contained in:
parent
504a6f080a
commit
3c4f093e3d
60 changed files with 5748 additions and 602 deletions
|
|
@ -4,10 +4,15 @@ using System.Text;
|
|||
namespace RynthCore.Plugin.RynthChat;
|
||||
|
||||
/// <summary>
|
||||
/// The newest 500 chat lines, exported to the engine as JSON
|
||||
/// The newest chat lines, exported to the engine as JSON
|
||||
/// (RynthChatGetScrollbackJson). Add runs on the game thread (chat dispatch),
|
||||
/// BuildJson on the engine's pump thread; the lock covers both.
|
||||
///
|
||||
/// How many: DefaultCapacity until the engine says otherwise
|
||||
/// (RynthChatSetCapacity, from the chat setting "Chat history"; 0.3.0). It was
|
||||
/// a fixed 500 before. Changing it keeps the newest lines and never touches the
|
||||
/// numbering: the engine (and its Discord relay) tell new lines from old by seq.
|
||||
///
|
||||
/// JSON per line: {"seq":N,"ts":"HH:mm:ss","chan":"Chat","type":3,"sender":"Bob"|null,"text":"..."}.
|
||||
/// "type" (AC's ChatMessageType) was added in 0.2.0; engines before it ignore it.
|
||||
/// Every non-ASCII character is written as a \uXXXX escape, so the export is
|
||||
|
|
@ -16,14 +21,55 @@ namespace RynthCore.Plugin.RynthChat;
|
|||
/// </summary>
|
||||
internal sealed class ChatBuffer
|
||||
{
|
||||
private const int Capacity = 500;
|
||||
/// <summary>The engine's default for "Chat history" (ChatHistory.DefaultLines there).</summary>
|
||||
internal const int DefaultCapacity = 2000;
|
||||
internal const int MinCapacity = 100;
|
||||
internal const int MaxCapacity = 10000;
|
||||
|
||||
private readonly ChatLine[] _ring = new ChatLine[Capacity];
|
||||
private readonly object _lock = new();
|
||||
private int _head; // next write index (mod Capacity)
|
||||
private int _count; // total items stored (≤ Capacity)
|
||||
private ChatLine?[] _ring;
|
||||
private readonly object _lock = new();
|
||||
private int _head; // next write index (mod _ring.Length)
|
||||
private int _count; // lines stored (<= _ring.Length)
|
||||
private ulong _nextSeq = 1;
|
||||
|
||||
internal ChatBuffer(int capacity = DefaultCapacity)
|
||||
{
|
||||
_ring = new ChatLine?[Math.Clamp(capacity, MinCapacity, MaxCapacity)];
|
||||
}
|
||||
|
||||
internal int Capacity
|
||||
{
|
||||
get { lock (_lock) return _ring.Length; }
|
||||
}
|
||||
|
||||
internal int Count
|
||||
{
|
||||
get { lock (_lock) return _count; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Keeps up to <paramref name="capacity"/> lines from now on (clamped to
|
||||
/// MinCapacity..MaxCapacity); when it shrinks, the oldest go. Returns the capacity
|
||||
/// applied. Any thread.
|
||||
/// </summary>
|
||||
internal int SetCapacity(int capacity)
|
||||
{
|
||||
capacity = Math.Clamp(capacity, MinCapacity, MaxCapacity);
|
||||
lock (_lock)
|
||||
{
|
||||
if (capacity == _ring.Length) return capacity;
|
||||
int keep = Math.Min(_count, capacity);
|
||||
var ring = new ChatLine?[capacity];
|
||||
int oldCap = _ring.Length;
|
||||
int start = (_head - keep + oldCap) % oldCap; // the oldest line kept
|
||||
for (int i = 0; i < keep; i++) ring[i] = _ring[(start + i) % oldCap];
|
||||
_ring = ring;
|
||||
_count = keep;
|
||||
_head = keep % capacity;
|
||||
return capacity;
|
||||
}
|
||||
}
|
||||
|
||||
internal void Add(string? text, int chatType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text)) return;
|
||||
|
|
@ -37,7 +83,7 @@ internal sealed class ChatBuffer
|
|||
{
|
||||
// Numbered under the lock, so the ring is always in seq order (the
|
||||
// engine skips anything at or below the last seq it has seen).
|
||||
_ring[_head % Capacity] = new ChatLine
|
||||
_ring[_head] = new ChatLine
|
||||
{
|
||||
Seq = _nextSeq++,
|
||||
Timestamp = ts,
|
||||
|
|
@ -46,33 +92,36 @@ internal sealed class ChatBuffer
|
|||
Sender = sender,
|
||||
Text = text,
|
||||
};
|
||||
_head = (_head + 1) % Capacity;
|
||||
if (_count < Capacity) _count++;
|
||||
_head = (_head + 1) % _ring.Length;
|
||||
if (_count < _ring.Length) _count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns JSON array of lines with Seq > sinceSeq, oldest-first.
|
||||
/// <summary>
|
||||
/// The lines with Seq > sinceSeq as a JSON array, oldest first. Only those are
|
||||
/// copied out under the lock: the ring holds consecutive seqs, so they are the
|
||||
/// newest (newest seq - sinceSeq) lines. Until 0.3.0 the whole ring was cloned
|
||||
/// and walked on every poll (10 a second), which grows with the history length.
|
||||
/// </summary>
|
||||
internal string BuildJson(ulong sinceSeq)
|
||||
{
|
||||
ChatLine[] snapshot;
|
||||
int count;
|
||||
int head;
|
||||
ChatLine?[] lines;
|
||||
lock (_lock)
|
||||
{
|
||||
snapshot = (ChatLine[])_ring.Clone();
|
||||
count = _count;
|
||||
head = _head;
|
||||
ulong newest = _nextSeq - 1;
|
||||
int n = sinceSeq >= newest ? 0 : (int)Math.Min((ulong)_count, newest - sinceSeq);
|
||||
if (n == 0) return "[]";
|
||||
lines = new ChatLine?[n];
|
||||
int cap = _ring.Length;
|
||||
int start = (_head - n + cap) % cap;
|
||||
for (int i = 0; i < n; i++) lines[i] = _ring[(start + i) % cap];
|
||||
}
|
||||
|
||||
var sb = new StringBuilder(256);
|
||||
var sb = new StringBuilder(64 + lines.Length * 192);
|
||||
sb.Append('[');
|
||||
bool first = true;
|
||||
|
||||
// Iterate oldest→newest. Oldest entry is at (head - count + Capacity) % Capacity.
|
||||
int start = (head - count + Capacity) % Capacity;
|
||||
for (int i = 0; i < count; i++)
|
||||
foreach (ChatLine? line in lines)
|
||||
{
|
||||
var line = snapshot[(start + i) % Capacity];
|
||||
if (line == null || line.Seq <= sinceSeq) continue;
|
||||
|
||||
if (!first) sb.Append(',');
|
||||
|
|
|
|||
|
|
@ -64,6 +64,18 @@ public static unsafe class PluginExports
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// How many lines the scrollback keeps (the engine's "Chat history" setting; 0.3.0).
|
||||
/// Returns the number applied (clamped to 100..10000), or 0 before the plugin is up.
|
||||
/// Engines before it never call this, and the plugin keeps ChatBuffer.DefaultCapacity.
|
||||
/// </summary>
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthChatSetCapacity", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static int SetCapacity(int lines)
|
||||
{
|
||||
try { return Runtime.Plugin?.SetScrollbackCapacity(lines) ?? 0; }
|
||||
catch { return 0; }
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(EntryPoint = "RynthChatSendLine", CallConvs = new[] { typeof(CallConvCdecl) })]
|
||||
public static void SendLine(IntPtr ansiText)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ namespace RynthCore.Plugin.RynthChat;
|
|||
public sealed class RynthChatPlugin : RynthPluginBase
|
||||
{
|
||||
internal static readonly IntPtr NamePointer = Marshal.StringToHGlobalAnsi("RynthChat");
|
||||
internal static readonly IntPtr VersionPointer = Marshal.StringToHGlobalAnsi("0.2.1");
|
||||
internal static readonly IntPtr VersionPointer = Marshal.StringToHGlobalAnsi("0.3.0");
|
||||
|
||||
private readonly ChatBuffer _buffer = new();
|
||||
|
||||
|
|
@ -42,6 +42,8 @@ public sealed class RynthChatPlugin : RynthPluginBase
|
|||
|
||||
internal string BuildScrollbackJson(ulong sinceSeq) => _buffer.BuildJson(sinceSeq);
|
||||
|
||||
internal int SetScrollbackCapacity(int lines) => _buffer.SetCapacity(lines);
|
||||
|
||||
// Called on the game thread from the engine's deferred chat dispatch
|
||||
// (Win32Backend posts WM_RYNTHCORE_CHAT so this runs on a fresh, non-reentrant
|
||||
// message dispatch rather than inline inside the Enter keystroke handler).
|
||||
|
|
|
|||
Loading…
Reference in a new issue