Skip to content

Commit

Permalink
Add hot reload to the extension (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: amylizzle <[email protected]>
Co-authored-by: wixoa <[email protected]>
  • Loading branch information
3 people authored Aug 19, 2024
1 parent de53818 commit 6c048dc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opendream",
"version": "0.2.2",
"version": "0.2.3",
"displayName": "OpenDream Dev Tools",
"icon": "OD.png",
"description": "Developer tools for OpenDream",
Expand Down Expand Up @@ -39,6 +39,11 @@
"type": "string",
"default": null,
"description": "Path to your copy of the OpenDream source code. This should be the top level folder containing OpenDream.sln. Leave empty to use pre-compiled binaries instead."
},
"opendream.hotReload": {
"type": "boolean",
"default": true,
"description": "Automatically reload resources when they're changed on disk."
}
}
},
Expand Down
32 changes: 30 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export async function activate(context: ExtensionContext) {
let buildClientPromise = openDream.buildClient(session.workspaceFolder);
// Wait for the OD server to connect back to us, then stop listening.
let socket = await socketPromise;
return new vscode.DebugAdapterInlineImplementation(new OpenDreamDebugAdapter(socket, buildClientPromise));
let hotReloadEnable:boolean = workspace.getConfiguration('opendream').get('hotReload') || false;
return new vscode.DebugAdapterInlineImplementation(new OpenDreamDebugAdapter(socket, buildClientPromise, hotReloadEnable));
}
}));

Expand Down Expand Up @@ -162,12 +163,14 @@ class OpenDreamDebugAdapter implements vscode.DebugAdapter {

private socket: net.Socket;
private buffer = Buffer.alloc(0);
private resourceWatcher?: vscode.FileSystemWatcher
private interfaceWatcher?: vscode.FileSystemWatcher

private didSendMessageEmitter = new vscode.EventEmitter<DebugProtocolMessage>();
private sendMessageToEditor = this.didSendMessageEmitter.fire.bind(this.didSendMessageEmitter);
onDidSendMessage = this.didSendMessageEmitter.event;

constructor(socket: net.Socket, buildClientPromise?: Promise<ODClient>) {
constructor(socket: net.Socket, buildClientPromise?: Promise<ODClient>, hotReloadAuto?: boolean) {
this.socket = socket;
this.buildClientPromise = buildClientPromise;

Expand Down Expand Up @@ -216,6 +219,29 @@ class OpenDreamDebugAdapter implements vscode.DebugAdapter {
headerEnd = this.buffer.indexOf('\r\n\r\n');
}
});

if(hotReloadAuto) {
this.resourceWatcher = vscode.workspace.createFileSystemWatcher(("**/*.{dmi,png,jpg,rsi,gif,bmp}"))//TODO all the sound file formats?
this.interfaceWatcher = vscode.workspace.createFileSystemWatcher(("**/*.{dmf}"))

this.interfaceWatcher.onDidChange(() => {this.hotReloadInterface()})
this.interfaceWatcher.onDidCreate(() => {this.hotReloadInterface()})
this.interfaceWatcher.onDidDelete(() => {this.hotReloadInterface()})

this.resourceWatcher.onDidChange((file) => {this.hotReloadResource(file)})
this.resourceWatcher.onDidCreate((file) => {this.hotReloadResource(file)})
this.resourceWatcher.onDidDelete((file) => {this.hotReloadResource(file)})
}
}

private hotReloadInterface(): void {
console.log("Hot reloading interface")
this.sendMessageToGame({ type: 'request', command: 'hotreloadinterface'})
}

private hotReloadResource(resource:vscode.Uri) {
console.log(`Hot reloading resource ${resource.fsPath}`)
this.sendMessageToGame({ type: 'request', command: 'hotreloadresource', arguments: {'file':resource.fsPath}})
}

handleMessage(message: any): void {
Expand Down Expand Up @@ -245,6 +271,8 @@ class OpenDreamDebugAdapter implements vscode.DebugAdapter {
}

dispose() {
this.resourceWatcher?.dispose()
this.interfaceWatcher?.dispose()
this.socket.destroy();
this.didSendMessageEmitter.dispose();
}
Expand Down

0 comments on commit 6c048dc

Please sign in to comment.