diff --git a/.eslintignore b/.eslintignore index c523dda4..a78a8a49 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,3 @@ node_modules -public \ No newline at end of file +public +src/js/vendor \ No newline at end of file diff --git a/.prettierrc b/.prettierrc index 2195a1f6..4568fe43 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,12 +1,11 @@ -{ - "printWidth": 120, - "tabWidth": 2, - "semi": true, - "singleQuote": true, - "quoteProps": "as-needed", - "jsxSingleQuote": false, - "trailingComma": "none", - "bracketSpacing": false, - "arrowParens": "avoid", - "endOfLine": "auto" +{ + "printWidth": 200, + "tabWidth": 4, + "semi": true, + "singleQuote": true, + "quoteProps": "as-needed", + "trailingComma": "none", + "bracketSpacing": false, + "arrowParens": "avoid", + "endOfLine": "auto" } \ No newline at end of file diff --git a/package.json b/package.json index dc0146a0..9ea2854d 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "build:prod": "webpack --mode=production --node-env=production", "dev": "webpack-dev-server --hot --watch", "lint": "eslint . --ext .ts", - "lint:fix": "eslint . --ext .ts --fix" + "lint:fix": "eslint . --ext .ts --fix", + "format": "prettier src/**/*.{js,ts} --write" }, "repository": { "type": "git", @@ -56,7 +57,7 @@ }, "lint-staged": { "src/**/*.{js,ts}": [ - "prettier --write", + "npm run format", "npm run lint:fix", "git add" ] diff --git a/src/js/client.ts b/src/js/client.ts index aa07a9c3..b860aa6b 100644 --- a/src/js/client.ts +++ b/src/js/client.ts @@ -89,7 +89,6 @@ class Client extends GameShell { private flameBuffer3: number[] = []; private flameBuffer2: number[] = []; - private flameGradient: number[] = []; private flameGradient0: number[] = []; private flameGradient1: number[] = []; @@ -111,7 +110,7 @@ class Client extends GameShell { } }, 35); await super.run(); - } + }; load = async (): Promise => { if (this.alreadyStarted) { @@ -206,7 +205,6 @@ class Client extends GameShell { // TODO } else { await this.drawTitleScreen(); - } }; @@ -229,15 +227,15 @@ class Client extends GameShell { const y = 200; const offsetY = 20; - this.fontBold12?.drawStringCenter(x / 2, (y / 2) - offsetY - 26, 'RuneScape is loading - please wait...', 0xFFFFFF); - const midY = (y / 2) - 18 - offsetY; + this.fontBold12?.drawStringCenter(x / 2, y / 2 - offsetY - 26, 'RuneScape is loading - please wait...', 0xffffff); + const midY = y / 2 - 18 - offsetY; - Draw2D.drawRect((x / 2) - 152, midY, 304, 34, 0x8c1111); - Draw2D.drawRect((x / 2) - 151, midY + 1, 302, 32, 0x000000); - Draw2D.fillRect((x / 2) - 150, midY + 2, progress * 3, 30, 0x8c1111); - Draw2D.fillRect(((x / 2) - 150) + (progress * 3), midY + 2, 300 - (progress * 3), 30, 0x000000); + Draw2D.drawRect(x / 2 - 152, midY, 304, 34, 0x8c1111); + Draw2D.drawRect(x / 2 - 151, midY + 1, 302, 32, 0x000000); + Draw2D.fillRect(x / 2 - 150, midY + 2, progress * 3, 30, 0x8c1111); + Draw2D.fillRect(x / 2 - 150 + progress * 3, midY + 2, 300 - progress * 3, 30, 0x000000); - this.fontBold12?.drawStringCenter(x / 2, (y / 2) + 5 - offsetY, str, 0xFFFFFF); + this.fontBold12?.drawStringCenter(x / 2, y / 2 + 5 - offsetY, str, 0xffffff); this.imageTitle4?.draw(214, 186); if (this.redrawTitleBackground) { @@ -371,7 +369,7 @@ class Client extends GameShell { const logo = Pix24.fromArchive(this.titleArchive, 'logo'); this.imageTitle2?.bind(); - logo.draw((this.width / 2) - (logo.width / 2) - 128, 18); + logo.draw(this.width / 2 - logo.width / 2 - 128, 18); }; private updateFlameBuffer = (image: Pix8 | null): void => { @@ -417,7 +415,7 @@ class Client extends GameShell { } } } - } + }; private loadTitleImages = (): void => { if (!this.titleArchive) { @@ -431,7 +429,7 @@ class Client extends GameShell { this.imageFlamesLeft = new Pix24(128, 265); this.imageFlamesRight = new Pix24(128, 265); - if (this.imageTitle0) arraycopy(this.imageTitle0.pixels, 0, this.imageFlamesLeft.pixels, 0, 33920) + if (this.imageTitle0) arraycopy(this.imageTitle0.pixels, 0, this.imageFlamesLeft.pixels, 0, 33920); if (this.imageTitle1) arraycopy(this.imageTitle1.pixels, 0, this.imageFlamesRight.pixels, 0, 33920); this.flameGradient0 = []; @@ -484,7 +482,7 @@ class Client extends GameShell { if (!this.flameActive) this.flameActive = true; this.showProgress(10, 'Connecting to fileserver').then(() => { - console.log('Finished loading.') + console.log('Finished loading.'); }); }; @@ -606,58 +604,58 @@ class Client extends GameShell { if (this.titleScreenState === 0) { let x = w / 2; - let y = (h / 2) - 20; - this.fontBold12?.drawStringTaggableCenter(x, y, 'Welcome to RuneScape', 0xFFFFFF00, true); + let y = h / 2 - 20; + this.fontBold12?.drawStringTaggableCenter(x, y, 'Welcome to RuneScape', 0xffffff00, true); - x = (w / 2) - 80; - y = (h / 2) + 20; + x = w / 2 - 80; + y = h / 2 + 20; this.imageTitleButton?.draw(x - 73, y - 20); - this.fontBold12?.drawStringTaggableCenter(x, y + 5, 'New user', 0xFFFFFFFF, true); + this.fontBold12?.drawStringTaggableCenter(x, y + 5, 'New user', 0xffffffff, true); - x = (w / 2) + 80; + x = w / 2 + 80; this.imageTitleButton?.draw(x - 73, y - 20); - this.fontBold12?.drawStringTaggableCenter(x, y + 5, 'Existing User', 0xFFFFFFFF, true); + this.fontBold12?.drawStringTaggableCenter(x, y + 5, 'Existing User', 0xffffffff, true); } else if (this.titleScreenState === 2) { let x = w / 2 - 80; let y = h / 2 - 40; if (this.loginMessage0.length === 0) { - this.fontBold12?.drawStringTaggableCenter(w / 2, y - 15, this.loginMessage0, 0xFFFF00, true); - this.fontBold12?.drawStringTaggableCenter(w / 2, y, this.loginMessage1, 0xFFFF00, true); + this.fontBold12?.drawStringTaggableCenter(w / 2, y - 15, this.loginMessage0, 0xffff00, true); + this.fontBold12?.drawStringTaggableCenter(w / 2, y, this.loginMessage1, 0xffff00, true); y += 30; } else { - this.fontBold12?.drawStringTaggableCenter(w / 2, y - 7, this.loginMessage1, 0xFFFF00, true); + this.fontBold12?.drawStringTaggableCenter(w / 2, y - 7, this.loginMessage1, 0xffff00, true); y += 30; } - this.fontBold12?.drawStringTaggable(w / 2 - 90, y, `Username: ${this.username}${(this.titleLoginField == 0 && this.loopCycle % 40 < 20) ? '@yel@|' : ''}`, 0xFFFFFF, true); + this.fontBold12?.drawStringTaggable(w / 2 - 90, y, `Username: ${this.username}${this.titleLoginField == 0 && this.loopCycle % 40 < 20 ? '@yel@|' : ''}`, 0xffffff, true); y += 15; - this.fontBold12?.drawStringTaggable(w / 2 - 88, y, `Password: ${this.password}${(this.titleLoginField == 1 && this.loopCycle % 40 < 20) ? '@yel@|' : ''}`, 0xFFFFFF, true); + this.fontBold12?.drawStringTaggable(w / 2 - 88, y, `Password: ${this.password}${this.titleLoginField == 1 && this.loopCycle % 40 < 20 ? '@yel@|' : ''}`, 0xffffff, true); // x = w / 2 - 80; dead code y = h / 2 + 50; this.imageTitleButton?.draw(x - 73, y - 20); - this.fontBold12?.drawStringTaggableCenter(x, y + 5, 'Login', 0xFFFFFF, true); + this.fontBold12?.drawStringTaggableCenter(x, y + 5, 'Login', 0xffffff, true); x = w / 2 + 80; this.imageTitleButton?.draw(x - 73, y - 20); - this.fontBold12?.drawStringTaggableCenter(x, y + 5, 'Cancel', 0xFFFFFF, true); + this.fontBold12?.drawStringTaggableCenter(x, y + 5, 'Cancel', 0xffffff, true); } else if (this.titleScreenState == 3) { this.fontBold12?.drawStringTaggableCenter(w / 2, 16776960, 'Create a free account', h / 2 - 60, true); const x = w / 2; let y = h / 2 - 35; - this.fontBold12?.drawStringTaggableCenter(w / 2, y, 'To create a new account you need to', 0xFFFFFF, true); + this.fontBold12?.drawStringTaggableCenter(w / 2, y, 'To create a new account you need to', 0xffffff, true); y += 15; - this.fontBold12?.drawStringTaggableCenter(w / 2, y, 'go back to the main RuneScape webpage', 0xFFFFFF, true); + this.fontBold12?.drawStringTaggableCenter(w / 2, y, 'go back to the main RuneScape webpage', 0xffffff, true); y += 15; - this.fontBold12?.drawStringTaggableCenter(w / 2, y, "and choose the red 'create account'", 0xFFFFFF, true); + this.fontBold12?.drawStringTaggableCenter(w / 2, y, "and choose the red 'create account'", 0xffffff, true); y += 15; - this.fontBold12?.drawStringTaggableCenter(w / 2, y, 'button at the top right of that page.', 0xFFFFFF, true); + this.fontBold12?.drawStringTaggableCenter(w / 2, y, 'button at the top right of that page.', 0xffffff, true); // y += 15; dead code y = h / 2 + 50; @@ -769,7 +767,7 @@ class Client extends GameShell { for (let x = 10; x < 117; x++) { const rand = Math.trunc(Math.random() * 100.0); - if (rand < 50) this.flameBuffer3[x + (height - 2 << 7)] = 255 + if (rand < 50) this.flameBuffer3[x + ((height - 2) << 7)] = 255; } for (let l = 0; l < 100; l++) { @@ -796,7 +794,7 @@ class Client extends GameShell { for (let y = 1; y < height - 1; y++) { for (let x = 1; x < 127; x++) { const index = x + (y << 7); - let intensity = (this.flameBuffer2[index + 128] - this.flameBuffer0[index + this.flameCycle0 & 32768 - 1] / 5) | 0; + let intensity = (this.flameBuffer2[index + 128] - this.flameBuffer0[(index + this.flameCycle0) & (32768 - 1)] / 5) | 0; if (intensity < 0) { intensity = 0; } @@ -827,12 +825,12 @@ class Client extends GameShell { this.flameGradientCycle1 = 1024; } } - } + }; private mix = (src: number, alpha: number, dst: number) => { const invAlpha = 256 - alpha; - return (((src & 0xFF00FF) * invAlpha + (dst & 0xFF00FF) * alpha) & 0xFF00FF00) + (((src & 0xFF00) * invAlpha + (dst & 0xFF00) * alpha) & 0xFF0000) >> 8; - } + return ((((src & 0xff00ff) * invAlpha + (dst & 0xff00ff) * alpha) & 0xff00ff00) + (((src & 0xff00) * invAlpha + (dst & 0xff00) * alpha) & 0xff0000)) >> 8; + }; private drawFlames = (): void => { const height = 256; @@ -864,15 +862,14 @@ class Client extends GameShell { } } for (let i = 0; i < 33920; i++) { - if (this.imageTitle0 && this.imageFlamesLeft) - this.imageTitle0.pixels[i] = this.imageFlamesLeft.pixels[i]; + if (this.imageTitle0 && this.imageFlamesLeft) this.imageTitle0.pixels[i] = this.imageFlamesLeft.pixels[i]; } let srcOffset = 0; let dstOffset = 1152; for (let y = 1; y < height - 1; y++) { - const offset = (this.flameLineOffset[y] * (height - y) / height) | 0; + const offset = ((this.flameLineOffset[y] * (height - y)) / height) | 0; let step = offset + 22; if (step < 0) { step = 0; @@ -888,7 +885,8 @@ class Client extends GameShell { value = this.flameGradient[value]; if (this.imageTitle0) { const background = this.imageTitle0.pixels[dstOffset]; - this.imageTitle0.pixels[dstOffset++] = (((value & 0xFF00FF) * alpha + (background & 0xFF00FF) * invAlpha) & 0xFF00FF00) + (((value & 0xFF00) * alpha + (background & 0xFF00) * invAlpha) & 0xFF0000) >> 8; + this.imageTitle0.pixels[dstOffset++] = + ((((value & 0xff00ff) * alpha + (background & 0xff00ff) * invAlpha) & 0xff00ff00) + (((value & 0xff00) * alpha + (background & 0xff00) * invAlpha) & 0xff0000)) >> 8; } } } @@ -906,7 +904,7 @@ class Client extends GameShell { srcOffset = 0; dstOffset = 1176; for (let y = 1; y < height - 1; y++) { - const offset = (this.flameLineOffset[y] * (height - y) / height) | 0; + const offset = ((this.flameLineOffset[y] * (height - y)) / height) | 0; const step = 103 - offset; dstOffset += offset; for (let x = 0; x < step; x++) { @@ -919,7 +917,8 @@ class Client extends GameShell { value = this.flameGradient[value]; if (this.imageTitle1) { const background = this.imageTitle1.pixels[dstOffset]; - this.imageTitle1.pixels[dstOffset++] = (((value & 0xFF00FF) * alpha + (background & 0xFF00FF) * invAlpha) & 0xFF00FF00) + (((value & 0xFF00) * alpha + (background & 0xFF00) * invAlpha) & 0xFF0000) >> 8; + this.imageTitle1.pixels[dstOffset++] = + ((((value & 0xff00ff) * alpha + (background & 0xff00ff) * invAlpha) & 0xff00ff00) + (((value & 0xff00) * alpha + (background & 0xff00) * invAlpha) & 0xff0000)) >> 8; } } } @@ -928,9 +927,8 @@ class Client extends GameShell { } this.imageTitle1?.draw(661, 0); - } + }; } const client = new Client(); -client.run().then(() => { -}); +client.run().then(() => {}); diff --git a/src/js/jagex2/audio/SoundEnvelope.ts b/src/js/jagex2/audio/SoundEnvelope.ts index 41884ae9..3dc22138 100644 --- a/src/js/jagex2/audio/SoundEnvelope.ts +++ b/src/js/jagex2/audio/SoundEnvelope.ts @@ -1,2 +1 @@ -export default class SoundEnvelope { -} +export default class SoundEnvelope {} diff --git a/src/js/jagex2/audio/SoundTone.ts b/src/js/jagex2/audio/SoundTone.ts index c7e7ac99..0b941624 100644 --- a/src/js/jagex2/audio/SoundTone.ts +++ b/src/js/jagex2/audio/SoundTone.ts @@ -1,2 +1 @@ -export default class SoundTone { -} +export default class SoundTone {} diff --git a/src/js/jagex2/audio/SoundTrack.ts b/src/js/jagex2/audio/SoundTrack.ts index 4cc2f2d6..8ce7b694 100644 --- a/src/js/jagex2/audio/SoundTrack.ts +++ b/src/js/jagex2/audio/SoundTrack.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class SoundTrack { - static unpack = (sounds: Archive): void => { - }; + static unpack = (sounds: Archive): void => {}; } diff --git a/src/js/jagex2/client/GameShell.ts b/src/js/jagex2/client/GameShell.ts index e8bd21a7..fd96540e 100644 --- a/src/js/jagex2/client/GameShell.ts +++ b/src/js/jagex2/client/GameShell.ts @@ -77,11 +77,15 @@ export default abstract class GameShell { } async run(): Promise { - window.addEventListener('resize', () => { - if (this.resizeToFit) { - this.resize(window.innerWidth, window.innerHeight); - } - }, false); + window.addEventListener( + 'resize', + () => { + if (this.resizeToFit) { + this.resize(window.innerWidth, window.innerHeight); + } + }, + false + ); window.addEventListener('keydown', this.keyDown); window.addEventListener('keyup', this.keyUp); @@ -156,7 +160,7 @@ export default abstract class GameShell { count += ratio; } - count &= 0xFF; + count &= 0xff; if (this.deltime > 0) { this.fps = Math.trunc((ratio * 1000) / (this.deltime * 256)); @@ -202,20 +206,15 @@ export default abstract class GameShell { this.state = -1; } - async load(): Promise { - } + async load(): Promise {} - update(): void { - } + update(): void {} - unload(): void { - } + unload(): void {} - async draw(): Promise { - } + async draw(): Promise {} - refresh(): void { - } + refresh(): void {} async showProgress(progress: number, message: string): Promise { const ctx = this.ctx; @@ -224,7 +223,7 @@ export default abstract class GameShell { if (this.redrawScreen) { ctx.fillStyle = 'black'; - ctx.clearRect(0, 0, width,height); + ctx.clearRect(0, 0, width, height); this.redrawScreen = false; } @@ -232,12 +231,12 @@ export default abstract class GameShell { // draw full progress bar ctx.fillStyle = 'rgb(140, 17, 17)'; - ctx.rect((width / 2) - 152, y, 304, 34); - ctx.fillRect((width / 2) - 150, y + 2, progress * 3, 30); + ctx.rect(width / 2 - 152, y, 304, 34); + ctx.fillRect(width / 2 - 150, y + 2, progress * 3, 30); // cover up progress bar ctx.fillStyle = 'black'; - ctx.fillRect(((width / 2) - 150) + (progress * 3), y + 2, 300 - (progress * 3), 30); + ctx.fillRect(width / 2 - 150 + progress * 3, y + 2, 300 - progress * 3, 30); // draw text ctx.font = 'bold 13px helvetica, sans-serif'; @@ -298,7 +297,7 @@ export default abstract class GameShell { if (ch > 4) { this.keyQueue[this.keyQueueWritePos] = ch; - this.keyQueueWritePos = this.keyQueueWritePos + 1 & 0x7F; + this.keyQueueWritePos = (this.keyQueueWritePos + 1) & 0x7f; } // TODO input tracking }; @@ -324,7 +323,7 @@ export default abstract class GameShell { let key = -1; if (this.keyQueueWritePos != this.keyQueueReadPos) { key = this.keyQueue[this.keyQueueReadPos]; - this.keyQueueReadPos = (this.keyQueueReadPos + 1) & 0x7F; + this.keyQueueReadPos = (this.keyQueueReadPos + 1) & 0x7f; } return key; } @@ -351,7 +350,7 @@ export default abstract class GameShell { // TODO input tracking }; - private get getInsets(): { top: number; left: number } { + private get getInsets(): {top: number; left: number} { const rect = this.canvas.getBoundingClientRect(); const computedStyle = window.getComputedStyle(this.canvas); const paddingLeft = parseFloat(computedStyle.paddingLeft || '0'); @@ -362,7 +361,7 @@ export default abstract class GameShell { const left = rect.left + borderLeft + paddingLeft; const top = rect.top + borderTop + paddingTop; - return { top, left }; + return {top, left}; } private get ms(): number { @@ -373,4 +372,4 @@ export default abstract class GameShell { } return ft / length; } -} \ No newline at end of file +} diff --git a/src/js/jagex2/config/IdkType.ts b/src/js/jagex2/config/IdkType.ts index 80b01f96..3ca617d1 100644 --- a/src/js/jagex2/config/IdkType.ts +++ b/src/js/jagex2/config/IdkType.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class IdkType { - static unpack = (config: Archive): void => { - }; + static unpack = (config: Archive): void => {}; } diff --git a/src/js/jagex2/config/IfType.ts b/src/js/jagex2/config/IfType.ts index a7ddf0ed..a9b106a1 100644 --- a/src/js/jagex2/config/IfType.ts +++ b/src/js/jagex2/config/IfType.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class IfType { - static unpack = (interfaces: Archive): void => { - }; + static unpack = (interfaces: Archive): void => {}; } diff --git a/src/js/jagex2/config/LocType.ts b/src/js/jagex2/config/LocType.ts index 5b55c842..1330e8e4 100644 --- a/src/js/jagex2/config/LocType.ts +++ b/src/js/jagex2/config/LocType.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class LocType { - static unpack = (config: Archive): void => { - }; + static unpack = (config: Archive): void => {}; } diff --git a/src/js/jagex2/config/NpcType.ts b/src/js/jagex2/config/NpcType.ts index 85a63a71..d457954f 100644 --- a/src/js/jagex2/config/NpcType.ts +++ b/src/js/jagex2/config/NpcType.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class NpcType { - static unpack = (config: Archive): void => { - }; + static unpack = (config: Archive): void => {}; } diff --git a/src/js/jagex2/config/ObjType.ts b/src/js/jagex2/config/ObjType.ts index 5f200653..5be66294 100644 --- a/src/js/jagex2/config/ObjType.ts +++ b/src/js/jagex2/config/ObjType.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class ObjType { - static unpack = (config: Archive): void => { - }; + static unpack = (config: Archive): void => {}; } diff --git a/src/js/jagex2/config/SeqType.ts b/src/js/jagex2/config/SeqType.ts index 5a615429..ebf5fe3f 100644 --- a/src/js/jagex2/config/SeqType.ts +++ b/src/js/jagex2/config/SeqType.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class SeqType { - static unpack = (config: Archive): void => { - }; + static unpack = (config: Archive): void => {}; } diff --git a/src/js/jagex2/config/SpotAnimType.ts b/src/js/jagex2/config/SpotAnimType.ts index 3c994cfa..b35e0ceb 100644 --- a/src/js/jagex2/config/SpotAnimType.ts +++ b/src/js/jagex2/config/SpotAnimType.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class SpotAnimType { - static unpack = (config: Archive): void => { - }; + static unpack = (config: Archive): void => {}; } diff --git a/src/js/jagex2/config/VarpType.ts b/src/js/jagex2/config/VarpType.ts index 87104bb1..8e400ed0 100644 --- a/src/js/jagex2/config/VarpType.ts +++ b/src/js/jagex2/config/VarpType.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class VarpType { - static unpack = (config: Archive): void => { - }; + static unpack = (config: Archive): void => {}; } diff --git a/src/js/jagex2/graphics/Draw2D.ts b/src/js/jagex2/graphics/Draw2D.ts index 622f4908..6680434d 100644 --- a/src/js/jagex2/graphics/Draw2D.ts +++ b/src/js/jagex2/graphics/Draw2D.ts @@ -1,5 +1,5 @@ export default class Draw2D { - static pixels: Uint32Array = new Uint32Array; + static pixels: Uint32Array = new Uint32Array(); static width: number = 0; static height: number = 0; @@ -78,16 +78,16 @@ export default class Draw2D { const width = this.width; for (let xx = x0; xx < x1; xx++) { - let off = xx + (y0 * width); + let off = xx + y0 * width; pixels[off] = color; - off = xx + ((y1 - 1) * width); + off = xx + (y1 - 1) * width; pixels[off] = color; } for (let yy = y0; yy < y1; yy++) { - let off = x0 + (yy * width); + let off = x0 + yy * width; pixels[off] = color; - off = (x1 - 1) + (yy * width); + off = x1 - 1 + yy * width; pixels[off] = color; } }; @@ -120,7 +120,7 @@ export default class Draw2D { const width = this.width; for (let yy = y0; yy < y1; yy++) { - const off = x0 + (yy * width); + const off = x0 + yy * width; pixels.fill(color, off, off + (x1 - x0)); } }; diff --git a/src/js/jagex2/graphics/Draw3D.ts b/src/js/jagex2/graphics/Draw3D.ts index 003a19dd..b9d5166c 100644 --- a/src/js/jagex2/graphics/Draw3D.ts +++ b/src/js/jagex2/graphics/Draw3D.ts @@ -53,17 +53,19 @@ export default class Draw3D { try { Draw3D.textures[i] = Pix8.fromArchive(textures, i.toString()); Draw3D.textureCount++; - } catch (err) { /* empty */ } + } catch (err) { + /* empty */ + } } }; static setBrightness = (brightness: number): void => { - brightness += (Math.random() * 0.3) - 0.15; + brightness += Math.random() * 0.3 - 0.15; let offset = 0; for (let y = 0; y < 512; y++) { - const hue = ((y / 8) / 64) + 0.0078125; - const saturation = ((y & 7) / 8) + 0.0625; + const hue = y / 8 / 64 + 0.0078125; + const saturation = (y & 7) / 8 + 0.0625; for (let x = 0; x < 128; x++) { const lightness = x / 128; @@ -77,7 +79,7 @@ export default class Draw3D { if (lightness < 0.5) { q = lightness * (1 + saturation); } else { - q = (lightness + saturation) - (lightness * saturation); + q = lightness + saturation - lightness * saturation; } const p = 2 * lightness - q; @@ -92,31 +94,31 @@ export default class Draw3D { } if (6 * t < 1) { - r = p + ((q - p) * 6 * t); + r = p + (q - p) * 6 * t; } else if (2 * t < 1) { r = q; } else if (3 * t < 2) { - r = p + ((q - p) * ((0.6666666666666666 - t) * 6)); + r = p + (q - p) * ((0.6666666666666666 - t) * 6); } else { r = p; } if (6 * hue < 1) { - g = p + ((q - p) * 6 * hue); + g = p + (q - p) * 6 * hue; } else if (2 * hue < 1) { g = q; } else if (3 * hue < 2) { - g = p + ((q - p) * ((0.6666666666666666 - hue) * 6)); + g = p + (q - p) * ((0.6666666666666666 - hue) * 6); } else { g = p; } if (6 * d11 < 1) { - b = p + ((q - p) * 6 * d11); + b = p + (q - p) * 6 * d11; } else if (2 * d11 < 1) { b = q; } else if (3 * d11 < 2) { - b = p + ((q - p) * ((0.6666666666666666 - d11) * 6)); + b = p + (q - p) * ((0.6666666666666666 - d11) * 6); } else { b = p; } @@ -138,7 +140,7 @@ export default class Draw3D { private static setGamma = (rgb: number, gamma: number): number => { let r = (rgb >> 16) / 256; - let g = (rgb >> 8 & 255) / 256; + let g = ((rgb >> 8) & 255) / 256; let b = (rgb & 255) / 256; r = Math.pow(r, gamma); g = Math.pow(g, gamma); @@ -149,8 +151,7 @@ export default class Draw3D { return (intR << 16) | (intG << 8) | intB; }; - static initPool = (): void => { - }; + static initPool = (): void => {}; static fillGouraudTriangle = (xA: number, xB: number, xC: number, yA: number, yB: number, yC: number, colorA: number, colorB: number, colorC: number): void => { let xStepAB = 0; @@ -176,7 +177,7 @@ export default class Draw3D { colorStepAC = ((colorA - colorC) << 15) / (yA - yC); } - if ((yA <= yB) && (yA <= yC)) { + if (yA <= yB && yA <= yC) { if (yA >= Draw2D.bottom) { return; } @@ -187,8 +188,8 @@ export default class Draw3D { yC = Draw2D.bottom; } if (yB < yC) { - xC = (xA <<= 16); - colorC = (colorA <<= 15); + xC = xA <<= 16; + colorC = colorA <<= 15; if (yA < 0) { xC -= xStepAC * yA; xA -= xStepAB * yA; @@ -203,7 +204,7 @@ export default class Draw3D { colorB -= colorStepBC * yB; yB = 0; } - if (((yA != yB) && (xStepAC < xStepAB)) || ((yA == yB) && (xStepAC > xStepBC))) { + if ((yA != yB && xStepAC < xStepAB) || (yA == yB && xStepAC > xStepBC)) { yC -= yB; yB -= yA; for (yA = Draw3D.lineOffset[yA]; --yB >= 0; yA += Draw2D.width) { @@ -242,8 +243,8 @@ export default class Draw3D { } return; } - xB = (xA <<= 16); - colorB = (colorA <<= 15); + xB = xA <<= 16; + colorB = colorA <<= 15; if (yA < 0) { xB -= xStepAC * yA; xA -= xStepAB * yA; @@ -258,7 +259,7 @@ export default class Draw3D { colorC -= colorStepBC * yC; yC = 0; } - if (((yA != yC) && (xStepAC < xStepAB)) || ((yA == yC) && (xStepBC > xStepAB))) { + if ((yA != yC && xStepAC < xStepAB) || (yA == yC && xStepBC > xStepAB)) { yB -= yC; yC -= yA; for (yA = Draw3D.lineOffset[yA]; --yC >= 0; yA += Draw2D.width) { @@ -308,8 +309,8 @@ export default class Draw3D { yA = Draw2D.bottom; } if (yC < yA) { - xA = (xB <<= 16); - colorA = (colorB <<= 15); + xA = xB <<= 16; + colorA = colorB <<= 15; if (yB < 0) { xA -= xStepAB * yB; xB -= xStepBC * yB; @@ -324,7 +325,7 @@ export default class Draw3D { colorC -= colorStepAC * yC; yC = 0; } - if (((yB != yC) && (xStepAB < xStepBC)) || ((yB == yC) && (xStepAB > xStepAC))) { + if ((yB != yC && xStepAB < xStepBC) || (yB == yC && xStepAB > xStepAC)) { yA -= yC; yC -= yB; for (yB = Draw3D.lineOffset[yB]; --yC >= 0; yB += Draw2D.width) { @@ -363,8 +364,8 @@ export default class Draw3D { } return; } - xC = (xB <<= 16); - colorC = (colorB <<= 15); + xC = xB <<= 16; + colorC = colorB <<= 15; if (yB < 0) { xC -= xStepAB * yB; xB -= xStepBC * yB; @@ -428,8 +429,8 @@ export default class Draw3D { yB = Draw2D.bottom; } if (yA < yB) { - xB = (xC <<= 16); - colorB = (colorC <<= 15); + xB = xC <<= 16; + colorB = colorC <<= 15; if (yC < 0) { xB -= xStepBC * yC; xC -= xStepAC * yC; @@ -483,8 +484,8 @@ export default class Draw3D { } return; } - xA = (xC <<= 16); - colorA = (colorC <<= 15); + xA = xC <<= 16; + colorA = colorC <<= 15; if (yC < 0) { xA -= xStepBC * yC; xC -= xStepAC * yC; @@ -546,7 +547,7 @@ export default class Draw3D { let colorStep = 0; if (Draw3D.clipX) { - if ((x1 - x0) > 3) { + if (x1 - x0 > 3) { colorStep = (color1 - color0) / (x1 - x0); } else { colorStep = 0; @@ -670,9 +671,7 @@ export default class Draw3D { } while (--length > 0); }; - static fillTriangle = (): void => { - }; + static fillTriangle = (): void => {}; - static fillTexturedTriangle = (): void => { - }; + static fillTexturedTriangle = (): void => {}; } diff --git a/src/js/jagex2/graphics/Font.ts b/src/js/jagex2/graphics/Font.ts index ee5c8fb0..7964a643 100644 --- a/src/js/jagex2/graphics/Font.ts +++ b/src/js/jagex2/graphics/Font.ts @@ -20,7 +20,7 @@ export default class Font { } } - pixels: Uint8Array[] = [] + pixels: Uint8Array[] = []; charWidth: number[] = []; charHeight: number[] = []; clipX: number[] = []; @@ -47,8 +47,8 @@ export default class Font { font.clipX[c] = index.g1; font.clipY[c] = index.g1; - const width = font.charWidth[c] = index.g2; - const height = font.charHeight[c] = index.g2; + const width = (font.charWidth[c] = index.g2); + const height = (font.charHeight[c] = index.g2); const size = width * height; font.pixels[c] = new Uint8Array(size); @@ -61,7 +61,7 @@ export default class Font { } else if (pixelOrder === 1) { for (let x = 0; x < width; x++) { for (let y = 0; y < height; y++) { - font.pixels[c][x + (y * width)] = dat.g1; + font.pixels[c][x + y * width] = dat.g1; } } } @@ -76,7 +76,7 @@ export default class Font { { let i = 0; for (let y = height / 7; y < height; y++) { - i += font.pixels[c][width + (y * width)]; + i += font.pixels[c][width + y * width]; } if (i <= height / 7) { @@ -88,7 +88,7 @@ export default class Font { { let i = 0; for (let y = height / 7; y < height; y++) { - i += font.pixels[c][width + (y * width) - 1]; + i += font.pixels[c][width + y * width - 1]; } if (i <= height / 7) { @@ -120,7 +120,6 @@ export default class Font { x += this.charSpace[c]; } - }; drawStringTaggable = (x: number, y: number, str: string, color: number, shadowed: boolean): void => { @@ -146,7 +145,6 @@ export default class Font { x += this.charSpace[c]; } } - }; getTextWidth = (str: string): number => { @@ -164,11 +162,11 @@ export default class Font { }; drawStringTaggableCenter = (x: number, y: number, str: string, color: number, shadowed: boolean): void => { - this.drawStringTaggable(x - (this.getTextWidth(str) / 2), y, str, color, shadowed); + this.drawStringTaggable(x - this.getTextWidth(str) / 2, y, str, color, shadowed); }; drawStringCenter = (x: number, y: number, str: string, color: number) => { - this.draw(x - (this.getTextWidth(str) / 2), y, str, color); + this.draw(x - this.getTextWidth(str) / 2, y, str, color); }; drawRight = (x: number, y: number, str: string, color: number, shadowed = true): void => { @@ -185,7 +183,7 @@ export default class Font { w = w | 0; h = h | 0; - let dstOff = x + (y * Draw2D.width); + let dstOff = x + y * Draw2D.width; let srcOff = 0; let dstStep = Draw2D.width - w; diff --git a/src/js/jagex2/graphics/Model.ts b/src/js/jagex2/graphics/Model.ts index f9e2f37e..7a2379e9 100644 --- a/src/js/jagex2/graphics/Model.ts +++ b/src/js/jagex2/graphics/Model.ts @@ -250,7 +250,7 @@ export default class Model { return 127 - scalar; } - scalar = (scalar * (hsl & 0x7F)) >> 7; + scalar = (scalar * (hsl & 0x7f)) >> 7; if (scalar < 2) { scalar = 2; @@ -258,7 +258,7 @@ export default class Model { scalar = 126; } - return (hsl & 0xFF80) + scalar; + return (hsl & 0xff80) + scalar; } // ---- @@ -309,7 +309,22 @@ export default class Model { vertexNormalOriginal: VertexNormal[] | null = null; constructor(id: number) { - if (Model.head === null || Model.face1 === null || Model.face2 === null || Model.face3 === null || Model.face4 === null || Model.face5 === null || Model.point1 === null || Model.point2 === null || Model.point3 === null || Model.point4 === null || Model.point5 === null || Model.vertex1 === null || Model.vertex2 === null || Model.axis === null) { + if ( + Model.head === null || + Model.face1 === null || + Model.face2 === null || + Model.face3 === null || + Model.face4 === null || + Model.face5 === null || + Model.point1 === null || + Model.point2 === null || + Model.point3 === null || + Model.point4 === null || + Model.point5 === null || + Model.vertex1 === null || + Model.vertex2 === null || + Model.axis === null + ) { return; } @@ -491,27 +506,27 @@ export default class Model { let identical = -1; const x = src.vertexX[vertexId]; - const y = src.vertexY[vertexId]; - const z = src.vertexZ[vertexId]; + const y = src.vertexY[vertexId]; + const z = src.vertexZ[vertexId]; for (let v = 0; v < this.vertexCount; v++) { - if (x == this.vertexX[v] && y == this.vertexY[v] && z == this.vertexZ[v]) { - identical = v; - break; - } - } + if (x == this.vertexX[v] && y == this.vertexY[v] && z == this.vertexZ[v]) { + identical = v; + break; + } + } if (identical == -1) { - this.vertexX[this.vertexCount] = x; - this.vertexY[this.vertexCount] = y; - this.vertexZ[this.vertexCount] = z; + this.vertexX[this.vertexCount] = x; + this.vertexY[this.vertexCount] = y; + this.vertexZ[this.vertexCount] = z; if (this.vertexLabel != null && src.vertexLabel != null) { - this.vertexLabel[this.vertexCount] = src.vertexLabel[vertexId]; - } + this.vertexLabel[this.vertexCount] = src.vertexLabel[vertexId]; + } identical = this.vertexCount++; - } + } return identical; } @@ -626,11 +641,9 @@ export default class Model { this.maxDepth = this.minDepth + Math.trunc(Math.sqrt(this.radius * this.radius + this.minY * this.minY)); } - createLabelReferences() { - } + createLabelReferences() {} - applyTransform() { - } + applyTransform() {} rotateY90() { if (this.vertexX === null || this.vertexY === null || this.vertexZ === null) { @@ -709,9 +722,9 @@ export default class Model { } for (let v = 0; v < this.vertexCount; v++) { - this.vertexX[v] = this.vertexX[v] * x / 128; - this.vertexY[v] = this.vertexY[v] * y / 128; - this.vertexZ[v] = this.vertexZ[v] * z / 128; + this.vertexX[v] = (this.vertexX[v] * x) / 128; + this.vertexY[v] = (this.vertexY[v] * y) / 128; + this.vertexZ[v] = (this.vertexZ[v] * z) / 128; } } @@ -765,9 +778,9 @@ export default class Model { length = 1; } - nx = nx * 256 / length; - ny = ny * 256 / length; - nz = nz * 256 / length; + nx = (nx * 256) / length; + ny = (ny * 256) / length; + nz = (nz * 256) / length; if (this.faceInfo === null || (this.faceInfo[f] & 0x1) === 0) { let n = this.vertexNormal[a]; @@ -952,15 +965,14 @@ export default class Model { } // try { - this.draw2(false, false, 0); + this.draw2(false, false, 0); // } catch (err) { // console.error(err); // } } // todo: better name, Java relies on overloads - draw(yaw: number, sinEyePitch: number, cosEyePitch: number, sinEyeYaw: number, cosEyeYaw: number, relativeX: number, relativeY: number, relativeZ: number, bitset: number) { - } + draw(yaw: number, sinEyePitch: number, cosEyePitch: number, sinEyeYaw: number, cosEyeYaw: number, relativeX: number, relativeY: number, relativeZ: number, bitset: number) {} // todo: better name, Java relies on overloads draw2(clipped: boolean, picking: boolean, bitset: number) { @@ -976,7 +988,15 @@ export default class Model { return; } - if (Model.tmpDepthFaceCount === null || Model.tmpDepthFaces === null || Model.tmpPriorityFaces === null || Model.tmpPriorityFaceCount === null || Model.tmpPriorityDepthSum === null || Model.tmpPriority10FaceDepth === null || Model.tmpPriority11FaceDepth === null) { + if ( + Model.tmpDepthFaceCount === null || + Model.tmpDepthFaces === null || + Model.tmpPriorityFaces === null || + Model.tmpPriorityFaceCount === null || + Model.tmpPriorityDepthSum === null || + Model.tmpPriority10FaceDepth === null || + Model.tmpPriority11FaceDepth === null + ) { return; } @@ -1017,7 +1037,7 @@ export default class Model { const dxCB = xC - xB; const dyCB = Model.vertexScreenY[c] - Model.vertexScreenY[b]; - if ((dxAB * dyCB - dyAB * dxCB) <= 0) { + if (dxAB * dyCB - dyAB * dxCB <= 0) { continue; } @@ -1228,7 +1248,17 @@ export default class Model { } if (type === 0 && this.faceColorA !== null && this.faceColorB !== null && this.faceColorC !== null) { - Draw3D.fillGouraudTriangle(Model.vertexScreenX[a], Model.vertexScreenX[b], Model.vertexScreenX[c], Model.vertexScreenY[a], Model.vertexScreenY[b], Model.vertexScreenY[c], this.faceColorA[face], this.faceColorB[face], this.faceColorC[face]); + Draw3D.fillGouraudTriangle( + Model.vertexScreenX[a], + Model.vertexScreenX[b], + Model.vertexScreenX[c], + Model.vertexScreenY[a], + Model.vertexScreenY[b], + Model.vertexScreenY[c], + this.faceColorA[face], + this.faceColorB[face], + this.faceColorC[face] + ); } else if (type === 1) { // Draw3D.fillTriangle(Model.vertexScreenX[a], Model.vertexScreenX[b], Model.vertexScreenX[c], Model.vertexScreenY[a], Model.vertexScreenY[b], Model.vertexScreenY[c], palette[this.faceColorA[face]]); } else if (type === 2 && this.faceInfo !== null && this.texturedVertexA !== null && this.texturedVertexB !== null && this.texturedVertexC !== null) { @@ -1246,8 +1276,7 @@ export default class Model { } } - drawNearClippedFace(face: number) { - } + drawNearClippedFace(face: number) {} pointWithinTriangle(x: number, y: number, xA: number, xB: number, xC: number, yA: number, yB: number, yC: number) { if (y < yA && y < yB && y < yC) { diff --git a/src/js/jagex2/graphics/Pix24.ts b/src/js/jagex2/graphics/Pix24.ts index e1704a59..9cbe8853 100644 --- a/src/js/jagex2/graphics/Pix24.ts +++ b/src/js/jagex2/graphics/Pix24.ts @@ -5,319 +5,301 @@ import Archive from '../io/Archive'; import Packet from '../io/Packet'; export default class Pix24 { - // constructor - readonly pixels: Uint32Array; - readonly width: number; - readonly height: number; - cropX: number; - cropY: number; - cropW: number; - cropH: number; - - constructor(width: number, height: number) { - this.pixels = new Uint32Array(width * height); - this.width = this.cropW = width; - this.height = this.cropH = height; - this.cropX = this.cropY = 0; - } - - static fromJpeg = async (archive: Archive, name: string): Promise => { - const dat = archive.read(name + '.dat'); - const jpeg = await decodeJpeg(dat); - const image = new Pix24(jpeg.width, jpeg.height); - - // copy pixels (uint32) to imageData (uint8) - const pixels = image.pixels; - const data = jpeg.data; - for (let i = 0; i < pixels.length; i++) { - const index = i * 4; - pixels[i] = (data[index + 3] << 24) | (data[index + 0] << 16) | (data[index + 1] << 8) | (data[index + 2] << 0); + // constructor + readonly pixels: Uint32Array; + readonly width: number; + readonly height: number; + cropX: number; + cropY: number; + cropW: number; + cropH: number; + + constructor(width: number, height: number) { + this.pixels = new Uint32Array(width * height); + this.width = this.cropW = width; + this.height = this.cropH = height; + this.cropX = this.cropY = 0; } - return image; - }; - - static fromArchive = (archive: Archive, name: string, sprite: number = 0): Pix24 => { - const dat = new Packet(archive.read(name + '.dat')); - const index = new Packet(archive.read('ind ex.dat')); - - // cropW/cropH are shared across all sprites in a single image - index.pos = dat.g2; - const cropW = index.g2; - const cropH = index.g2; - - // palette is shared across all images in a single archive - const paletteCount = index.g1; - const palette = new Uint32Array(paletteCount); - const length = paletteCount - 1; - for (let i = 0; i < length; i++) { - // the first color (0) is reserved for transparency - palette[i + 1] = index.g3; - - // black (0) will become transparent, make it black (1) so it's visible - if (palette[i + 1] === 0) { - palette[i + 1] = 1; - } - } + static fromJpeg = async (archive: Archive, name: string): Promise => { + const dat = archive.read(name + '.dat'); + const jpeg = await decodeJpeg(dat); + const image = new Pix24(jpeg.width, jpeg.height); + + // copy pixels (uint32) to imageData (uint8) + const pixels = image.pixels; + const data = jpeg.data; + for (let i = 0; i < pixels.length; i++) { + const index = i * 4; + pixels[i] = (data[index + 3] << 24) | (data[index + 0] << 16) | (data[index + 1] << 8) | (data[index + 2] << 0); + } - // advance to sprite - for (let i = 0; i < sprite; i++) { - index.pos += 2; - dat.pos += index.g2 * index.g2; - index.pos += 1; - } + return image; + }; + + static fromArchive = (archive: Archive, name: string, sprite: number = 0): Pix24 => { + const dat = new Packet(archive.read(name + '.dat')); + const index = new Packet(archive.read('index.dat')); + + // cropW/cropH are shared across all sprites in a single image + index.pos = dat.g2; + const cropW = index.g2; + const cropH = index.g2; + + // palette is shared across all images in a single archive + const paletteCount = index.g1; + const palette = new Uint32Array(paletteCount); + const length = paletteCount - 1; + for (let i = 0; i < length; i++) { + // the first color (0) is reserved for transparency + palette[i + 1] = index.g3; + + // black (0) will become transparent, make it black (1) so it's visible + if (palette[i + 1] === 0) { + palette[i + 1] = 1; + } + } - // read sprite - const cropX = index.g1; - const cropY = index.g1; - const width = index.g2; - const height = index.g2; - - const image = new Pix24(width, height); - image.cropX = cropX; - image.cropY = cropY; - image.cropW = cropW; - image.cropH = cropH; - - const pixelOrder = index.g1; - if (pixelOrder === 0) { - const length = image.width * image.height; - for (let i = 0; i < length; i++) { - image.pixels[i] = palette[dat.g1]; - } - } else if (pixelOrder === 1) { - const width = image.width; - for (let x = 0; x < width; x++) { - const height = image.height; - for (let y = 0; y < height; y++) { - image.pixels[x + y * width] = palette[dat.g1]; + // advance to sprite + for (let i = 0; i < sprite; i++) { + index.pos += 2; + dat.pos += index.g2 * index.g2; + index.pos += 1; } - } - } - return image; - }; + // read sprite + const cropX = index.g1; + const cropY = index.g1; + const width = index.g2; + const height = index.g2; + + const image = new Pix24(width, height); + image.cropX = cropX; + image.cropY = cropY; + image.cropW = cropW; + image.cropH = cropH; + + const pixelOrder = index.g1; + if (pixelOrder === 0) { + const length = image.width * image.height; + for (let i = 0; i < length; i++) { + image.pixels[i] = palette[dat.g1]; + } + } else if (pixelOrder === 1) { + const width = image.width; + for (let x = 0; x < width; x++) { + const height = image.height; + for (let y = 0; y < height; y++) { + image.pixels[x + y * width] = palette[dat.g1]; + } + } + } - draw = (x: number, y: number): void => { - x = x | 0; - y = y | 0; + return image; + }; - x += this.cropX; - y += this.cropY; + draw = (x: number, y: number): void => { + x = x | 0; + y = y | 0; - let dstOff = x + y * Draw2D.width; - let srcOff = 0; + x += this.cropX; + y += this.cropY; - let h = this.height; - let w = this.width; + let dstOff = x + y * Draw2D.width; + let srcOff = 0; - let dstStep = Draw2D.width - w; - let srcStep = 0; + let h = this.height; + let w = this.width; - if (y < Draw2D.top) { - const cutoff = Draw2D.top - y; - h -= cutoff; - y = Draw2D.top; - srcOff += cutoff * w; - dstOff += cutoff * Draw2D.width; - } + let dstStep = Draw2D.width - w; + let srcStep = 0; - if (y + h > Draw2D.bottom) { - h -= y + h - Draw2D.bottom; - } - - if (x < Draw2D.left) { - const cutoff = Draw2D.left - x; - w -= cutoff; - x = Draw2D.left; - srcOff += cutoff; - dstOff += cutoff; - srcStep += cutoff; - dstStep += cutoff; - } + if (y < Draw2D.top) { + const cutoff = Draw2D.top - y; + h -= cutoff; + y = Draw2D.top; + srcOff += cutoff * w; + dstOff += cutoff * Draw2D.width; + } - if (x + w > Draw2D.right) { - const cutoff = x + w - Draw2D.right; - w -= cutoff; - srcStep += cutoff; - dstStep += cutoff; - } + if (y + h > Draw2D.bottom) { + h -= y + h - Draw2D.bottom; + } - if (w > 0 && h > 0) { - this.copyImageDraw(w, h, this.pixels, srcOff, srcStep, Draw2D.pixels, dstOff, dstStep); - } - }; + if (x < Draw2D.left) { + const cutoff = Draw2D.left - x; + w -= cutoff; + x = Draw2D.left; + srcOff += cutoff; + dstOff += cutoff; + srcStep += cutoff; + dstStep += cutoff; + } - blitOpaque = (x: number, y: number): void => { - x = x | 0; - y = y | 0; + if (x + w > Draw2D.right) { + const cutoff = x + w - Draw2D.right; + w -= cutoff; + srcStep += cutoff; + dstStep += cutoff; + } - x += this.cropX; - y += this.cropY; + if (w > 0 && h > 0) { + this.copyImageDraw(w, h, this.pixels, srcOff, srcStep, Draw2D.pixels, dstOff, dstStep); + } + }; - let dstOff = x + y * Draw2D.width; - let srcOff = 0; + blitOpaque = (x: number, y: number): void => { + x = x | 0; + y = y | 0; - let h = this.height; - let w = this.width; + x += this.cropX; + y += this.cropY; - let dstStep = Draw2D.width - w; - let srcStep = 0; + let dstOff = x + y * Draw2D.width; + let srcOff = 0; - if (y < Draw2D.top) { - const cutoff = Draw2D.top - y; - h -= cutoff; - y = Draw2D.top; - srcOff += cutoff * w; - dstOff += cutoff * Draw2D.width; - } + let h = this.height; + let w = this.width; - if (y + h > Draw2D.bottom) { - h -= y + h - Draw2D.bottom; - } + let dstStep = Draw2D.width - w; + let srcStep = 0; - if (x < Draw2D.left) { - const cutoff = Draw2D.left - x; - w -= cutoff; - x = Draw2D.left; - srcOff += cutoff; - dstOff += cutoff; - srcStep += cutoff; - dstStep += cutoff; - } + if (y < Draw2D.top) { + const cutoff = Draw2D.top - y; + h -= cutoff; + y = Draw2D.top; + srcOff += cutoff * w; + dstOff += cutoff * Draw2D.width; + } - if (x + w > Draw2D.right) { - const cutoff = x + w - Draw2D.right; - w -= cutoff; - srcStep += cutoff; - dstStep += cutoff; - } + if (y + h > Draw2D.bottom) { + h -= y + h - Draw2D.bottom; + } - if (w > 0 && h > 0) { - this.copyImageBlitOpaque(w, h, this.pixels, srcOff, srcStep, Draw2D.pixels, dstOff, dstStep); - } - }; - - flipHorizontally = (): void => { - const pixels = this.pixels; - const width = this.width; - const height = this.height; - - for (let y = 0; y < height; y++) { - const div = width / 2; - for (let x = 0; x < div; x++) { - const off1 = x + y * width; - const off2 = width - x - 1 + y * width; - - const tmp = pixels[off1]; - pixels[off1] = pixels[off2]; - pixels[off2] = tmp; - } - } - }; - - flipVertically = (): void => { - const pixels = this.pixels; - const width = this.width; - const height = this.height; - - for (let y = 0; y < height / 2; y++) { - for (let x = 0; x < width; x++) { - const off1 = x + y * width; - const off2 = x + (height - y - 1) * width; - - const tmp = pixels[off1]; - pixels[off1] = pixels[off2]; - pixels[off2] = tmp; - } - } - }; - - private copyImageBlitOpaque = ( - w: number, - h: number, - src: Uint32Array, - srcOff: number, - srcStep: number, - dst: Uint32Array, - dstOff: number, - dstStep: number - ): void => { - const qw = -(w >> 2); - w = -(w & 0x3); - - for (let y = -h; y < 0; y++) { - for (let x = qw; x < 0; x++) { - dst[dstOff++] = src[srcOff++]; - dst[dstOff++] = src[srcOff++]; - dst[dstOff++] = src[srcOff++]; - dst[dstOff++] = src[srcOff++]; - } - - for (let x = w; x < 0; x++) { - dst[dstOff++] = src[srcOff++]; - } - - dstOff += dstStep; - srcOff += srcStep; - } - }; - - private copyImageDraw = ( - w: number, - h: number, - src: Uint32Array, - srcOff: number, - srcStep: number, - dst: Uint32Array, - dstOff: number, - dstStep: number - ): void => { - const qw = -(w >> 2); - w = -(w & 0x3); - - for (let y = -h; y < 0; y++) { - for (let x = qw; x < 0; x++) { - let rgb = src[srcOff++]; - if (rgb === 0) { - dstOff++; - } else { - dst[dstOff++] = rgb; + if (x < Draw2D.left) { + const cutoff = Draw2D.left - x; + w -= cutoff; + x = Draw2D.left; + srcOff += cutoff; + dstOff += cutoff; + srcStep += cutoff; + dstStep += cutoff; } - rgb = src[srcOff++]; - if (rgb === 0) { - dstOff++; - } else { - dst[dstOff++] = rgb; + if (x + w > Draw2D.right) { + const cutoff = x + w - Draw2D.right; + w -= cutoff; + srcStep += cutoff; + dstStep += cutoff; } - rgb = src[srcOff++]; - if (rgb === 0) { - dstOff++; - } else { - dst[dstOff++] = rgb; + if (w > 0 && h > 0) { + this.copyImageBlitOpaque(w, h, this.pixels, srcOff, srcStep, Draw2D.pixels, dstOff, dstStep); } + }; - rgb = src[srcOff++]; - if (rgb === 0) { - dstOff++; - } else { - dst[dstOff++] = rgb; + flipHorizontally = (): void => { + const pixels = this.pixels; + const width = this.width; + const height = this.height; + + for (let y = 0; y < height; y++) { + const div = width / 2; + for (let x = 0; x < div; x++) { + const off1 = x + y * width; + const off2 = width - x - 1 + y * width; + + const tmp = pixels[off1]; + pixels[off1] = pixels[off2]; + pixels[off2] = tmp; + } } - } - - for (let x = w; x < 0; x++) { - const rgb = src[srcOff++]; - if (rgb === 0) { - dstOff++; - } else { - dst[dstOff++] = rgb; + }; + + flipVertically = (): void => { + const pixels = this.pixels; + const width = this.width; + const height = this.height; + + for (let y = 0; y < height / 2; y++) { + for (let x = 0; x < width; x++) { + const off1 = x + y * width; + const off2 = x + (height - y - 1) * width; + + const tmp = pixels[off1]; + pixels[off1] = pixels[off2]; + pixels[off2] = tmp; + } } - } - - dstOff += dstStep; - srcOff += srcStep; - } - }; + }; + + private copyImageBlitOpaque = (w: number, h: number, src: Uint32Array, srcOff: number, srcStep: number, dst: Uint32Array, dstOff: number, dstStep: number): void => { + const qw = -(w >> 2); + w = -(w & 0x3); + + for (let y = -h; y < 0; y++) { + for (let x = qw; x < 0; x++) { + dst[dstOff++] = src[srcOff++]; + dst[dstOff++] = src[srcOff++]; + dst[dstOff++] = src[srcOff++]; + dst[dstOff++] = src[srcOff++]; + } + + for (let x = w; x < 0; x++) { + dst[dstOff++] = src[srcOff++]; + } + + dstOff += dstStep; + srcOff += srcStep; + } + }; + + private copyImageDraw = (w: number, h: number, src: Uint32Array, srcOff: number, srcStep: number, dst: Uint32Array, dstOff: number, dstStep: number): void => { + const qw = -(w >> 2); + w = -(w & 0x3); + + for (let y = -h; y < 0; y++) { + for (let x = qw; x < 0; x++) { + let rgb = src[srcOff++]; + if (rgb === 0) { + dstOff++; + } else { + dst[dstOff++] = rgb; + } + + rgb = src[srcOff++]; + if (rgb === 0) { + dstOff++; + } else { + dst[dstOff++] = rgb; + } + + rgb = src[srcOff++]; + if (rgb === 0) { + dstOff++; + } else { + dst[dstOff++] = rgb; + } + + rgb = src[srcOff++]; + if (rgb === 0) { + dstOff++; + } else { + dst[dstOff++] = rgb; + } + } + + for (let x = w; x < 0; x++) { + const rgb = src[srcOff++]; + if (rgb === 0) { + dstOff++; + } else { + dst[dstOff++] = rgb; + } + } + + dstOff += dstStep; + srcOff += srcStep; + } + }; } diff --git a/src/js/jagex2/graphics/Pix8.ts b/src/js/jagex2/graphics/Pix8.ts index 7c40ceec..edc94dde 100644 --- a/src/js/jagex2/graphics/Pix8.ts +++ b/src/js/jagex2/graphics/Pix8.ts @@ -77,7 +77,7 @@ export default class Pix8 { for (let x = 0; x < width; x++) { const height = image.height; for (let y = 0; y < height; y++) { - image.pixels[x + (y * width)] = dat.g1; + image.pixels[x + y * width] = dat.g1; } } } @@ -92,7 +92,7 @@ export default class Pix8 { x += this.cropX; y += this.cropY; - let dstOff = x + (y * Draw2D.width); + let dstOff = x + y * Draw2D.width; let srcOff = 0; let h = this.height; @@ -118,7 +118,7 @@ export default class Pix8 { } if (y + h > Draw2D.bottom) { - h -= (y + h) - Draw2D.bottom; + h -= y + h - Draw2D.bottom; } if (x < Draw2D.left) { @@ -132,7 +132,7 @@ export default class Pix8 { } if (x + w > Draw2D.right) { - const cutoff = (x + w) - Draw2D.right; + const cutoff = x + w - Draw2D.right; w -= cutoff; srcStep += cutoff; dstStep += cutoff; @@ -150,7 +150,7 @@ export default class Pix8 { for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { - const off = x + (y * w); + const off = x + y * w; const p = src[srcOff + off]; if (p != 0) { diff --git a/src/js/jagex2/graphics/SeqBase.ts b/src/js/jagex2/graphics/SeqBase.ts index 197c1cb9..8591082e 100644 --- a/src/js/jagex2/graphics/SeqBase.ts +++ b/src/js/jagex2/graphics/SeqBase.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class SeqBase { - static unpack = (models: Archive): void => { - }; + static unpack = (models: Archive): void => {}; } diff --git a/src/js/jagex2/graphics/SeqFrame.ts b/src/js/jagex2/graphics/SeqFrame.ts index 42007cc7..5aff529d 100644 --- a/src/js/jagex2/graphics/SeqFrame.ts +++ b/src/js/jagex2/graphics/SeqFrame.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class SeqFrame { - static unpack = (models: Archive): void => { - }; + static unpack = (models: Archive): void => {}; } diff --git a/src/js/jagex2/io/Archive.ts b/src/js/jagex2/io/Archive.ts index fb897956..c4e572a9 100644 --- a/src/js/jagex2/io/Archive.ts +++ b/src/js/jagex2/io/Archive.ts @@ -45,7 +45,7 @@ export default class Archive { this.fileSizeDeflated = []; this.fileOffset = []; - let offset = buffer.pos + (this.fileCount * 10); + let offset = buffer.pos + this.fileCount * 10; for (let i = 0; i < this.fileCount; i++) { this.fileHash.push(buffer.g4); this.fileSizeInflated.push(buffer.g3); diff --git a/src/js/jagex2/io/Packet.ts b/src/js/jagex2/io/Packet.ts index aebe75a1..e5f13607 100644 --- a/src/js/jagex2/io/Packet.ts +++ b/src/js/jagex2/io/Packet.ts @@ -1,13 +1,13 @@ export default class Packet { static crctable: Int32Array = new Int32Array(256); - static CRC32_POLYNOMIAL: number = 0xEDB88320; + static CRC32_POLYNOMIAL: number = 0xedb88320; static bitmask: Uint32Array = new Uint32Array(33); static { for (let i = 0; i < 32; i++) { Packet.bitmask[i] = (1 << i) - 1; } - Packet.bitmask[32] = 0xFFFFFFFF; + Packet.bitmask[32] = 0xffffffff; for (let i = 0; i < 256; i++) { let remainder = i; @@ -46,7 +46,7 @@ export default class Packet { return new Packet(new Uint8Array(5000)); } return new Packet(new Uint8Array(30000)); - } + }; get g1(): number { return this.data[this.pos++]; @@ -54,28 +54,28 @@ export default class Packet { // signed get g1b(): number { - return this.data[this.pos++] << 24 >> 24; + return (this.data[this.pos++] << 24) >> 24; } get g2(): number { - return (this.data[this.pos++] << 8 | this.data[this.pos++]) >>> 0; + return ((this.data[this.pos++] << 8) | this.data[this.pos++]) >>> 0; } get g2s(): number { - return this.data[this.pos++] << 8 | this.data[this.pos++]; + return (this.data[this.pos++] << 8) | this.data[this.pos++]; } get g3(): number { - return (this.data[this.pos++] << 16 | this.data[this.pos++] << 8 | this.data[this.pos++]) >>> 0; + return ((this.data[this.pos++] << 16) | (this.data[this.pos++] << 8) | this.data[this.pos++]) >>> 0; } get g4(): number { - return (this.data[this.pos++] << 24 | this.data[this.pos++] << 16 | this.data[this.pos++] << 8 | this.data[this.pos++]) >>> 0; + return ((this.data[this.pos++] << 24) | (this.data[this.pos++] << 16) | (this.data[this.pos++] << 8) | this.data[this.pos++]) >>> 0; } // signed get g4s(): number { - return this.data[this.pos++] << 24 | this.data[this.pos++] << 16 | this.data[this.pos++] << 8 | this.data[this.pos++]; + return (this.data[this.pos++] << 24) | (this.data[this.pos++] << 16) | (this.data[this.pos++] << 8) | this.data[this.pos++]; } get g8(): bigint { @@ -83,12 +83,12 @@ export default class Packet { } get gsmart(): number { - return (this.data[this.pos] < 0x80) ? this.g1 : (this.g2 - 0x8000); + return this.data[this.pos] < 0x80 ? this.g1 : this.g2 - 0x8000; } // signed get gsmarts(): number { - return (this.data[this.pos] < 0x80) ? (this.g1 - 0x40) : (this.g2 - 0xC000); + return this.data[this.pos] < 0x80 ? this.g1 - 0x40 : this.g2 - 0xc000; } get gjstr(): string { @@ -104,67 +104,67 @@ export default class Packet { p1 = (value: number): void => { this.data[this.pos++] = value; - } + }; p2 = (value: number): void => { this.data[this.pos++] = value >>> 8; this.data[this.pos++] = value; - } + }; ip2 = (value: number): void => { this.data[this.pos++] = value; this.data[this.pos++] = value >>> 8; - } + }; p3 = (value: number): void => { this.data[this.pos++] = value >>> 16; this.data[this.pos++] = value >>> 8; this.data[this.pos++] = value; - } + }; p4 = (value: number): void => { this.data[this.pos++] = value >>> 24; this.data[this.pos++] = value >>> 16; this.data[this.pos++] = value >>> 8; this.data[this.pos++] = value; - } + }; ip4 = (value: number): void => { this.data[this.pos++] = value; this.data[this.pos++] = value >>> 8; this.data[this.pos++] = value >>> 16; this.data[this.pos++] = value >>> 24; - } + }; p8 = (value: bigint): void => { this.p4(Number(value >> 32n)); - this.p4(Number(value & 0xFFFFFFFFn)); - } + this.p4(Number(value & 0xffffffffn)); + }; pjstr = (str: string): void => { for (let i = 0; i < str.length; i++) { this.data[this.pos++] = str.charCodeAt(i); } this.data[this.pos++] = 10; - } + }; pdata = (src: Uint8Array, length: number, offset: number): void => { for (let i = offset; i < offset + length; i++) { this.data[this.pos++] = src[i]; } - } + }; psize1 = (size: number): void => { this.data[this.pos - size - 1] = size; - } + }; bits = (): void => { this.bitPos = this.pos * 8; - } + }; bytes = (): void => { this.pos = ((this.bitPos + 7) / 8) >>> 0; - } + }; gBit = (n: number): number => { let bytePos = this.bitPos >>> 3; @@ -184,5 +184,5 @@ export default class Packet { } return value; - } + }; } diff --git a/src/js/jagex2/util/Censor.ts b/src/js/jagex2/util/Censor.ts index 908eb009..da485d7e 100644 --- a/src/js/jagex2/util/Censor.ts +++ b/src/js/jagex2/util/Censor.ts @@ -1,6 +1,5 @@ import Archive from '../io/Archive'; export default class Censor { - static unpack = (wordenc: Archive): void => { - }; + static unpack = (wordenc: Archive): void => {}; } diff --git a/src/js/jagex2/util/JsUtil.ts b/src/js/jagex2/util/JsUtil.ts index b555c087..41671098 100644 --- a/src/js/jagex2/util/JsUtil.ts +++ b/src/js/jagex2/util/JsUtil.ts @@ -1,54 +1,53 @@ import {decompress} from '../../vendor/bz2.js'; export const sleep = async (ms: number): Promise => new Promise(resolve => setTimeout(resolve, ms)); -export const downloadUrl = async (url: string): Promise => - new Uint8Array(await (await fetch(url)).arrayBuffer()); +export const downloadUrl = async (url: string): Promise => new Uint8Array(await (await fetch(url)).arrayBuffer()); export const downloadText = async (url: string): Promise => (await fetch(url)).text(); export const decompressBz2 = (data: Uint8Array, addMagic: boolean = true, prepend: boolean = true): Uint8Array => { - if (addMagic) { - const magic = Uint8Array.from(['B'.charCodeAt(0), 'Z'.charCodeAt(0), 'h'.charCodeAt(0), '1'.charCodeAt(0)]); + if (addMagic) { + const magic = Uint8Array.from(['B'.charCodeAt(0), 'Z'.charCodeAt(0), 'h'.charCodeAt(0), '1'.charCodeAt(0)]); - if (prepend) { - const temp = data; - data = new Uint8Array(magic.length + data.length); - data.set(temp, magic.length); - } + if (prepend) { + const temp = data; + data = new Uint8Array(magic.length + data.length); + data.set(temp, magic.length); + } - data.set(magic, 0); - } + data.set(magic, 0); + } - return decompress(data); + return decompress(data); }; export const decodeJpeg = async (data: Uint8Array | null): Promise => { - if (!data) { - throw new Error('Input jpeg data was null!'); - } - if (data[0] !== 0xff) { - // fix invalid JPEG header - data[0] = 0xff; - } - - // create img element - const img = document.createElement('img'); - img.src = 'data:image/jpeg;base64,' + btoa(String.fromCharCode(...data)); - - // wait for img to load - await new Promise(resolve => (img.onload = resolve)); - - // get imagedata from img element - const canvas = document.createElement('canvas'); - canvas.width = img.naturalWidth; - canvas.height = img.naturalHeight; - const ctx = canvas.getContext('2d'); - if (!ctx) { - throw new Error('Canvas 2d not found!!!!!!!!'); - } - ctx.drawImage(img, 0, 0); - return ctx.getImageData(0, 0, canvas.width, canvas.height); + if (!data) { + throw new Error('Input jpeg data was null!'); + } + if (data[0] !== 0xff) { + // fix invalid JPEG header + data[0] = 0xff; + } + + // create img element + const img = document.createElement('img'); + img.src = 'data:image/jpeg;base64,' + btoa(String.fromCharCode(...data)); + + // wait for img to load + await new Promise(resolve => (img.onload = resolve)); + + // get imagedata from img element + const canvas = document.createElement('canvas'); + canvas.width = img.naturalWidth; + canvas.height = img.naturalHeight; + const ctx = canvas.getContext('2d'); + if (!ctx) { + throw new Error('Canvas 2d not found!!!!!!!!'); + } + ctx.drawImage(img, 0, 0); + return ctx.getImageData(0, 0, canvas.width, canvas.height); }; export const arraycopy = (src: Uint32Array, srcPos: number, dst: Uint32Array, dstPos: number, length: number): void => { - while (length--) dst[dstPos++] = src[srcPos++]; + while (length--) dst[dstPos++] = src[srcPos++]; }; diff --git a/src/js/playground.ts b/src/js/playground.ts index 8675e259..446d696f 100644 --- a/src/js/playground.ts +++ b/src/js/playground.ts @@ -21,7 +21,7 @@ import SeqFrame from './jagex2/graphics/SeqFrame.js'; import Archive from './jagex2/io/Archive.js'; import Censor from './jagex2/util/Censor.js'; -import { downloadUrl } from './jagex2/util/JsUtil.js'; +import {downloadUrl} from './jagex2/util/JsUtil.js'; import Draw2D from './jagex2/graphics/Draw2D.js'; import Packet from './jagex2/io/Packet.js'; @@ -170,32 +170,37 @@ class Playground extends GameShell { // debug if (this.fontBold12) { - this.fontBold12.drawRight(this.width, this.fontBold12.fontHeight, `FPS: ${this.fps}`, 0xFFFF00); - this.fontBold12.drawRight(this.width, this.height, `${this.model.pitch},${this.model.yaw},${this.model.roll},${this.camera.pitch},${this.camera.x},${this.camera.z},${this.camera.y}`, 0xFFFF00); + this.fontBold12.drawRight(this.width, this.fontBold12.fontHeight, `FPS: ${this.fps}`, 0xffff00); + this.fontBold12.drawRight( + this.width, + this.height, + `${this.model.pitch},${this.model.yaw},${this.model.roll},${this.camera.pitch},${this.camera.x},${this.camera.z},${this.camera.y}`, + 0xffff00 + ); // controls let leftY = this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, `Model: ${this.model.id}`, 0xFFFF00); + this.fontBold12.draw(0, leftY, `Model: ${this.model.id}`, 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, 'Controls:', 0xFFFF00); + this.fontBold12.draw(0, leftY, 'Controls:', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, 'r - reset camera and model rotation + movement speed', 0xFFFF00); + this.fontBold12.draw(0, leftY, 'r - reset camera and model rotation + movement speed', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, '1 and 2 - change model', 0xFFFF00); + this.fontBold12.draw(0, leftY, '1 and 2 - change model', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, '[ and ] - adjust movement speed', 0xFFFF00); + this.fontBold12.draw(0, leftY, '[ and ] - adjust movement speed', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, 'left and right - adjust model yaw', 0xFFFF00); + this.fontBold12.draw(0, leftY, 'left and right - adjust model yaw', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, 'up and down - adjust model pitch', 0xFFFF00); + this.fontBold12.draw(0, leftY, 'up and down - adjust model pitch', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, '. and / - adjust model roll', 0xFFFF00); + this.fontBold12.draw(0, leftY, '. and / - adjust model roll', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, 'w and s - move camera along z axis', 0xFFFF00); + this.fontBold12.draw(0, leftY, 'w and s - move camera along z axis', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, 'a and d - move camera along x axis', 0xFFFF00); + this.fontBold12.draw(0, leftY, 'a and d - move camera along x axis', 0xffff00); leftY += this.fontBold12.fontHeight; - this.fontBold12.draw(0, leftY, 'q and e - move camera along y axis', 0xFFFF00); + this.fontBold12.draw(0, leftY, 'q and e - move camera along y axis', 0xffff00); } this.drawArea?.draw(0, 0); @@ -215,13 +220,13 @@ class Playground extends GameShell { id: parseInt(GameShell.getParameter('model')) || 0, pitch: parseInt(GameShell.getParameter('x')) || 0, yaw: parseInt(GameShell.getParameter('y')) || 0, - roll: parseInt(GameShell.getParameter('z')) || 0, + roll: parseInt(GameShell.getParameter('z')) || 0 }; camera = { x: parseInt(GameShell.getParameter('eyeX')) || 0, y: parseInt(GameShell.getParameter('eyeY')) || 0, z: parseInt(GameShell.getParameter('eyeZ')) || 420, - pitch: parseInt(GameShell.getParameter('eyePitch')) || 0, + pitch: parseInt(GameShell.getParameter('eyePitch')) || 0 }; updateKeysPressed() { diff --git a/src/js/vendor/bz2.js b/src/js/vendor/bz2.js index e17cd78b..07b0986f 100644 --- a/src/js/vendor/bz2.js +++ b/src/js/vendor/bz2.js @@ -2,330 +2,309 @@ // https://www.ncbi.nlm.nih.gov/IEB/ToolBox/CPP_DOC/lxr/source/src/util/compress/bzip2/crctable.c const crc32Table = [ - 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, - 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, - 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, - 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd, - 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, - 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, - 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95, - 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, - 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072, - 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca, - 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, - 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, - 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692, - 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, - 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, - 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a, - 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, - 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53, - 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b, - 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, - 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, - 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3, - 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, - 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3, - 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c, - 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, - 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, - 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654, - 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, - 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, - 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c, - 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4, + 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, + 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd, + 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, + 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, + 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072, 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca, + 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, + 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, + 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a, + 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53, + 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, + 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3, + 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3, + 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, + 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654, + 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, + 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4 ]; // generated from 1 << i, except for 32 const masks = [ - 0x00000000, 0x00000001, 0x00000003, 0x00000007, - 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, - 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, - 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, - 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff, - 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff, - 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff, - 0x0fffffff, 0x1fffffff, 0x3fffffff, -0x80000000, + 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, + 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff, 0x1fffffff, 0x3fffffff, -0x80000000 ]; function createOrderedHuffmanTable(lengths) { - const z = []; - for (let i = 0; i < lengths.length; i += 1) { - z.push([i, lengths[i]]); - } - z.push([lengths.length, -1]); - const table = []; - let start = z[0][0]; - let bits = z[0][1]; - for (let i = 0; i < z.length; i += 1) { - const finish = z[i][0]; - const endbits = z[i][1]; - if (bits) { - for (let code = start; code < finish; code += 1) { - table.push({ code, bits, symbol: undefined }); - } + const z = []; + for (let i = 0; i < lengths.length; i += 1) { + z.push([i, lengths[i]]); } - start = finish; - bits = endbits; - if (endbits === -1) { - break; + z.push([lengths.length, -1]); + const table = []; + let start = z[0][0]; + let bits = z[0][1]; + for (let i = 0; i < z.length; i += 1) { + const finish = z[i][0]; + const endbits = z[i][1]; + if (bits) { + for (let code = start; code < finish; code += 1) { + table.push({code, bits, symbol: undefined}); + } + } + start = finish; + bits = endbits; + if (endbits === -1) { + break; + } } - } - table.sort((a, b) => ((a.bits - b.bits) || (a.code - b.code))); - let tempBits = 0; - let symbol = -1; - const fastAccess = []; - let current; - for (let i = 0; i < table.length; i += 1) { - const t = table[i]; - symbol += 1; - if (t.bits !== tempBits) { - symbol <<= t.bits - tempBits; - tempBits = t.bits; - current = fastAccess[tempBits] = {}; + table.sort((a, b) => a.bits - b.bits || a.code - b.code); + let tempBits = 0; + let symbol = -1; + const fastAccess = []; + let current; + for (let i = 0; i < table.length; i += 1) { + const t = table[i]; + symbol += 1; + if (t.bits !== tempBits) { + symbol <<= t.bits - tempBits; + tempBits = t.bits; + current = fastAccess[tempBits] = {}; + } + t.symbol = symbol; + current[symbol] = t; } - t.symbol = symbol; - current[symbol] = t; - } - return { - table, - fastAccess, - }; + return { + table, + fastAccess + }; } function bwtReverse(src, primary) { - if (primary < 0 || primary >= src.length) { - throw RangeError('Out of bound'); - } - const unsorted = src.slice(); - src.sort((a, b) => a - b); - const start = {}; - for (let i = src.length - 1; i >= 0; i -= 1) { - start[src[i]] = i; - } - const links = []; - for (let i = 0; i < src.length; i += 1) { - links.push(start[unsorted[i]]++); // eslint-disable-line no-plusplus - } - let i; - const first = src[i = primary]; - const ret = []; - for (let j = 1; j < src.length; j += 1) { - const x = src[i = links[i]]; - if (x === undefined) { - ret.push(255); - } else { - ret.push(x); + if (primary < 0 || primary >= src.length) { + throw RangeError('Out of bound'); } - } - ret.push(first); - ret.reverse(); - return ret; -} - -export function decompress(bytes, checkCRC = false) { - let index = 0; - let bitfield = 0; - let bits = 0; - const read = (n) => { - if (n >= 32) { - const nd = n >> 1; - return read(nd) * (1 << nd) + read(n - nd); + const unsorted = src.slice(); + src.sort((a, b) => a - b); + const start = {}; + for (let i = src.length - 1; i >= 0; i -= 1) { + start[src[i]] = i; } - while (bits < n) { - bitfield = (bitfield << 8) + bytes[index]; - index += 1; - bits += 8; + const links = []; + for (let i = 0; i < src.length; i += 1) { + links.push(start[unsorted[i]]++); // eslint-disable-line no-plusplus } - const m = masks[n]; - const r = (bitfield >> (bits - n)) & m; - bits -= n; - bitfield &= ~(m << bits); - return r; - }; - - const magic = read(16); - if (magic !== 0x425A) { // 'BZ' - throw new Error('Invalid magic'); - } - const method = read(8); - if (method !== 0x68) { // h for huffman - throw new Error('Invalid method'); - } - - let blocksize = read(8); - if (blocksize >= 49 && blocksize <= 57) { // 1..9 - blocksize -= 48; - } else { - throw new Error('Invalid blocksize'); - } - - let out = new Uint8Array(bytes.length * 1.5); - let outIndex = 0; - let newCRC = -1; - while (true) { - const blocktype = read(48); - const crc = read(32) | 0; - if (blocktype === 0x314159265359) { - if (read(1)) { - throw new Error('do not support randomised'); - } - const pointer = read(24); - const used = []; - const usedGroups = read(16); - for (let i = 1 << 15; i > 0; i >>= 1) { - if (!(usedGroups & i)) { - for (let j = 0; j < 16; j += 1) { - used.push(false); - } - continue; // eslint-disable-line no-continue - } - const usedChars = read(16); - for (let j = 1 << 15; j > 0; j >>= 1) { - used.push(!!(usedChars & j)); - } - } - const groups = read(3); - if (groups < 2 || groups > 6) { - throw new Error('Invalid number of huffman groups'); - } - const selectorsUsed = read(15); - const selectors = []; - const mtf = Array.from({ length: groups }, (_, i) => i); - for (let i = 0; i < selectorsUsed; i += 1) { - let c = 0; - while (read(1)) { - c += 1; - if (c >= groups) { - throw new Error('MTF table out of range'); - } - } - const v = mtf[c]; - for (let j = c; j > 0; mtf[j] = mtf[--j]) { // eslint-disable-line no-plusplus - // nothing - } - selectors.push(v); - mtf[0] = v; - } - const symbolsInUse = used.reduce((a, b) => a + b, 0) + 2; - const tables = []; - for (let i = 0; i < groups; i += 1) { - let length = read(5); - const lengths = []; - for (let j = 0; j < symbolsInUse; j += 1) { - if (length < 0 || length > 20) { - throw new Error('Huffman group length outside range'); - } - while (read(1)) { - length -= (read(1) * 2) - 1; - } - lengths.push(length); - } - tables.push(createOrderedHuffmanTable(lengths)); - } - const favourites = []; - for (let i = 0; i < used.length - 1; i += 1) { - if (used[i]) { - favourites.push(i); + let i; + const first = src[(i = primary)]; + const ret = []; + for (let j = 1; j < src.length; j += 1) { + const x = src[(i = links[i])]; + if (x === undefined) { + ret.push(255); + } else { + ret.push(x); } - } - let decoded = 0; - let selectorPointer = 0; - let t; - let r; - let repeat = 0; - let repeatPower = 0; - const buffer = []; - while (true) { - decoded -= 1; - if (decoded <= 0) { - decoded = 50; - if (selectorPointer <= selectors.length) { - t = tables[selectors[selectorPointer]]; - selectorPointer += 1; - } + } + ret.push(first); + ret.reverse(); + return ret; +} + +export function decompress(bytes, checkCRC = false) { + let index = 0; + let bitfield = 0; + let bits = 0; + const read = n => { + if (n >= 32) { + const nd = n >> 1; + return read(nd) * (1 << nd) + read(n - nd); } - for (const b in t.fastAccess) { - if (!Object.prototype.hasOwnProperty.call(t.fastAccess, b)) { - continue; // eslint-disable-line no-continue - } - if (bits < b) { + while (bits < n) { bitfield = (bitfield << 8) + bytes[index]; index += 1; bits += 8; - } - r = t.fastAccess[b][bitfield >> (bits - b)]; - if (r) { - bitfield &= masks[bits -= b]; - r = r.code; - break; - } - } - if (r >= 0 && r <= 1) { - if (repeat === 0) { - repeatPower = 1; - } - repeat += repeatPower << r; - repeatPower <<= 1; - continue; // eslint-disable-line no-continue - } else { - const v = favourites[0]; - for (; repeat > 0; repeat -= 1) { - buffer.push(v); - } } - if (r === symbolsInUse - 1) { - break; - } else { - const v = favourites[r - 1]; - // eslint-disable-next-line no-plusplus - for (let j = r - 1; j > 0; favourites[j] = favourites[--j]) { - // nothing - } - favourites[0] = v; - buffer.push(v); - } - } - const nt = bwtReverse(buffer, pointer); - let i = 0; - while (i < nt.length) { - const c = nt[i]; - let count = 1; - if ((i < nt.length - 4) - && nt[i + 1] === c - && nt[i + 2] === c - && nt[i + 3] === c) { - count = nt[i + 4] + 4; - i += 5; + const m = masks[n]; + const r = (bitfield >> (bits - n)) & m; + bits -= n; + bitfield &= ~(m << bits); + return r; + }; + + const magic = read(16); + if (magic !== 0x425a) { + // 'BZ' + throw new Error('Invalid magic'); + } + const method = read(8); + if (method !== 0x68) { + // h for huffman + throw new Error('Invalid method'); + } + + let blocksize = read(8); + if (blocksize >= 49 && blocksize <= 57) { + // 1..9 + blocksize -= 48; + } else { + throw new Error('Invalid blocksize'); + } + + let out = new Uint8Array(bytes.length * 1.5); + let outIndex = 0; + let newCRC = -1; + while (true) { + const blocktype = read(48); + const crc = read(32) | 0; + if (blocktype === 0x314159265359) { + if (read(1)) { + throw new Error('do not support randomised'); + } + const pointer = read(24); + const used = []; + const usedGroups = read(16); + for (let i = 1 << 15; i > 0; i >>= 1) { + if (!(usedGroups & i)) { + for (let j = 0; j < 16; j += 1) { + used.push(false); + } + continue; // eslint-disable-line no-continue + } + const usedChars = read(16); + for (let j = 1 << 15; j > 0; j >>= 1) { + used.push(!!(usedChars & j)); + } + } + const groups = read(3); + if (groups < 2 || groups > 6) { + throw new Error('Invalid number of huffman groups'); + } + const selectorsUsed = read(15); + const selectors = []; + const mtf = Array.from({length: groups}, (_, i) => i); + for (let i = 0; i < selectorsUsed; i += 1) { + let c = 0; + while (read(1)) { + c += 1; + if (c >= groups) { + throw new Error('MTF table out of range'); + } + } + const v = mtf[c]; + for (let j = c; j > 0; mtf[j] = mtf[--j]) { + // eslint-disable-line no-plusplus + // nothing + } + selectors.push(v); + mtf[0] = v; + } + const symbolsInUse = used.reduce((a, b) => a + b, 0) + 2; + const tables = []; + for (let i = 0; i < groups; i += 1) { + let length = read(5); + const lengths = []; + for (let j = 0; j < symbolsInUse; j += 1) { + if (length < 0 || length > 20) { + throw new Error('Huffman group length outside range'); + } + while (read(1)) { + length -= read(1) * 2 - 1; + } + lengths.push(length); + } + tables.push(createOrderedHuffmanTable(lengths)); + } + const favourites = []; + for (let i = 0; i < used.length - 1; i += 1) { + if (used[i]) { + favourites.push(i); + } + } + let decoded = 0; + let selectorPointer = 0; + let t; + let r; + let repeat = 0; + let repeatPower = 0; + const buffer = []; + while (true) { + decoded -= 1; + if (decoded <= 0) { + decoded = 50; + if (selectorPointer <= selectors.length) { + t = tables[selectors[selectorPointer]]; + selectorPointer += 1; + } + } + for (const b in t.fastAccess) { + if (!Object.prototype.hasOwnProperty.call(t.fastAccess, b)) { + continue; // eslint-disable-line no-continue + } + if (bits < b) { + bitfield = (bitfield << 8) + bytes[index]; + index += 1; + bits += 8; + } + r = t.fastAccess[b][bitfield >> (bits - b)]; + if (r) { + bitfield &= masks[(bits -= b)]; + r = r.code; + break; + } + } + if (r >= 0 && r <= 1) { + if (repeat === 0) { + repeatPower = 1; + } + repeat += repeatPower << r; + repeatPower <<= 1; + continue; // eslint-disable-line no-continue + } else { + const v = favourites[0]; + for (; repeat > 0; repeat -= 1) { + buffer.push(v); + } + } + if (r === symbolsInUse - 1) { + break; + } else { + const v = favourites[r - 1]; + // eslint-disable-next-line no-plusplus + for (let j = r - 1; j > 0; favourites[j] = favourites[--j]) { + // nothing + } + favourites[0] = v; + buffer.push(v); + } + } + const nt = bwtReverse(buffer, pointer); + let i = 0; + while (i < nt.length) { + const c = nt[i]; + let count = 1; + if (i < nt.length - 4 && nt[i + 1] === c && nt[i + 2] === c && nt[i + 3] === c) { + count = nt[i + 4] + 4; + i += 5; + } else { + i += 1; + } + if (outIndex + count >= out.length) { + const old = out; + out = new Uint8Array(old.length * 2); + out.set(old); + } + for (let j = 0; j < count; j += 1) { + if (checkCRC) { + newCRC = (newCRC << 8) ^ crc32Table[((newCRC >> 24) ^ c) & 0xff]; + } + out[outIndex] = c; + outIndex += 1; + } + } + if (checkCRC) { + const calculatedCRC = newCRC ^ -1; + if (calculatedCRC !== crc) { + throw new Error(`CRC mismatch: ${calculatedCRC} !== ${crc}`); + } + newCRC = -1; + } + } else if (blocktype === 0x177245385090) { + read(bits & 0x07); // pad align + break; } else { - i += 1; + throw new Error('Invalid bz2 blocktype'); } - if (outIndex + count >= out.length) { - const old = out; - out = new Uint8Array(old.length * 2); - out.set(old); - } - for (let j = 0; j < count; j += 1) { - if (checkCRC) { - newCRC = (newCRC << 8) ^ crc32Table[((newCRC >> 24) ^ c) & 0xff]; - } - out[outIndex] = c; - outIndex += 1; - } - } - if (checkCRC) { - const calculatedCRC = newCRC ^ -1; - if (calculatedCRC !== crc) { - throw new Error(`CRC mismatch: ${calculatedCRC} !== ${crc}`); - } - newCRC = -1; - } - } else if (blocktype === 0x177245385090) { - read(bits & 0x07); // pad align - break; - } else { - throw new Error('Invalid bz2 blocktype'); } - } - return out.subarray(0, outIndex); + return out.subarray(0, outIndex); } diff --git a/src/js/vendor/midi.js b/src/js/vendor/midi.js index d4155774..3c1a5ddf 100644 --- a/src/js/vendor/midi.js +++ b/src/js/vendor/midi.js @@ -2,39 +2,39 @@ import TinyMidiPCM from './tinymidipcm/index.js'; // Fix iOS Audio Context by Blake Kus https://gist.github.com/kus/3f01d60569eeadefe3a1 // MIT license -(function() { - window.AudioContext = window.AudioContext || window.webkitAudioContext; - if (window.AudioContext) { - window.audioContext = new window.AudioContext(); - } - var fixAudioContext = function (e) { - if (window.audioContext) { - // Create empty buffer - var buffer = window.audioContext.createBuffer(1, 1, 22050); - var source = window.audioContext.createBufferSource(); - source.buffer = buffer; - // Connect to output (speakers) - source.connect(window.audioContext.destination); - // Play sound - if (source.start) { - source.start(0); - } else if (source.play) { - source.play(0); - } else if (source.noteOn) { - source.noteOn(0); - } - } - // Remove events - document.removeEventListener('touchstart', fixAudioContext); - document.removeEventListener('touchend', fixAudioContext); - document.removeEventListener('click', fixAudioContext); - }; - // iOS 6-8 - document.addEventListener('touchstart', fixAudioContext); - // iOS 9 - document.addEventListener('touchend', fixAudioContext); - // Safari - document.addEventListener('click', fixAudioContext); +(function () { + window.AudioContext = window.AudioContext || window.webkitAudioContext; + if (window.AudioContext) { + window.audioContext = new window.AudioContext(); + } + var fixAudioContext = function (e) { + if (window.audioContext) { + // Create empty buffer + var buffer = window.audioContext.createBuffer(1, 1, 22050); + var source = window.audioContext.createBufferSource(); + source.buffer = buffer; + // Connect to output (speakers) + source.connect(window.audioContext.destination); + // Play sound + if (source.start) { + source.start(0); + } else if (source.play) { + source.play(0); + } else if (source.noteOn) { + source.noteOn(0); + } + } + // Remove events + document.removeEventListener('touchstart', fixAudioContext); + document.removeEventListener('touchend', fixAudioContext); + document.removeEventListener('click', fixAudioContext); + }; + // iOS 6-8 + document.addEventListener('touchstart', fixAudioContext); + // iOS 9 + document.addEventListener('touchend', fixAudioContext); + // Safari + document.addEventListener('click', fixAudioContext); })(); // controlling tinymidipcm: @@ -58,14 +58,14 @@ import TinyMidiPCM from './tinymidipcm/index.js'; const tinyMidiPCM = new TinyMidiPCM({ renderInterval, - onPCMData: (pcm) => { + onPCMData: pcm => { let float32 = new Float32Array(pcm.buffer); let temp = new Float32Array(samples.length + float32.length); temp.set(samples, 0); temp.set(float32, samples.length); samples = temp; }, - onRenderEnd: (ms) => { + onRenderEnd: ms => { renderEndSeconds = Math.floor(startTime + Math.floor(ms / 1000)); }, bufferSize: 1024 * 100 @@ -83,7 +83,7 @@ import TinyMidiPCM from './tinymidipcm/index.js'; } let bufferSource = window.audioContext.createBufferSource(); - bufferSource.onended = function(event) { + bufferSource.onended = function (event) { const timeSeconds = Math.floor(window.audioContext.currentTime); if (renderEndSeconds > 0 && Math.abs(timeSeconds - renderEndSeconds) <= 2) { @@ -93,7 +93,7 @@ import TinyMidiPCM from './tinymidipcm/index.js'; window._tinyMidiPlay(currentMidiBuffer, -1); } } - } + }; const length = samples.length / channels; const audioBuffer = window.audioContext.createBuffer(channels, length, sampleRate); diff --git a/src/js/vendor/tinymidipcm/index.js b/src/js/vendor/tinymidipcm/index.js index 4a381387..9eba846e 100644 --- a/src/js/vendor/tinymidipcm/index.js +++ b/src/js/vendor/tinymidipcm/index.js @@ -37,10 +37,8 @@ class TinyMidiPCM { // check if node // http://philiplassen.com/2021/08/11/node-es6-emscripten.html if (typeof process !== 'undefined') { - const { dirname } = await import(/* webpackIgnore: true */ 'path'); - const { createRequire } = await import( - /* webpackIgnore: true */ 'module' - ); + const {dirname} = await import(/* webpackIgnore: true */ 'path'); + const {createRequire} = await import(/* webpackIgnore: true */ 'module'); globalThis.__dirname = dirname(import.meta.url); globalThis.require = createRequire(import.meta.url); @@ -59,24 +57,14 @@ class TinyMidiPCM { ensureInitialized() { if (!this.wasmModule) { - throw new Error( - `${this.constructor.name} not initalized. call .init()` - ); + throw new Error(`${this.constructor.name} not initalized. call .init()`); } } setSoundfont(buffer) { this.ensureInitialized(); - const { - _malloc, - _free, - _tsf_load_memory, - _tsf_set_output, - _tsf_channel_set_bank_preset, - _tsf_set_max_voices, - _tsf_channel_set_presetnumber - } = this.wasmModule; + const {_malloc, _free, _tsf_load_memory, _tsf_set_output, _tsf_channel_set_bank_preset, _tsf_set_max_voices, _tsf_channel_set_presetnumber} = this.wasmModule; _free(this.soundfontBufferPtr); @@ -85,19 +73,11 @@ class TinyMidiPCM { //_tsf_channel_set_bank_preset(this.soundfontPtr, 9, 128, 0); - this.soundfontPtr = _tsf_load_memory( - this.soundfontBufferPtr, - buffer.length - ); + this.soundfontPtr = _tsf_load_memory(this.soundfontBufferPtr, buffer.length); //_tsf_set_max_voices(this.soundfontPtr, 10); - _tsf_set_output( - this.soundfontPtr, - this.channels === 2 ? 0 : 2, - this.sampleRate, - this.gain - ); + _tsf_set_output(this.soundfontPtr, this.channels === 2 ? 0 : 2, this.sampleRate, this.gain); } getPCMBuffer() { @@ -105,18 +85,13 @@ class TinyMidiPCM { const pcm = new Uint8Array(this.bufferSize); - pcm.set( - this.wasmModule.HEAPU8.subarray( - this.pcmBufferPtr, - this.pcmBufferPtr + this.bufferSize - ) - ); + pcm.set(this.wasmModule.HEAPU8.subarray(this.pcmBufferPtr, this.pcmBufferPtr + this.bufferSize)); return pcm; } getMIDIMessagePtr(midiBuffer) { - const { _malloc, _free, _tml_load_memory } = this.wasmModule; + const {_malloc, _free, _tml_load_memory} = this.wasmModule; _free(this.midiBufferPtr); @@ -127,17 +102,9 @@ class TinyMidiPCM { } renderMIDIMessage(midiMessagePtr) { - const { _midi_render } = this.wasmModule; - - return _midi_render( - this.soundfontPtr, - midiMessagePtr, - this.channels, - this.sampleRate, - this.pcmBufferPtr, - this.bufferSize, - this.msecsPtr - ); + const {_midi_render} = this.wasmModule; + + return _midi_render(this.soundfontPtr, midiMessagePtr, this.channels, this.sampleRate, this.pcmBufferPtr, this.bufferSize, this.msecsPtr); } render(midiBuffer) { @@ -149,7 +116,7 @@ class TinyMidiPCM { window.clearTimeout(this.renderTimer); - const { setValue, getValue } = this.wasmModule; + const {setValue, getValue} = this.wasmModule; setValue(this.msecsPtr, 0, 'double'); diff --git a/src/js/viewer.ts b/src/js/viewer.ts index 3a3f6feb..70a7dfeb 100644 --- a/src/js/viewer.ts +++ b/src/js/viewer.ts @@ -22,7 +22,7 @@ import SeqFrame from './jagex2/graphics/SeqFrame.js'; import Archive from './jagex2/io/Archive.js'; import Censor from './jagex2/util/Censor.js'; -import { downloadText, downloadUrl } from './jagex2/util/JsUtil.js'; +import {downloadText, downloadUrl} from './jagex2/util/JsUtil.js'; import GameShell from './jagex2/client/GameShell.js'; import Packet from './jagex2/io/Packet'; @@ -53,13 +53,13 @@ export default class Client extends GameShell { pitch: parseInt(GameShell.getParameter('x')) || 0, yaw: parseInt(GameShell.getParameter('y')) || 0, roll: parseInt(GameShell.getParameter('z')) || 0, - built: null as Model | null, + built: null as Model | null }; camera = { x: parseInt(GameShell.getParameter('eyeX')) || 0, y: parseInt(GameShell.getParameter('eyeY')) || 0, z: parseInt(GameShell.getParameter('eyeZ')) || 420, - pitch: parseInt(GameShell.getParameter('eyePitch')) || 0, + pitch: parseInt(GameShell.getParameter('eyePitch')) || 0 }; async loadPack(url: string): Promise> { @@ -181,8 +181,8 @@ export default class Client extends GameShell { this.model.built.drawSimple(this.model.pitch, this.model.yaw, this.model.roll, this.camera.pitch, this.camera.x, this.camera.y, this.camera.z); // debug - this.fontBold12?.drawRight(this.width - 1, this.fontBold12.fontHeight, `FPS: ${this.fps}`, 0xFFFF00); - this.fontBold12?.draw(1, this.fontBold12.fontHeight, `ID: ${this.model.id}`, 0xFFFF00); + this.fontBold12?.drawRight(this.width - 1, this.fontBold12.fontHeight, `FPS: ${this.fps}`, 0xffff00); + this.fontBold12?.draw(1, this.fontBold12.fontHeight, `ID: ${this.model.id}`, 0xffff00); this.drawArea?.draw(0, 0); }; @@ -335,5 +335,4 @@ export default class Client extends GameShell { } const client = new Client(); -client.run().then(() => { -}); +client.run().then(() => {});