Files
Jiaqi Liu 5d0f564a00 Add a standalone battle editor and battle system for PAL3 and PAL3A (#99)
* build: migrate C# formatting and add offscreen editor test support

Host IMGUI tests on a detached panel without creating a native editor window.

* feat(core): decode native battle records, geometry and animation metadata

Read the two game variants’ battle tables and model data, with binary-reader and original-resource coverage.

* feat(battle): implement deterministic combat rules and action settlement

Implement actors, inventory, formation, action timing, status interruption, AI, stage ownership and deterministic replay. Keep the domain and its regression suite independent of Unity.

* feat(battle): project native HUD state and input commands

Add the presentation assembly and tests for command routing, lineup selection, floating numbers and stage messages.

* feat(rendering): add battle lighting, actor appearance and native shadows

Support baked and toon lighting, actor and weapon rendering, display refresh rate and resource ownership.

* feat(effects): drive battle effects through shared resource previews

Bind battle control nodes to action owners. Share preview lighting, geometry and effect resources with the existing Unity editors.

* refactor(game): isolate story battle targeting and actor lifetimes

Preserve scene ownership, bound story battle targeting and stop debug attacks when their actor leaves the scene.

* feat(editor): compile original game data into battle scenarios

Build per-variant animation and ability catalogs, encounter music, scene lighting profiles, random parties, equipment and test inventory from the original resources.

* feat(editor): play battle sessions with animated previews and synced media

Drive the preview scene, camera, effects, hit feedback, result choreography, music and movies from simulation snapshots and events.

* feat(editor): add the battle workbench, native HUD and input controls

Expose scenario authoring, replay, diagnostics, responsive command menus and mouse, touch and gamepad targeting. Include the editor workflow and HUD integration tests.
2026-11-21 22:28:47 +08:00

87 lines
4.0 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
namespace Pal3.Tools.CodeStyle;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
internal readonly record struct ByteRange(int Start, int End);
internal static class ProtectedText
{
internal static readonly UTF8Encoding Utf8 = new(false, true);
internal static IReadOnlyList<ByteRange> Capture(byte[] source)
{
string text = Utf8.GetString(source);
var ranges = new List<ByteRange>();
var inactive = new List<ByteRange>[2];
int context = 0;
foreach (string variant in new[] { "PAL3", "PAL3A" })
{
SyntaxNode root = CSharpSyntaxTree.ParseText(text,
new CSharpParseOptions(LanguageVersion.Preview, preprocessorSymbols: [variant, "UNITY_EDITOR"])).GetRoot();
inactive[context++] = root.DescendantTrivia(descendIntoTrivia: true)
.Where(trivia => trivia.IsKind(SyntaxKind.DisabledTextTrivia))
.Select(trivia => new ByteRange(trivia.SpanStart, trivia.Span.End)).ToList();
foreach (SyntaxNode node in root.DescendantNodes())
{
switch (node)
{
case InterpolatedStringExpressionSyntax interpolation:
ranges.Add(new(interpolation.StringStartToken.SpanStart, interpolation.StringStartToken.Span.End));
ranges.Add(new(interpolation.StringEndToken.SpanStart, interpolation.StringEndToken.Span.End));
break;
case InterpolatedStringTextSyntax part:
ranges.Add(new(part.SpanStart, part.Span.End));
break;
case InterpolationFormatClauseSyntax format:
ranges.Add(new(format.FormatStringToken.SpanStart, format.FormatStringToken.Span.End));
break;
}
}
foreach (SyntaxToken token in root.DescendantTokens(descendIntoTrivia: true))
{
if (token.Kind() is SyntaxKind.StringLiteralToken or SyntaxKind.CharacterLiteralToken or
SyntaxKind.Utf8StringLiteralToken or SyntaxKind.SingleLineRawStringLiteralToken or
SyntaxKind.MultiLineRawStringLiteralToken or SyntaxKind.Utf8SingleLineRawStringLiteralToken or
SyntaxKind.Utf8MultiLineRawStringLiteralToken)
ranges.Add(new(token.SpanStart, token.Span.End));
}
}
// Text inactive in both formatting contexts is opaque as well: its
// player-only literals must retain their exact original bytes.
for (int left = 0, right = 0; left < inactive[0].Count && right < inactive[1].Count;)
{
ByteRange first = inactive[0][left], second = inactive[1][right];
int start = Math.Max(first.Start, second.Start), end = Math.Min(first.End, second.End);
if (start < end)
ranges.Add(new(start, end));
if (first.End <= second.End)
left++;
else
right++;
}
var merged = new List<ByteRange>();
foreach (ByteRange range in ranges.OrderBy(range => range.Start).ThenByDescending(range => range.End))
{
if (range.Start == range.End)
continue;
if (merged.Count > 0 && range.Start < merged[^1].End)
merged[^1] = merged[^1] with
{
End = Math.Max(merged[^1].End, range.End)
};
else
merged.Add(range);
}
return merged.Select(range => new ByteRange(Utf8.GetByteCount(text.AsSpan(0, range.Start)),
Utf8.GetByteCount(text.AsSpan(0, range.End)))).ToArray();
}
}