mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-23 17:09:19 +00:00
fix(core): 增强字符处理逻辑并优化错误提示
This commit is contained in:
@@ -80,7 +80,7 @@ warning.config.image.invalid_font_chars: "<yellow>Issue found in file <arg:0> -
|
||||
warning.config.image.missing_char: "<yellow>Issue found in file <arg:0> - The image '<arg:1>' is missing the required 'char' argument.</yellow>"
|
||||
warning.config.image.codepoint_conflict: "<yellow>Issue found in file <arg:0> - The image '<arg:1>' is using a character '<arg:3>(<arg:4>)' in font <arg:2> that has been used by another image '<arg:5>'.</yellow>"
|
||||
warning.config.image.invalid_codepoint_grid: "<yellow>Issue found in file <arg:0> - Image '<arg:1>' has an invalid 'chars' codepoint grid.</yellow>"
|
||||
warning.config.image.invalid_char: "<yellow>Issue found in file <arg:0> - The image '<arg:1>' has a 'char' argument is invalid. Please do not write a character that contains two Unicode characters such as '⁉️'.</yellow>"
|
||||
warning.config.image.invalid_char: "<yellow>Issue found in file <arg:0> - Image '<arg:1>' has a char parameter containing combining characters, which may result in image splitting.</yellow>"
|
||||
warning.config.image.file_not_found: "<yellow>Issue found in file <arg:0> - PNG file '<arg:2>' not found for image '<arg:1>'.</yellow>"
|
||||
warning.config.image.invalid_hex_value: "<yellow>Issue found in file <arg:0> - The image '<arg:1>' is using a unicode character '<arg:2>' that is not a valid hexadecimal (radix 16) value.</yellow>"
|
||||
warning.config.recipe.duplicate: "<yellow>Issue found in file <arg:0> - Duplicated recipe '<arg:1>'. Please check if there is the same configuration in other files.</yellow>"
|
||||
|
||||
@@ -80,7 +80,7 @@ warning.config.image.invalid_font_chars: "<yellow>在文件 <arg:0> 发现问题
|
||||
warning.config.image.missing_char: "<yellow>在文件 <arg:0> 发现问题 - 图片 '<arg:1>' 缺少必需的 'char' 参数</yellow>"
|
||||
warning.config.image.codepoint_conflict: "<yellow>在文件 <arg:0> 发现问题 - 图片 '<arg:1>' 在字体 <arg:2> 中使用的字符 '<arg:3>(<arg:4>)' 已被其他图片 '<arg:5>' 占用</yellow>"
|
||||
warning.config.image.invalid_codepoint_grid: "<yellow>在文件 <arg:0> 发现问题 - 图片 '<arg:1>' 的 'chars' 码位网格无效</yellow>"
|
||||
warning.config.image.invalid_char: "<yellow>在文件 <arg:0> 发现问题 - 图片 '<arg:1>' 的 'char' 参数无效 请不要写一个包含两个 Unicode 的字符例如 '⁉️'</yellow>"
|
||||
warning.config.image.invalid_char: "<yellow>在文件 <arg:0> 发现问题 - 图片 '<arg:1>' 的 'char' 参数包含组合字符可能导致图片分裂</yellow>"
|
||||
warning.config.image.file_not_found: "<yellow>在文件 <arg:0> 发现问题 - 图片 '<arg:1>' 的 PNG 文件 '<arg:2>' 未找到</yellow>"
|
||||
warning.config.image.invalid_hex_value: "<yellow>在文件 <arg:0> 发现问题 - 图片 '<arg:1>' 使用的 Unicode 字符 '<arg:2>' 不是有效的十六进制值</yellow>"
|
||||
warning.config.recipe.duplicate: "<yellow>在文件 <arg:0> 发现问题 - 重复的配方 '<arg:1>' 请检查其他文件中是否存在相同配置</yellow>"
|
||||
|
||||
@@ -462,7 +462,18 @@ public abstract class AbstractFontManager implements FontManager {
|
||||
if (character.length() == 1) {
|
||||
chars = List.of(character.toCharArray());
|
||||
} else {
|
||||
chars = List.of(CharacterUtils.decodeUnicodeToChars(character));
|
||||
if (character.startsWith("\\u")) {
|
||||
chars = List.of(CharacterUtils.decodeUnicodeToChars(character));
|
||||
} else {
|
||||
if (CharacterUtils.containsCombinedCharacter(character)) {
|
||||
TranslationManager.instance().log("warning.config.image.invalid_char", path.toString(), id.toString());
|
||||
}
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (char c : character.toCharArray()) {
|
||||
stringBuilder.append(String.format("\\u%04x", (int) c));
|
||||
}
|
||||
chars = List.of(CharacterUtils.decodeUnicodeToChars(stringBuilder.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -482,7 +493,7 @@ public abstract class AbstractFontManager implements FontManager {
|
||||
}
|
||||
}
|
||||
if (codepoints.length == 0) {
|
||||
throw new LocalizedResourceConfigException("warning.config.image.invalid_char", path, id);
|
||||
throw new LocalizedResourceConfigException("warning.config.image.missing_char", path, id);
|
||||
}
|
||||
codepointGrid[i] = codepoints;
|
||||
if (size == -1) size = codepoints.length;
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.momirealms.craftengine.core.util;
|
||||
|
||||
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class CharacterUtils {
|
||||
@@ -72,4 +73,27 @@ public class CharacterUtils {
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static boolean containsCombinedCharacter(String input) {
|
||||
if (input == null || input.isEmpty() || input.length() == 1) return false;
|
||||
for (int i = 0; i < input.length();) {
|
||||
int codePoint = input.codePointAt(i);
|
||||
i += Character.charCount(codePoint);
|
||||
int type = Character.getType(codePoint);
|
||||
if (type == Character.NON_SPACING_MARK ||
|
||||
type == Character.ENCLOSING_MARK ||
|
||||
type == Character.COMBINING_SPACING_MARK ||
|
||||
type == Character.FORMAT ||
|
||||
type == Character.CONTROL ||
|
||||
type == Character.SURROGATE ||
|
||||
type == Character.PRIVATE_USE ||
|
||||
Pattern.compile("[\\p{Mn}\\p{Me}\\p{Mc}\\p{Cf}]").matcher(new String(Character.toChars(codePoint))).find()
|
||||
) return true;
|
||||
if (i < input.length()) {
|
||||
int nextCodePoint = input.codePointAt(i);
|
||||
if (Character.isSurrogatePair(Character.toChars(codePoint)[0], Character.toChars(nextCodePoint)[0])) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user