-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUnpacker.ts
115 lines (99 loc) · 3.64 KB
/
Unpacker.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
/* eslint-disable @typescript-eslint/naming-convention */
import * as fs from "fs";
import * as path from "path";
import { ConfigService } from "./services/ConfigService";
import { LoggingService } from "./services/LoggingService";
import {
extractScriptFiles,
RubyScriptService,
} from "./commands/ExtractScriptFiles";
import { Path } from "./utils/Path";
const message = {
UNPACKER_NOT_READY: "Unpacker is not ready.",
JOB_COMPLETED: "Job completed.",
};
namespace RGSS {
export const TARGET_SCRIPT_FILE_NAME = "Scripts.rvdata2";
export class Unpacker {
protected _targetFile: string;
protected _isReady: boolean;
constructor(
protected readonly configService: ConfigService,
protected readonly loggingService: LoggingService,
protected readonly successCallback?: () => void,
) {
this._targetFile = "";
this._isReady = false;
this.start();
}
start() {
this.initWithTargetFile();
}
/**
* Sets the target file from the main game folder.
* it is assumed that the file extension is one of ruby serialized files(*.rvdata2, *.rvdata, *.rxdata)
*/
initWithTargetFile() {
const root = Path.resolve(this.configService.getMainGameFolder());
const targetFile = path
.join(root, "Data", ConfigService.TARGET_SCRIPT_FILE_NAME)
.replace(/\\/g, "/");
if (!fs.existsSync(targetFile)) {
this.loggingService.info(`${targetFile} not found.`);
throw new Error(
`Data/${ConfigService.TARGET_SCRIPT_FILE_NAME} not found.`,
);
}
this._targetFile = targetFile;
this._isReady = true;
}
updateTargetFile() {
this.initWithTargetFile();
}
public static isExistFile(configService: ConfigService) {
const root = Path.resolve(configService.getMainGameFolder());
const targetFile = path
.join(root, "Data", ConfigService.TARGET_SCRIPT_FILE_NAME)
.replace(/\\/g, "/");
return !fs.existsSync(targetFile);
}
/**
* Extract script files to vscode workspace.
*/
unpack() {
if (!this._isReady) {
this.loggingService.info(message.UNPACKER_NOT_READY);
throw new Error(message.UNPACKER_NOT_READY);
}
this.updateTargetFile();
const targetFile = <string>this._targetFile;
try {
// Create ruby script service
const rubyScriptService = new RubyScriptService(
this.configService,
this.loggingService,
{
vscodeWorkspaceFolder: Path.resolve(
this.configService.getVSCodeWorkSpace(),
),
scriptFile: targetFile,
},
(err: any, stdout: any, stderr: any) => {
if (err) {
this.loggingService.info(err);
}
this.loggingService.info(message.JOB_COMPLETED);
},
);
extractScriptFiles(
this.loggingService,
rubyScriptService,
this.successCallback,
);
} catch (e) {
this.loggingService.info((<Error>e).message);
}
}
}
}
export = RGSS;