Skip to content

Commit

Permalink
feat: Build leveldb files for xdy-customizable-macros and xdy-interna…
Browse files Browse the repository at this point in the history
…l-utility-macros

As was done for the other macros already.

Also refactor the two functions, one for each pack, into a generic one that only
need the pack name to work for both compendiums.
  • Loading branch information
xyzzy42 authored and xdy committed Dec 20, 2024
1 parent 7d8ffb3 commit c1ee923
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 59 deletions.
101 changes: 44 additions & 57 deletions build/build-packs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ const macroIcons = new Map<string, string>([
["Use Scroll or Wand", "systems/pf2e/icons/equipment/wands/magic-wands/magic-wand.webp"],
["Versatile Performance", "systems/pf2e/icons/spells/summon-instrument.webp"],
["Wand and Scroll Generator", "systems/pf2e/icons/equipment/wands/specialty-wands/wand-of-continuation.webp"],

// xdy-customizable-macros
["customizableBasicActionMacros", "modules/xdy-pf2e-workbench/assets/icons/cc0/bam.webp"],
["customizableProceduralChecks", "systems/pf2e/icons/default-icons/party.svg"],
["customizableRefocusPremaster", "icons/magic/perception/third-eye-blue-red.webp"],

// xdy-internal-utility-macros
["Macro: Effect: Aid", "systems/pf2e/icons/spells/efficient-apport.webp"],
["Macro: Effect: Cover", "systems/pf2e/icons/conditions-2/status_acup.webp"],
["Macro: Effect: Follow The Expert", "systems/pf2e/icons/spells/favorable-review.webp"],
]);

function myRandomId() {
Expand All @@ -66,8 +76,6 @@ if (!outDir) {
throw new Error("Could not create output directory");
}

fs.mkdirsSync(path.resolve(outDir, "packs/data"));

async function buildAsymonousPack() {
const submod = "submodules/my-foundryvtt-macros";
const asymonousSource = ["PF2e", "PF2e/Contributions by others"];
Expand Down Expand Up @@ -158,73 +166,52 @@ ${documentation ? documentation[0] : "/* There is no documentation in the macro.
await compilePack(macrosImport, importPack, { log: true });
}

function buildCustomizableMacrosPack() {
const folderPath = "./src/packs/data/xdy-customizable-macros";
const lines: string[] = [];
// Pack "*.macro" files into a compendium
// Argument is both the directory in src/packs/data with source as well the the compendium name in outDir/packs/generated
async function buildMacrosPack(packName: string) {
const folderPath = path.resolve("src/packs/data", packName);
const baseMacro = {
author: null,
flags: {},
permission: { default: 1 },
scope: "global",
type: "script",
};
const baseMacroStats = { systemId: "pf2e", createdTime: Date.now(), modifiedTime: Date.now() };
const macros: object[] = [];
const files = fs.readdirSync(folderPath);
for (const file of files) {
const filePath = path.join(folderPath, file);
if (!filePath.endsWith(".macro")) {
continue;
}

try {
const macroName = path.parse(file).name;
const contents = fs.readFileSync(filePath, { encoding: "utf8" });
const map = new Map<string, string>();
map.set("customizableBasicActionMacros", "modules/xdy-pf2e-workbench/assets/icons/cc0/bam.webp");
map.set("customizableProceduralChecks", "systems/pf2e/icons/default-icons/party.svg");
map.set("customizableRefocusPremaster", "icons/magic/perception/third-eye-blue-red.webp");
const img = map.get(macroName) || "icons/svg/dice-target.svg";

// eslint-disable-next-line
let json = `{"_id": "${myRandomId()}", "actorIds": [], "author": "${myRandomId()}", "command": ${JSON.stringify(
contents,
)},"flags": {},"img":"${img}","name": "${macroName}","permission": {"default": 1},"scope": "global","type": "script"}`;
lines.push(json);
} catch (err) {
console.error(`Failed to read JSON file ${filePath}`, err);
}
const macroName = path.parse(file).name;
const contents = fs.readFileSync(filePath, { encoding: "utf8" });
const id = myRandomId();
macros.push({
_key: `!macros!${id}`,
_id: id,
_stats: { compendiumSource: compendiumUuid(packName, "Macro", id), ...baseMacroStats },
command: contents,
img: macroIcons.get(macroName) || "icons/svg/dice-target.svg",
name: macroName,
...baseMacro,
});
}
const file1 = path.resolve(outDir, "./packs/data/xdy-customizable-macros" + ".db");
// console.log(file1);
fs.writeFileSync(file1, lines.join("\n"), "utf8");
const pack = path.resolve(outDir, "packs", "generated", packName);
await compilePack(macros, pack, { log: true });
}

function buildInternalUtilityMacrosPack() {
const folderPath = "./src/packs/data/xdy-internal-utility-macros";
const lines: string[] = [];
const files = fs.readdirSync(folderPath);
for (const file of files) {
const filePath = path.join(folderPath, file);
if (!filePath.endsWith(".macro")) {
continue;
}
async function buildCustomizableMacrosPack() {
await buildMacrosPack("xdy-customizable-macros");
}

try {
const macroName = path.parse(file).name;
const contents = fs.readFileSync(filePath, { encoding: "utf8" });
const map = new Map<string, string>();
map.set("Macro: Effect: Aid", "systems/pf2e/icons/spells/efficient-apport.webp");
map.set("Macro: Effect: Cover", "systems/pf2e/icons/conditions-2/status_acup.webp");
map.set("Macro: Effect: Follow The Expert", "systems/pf2e/icons/spells/favorable-review.webp");
const img = map.get(macroName) || "icons/svg/dice-target.svg";

// eslint-disable-next-line
let json = `{"_id": "${myRandomId()}", "actorIds": [], "author": "${myRandomId()}", "command": ${JSON.stringify(
contents,
)},"flags": {},"img":"${img}","name": "${macroName}","permission": {"default": 1},"scope": "global","type": "script"}`;
lines.push(json);
} catch (err) {
console.error(`Failed to read JSON file ${filePath}`, err);
}
}
const file1 = path.resolve(outDir, "./packs/data/xdy-internal-utility-macros" + ".db");
fs.writeFileSync(file1, lines.join("\n"), "utf8");
async function buildInternalUtilityMacrosPack() {
await buildMacrosPack("xdy-internal-utility-macros");
}

buildCustomizableMacrosPack();
buildInternalUtilityMacrosPack();
await buildCustomizableMacrosPack();
await buildInternalUtilityMacrosPack();
await buildAsymonousPack();
fs.ensureDirSync("dist");
fs.rmSync("./dist/packs", { recursive: true, force: true });
Expand Down
4 changes: 2 additions & 2 deletions static/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"name": "xdy-customizable-macros",
"label": "PF2e Workbench Customizable Macros (xdy-customizable-macros)",
"system": "pf2e",
"path": "packs/data/xdy-customizable-macros.db",
"path": "packs/generated/xdy-customizable-macros",
"type": "Macro",
"flags": {},
"banner": "modules/xdy-pf2e-workbench/assets/media/pf2e_workbench_cover_compressed.webp",
Expand Down Expand Up @@ -133,7 +133,7 @@
"name": "xdy-internal-utility-macros",
"label": "xdy-internal-utility-macros, do not import",
"system": "pf2e",
"path": "packs/data/xdy-internal-utility-macros.db",
"path": "packs/generated/xdy-internal-utility-macros",
"type": "Macro",
"flags": {},
"banner": "modules/xdy-pf2e-workbench/assets/media/pf2e_workbench_cover_compressed.webp",
Expand Down

0 comments on commit c1ee923

Please sign in to comment.