Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename extension to js for syntax highlighting. add socket.io.slim.js… #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
home/*
!home/socketburner.ns
built/home/*
yarn.lock
package-lock.json
!built/home/socketburner.js
!built/home/socket.io.slim.js
52 changes: 47 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,59 @@ A WebSocket script mirroring service for the game [Bitburner](https://github.com
* [`nodejs`](https://nodejs.org/)
* [`yarn`](https://yarnpkg.com/)

### Installation and Usage
---

### Installation
#### Linux
In your terminal:
```
git clone https://github.com/MasonD/socketburner.git
cd socketburner/
yarn
```

Then save all the scripts in `built/home/` into their in-game equivalents using the in-game `nano`. E.g., for `built/home/socketburner.js`:
```
nano socketburner.js
```

---

### Usage
In-game:
```
run socketburner.js
```

#### Starting `socketburner`
#### Linux
In your terminal:
```
cd socketburner/
yarn start
cp <directory_of_your_scripts>/* home/
```
* Save the contents of `home/socketburner.ns` to an in-game file `socketburner.ns` and run it:

Any changes made to files in `built/home/` will now be mirrored to files in the game.

To recursively copy scripts from a directory `<scripts_directory>/` into the game:
```
cp -r <scripts_directory>/* built/home/
```

Note that the above command will cause `socketburner` to fail if the directory used contains files that don't have the extensions ".txt", ".js", ".ns" or ".script".

#### Stopping `socketburner`
In-game:
```
nano socketburner.ns
run socketburner.ns
Ctrl+/
```

then, in the terminal from where you ran `yarn start`:
```
Ctrl+c
```

---

### Notes
* Deleting, moving or renaming files in `built/home/` whilst `socketburner` is running will cause it to break.
9 changes: 9 additions & 0 deletions built/home/socket.io.slim.js

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions built/home/socketburner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const // exposes document for free
object_document = parent["document"],
// takes a file, creates a blob, and returns a url
string_get_url = (ns, string_file, string_mime_type) => URL.createObjectURL(new Blob([
new TextEncoder().encode(ns.read !== undefined ? ns.read(string_file) : undefined).buffer,
], {
type: string_mime_type,
})),
// loads a script for use
void_load_script = (string_url_script) => new Promise((resolve) => {
const script = object_document.createElement("script");
(script.onload = resolve),
(script.src = string_url_script),
object_document.getElementsByTagName("head")[0].appendChild(script);
}),
// responds to keyboard events
void_responder = (object_keyboard_event, array_key_action) => {
if (object_keyboard_event.ctrlKey &&
!object_keyboard_event.getModifierState(object_keyboard_event.key) &&
object_keyboard_event.key.toLowerCase() === array_key_action[0])
switch (array_key_action.length) {
case 2:
array_key_action[1]();
break;
case 3:
array_key_action[1](array_key_action[2]);
break;
default:
throw new Error(`Invalid parameter passed:\n${JSON.stringify(array_key_action)}`);
}
};
export const main = (ns) => __awaiter(void 0, void 0, void 0, function* () {
// start hotkey listener
const void_listener = (object_keyboard_event) => {
try {
void_responder(object_keyboard_event, ["/", parent.quitSocketBurner]);
}
catch (error) {
console.log(error),
object_document.removeEventListener("keydown", void_listener);
}
};
// load socket.io
parent.io ||
(yield void_load_script(string_get_url(ns, "socket.io.slim.js", "application/javascript")));
const socket = io("http://localhost:3000", { secure: !1 });
socket.on("message", ({ name: name, contents: contents, }) => {
ns.write(name, contents, "w");
}),
object_document.addEventListener("keydown", void_listener),
// don't stop unless the exit signal is received
yield new Promise((resolve) => {
parent.quitSocketBurner = () => {
ns.tprint("Exit signal received."), socket.close(), resolve();
};
});
});
23 changes: 23 additions & 0 deletions built/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";
const server = require("http").createServer(), io = require("socket.io").listen(server), path = require("path"), fs = require("fs"), watch = require("node-watch"), watchPath = path.resolve(__dirname, "../home"), port = 3000;
io.origins("*:*"),
io.on("connection", (socket) => {
console.log("a user connected");
const watcher = watch(watchPath, { recursive: !0 }, (evt, name) => {
console.log(name),
fs.readFile(name, (err, contents) => {
if (err)
throw err;
socket.send({
name: path.relative(watchPath, name),
contents: contents.toString(),
});
});
});
socket.on("disconnect", () => {
watcher.close(), console.log("user disconnected");
});
}),
server.listen(port, () => {
console.log(`listening on *:${port}`);
});
29 changes: 0 additions & 29 deletions home/socketburner.ns

This file was deleted.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
"name": "socketburner",
"version": "0.0.0",
"description": "A WebSocket script mirroring service for bitburner.",
"main": "server/index.js",
"main": "built/server/index.js",
"repository": "[email protected]:MasonD/socketburner.git",
"author": "Mason Dechaineux <[email protected]>",
"license": "MIT",
"private": false,
"dependencies": {
"@types/node": "^14.0.5",
"http": "^0.0.0",
"node-watch": "^0.6.2",
"socket.io": "^2.2.0"
"socket.io": "^2.3.0"
},
"scripts": {
"start": "node server/index.js"
"start": "node built/server/index.js --poll"
}
}
26 changes: 0 additions & 26 deletions server/index.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/home/socket.io.slim.js

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions src/home/socketburner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
interface CustomWindow extends Window {
quitSocketBurner: Function;
io: any;
}
declare let parent: CustomWindow;
const // exposes document for free
object_document = parent["document"],
// takes a file, creates a blob, and returns a url
string_get_url = (
ns: {
write?: (arg0: string, arg1: string, arg2: string) => void;
tprint?: (arg0: string) => void;
read?: (arg0: string) => string;
},
string_file: string,
string_mime_type: string
) =>
URL.createObjectURL(
new Blob(
[
new TextEncoder().encode(
ns.read !== undefined ? ns.read(string_file) : undefined
).buffer,
],
{
type: string_mime_type,
}
)
),
// loads a script for use
void_load_script = (string_url_script: string) =>
new Promise((resolve) => {
const script = object_document.createElement("script");
(script.onload = resolve),
(script.src = string_url_script),
object_document.getElementsByTagName("head")[0].appendChild(script);
}),
// responds to keyboard events
void_responder = (
object_keyboard_event: KeyboardEvent,
array_key_action: any[]
) => {
if (
object_keyboard_event.ctrlKey &&
!object_keyboard_event.getModifierState(object_keyboard_event.key) &&
object_keyboard_event.key.toLowerCase() === array_key_action[0]
)
switch (array_key_action.length) {
case 2:
array_key_action[1]();
break;
case 3:
array_key_action[1](array_key_action[2]);
break;
default:
throw new Error(
`Invalid parameter passed:\n${JSON.stringify(array_key_action)}`
);
}
};
export const main = async (ns: {
write: (arg0: string, arg1: string, arg2: string) => void;
tprint: (arg0: string) => void;
}) => {
// start hotkey listener
const void_listener = (object_keyboard_event: KeyboardEvent) => {
try {
void_responder(object_keyboard_event, ["/", parent.quitSocketBurner]);
} catch (error) {
console.log(error),
object_document.removeEventListener("keydown", void_listener);
}
};
// load socket.io
parent.io ||
(await void_load_script(
string_get_url(ns, "socket.io.slim.js", "application/javascript")
));
const socket = io("http://localhost:3000", { secure: !1 });
socket.on(
"message",
({
name: name,
contents: contents,
}: {
name: string;
contents: string;
}) => {
ns.write(name, contents, "w");
}
),
object_document.addEventListener("keydown", void_listener),
// don't stop unless the exit signal is received
await new Promise((resolve) => {
parent.quitSocketBurner = () => {
ns.tprint("Exit signal received."), socket.close(), resolve();
};
});
};
37 changes: 37 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const server = require("http").createServer(),
io = require("socket.io").listen(server),
path = require("path"),
fs = require("fs"),
watch = require("node-watch"),
watchPath = path.resolve(__dirname, "../home"),
port = 3000;
io.origins("*:*"),
io.on(
"connection",
(socket: {
send: (arg0: { name: any; contents: any }) => void;
on: (arg0: string, arg1: () => void) => void;
}) => {
console.log("a user connected");
const watcher = watch(
watchPath,
{ recursive: !0 },
(evt: any, name: any) => {
console.log(name),
fs.readFile(name, (err: any, contents: { toString: () => any }) => {
if (err) throw err;
socket.send({
name: path.relative(watchPath, name),
contents: contents.toString(),
});
});
}
);
socket.on("disconnect", () => {
watcher.close(), console.log("user disconnected");
});
}
),
server.listen(port, () => {
console.log(`listening on *:${port}`);
});
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "es6",
"strict": true,
},
"include": ["./src/**/*"],
"exclude": ["./src/home/socket.io.slim.js", "./node_modules"],
}
Loading