sc4
Version:
A command line utility for automating SimCity 4 modding tasks & modifying savegames
39 lines (38 loc) • 1.2 kB
JavaScript
// # pointer.ts
// Small helper class that represents a pointer to a certain record in the
// subfile.
import { hex } from 'sc4/utils';
import { getClassType } from './helpers.js';
// # Pointer
export default class Pointer {
type;
address = 0x00000000;
constructor(objectOrType, address = 0x00000000) {
if (typeof objectOrType === 'number') {
this.type = objectOrType;
this.address = address;
}
else {
this.type = getClassType(objectOrType);
this.address = objectOrType.mem;
}
}
// ## get mem()
// Proxy to address for legacy purposes.
get mem() {
return this.address;
}
set mem(value) {
this.address = value;
}
// ## get [Symbol.toPrimitive](hint)
// Allows you to get the numerical value of the pointer by using +pointer.
[Symbol.toPrimitive](hint) {
return hint === 'number' ? this.address : hex(this.address);
}
// ## [inspect]()
// When logging this in the console, format as a pointer.
[Symbol.for('nodejs.util.inspect.custom')]() {
return `Pointer(\x1B[32m${hex(this.address)}\x1B[39m, ${hex(this.type)})`;
}
}