Skip to content

Instantly share code, notes, and snippets.

@chisaato
Created July 17, 2024 22:50
Show Gist options
  • Save chisaato/6f8dbe1f53fe0b0e3dcf9c55ae239d98 to your computer and use it in GitHub Desktop.
Save chisaato/6f8dbe1f53fe0b0e3dcf9c55ae239d98 to your computer and use it in GitHub Desktop.
生成Modrinth规范的整合包信息,共HMCL用
import { $ } from "bun";
import { readdir } from "node:fs/promises";
import { mkdirSync, cpSync } from "node:fs";
const PACK_VERSION = "1.3";
const PACK_NAME = "creators-adventure";
const FORGE_VER = "47.3.5";
const MC_VER = "1.20.1";
// 只有在客户端出现的 Mod
// 采用 contains 匹配
const clientOnlyModsPattern = [];
// 额外的文件
const extraFiles = ["config/alexscaves-client.toml"];
// 下载前缀
const downloadPrefix = `https://example.com/creators-adventure`;
// read all the files in the current directory
const mods = (await readdir("mods", { withFileTypes: true })).filter((a) =>
a.name.endsWith(".jar")
);
// console.log(text);
// 遍历 mods 计算文件 SHA1 和 SHA256 还有文件大小
const modList = [];
mods.forEach((mod) => {
const modPath = `mods/${mod.name}`;
console.log("扫描并添加 Mod", modPath);
const modFile = Bun.file(modPath);
const sha256Haser = new Bun.CryptoHasher("sha256");
const sha1Hasher = new Bun.CryptoHasher("sha1");
sha256Haser.update(modFile.stream());
sha1Hasher.update(modFile.stream());
modList.push({
path: modPath,
hashes: {
sha1: sha1Hasher.digest("hex"),
sha256: sha256Haser.digest("hex"),
},
env: {
client: "required",
server: "required",
},
downloads: [`${downloadPrefix}/${PACK_VERSION}/${modPath}`],
fileSize: modFile.size,
});
});
const extraFilesList = [];
extraFiles.forEach((file) => {
const filePath = `${file}`;
console.log("扫描并添加额外文件", filePath);
const fileBytes = Bun.file(filePath).stream();
const sha256Haser = new Bun.CryptoHasher("sha256");
const sha1Hasher = new Bun.CryptoHasher("sha1");
sha256Haser.update(fileBytes);
sha1Hasher.update(fileBytes);
extraFilesList.push({
path: filePath,
hashes: {
sha1: sha1Hasher.digest("hex"),
sha256: sha256Haser.digest("hex"),
},
env: {
client: "required",
server: "required",
},
downloads: [`${downloadPrefix}/${PACK_VERSION}/${file}`],
fileSize: fileBytes.length,
});
});
// 组装最终的 JSON
const packManifest = {
formatVersion: 1,
game: "minecraft",
versionId: PACK_VERSION,
name: PACK_NAME,
files: [...modList, ...extraFilesList],
dependencies: {
forge: FORGE_VER,
minecraft: MC_VER,
},
};
// 把这个文件写成 modrinth.index.json
await Bun.write("modrinth.index.json", JSON.stringify(packManifest));
// 创建 pack-dump/PACK_VER/ 文件夹
// 拷贝符合的 mods 文件夹到 pack-dump/PACK_VER/
// 还有额外文件(如果有的话)
// 拷贝符合的额外文件到 pack-dump/PACK_VER/
// 拷贝 modrinth.index.json 到 pack-dump/PACK_VER/
// 打包 pack-dump/PACK_VER/ 为 zip
mkdirSync(`pack-dump/${PACK_VERSION}/mods`, { recursive: true });
mods.forEach((mod) => {
cpSync(`mods/${mod.name}`, `pack-dump/${PACK_VERSION}/mods/${mod.name}`, {
recursive: true,
});
});
extraFiles.forEach((file) => {
console.log("拷贝", `${file}`, "到", `pack-dump/${PACK_VERSION}/${file}`);
cpSync(`${file}`, `pack-dump/${PACK_VERSION}/${file}`, {
recursive: true,
});
});
cpSync("modrinth.index.json", `pack-dump/${PACK_VERSION}/modrinth.index.json`, {
recursive: true,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment