Skip to content

Commit

Permalink
File exporter: enable compression, use timestamps, handle missing fil…
Browse files Browse the repository at this point in the history
…etype (#141)
  • Loading branch information
curiousdannii committed Dec 24, 2023
1 parent 109cf76 commit e3890f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"dependencies": {
"base32768": "^3.0.1",
"emglken": "^0.5.1",
"fflate": "^0.8.1",
"file-saver": "^2.0.5",
"jquery": "^3.7.1",
"js-cookie": "^3.0.1",
"jszip": "^3.10.1",
"lodash-es": "^4.17.21"
},
"devDependencies": {
Expand Down
42 changes: 23 additions & 19 deletions src/tools/file-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,32 @@ https://github.com/curiousdannii/parchment

import {decode as base32768_decode} from 'base32768'
import filesaver from 'file-saver'
import JSZip from 'jszip'
import {ZippableFile, zipSync} from 'fflate'

declare global {
interface Window {run: () => void}
}

const record_parts = /content:(\w+):\w*:(.+)/
const extension_map: Record<string, string> = {
command: '.txt',
data: '.glkdata',
save: '.glksave',
transcript: '.txt',
command: 'txt',
data: 'glkdata',
save: 'glksave',
transcript: 'txt',
}
const metadata_parts = /modified:(\d+)$/
const record_parts = /^content:(\w+):\w*:(.+)$/

window.run = async function() {
window.run = function() {
// Check we have the correct Dialog storage version
const version = parseInt(localStorage.getItem('dialog_storage_version') || '', 10)
if (version !== 1) {
alert(`This tool doesn't support dialog_storage_version=${version}`)
return
}

// Create a zip file
const zip = new JSZip()

// Loop through localStorage to find the files
const files: Record<string, number> = {}
const files: Record<string, ZippableFile> = {}
const filenames: Record<string, number> = {}
for (const [key, value] of Object.entries(localStorage)) {
if (!key.startsWith('content:')) {
continue
Expand All @@ -49,18 +48,23 @@ window.run = async function() {
continue
}

const file = base32768_decode(value)
// Work out a unique filename, if there were multiple games with the same savefile names
const files_key = `${match[1]}:${match[2]}`
if (!files[files_key]) {
files[files_key] = 0
if (!filenames[files_key]) {
filenames[files_key] = 0
}
files[files_key]++
const duplicate_counter = files[files_key] > 1 ? `-${files[files_key]}` : ''
filenames[files_key]++
const duplicate_counter = filenames[files_key] > 1 ? `-${filenames[files_key]}` : ''
const filename = `parchment-files/${match[2]}${duplicate_counter}.${extension_map[match[1]] || match[1] || 'unknown'}`

// Get the modified date
const metadata = localStorage.getItem('dirent:' + key.substring(8))
const modified = metadata ? parseInt(metadata_parts.exec(metadata)![1]) : Date.now()

// Add the file
zip.file('parchment-files/' + match[2] + duplicate_counter + extension_map[match[1]], file)
files[filename] = [base32768_decode(value), {mtime: modified}]
}

const zip_file = await zip.generateAsync({type: 'blob'})
filesaver.saveAs(zip_file, 'parchment-files.zip')
const zip_file = zipSync(files)
filesaver.saveAs(new Blob([zip_file]), 'parchment-files.zip')
}

0 comments on commit e3890f3

Please sign in to comment.