mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-17 23:08:36 +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";
|
import type { BaseServer } from "../Server/BaseServer";
|
||||||
|
|
||||||
export class RFAMessage {
|
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;
|
type FileMetadata = FileData | FileContent | FileLocation | FileServer;
|
||||||
|
|
||||||
export interface FileData {
|
export interface FileData {
|
||||||
|
|||||||
@@ -13,12 +13,14 @@ import {
|
|||||||
} from "./MessageDefinitions";
|
} from "./MessageDefinitions";
|
||||||
|
|
||||||
import libSource from "../ScriptEditor/NetscriptDefinitions.d.ts?raw";
|
import libSource from "../ScriptEditor/NetscriptDefinitions.d.ts?raw";
|
||||||
|
import { saveObject } from "../SaveObject";
|
||||||
|
import { Player } from "@player";
|
||||||
|
|
||||||
function error(errorMsg: string, { id }: RFAMessage): RFAMessage {
|
function error(errorMsg: string, { id }: RFAMessage): RFAMessage {
|
||||||
return new RFAMessage({ error: errorMsg, id: id });
|
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 {
|
pushFile: function (msg: RFAMessage): RFAMessage {
|
||||||
if (!isFileData(msg.params)) return error("Misses parameters", msg);
|
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 });
|
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 {
|
getAllServers: function (msg: RFAMessage): RFAMessage {
|
||||||
const servers = GetAllServers().map(({ hostname, hasAdminRights, purchasedByPlayer }) => ({
|
const servers = GetAllServers().map(({ hostname, hasAdminRights, purchasedByPlayer }) => ({
|
||||||
hostname,
|
hostname,
|
||||||
|
|||||||
@@ -60,5 +60,10 @@ function handleMessageEvent(this: WebSocket, e: MessageEvent): void {
|
|||||||
}
|
}
|
||||||
const response = RFARequestHandler[msg.method](msg);
|
const response = RFARequestHandler[msg.method](msg);
|
||||||
if (!response) return;
|
if (!response) return;
|
||||||
|
|
||||||
|
if (response instanceof Promise) {
|
||||||
|
void response.then((data) => this.send(JSON.stringify(data)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
this.send(JSON.stringify(response));
|
this.send(JSON.stringify(response));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user