Skip to content

Instantly share code, notes, and snippets.

@subsoap
Created September 26, 2016 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subsoap/a602963f4a9a5323207edf1c3e433d75 to your computer and use it in GitHub Desktop.
Save subsoap/a602963f4a9a5323207edf1c3e433d75 to your computer and use it in GitHub Desktop.
Testing Get and Set data to Defold filesystem on HTML5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>My project 0.1</title>
<style>
.canvas-app-container {
background: rgb(255,255,255) no-repeat center url("try_game.png");
/* A positioned parent for loading visuals */
position: relative;
}
.canvas-app-container:-webkit-full-screen {
/* Auto width and height in Safari/Chrome fullscreen. */
width: auto;
height: auto;
}
.canvas-app-progress {
position: absolute;
background-color: rgb(245, 245, 245);
height: 20px;
/* Progress same width as canvas. */
width: 1280px;
bottom: 0px;
}
.canvas-app-progress-bar {
font-size: 12px;
height: 20px;
color: rgb(255, 255, 255);
background-color: rgb(30, 100, 234);
text-align: center;
line-height: 20px;
}
.button {
color: #fff;
background-color: #1e64ea;
border-color: transparent;
padding: 10px 20px;
}
</style>
</head>
<body>
<div id="fb-root"></div>
<div id="app-container" class="canvas-app-container">
<canvas id="canvas" class="canvas-app-canvas" tabindex="1" width="1280" height="720"></canvas>
</div>
<button id="fullscreen" class="button">Fullscreen</button>
<!-- -->
<script type='text/javascript' src="dmloader.js"></script>
<script type='text/javascript' src="My project.js" async onload=""></script>
<!-- -->
<script type='text/javascript'>
var extra_params = {
archive_location_filter: function( path ) {
return ("archive" + path + "");
},
engine_arguments: ["--verify-graphics-calls=false"],
splash_image: "splash_image.png",
custom_heap_size: 268435456
}
Module.runApp("canvas", extra_params);
/* Fullscreen button */
document.getElementById("fullscreen").onclick = function (e) {
Module.toggleFullscreen();
};
</script>
<script type='text/javascript'>
// this is a proof, do not use as is
function html_inbox() {
// This works on all devices/browsers
console.log("Sanity Test .....")
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var DB_NAME = "/data";
var DB_VERSION = 21;
var DB_STORE_NAME = "FILE_DATA";
// Open (or create) the database
var open;
try {
open = indexedDB.open(DB_NAME, DB_VERSION);
console.log("DB open!")
} catch (e) {
return callback(e)
}
open.onupgradeneeded = function() {
// todo: match the way for 21 THIS IS INCOMPLETE!
console.log("creating /data db");
var db = open.result;
db.createObjectStore(DB_STORE_NAME)
}
open.onsuccess = function() {
// Start a new transaction
var db = open.result;
var transaction = db.transaction(DB_STORE_NAME, "readwrite");
var store = transaction.objectStore(DB_STORE_NAME);
var index = store.index("timestamp"); // kind of weird that all of the Defold file timestamps are not set
// test put data
var test_data_array = {timestamp: {}, mode: 33206};
test_data_array.contents = stringToUint("test data here"); // this is obviously not JSON,
store.put(test_data_array, "/data/.html5_inbox/data.json")
// test get data
var get_html5_inbox = store.get("/data/.html5_inbox/data.json");
get_html5_inbox.onsuccess = function() {
console.log(uintToString(get_html5_inbox.result.contents));
}
// Close the db when the transaction is done
transaction.oncomplete = function() {
db.close();
};
}
}
function stringToUint(string) {
var string = btoa(unescape(encodeURIComponent(string))),
charList = string.split(''),
uintArray = [];
for (var i = 0; i < charList.length; i++) {
uintArray.push(charList[i].charCodeAt(0));
}
return new Uint8Array(uintArray);
}
function uintToString(uintArray) {
var encodedString = String.fromCharCode.apply(null, uintArray),
decodedString = decodeURIComponent(escape(atob(encodedString)));
return decodedString;
}
html_inbox();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment