dynamic-loader-plugin
Version:
Dynamic asset loader for Phaser 3 with priority-based loading
97 lines (96 loc) • 4.34 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scanAssets = scanAssets;
const fast_glob_1 = require("fast-glob");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
function scanAssets(basePath) {
return __awaiter(this, void 0, void 0, function* () {
const manifest = { scenes: {} };
const sceneDirs = yield (0, fast_glob_1.glob)("*_*", {
cwd: basePath,
onlyDirectories: true
});
for (const dir of sceneDirs) {
const [priorityStr, ...sceneNameParts] = dir.split("_");
const sceneName = sceneNameParts.join("_");
const assets = [];
const files = yield (0, fast_glob_1.glob)("**/*.{png,jpeg,jpg,json,mp3,ogg,wav}", {
cwd: path_1.default.join(basePath, dir),
caseSensitiveMatch: false
});
const handledAtlasBases = new Set(); // 🔄 NEW: track already processed atlas base names
for (const file of files) {
const ext = path_1.default.extname(file).toLowerCase();
const baseName = path_1.default.basename(file, ext);
const fullKey = `${sceneName}_${baseName}`;
// Check if this is an atlas pair (JSON + matching PNG)
if (ext === ".json") {
const imageCandidate = path_1.default.join(path_1.default.dirname(path_1.default.join(basePath, dir, file)), baseName + ".png");
if (fs_1.default.existsSync(imageCandidate)) {
const atlasAsset = {
key: fullKey,
type: "atlas",
imagePath: path_1.default.join(dir, baseName + ".png").replace(/\\/g, "/"),
jsonPath: path_1.default.join(dir, file).replace(/\\/g, "/")
};
assets.push(atlasAsset);
continue;
}
}
// For all other files: image, audio, json, spritesheet
const assetType = getAssetType(file, ext);
// Narrow type to "non-atlas" asset only
if (assetType !== "atlas") {
const normalAsset = {
key: fullKey,
type: assetType, // now type is correctly narrowed
path: path_1.default.join(dir, file).replace(/\\/g, "/")
};
if (file.includes("spritesheet")) {
normalAsset.frameConfig = { frameWidth: 32 };
}
assets.push(normalAsset);
}
}
manifest.scenes[sceneName] = {
priority: parseInt(priorityStr) || 0,
assets
};
}
return manifest;
});
}
function getAssetType(filename, ext) {
const lowerName = filename.toLowerCase();
if (lowerName.includes("spritesheet"))
return "spritesheet";
if (lowerName.includes("atlas"))
return "atlas"; // 🔄 Optional: still valid fallback
switch (ext) {
case ".png":
case ".jpg":
case ".jpeg":
return "image";
case ".json":
return "json";
case ".mp3":
case ".ogg":
case ".wav":
return "audio";
default:
return "image";
}
}