unreal.js
Version:
A pak reader for games like VALORANT & Fortnite written in Node.JS
189 lines (188 loc) • 6.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResolvedLoadedObject = exports.ResolvedObject = exports.Package = void 0;
const UObject_1 = require("./exports/UObject");
const Lazy_1 = require("../../util/Lazy");
const VersionContainer_1 = require("../versions/VersionContainer");
const FName_1 = require("../objects/uobject/FName");
/**
* UE4 Package
* @abstract
* @extends {UObject}
*/
class Package extends UObject_1.UObject {
/**
* Creates an instnace
* @param {string} fileName Name of file
* @param {FileProvider} provider File provider
* @param {VersionContainer} versions Game which is used
* @constructor
* @protected
*/
constructor(fileName, provider, versions) {
super();
/**
* File provider
* @type {FileProvider}
* @public
*/
this.provider = null;
/**
* Game which is used
* @type {VersionContainer}
* @public
*/
this.versions = this.provider?.versions || VersionContainer_1.VersionContainer.DEFAULT;
/**
* Package flags
* @type {number}
* @public
*/
this.packageFlags = 0;
this.fileName = fileName;
this.provider = provider;
this.versions = versions;
}
/**
* Returns exports
* @type {Array<UObject>}
* @public
*/
get exports() {
return this.exportsLazy.map(it => it.value);
}
/**
* Constructs an export from UStruct
* @param {UStruct} struct Struct to use
* @returns {UObject} Constructed export
* @protected
*/
static constructExport(struct) {
let current = struct;
while (current != null) {
const c = current?.structClass;
if (c) {
const nc = new c.constructor();
nc.clazz = struct;
return nc;
}
current = current.superStruct?.value;
}
const u = new UObject_1.UObject();
u.clazz = struct;
return u;
}
/**
* Gets an export of specific type
* @param {Function} type The class object which is either UObject or extends it
* @returns {any} the first export of the given type
* @throws {TypeError} if there is no export of the given type
* @example getExportOfType(CharacterAbilityUIData)
* @public
*/
getExportOfType(type) {
const obj = this.getExportsOfType(type)[0];
if (obj)
return obj;
throw new TypeError(`Could not find export of type '${type.name}'`);
}
/**
* Gets an export of specific type
* @param {Function} type The class object which is either UObject or extends it
* @returns {?any} the first export of the given type or null
* @example getExportOfTypeOrNull(CharacterAbilityUIData)
* @public
*/
getExportOfTypeOrNull(type) {
return this.getExportsOfType(type)[0] || null;
}
/**
* Gets an exports of specific type
* @param {Function} type The class object which is either UObject or extends it
* @returns {any[]} the first export of the given type or null
* @example getExportsOfType(CharacterAbilityUIData)
* @public
*/
getExportsOfType(type) {
return this.exports.filter(e => e instanceof type);
}
/**
* Loads an object by index
* @param {FPackageIndex} index Index to find
* @returns {?any} Object or null
* @public
*/
loadObject(index) {
return this.findObject(index)?.value;
}
}
exports.Package = Package;
/**
* Resolved Object
*/
class ResolvedObject {
constructor(pkg, exportIndex = -1) {
this.pkg = pkg;
this.exportIndex = exportIndex;
}
getOuter() {
return null;
}
getClazz() {
return null;
}
getSuper() {
return null;
}
getObject() {
return null;
}
getFullName0(includePackageName = true, includeClassPackage = false) {
return this.getFullName1(includePackageName, "", includeClassPackage);
}
getFullName1(includePackageName, resultString, includeClassPackage) {
if (includeClassPackage)
resultString += this.getClazz()?.getPathName0();
else
resultString += this.getClazz()?.name;
resultString += " ";
return this.getPathName1(includePackageName, resultString);
}
getPathName0(includePackageName = true) {
return this.getPathName1(includePackageName, "");
}
getPathName1(includePackageName, resultString) {
const objOuter = this.getOuter();
if (objOuter != null) {
const objOuterOuter = objOuter.getOuter();
if (objOuterOuter != null || includePackageName) {
resultString = objOuter.getPathName1(includePackageName, resultString);
// SUBOBJECT_DELIMITER_CHAR is used to indicate that this object's outer is not a UPackage
resultString += objOuterOuter != null && objOuterOuter.getOuter() == null ? ":" : ".";
}
}
return resultString + this.name;
}
}
exports.ResolvedObject = ResolvedObject;
class ResolvedLoadedObject extends ResolvedObject {
constructor(obj) {
super(obj instanceof Package ? obj : obj.owner);
this.obj = obj;
}
get name() {
return FName_1.FName.dummy(this.obj.name);
}
getOuter() {
const it = this.obj.outer;
return it != null ? new ResolvedLoadedObject(it) : null;
}
getClazz() {
const it = this.obj.clazz;
return it != null ? new ResolvedLoadedObject(it) : null;
}
getObject() {
return new Lazy_1.Lazy(() => this.obj);
}
}
exports.ResolvedLoadedObject = ResolvedLoadedObject;