Skip to content

Commit

Permalink
added getting the save file
Browse files Browse the repository at this point in the history
  • Loading branch information
G4mingJon4s committed Mar 6, 2025
1 parent 23ad555 commit 6837ab9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/RemoteFileAPI/MessageDefinitions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SaveData } from "../types";
import type { BaseServer } from "../Server/BaseServer";

export class RFAMessage {
Expand All @@ -17,7 +18,16 @@ export class RFAMessage {
}
}

type ResultType = string | number | string[] | FileContent[] | RFAServerData[];
type ResultType =
| string
| number
| string[]
| FileContent[]
| RFAServerData[]
| {
identifier: string;
save: SaveData;
};
type FileMetadata = FileData | FileContent | FileLocation | FileServer;

export interface FileData {
Expand Down
20 changes: 19 additions & 1 deletion src/RemoteFileAPI/MessageHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import {
} from "./MessageDefinitions";

import libSource from "../ScriptEditor/NetscriptDefinitions.d.ts?raw";
import { saveObject } from "../SaveObject";
import { Player } from "@player";

function error(errorMsg: string, { id }: RFAMessage): RFAMessage {
return new RFAMessage({ error: errorMsg, id: id });
}

export const RFARequestHandler: Record<string, (message: RFAMessage) => RFAMessage> = {
export const RFARequestHandler: Record<string, (message: RFAMessage) => RFAMessage | Promise<RFAMessage>> = {
pushFile: function (msg: RFAMessage): RFAMessage {
if (!isFileData(msg.params)) return error("Misses parameters", msg);

Expand Down Expand Up @@ -112,6 +114,22 @@ export const RFARequestHandler: Record<string, (message: RFAMessage) => RFAMessa
return new RFAMessage({ result: libSource, id: msg.id });
},

getSaveFile: async function (msg: RFAMessage): Promise<RFAMessage> {
const saveData = await saveObject.getSaveData();
// We can't serialize the Uint8Array directly, so we convert every integer to a character and make a string out of the array
// The external editor can simply recreate the save by converting each char back to their char code
const converted =
typeof saveData === "string" ? saveData : saveData.reduce((acc, cur) => acc + String.fromCharCode(cur), "");

return new RFAMessage({
result: {
identifier: Player.identifier,
save: converted,
},
id: msg.id,
});
},

getAllServers: function (msg: RFAMessage): RFAMessage {
const servers = GetAllServers().map(({ hostname, hasAdminRights, purchasedByPlayer }) => ({
hostname,
Expand Down
5 changes: 5 additions & 0 deletions src/RemoteFileAPI/Remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@ function handleMessageEvent(this: WebSocket, e: MessageEvent): void {
}
const response = RFARequestHandler[msg.method](msg);
if (!response) return;

if (response instanceof Promise) {
void response.then((data) => this.send(JSON.stringify(data)));
return;
}
this.send(JSON.stringify(response));
}

0 comments on commit 6837ab9

Please sign in to comment.