Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化模组翻译匹配 #3231

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static class ModInfoObject extends RecursiveTreeObject<ModInfoObject> implements
message.append(", ").append(i18n("archive.author")).append(": ").append(localModFile.getAuthors());
this.message = message.toString();

this.mod = ModTranslations.MOD.getModById(localModFile.getId());
this.mod = ModTranslations.MOD.getModByName(localModFile.getName());
}

String getTitle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.jackhuang.hmcl.util.logging.Logger.LOG;
Expand Down Expand Up @@ -67,7 +68,7 @@ public static ModTranslations getTranslationsByRepositoryType(RemoteModRepositor

private final String resourceName;
private List<Mod> mods;
private Map<String, Mod> modIdMap; // mod id -> mod
private Map<String, Mod> modSubnameMap; // mod name -> mod
private Map<String, Mod> curseForgeMap; // curseforge id -> mod
private List<Pair<String, Mod>> keywords;
private int maxKeywordLength = -1;
Expand All @@ -84,10 +85,10 @@ public Mod getModByCurseForgeId(String id) {
}

@Nullable
public Mod getModById(String id) {
if (StringUtils.isBlank(id) || !loadModIdMap()) return null;
public Mod getModByName(String name) {
if (StringUtils.isBlank(name) || !loadModSubnameMap()) return null;

return modIdMap.get(id);
return modSubnameMap.get(name);
}

public abstract String getMcmodUrl(Mod mod);
Expand Down Expand Up @@ -149,20 +150,25 @@ private boolean loadCurseForgeMap() {
return true;
}

private boolean loadModIdMap() {
if (modIdMap != null) {
private boolean loadModSubnameMap() {
if (modSubnameMap != null) {
return true;
}

if (mods == null) {
if (!loadFromResource()) return false;
}

modIdMap = new HashMap<>();
modSubnameMap = new HashMap<>();
for (Mod mod : mods) {
for (String id : mod.getModIds()) {
if (StringUtils.isNotBlank(id) && !"examplemod".equals(id)) {
modIdMap.put(id, mod);
String name = mod.getSubname();
if (StringUtils.isNotBlank(name) && !"examplemod".equals(name)) {
modSubnameMap.put(name, mod);
String illegalCharsPattern = "[^\\x00-\\x7F]"; // 匹配 ASCII 范围(0-127)之外的任何字符
if (name.matches(".* .*$") || Pattern.compile(illegalCharsPattern).matcher(name).find()) { // 如果 name 有且仅有一个空格
name = name.replace(" ", "");
name = name.replaceAll(illegalCharsPattern, "");
modSubnameMap.put(name, mod);
}
}
}
Expand Down