sc4
Version:
A command line utility for automating SimCity 4 modding tasks & modifying savegames
56 lines (55 loc) • 1.63 kB
JavaScript
import { hex, randomId, inspect } from 'sc4/utils';
export default class TGI {
type = 0x00000000;
group = 0x00000000;
instance = 0x00000000;
// Generates a random tgi, useful for testing.
static random(type = randomId(), group = randomId(), instance = randomId()) {
return new TGI(type, group, instance);
}
constructor(tgiOrType = 0, group = 0, instance = 0) {
let type;
if (Array.isArray(tgiOrType)) {
[type, group, instance] = tgiOrType;
}
else if (typeof tgiOrType === 'number') {
type = tgiOrType;
}
else {
({ type, group, instance } = tgiOrType);
}
this.type = type;
this.group = group;
this.instance = instance;
}
[Symbol.for('nodejs.util.inspect.custom')](_level, opts, nodeInspect) {
let str = [...this]
.map(nr => nodeInspect(inspect.hex(nr), opts))
.join(', ');
return `TGI(${str})`;
}
// ## toString()
toString() {
return [...this].map(nr => hex(nr)).join(',');
}
// ## toArray()
toArray() {
return [this.type, this.group, this.instance];
}
// ## toBigInt()
// Converts the TGI to a BigInt, which can be usful for hashing it.
toBigInt() {
return BigInt(this.type) << 64n
| BigInt(this.group) << 32n
| BigInt(this.instance);
}
// ## map()
map(...args) {
return this.toArray().map(...args);
}
*[Symbol.iterator]() {
yield this.type;
yield this.group;
yield this.instance;
}
}