Skip to content

Commit

Permalink
feat: cache config indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraviolet-jordan committed Jan 8, 2024
1 parent 9c9856d commit f7b4826
Show file tree
Hide file tree
Showing 14 changed files with 338 additions and 170 deletions.
54 changes: 27 additions & 27 deletions src/js/client.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import SeqType from './jagex2/config/SeqType.js';
import LocType from './jagex2/config/LocType.js';
import FloType from './jagex2/config/FloType.js';
import ObjType from './jagex2/config/ObjType.js';
import NpcType from './jagex2/config/NpcType.js';
import IdkType from './jagex2/config/IdkType.js';
import SpotAnimType from './jagex2/config/SpotAnimType.js';
import VarpType from './jagex2/config/VarpType.js';
import ComType from './jagex2/config/ComType.js';

import PixMap from './jagex2/graphics/PixMap.js';
import Draw2D from './jagex2/graphics/Draw2D.js';
import Draw3D from './jagex2/graphics/Draw3D.js';
import Pix8 from './jagex2/graphics/Pix8.js';
import Pix24 from './jagex2/graphics/Pix24.js';
import PixFont from './jagex2/graphics/PixFont.js';
import Model from './jagex2/graphics/Model.js';
import SeqBase from './jagex2/graphics/SeqBase.js';
import SeqFrame from './jagex2/graphics/SeqFrame.js';

import Jagfile from './jagex2/io/Jagfile.js';

import WordFilter from './jagex2/wordenc/WordFilter.js';
import {arraycopy, decompressBz2, downloadUrl, sleep} from './jagex2/util/JsUtil.js';
import SeqType from './jagex2/config/SeqType';
import LocType from './jagex2/config/LocType';
import FloType from './jagex2/config/FloType';
import ObjType from './jagex2/config/ObjType';
import NpcType from './jagex2/config/NpcType';
import IdkType from './jagex2/config/IdkType';
import SpotAnimType from './jagex2/config/SpotAnimType';
import VarpType from './jagex2/config/VarpType';
import ComType from './jagex2/config/ComType';

import PixMap from './jagex2/graphics/PixMap';
import Draw2D from './jagex2/graphics/Draw2D';
import Draw3D from './jagex2/graphics/Draw3D';
import Pix8 from './jagex2/graphics/Pix8';
import Pix24 from './jagex2/graphics/Pix24';
import PixFont from './jagex2/graphics/PixFont';
import Model from './jagex2/graphics/Model';
import SeqBase from './jagex2/graphics/SeqBase';
import SeqFrame from './jagex2/graphics/SeqFrame';

import Jagfile from './jagex2/io/Jagfile';

import WordFilter from './jagex2/wordenc/WordFilter';
import {arraycopy, decompressBz2, downloadUrl, sleep} from './jagex2/util/JsUtil';
import {playMidi} from './jagex2/util/AudioUtil.js';
import GameShell from './jagex2/client/GameShell.js';
import GameShell from './jagex2/client/GameShell';

import './vendor/midi.js';
import Packet from './jagex2/io/Packet.js';
import Packet from './jagex2/io/Packet';
import Wave from './jagex2/sound/Wave';
import JString from './jagex2/datastruct/JString';
import World3D from './jagex2/dash3d/World3D';
Expand Down Expand Up @@ -400,7 +400,7 @@ class Client extends GameShell {
SeqType.unpack(config);
LocType.unpack(config);
FloType.unpack(config);
ObjType.unpack(config, Client.MEMBERS);
ObjType.unpack(config);
NpcType.unpack(config);
IdkType.unpack(config);
SpotAnimType.unpack(config);
Expand Down
12 changes: 3 additions & 9 deletions src/js/jagex2/config/ConfigType.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import Packet from '../io/Packet';

export abstract class ConfigType {
readonly index: number;
abstract decode(index: number, code: number, dat: Packet): void;

constructor(index: number) {
this.index = index;
}

abstract decode(code: number, dat: Packet): void;

decodeType = (dat: Packet): void => {
decodeType = (index: number, dat: Packet): void => {
// eslint-disable-next-line no-constant-condition
while (true) {
const opcode: number = dat.g1;
if (opcode === 0) {
break;
}
this.decode(opcode, dat);
this.decode(index, opcode, dat);
}
};
}
6 changes: 3 additions & 3 deletions src/js/jagex2/config/FloType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export default class FloType extends ConfigType {
const dat = new Packet(config.read('flo.dat'));
this.count = dat.g2;
for (let i = 0; i < this.count; i++) {
this.instances[i] = new FloType(i);
this.instances[i].decodeType(dat);
this.instances[i] = new FloType();
this.instances[i].decodeType(i, dat);
}
};

Expand All @@ -25,7 +25,7 @@ export default class FloType extends ConfigType {
occludes: boolean = true;
name: string | null = null;

decode = (code: number, dat: Packet): void => {
decode = (_index: number, code: number, dat: Packet): void => {
if (code === 1) {
this.rgb = dat.g3;
} else if (code === 2) {
Expand Down
6 changes: 3 additions & 3 deletions src/js/jagex2/config/IdkType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export default class IdkType extends ConfigType {
const dat = new Packet(config.read('idk.dat'));
this.count = dat.g2;
for (let i = 0; i < this.count; i++) {
this.instances[i] = new IdkType(i);
this.instances[i].decodeType(dat);
this.instances[i] = new IdkType();
this.instances[i].decodeType(i, dat);
}
};

Expand All @@ -26,7 +26,7 @@ export default class IdkType extends ConfigType {
recol_d: Uint16Array = new Uint16Array(6);
disable: boolean = false;

decode = (code: number, dat: Packet): void => {
decode = (_index: number, code: number, dat: Packet): void => {
if (code === 1) {
this.type = dat.g1;
} else if (code === 2) {
Expand Down
100 changes: 80 additions & 20 deletions src/js/jagex2/config/LocType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,59 @@ import Packet from '../io/Packet';

export default class LocType extends ConfigType {
static count: number = 0;
static instances: LocType[] = [];
static cache: Array<LocType> | null = null;
static dat: Packet | null = null;
static offsets: Int32Array | null = null;
static cachePos: number = 0;

static unpack = (config: Jagfile): void => {
const dat = new Packet(config.read('loc.dat'));
this.count = dat.g2;
for (let i = 0; i < this.count; i++) {
const type = new LocType(i);
this.instances[i] = type;
type.decodeType(dat);

if (!type.shapes) {
type.shapes = new Uint8Array(0);
}
this.dat = new Packet(config.read('loc.dat'));
const idx = new Packet(config.read('loc.idx'));

this.count = idx.g2;
this.offsets = new Int32Array(this.count);

let offset = 2;
for (let id = 0; id < this.count; id++) {
this.offsets[id] = offset;
offset += idx.g2;
}

this.cache = new Array(10);
for (let id = 0; id < 10; id++) {
this.cache[id] = new LocType();
}
};

if (type.active === -1) {
type.active = type.shapes.length > 0 && type.shapes[0] == 10 ? 1 : 0;
static get = (id: number): LocType => {
if (!this.cache || !this.offsets || !this.dat) {
throw new Error('LocType not loaded!!!');
}

if (type.ops.length > 0) {
type.active = 1;
}
for (let id = 0; id < 10; id++) {
if (this.cache[id].index == id) {
return this.cache[id];
}
}

this.cachePos = (this.cachePos + 1) % 10;
const loc: LocType = this.cache[this.cachePos];
this.dat.pos = this.offsets[id];
loc.index = id;
loc.reset();
loc.decodeType(id, this.dat);
return loc;
};

static get = (id: number): LocType => LocType.instances[id];
static unload = (): void => {
this.offsets = null;
this.cache = null;
this.dat = null;
};

// ----

index: number = -1;
models: Uint16Array | null = null;
shapes: Uint8Array | null = null;
name: string | null = null;
Expand All @@ -47,7 +72,7 @@ export default class LocType extends ConfigType {
sharelight: boolean = false;
occlude: boolean = false;
anim: number = -1;
hasalpha: boolean = false;
disposeAlpha: boolean = false;
walloff: number = 16;
ambient: number = 0;
contrast: number = 0;
Expand All @@ -65,7 +90,7 @@ export default class LocType extends ConfigType {
zoff: number = 0;
forcedecor: boolean = false;

decode = (code: number, dat: Packet): void => {
decode = (_index: number, code: number, dat: Packet): void => {
if (code === 1) {
const count = dat.g1;
this.models = new Uint16Array(count);
Expand Down Expand Up @@ -102,7 +127,7 @@ export default class LocType extends ConfigType {
this.anim = -1;
}
} else if (code === 25) {
this.hasalpha = true;
this.disposeAlpha = true;
} else if (code === 28) {
this.walloff = dat.g1;
} else if (code === 29) {
Expand Down Expand Up @@ -152,4 +177,39 @@ export default class LocType extends ConfigType {
throw new Error(`Unrecognized loc config code: ${code}`);
}
};

private reset = (): void => {
this.models = null;
this.shapes = null;
this.name = null;
this.desc = null;
this.recol_s = null;
this.recol_d = null;
this.width = 1;
this.length = 1;
this.blockwalk = true;
this.blockrange = true;
this.active = 0;
this.hillskew = false;
this.sharelight = false;
this.occlude = false;
this.anim = -1;
this.walloff = 16;
this.ambient = 0;
this.contrast = 0;
this.ops = [];
this.disposeAlpha = false;
this.mapfunction = -1;
this.mapscene = -1;
this.mirror = false;
this.shadow = true;
this.resizex = 128;
this.resizey = 128;
this.resizez = 128;
this.forceapproach = 0;
this.xoff = 0;
this.yoff = 0;
this.zoff = 0;
this.forcedecor = false;
};
}
53 changes: 45 additions & 8 deletions src/js/jagex2/config/NpcType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,58 @@ import Packet from '../io/Packet';

export default class NpcType extends ConfigType {
static count: number = 0;
static instances: NpcType[] = [];
static cache: Array<NpcType> | null = null;
static dat: Packet | null = null;
static offsets: Int32Array | null = null;
static cachePos: number = 0;

static unpack = (config: Jagfile): void => {
const dat = new Packet(config.read('npc.dat'));
this.count = dat.g2;
for (let i = 0; i < this.count; i++) {
this.instances[i] = new NpcType(i);
this.instances[i].decodeType(dat);
this.dat = new Packet(config.read('npc.dat'));
const idx = new Packet(config.read('npc.idx'));

this.count = idx.g2;
this.offsets = new Int32Array(this.count);

let offset = 2;
for (let id = 0; id < this.count; id++) {
this.offsets[id] = offset;
offset += idx.g2;
}

this.cache = new Array(20);
for (let id = 0; id < 20; id++) {
this.cache[id] = new NpcType();
}
};

static get = (id: number): NpcType => {
if (!this.cache || !this.offsets || !this.dat) {
throw new Error('LocType not loaded!!!');
}

for (let id = 0; id < 20; id++) {
if (this.cache[id].index == id) {
return this.cache[id];
}
}

this.cachePos = (this.cachePos + 1) % 20;
const loc: NpcType = (this.cache[this.cachePos] = new NpcType());
this.dat.pos = this.offsets[id];
loc.index = id;
loc.decodeType(id, this.dat);
return loc;
};

static get = (id: number): NpcType => NpcType.instances[id];
static unload = (): void => {
this.offsets = null;
this.cache = null;
this.dat = null;
};

// ----

index: number = -1;
name: string | null = null;
desc: string | null = null;
size: number = 1;
Expand All @@ -42,7 +79,7 @@ export default class NpcType extends ConfigType {
resizeh: number = 128;
resizev: number = 128;

decode = (code: number, dat: Packet): void => {
decode = (_index: number, code: number, dat: Packet): void => {
if (code === 1) {
const count = dat.g1;
this.models = new Uint16Array(count);
Expand Down
Loading

0 comments on commit f7b4826

Please sign in to comment.