-
Notifications
You must be signed in to change notification settings - Fork 10
/
remote-handler.ts
313 lines (250 loc) · 11.1 KB
/
remote-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* A module handling the connection between Unreal and VSCode
*/
import * as vscode from 'vscode';
import { RemoteExecution, RemoteExecutionConfig, RemoteExecutionNode, ECommandOutputType } from "unreal-remote-execution";
import * as extensionWiki from "./extension-wiki";
import * as utils from "./utils";
import * as logger from "./logger";
let gIsInitializatingConnection = false;
let gCachedRemoteExecution: RemoteExecution | null = null;
let gStatusBarItem: vscode.StatusBarItem | null = null;
// ------------------------------------
// Status Bar Item
// ------------------------------------
function getStatusBarItem(bEnsureExists = true) {
if (!gStatusBarItem && bEnsureExists) {
gStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 10);
gStatusBarItem.command = "ue-python.selectInstance";
gStatusBarItem.tooltip = "Connected Unreal Engine instance";
}
return gStatusBarItem;
}
export function removeStatusBarItem() {
gStatusBarItem?.dispose();
gStatusBarItem = null;
}
export function updateStatusBar(node: RemoteExecutionNode) {
const statusBarItem = getStatusBarItem();
if (statusBarItem) {
statusBarItem.text = `$(unreal-engine) ${node.data.project_name}`;
statusBarItem.show();
}
}
/**
* Get a `RemoteExecutionConfig` based on the extension user settings
*/
function getRemoteConfig() {
const extensionConfig = utils.getExtensionConfig();
const multicastTTL: number | undefined = extensionConfig.get("remote.multicastTTL");
const multicastBindAddress: string | undefined = extensionConfig.get("remote.multicastBindAddress");
let multicastGroupEndpoint: [string, number] | undefined = undefined;
const multicastGroupEndpointStr: string | undefined = extensionConfig.get("remote.multicastGroupEndpoint");
if (multicastGroupEndpointStr) {
const [multicastGroupStr, portStr] = multicastGroupEndpointStr.split(":", 2);
multicastGroupEndpoint = [multicastGroupStr, Number(portStr)];
}
let commandEndpoint: [string, number] | undefined = undefined;
const commandEndpointStr: string | undefined = extensionConfig.get("remote.commandEndpoint");
if (commandEndpointStr) {
const [commandHost, commandPortStr] = commandEndpointStr.split(":", 2);
commandEndpoint = [commandHost, Number(commandPortStr)];
}
return new RemoteExecutionConfig(multicastTTL, multicastGroupEndpoint, multicastBindAddress, commandEndpoint);
}
/**
* Make sure the command port is avaliable, and if not it'll try to find a port that's free and modify the port in config to use this new port.
* @param config The remote execution config
* @returns A list with 2 elements, the first one is a boolean depending on if a free port was found/assigned to the config. Second element is a error message.
*/
async function ensureCommandPortAvaliable(config: RemoteExecutionConfig): Promise<boolean> {
const extensionConfig = utils.getExtensionConfig();
const host = config.commandEndpoint[0];
const commandEndpointPort = config.commandEndpoint[1];
// Check if user has enabled 'strictPort'
if (extensionConfig.get("strictPort")) {
if (!await utils.isPortAvailable(commandEndpointPort, host)) {
vscode.window.showErrorMessage(`Port ${commandEndpointPort} is currently busy. Consider changing the config: 'ue-python.remote.commandEndpoint'.`);
return false;
}
}
else {
// Check the next 100 ports, one should hopefully be free
const freePort = await utils.findFreePort(commandEndpointPort, 101, host);
if (!freePort) {
vscode.window.showErrorMessage(`All ports between ${commandEndpointPort} - ${commandEndpointPort + 100} are busy. Consider changing the config: 'ue-python.remote.commandEndpoint'.`);
return false;
}
// If the first found free port wasn't the original port, update it
if (commandEndpointPort !== freePort) {
config.commandEndpoint[1] = freePort;
}
}
return true;
}
/**
* Get the port the remote execution command connection is using
*/
export async function getRemoteExecutionCommandPort() {
const remoteExecution = await getRemoteExecutionInstance(false);
if (!remoteExecution)
return null;
return remoteExecution.config.commandEndpoint[1];
}
/**
* Get the global remote connection instance
* @param bEnsureConnection Make sure the remote execution instance exists, if not it'll create one.
*/
export async function getRemoteExecutionInstance(bEnsureExists = true) {
if (!gCachedRemoteExecution && bEnsureExists) {
const config = getRemoteConfig();
if (await ensureCommandPortAvaliable(config)) {
gCachedRemoteExecution = new RemoteExecution(config);
gCachedRemoteExecution.events.addEventListener("commandConnectionClosed", onRemoteConnectionClosed);
await gCachedRemoteExecution.start();
logger.log("Remote execution instance created");
logger.log(`Multicast TTL: ${config.multicastTTL}`);
logger.log(`Multicast Bind Address: ${config.multicastBindAddress}`);
logger.log(`Multicast Group Endpoint: ${config.multicastGroupEndpoint[0]}:${config.multicastGroupEndpoint[1]}`);
logger.log(`Command Endpoint: ${config.commandEndpoint[0]}:${config.commandEndpoint[1]}`);
}
}
return gCachedRemoteExecution;
}
export function nullifyRemoteExecutionInstance() {
gCachedRemoteExecution?.stop();
gCachedRemoteExecution = null;
}
/**
* Get the global remote connection instance, and make sure it's connected
* @returns The remote execution instance, or null if it failed to connect
*/
export async function getConnectedRemoteExecutionInstance(): Promise<RemoteExecution | null> {
const remoteExecution = await getRemoteExecutionInstance();
if (!remoteExecution)
return null;
if (!remoteExecution.hasCommandConnection()) {
const config = getRemoteConfig();
if (gIsInitializatingConnection) {
return new Promise((resolve) => {
const interval = setInterval(() => {
if (!gIsInitializatingConnection) {
clearInterval(interval);
resolve(getConnectedRemoteExecutionInstance());
}
}, 1000);
});
}
gIsInitializatingConnection = true;
if (await ensureCommandPortAvaliable(config)) {
const extensionConfig = utils.getExtensionConfig();
const timeout: number = extensionConfig.get("remote.timeout") ?? 3000;
logger.log(`Connecting with a timeout of ${timeout}ms.`);
try {
const node = await remoteExecution.getFirstRemoteNode(1000, timeout);
await remoteExecution.openCommandConnection(node, true, timeout);
logger.log("Connected to: " + JSON.stringify(node.data));
await onRemoteInstanceCreated(remoteExecution);
updateStatusBar(node);
}
catch (error: any) {
logger.log(error);
let message: string = error.message;
if (message.startsWith("Timed out"))
message = "Timed out while trying to connect to Unreal Engine.";
vscode.window.showErrorMessage(message, "Help").then((clickedItem) => {
if (clickedItem === "Help")
extensionWiki.openPageInBrowser(extensionWiki.FPages.failedToConnect);
});
nullifyRemoteExecutionInstance();
return null;
}
finally {
gIsInitializatingConnection = false;
}
}
else {
nullifyRemoteExecutionInstance();
gIsInitializatingConnection = false;
return null;
}
}
return remoteExecution;
}
/**
* Called when a remote instance is created
*/
async function onRemoteInstanceCreated(instance: RemoteExecution) {
// Check if we should add any workspace folders to the python path
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders) {
let foldersToAddToPath: string[] = [];
for (const folder of workspaceFolders) {
const config = vscode.workspace.getConfiguration(utils.EXTENSION_ID, folder.uri);
if (config.get<boolean>('environment.addWorkspaceToPath', false)) {
foldersToAddToPath.push(folder.uri.fsPath);
}
}
if (foldersToAddToPath.length > 0) {
const response = await executeFile(
utils.FPythonScriptFiles.getAbsPath(utils.FPythonScriptFiles.addSysPath),
{
vsc_paths: foldersToAddToPath // eslint-disable-line @typescript-eslint/naming-convention
}
);
if (response) {
if (response.result && response.result !== "None")
logger.log(response.result);
for (const output of response.output) {
if (output.type === ECommandOutputType.ERROR)
logger.logError("Ran into an error while adding workspace folders to the python path.", new Error(output.output));
else
logger.log(`[${output.type}] ${output.output}`);
}
}
}
}
}
/**
* Called when the remote connection is closed
*/
async function onRemoteConnectionClosed() {
const remoteExecution = await getRemoteExecutionInstance(false);
if (!remoteExecution?.hasCommandConnection())
removeStatusBarItem();
logger.log("Remote connection closed");
}
/**
* Send a command to the remote connection
* @param command The python code as a string
*/
export async function runCommand(command: string) {
const remoteExec = await getConnectedRemoteExecutionInstance();
if (!remoteExec) {
return;
}
const bUnattended = utils.getExtensionConfig().get("execute.unattended") ? true : false;
return remoteExec.runCommand(command, bUnattended);
}
/**
* Execute a file in Unreal through the remote exection
* @param filepath Absolute filepath to the python file to execute
* @param variables Optional dict with global variables to set before executing the file
*/
export function executeFile(filepath: string, globals: any = {}) {
if (!globals.hasOwnProperty('__file__')) {
globals["__file__"] = filepath;
}
let globalsStr = JSON.stringify(globals);
globalsStr = globalsStr.replace(/\\/g, "\\\\");
// Put together one line of code for settings the global variables, then opening, reading & executing the given filepath
const command = `import json;globals().update(json.loads('${globalsStr}'));f=open(r'${filepath}','r');exec(f.read());f.close()`;
return runCommand(command);
}
/**
* Close the global remote connection, if there is one
*/
export async function closeRemoteConnection() {
const remoteConnection = await getRemoteExecutionInstance(false);
remoteConnection?.stop();
}