mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 06:18:42 +02:00
MISC: Add support for getting the save file through the RFA (#2004)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { SaveData } from "../types";
|
||||
import type { BaseServer } from "../Server/BaseServer";
|
||||
|
||||
export class RFAMessage {
|
||||
@@ -17,7 +18,17 @@ export class RFAMessage {
|
||||
}
|
||||
}
|
||||
|
||||
type ResultType = string | number | string[] | FileContent[] | RFAServerData[];
|
||||
type ResultType =
|
||||
| string
|
||||
| number
|
||||
| string[]
|
||||
| FileContent[]
|
||||
| RFAServerData[]
|
||||
| {
|
||||
identifier: string;
|
||||
binary: boolean;
|
||||
save: SaveData;
|
||||
};
|
||||
type FileMetadata = FileData | FileContent | FileLocation | FileServer;
|
||||
|
||||
export interface FileData {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -112,6 +114,37 @@ 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();
|
||||
|
||||
if (typeof saveData === "string") {
|
||||
return new RFAMessage({
|
||||
result: {
|
||||
identifier: Player.identifier,
|
||||
binary: false,
|
||||
save: saveData,
|
||||
},
|
||||
id: msg.id,
|
||||
});
|
||||
}
|
||||
|
||||
// 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
|
||||
let converted = "";
|
||||
for (let i = 0; i < saveData.length; i++) {
|
||||
converted += String.fromCharCode(saveData[i]);
|
||||
}
|
||||
|
||||
return new RFAMessage({
|
||||
result: {
|
||||
identifier: Player.identifier,
|
||||
binary: true,
|
||||
save: converted,
|
||||
},
|
||||
id: msg.id,
|
||||
});
|
||||
},
|
||||
|
||||
getAllServers: function (msg: RFAMessage): RFAMessage {
|
||||
const servers = GetAllServers().map(({ hostname, hasAdminRights, purchasedByPlayer }) => ({
|
||||
hostname,
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user