Skip to content

Commit 6837ab9

Browse files
committed
added getting the save file
1 parent 23ad555 commit 6837ab9

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/RemoteFileAPI/MessageDefinitions.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SaveData } from "../types";
12
import type { BaseServer } from "../Server/BaseServer";
23

34
export class RFAMessage {
@@ -17,7 +18,16 @@ export class RFAMessage {
1718
}
1819
}
1920

20-
type ResultType = string | number | string[] | FileContent[] | RFAServerData[];
21+
type ResultType =
22+
| string
23+
| number
24+
| string[]
25+
| FileContent[]
26+
| RFAServerData[]
27+
| {
28+
identifier: string;
29+
save: SaveData;
30+
};
2131
type FileMetadata = FileData | FileContent | FileLocation | FileServer;
2232

2333
export interface FileData {

src/RemoteFileAPI/MessageHandlers.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import {
1313
} from "./MessageDefinitions";
1414

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

1719
function error(errorMsg: string, { id }: RFAMessage): RFAMessage {
1820
return new RFAMessage({ error: errorMsg, id: id });
1921
}
2022

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

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

117+
getSaveFile: async function (msg: RFAMessage): Promise<RFAMessage> {
118+
const saveData = await saveObject.getSaveData();
119+
// We can't serialize the Uint8Array directly, so we convert every integer to a character and make a string out of the array
120+
// The external editor can simply recreate the save by converting each char back to their char code
121+
const converted =
122+
typeof saveData === "string" ? saveData : saveData.reduce((acc, cur) => acc + String.fromCharCode(cur), "");
123+
124+
return new RFAMessage({
125+
result: {
126+
identifier: Player.identifier,
127+
save: converted,
128+
},
129+
id: msg.id,
130+
});
131+
},
132+
115133
getAllServers: function (msg: RFAMessage): RFAMessage {
116134
const servers = GetAllServers().map(({ hostname, hasAdminRights, purchasedByPlayer }) => ({
117135
hostname,

src/RemoteFileAPI/Remote.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ function handleMessageEvent(this: WebSocket, e: MessageEvent): void {
6060
}
6161
const response = RFARequestHandler[msg.method](msg);
6262
if (!response) return;
63+
64+
if (response instanceof Promise) {
65+
void response.then((data) => this.send(JSON.stringify(data)));
66+
return;
67+
}
6368
this.send(JSON.stringify(response));
6469
}

0 commit comments

Comments
 (0)