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 7 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 @@ -278,7 +278,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,11 @@ 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);
String filteredName = name.replaceAll("[^a-zA-Z0-9]", "");
Copy link
Member

Choose a reason for hiding this comment

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

可以解释下这行代码吗?

Copy link
Member Author

Choose a reason for hiding this comment

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

去掉除英文字母和数字的字符进行匹配,modSubnameMap 中的内容也是过滤后的内容

return modSubnameMap.get(filteredName);
}

public abstract String getMcmodUrl(Mod mod);
Expand Down Expand Up @@ -149,20 +151,22 @@ 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)) {
String filteredName = name.replaceAll("[^a-zA-Z0-9]", "");
if (!filteredName.isEmpty()) {
modSubnameMap.put(filteredName, mod);
}
}
}
Expand Down
Loading