@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
112 lines (111 loc) • 5.3 kB
JavaScript
"use strict";
/**
* Handles WebSocket server commands for connecting to Minecraft game instances.
*
* This module manages the MinecraftWebSocketServer that allows the Electron app
* to communicate with running Minecraft instances via WebSocket protocol.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebSocketCommandHandler = void 0;
const MinecraftWebSocketServer_1 = __importDefault(require("../local/MinecraftWebSocketServer"));
class WebSocketCommandHandler {
_window;
_ipcMain;
_wss;
_localUtilities;
_env;
constructor(browserWindow, incomingIpcMain, env) {
this._window = browserWindow;
this._ipcMain = incomingIpcMain;
this.startServer = this.startServer.bind(this);
this.stopServer = this.stopServer.bind(this);
this.webSocketCommand = this.webSocketCommand.bind(this);
this.handleCommandCompleted = this.handleCommandCompleted.bind(this);
this.handleEventReceived = this.handleEventReceived.bind(this);
this.handleClientConnected = this.handleClientConnected.bind(this);
this.handleClientDisconnected = this.handleClientDisconnected.bind(this);
this.getMinecraftGameProjectPath = this.getMinecraftGameProjectPath.bind(this);
this.getMinecraftGameWorldPath = this.getMinecraftGameWorldPath.bind(this);
this._env = env;
this._wss = new MinecraftWebSocketServer_1.default(env);
this._localUtilities = this._env.utilities;
this._wss.onClientConnected.subscribe(this.handleClientConnected);
this._wss.onClientDisconnected.subscribe(this.handleClientDisconnected);
this._wss.onCommandCompleted.subscribe(this.handleCommandCompleted);
this._wss.onEventReceived.subscribe(this.handleEventReceived);
this._ipcMain.handle("asyncstartWebSocketServer", this.startServer);
this._ipcMain.handle("asyncstopWebSocketServer", this.stopServer);
this._ipcMain.handle("asyncwebSocketCommand", this.webSocketCommand);
this._ipcMain.handle("asyncgetMinecraftGameProjectDeployDir", this.getMinecraftGameProjectPath);
this._ipcMain.handle("asyncgetMinecraftGameWorldDeployDir", this.getMinecraftGameWorldPath);
}
webSocketCommand(_event, data) {
const slargs = data.split("|");
const command = slargs[1];
const requestId = slargs[0];
this._wss.runCommand(command, requestId, "");
}
async startServer(_event, data) {
const slargs = data.split("|");
const result = this._wss.openServer();
this._window.webContents.send("appsvc", "asyncstartWebSocketServerComplete|" + slargs[0] + "|" + result);
}
async stopServer(_event, data) {
const slargs = data.split("|");
const result = this._wss.closeServer();
this._window.webContents.send("appsvc", "asyncstopWebSocketServerComplete|" + slargs[0] + "|" + result);
}
async getMinecraftGameProjectPath(_event, data) {
const slargs = data.split("|");
if (slargs[1].length > 0) {
let val = -1;
try {
val = parseInt(slargs[1]);
if (val === 1 /* minecraftPreview */) {
this._window.webContents.send("appsvc", "asyncgetMinecraftGameProjectDeployDir|" + slargs[0] + "|" + this._localUtilities.minecraftPreviewPath);
return;
}
else if (val === 2 /* remote web sockets */) {
this._window.webContents.send("appsvc", "asyncgetMinecraftGameProjectDirComplete|" + slargs[0] + "|");
return;
}
}
catch (e) {
// Ignore parse errors
}
}
this._window.webContents.send("appsvc", "asyncgetMinecraftGameProjectDeployDirComplete|" + slargs[0] + "|" + this._localUtilities.minecraftPath);
}
async getMinecraftGameWorldPath(_event, data) {
const slargs = data.split("|");
let worldPath = "";
if (slargs[1].length > 0) {
let val = -1;
try {
val = parseInt(slargs[1]);
worldPath = this._wss.getWebSocketWorldPath(val);
}
catch (e) {
console.log("Error retrieving socket path: " + e);
}
}
this._window.webContents.send("appsvc", "asyncgetMinecraftGameWorldDeployDirComplete|" + slargs[0] + "|" + worldPath);
}
handleEventReceived(_server, req) {
this._window.webContents.send("appsvc", "wsevent|" + JSON.stringify(req));
}
handleCommandCompleted(_server, req) {
this._window.webContents.send("appsvc", "asyncwebSocketCommandComplete|" + req.requestId + "|" + JSON.stringify(req.result));
}
handleClientConnected(_server, _message) {
this._window.webContents.send("appsvc", "webSocketConnected|");
}
handleClientDisconnected(_server, _message) {
this._window.webContents.send("appsvc", "webSocketDisconnected|");
}
}
exports.WebSocketCommandHandler = WebSocketCommandHandler;
exports.default = WebSocketCommandHandler;