Skip to content

补充一些功能#6

Open
Cjx8848 wants to merge 1 commit intoUnrealMultiple:mainfrom
Cjx8848:main
Open

补充一些功能#6
Cjx8848 wants to merge 1 commit intoUnrealMultiple:mainfrom
Cjx8848:main

Conversation

@Cjx8848
Copy link
Member

@Cjx8848 Cjx8848 commented Feb 27, 2026

1.二进制文件获取字符列表
2.导出字符到txt文件
3.1000汉字选项增加(combat text只能用少量字)

Summary by Sourcery

添加新的字符集选项和元数据读取器,以支持在字体元数据与 TXT 文件之间导入/导出字符列表。

新功能:

  • 引入用于从较小的常用中文子集生成字体的预设字符集“常用1000字”。
  • 在构建字符集时,允许从导出的字体元数据 TXT 文件中导入字符。
  • 增加一个选项,可将当前选定的字符集直接导出为 TXT 文件。

增强:

  • 扩展字体元数据处理功能,提供读取二进制元数据文件并提取其中字符列表以复用的 API。
Original summary in English

Summary by Sourcery

Add new character set options and metadata readers to support importing/exporting character lists between font metadata and TXT files.

New Features:

  • Introduce a "常用1000字" preset character set for generating fonts from a smaller common Chinese subset.
  • Allow importing characters from exported font metadata TXT files when building the character set.
  • Add an option to export the currently selected character set directly to a TXT file.

Enhancements:

  • Extend font metadata handling with APIs to read binary metadata files and extract their character lists for reuse.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了两个问题,并提供了一些整体性的反馈:

  • 使用「仅导出字符集到 TXT」选项导出字符时,建议在写入前对 chars 集合进行排序(例如按码点排序),以确保输出是确定性的,更便于检查或进行 diff。
  • Program.cs 中的 Run 方法已经变得相当庞大,现在包含了多个针对不同导入/导出模式的交互式循环;建议将新增的「从字体导出 TXT 读取」和「仅导出字符集到 TXT」流程提取到单独的辅助方法中,从而让 CLI 入口逻辑更易于阅读和维护。
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When exporting characters with the "仅导出字符集到 TXT" option, consider sorting the `chars` set (e.g., by code point) before writing so the output is deterministic and easier to inspect or diff.
- The `Run` method in `Program.cs` is becoming quite large and now contains multiple interactive loops for different import/export modes; consider extracting the new "从字体导出 TXT 读取" and "仅导出字符集到 TXT" flows into dedicated helper methods to keep the CLI entrypoint easier to follow and maintain.

## Individual Comments

### Comment 1
<location path="TRPEFontGen/WriterExtensions.cs" line_range="27-34" />
<code_context>
         }
     }

+    extension(BinaryReader br)
+    {
+        public Rectangle ReadRectangle()
+        {
+            return new Rectangle(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32());
+        }
+
+        public Vector3 ReadVector3()
+        {
+            return new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
</code_context>
<issue_to_address>
**issue (bug_risk):** `extension(BinaryReader br)` 代码块不是合法的 C# 语法,会导致项目无法编译;这里应该使用标准的扩展方法。

要将其改为合法的扩展方法,请将这些方法移动到静态的 `WriterExtensions` 类中,并将其声明为 `public static`,同时使用 `this BinaryReader` 作为第一个参数,例如:

```csharp
public static Rectangle ReadRectangle(this BinaryReader br)
    => new Rectangle(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32());

public static Vector3 ReadVector3(this BinaryReader br)
    => new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
```

这样可以恢复编译,并使 `br.ReadRectangle()` / `br.ReadVector3()` 按预期工作。
</issue_to_address>

### Comment 2
<location path="FontGeneratorCLI/Program.cs" line_range="188-194" />
<code_context>
+                return;
+            }
+            var exportPath = AnsiConsole.Ask<string>("请输入 [blue]导出 TXT 文件路径[/]:", "chars_export.txt");
+            var sb = new System.Text.StringBuilder();
+            foreach (var c in chars)
+            {
+                sb.Append(c);
+            }
+            File.WriteAllText(exportPath, sb.ToString());
+            AnsiConsole.MarkupLine($"[green]已导出 [yellow]{chars.Count}[/] 个字符到: {exportPath}[/]");
+            return;
+        }
</code_context>
<issue_to_address>
**suggestion (bug_risk):**`HashSet<char>` 导出字符时,其顺序是非确定性的,这可能会导致多次运行间 TXT 输出结果不稳定。

由于 `chars``HashSet<char>`,它的迭代顺序并不稳定。对于 TXT 导出,更理想的方式是使用确定性的顺序(例如按码点排序)。建议在写入前先排序:

```csharp
var orderedChars = chars.OrderBy(c => c).ToArray();
File.WriteAllText(exportPath, new string(orderedChars));
```

这样可以确保多次运行之间的输出保持一致。

建议实现如下:

```csharp
        if (charOptions.Contains("仅导出字符集到 TXT"))
        {
            if (chars.Count == 0)
            {
                AnsiConsole.MarkupLine("[red]错误: 未选择或未识别到任何字符,程序退出。[/]");
                return;
            }

            var exportPath = AnsiConsole.Ask<string>("请输入 [blue]导出 TXT 文件路径[/]:", "chars_export.txt");

            // 将 HashSet<char> 按字符码点排序,保证导出顺序稳定
            var orderedChars = chars.OrderBy(c => c).ToArray();
            var content = new string(orderedChars);

            File.WriteAllText(exportPath, content);
            AnsiConsole.MarkupLine($"[green]已导出 [yellow]{orderedChars.Length}[/] 个字符到: {exportPath}[/]");
        }

        if (charOptions.Contains("从字体导出 TXT 读取"))

```

1. 确保文件顶部已包含 `using System.Linq;` 和 `using System.IO;`,如果没有,需要补充,以便使用 `OrderBy` 和 `File.WriteAllText`。
2. 如果项目中对输出格式有统一要求(例如换行结尾),可以在 `content` 生成后根据需要追加 `Environment.NewLine`。

Sourcery 对开源项目免费——如果你觉得这些代码评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English

Hey - I've found 2 issues, and left some high level feedback:

  • When exporting characters with the "仅导出字符集到 TXT" option, consider sorting the chars set (e.g., by code point) before writing so the output is deterministic and easier to inspect or diff.
  • The Run method in Program.cs is becoming quite large and now contains multiple interactive loops for different import/export modes; consider extracting the new "从字体导出 TXT 读取" and "仅导出字符集到 TXT" flows into dedicated helper methods to keep the CLI entrypoint easier to follow and maintain.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When exporting characters with the "仅导出字符集到 TXT" option, consider sorting the `chars` set (e.g., by code point) before writing so the output is deterministic and easier to inspect or diff.
- The `Run` method in `Program.cs` is becoming quite large and now contains multiple interactive loops for different import/export modes; consider extracting the new "从字体导出 TXT 读取" and "仅导出字符集到 TXT" flows into dedicated helper methods to keep the CLI entrypoint easier to follow and maintain.

## Individual Comments

### Comment 1
<location path="TRPEFontGen/WriterExtensions.cs" line_range="27-34" />
<code_context>
         }
     }

+    extension(BinaryReader br)
+    {
+        public Rectangle ReadRectangle()
+        {
+            return new Rectangle(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32());
+        }
+
+        public Vector3 ReadVector3()
+        {
+            return new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
</code_context>
<issue_to_address>
**issue (bug_risk):** The `extension(BinaryReader br)` block is not valid C# and will prevent the project from compiling; these should be standard extension methods.

To make these valid extension methods, move them into the static `WriterExtensions` class as `public static` methods with a `this BinaryReader` first parameter, e.g.

```csharp
public static Rectangle ReadRectangle(this BinaryReader br)
    => new Rectangle(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32());

public static Vector3 ReadVector3(this BinaryReader br)
    => new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
```

This will restore compilation and allow `br.ReadRectangle()` / `br.ReadVector3()` to work as intended.
</issue_to_address>

### Comment 2
<location path="FontGeneratorCLI/Program.cs" line_range="188-194" />
<code_context>
+                return;
+            }
+            var exportPath = AnsiConsole.Ask<string>("请输入 [blue]导出 TXT 文件路径[/]:", "chars_export.txt");
+            var sb = new System.Text.StringBuilder();
+            foreach (var c in chars)
+            {
+                sb.Append(c);
+            }
+            File.WriteAllText(exportPath, sb.ToString());
+            AnsiConsole.MarkupLine($"[green]已导出 [yellow]{chars.Count}[/] 个字符到: {exportPath}[/]");
+            return;
+        }
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Exporting characters from a `HashSet<char>` yields a non-deterministic order, which may make the TXT output unstable between runs.

Since `chars` is a `HashSet<char>`, its iteration order is not stable. For TXT export, a deterministic order (e.g., by code point) is preferable. Consider ordering before writing:

```csharp
var orderedChars = chars.OrderBy(c => c).ToArray();
File.WriteAllText(exportPath, new string(orderedChars));
```

This ensures consistent output across runs.

Suggested implementation:

```csharp
        if (charOptions.Contains("仅导出字符集到 TXT"))
        {
            if (chars.Count == 0)
            {
                AnsiConsole.MarkupLine("[red]错误: 未选择或未识别到任何字符,程序退出。[/]");
                return;
            }

            var exportPath = AnsiConsole.Ask<string>("请输入 [blue]导出 TXT 文件路径[/]:", "chars_export.txt");

            // 将 HashSet<char> 按字符码点排序,保证导出顺序稳定
            var orderedChars = chars.OrderBy(c => c).ToArray();
            var content = new string(orderedChars);

            File.WriteAllText(exportPath, content);
            AnsiConsole.MarkupLine($"[green]已导出 [yellow]{orderedChars.Length}[/] 个字符到: {exportPath}[/]");
        }

        if (charOptions.Contains("从字体导出 TXT 读取"))

```

1. 确保文件顶部已包含 `using System.Linq;` 和 `using System.IO;`,如果没有,需要补充,以便使用 `OrderBy` 和 `File.WriteAllText`。
2. 如果项目中对输出格式有统一要求(例如换行结尾),可以在 `content` 生成后根据需要追加 `Environment.NewLine`。
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +27 to +34
extension(BinaryReader br)
{
public Rectangle ReadRectangle()
{
return new Rectangle(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32());
}

public Vector3 ReadVector3()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): extension(BinaryReader br) 代码块不是合法的 C# 语法,会导致项目无法编译;这里应该使用标准的扩展方法。

要将其改为合法的扩展方法,请将这些方法移动到静态的 WriterExtensions 类中,并将其声明为 public static,同时使用 this BinaryReader 作为第一个参数,例如:

public static Rectangle ReadRectangle(this BinaryReader br)
    => new Rectangle(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32());

public static Vector3 ReadVector3(this BinaryReader br)
    => new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());

这样可以恢复编译,并使 br.ReadRectangle() / br.ReadVector3() 按预期工作。

Original comment in English

issue (bug_risk): The extension(BinaryReader br) block is not valid C# and will prevent the project from compiling; these should be standard extension methods.

To make these valid extension methods, move them into the static WriterExtensions class as public static methods with a this BinaryReader first parameter, e.g.

public static Rectangle ReadRectangle(this BinaryReader br)
    => new Rectangle(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32());

public static Vector3 ReadVector3(this BinaryReader br)
    => new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());

This will restore compilation and allow br.ReadRectangle() / br.ReadVector3() to work as intended.

Comment on lines +188 to +194
var sb = new System.Text.StringBuilder();
foreach (var c in chars)
{
sb.Append(c);
}
File.WriteAllText(exportPath, sb.ToString());
AnsiConsole.MarkupLine($"[green]已导出 [yellow]{chars.Count}[/] 个字符到: {exportPath}[/]");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk):HashSet<char> 导出字符时,其顺序是非确定性的,这可能会导致多次运行间 TXT 输出结果不稳定。

由于 charsHashSet<char>,它的迭代顺序并不稳定。对于 TXT 导出,更理想的方式是使用确定性的顺序(例如按码点排序)。建议在写入前先排序:

var orderedChars = chars.OrderBy(c => c).ToArray();
File.WriteAllText(exportPath, new string(orderedChars));

这样可以确保多次运行之间的输出保持一致。

建议实现如下:

        if (charOptions.Contains("仅导出字符集到 TXT"))
        {
            if (chars.Count == 0)
            {
                AnsiConsole.MarkupLine("[red]错误: 未选择或未识别到任何字符,程序退出。[/]");
                return;
            }

            var exportPath = AnsiConsole.Ask<string>("请输入 [blue]导出 TXT 文件路径[/]:", "chars_export.txt");

            // 将 HashSet<char> 按字符码点排序,保证导出顺序稳定
            var orderedChars = chars.OrderBy(c => c).ToArray();
            var content = new string(orderedChars);

            File.WriteAllText(exportPath, content);
            AnsiConsole.MarkupLine($"[green]已导出 [yellow]{orderedChars.Length}[/] 个字符到: {exportPath}[/]");
        }

        if (charOptions.Contains("从字体导出 TXT 读取"))
  1. 确保文件顶部已包含 using System.Linq;using System.IO;,如果没有,需要补充,以便使用 OrderByFile.WriteAllText
  2. 如果项目中对输出格式有统一要求(例如换行结尾),可以在 content 生成后根据需要追加 Environment.NewLine
Original comment in English

suggestion (bug_risk): Exporting characters from a HashSet<char> yields a non-deterministic order, which may make the TXT output unstable between runs.

Since chars is a HashSet<char>, its iteration order is not stable. For TXT export, a deterministic order (e.g., by code point) is preferable. Consider ordering before writing:

var orderedChars = chars.OrderBy(c => c).ToArray();
File.WriteAllText(exportPath, new string(orderedChars));

This ensures consistent output across runs.

Suggested implementation:

        if (charOptions.Contains("仅导出字符集到 TXT"))
        {
            if (chars.Count == 0)
            {
                AnsiConsole.MarkupLine("[red]错误: 未选择或未识别到任何字符,程序退出。[/]");
                return;
            }

            var exportPath = AnsiConsole.Ask<string>("请输入 [blue]导出 TXT 文件路径[/]:", "chars_export.txt");

            // 将 HashSet<char> 按字符码点排序,保证导出顺序稳定
            var orderedChars = chars.OrderBy(c => c).ToArray();
            var content = new string(orderedChars);

            File.WriteAllText(exportPath, content);
            AnsiConsole.MarkupLine($"[green]已导出 [yellow]{orderedChars.Length}[/] 个字符到: {exportPath}[/]");
        }

        if (charOptions.Contains("从字体导出 TXT 读取"))
  1. 确保文件顶部已包含 using System.Linq;using System.IO;,如果没有,需要补充,以便使用 OrderByFile.WriteAllText
  2. 如果项目中对输出格式有统一要求(例如换行结尾),可以在 content 生成后根据需要追加 Environment.NewLine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant