| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <title>fheroes2</title> |
| 5 | <link rel="preconnect" href="https://fonts.googleapis.com"> |
| 6 | <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> |
| 7 | <link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet"> |
| 8 | <script src="https://cdnjs.cloudflare.com/ajax/libs/progress.js/0.1.0/progress.min.js" integrity="sha512-CklrzCqwODBOEJHJq73SZrgWC7xcxssgg5M1xokosfDDz2/nLTCuMDyc51gbJtb8DriV4EYjJSlklPH5Ejn9XA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> |
| 9 | <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/progress.js/0.1.0/progressjs.min.css" integrity="sha512-ovRFHsWpnYUBNZd/8CmbaWAKBDO8xb0l5Y8PdIae1O5RXO2QyU3CZGNzuYuE0CQHKazIzWMQpnTn26WpPjexfw==" crossorigin="anonymous" referrerpolicy="no-referrer" /> |
| 10 | <style> |
| 11 | body { margin: 0; height: 0; font-family: "Roboto", serif;} |
| 12 | #canvas { |
| 13 | height: 100vh; |
| 14 | width: 100vw; |
| 15 | display: block; |
| 16 | margin: 0 auto; |
| 17 | background: url(./fheroes2.jpeg) center center; |
| 18 | background-size: cover; |
| 19 | position: absolute; |
| 20 | z-index: 10; |
| 21 | } |
| 22 | #uploader, #launcher { |
| 23 | z-index: 100; |
| 24 | position: absolute; |
| 25 | color: #fff; |
| 26 | font-size: 30px; |
| 27 | -webkit-text-stroke: 1px #000; |
| 28 | height: 100vh; |
| 29 | width: 100vw; |
| 30 | } |
| 31 | #launcher { |
| 32 | display: none; |
| 33 | -webkit-text-stroke: 1px #fff; |
| 34 | } |
| 35 | #uploader input[type=file] { |
| 36 | display: none |
| 37 | } |
| 38 | #uploader label { |
| 39 | text-align: center; |
| 40 | display: block; |
| 41 | cursor: pointer; |
| 42 | height: 100vh; |
| 43 | width: 100vw; |
| 44 | position: absolute; |
| 45 | } |
| 46 | #uploader label span { |
| 47 | padding: 10vh 0 0 0; |
| 48 | display: block; |
| 49 | } |
| 50 | #launcher button { |
| 51 | font-size: 30px; |
| 52 | } |
| 53 | #launcher .buttons { |
| 54 | text-align: center; |
| 55 | padding: 10vh 0 0 0; |
| 56 | } |
| 57 | </style> |
| 58 | </head> |
| 59 | <body> |
| 60 | <canvas id="canvas" width="640" height="400"></canvas> |
| 61 | <div id="uploader"> |
| 62 | <label> |
| 63 | <span>Click to select your Heroes 2 folder</span> |
| 64 | <input |
| 65 | type="file" |
| 66 | directory="directory" |
| 67 | multiple |
| 68 | webkitdirectory="webkitdirectory" |
| 69 | id="files" |
| 70 | /> |
| 71 | </label> |
| 72 | </div> |
| 73 | <div id="launcher"> |
| 74 | <div class="buttons"> |
| 75 | <button onclick="Module.startGame()">Start game</button> |
| 76 | <button onclick="Module.deleteFiles()">Delete files</button> |
| 77 | </div> |
| 78 | </div> |
| 79 | <script> |
| 80 | const S_IFMT = 61440; /* 0x0170000 type of file */ |
| 81 | const S_IFDIR = 16384; /* 0x0040000 directory */ |
| 82 | const progressBar = progressJs("#canvas").setOptions({ |
| 83 | theme: 'blueOverlayRadiusWithPercentBar', |
| 84 | overlayMode: true, |
| 85 | considerTransition: true |
| 86 | }); |
| 87 | const canvas = document.querySelector('canvas'); |
| 88 | canvas.oncontextmenu = event => event.preventDefault(); |
| 89 | Object.assign(globalThis, { Module: { |
| 90 | preRun: [ |
| 91 | () => Object.assign(globalThis.ENV, { |
| 92 | FHEROES2_DATA: `/fheroes2/data`, |
| 93 | HOME: '/fheroes2' |
| 94 | }), |
| 95 | () => { |
| 96 | addRunDependency('syncfs'); |
| 97 | FS.mkdir('/fheroes2'); |
| 98 | FS.mount(IDBFS, { root: '/' }, '/fheroes2'); |
| 99 | FS.syncfs(true, (err) => { |
| 100 | if (err) throw err; |
| 101 | try { |
| 102 | if (Object.keys(FS.lookupPath(`${ENV.FHEROES2_DATA}`)).length === 0) return; |
| 103 | document.querySelector('#uploader').style.display = 'none'; |
| 104 | document.querySelector('#launcher').style.display = 'block'; |
| 105 | } |
| 106 | catch (ignore) { |
| 107 | FS.mkdir(`${ENV.FHEROES2_DATA}`); |
| 108 | } |
| 109 | }); |
| 110 | }, |
| 111 | () => { |
| 112 | document.querySelector('input[type=file]').addEventListener('input', e => { |
| 113 | const files = [...e.target.files]; |
| 114 | const requiredFiles = ['data/HEROES2.AGG', 'data/HEROES2X.AGG'] |
| 115 | if (files.length === 1) return Module.clearInput('zip archives are not supported yet, sorry'); |
| 116 | if (files.length > 3000) return Module.clearInput('wrong directory, or your maps collection is really awesome.'); |
| 117 | const requiredResolved = files.filter(({ webkitRelativePath: path }) => |
| 118 | requiredFiles.some(rq => path.toLocaleLowerCase().endsWith(rq.toLocaleLowerCase()))).length === 2; |
| 119 | if (!requiredResolved) return Module.clearInput('wrong directory'); |
| 120 | const whitListed = ['data', 'heroes2', 'maps', 'music']; |
| 121 | files.filter(file => { |
| 122 | const [fileName, ...path] = file.webkitRelativePath.split('/').reverse(); |
| 123 | if (fileName.startsWith('.')) return false; |
| 124 | return path |
| 125 | .map(folder => folder.toLocaleLowerCase()) |
| 126 | .some(folder => whitListed.includes(folder)) |
| 127 | }).reduce((uploaded, file, _ignore, queue) => { |
| 128 | const [fileName, ...path] = file.webkitRelativePath.split('/').reverse(); |
| 129 | const [ignore, ...relativePath] = path.reverse(); |
| 130 | Module.mkdirWithParents(`${ENV.FHEROES2_DATA}/${relativePath.join('/')}`); |
| 131 | |
| 132 | const reader = new FileReader(); |
| 133 | reader.addEventListener('error', console.error); |
| 134 | reader.addEventListener('loadend', ({ target: { result } }) => { |
| 135 | FS.writeFile(`${ENV.FHEROES2_DATA}/${relativePath.join('/')}/${fileName}`, new Uint8Array(result), { |
| 136 | encoding: 'binary' |
| 137 | }); |
| 138 | uploaded.push(file); |
| 139 | if (uploaded.length === queue.length) { |
| 140 | FS.syncfs(false, err => { |
| 141 | if (err) return Module.clearInput('Failed to sync FS'); |
| 142 | document.querySelector('#uploader').style.display = 'none'; |
| 143 | document.querySelector('#launcher').style.display = 'block'; |
| 144 | }) |
| 145 | } |
| 146 | }); |
| 147 | reader.readAsArrayBuffer(file); |
| 148 | return uploaded; |
| 149 | }, []); |
| 150 | }) |
| 151 | } |
| 152 | ], |
| 153 | clearInput: (msg = '') => { |
| 154 | document.querySelector('input[type=file]').value = null; |
| 155 | msg && alert(msg); |
| 156 | }, |
| 157 | mkdirWithParents: path => { |
| 158 | const parts = path.split('/'); |
| 159 | try { FS.lookupPath(parts.join('/')) } |
| 160 | catch (ignore) { |
| 161 | parts.reduce((p, part) => { |
| 162 | p = [...p, part]; |
| 163 | if (!part) return p; |
| 164 | try { FS.lookupPath(p.join('/')) } |
| 165 | catch (ignore) { FS.mkdir(p.join('/')) } |
| 166 | return p; |
| 167 | }, []); |
| 168 | } |
| 169 | }, |
| 170 | clearPath: path => { |
| 171 | try { |
| 172 | Object.keys(FS.lookupPath(path).node.contents).forEach(node => |
| 173 | (FS.stat(`${path}/${node}`).mode & S_IFMT) === S_IFDIR |
| 174 | ? Module.clearPath(`${path}/${node}`) |
| 175 | : FS.unlink(`${path}/${node}`)); |
| 176 | FS.rmdir(`${path}`) |
| 177 | } catch (ignore) {} |
| 178 | }, |
| 179 | startGame: () => { |
| 180 | removeRunDependency('syncfs'); |
| 181 | document.querySelector('#uploader').style.display = 'none'; |
| 182 | document.querySelector('#launcher').style.display = 'none'; |
| 183 | }, |
| 184 | deleteFiles: () => { |
| 185 | if (false === confirm('Are you sure?')) return; |
| 186 | Module.clearPath(ENV.FHEROES2_DATA); |
| 187 | FS.syncfs(false, ignore => {}); |
| 188 | Module.clearInput(); |
| 189 | document.querySelector('#uploader').style.display = 'block'; |
| 190 | document.querySelector('#launcher').style.display = 'none'; |
| 191 | }, |
| 192 | setStatus: status => { |
| 193 | const dlProgressRE = /(?<progress>\d+)\/(?<total>\d+)/ig; |
| 194 | if (!status || !dlProgressRE.test(status)) { |
| 195 | if ((status ?? '').startsWith('Downloading data')) progressBar.start(); |
| 196 | return; |
| 197 | } |
| 198 | dlProgressRE.lastIndex = 0; |
| 199 | const {groups: {progress, total}} = [...status.matchAll(dlProgressRE)][0]; |
| 200 | const done = Math.round(progress / total * 100); |
| 201 | progressBar.set(done); |
| 202 | if (100 === done) progressBar.end(); |
| 203 | }, |
| 204 | canvas |
| 205 | } |
| 206 | }); |
| 207 | |
| 208 | document.head.append(Object.assign(document.createElement('script'), {src: './fheroes2.js'})); |
| 209 | </script> |
| 210 | </body> |