Repository navigation
Expand file tree
/
Copy pathliveCompiler.js
More file actions
323 lines (246 loc) · 8.88 KB
/
Copy pathliveCompiler.js
File metadata and controls
323 lines (246 loc) · 8.88 KB
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
323
const ipc = require("electron").ipcRenderer;
const _ = require("lodash");
var randomstring = require("randomstring");
var namespace = null;
var sessionIdx = 0;
var currentPlaySessionId = null;
var currentExportSessionId = null;
var exportCompleteCallback = null;
var lastEditorChange = null;
var choiceSequence = [];
var currentTurnIdx = -1;
var replaying = false;
var issues = [];
var selectedIssueIdx = -1;
var locationInSourceCallbackObj = null;
var expressionEvaluationObj = null;
var project = null;
var events = {};
function setProject(p) {
project = p;
// Generate the namespace just once so it stays constant all the while the project/app is open
// otherwise when the name changes, the temp folder could go out of sync
var namespaceCode = randomstring.generate(7);
namespace = project.mainInk.filename().replace(/\./g, "_") + "_" + namespaceCode;
}
function resetErrors() {
issues = [];
selectedIssueIdx = -1;
}
function buildCompileInstruction() {
sessionIdx += 1;
// Construct instruction object to send to inklecate.js
var compileInstruction = {
mainName: project.mainInk.filename(),
updatedFiles: {},
sessionId: `${namespace}_${sessionIdx}`,
namespace: namespace
};
project.files.forEach((inkFile) => {
// Add Ink Files with changes to be saved before the next compile
// If we're running for the first time, add all because non of the files has been saved to tempInkPath
if( inkFile.compilerVersionDirty ) {
compileInstruction.updatedFiles[inkFile.relativePath()] = inkFile.getValue();
inkFile.compilerVersionDirty = false;
}
});
return compileInstruction;
}
function sessionIsCurrent(id) {
return id == currentPlaySessionId || id == currentExportSessionId;
}
function reloadInklecateSession() {
lastEditorChange = null;
if( currentPlaySessionId )
stopInklecateSession(currentPlaySessionId);
replaying = true;
currentTurnIdx = 0;
var instr = buildCompileInstruction();
instr.play = true;
events.resetting(instr.sessionId);
resetErrors();
currentPlaySessionId = instr.sessionId;
console.log("This window sending session "+instr.sessionId);
ipc.send("compile", instr);
}
function exportJson(callback) {
exportCompleteCallback = callback;
var instr = buildCompileInstruction();
instr.export = true;
currentExportSessionId = instr.sessionId;
ipc.send("compile", instr);
}
function completeExport(error, path) {
var callback = exportCompleteCallback;
exportCompleteCallback = null;
if( error )
callback(error.message);
else
callback(null, path);
}
function stopInklecateSession(idToStop) {
ipc.send("play-stop-ink", idToStop);
}
function choose(choice) {
ipc.send("play-continue-with-choice-number", choice.number, choice.sourceSessionId);
choiceSequence.push(choice.number);
currentTurnIdx++;
}
function rewind() {
choiceSequence = [];
currentTurnIdx = -1;
reloadInklecateSession();
}
function stepBack() {
if( choiceSequence.length > 0 )
choiceSequence.splice(-1, 1);
reloadInklecateSession();
}
function getLocationInSource(offset, callback) {
ipc.send("get-location-in-source", offset, currentPlaySessionId);
locationInSourceCallbackObj = { callback: callback, sessionId: currentPlaySessionId };
}
function evaluateExpression(expressionText, callback) {
ipc.send("evaluate-expression", expressionText, currentPlaySessionId);
expressionEvaluationObj = { callback: callback, sessionId: currentPlaySessionId };
}
// --------------------------------------------------------
// Live re-compile loop
// --------------------------------------------------------
// Do first compile
// Really just for debug when loading ink immediately
// other actions will cause editor changes
setTimeout(reloadInklecateSession, 1000);
// compile loop - detect changes every 0.25 and make sure
// user has paused before actually compiling
setInterval(() => {
if( lastEditorChange != null && Date.now() - lastEditorChange > 500 ) {
lastEditorChange = null;
reloadInklecateSession();
}
}, 250);
// --------------------------------------------------------
// IPC event from the native menu option to cycle issues
// --------------------------------------------------------
ipc.on("next-issue", () => {
if( issues.length > 0 ) {
selectedIssueIdx++;
if( selectedIssueIdx >= issues.length )
selectedIssueIdx = 0;
events.selectIssue(issues[selectedIssueIdx]);
}
});
// --------------------------------------------------------
// IPC Events from inklecate.js
// --------------------------------------------------------
ipc.on("play-generated-text", (event, result, fromSessionId) => {
if( fromSessionId != currentPlaySessionId ) return;
events.textAdded(result);
});
ipc.on("play-generated-errors", (event, errors, fromSessionId) => {
if( !sessionIsCurrent(fromSessionId) ) return;
issues = errors;
events.errorsAdded(errors);
});
ipc.on("play-generated-tags", (event, tags, fromSessionId) => {
if( fromSessionId != currentPlaySessionId ) return;
events.tagsAdded(tags);
});
ipc.on("play-generated-choice", (event, choice, fromSessionId) => {
if( fromSessionId != currentPlaySessionId ) return;
choice.sourceSessionId = fromSessionId;
// If there's one choice, that means there are two turns/chunks
var turnCount = choiceSequence.length+1;
var isLatestTurn = currentTurnIdx >= turnCount-1;
events.choiceAdded(choice, isLatestTurn);
});
ipc.on("play-requires-input", (event, fromSessionId) => {
if( fromSessionId != currentPlaySessionId )
return;
var justCompletedReplay = false;
if( replaying && currentTurnIdx >= choiceSequence.length ) {
replaying = false;
justCompletedReplay = true;
}
events.playerPrompt(replaying, () => {
if( replaying ) {
var replayChoiceNumber = choiceSequence[currentTurnIdx];
currentTurnIdx++;
ipc.send("play-continue-with-choice-number", replayChoiceNumber, fromSessionId);
}
if( justCompletedReplay )
events.replayComplete(currentPlaySessionId);
});
});
ipc.on("inklecate-complete", (event, fromSessionId, exportJsonPath) => {
if( fromSessionId == currentPlaySessionId )
events.storyCompleted();
if( replaying ) {
replaying = false;
events.replayComplete(currentPlaySessionId);
}
else if( fromSessionId == currentExportSessionId ) {
completeExport(null, exportJsonPath);
}
});
ipc.on("play-exit-due-to-error", (event, exitCode, fromSessionId) => {
if( !sessionIsCurrent(fromSessionId) ) return;
if( fromSessionId == currentExportSessionId ) {
completeExport({message: "Ink has errors - please fix them before exporting."});
} else {
if( replaying ) {
replaying = false;
events.replayComplete();
}
events.exitDueToError();
}
});
ipc.on("play-story-unexpected-error", (event, error, fromSessionId) => {
if( !sessionIsCurrent(fromSessionId) ) return;
if( fromSessionId == currentExportSessionId ) {
completeExport({message: "Unexpected error"});
} else {
if( replaying ) {
replaying = false;
events.replayComplete(fromSessionId);
}
events.unexpectedError(error);
}
});
ipc.on("play-story-stopped", (event, fromSessionId) => {
});
ipc.on("return-location-from-source", (event, fromSessionId, locationInfo) => {
if( fromSessionId == locationInSourceCallbackObj.sessionId ) {
var callback = locationInSourceCallbackObj.callback;
locationInSourceCallbackObj = null;
callback(locationInfo);
}
});
ipc.on("play-evaluated-expression", (event, textResult, fromSessionId) => {
if( fromSessionId == expressionEvaluationObj.sessionId && expressionEvaluationObj ) {
var callback = expressionEvaluationObj.callback;
expressionEvaluationObj = null;
callback(textResult);
}
});
ipc.on("play-evaluated-expression-error", (event, errorMessage, fromSessionId) => {
if( fromSessionId == expressionEvaluationObj.sessionId && expressionEvaluationObj ) {
var callback = expressionEvaluationObj.callback;
expressionEvaluationObj = null;
callback(null, errorMessage);
}
});
exports.LiveCompiler = {
setProject: setProject,
reload: reloadInklecateSession,
exportJson: exportJson,
setEdited: () => { lastEditorChange = Date.now(); },
setEvents: (e) => { events = e; },
getIssues: () => { return issues; },
getIssuesForFilename: (filename) => _.filter(issues, i => i.filename == filename),
choose: choose,
rewind: rewind,
stepBack: stepBack,
getLocationInSource: getLocationInSource,
evaluateExpression: evaluateExpression
}