diff --git a/.github/workflows/common.yml b/.github/workflows/common.yml new file mode 100644 index 0000000..27c69b5 --- /dev/null +++ b/.github/workflows/common.yml @@ -0,0 +1,51 @@ +name: Common +on: + workflow_call: + inputs: + type: + required: true + type: string + publish-task: + required: false + type: string + is-snapshot: + required: false + type: boolean + default: true + +jobs: + build-common: + name: Build Common + runs-on: ubuntu-latest + environment: Build + steps: + - + name: Checkout + uses: actions/checkout@v3 + - + name: Set up JDK + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: '17' + cache: 'gradle' + - + name: Build + env: + IS_SNAPSHOT: ${{ inputs.is-snapshot }} + run: | + ./gradlew clean build --stacktrace + - + name: Publish + if: ${{ inputs.publish-task }} + env: + CURSEFORGE_API_KEY: ${{ secrets.CURSEFORGE_API_KEY }} + MODRINTH_TOKEN: ${{ secrets.MODRINTH_TOKEN }} + run: | + export GIT_COMMIT_DESC=$(git log --format=%B -n 1 $GITHUB_SHA) + ./gradlew ${{ inputs.publish-task }} --info --stacktrace + - + uses: actions/upload-artifact@v3 + with: + name: CustomSkinLoader-${{ inputs.type }}-${{ github.run_number }} + path: build/libs \ No newline at end of file diff --git a/README.md b/README.md index 80da946..b5d395d 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,11 @@ 本Mod用于自动更新「简体中文资源包」。 ## 下载 -- CurseForge +- [CurseForge](https://www.curseforge.com/minecraft/mc-mods/i18nupdatemod) - Modrinth ## 支持的版本 -- Minecraft:1.6-1.19.3都支持(不代表这些版本都有对应的「简体中文资源包」) +- Minecraft:1.12.2-1.19.3都支持(不代表这些版本都有对应的「简体中文资源包」、未来会添加更多版本支持) - Mod加载器:Forge、Fabric、Quilt都支持(有需求的话也可以增加LiteLoader、Rift支持) - Java:8-19都支持 diff --git a/build.gradle.kts b/build.gradle.kts index 29878c5..a3bfdb0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "i18nupdatemod" -version = "3.1.0-SNAPSHOT" +version = "3.2.0" java { toolchain { @@ -62,6 +62,7 @@ dependencies { implementation("commons-codec:commons-codec:1.15") implementation("org.ow2.asm:asm:9.4") implementation("org.jetbrains:annotations:24.0.0") + implementation("com.google.code.gson:gson:2.10.1") } tasks.test { diff --git a/src/main/java/i18nupdatemod/I18nUpdateMod.java b/src/main/java/i18nupdatemod/I18nUpdateMod.java index 7cd8f76..a661be0 100644 --- a/src/main/java/i18nupdatemod/I18nUpdateMod.java +++ b/src/main/java/i18nupdatemod/I18nUpdateMod.java @@ -1,6 +1,7 @@ package i18nupdatemod; import i18nupdatemod.core.AssetConfig; +import i18nupdatemod.core.GameConfig; import i18nupdatemod.core.ResourcePack; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -24,11 +25,23 @@ public static void init(Path minecraftPath, String minecraftVersion, String load ResourcePack.resourcePackPath = minecraftPath.resolve("resourcepacks"); ResourcePack.temporaryPath = Paths.get(userHome, "." + MOD_ID, minecraftVersion); + + int minecraftMajorVersion = Integer.parseInt(minecraftVersion.split("\\.")[1]); + try { + //Get asset Map assets = AssetConfig.getAsset(minecraftVersion, loader); + + //Update resource pack ResourcePack languagePack = new ResourcePack(assets.get(AssetConfig.Type.FILE_NAME)); languagePack.checkUpdate(assets.get(AssetConfig.Type.FILE_URL), assets.get(AssetConfig.Type.MD5_URL)); + + //Apply resource pack + GameConfig config = new GameConfig(minecraftPath.resolve("options.txt")); + config.addResourcePack("Minecraft-Mod-Language-Modpack", + (minecraftMajorVersion <= 12 ? "" : "file/") + assets.get(AssetConfig.Type.FILE_NAME)); + config.writeToFile(); } catch (Exception e) { LOGGER.warn("Failed to update resource pack: {}", e.toString()); // e.printStackTrace(); diff --git a/src/main/java/i18nupdatemod/core/GameConfig.java b/src/main/java/i18nupdatemod/core/GameConfig.java new file mode 100644 index 0000000..871560f --- /dev/null +++ b/src/main/java/i18nupdatemod/core/GameConfig.java @@ -0,0 +1,48 @@ +package i18nupdatemod.core; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import i18nupdatemod.I18nUpdateMod; +import org.apache.commons.io.FileUtils; + +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class GameConfig { + private static final Gson GSON = new Gson(); + private static final Type STRING_LIST_TYPE = new TypeToken>() { + }.getType(); + protected Map configs = new LinkedHashMap<>(); + private final Path configFile; + + public GameConfig(Path configFile) throws Exception { + this.configFile = configFile; + if (!Files.exists(configFile)) { + return; + } + this.configs = FileUtils.readLines(configFile.toFile(), StandardCharsets.UTF_8).stream() + .map(it -> it.split(":", 2)) + .collect(Collectors.toMap(it -> it[0], it -> it[1], (a, b) -> a, LinkedHashMap::new)); + } + + public void writeToFile() throws Exception { + FileUtils.writeLines(configFile.toFile(), configs.entrySet().stream() + .map(it -> it.getKey() + ":" + it.getValue()).collect(Collectors.toList())); + } + + public void addResourcePack(String baseName, String resourcePack) { + List resourcePacks = GSON.fromJson( + configs.computeIfAbsent("resourcePacks", it -> "[]"), STRING_LIST_TYPE); + //Remove other Minecraft Mod Language Pack + resourcePacks = resourcePacks.stream().filter(it -> !it.contains(baseName)).collect(Collectors.toList()); + resourcePacks.add(resourcePack); + configs.put("resourcePacks", GSON.toJson(resourcePacks)); + I18nUpdateMod.LOGGER.info("Resource Packs: {}", configs.get("resourcePacks")); + } +} diff --git a/src/main/java/i18nupdatemod/fabricloader/FabricLoaderMod.java b/src/main/java/i18nupdatemod/fabricloader/FabricLoaderMod.java index b051837..647a113 100644 --- a/src/main/java/i18nupdatemod/fabricloader/FabricLoaderMod.java +++ b/src/main/java/i18nupdatemod/fabricloader/FabricLoaderMod.java @@ -4,7 +4,6 @@ import i18nupdatemod.util.Reflection; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.impl.FabricLoaderImpl; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -35,7 +34,7 @@ private String getMcVersion(){ } try { - //Fabric + //Quilt return (String) Reflection.clazz("org.quiltmc.loader.impl.QuiltLoaderImpl") .get("INSTANCE") .get("getGameProvider()")