-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sugarcube.js
288 lines (236 loc) · 7.44 KB
/
sugarcube.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
/***********************************************************************************************************************
sugarcube.js
Copyright © 2013–2021 Thomas Michael Edwards <[email protected]>. All rights reserved.
Use of this source code is governed by a BSD 2-clause "Simplified" License, which may be found in the LICENSE file.
***********************************************************************************************************************/
/*
global Alert, Browser, Config, Dialog, Engine, Fullscreen, Has, LoadScreen, SimpleStore, L10n, Macro, Passage,
Save, Scripting, Setting, SimpleAudio, State, Story, UI, UIBar, DebugBar, Util, Visibility, Wikifier
, Links, idb
*/
/* eslint-disable no-var */
/*
Version object.
*/
var version = Object.freeze({
title : 'SugarCube',
major : '{{BUILD_VERSION_MAJOR}}',
minor : '{{BUILD_VERSION_MINOR}}',
patch : '{{BUILD_VERSION_PATCH}}',
prerelease : '{{BUILD_VERSION_PRERELEASE}}',
build : '{{BUILD_VERSION_BUILD}}',
date : new Date('{{BUILD_VERSION_DATE}}'),
/* legacy */
extensions : {},
/* /legacy */
toString() {
'use strict';
const prerelease = this.prerelease ? `-${this.prerelease}` : '';
return `${this.major}.${this.minor}.${this.patch}${prerelease}+${this.build}`;
},
short() {
'use strict';
const prerelease = this.prerelease ? `-${this.prerelease}` : '';
return `${this.title} (v${this.major}.${this.minor}.${this.patch}${prerelease})`;
},
long() {
'use strict';
return `${this.title} v${this.toString()} (${this.date.toUTCString()})`;
}
});
/* eslint-disable no-unused-vars */
/*
Internal variables.
*/
// Temporary state object.
var TempState = {};
// Legacy macros object.
var macros = {};
// Post-display task callbacks object.
var postdisplay = {};
// Post-render task callbacks object.
var postrender = {};
// Pre-display task callbacks object.
var predisplay = {};
// Pre-history task callbacks object.
var prehistory = {};
// Pre-render task callbacks object.
var prerender = {};
// Session storage manager object.
var session = null;
// Settings object.
var settings = {};
// Setup object.
var setup = {};
// Persistant storage manager object.
var storage = null;
/*
Legacy aliases.
*/
var browser = Browser;
var config = Config;
var has = Has;
var History = State;
var state = State;
var tale = Story;
var TempVariables = State.temporary;
/* eslint-enable no-unused-vars */
/*
Global `SugarCube` object. Allows scripts to detect if they're running in SugarCube by
testing for the object (e.g. `"SugarCube" in window`) and contains exported identifiers
for debugging purposes.
*/
window.SugarCube = {};
/*
Main function, entry point for the story.
*/
jQuery(() => {
'use strict';
const mainStart = () => {
if (DEBUG) { console.log('[SugarCube/main()] Document loaded; beginning startup.'); }
/*
WARNING!
The ordering of the code within this function is critically important,
so be careful when mucking around with it.
*/
try {
// Acquire an initial lock for and initialize the loading screen.
const lockId = LoadScreen.lock();
LoadScreen.init();
// Normalize the document.
if (document.normalize) {
document.normalize();
}
const initProcess = () => {
console.log('initProcess()');
// Load the story data (must be done before most anything else).
Story.load();
// Instantiate the storage and session objects.
// NOTE: `SimpleStore.create(storageId, persistent)`
storage = SimpleStore.create(Story.domId, true);
session = SimpleStore.create(Story.domId, false);
// Initialize the user interface (must be done before story initialization, specifically before scripts).
Dialog.init();
UIBar.init();
Engine.init();
// Initialize the story (largely load the user styles, scripts, and widgets).
Story.init();
// Initialize the localization (must be done after story initialization).
L10n.init();
// Alert when the browser is degrading required capabilities (must be done after localization initialization).
if (!session.has('rcWarn') && storage.name === 'cookie') {
/* eslint-disable no-alert */
session.set('rcWarn', 1);
window.alert(L10n.get('warningNoWebStorage'));
/* eslint-enable no-alert */
}
// Initialize the saves (must be done after story initialization, but before engine start).
Save.init();
// Initialize the settings.
Setting.init();
// Initialize indexedDB
idb.init(Story.domId);
// Initialize hotkeys
Links.init();
// Initialize the macros.
Macro.init();
// Start the engine (should be done as late as possible, but before interface startup).
Engine.start();
// Initialize the debug bar interface (should be done as late as possible, but before interface startup).
if (Config.debug) {
DebugBar.init();
}
// Set a recurring timer to start the interfaces (necessary due to DOM readiness issues in some browsers).
const $window = $(window);
const vprCheckId = setInterval(() => {
// If `$window.width()` returns a zero value, bail out and wait.
if (!$window.width()) {
return;
}
// Clear the recurring timer.
clearInterval(vprCheckId);
// Start the UI bar interface.
UIBar.start();
// Start the debug bar interface.
if (Config.debug) {
DebugBar.start();
}
// Trigger the `:storyready` global synthetic event.
jQuery.event.trigger({ type : ':storyready' });
// Release the loading screen lock after a short delay.
setTimeout(() => LoadScreen.unlock(lockId), Engine.minDomActionDelay * 2);
}, Engine.minDomActionDelay);
// Finally, export identifiers for debugging purposes.
Object.defineProperty(window, 'SugarCube', {
// WARNING: We need to assign new values at points, so seal it, do not freeze it.
value : Object.seal(Object.assign(Object.create(null), {
Browser,
Config,
Dialog,
Engine,
Fullscreen,
Has,
L10n,
Macro,
Passage,
Save,
Scripting,
Setting,
SimpleAudio,
State,
Story,
UI,
UIBar,
DebugBar,
Util,
Visibility,
Wikifier,
session,
settings,
setup,
storage,
version
}))
});
if (DEBUG) { console.log('[SugarCube/main()] Startup complete; story ready.'); }
};
// inject i18n on there
if (typeof i18nManager !== 'undefined') {
// eslint-disable-next-line no-undef
i18nManager.loadTranslateData(['ValueZip', 'Remote']);
// eslint-disable-next-line no-undef
i18nManager.isInited.then(() => {
initProcess();
});
}
else {
initProcess();
}
}
catch (ex) {
console.error(ex);
LoadScreen.clear();
return Alert.fatal(null, ex.message, ex);
}
};
// inject ModLoader on there
if (typeof window.modSC2DataManager !== 'undefined') {
// eslint-disable-next-line no-alert
// alert('start modSC2DataManager');
window.modSC2DataManager.startInit()
.then(() => window.jsPreloader.startLoad())
// eslint-disable-next-line no-alert
// .then(() => alert('modSC2DataManager ok'))
.then(() => mainStart())
.catch(err => {
console.error(err);
// eslint-disable-next-line no-alert
// alert(`Error loading mod data: ${err?.message ? err.message : err}`);
});
}
else {
// eslint-disable-next-line no-alert
// alert('cannot find modSC2DataManager');
mainStart();
}
});