Skip to content

Conversation

@MakinoharaShoko
Copy link
Member

在模板的描述文件 template.json 中,添加了 font 字段,用于描述用户的自定义字体。这一字体由引擎在初始化阶段通过插入 style 标签的方式被加载到页面中,并在 Options 中注册并成为可选项。

如果有自定义字体,其将会被添加到默认字体之前的位置。并且由于引擎默认选中第一个字体作为对话框和选项等 UI 的字体,所以一般来说自定义字体会被优先选中。

@MakinoharaShoko MakinoharaShoko changed the base branch from main to dev October 26, 2025 15:30
@gemini-code-assist
Copy link

Summary of Changes

Hello @MakinoharaShoko, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求的核心是引入了对自定义字体的支持,允许用户通过 template.json 文件定义自己的字体,并在游戏初始化时加载和应用。这些自定义字体将集成到游戏选项中,并优先用于UI显示。此外,还包含对脚本解析器的多项细微改进,以提高其健壮性,并添加了一个新的调试命令,以增强与外部编辑器的集成能力。

Highlights

  • 自定义字体支持: 在 template.json 描述文件中新增 font 字段,允许用户定义和加载自定义字体。
  • 字体加载机制: 引擎在初始化阶段通过动态插入 <style> 标签的方式加载自定义字体,并将其注册为游戏选项。
  • UI 字体优先级: 自定义字体将优先于默认字体显示,并成为对话框和UI元素的默认选项。
  • 解析器改进: 优化了脚本解析器,以更健壮地处理换行符、参数中的空白字符以及注释的解析。
  • 快速跳过功能增强: 导出了 setButton 函数为 setFastButton,并改进了鼠标滚轮快速跳过逻辑,以更好地控制和显示快速跳过状态。
  • 编辑器调试命令: 新增了一个 SET_EFFECT 调试命令,允许外部编辑器直接设置舞台效果(如位置和缩放)。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

您好,感谢您对项目的贡献。这次的 PR 引入了对自定义模板字体的支持,这是一个很棒的功能。整体实现结构清晰,通过创建新的工具函数、Redux action 和 reducer 以及新的类型定义,将新功能很好地集成到了现有代码库中。代码的模块化做得很好,例如将模板加载逻辑和字体选项逻辑分别放在了 templateLoader.tsfontOptions.ts 中。

不过,我发现了一些潜在的问题,主要集中在异步操作处理、路径解析和字符串处理上,这些问题可能会导致竞态条件(race condition)、资源加载失败或 CSS 语法错误。具体的修改建议请见下面的评论。

logger.info(`WebGAL v${__INFO.version}`);
logger.info('Github: https://github.com/OpenWebGAL/WebGAL ');
logger.info('Made with ❤ by OpenWebGAL');
loadTemplate();

Choose a reason for hiding this comment

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

high

loadTemplate 是一个异步函数,但在这里以“即发即忘”的方式调用,没有等待其完成。这会导致 initializeScript 中的后续代码在模板和字体加载完成前就执行,可能引发竞态条件(race condition)。例如,UI 可能在自定义字体注入前就渲染,导致字体闪烁或初期显示不正确。为了确保初始化按预期顺序完成,initializeScript 函数应该被声明为 async,并且在这里 await loadTemplate()

Comment on lines +57 to +70
function generateFontFaceRule(font: TemplateFontDescriptor): string | null {
const fontFamily = font['font-family'];
if (!fontFamily || !font.url || !font.type) {
logger.warn('忽略无效的模板字体配置', font);
return null;
}

const src = resolveTemplateAssetPath(font.url);
const weight = font.weight !== undefined ? `font-weight: ${font.weight};` : '';
const style = font.style ? `font-style: ${font.style};` : '';
const display = `font-display: ${font.display ?? 'swap'};`;

return `@font-face { font-family: '${fontFamily}'; src: url('${src}') format('${font.type}'); ${weight} ${style} ${display} }`;
}

Choose a reason for hiding this comment

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

high

generateFontFaceRule 函数在生成 @font-face 规则时,直接用单引号包裹了从 template.json 中获取的 font-family。如果用户在 JSON 文件中提供的字体名称本身已经包含了引号(例如 "'My Font'"),这会导致生成无效的 CSS(例如 font-family: ''My Font'')。建议在处理前先移除字体名称两端可能存在的引号,以增强函数的健壮性。

Suggested change
function generateFontFaceRule(font: TemplateFontDescriptor): string | null {
const fontFamily = font['font-family'];
if (!fontFamily || !font.url || !font.type) {
logger.warn('忽略无效的模板字体配置', font);
return null;
}
const src = resolveTemplateAssetPath(font.url);
const weight = font.weight !== undefined ? `font-weight: ${font.weight};` : '';
const style = font.style ? `font-style: ${font.style};` : '';
const display = `font-display: ${font.display ?? 'swap'};`;
return `@font-face { font-family: '${fontFamily}'; src: url('${src}') format('${font.type}'); ${weight} ${style} ${display} }`;
}
function generateFontFaceRule(font: TemplateFontDescriptor): string | null {
const fontFamily = font['font-family'];
if (!fontFamily || !font.url || !font.type) {
logger.warn('忽略无效的模板字体配置', font);
return null;
}
const sanitizedFontFamily = fontFamily.trim().replace(/^['"]|['"]$/g, '');
const src = resolveTemplateAssetPath(font.url);
const weight = font.weight !== undefined ? `font-weight: ${font.weight};` : '';
const style = font.style ? `font-style: ${font.style};` : '';
const display = `font-display: ${font.display ?? 'swap'};`;
return `@font-face { font-family: '${sanitizedFontFamily}'; src: url('${src}') format('${font.type}'); ${weight} ${style} ${display} }`;
}

Comment on lines +72 to +78
function resolveTemplateAssetPath(path: string): string {
if (/^(https?:)?\/\//i.test(path) || path.startsWith('data:')) {
return path;
}
const normalized = path.replace(/^[./]+/, '');
return `./game/template/${normalized}`;
}

Choose a reason for hiding this comment

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

high

resolveTemplateAssetPath 函数对相对路径的处理存在问题。当前的实现 path.replace(/^[./]+/,'') 会错误地处理包含 .. 的路径(例如,../fonts/myfont.woff 会被解析到 game/template/fonts/myfont.woff 而不是 game/fonts/myfont.woff),并且也会不正确地处理以 / 开头的根相对路径。这会导致字体等资源无法按预期加载。建议重写此函数,以更准确地处理不同类型的路径。

Suggested change
function resolveTemplateAssetPath(path: string): string {
if (/^(https?:)?\/\//i.test(path) || path.startsWith('data:')) {
return path;
}
const normalized = path.replace(/^[./]+/, '');
return `./game/template/${normalized}`;
}
function resolveTemplateAssetPath(path: string): string {
if (/^(https?:)?\/\//i.test(path) || path.startsWith('data:')) {
return path;
}
if (path.startsWith('/')) {
return path;
}
return `./game/template/${path}`;
}

Comment on lines +41 to +46
export function formatFontFamily(fontFamily: string): string {
const trimmed = fontFamily.trim();
const needsQuote = /\s/.test(trimmed);
const normalized = needsQuote ? `'${trimmed}'` : trimmed;
return `${normalized}, serif`;
}

Choose a reason for hiding this comment

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

high

formatFontFamily 函数没有处理 fontFamily 字符串本身已包含引号的情况。如果 template.json 中的字体名是 "'My Font'",此函数会返回 ''My Font'', serif,这是一个无效的 font-family 值。建议在处理前先移除字符串两端可能存在的引号,以提高代码的健壮性。

Suggested change
export function formatFontFamily(fontFamily: string): string {
const trimmed = fontFamily.trim();
const needsQuote = /\s/.test(trimmed);
const normalized = needsQuote ? `'${trimmed}'` : trimmed;
return `${normalized}, serif`;
}
export function formatFontFamily(fontFamily: string): string {
const trimmed = fontFamily.trim().replace(/^['"]|['"]$/g, '');
const needsQuote = /\s/.test(trimmed);
const normalized = needsQuote ? `'${trimmed}'` : trimmed;
return `${normalized}, serif`;
}

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.

2 participants