Skip to content

Commit

Permalink
feat: vscode transmitter extensions upport (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuyk authored Jun 14, 2024
1 parent 6571b9c commit b417a77
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ webview/pages/plugins.ts
.env
test/
tmp/
src/scratchpad/*.ts

src/plugins/sandbox-*

Expand Down
13 changes: 13 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## Version 30

### Code Changes

- Added a `vscode transmitter` for debug mode server and client.
- Allows for code to be executed from VSCode using the Rebar Transmitter

### Docs Changes

- Added vscode transmitter extension page

---

## Version 29

### Code Changes
Expand Down
Binary file added docs/static/transmitter-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions docs/vscode-transmitter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# VSCode Transmitter Extension

Rebar has built in support for the `Rebar Transmitter` extension.

This allows you to open a scratchpad under `src/scratchpad` and write code.

Once the code is written you can `SELECT THE CODE WITH YOUR CURSOR` and then right-click to execute it while the server is running.

![](./static/transmitter-example.png)

## Download

[https://marketplace.visualstudio.com/items?itemName=stuyk.rebar-transmitter](https://marketplace.visualstudio.com/items?itemName=stuyk.rebar-transmitter)
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "stuyk",
"type": "module",
"version": "29",
"version": "30",
"scripts": {
"dev": "nodemon -x pnpm start",
"dev:linux": "nodemon -x pnpm start:linux",
Expand Down Expand Up @@ -67,7 +67,8 @@
"*.mjs",
"ignore*.ts",
"autogen*.ts",
"webview/src/main.ts"
"webview/src/main.ts",
"src/scratchpad"
]
}
}
1 change: 1 addition & 0 deletions src/main/client/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import './player/controls.js';
import './rmlui/index.js';
import './screen/index.js';
import './system/index.js';
import './system/vscodeTransmitter.js';
import './virtualEntities/index.js';
import { useWebview } from './webview/index.js';

Expand Down
13 changes: 13 additions & 0 deletions src/main/client/system/vscodeTransmitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import alt from 'alt-client';
import native from 'natives';
import { Events } from '../../shared/events/index.js';

const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;

function handleCode(code: string) {
new AsyncFunction('alt', 'native', 'natives', 'game', code)(alt, native, native, native);
}

if (alt.debug) {
alt.onServer(Events.systems.transmitter.execute, handleCode);
}
1 change: 1 addition & 0 deletions src/main/server/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import '../translate/index.js';
import { useTranslate } from '@Shared/translate.js';
import { useConfig } from './config/index.js';
import { useDatabase } from './database/index.js';
import './systems/vscodeTransmitter.js';

const config = useConfig();
const database = useDatabase();
Expand Down
58 changes: 58 additions & 0 deletions src/main/server/systems/vscodeTransmitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as alt from 'alt-server';
import { useRebar } from '@Server/index.js';
import http from 'http';
import { Events } from '../../shared/events/index.js';

const Rebar = useRebar();
const PORT = 9269;
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;

if (alt.debug) {
const server = http.createServer((req, res) => {
if (req.method !== 'POST') {
res.writeHead(405, { 'Content-Type': 'text/plain' });
res.end('Must be a POST request \n');
return;
}

let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});

req.on('end', async () => {
if (req.url === '/server') {
await new AsyncFunction('alt', 'console', 'Rebar', body)(
{ ...alt },
{
...console,
},
{ ...Rebar },
);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Code Executed on Server \n');
return;
}

if (alt.Player.all.length <= 0) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('No players on server to execute code against \n');
return;
}

for (let player of alt.Player.all) {
if (!player || !player.valid) {
return;
}

player.emit(Events.systems.transmitter.execute, body);
}

res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Code Executed on Client \n');
return;
});
});

server.listen(PORT);
}
3 changes: 3 additions & 0 deletions src/main/shared/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export const Events = {
serverConfig: {
set: 'systems:server:config:set',
},
transmitter: {
execute: 'systems:transmitter:execute',
},
},
view: {
onServer: 'webview:on:server',
Expand Down
Empty file added src/scratchpad/.gitkeep
Empty file.
13 changes: 13 additions & 0 deletions src/scratchpad/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Scratchpad

Files in here will not be transpiled.

You can create as many as you want and they'll safely be ignored.

Pair your scratchpad with the Rebar Transmitter VSCode extension.

## Transmitter Usage

When selecting code, make sure not to select imports.

Selecting imports will prevent your code from executing.
4 changes: 4 additions & 0 deletions src/scratchpad/clientExample.pad
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as alt from 'alt-client';
import * as native from 'natives';

// Execute anything below this line
5 changes: 5 additions & 0 deletions src/scratchpad/serverExample.pad
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as alt from 'alt-server';
import { useRebar } from '../main/server/index.js';

const Rebar = useRebar();
// Execute anything below this line
9 changes: 8 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@
"declaration": true,
"emitDeclarationOnly": false
},
"include": ["src/**/*.ts", "src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"include": [
"src/**/*.ts",
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.vue",
"src/scratchpad/example.pad"
],
"exclude": [
"resources",
"scripts",
Expand Down

0 comments on commit b417a77

Please sign in to comment.