novaparse
Version:
An EV Nova file parser for NovaJS
227 lines • 11.6 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const Gettable_1 = require("novadatainterface/Gettable");
const NovaDataInterface_1 = require("novadatainterface/NovaDataInterface");
const path = require("path");
const IDSpaceHandler_1 = require("./IDSpaceHandler");
const ExplosionParse_1 = require("./parsers/ExplosionParse");
const OutfitParse_1 = require("./parsers/OutfitParse");
const PlanetParse_1 = require("./parsers/PlanetParse");
const ResourceIDNotFound_1 = require("./parsers/ResourceIDNotFound");
const ShipParse_1 = require("./parsers/ShipParse");
const SpriteSheetMultiParse_1 = require("./parsers/SpriteSheetMultiParse");
const StatusBarParse_1 = require("./parsers/StatusBarParse");
const SystemParse_1 = require("./parsers/SystemParse");
const TargetCornersParse_1 = require("./parsers/TargetCornersParse");
const WeaponParse_1 = require("./parsers/WeaponParse");
const ResourceHolderBase_1 = require("./ResourceHolderBase");
const NovaIDs_1 = require("novadatainterface/NovaIDs");
const PictParse_1 = require("./parsers/PictParse");
class NovaParse {
constructor(dataPath, strict = true) {
// Strict will throw an error if any resource is not found.
// Otherwise, it will try to substitute default resources whenever possible (success may vary).
if (strict) {
this.resourceNotFoundFunction = ResourceIDNotFound_1.resourceIDNotFoundStrict;
}
else {
this.resourceNotFoundFunction = ResourceIDNotFound_1.resourceIDNotFoundWarn;
}
this.path = path.join(dataPath);
this.idSpaceHandler = new IDSpaceHandler_1.IDSpaceHandler(dataPath);
this.idSpace = this.idSpaceHandler.getIDSpace().catch((e) => {
// Suppress all promise rejections. These are instead thrown when specific resources are requested
//console.log("Got an error");
return e;
});
this.idSpace.catch((_e) => { });
this.shipPICTMap = this.makeShipPictMap();
this.weaponOutfitMap = this.makeWeaponOutfitMap();
this.shipParser = ShipParse_1.ShipParseClosure(this.shipPICTMap, this.weaponOutfitMap, this.idSpace);
// Holds spriteSheetMulti which gets split up
this.spriteSheetMultiGettable = this.makeGettable(ResourceHolderBase_1.NovaResourceType.rlëD, SpriteSheetMultiParse_1.SpriteSheetMultiParse);
// Since everything about a spriteSheet is parsed at once, it needs to be split up here
this.spriteSheetDataGettable = new Gettable_1.Gettable(this.getSpriteSheetData.bind(this));
this.spriteSheetImageGettable = new Gettable_1.Gettable(this.getSpriteSheetImage.bind(this));
this.spriteSheetFramesGettable = new Gettable_1.Gettable(this.getSpriteSheetFrames.bind(this));
// Similar for pict
this.pictMultiGettable = this.makeGettable(ResourceHolderBase_1.NovaResourceType.PICT, PictParse_1.PictImageMultiParse);
this.pictGettable = new Gettable_1.Gettable(this.getPictData.bind(this));
this.pictImageGettable = new Gettable_1.Gettable(this.getPictImage.bind(this));
this.ids = this.buildIDs();
this.data = this.buildData();
}
buildIDsForResource(resourceList) {
return Object.keys(resourceList);
}
buildIDs() {
return __awaiter(this, void 0, void 0, function* () {
var idSpace = yield this.idSpace;
if (idSpace instanceof Error) {
return NovaIDs_1.DefaultNovaIDs;
}
return {
Ship: this.buildIDsForResource(idSpace.shïp),
Outfit: this.buildIDsForResource(idSpace.oütf),
Weapon: this.buildIDsForResource(idSpace.wëap),
Pict: this.buildIDsForResource(idSpace.PICT),
PictImage: this.buildIDsForResource(idSpace.PICT),
Planet: this.buildIDsForResource(idSpace.spöb),
System: this.buildIDsForResource(idSpace.sÿst),
TargetCorners: [],
SpriteSheet: this.buildIDsForResource(idSpace.rlëD),
SpriteSheetImage: this.buildIDsForResource(idSpace.rlëD),
SpriteSheetFrames: this.buildIDsForResource(idSpace.rlëD),
StatusBar: this.buildIDsForResource(idSpace.ïntf),
Explosion: this.buildIDsForResource(idSpace.bööm)
};
});
}
// Assigns all the gettables to this.data
buildData() {
// This should really use NovaDataType.Ship etc but that isn't allowed when constructing like this.
var data = {
Ship: this.makeGettable(ResourceHolderBase_1.NovaResourceType.shïp, this.shipParser),
Outfit: this.makeGettable(ResourceHolderBase_1.NovaResourceType.oütf, OutfitParse_1.OutfitParse),
Weapon: this.makeGettable(ResourceHolderBase_1.NovaResourceType.wëap, WeaponParse_1.WeaponParse),
Pict: this.pictGettable,
PictImage: this.pictImageGettable,
Planet: this.makeGettable(ResourceHolderBase_1.NovaResourceType.spöb, PlanetParse_1.PlanetParse),
System: this.makeGettable(ResourceHolderBase_1.NovaResourceType.sÿst, SystemParse_1.SystemParse),
TargetCorners: this.makeGettable(ResourceHolderBase_1.NovaResourceType.cicn, TargetCornersParse_1.TargetCornersParse),
SpriteSheet: this.spriteSheetDataGettable,
SpriteSheetImage: this.spriteSheetImageGettable,
SpriteSheetFrames: this.spriteSheetFramesGettable,
StatusBar: this.makeGettable(ResourceHolderBase_1.NovaResourceType.ïntf, StatusBarParse_1.StatusBarParse),
Explosion: this.makeGettable(ResourceHolderBase_1.NovaResourceType.bööm, ExplosionParse_1.ExplosionParse)
};
return data;
}
makeGettable(resourceType, parseFunction) {
return new Gettable_1.Gettable((id) => __awaiter(this, void 0, void 0, function* () {
var idSpace = yield this.idSpace; // May be an error
if (idSpace instanceof Error) {
throw idSpace;
}
var resource = idSpace[resourceType][id];
// Shouldn't this just call resourceNotFoundFunction???
if (typeof resource === "undefined") {
throw new NovaDataInterface_1.NovaIDNotFoundError("NovaParse could not find " + resourceType + " of ID " + id + ".");
}
return yield parseFunction(resource, this.resourceNotFoundFunction);
}));
}
// shïps whose corresponding PICT does not exist
// use the PICT of the first shïp that had the same baseImage ID
makeShipPictMap() {
return __awaiter(this, void 0, void 0, function* () {
var idSpace = yield this.idSpace;
if (idSpace instanceof Error) {
return {};
}
// Maps shïp ids to their baseImage ids
var shipPICTMap = {};
// maps baseImage ids to pict ids
var baseImagePICTMap = {};
// Populate baseImagePICTMap
for (let shipGlobalID in idSpace.shïp) {
var ship = idSpace.shïp[shipGlobalID];
var pict = ship.idSpace.PICT[ship.pictID];
if (!pict) {
continue; // Ship has no corresponding pict, so don't set anything.
}
var shan = ship.idSpace.shän[ship.id];
if (!shan) {
this.resourceNotFoundFunction("shïp id " + ship.globalID + " missing shan");
continue; // If it's not found, there's no baseImage to map from
}
var baseImageLocalID = shan.images.baseImage.ID;
var baseImageGlobalID = shan.idSpace.rlëD[baseImageLocalID].globalID;
// Don't overwrite if it already exists. The first ship with the
// baseImage determines the PICT
if (!baseImagePICTMap[baseImageGlobalID]) {
// The base image corresponds to this pict.
baseImagePICTMap[baseImageGlobalID] = pict.globalID;
}
}
// Populate shipPICTMap
for (let shipGlobalID in idSpace.shïp) {
var ship = idSpace.shïp[shipGlobalID];
var pict = ship.idSpace.PICT[ship.pictID];
if (pict) {
// Then there is a pict for this ship.
// Set it in the map.
shipPICTMap[shipGlobalID] = pict.globalID;
}
else {
// No pict found for this ship, so look up the first
// ship's baseImage in the baseImagePICTMap
var shan = ship.idSpace.shän[ship.id];
var baseImageLocalID = shan.images.baseImage.ID;
var baseImageGlobalID = shan.idSpace.rlëD[baseImageLocalID].globalID;
shipPICTMap[shipGlobalID] = baseImagePICTMap[baseImageGlobalID];
}
}
return shipPICTMap;
});
}
makeWeaponOutfitMap() {
return __awaiter(this, void 0, void 0, function* () {
var idSpace = yield this.idSpace;
if (idSpace instanceof Error) {
return {};
}
// Maps a weapon to the first outfit that provides it.
var weaponOutfitMap = {};
for (let outfitID in idSpace.oütf) {
var outfit = yield this.data.Outfit.get(outfitID);
for (let weaponID in outfit.weapons) {
if (!(weaponOutfitMap[weaponID])) {
weaponOutfitMap[weaponID] = outfitID;
}
}
}
return weaponOutfitMap;
});
}
getSpriteSheetData(id) {
return __awaiter(this, void 0, void 0, function* () {
var multi = yield this.spriteSheetMultiGettable.get(id);
return multi.spriteSheet;
});
}
getSpriteSheetImage(id) {
return __awaiter(this, void 0, void 0, function* () {
var multi = yield this.spriteSheetMultiGettable.get(id);
return multi.spriteSheetImage;
});
}
getSpriteSheetFrames(id) {
return __awaiter(this, void 0, void 0, function* () {
var multi = yield this.spriteSheetMultiGettable.get(id);
return multi.spriteSheetFrames;
});
}
getPictData(id) {
return __awaiter(this, void 0, void 0, function* () {
var multi = yield this.pictMultiGettable.get(id);
return multi.pict;
});
}
getPictImage(id) {
return __awaiter(this, void 0, void 0, function* () {
var multi = yield this.pictMultiGettable.get(id);
return multi.image;
});
}
}
exports.NovaParse = NovaParse;
//# sourceMappingURL=NovaParse.js.map