diff --git a/.gitignore b/.gitignore index 6bf060c47..f3d39d296 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,9 @@ yarn.lock ## windows desktop.ini +## OSX +.DS_Store + ## parcel .parcel-cache/ diff --git a/project/package.json b/project/package.json index feaa7d500..9e749eece 100644 --- a/project/package.json +++ b/project/package.json @@ -11,6 +11,7 @@ "node": "20.11.1" }, "scripts": { + "postinstall": "node scripts/fix-7za-permissions.js", "check:circular": "madge --circular --ts-config tsconfig.json --extensions ts ./src/", "lint": "npx @biomejs/biome lint ./", "lint:fix": "npx @biomejs/biome lint --write ./", @@ -31,7 +32,9 @@ "gen:types": "tsc -p tsconfig.typedef.json --resolveJsonModule", "gen:docs": "typedoc --options ./typedoc.json --entryPointStrategy expand ./src", "gen:items": "ts-node -r tsconfig-paths/register ./src/tools/ItemTplGenerator/ItemTplGeneratorProgram.ts", - "gen:productionquests": "ts-node -r tsconfig-paths/register ./src/tools/ProductionQuestsGen/ProductionQuestsGenProgram.ts" + "gen:productionquests": "ts-node -r tsconfig-paths/register ./src/tools/ProductionQuestsGen/ProductionQuestsGenProgram.ts", + "database:compress": "node scripts/databaseCompress.js", + "database:decompress": "node scripts/databaseDecompress.js" }, "dependencies": { "atomically": "~1.7", @@ -68,6 +71,7 @@ "@vitest/ui": "~2", "@yao-pkg/pkg": "5.12", "@yao-pkg/pkg-fetch": "3.5.9", + "7zip-bin": "^5.2.0", "cross-env": "~7.0", "fs-extra": "~11.2", "gulp": "~5.0", @@ -77,11 +81,11 @@ "gulp-rename": "~2.0", "madge": "~7", "minimist": "~1.2", + "node-7z": "^3.0.0", "resedit": "~2.0", "ts-node-dev": "~2.0", "tsconfig-paths": "~4.2", - "typedoc": "~0.26", - "typemoq": "~2.1" + "typedoc": "~0.26" }, "targets": { "default": { diff --git a/project/scripts/databaseCompress.js b/project/scripts/databaseCompress.js new file mode 100644 index 000000000..80b3b0a50 --- /dev/null +++ b/project/scripts/databaseCompress.js @@ -0,0 +1,31 @@ +// This script compresses the locations database into a 7z archive. + +const Seven = require("node-7z"); +const path = require("node:path"); +const { path7za } = require("7zip-bin"); + +const archivePath = path.resolve(__dirname, "../assets/compressed/locations.7z"); +const locationsDir = path.resolve(__dirname, "../assets/database/locations/*"); + +let hadError = false; + +console.log(archivePath); +console.log(locationsDir); + +const myStream = Seven.add(archivePath, locationsDir, { + recursive: true, + $bin: path7za, + method: ["0=LZMA2"], + compressionLevel: 9, +}); + +myStream.on("end", () => { + if (!hadError) { + console.log("Compression completed successfully."); + } +}); + +myStream.on("error", (err) => { + hadError = true; + console.error(`Error compressing locations: ${err}`); +}); diff --git a/project/scripts/databaseDecompress.js b/project/scripts/databaseDecompress.js new file mode 100644 index 000000000..695bcb1e2 --- /dev/null +++ b/project/scripts/databaseDecompress.js @@ -0,0 +1,47 @@ +// This script removes the contents of the locations directory and then decompresses +// the locations database from a 7z archive. + +const Seven = require("node-7z"); +const path = require("node:path"); +const fs = require("fs-extra"); +const { path7za } = require("7zip-bin"); + +const archivePath = path.resolve(__dirname, "../assets/compressed/locations.7z"); +const databaseDir = path.resolve(__dirname, "../assets/database/locations"); + +(async () => { + try { + const archiveExists = await fs.pathExists(archivePath); + if (!archiveExists) { + console.error("Error: Archive file does not exist:", archivePath); + process.exit(1); + } + + const locationsDir = path.join(databaseDir, "locations"); + if (await fs.pathExists(locationsDir)) { + await fs.remove(locationsDir); + console.log("Existing locations directory removed."); + } + + let hadError = false; + + const myStream = Seven.extractFull(archivePath, databaseDir, { + $bin: path7za, + overwrite: "a", + }); + + myStream.on("end", () => { + if (!hadError) { + console.log("Decompression completed successfully."); + } + }); + + myStream.on("error", (err) => { + hadError = true; + console.error(`Error decompressing locations: ${err}`); + }); + } catch (err) { + console.error(`Error during decompression: ${err}`); + process.exit(1); + } +})(); diff --git a/project/scripts/fix-7za-permissions.js b/project/scripts/fix-7za-permissions.js new file mode 100644 index 000000000..e166bbc90 --- /dev/null +++ b/project/scripts/fix-7za-permissions.js @@ -0,0 +1,30 @@ +// This script sets the execute permission on the 7za binary if you're on macOS or Linux. + +const fs = require("node:fs"); +const path = require("node:path"); +const os = require("node:os"); + +const platform = os.platform(); +const arch = os.arch(); + +let sevenZipPath; + +if (platform === "darwin") { + // macOS + sevenZipPath = path.join(__dirname, "..", "node_modules", "7zip-bin", "mac", arch, "7za"); +} else if (platform === "linux") { + // Linux + sevenZipPath = path.join(__dirname, "..", "node_modules", "7zip-bin", "linux", arch, "7za"); +} else { + // Windows (or other) + process.exit(0); +} + +fs.chmod(sevenZipPath, 0o755, (err) => { + if (err) { + console.error("Failed to set execute permission on 7za:", err); + process.exit(1); + } else { + console.log("Execute permission set on 7za."); + } +});