Files
Pal3.Unity/Assets/Scripts/Editor/SourceGeneratorEditorOptions.cs
Jiaqi (0x7c13) Liu e870cf487a Add core protocol regression tests and copyright checks
Cover SCE command contracts, binary readers, resource layouts and full local CPK decompression for PAL3 and PAL3A. Fix CVD time rebasing, binary read bounds and short reads, and CPK/NAV validation.

Standardize first-party copyright headers, preserve correct creation years, and update console source generation. Validate both variants in Unity and verify all live CPK entries.
2026-11-12 13:37:27 +08:00

92 lines
3.7 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
namespace Editor
{
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Pal3.Core.Command;
using SourceGenerator;
using SourceGenerator.Base;
using UnityEditor;
using UnityEngine;
public static class SourceGeneratorEditorOptions
{
private const string NameSpace = "Pal3.Game.Command";
private const string ClassName = "ConsoleCommands";
private const string OutputFolderPath = "Assets/Scripts/Pal3.Game/Command/";
#if PAL3
private const string GAME_VARIANT_SYMBOL = "PAL3";
private const string OutputFileName = "ConsoleCommands.PAL3.g.cs";
#elif PAL3A
private const string GAME_VARIANT_SYMBOL = "PAL3A";
private const string OutputFileName = "ConsoleCommands.PAL3A.g.cs";
#endif
#if PAL3
[MenuItem("PAL3/Source Generator/Generate ConsoleCommands.PAL3.g.cs", priority = 2)]
#elif PAL3A
[MenuItem("PAL3A/Source Generator/Generate ConsoleCommands.PAL3A.g.cs", priority = 2)]
#endif
public static void GenerateConsoleCommands()
{
string writePath = $"{OutputFolderPath}{OutputFileName}";
ISourceGenerator sourceGenerator = new ConsoleCommandsAutoGen<ICommand>();
GenerateSourceInternal(OutputFileName, writePath, ClassName, NameSpace, sourceGenerator, true);
}
private static string CopyrightHeader(string writePath)
{
int currentYear = DateTime.Now.Year;
Match previous = Regex.Match(File.Exists(writePath) ? File.ReadAllText(writePath) : string.Empty,
@"Copyright \(c\) (?<first>\d{4})");
int firstYear = previous.Success ? int.Parse(previous.Groups["first"].Value) : currentYear;
string years = firstYear == currentYear ? $"{currentYear}" : $"{firstYear}-{currentYear}";
const string separator = "// ------------------------------------------------------------------------";
return $"{separator}\n// Copyright (c) {years}, Jiaqi (0x7c13) Liu. All rights reserved.\n" +
$"// See LICENSE file in the project root for license information.\n{separator}\n\n";
}
private static void GenerateSourceInternal(string fileName,
string writePath,
string className,
string nameSpace,
ISourceGenerator sourceGenerator,
bool overwrite)
{
if (!overwrite && File.Exists(writePath))
{
Debug.LogError($"[{nameof(SourceGeneratorEditorOptions)}] File already generated: {writePath}\n");
return;
}
Debug.Log($"[{nameof(SourceGeneratorEditorOptions)}] Generating source file: " + writePath);
CodeWriter writer = new()
{
Buffer = new StringBuilder(),
SpacesPerIndentLevel = 4,
};
sourceGenerator.GenerateSourceClass(writer, className, nameSpace);
// Final output string
string output = CopyrightHeader(writePath) + $"#if {GAME_VARIANT_SYMBOL}\n\n{writer.Buffer}\n#endif";
// Write to file
using StreamWriter sw = new(writePath);
sw.Write(output);
Debug.Log($"[{nameof(SourceGeneratorEditorOptions)}] {fileName} generated.");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}