|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
| 17 | +import * as cp from "child_process"; |
17 | 18 | import * as fs from "fs";
|
18 | 19 | import * as glob from "glob";
|
19 | 20 | import * as path from "path";
|
20 | 21 | import * as vscode from "vscode";
|
21 | 22 |
|
| 23 | +import { Balloon } from "../Utils/Balloon"; |
22 | 24 | import { Logger } from "../Utils/Logger";
|
23 | 25 | import { getNonce } from "../Utils/external/Nonce";
|
24 | 26 | import { getUri } from "../Utils/external/Uri";
|
@@ -267,6 +269,89 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
|
267 | 269 | //TODO process messages
|
268 | 270 | }
|
269 | 271 |
|
| 272 | + /** |
| 273 | + * @brief Get path of the parent .circle file |
| 274 | + */ |
| 275 | + private getModelFilePath(document: vscode.TextDocument): string { |
| 276 | + const dirPath = path.parse(document.uri.path).dir; |
| 277 | + let fileName = |
| 278 | + this._mpqDataMap[document.uri.toString()].getSection("model_path"); |
| 279 | + return path.join(dirPath, fileName); |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * @brief Get all model nodes |
| 284 | + */ |
| 285 | + private handleRequestModelNodes( |
| 286 | + document: vscode.TextDocument, |
| 287 | + webview: vscode.Webview |
| 288 | + ): void { |
| 289 | + const K_DATA: string = "data"; |
| 290 | + const K_EXIT: string = "exit"; |
| 291 | + const K_ERROR: string = "error"; |
| 292 | + let modelFilePath = this.getModelFilePath(document); |
| 293 | + |
| 294 | + // TODO integrate with Toolchain |
| 295 | + const tool = "/usr/share/one/bin/circle-operator"; |
| 296 | + if (!fs.existsSync(tool)) { |
| 297 | + // check whether it is installed |
| 298 | + Balloon.info("To add more layers for editing please install Toolchain"); |
| 299 | + return; |
| 300 | + } |
| 301 | + |
| 302 | + const toolargs = ["--name", modelFilePath]; |
| 303 | + let result: string = ""; |
| 304 | + let error: string = ""; |
| 305 | + |
| 306 | + let runPromise = new Promise<string>((resolve, reject) => { |
| 307 | + let cmd = cp.spawn(tool, toolargs, { shell: false }); |
| 308 | + |
| 309 | + cmd.stdout.on(K_DATA, (data: any) => { |
| 310 | + let str = data.toString(); |
| 311 | + if (str.length > 0) { |
| 312 | + result = result + str; |
| 313 | + } |
| 314 | + }); |
| 315 | + |
| 316 | + cmd.stderr.on(K_DATA, (data: any) => { |
| 317 | + error = result + data.toString(); |
| 318 | + Logger.error("MPQEditor", error); |
| 319 | + }); |
| 320 | + |
| 321 | + cmd.on(K_EXIT, (code: any) => { |
| 322 | + let codestr = code.toString(); |
| 323 | + if (codestr === "0") { |
| 324 | + resolve(result); |
| 325 | + } else { |
| 326 | + let msg = "Failed to load model: " + modelFilePath; |
| 327 | + Balloon.error(msg); |
| 328 | + reject(msg); |
| 329 | + } |
| 330 | + }); |
| 331 | + |
| 332 | + cmd.on(K_ERROR, () => { |
| 333 | + let msg = "Failed to run circle-operator: " + modelFilePath; |
| 334 | + Balloon.error(msg); |
| 335 | + reject(msg); |
| 336 | + }); |
| 337 | + }); |
| 338 | + |
| 339 | + runPromise |
| 340 | + .then((names) => { |
| 341 | + const layersNames = names.split(/\r?\n/); |
| 342 | + this._mpqDataMap[document.uri.toString()].setAllModelLayers( |
| 343 | + layersNames |
| 344 | + ); |
| 345 | + webview.postMessage({ |
| 346 | + type: "modelNodesChanged", |
| 347 | + names: layersNames, |
| 348 | + }); |
| 349 | + }) |
| 350 | + .catch((error) => { |
| 351 | + Logger.error("MPQEditor", error); |
| 352 | + }); |
| 353 | + } |
| 354 | + |
270 | 355 | /**
|
271 | 356 | * @brief Update document by text
|
272 | 357 | */
|
|
0 commit comments