Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MISC: Add support for getting the save file through the RFA #2004

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}