forked from dfabulich/choicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headless.js
143 lines (131 loc) · 4.66 KB
/
headless.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
/*
* Copyright 2010 by Dan Fabulich.
*
* Dan Fabulich licenses this file to you under the
* ChoiceScript License, Version 1.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.choiceofgames.com/LICENSE-1.0.txt
*
* See the License for the specific language governing
* permissions and limitations under the License.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied.
*/
printed = [];
headless = true;
_global = this;
debughelp = function debughelp() {
debugger;
};
printx = function printx(msg, parent) {
printed.push(msg);
};
function println(msg, parent) {
printed.push(msg);
printed.push("<br>");
}
function printParagraph(msg, parent) {
if (msg === "") return;
printed.push("<p>");
printed.push(msg);
printed.push("</p>");
}
_global = this;
isRhino = typeof(java) != "undefined";
clearScreen = function clearScreen(code) {code.call();};
saveCookie = function(callback) { if (callback) callback.call(); };
loadTempStats = function (stats, callback) { if (callback) callback.call(null, stats); };
clearTemp = function() {};
doneLoading = function() {};
printFooter = function() {};
printShareLinks = function() {};
printLink = function() {};
kindleButton = function() {};
printImage = function() {};
showPassword = function() {};
printDiscount = function() {};
isRegistered = function() {return false;};
isRegisterAllowed = function() {return false;};
isFullScreenAdvertisingSupported = function() {return false;};
isRestorePurchasesSupported = function() {return false;};
areSaveSlotsSupported = function() {return false;};
isAdvertisingSupported = function() {return false;};
isPrerelease = function() {return false;};
showFullScreenAdvertisementButton = function(message, callback) { callback(); }
function fileExists(filePath) {
if (isRhino) {
return new java.io.File(filePath).exists();
} else {
return fs.existsSync(filePath);
}
}
function fileLastMod(filePath) {
if (isRhino) {
return new java.io.File(filePath).lastModified();
} else {
if (fs.existsSync(filePath)) return fs.statSync(filePath).mtime.getTime();
return 0;
}
}
function mkdirs(filePath) {
if (isRhino) {
new java.io.File(filePath).mkdirs();
} else {
if (!fs.existsSync(filePath)) {
var parentDir = path.dirname(filePath);
if (!fs.existsSync(parentDir)) {
mkdirs(parentDir);
}
fs.mkdirSync(filePath);
}
}
}
function slurpFile(name, throwOnError) {
return slurpFileLines(name, throwOnError).join('\n');
}
function slurpFileLines(name, throwOnError) {
var lines, line, i, invalidCharacter;
if (isRhino) {
lines = [];
var reader = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(name), "UTF-8"));
for (i = 0; !!(line = reader.readLine()); i++) {
if (i === 0 && line.charCodeAt(0) == 65279) line = line.substring(1);
if (throwOnError) {
invalidCharacter = line.match(/^(.*)\ufffd/);
if (invalidCharacter) throw new Error(name+" line " + (i+1) + ": invalid character. (ChoiceScript text should be saved in the UTF-8 encoding.)\n" + invalidCharacter[0]);
}
lines.push(line);
}
return lines;
} else {
var blob = fs.readFileSync(name, "utf-8");
lines = blob.split(/\r?\n/);
var firstLine = lines[0];
// strip byte order mark
if (firstLine.charCodeAt(0) == 65279) lines[0] = firstLine.substring(1);
if (throwOnError) {
for (i = 0; i < lines.length; i++) {
line = lines[i];
invalidCharacter = line.match(/^(.*)\ufffd/);
if (invalidCharacter) throw new Error(name+" line " + (i+1) + ": invalid character. (ChoiceScript text should be saved in the UTF-8 encoding.)\n" + invalidCharacter[0]);
}
}
return lines;
}
}
function slurpImage(name) {
var blob = fs.readFileSync(name);
var dataType;
if (/\.jpe?g$/i.test(name)) {
dataType = 'image/jpeg';
} else if (/\.png/i.test(name)) {
dataType = 'image/png';
}
return `data:${dataType};base64,${Buffer.from(blob).toString('base64')}`;
}
function initStore() { return false; }