fix editor not loading files

This commit is contained in:
Olivier Gagnon 2021-12-21 13:41:11 -05:00
parent 41593e0dce
commit b2b682fa04
6 changed files with 74 additions and 29 deletions

24
dist/vendor.bundle.js vendored

File diff suppressed because one or more lines are too long

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
const { app, BrowserWindow, Menu, shell } = require("electron"); const { app, BrowserWindow, Menu, shell } = require("electron");
const greenworks = require("./greenworks"); const greenworks = require("./greenworks");
if (greenworks.init()) { if (greenworks.init()) {
@ -10,8 +10,26 @@ if (greenworks.init()) {
const debug = false; const debug = false;
let win = null;
require("http")
.createServer(async function (req, res) {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString(); // convert Buffer to string
});
req.on("end", () => {
const data = JSON.parse(body);
win.webContents.executeJavaScript(`document.saveFile("${data.filename}", "${data.code}")`).then((result) => {
res.write(result);
res.end();
});
});
})
.listen(9990);
function createWindow(killall) { function createWindow(killall) {
const win = new BrowserWindow({ win = new BrowserWindow({
show: false, show: false,
backgroundThrottling: false, backgroundThrottling: false,
backgroundColor: "#000000", backgroundColor: "#000000",
@ -41,7 +59,6 @@ function createWindow(killall) {
const achievements = greenworks.getAchievementNames(); const achievements = greenworks.getAchievementNames();
const intervalID = setInterval(async () => { const intervalID = setInterval(async () => {
const achs = await win.webContents.executeJavaScript("document.achievements"); const achs = await win.webContents.executeJavaScript("document.achievements");
console.log(achs);
for (const ach of achs) { for (const ach of achs) {
if (!achievements.includes(ach)) continue; if (!achievements.includes(ach)) continue;
greenworks.activateAchievement(ach, () => undefined); greenworks.activateAchievement(ach, () => undefined);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -14,6 +14,8 @@ import { HacknetServer } from "./Hacknet/HacknetServer";
import { CityName } from "./Locations/data/CityNames"; import { CityName } from "./Locations/data/CityNames";
import { Player } from "./Player"; import { Player } from "./Player";
import { Programs } from "./Programs/Programs"; import { Programs } from "./Programs/Programs";
import { isScriptFilename } from "./Script/isScriptFilename";
import { Script } from "./Script/Script";
import { GetAllServers, GetServer } from "./Server/AllServers"; import { GetAllServers, GetServer } from "./Server/AllServers";
import { SpecialServers } from "./Server/data/SpecialServers"; import { SpecialServers } from "./Server/data/SpecialServers";
import { Server } from "./Server/Server"; import { Server } from "./Server/Server";
@ -412,5 +414,30 @@ function calculateAchievements(): void {
export function initElectron(): void { export function initElectron(): void {
setAchievements([]); setAchievements([]);
initWebserver();
setInterval(calculateAchievements, 5000); setInterval(calculateAchievements, 5000);
} }
function initWebserver(): void {
(document as any).saveFile = function (filename: string, code: string): string {
const home = GetServer("home");
if (home === null) return "'home' server not found.";
if (home === null) return "Server should not be null but it is.";
if (isScriptFilename(filename)) {
//If the current script already exists on the server, overwrite it
for (let i = 0; i < home.scripts.length; i++) {
if (filename == home.scripts[i].filename) {
home.scripts[i].saveScript(filename, code, "home", home.scripts);
}
}
//If the current script does NOT exist, create a new one
const script = new Script();
script.saveScript(filename, code, "home", home.scripts);
home.scripts.push(script);
return "written";
}
return "not a script file";
};
}

@ -333,20 +333,19 @@ export function Root(props: IProps): React.ReactElement {
if (editorRef.current === null || monacoRef.current === null) return; if (editorRef.current === null || monacoRef.current === null) return;
if (!props.files && currentScript !== null) {
// Open currentscript
regenerateModel(currentScript);
editorRef.current.setModel(currentScript.model);
editorRef.current.setPosition(currentScript.lastPosition);
editorRef.current.revealLineInCenter(currentScript.lastPosition.lineNumber);
updateRAM(currentScript.code);
editorRef.current.focus();
return;
}
if (props.files) { if (props.files) {
const files = Object.entries(props.files); const files = Object.entries(props.files);
if (!files.length && currentScript !== null) {
// Open currentscript
regenerateModel(currentScript);
editorRef.current.setModel(currentScript.model);
editorRef.current.setPosition(currentScript.lastPosition);
editorRef.current.revealLineInCenter(currentScript.lastPosition.lineNumber);
updateRAM(currentScript.code);
editorRef.current.focus();
return;
}
if (!files.length) { if (!files.length) {
editorRef.current.focus(); editorRef.current.focus();
return; return;
@ -383,6 +382,8 @@ export function Root(props: IProps): React.ReactElement {
updateRAM(newScript.code); updateRAM(newScript.code);
} }
} }
} else {
console.log("here we need to load something if we can");
} }
editorRef.current.focus(); editorRef.current.focus();