Repository navigation
Expand file tree
/
Copy pathloadsave.js
More file actions
374 lines (327 loc) · 10.1 KB
/
Copy pathloadsave.js
File metadata and controls
374 lines (327 loc) · 10.1 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2026 Jeff Bush
//
// Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
//
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as sprites from './sprites.js';
import * as audio from './audio.js';
// These delineate where sprites and sound data occur in the save file.
const SPRITE_DELIMITER = '\n--SPRITE DATA------\n';
const SOUND_DELIMITER = '\n--SOUND DATA--------\n';
export let saveFileName = null;
export let needsSave = false;
/**
* This is called whenever the user modifies content (sprites, sound, source
* code), and thus it is unsaved. As a side effect it will:
* - Display an indicator in the title bar
* - Pop up a message if the user tries to close the window without
* saving.
*/
export function setNeedsSave() {
if (!needsSave) {
needsSave = true;
updateTitleBar();
}
}
export function clearNeedsSave() {
needsSave = false;
updateTitleBar();
}
export function setSaveFileName(value) {
saveFileName = value;
}
/**
* Draw current file name, and an asterisk if has unsaved changes.
*/
export function updateTitleBar() {
// The star indicates it needs saving.
document.title = (saveFileName ? saveFileName : 'Untitled') +
(needsSave ? '*' : '');
}
/**
* Copy source code and sprites to the server (serve.js), which it saves
* on its local filesystem.
* @note this does not check needsSave and will always save, just to be safe.
*/
export function saveToServer() {
console.log('Saving to server...');
if (!saveFileName) {
saveFileName = window.prompt('Enter filename:');
document.title = saveFileName;
}
if (!saveFileName) {
return; // user hit cancel.
}
if (!saveFileName.toLowerCase().endsWith('.fth')) {
setSaveFileName(saveFileName + '.fth');
}
const content = encodeSaveData(getSourceCode(), sprites.spriteData.data,
audio.soundEffects);
fetch(`/save/${saveFileName}`, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
body: content,
}).then((response) => {
if (!response.ok) {
throw new Error('Unable to save to server');
}
console.log('Saved');
updateFileList();
clearNeedsSave();
}).catch((error) => {
alert('Error saving text to server:' + error);
});
}
/**
* Convert all game data into a file that can be saved
* @param {str} code Text of the source code
* @param {Uint8ClampedArray} imageData
* @param {[]} soundEffects
* @return {str}
*/
export function encodeSaveData(code, imageData, soundEffects) {
return code +
'\n(' + SPRITE_DELIMITER + encodeSprites(imageData) +
SOUND_DELIMITER + encodeSoundEffects(soundEffects) + '\n)\n';
}
/**
* Convert current sprites sheet to a string suitable for storing in a text
* file. The format is described in decodeSprites.
* @param {Uint8ClampedArray} imageData
* @return {string} Hex representation of image data
* @see decodeSprites
*/
export function encodeSprites(imageData) {
// Ignore any zeroes at the end to save space. Walk backward
// to determine how many there are.
const dataEnd = countTrailingZeros(imageData);
let result = '';
for (let i = 0; i <= dataEnd; i += 4) {
const index = sprites.INVERSE_PALETTE.get(imageData.slice(i, i + 4).
toString());
if (index === undefined) {
// This shouldn't happen normally.
console.log('invalid color in sprites data');
result += '0';
} else {
result += index.toString(16);
}
if (((i / 4) % sprites.SPRITE_SHEET_WIDTH) ==
sprites.SPRITE_SHEET_WIDTH - 1) {
result += '\n';
}
}
return result;
}
/**
* Find index of last non-zero value in array.
* @param {number} arr
* @return {number}
*/
function countTrailingZeros(arr) {
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] != 0) {
return i;
}
}
return 0;
}
function encodeSoundEffects(soundEffects) {
// Ignore effects that are empty
let totalEffects = 0;
for (let i = soundEffects.length - 1; i >= 0; i--) {
if (!soundEffects[i].amplitudes.every((value) => value === 0) ||
!soundEffects[i].pitches.every((value) => value === 0)) {
totalEffects = i + 1;
break;
}
}
// Encode effects that are non-zero
let result = '';
for (let i = 0; i < totalEffects; i++) {
result += encodeSoundEffect(soundEffects[i]) + '\n';
}
return result;
}
function encodeSoundEffect(effect) {
let encoded = '';
function encodeByte(val) {
encoded += val.toString(16).padStart(2, '0');
}
encodeByte(effect.noteDuration);
encodeByte(effect.waveform);
for (let i = 0; i < audio.NOTES_PER_EFFECT; i++) {
if (i < effect.pitches.length) {
encodeByte(effect.pitches[i]);
} else {
encodeByte(0);
}
}
for (let i = 0; i < audio.NOTES_PER_EFFECT; i++) {
if (i < effect.amplitudes.length) {
encodeByte(effect.amplitudes[i]);
} else {
encodeByte(0);
}
}
return encoded;
}
/**
* Load source code and sprites from the server over HTTP.
* @param {string} filename Name of file to load
*/
export async function loadFromServer(filename) {
console.log('loadFromServer', filename);
setSaveFileName(filename);
return fetch('games/' + saveFileName).then((response) => {
if (!response.ok) {
throw new Error(`Server error, returned status ${response.status}`);
}
return response.text();
}).then((data) => {
const [code, spritePixels, soundEffects] = decodeSaveData(data);
setSourceCode(code);
sprites.setSpriteData(spritePixels);
audio.setSoundEffectData(soundEffects);
clearNeedsSave();
}).catch((error) => {
alert('Error loading file: ' + error);
});
}
/**
* Parse string contents of a file containing sprites, source, and sound
* data and populate global data structures used by the engine.
* @param {string} data
* @return {[code, spritPixels, soundEffects]}
*/
export function decodeSaveData(data) {
// Split this into sections.
const split1 = data.indexOf(SPRITE_DELIMITER);
const split2 = data.indexOf(SOUND_DELIMITER);
if (split1 == -1 || split2 == -1) {
throw new Error('error loading file: missing sound/sprites data');
}
const endOfCode = data.lastIndexOf('(', split1);
const code = data.substring(0, endOfCode - 1);
const spriteString = data.substring(split1 + SPRITE_DELIMITER.length, split2);
const spritePixels = decodeSprites(spriteString);
const soundString = data.substring(split2 + SOUND_DELIMITER.length);
const soundEffects = decodeSoundEffects(soundString);
return [code, spritePixels, soundEffects];
}
function decodeSoundEffects(string) {
const result = [];
// Remove stray characters
const compressed = string.replace(/[^a-f0-9]/gi, '');
let index = 0;
function nextByte() {
if (index >= compressed.length) {
return null;
}
const val = parseInt(compressed.substring(index, index + 2), 16);
index += 2;
return val;
}
while (index < compressed.length) {
const noteDuration = nextByte();
if (noteDuration == null) {
break;
}
const waveform = nextByte();
const pitches = new Uint8ClampedArray(audio.NOTES_PER_EFFECT).fill(0);
const amplitudes = new Uint8ClampedArray(audio.NOTES_PER_EFFECT).fill(0);
for (let i = 0; i < audio.NOTES_PER_EFFECT; i++) {
pitches[i] = nextByte();
}
for (let i = 0; i < audio.NOTES_PER_EFFECT; i++) {
amplitudes[i] = nextByte();
}
result.push({
noteDuration,
waveform,
pitches,
amplitudes,
});
}
return result;
}
/**
* Populate the spriteBitmap and spriteData from a string containing the
* sprites data (as stored in the file). Each pixel is stored as a single
* hex digit. These are references into the PALETTE table.
* @param {string} text Hex encoded version of sprites data
* @see encodeSprites
* @return {Uint8ClampedArray} Pixel data
*/
export function decodeSprites(text) {
const pixelData = new Uint8ClampedArray(sprites.SPRITE_SHEET_WIDTH *
sprites.SPRITE_SHEET_HEIGHT * 4);
let offset = 0;
for (let i = 0; i < text.length; i++) {
if (!/[\s)]/.test(text[i])) {
const rgba = sprites.PALETTE[parseInt(text[i], 16)];
for (let i = 0; i < 4; i++) {
pixelData[offset++] = rgba[i];
}
}
}
return pixelData;
}
/**
* Replace the contents of the source code tab.
* @param {string} text
*/
export function setSourceCode(text) {
const source = document.getElementById('source');
source.innerHTML = '';
for (const line of text.trimEnd().split('\n')) {
const lineDiv = document.createElement('div');
if (line == '') {
lineDiv.innerText = ' '; // Avoid collapsing divs.
} else {
lineDiv.innerText = line;
}
source.appendChild(lineDiv);
}
}
/**
* Get the content of the source code tab as a string.
* @return {string} content of the source code tab
*/
export function getSourceCode() {
let source = '';
for (const lineDiv of document.getElementById('source').childNodes) {
if (lineDiv.innerText) {
source += lineDiv.innerText.trimEnd() + '\n';
}
}
return source;
}
/**
* Load the list of available files from the server, which are stored
* in a manifest file.
* This uses a manifest file rather than an explicit API, because the
* former allows serving from a public web server like github for demo mode.
* See serve.js for more description.
*/
export function updateFileList() {
fetch('games/manifest.json').then((response) => {
return response.json();
}).then((files) => {
const fileSelect = document.getElementById('fileSelect');
fileSelect.innerHTML = '<option value="">Select a file...</option>';
const selectOptions = files.map((file) =>
`<option value="${file}">${file}</option>`);
fileSelect.innerHTML += selectOptions.join('');
});
}