Skip to content

Commit

Permalink
fix: linklist not ported properly from java
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraviolet-jordan committed Feb 2, 2025
1 parent 1d6af12 commit 3791b91
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 103 deletions.
12 changes: 12 additions & 0 deletions src/js/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,18 @@ export class Game extends Client {
}
y += 13;
this.fontPlain11?.drawStringRight(x, y, `Kit: ${Renderer.renderer ? 'WebGPU' : 'CPU'}`, Colors.YELLOW, true);

// y += 13;
// let memoryUsage = -1;
// if (typeof window.performance['memory' as keyof Performance] !== 'undefined') {
// const memory = window.performance['memory' as keyof Performance] as any;
// memoryUsage = (memory.usedJSHeapSize / 1024) | 0;
// }
//
// if (memoryUsage !== -1) {
// this.fontPlain12?.drawStringRight(x, y, 'Mem:' + memoryUsage + "k", Colors.YELLOW);
// }

y += 13;
this.fontPlain11?.drawStringRight(x, y, `Fps: ${this.fps}, ${this.deltime} ms`, Colors.YELLOW, true);
y += 13;
Expand Down
32 changes: 32 additions & 0 deletions src/js/jagex2/datastruct/DoublyLinkList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import DoublyLinkable from './DoublyLinkable';

export default class DoublyLinkList {
readonly head: DoublyLinkable = new DoublyLinkable();

constructor() {
this.head.next2 = this.head;
this.head.prev2 = this.head;
}

push(node: DoublyLinkable): void {
if (node.prev2) {
node.unlink2();
}
node.prev2 = this.head.prev2;
node.next2 = this.head;
if (node.prev2) {
node.prev2.next2 = node;
}
node.next2.prev2 = node;
}

pop(): DoublyLinkable | null {
const node: DoublyLinkable | null = this.head.next2;
if (node === this.head) {
return null;
} else {
node?.unlink2();
return node;
}
}
}
18 changes: 18 additions & 0 deletions src/js/jagex2/datastruct/DoublyLinkable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Linkable from './Linkable';

export default class DoublyLinkable extends Linkable {
// constructor
next2: DoublyLinkable | null = null;
prev2: DoublyLinkable | null = null;

unlink2(): void {
if (this.prev2 !== null) {
this.prev2.next2 = this.next2;
if (this.next2) {
this.next2.prev2 = this.prev2;
}
this.next2 = null;
this.prev2 = null;
}
}
}
23 changes: 0 additions & 23 deletions src/js/jagex2/datastruct/Hashable.ts

This file was deleted.

8 changes: 3 additions & 5 deletions src/js/jagex2/datastruct/LinkList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import Linkable from './Linkable';

export default class LinkList {
// constructor
private readonly sentinel: Linkable;
private readonly sentinel: Linkable = new Linkable();

// runtime
private cursor: Linkable | null = null;

constructor() {
const head: Linkable = new Linkable();
head.next = head;
head.prev = head;
this.sentinel = head;
this.sentinel.next = this.sentinel;
this.sentinel.prev = this.sentinel;
}

addTail(node: Linkable): void {
Expand Down
25 changes: 10 additions & 15 deletions src/js/jagex2/datastruct/Linkable.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
export default class Linkable {
key: bigint;
next: Linkable | null;
prev: Linkable | null;

constructor() {
this.key = 0n;
this.next = this;
this.prev = this;
}
key: bigint = 0n;
next: Linkable | null = null;
prev: Linkable | null = null;

unlink(): void {
if (!this.prev || !this.next) {
return;
if (this.prev != null) {
this.prev.next = this.next;
if (this.next) {
this.next.prev = this.prev;
}
this.next = null;
this.prev = null;
}
this.prev.next = this.next;
this.next.prev = this.prev;
this.next = null;
this.prev = null;
}
}
35 changes: 18 additions & 17 deletions src/js/jagex2/datastruct/LruCache.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import Stack from './Stack';
import DoublyLinkList from './DoublyLinkList';
import HashTable from './HashTable';
import Hashable from './Hashable';
import DoublyLinkable from './DoublyLinkable';

export default class LruCache {
// constructor
readonly capacity: number;
readonly hashtable: HashTable;
readonly history: Stack;
readonly hashtable: HashTable = new HashTable(1024);
readonly history: DoublyLinkList = new DoublyLinkList();
available: number;

constructor(size: number) {
this.capacity = size;
this.available = size;
this.hashtable = new HashTable(1024);
this.history = new Stack();
}

get(key: bigint): Hashable | null {
const node: Hashable | null = this.hashtable.get(key) as Hashable | null;
get(key: bigint): DoublyLinkable | null {
const node: DoublyLinkable | null = this.hashtable.get(key) as DoublyLinkable | null;
if (node) {
this.history.push(node);
}
return node;
}

put(key: bigint, value: Hashable): void {
put(key: bigint, value: DoublyLinkable): void {
if (this.available === 0) {
const node: Hashable | null = this.history.pop();
const node: DoublyLinkable | null = this.history.pop();
node?.unlink();
node?.uncache();
node?.unlink2();
} else {
this.available--;
}
Expand All @@ -37,12 +35,15 @@ export default class LruCache {
}

clear(): void {
const node: Hashable | null = this.history.pop();
if (!node) {
this.available = this.capacity;
return;
// eslint-disable-next-line no-constant-condition
while (true) {
const node: DoublyLinkable | null = this.history.pop();
if (!node) {
this.available = this.capacity;
return;
}
node.unlink();
node.unlink2();
}
node.unlink();
node.uncache();
}
}
31 changes: 0 additions & 31 deletions src/js/jagex2/datastruct/Stack.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/js/jagex2/graphics/Draw2D.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Hashable from '../datastruct/Hashable';
import DoublyLinkable from '../datastruct/DoublyLinkable';

export default class Draw2D extends Hashable {
export default class Draw2D extends DoublyLinkable {
static pixels: Int32Array = new Int32Array();

static width2d: number = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/js/jagex2/graphics/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Draw3D from './Draw3D';
import AnimFrame from './AnimFrame';
import AnimBase from './AnimBase';

import Hashable from '../datastruct/Hashable';
import DoublyLinkable from '../datastruct/DoublyLinkable';
import {Int32Array2d, TypedArray1d} from '../util/Arrays';
import {LostLiteConfiguration} from '../../lostlite/LostLiteConfiguration';

Expand Down Expand Up @@ -77,7 +77,7 @@ type ModelType = {
vertexNormalOriginal?: (VertexNormal | null)[] | null;
};

export default class Model extends Hashable {
export default class Model extends DoublyLinkable {
static metadata: (Metadata | null)[] | null = null;

static head: Packet | null = null;
Expand Down
4 changes: 2 additions & 2 deletions src/js/jagex2/graphics/Pix24.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import Draw2D from './Draw2D';
import Jagfile from '../io/Jagfile';
import Packet from '../io/Packet';

import Hashable from '../datastruct/Hashable';
import DoublyLinkable from '../datastruct/DoublyLinkable';

import {decodeJpeg} from './Jpeg';
import Pix8 from './Pix8';

export default class Pix24 extends Hashable {
export default class Pix24 extends DoublyLinkable {
// constructor
readonly pixels: Int32Array;
readonly width: number;
Expand Down
4 changes: 2 additions & 2 deletions src/js/jagex2/graphics/Pix8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Draw2D from './Draw2D';

import Jagfile from '../io/Jagfile';
import Packet from '../io/Packet';
import Hashable from '../datastruct/Hashable';
import DoublyLinkable from '../datastruct/DoublyLinkable';

// identical to Pix24 except the image is indexed by a palette
export default class Pix8 extends Hashable {
export default class Pix8 extends DoublyLinkable {
// constructor
pixels: Int8Array;
width: number;
Expand Down
4 changes: 2 additions & 2 deletions src/js/jagex2/graphics/PixFont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import Draw2D from './Draw2D';

import Jagfile from '../io/Jagfile';
import Packet from '../io/Packet';
import Hashable from '../datastruct/Hashable';
import DoublyLinkable from '../datastruct/DoublyLinkable';
import JavaRandom from '../util/JavaRandom';
import Colors from './Colors';
import {Renderer} from '../renderer/Renderer';

export default class PixFont extends Hashable {
export default class PixFont extends DoublyLinkable {
static readonly CHARSET: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!"£$%^&*()-_=+[{]};:\'@#~,<.>/?\\| ';
static readonly CHARCODESET: number[] = [];

Expand Down
4 changes: 2 additions & 2 deletions src/js/jagex2/io/Packet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {bigIntModPow, bigIntToBytes, bytesToBigInt} from '../util/JsUtil';
import Isaac from './Isaac';
import LinkList from '../datastruct/LinkList';
import Hashable from '../datastruct/Hashable';
import DoublyLinkable from '../datastruct/DoublyLinkable';

export default class Packet extends Hashable {
export default class Packet extends DoublyLinkable {
private static readonly CRC32_POLYNOMIAL: number = 0xedb88320;

private static readonly crctable: Int32Array = new Int32Array(256);
Expand Down

0 comments on commit 3791b91

Please sign in to comment.