bitburner-src/src/utils/v1APIBreak.ts

151 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-07-19 20:21:12 +02:00
import { AugmentationNames } from "../Augmentation/data/AugmentationNames";
import { PlayerOwnedAugmentation } from "../Augmentation/PlayerOwnedAugmentation";
import { Player } from "@player";
import { Script } from "../Script/Script";
2021-11-03 03:11:22 +01:00
import { GetAllServers } from "../Server/AllServers";
FILES: Path rework & typesafety (#479) * Added new types for various file paths, all in the Paths folder. * TypeSafety and other helper functions related to these types * Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands * Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way * Server.textFiles is now a map * TextFile no longer uses a fn property, now it is filename * Added a shared ContentFile interface for shared functionality between TextFile and Script. * related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa. * File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root. * Singularized the MessageFilename and LiteratureName enums * Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath). * Fix several issues with tab completion, which included pretty much a complete rewrite * Changed the autocomplete display options so there's less chance it clips outside the display area. * Turned CompletedProgramName into an enum. * Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine. * For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
import { resolveTextFilePath } from "../Paths/TextFilePath";
import { resolveScriptFilePath } from "../Paths/ScriptFilePath";
2021-11-03 03:11:22 +01:00
2021-11-10 05:42:36 +01:00
const detect: [string, string][] = [
["getHackTime", "returns milliseconds"],
["getGrowTime", "returns milliseconds"],
["getWeakenTime", "returns milliseconds"],
["getActionTime", "returns milliseconds"],
["hackAnalyzePercent", "renamed 'hackAnalyze' and returns decimal"],
["hackChance", "renamed 'hackAnalyzeChance'"],
["basic.calculateSkill", "renamed 'skills.calculateSkill'"],
["basic.calculateExp", "renamed 'skills.calculateExp'"],
["basic.hackChance", "renamed 'hacking.hackChance'"],
["basic.hackExp", "renamed 'hacking.hackExp'"],
["basic.hackPercent", "renamed 'hacking.hackPercent'"],
["basic.growPercent", "renamed 'hacking.growPercent'"],
["basic.hackTime", "renamed 'hacking.hackTime'"],
["basic.growTime", "renamed 'hacking.growTime'"],
["basic.weakenTime", "renamed 'hacking.weakenTime'"],
["write", "needs to be awaited"],
["scp", "needs to be awaited"],
2022-10-09 07:25:31 +02:00
["sleep", "Can no longer be called simultaneously."],
2021-11-10 05:42:36 +01:00
["hacking_skill", "renamed 'hacking'"],
2021-11-12 21:56:48 +01:00
["tryWrite", "renamed 'tryWritePort'"],
2021-11-10 05:42:36 +01:00
];
2021-11-03 03:11:22 +01:00
const changes: [RegExp, string][] = [
[/ns.getHackTime/g, "((...a)=>ns.getHackTime(...a)/1000)"],
[/ns.getGrowTime/g, "((...a)=>ns.getGrowTime(...a)/1000)"],
[/ns.getWeakenTime/g, "((...a)=>ns.getWeakenTime(...a)/1000)"],
[/ns.bladeburner.getActionTime/g, "((...a)=>ns.bladeburner.getActionTime(...a)/1000)"],
[/ns.hackAnalyzePercent/g, "((...a)=>ns.hackAnalyze(...a)*100)"],
[/ns.hackChance/g, "ns.hackAnalyzeChance"],
2021-11-12 21:56:48 +01:00
[/ns.tryWrite/g, "ns.tryWritePort"],
2021-11-03 03:58:40 +01:00
[/formulas.basic.calculateSkill/g, "formulas.skills.calculateSkill"],
[/formulas.basic.calculateExp/g, "formulas.skills.calculateExp"],
[/formulas.basic.hackChance/g, "formulas.hacking.hackChance"],
[/formulas.basic.hackExp/g, "formulas.hacking.hackExp"],
[/formulas.basic.hackPercent/g, "formulas.hacking.hackPercent"],
[/formulas.basic.growPercent/g, "formulas.hacking.growPercent"],
[/formulas.basic.hackTime/g, "formulas.hacking.hackTime"],
[/formulas.basic.growTime/g, "formulas.hacking.growTime"],
[/formulas.basic.weakenTime/g, "formulas.hacking.weakenTime"],
2021-11-03 03:11:22 +01:00
];
function hasChanges(code: string): boolean {
for (const change of changes) {
if (code.match(change[0])) return true;
}
return false;
}
function convert(code: string): string {
2021-11-03 03:11:22 +01:00
const lines = code.split("\n");
const out: string[] = [];
for (let i = 0; i < lines.length; i++) {
const orig = lines[i];
let line = lines[i];
for (const change of changes) {
line = line.replace(change[0], change[1]);
}
if (line != orig) {
out.push(`// =============================== original line ===============================`);
2021-11-03 03:49:46 +01:00
out.push(`/**`);
out.push(` * ${orig}`);
out.push(" */");
2021-11-03 03:11:22 +01:00
out.push(`// =============================================================================`);
}
out.push(line);
}
FILES: Path rework & typesafety (#479) * Added new types for various file paths, all in the Paths folder. * TypeSafety and other helper functions related to these types * Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands * Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way * Server.textFiles is now a map * TextFile no longer uses a fn property, now it is filename * Added a shared ContentFile interface for shared functionality between TextFile and Script. * related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa. * File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root. * Singularized the MessageFilename and LiteratureName enums * Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath). * Fix several issues with tab completion, which included pretty much a complete rewrite * Changed the autocomplete display options so there's less chance it clips outside the display area. * Turned CompletedProgramName into an enum. * Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine. * For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
code = out.join("\n");
return code;
2021-11-03 03:11:22 +01:00
}
2022-07-19 20:21:12 +02:00
export function AwardNFG(n = 1): void {
const nf = Player.augmentations.find((a) => a.name === AugmentationNames.NeuroFluxGovernor);
if (nf) {
nf.level += n;
} else {
const nf = new PlayerOwnedAugmentation(AugmentationNames.NeuroFluxGovernor);
nf.level = n;
Player.augmentations.push(nf);
}
}
2022-07-26 21:09:11 +02:00
export interface IFileLine {
file: string;
line: number;
content: string;
}
2021-11-03 03:11:22 +01:00
export function v1APIBreak(): void {
2021-11-10 05:42:36 +01:00
let txt = "";
for (const server of GetAllServers()) {
for (const change of detect) {
const s: IFileLine[] = [];
const scriptsArray: Script[] = Array.isArray(server.scripts)
? (server.scripts as Script[])
: [...server.scripts.values()];
for (const script of scriptsArray) {
2021-11-10 05:42:36 +01:00
const lines = script.code.split("\n");
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes(change[0])) {
s.push({
file: script.filename,
line: i + 1,
2022-07-26 21:09:11 +02:00
content: "",
2021-11-10 05:42:36 +01:00
});
}
}
}
if (s.length === 0) continue;
txt += `// Detected change ${change[0]}, reason: ${change[1]}\n`;
2021-11-10 05:42:36 +01:00
for (const fl of s) {
txt += `${fl.file}:${fl.line}\n`;
2021-11-10 05:42:36 +01:00
}
}
}
if (txt !== "") {
const home = Player.getHomeComputer();
FILES: Path rework & typesafety (#479) * Added new types for various file paths, all in the Paths folder. * TypeSafety and other helper functions related to these types * Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands * Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way * Server.textFiles is now a map * TextFile no longer uses a fn property, now it is filename * Added a shared ContentFile interface for shared functionality between TextFile and Script. * related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa. * File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root. * Singularized the MessageFilename and LiteratureName enums * Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath). * Fix several issues with tab completion, which included pretty much a complete rewrite * Changed the autocomplete display options so there's less chance it clips outside the display area. * Turned CompletedProgramName into an enum. * Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine. * For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
const textPath = resolveTextFilePath("v1_DETECTED_CHANGES.txt");
if (!textPath) return console.error("Filepath unexpectedly failed to parse");
home.writeToTextFile(textPath, txt);
2021-11-10 05:42:36 +01:00
}
// API break function is called before version31 / 2.3.0 changes - scripts is still an array
for (const server of GetAllServers() as unknown as { scripts: Script[] }[]) {
2021-11-03 03:11:22 +01:00
const backups: Script[] = [];
for (const script of server.scripts) {
if (!hasChanges(script.code)) continue;
FILES: Path rework & typesafety (#479) * Added new types for various file paths, all in the Paths folder. * TypeSafety and other helper functions related to these types * Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands * Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way * Server.textFiles is now a map * TextFile no longer uses a fn property, now it is filename * Added a shared ContentFile interface for shared functionality between TextFile and Script. * related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa. * File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root. * Singularized the MessageFilename and LiteratureName enums * Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath). * Fix several issues with tab completion, which included pretty much a complete rewrite * Changed the autocomplete display options so there's less chance it clips outside the display area. * Turned CompletedProgramName into an enum. * Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine. * For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 16:26:57 +02:00
// Sanitize first before combining
const oldFilename = resolveScriptFilePath(script.filename);
const filename = resolveScriptFilePath("BACKUP_" + oldFilename);
if (!filename) {
console.error(`Unexpected error resolving backup path for ${script.filename}`);
continue;
}
backups.push(new Script(filename, script.code, script.server));
2021-11-03 03:11:22 +01:00
script.code = convert(script.code);
}
server.scripts = server.scripts.concat(backups);
}
}