-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebdb.js
151 lines (141 loc) · 4.39 KB
/
webdb.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
/**
* @author Marcelo Silva Nascimento Mancini
* @github https://github.com/MrcSnm/Love.js-Api-Player
*/
//This line is needed for retrieving data from .lua
//Please remember that this is not a flexible API, it should be used with consolewrapper.js + love.js
//This will activate the web database for using in conjunction to .lua + love.js
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
if(!window.indexedDB)
{
alert("Please, update your browser to newer versions or you won't be able to save your game")
}
var __webDb = null;
var __webTunnel = ""
var __currRequest = null
async function __getWebDB(dbName)
{
if(__webTunnel == "")
__webTunnel = dbName;
return new Promise(function(resolve, reject)
{
if(__webDb != null)
{
console.log("Already connected with WebDB")
return resolve();
}
if(__currRequest != null)
{
let success = __currRequest.onsuccess
__currRequest.onsuccess = function()
{
success()
console.log("Request stack completed")
return resolve();
}
let blocked = __currRequest.onblocked
__currRequest.onblocked = function()
{
blocked()
return reject();
}
}
else
{
__currRequest = indexedDB.open(dbName);
__currRequest.onerror = function()
{
console.error("Some error ocurred when trying to access web database '" + dbName + "'");
reject();
}
//This webdb should not change version, so it will not add any field
__currRequest.onupgradeneeded = function()
{
__webDb = __currRequest.result;
__webDb.createObjectStore("FILE_DATA");
}
__currRequest.onblocked = function(ev)
{
console.error("Access to the web database were blocked, please refresh the page");
reject();
}
__currRequest.onsuccess = function()
{
__webDb = __currRequest.result;
console.log("Connected with WebDB '"+dbName+"'")
}
}
})
}
/**
* This function must not be called manually
* @param {any} val
*/
function ___convertToUint8Array(val)
{
val = String(val);
let content = new Uint8Array(val.length);
for(i = 0, len = val.length; i < len; i++)
{
content[i] = (val[i]).charCodeAt(0);
}
return content;
}
/**
* This function must not be called manually
* @param {any} value
*/
function ___getLoveJSCompatibleObject(value)
{
value = ___convertToUint8Array(value); //Content must be Uint8Array
return {
timestamp: Date.now(),
mode : 33152, //For some reason it is the mode used for when doing love.filesystem.write
contents: value //Uint8Array
}
}
async function __storeWebDB(value, objStore, where)
{
await __getWebDB(__webTunnel);
console.log("Storing value '" + value + "'")
let lovejsCompatibleObject = ___getLoveJSCompatibleObject(value)
let transaction = __webDb.transaction(objStore, "readwrite");
let store = transaction.objectStore(objStore);
let request = store.put(lovejsCompatibleObject, where);
request.onsuccess = function()
{
console.log("'" + value + "' were succesfully added to "+where);
}
request.onerror = function()
{
console.error("Could not add the value '" + value + "' to "+where +": " + request.error);
}
}
function __unconvertFromUint8Array(arr)
{
return String.fromCharCode(arr);
}
/**
* Please use it only for debugging purposes
* @param {String= "FILE_DATA"} objStore
*/
async function __readWebDB(objStore, where)
{
await __getWebDB(__webTunnel);
objStore = (objStore == undefined) ? "FILE_DATA" : objStore;
let transaction = __webDb.transaction(objStore, "readonly");
let store = transaction.objectStore(objStore);
let request = store.get(where);
request.onsuccess = function()
{
console.log("Value gotten: " + __unconvertFromUint8Array(request.result.contents));
}
}
if(typeof FS === 'undefined')
{
FS = {};
FS.writeFile = function(where, content)
{
__storeWebDB(content, "FILE_DATA", where);
}
}