Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lesleyrs committed Jul 11, 2024
1 parent 584cd91 commit c3108cb
Show file tree
Hide file tree
Showing 8 changed files with 2,040 additions and 432 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ Click [here](https://github.com/2004scape/Client2/tree/gh-pages) to view the cur

## Singleplayer

When worker=1 is set in the url it will load the server inside the browser on login, world=0 is also set by default to avoid being dependant on 2004scape.org. Locally this takes only a few seconds but on github pages it can take around 3 minutes.
Set world=999 in the client url to start a singleplayer server. Locally this takes only a few seconds but on github pages it can take around 3 minutes.

Saves will load from `data/players` and open save dialog on logout. Player logs are disabled to avoid excessive pop-ups.

To update server code you have to:
1. pack server caches in the main branch, copy `data/pack` and `data/players` from server to `src/public` in client
2. run `npm run bundle` in server, this will turn LoginThread into a worker blob and copies the server to `../Client2/src/public`
3. run `npm run dev`/`npm run local` or `npm run prod` for release build

NOTE:
- modpow, crc, randomuuid and compression are disabled, crypto.randomuuid() needs https. Cache+midis are fetched locally, but crcs are an http request.
- modpow, crc, and randomuuid are disabled, crypto.randomuuid() needs secure context. Cache and midis are fetched locally, but crcs are an http request.
- bzip2 compression could be used to build from sources, but that would slow login times even more (specifically for github pages).
- maybe simplify build if needed by having LoginThread be a separate worker file instead of an imported variable. But it's nice having only 1 file.

TODO:
- exclude dirs from being modified as crc checks fail due to it, then enable them again
- use a specific worker world to avoid getting stuck on fetching world data from 2004scape.org when world isn't 0
- let server store player saves (or download them instead?) + packs in db
- save packs in indexeddb after first login
- webrtc p2p multiplayer, possibly login server entrypoint?
- fix bug where worker instances aren't being terminated/closed, might have to use a message to let the server close it.
- fix see-through player models

## Site Index

Expand Down
2 changes: 1 addition & 1 deletion src/js/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ export abstract class Client extends GameShell {

if (!data) {
try {
if (GameShell.getParameter('worker') === '0') {
if (GameShell.getParameter('world') !== '999') {
data = await downloadUrl(`${Client.httpAddress}/${name}_${crc}.mid`);
} else {
data = await downloadUrl(`data/pack/client/songs/${name}.mid`);
Expand Down
12 changes: 2 additions & 10 deletions src/js/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ export async function setupConfiguration(): Promise<void> {
await world();
detail();
method();
worker();
}

// setup the world config.
async function world(): Promise<void> {
if (GameShell.getParameter('world').length === 0) {
GameShell.setParameter('world', '0');
GameShell.setParameter('world', '999');
}
if (GameShell.getParameter('world') === '0') {
if (GameShell.getParameter('world') === '0' || GameShell.getParameter('world') === '999') {
localConfiguration();
} else {
await liveConfiguration(window.location.protocol.startsWith('https'));
Expand All @@ -49,13 +48,6 @@ function method(): void {
}
}

// setup the clientstream type
function worker(): void {
if (GameShell.getParameter('worker').length === 0) {
GameShell.setParameter('worker', '1');
}
}

// ---

function localConfiguration(): void {
Expand Down
2 changes: 1 addition & 1 deletion src/js/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ class Game extends Client {
this.loginMessage1 = 'Connecting to server...';
await this.drawTitleScreen();
}
if (Game.getParameter('worker') === '0') {
if (Game.getParameter('world') !== '999') {
this.stream = new ClientStream(await ClientStream.openSocket({host: Client.serverAddress, port: 43594 + Client.portOffset}));
} else {
this.stream = new ClientWorkerStream();
Expand Down
52 changes: 34 additions & 18 deletions src/js/jagex2/io/ClientWorkerStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,40 @@ class WorkerReader {
}

private onmessage = (e: MessageEvent): void => {
if (this.closed) {
throw new Error('WorkerReader is closed!');
}
const event: WorkerEvent = new WorkerEvent(new Uint8Array(e.data));
if (this.event) {
this.queue.addTail(event);
} else {
this.event = event;
}
this.total += event.len;
if (!this.callback) {
return;
}
this.callback(this.event);
this.callback = null;
// check for the overflow after the callback
if (this.total > this.limit) {
throw new Error('buffer overflow');
switch (e.data.type) {
case 'save': {
console.log(e.data.value, e.data.path);
const a: HTMLAnchorElement = document.createElement('a');
a.href = e.data.value;
a.download = e.data.path.split('/').pop();
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(e.data.value);
break;
}
default: {
if (this.closed) {
throw new Error('WorkerReader is closed!');
}
const event: WorkerEvent = new WorkerEvent(new Uint8Array(e.data));
if (this.event) {
this.queue.addTail(event);
} else {
this.event = event;
}
this.total += event.len;
if (!this.callback) {
return;
}
this.callback(this.event);
this.callback = null;
// check for the overflow after the callback
if (this.total > this.limit) {
throw new Error('buffer overflow');
}
break;
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/js/jagex2/io/Packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export default class Packet extends Hashable {
this.gdata(length, 0, temp);

const bigRaw: bigint = bytesToBigInt(temp);
const bigEnc: bigint = GameShell.getParameter('worker') === '0' ? bigIntModPow(bigRaw, exp, mod) : bigRaw;
const bigEnc: bigint = GameShell.getParameter('world') !== '999' ? bigIntModPow(bigRaw, exp, mod) : bigRaw;
const rawEnc: Uint8Array = bigIntToBytes(bigEnc);

this.pos = 0;
Expand Down
Loading

0 comments on commit c3108cb

Please sign in to comment.