-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.js
More file actions
165 lines (148 loc) · 5.44 KB
/
game.js
File metadata and controls
165 lines (148 loc) · 5.44 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
export default async (canvas, uri, arg) => {
return new Promise(async (resolve, reject) => {
const fetchPkg = async () => {
// Open the local database used to cache packages
const db = await new Promise((resolve, reject) => {
//const indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
const req = indexedDB.open('NET_LOCJAM_PRELOAD_CACHE', 1);
req.onupgradeneeded = (event) => {
const db = event.target.result;
if (db.objectStoreNames.contains('PACKAGES')) {
db.deleteObjectStore('PACKAGES');
};
if (db.objectStoreNames.contains('ETAGS')) {
db.deleteObjectStore('ETAGS');
};
db.createObjectStore('PACKAGES');
db.createObjectStore('ETAGS');
};
req.onerror = (error) => {
reject(error);
};
req.onsuccess = (event) => {
resolve(event.target.result);
};
});
// get remote file's ETag
let res = await fetch(uri, {method: 'HEAD'});
if (!res.ok) {
alert('Could not fetch the love package');
return null;
}
const etag = res.headers.get("Etag");
let data = null;
if (etag) {
// Check if there's a cached package, and if so whether it's the latest available
var results = await new Promise((resolve, reject) => {
const trans = db.transaction(['PACKAGES', 'ETAGS'], 'readonly');
const packages = trans.objectStore('PACKAGES');
const etags = trans.objectStore('ETAGS');
const packagesRequest = packages.get(uri);
const etagsRequest = etags.get(uri);
let results = {};
packagesRequest.onsuccess = (event) => {
results.package = event.target.result;
};
etagsRequest.onsuccess = (event) => {
results.etag = event.target.result;
};
trans.oncomplete = (event) => {
resolve(results);
}
trans.onerror = (error) => {
reject(error);
};
});
if (results.etag == etag)
data = results.package;
};
// Fetch the package remotely, if we do not have it in local storage
if (!data || !(data instanceof Uint8Array)) {
console.log('fetching ' + uri + ' remotely');
const res = await fetch(uri);
if (!res.ok)
return reject('Could not fetch the love package');
data = await res.arrayBuffer();
// Check if the header is a valid ZIP archive
data = new Uint8Array(data);
const head = [80,75,3,4];
for (let i = 0; i < head.length; i++)
if (data[i] != head[i])
return reject('The fetched resource is not a valid love package');
// Cache remote package for subsequent requests
await new Promise((resolve, reject) => {
const trans = db.transaction(['PACKAGES', 'ETAGS'], 'readwrite');
const packages = trans.objectStore('PACKAGES');
const etags = trans.objectStore('ETAGS');
packages.put(data, uri);
etags.put(etag, uri);
trans.oncomplete = (event) => {
resolve();
}
trans.onerror = (error) => {
reject(error);
};
});
}
else {
console.log('fetching ' + uri + ' from cache');
};
return data;
}
const data = await fetchPkg();
const pkg = 'game.love'; //uri.substring(uri.lastIndexOf('/') + 1);
let Module = {};
const mem = (navigator.deviceMemory || 1)*1e+9;
Module.INITIAL_MEMORY = Math.min(4*data.length + 2e+7, mem);
Module.canvas = canvas;
Module.printErr = window.onerror;
Module.arguments = [pkg];
if (arg && Array.isArray(arg))
for (let i = 0; i < arg.length; i++)
Module.arguments.push(String(arg[i]));
const runWithFS = async () => {
Module.addRunDependency('fp '+pkg);
// Copy the entire loaded file into a spot in the heap.
// Files will refer to slices in the heap, but cannot be freed
// (we may be allocating before malloc is ready, during startup).
if (Module['SPLIT_MEMORY'])
Module.printErr('warning: you should run the file packager with --no-heap-copy when SPLIT_MEMORY is used, otherwise copying into the heap may fail due to the splitting');
//const data = await fetchPkg();
const ptr = Module.getMemory(data.length);
Module['HEAPU8'].set(data, ptr);
Module.FS_createDataFile(pkg, null, data, true, true, true);
Module.removeRunDependency('fp '+pkg);
resolve(Module);
Module.finishedDataFileDownloads ++;
}
if (Module.calledRun) {
runWithFS();
} else {
// FS is not initialized yet, wait for it
if (!Module.preRun)
Module.preRun = [];
Module.preRun.push(runWithFS);
}
if (window.Love === undefined) {
// this operation initiates local storage
let s = document.createElement('script');
s.type = 'text/javascript';
s.src = 'love/love.js';
s.async = true;
s.onload = () => {
Love(Module);
};
document.body.appendChild(s);
} else {
window.Module.pauseMainLoop();
Love(Module);
}
Module._luaCode = "";
Module._getLuaCode = function() {
let s = Module._luaCode;
Module._luaCode = "";
return s;
}
window.Module = Module;
});
};