Skip to content

Commit 5c4a893

Browse files
committed
✨ feat: 重构资源包转换逻辑
1 parent be3273b commit 5c4a893

5 files changed

Lines changed: 23 additions & 14 deletions

File tree

src/main/java/i18nupdatemod/I18nUpdateMod.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import i18nupdatemod.core.ResourcePack;
88
import i18nupdatemod.core.ResourcePackConverter;
99
import i18nupdatemod.entity.GameAssetDetail;
10+
import i18nupdatemod.entity.GameMetaData;
1011
import i18nupdatemod.util.FileUtil;
1112
import i18nupdatemod.util.Log;
1213

@@ -74,8 +75,9 @@ public static void init(Path minecraftPath, String minecraftVersion, String load
7475
if (!convertNotNeed) {
7576
FileUtil.setTemporaryDirPath(Paths.get(localStorage, "." + MOD_ID, minecraftVersion));
7677
applyFileName = assets.covertFileName;
78+
GameMetaData metaData = I18nConfig.getPackFormat(minecraftVersion);
7779
ResourcePackConverter converter = new ResourcePackConverter(languagePacks, applyFileName);
78-
converter.convert(assets.covertPackFormat, assets.minFormat, assets.maxFormat, getResourcePackDescription(assets.downloads));
80+
converter.convert(metaData, getResourcePackDescription(assets.downloads));
7981
}
8082

8183
//Apply resource pack

src/main/java/i18nupdatemod/core/I18nConfig.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ public static GameAssetDetail getAssetDetail(String minecraftVersion, String loa
7171
ret.downloads = createDownloadDetails(convert, loader, assetRoot);
7272
}
7373

74-
ret.covertPackFormat = convert.packFormat;
75-
ret.minFormat = convert.minFormat;
76-
ret.maxFormat = convert.maxFormat;
7774
ret.covertFileName =
7875
String.format("Minecraft-Mod-Language-Modpack-Converted-%s.zip", minecraftVersion);
7976
return ret;
8077
}
8178

79+
public static GameMetaData getPackFormat(String minecraftVersion) {
80+
return getGameMetaData(minecraftVersion);
81+
}
82+
8283
private static List<GameAssetDetail.AssetDownloadDetail> createDownloadDetails(GameMetaData convert, String loader, String assetRoot) {
8384
return convert.convertFrom.stream().map(it -> getAssetMetaData(it, loader)).map(it -> {
8485
GameAssetDetail.AssetDownloadDetail adi = new GameAssetDetail.AssetDownloadDetail();

src/main/java/i18nupdatemod/core/ResourcePackConverter.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5+
import i18nupdatemod.entity.GameMetaData;
56
import i18nupdatemod.util.FileUtil;
67
import i18nupdatemod.util.Log;
78
import org.apache.commons.io.IOUtils;
@@ -32,7 +33,7 @@ public ResourcePackConverter(List<ResourcePack> resourcePack, String filename) {
3233
this.tmpFilePath = FileUtil.getTemporaryPath(filename);
3334
}
3435

35-
public void convert(Integer packFormat, Integer minFormat, Integer maxFormat, String description) throws Exception {
36+
public void convert(GameMetaData metaData, String description) throws Exception {
3637
Set<String> fileList = new HashSet<>();
3738
try (ZipOutputStream zos = new ZipOutputStream(
3839
Files.newOutputStream(tmpFilePath),
@@ -57,7 +58,7 @@ public void convert(Integer packFormat, Integer minFormat, Integer maxFormat, St
5758
InputStream is = zf.getInputStream(ze);
5859
if (name.equalsIgnoreCase("pack.mcmeta")) {
5960
//Convert pack.mcmeta
60-
zos.write(convertPackMeta(is, packFormat, minFormat, maxFormat, description));
61+
zos.write(convertPackMeta(is, metaData, description));
6162
} else {
6263
//Copy other file
6364
IOUtils.copy(is, zos);
@@ -74,15 +75,15 @@ public void convert(Integer packFormat, Integer minFormat, Integer maxFormat, St
7475
}
7576
}
7677

77-
private byte[] convertPackMeta(InputStream is, Integer packFormat, Integer minFormat, Integer maxFormat, String description) {
78+
private byte[] convertPackMeta(InputStream is, GameMetaData metaData, String description) {
7879
PackMeta meta = GSON.fromJson(new InputStreamReader(is, StandardCharsets.UTF_8), PackMeta.class);
7980
// 1.21.9+ 使用 min_format/max_format,不再使用 pack_format
80-
if (minFormat != null && maxFormat != null) {
81+
if (metaData.useNewFormat()) {
8182
meta.pack.pack_format = null;
82-
meta.pack.min_format = minFormat;
83-
meta.pack.max_format = maxFormat;
83+
meta.pack.min_format = metaData.minFormat;
84+
meta.pack.max_format = metaData.maxFormat;
8485
} else {
85-
meta.pack.pack_format = packFormat;
86+
meta.pack.pack_format = metaData.packFormat;
8687
meta.pack.min_format = null;
8788
meta.pack.max_format = null;
8889
}

src/main/java/i18nupdatemod/entity/GameAssetDetail.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
public class GameAssetDetail {
66
public List<AssetDownloadDetail> downloads;
7-
public Integer covertPackFormat; // 旧格式 pack_format
8-
public Integer minFormat; // 新格式 min_format
9-
public Integer maxFormat; // 新格式 max_format
107
public String covertFileName;
118

129
public static class AssetDownloadDetail {

src/main/java/i18nupdatemod/entity/GameMetaData.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ public class GameMetaData {
88
public Integer minFormat; // 新格式,用于 1.21.9 及之后
99
public Integer maxFormat; // 新格式,用于 1.21.9 及之后
1010
public List<String> convertFrom;
11+
12+
/**
13+
* 判断是否使用新格式 (min_format/max_format)
14+
* 1.21.9+ 使用新格式,不再使用 pack_format
15+
*/
16+
public boolean useNewFormat() {
17+
return minFormat != null && maxFormat != null;
18+
}
1119
}

0 commit comments

Comments
 (0)