| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (c) 2014 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 var PAK_FILE = 'id1/pak0.pak'; |
| 8 |
| 9 function updateStatus(message) { |
| 10 document.getElementById('status').innerHTML += message + '<br />'; |
| 11 } |
| 12 |
| 13 /* |
| 14 * Creates and add to the DOM the NaCl embed tag which |
| 15 * in effect launches Quake. |
| 16 */ |
| 17 function runQuake(pwd) { |
| 18 var embed = document.createElement('embed'); |
| 19 embed.width = 800; |
| 20 embed.height = 600; |
| 21 embed.type = 'application/x-nacl'; |
| 22 embed.src = 'quakespasm.nmf'; |
| 23 if (pwd) |
| 24 embed.setAttribute('PWD', pwd); |
| 25 document.getElementById('quake').appendChild(embed); |
| 26 } |
| 27 |
| 28 function extractNextEntry(entries, i, filesystem, oncomplete) { |
| 29 if (i === entries.length) { |
| 30 oncomplete(); |
| 31 return; |
| 32 } |
| 33 |
| 34 var entry = entries[i]; |
| 35 if (entry.directory) { |
| 36 filesystem.root.getDirectory(entry.filename, { create : true }, |
| 37 function() { |
| 38 extractNextEntry(entries, i + 1, filesystem, oncomplete); |
| 39 } |
| 40 ); |
| 41 return; |
| 42 } |
| 43 |
| 44 filesystem.root.getFile(entry.filename, { create : true }, |
| 45 function(fileEntry) { |
| 46 var writer = new zip.FileWriter(fileEntry); |
| 47 entry.getData(writer, |
| 48 function() { |
| 49 extractNextEntry(entries, i + 1, filesystem, oncomplete); |
| 50 }); |
| 51 }, |
| 52 function(error) { |
| 53 updateStatus('error creating temp file: ' + error); |
| 54 } |
| 55 ); |
| 56 } |
| 57 |
| 58 function extractZipFile(file, filesystem, oncomplete) { |
| 59 updateStatus('Extracting file: ' + file.name); |
| 60 zip.createReader(new zip.BlobReader(file), |
| 61 function(zipReader) { |
| 62 zipReader.getEntries(function(entries) { |
| 63 extractNextEntry(entries, 0, filesystem, oncomplete); |
| 64 }); |
| 65 }, |
| 66 function() { |
| 67 updateStatus('Error reading zip file'); |
| 68 } |
| 69 ) |
| 70 } |
| 71 |
| 72 function uploadDidChange(event) { |
| 73 function oncomplete() { |
| 74 // Once we have extracted all the files then |
| 75 // launch the game. |
| 76 updateStatus("Extracted all file. Launching Quake."); |
| 77 runQuake('/tmp'); |
| 78 } |
| 79 |
| 80 window.webkitRequestFileSystem(window.TEMPORARY, 50 * 1024 * 1024, |
| 81 function(fs) { |
| 82 var file = event.target.files[0]; |
| 83 extractZipFile(file, fs, oncomplete); |
| 84 }, |
| 85 function() { |
| 86 updateStatus('Error accessing html5 filesystem.'); |
| 87 } |
| 88 ); |
| 89 } |
| 90 |
| 91 function loadFromLocalStorage() { |
| 92 function loadFailure() { |
| 93 updateStatus('Quake data not found in local html5 filesystem.'); |
| 94 updateStatus('Please locate a quake level set (for example the ' |
| 95 + '<a href="http://www.libsdl.org/projects/quake/data/quakesw-1.0.6.zip">' |
| 96 + 'shareware levels</a>) and either extract them alongside the nmf file,' |
| 97 + ' or use the upload button below to unzip them in the local html5' |
| 98 + ' filesystem.'); |
| 99 // Create an html5 file input elemnt in so the user can upload the game |
| 100 // data as a zip file. |
| 101 document.getElementById('quake').innerHTML = |
| 102 '<input type="file" accept="application/zip" id="infile">'; |
| 103 document.getElementById('infile').addEventListener('change', |
| 104 uploadDidChange, |
| 105 false); |
| 106 } |
| 107 |
| 108 function loadSuccess() { |
| 109 updateStatus('Found pak file in local storage. Launching quake.'); |
| 110 runQuake('/tmp'); |
| 111 } |
| 112 |
| 113 window.webkitRequestFileSystem(window.TEMPORARY, 3 * 1024 * 1024, |
| 114 function(fs) { |
| 115 fs.root.getFile(PAK_FILE, {}, loadSuccess, loadFailure); |
| 116 } |
| 117 ); |
| 118 } |
| 119 |
| 120 function onLoad() { |
| 121 updateStatus('Searching for Quake data (' + PAK_FILE + ')'); |
| 122 var req = new XMLHttpRequest(); |
| 123 req.onload = function() { |
| 124 if (this.status === 200) { |
| 125 updateStatus('Found ' + PAK_FILE + '. Launching quake.'); |
| 126 runQuake(); |
| 127 } else { |
| 128 // Failed to find PAK_FILE on webserver, try local storage |
| 129 updateStatus('Quake data not found alongside executable.'); |
| 130 loadFromLocalStorage(); |
| 131 } |
| 132 }; |
| 133 req.open('GET', PAK_FILE); |
| 134 req.send(null); |
| 135 } |
| 136 |
| 137 window.onload = function() { |
| 138 onLoad(); |
| 139 }; |
| OLD | NEW |