forked from nommiin/builder
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
322 lines (302 loc) · 13.5 KB
/
main.js
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
314
315
316
317
318
319
320
321
322
if (require("os").type().includes("Darwin")) process.env.ProgramData = "/Users/Shared";
Builder = {
Version: "1.25",
MenuItems: { list: [], run: null, stop: null, fork: null },
Platform: require("os").type(),
RuntimeSettings: null,
LoadKeywords: function(path) {
const custBase = $gmedit["electron.FileWrap"].userPath + "/api/" + GmlAPI.version.getName();
const GmlAPILoader = $gmedit["gml.GmlAPILoader"];
const GmlParseAPI = $gmedit["parsers.GmlParseAPI"];
const getText = (path) => {
if (Electron_FS.existsSync(path)) {
try {
return Electron_FS.readFileSync(path, "utf8")
} catch (x) {
console.error(x);
return null;
}
} else return null;
}
for (let platform of Electron_FS.readdirSync(path)) {
let platformPath = `${path}/${platform}`;
if (!Electron_FS.statSync(platformPath).isDirectory()) continue;
let fnamesPath = platformPath + "/fnames";
if (!Electron_FS.existsSync(fnamesPath)) continue;
let apiText = Electron_FS.readFileSync(fnamesPath, "utf8");
//
let custDir = custBase + "/" + platform;
if (Electron_FS.existsSync(custDir)) {
let replText = getText(custDir + "/replace.gml");
if (replText) apiText = GmlAPILoader.applyPatchFile(apiText, replText);
//
let extraText = getText(custDir + "/extra.gml");
if (extraText) apiText += "\n" + extraText;
}
//
let args = GmlAPILoader.getArgs();
GmlParseAPI.loadStd(apiText, args);
}
},
ProjectVersion: function(project) {
// GMEdit seems to adjust the .version property to be a gml_GmlVersion class, check if it's an object or not to maintain backwards compatibility
if (typeof(project.version) == "object") {
switch (project.version.config.projectMode) {
case "gms2": return 2;
case "gms1": return 1;
}
return -1;
}
return project.version;
},
GetRuntimes: function(path) {
let Runtimes = [];
try {
Electron_FS.readdirSync(path).forEach((e) => {
let rtStat = Electron_FS.statSync(path + e);
if (rtStat.isDirectory() && Electron_FS.existsSync(path + e + "/fnames")) {
Runtimes.push(e);
}
});
} catch (x) {
console.warn(`Failed to index ${path}:`, x);
}
Runtimes.sort((a, b) => a < b ? 1 : -1);
return Runtimes;
},
InitalizeRuntimes: function(set, showWarning) {
// [Re-]gather list of runtimes
set.runtimeList = this.GetRuntimes(set.location);
if (set.runtimeList.length <= 0) {
if (showWarning) Electron_Dialog.showMessageBox({
type: "warning",
message: `builder was unable to find any runtimes in ${set.location}, please verify your runtime location and rescan.`
});
set.selection = "";
return;
}
if (set.selection.trim() == "" || !set.runtimeList.includes(set.selection)) {
set.selection = set.runtimeList[0];
}
},
Initalize: function() {
// Check if platform is supported
this.Platform = (this.Platform.includes("Windows") ? "win" : (this.Platform.includes("Darwin") ? "mac" : "unknown"));
if (this.Platform == "unknown") {
Electron_Dialog.showMessageBox({
type: "error",
title: "Builder",
message: `builder v${Builder.Version} is not supported on your platform (${require("os").type()})`
});
return false;
}
// Load preferences file
BuilderPreferences.init();
for (let [key, val] of Object.entries(BuilderPreferences.current.runtimeSettings)) {
this.InitalizeRuntimes(val, key == "Stable");
}
let runtimeSettings = BuilderPreferences.current.runtimeSettings.Stable;
Builder.LoadKeywords(runtimeSettings.location + runtimeSettings.selection);
return true;
}
};
(function() {
function initCommands() {
const commands = [{
name: "builder-run",
title: "builder: Compile and run",
bindKey: "F5",
exec: () => Builder.Run(),
}, {
name: "builder-run-and-fork",
title: "builder: Compile and run two instances",
bindKey: { win: "ctrl-F5", mac: "cmd-F5" },
exec: () => Builder.Run(true),
}, {
name: "builder-stop",
title: "builder: Stop compiler or runner process",
bindKey: "F6",
exec: Builder.Stop,
}, {
name: "builder-fork",
title: "builder: Fork instance of runner",
bindKey: "F7",
exec: Builder.Fork,
}];
let hashHandler = $gmedit["ui.KeyboardShortcuts"].hashHandler;
let AceCommands = $gmedit["ace.AceCommands"];
for (let cmd of commands) {
hashHandler.addCommand(cmd);
AceCommands.add(cmd);
AceCommands.addToPalette({
name: cmd.title,
exec: cmd.name,
})
}
}
GMEdit.register("builder", {
init: function(config) {
// Initalize Builder!
if (Builder.Initalize() == false) {
console.error("builder - Failed to initalize.");
return;
}
// Create main menu items!
let MainMenu = $gmedit["ui.MainMenu"].menu;
for (let [index, mainMenuItem] of MainMenu.items.entries()) {
if (mainMenuItem.id != "close-project") continue;
Builder.MenuItems.list = [
new Electron_MenuItem({
id: "builder-sep",
type: "separator"
}),
Builder.MenuItems.run = new Electron_MenuItem({
id: "builder-run",
label: "Run",
accelerator: "F5",
icon: config.dir + "/icons/run.png",
enabled: false,
click: () => Builder.Run()
}),
Builder.MenuItems.runAndFork = new Electron_MenuItem({
id: "builder-run-and-fork",
label: "Run and Fork",
accelerator: "Ctrl+F5",
icon: config.dir + "/icons/run-and-fork.png",
enabled: false,
visible: BuilderPreferences.current.showRunAndFork,
click: () => Builder.Run(true)
}),
Builder.MenuItems.stop = new Electron_MenuItem({
id: "builder-stop",
label: "Stop",
accelerator: "F6",
icon: config.dir + "/icons/stop.png",
enabled: false,
click: Builder.Stop
}),
Builder.MenuItems.fork = new Electron_MenuItem({
id: "builder-fork",
label: "Fork",
accelerator: "F7",
icon: config.dir + "/icons/fork.png",
enabled: false,
click: Builder.Fork
}),
Builder.MenuItems.clean = new Electron_MenuItem({
id: "builder-clean",
label: "Clean",
accelerator: "Ctrl+F7",
icon: config.dir + "/icons/clean.png",
enabled: false,
click: Builder.CleanGUI
}),
];
for (let newItem of Builder.MenuItems.list) {
MainMenu.insert(++index, newItem);
}
break;
}
if (Builder.MenuItems.run == null) return; // probably running in GMLive.js
BuilderPreferences.ready();
BuilderProjectProperties.ready();
// Add ace commands!
initCommands();
// Hook into finishedIndexing
let Project = $gmedit["gml.Project"], finishedIndexing = Project.prototype.finishedIndexing;
function onFinishedIndexing() {
if (Builder.ProjectVersion(this) != 2) return;
const project = Project.current;
const projectContent = project.readTextFileSync(project.name);
const v23 = $gmedit['gml.Project'].current.isGMS23;
if (v23 == null) v23 = $gmedit["yy.YyJson"].isExtJson(projectContent);
const projectRoot = $gmedit["yy.YyJson"].parse(projectContent);
if (v23) {
this.configs = [];
function addConfigRec(project, config) {
if (!project.configs.includes(config.name)) project.configs.push(config.name);
for (let childConfig of config.children) addConfigRec(project, childConfig);
}
addConfigRec(this, projectRoot.configs);
} else {
this.configs = ["default"];
for (let configData of projectRoot.configs) {
for (let configName of configData.split(";")) {
if (!this.configs.includes(configName)) this.configs.push(configName);
}
}
}
let sf = Builder.SessionsFile;
if (sf.sync()) sf.data = {};
let path = project.path;
let pc = sf.data[path];
if (pc) pc.mtime = Date.now();
this.config = (pc && pc.config) || (v23 ? "Default" : "default");
sf.flush();
let TreeView = $gmedit["ui.treeview.TreeView"];
let Configurations = undefined;
for (let dir of document.querySelectorAll(".dir")) {
if (dir.textContent == "Configs") {
Configurations = dir;
break;
}
}
Configurations = Configurations || TreeView.makeAssetDir("Configs", "");
this.configs.forEach((configName) => {
let Configuration = TreeView.makeItem(configName);
Configuration.addEventListener("dblclick", function() {
Project.current.config = configName;
//
let sf = Builder.SessionsFile;
if (sf.sync()) sf.data = {};
let path = Project.current.path;
let pc = sf.data[path];
if (pc == null) pc = sf.data[path] = { };
pc.config = configName;
pc.mtime = Date.now();
sf.flush();
//
document.getElementById("project-name").innerText = `${Project.current.displayName} (${configName})`;
});
Configurations.treeItems.appendChild(Configuration);
});
TreeView.element.appendChild(Configurations);
document.getElementById("project-name").innerText = `${Project.current.displayName} (${this.config})`;
}
Project.prototype.finishedIndexing = function(arguments) {
let result = finishedIndexing.apply(this, arguments);
try {
onFinishedIndexing.call(this);
} catch (x) {
console.error(x);
}
return result;
}
function projectOpened() {
for (let item of Builder.MenuItems.list) item.enabled = false;
let project = $gmedit["gml.Project"].current;
if (Builder.ProjectVersion(project) == 2) {
Builder.MenuItems.run.enabled = true;
Builder.MenuItems.runAndFork.enabled = true;
Builder.MenuItems.clean.enabled = true;
let runtime;
const pref = BuilderPreferences.current;
if (project.version.name == "v23"
&& pref.runtimeSettings.Stable.selection < "runtime-2.3"
&& pref.runtimeSettings.Beta.selection != ""
) {
// if runtime is set to 2.2.5 but project uses 2.3, prefer a beta runtime
runtime = pref.runtimeSettings.Beta;
} else runtime = pref.runtimeSettings.Stable;
Builder.RuntimeSettings = runtime;
Builder.LoadKeywords(runtime.location + runtime.selection);
}
}
GMEdit.on("projectOpen", projectOpened);
GMEdit.on("projectClose", function() {
for (let item of Builder.MenuItems.list) item.enabled = false;
});
projectOpened();
}
});
})();