Skip to content

Instantly share code, notes, and snippets.

@Dakedres
Last active May 13, 2020 10:26
Show Gist options
  • Save Dakedres/f45e8a74a717b6fd1061bff1f0b81911 to your computer and use it in GitHub Desktop.
Save Dakedres/f45e8a74a717b6fd1061bff1f0b81911 to your computer and use it in GitHub Desktop.
A https://windows93.net boot script which creates a settings.json in /a/.config/ which allows the usage of sound files in /a/

Install

1.1.1

  • Fixed the old MP3 mimetypes being left in the IndexedDB, leading to nasty lock up at boot
  • General code changes

1.1.0

  • Prevented a pretty much bricking of your Win93 install after deleting the config
    • If the config is deleted, the DataURIs in /a/settings.json will be saved in /a/.config/sounds
  • Script isn't started until storage is loaded, preventing some race conditions
  • Fixed mp3 support in le._get, for some reason it was saying it was MMPEG? If someone more knowledgable could inform me what was going on here it'd be greatly appreciated
  • Added version number
  • Learned a lesson about object cloning the hard way
// Creates a settings.json in /a/.config/ which allows the usage of sound files in /a/
// Version 1.1.1
;(assets => {
// Patch mp3 support into le._get
assets.merge(window.le._get, assets.constants.mimetypePatch)
const ready = () =>
assets.main(assets)
window.system42.once('storage:ready', ready)
})({
main: async assets => {
// Clean DB of Blob files with the wrong mimetypes
await assets.cleanDB(assets)
const { $file, $io, le } = window
const { promisify, constants, exists } = assets
const { configLocation } = constants,
open = promisify($file.open),
save = promisify($file.save),
toURI = promisify($io.Blob.DataURL)
const linked = await exists(configLocation)
let settings = {}
if(linked) {
[ settings ] = await open(configLocation, 'String')
settings = JSON.parse(settings)
}
settings = Object.assign(le._settings, settings)
let modifiedSettings = $io.clone(settings)
for(key in settings.sounds) {
const value = settings.sounds[key]
if(value.startsWith('/a/')) {
let [ sound ] = await open(value, 'Blob')
sound = (await toURI(sound))[0]
settings.sounds[key] = sound
}
// This can happen if the config is deleted
if(value.startsWith('data:')) {
const mimetype = constants.mimetypeRegex.exec(value),
ext = le._get.mime.ext[mimetype[1]][mimetype[2]].split(',')[0],
location = [ constants.soundsLocation + key, ext ].join('.')
let sound = await fetch(value)
sound = await sound.arrayBuffer()
await save(location, sound)
modifiedSettings.sounds[key] = location
}
}
if(!linked) {
let config = JSON.stringify(modifiedSettings)
await save(configLocation, config)
}
Object.assign(window.le._settings, settings)
},
cleanDB: async assets => {
const { $db, $fs, Blob } = window,
{ constants, promisify } = assets
const getKeys = promisify($db.keys, true),
getRaw = promisify($db.getRaw, true),
set = promisify($db.set, true)
const patches = Object.keys(constants.mimetypePatch.ext.mime)
const matches = path =>
patches.includes($fs.utils.getExt(path))
const targets = (await getKeys()).filter(matches)
for(const path of targets) {
const original = await getRaw(path),
type = $fs.utils.getMime(path),
modified = new Blob([ original ], { type })
await set(path, modified)
}
},
promisify: (() => {
const promisify = (original, isTraditional) => {
const async = (...args) => {
const executor = (resolve, reject) => {
const callback = (...items) => {
if(isTraditional) {
if(items[0])
reject(items[0])
else
resolve(items[1])
} else {
resolve(items)
}
}
try {
original(...args, callback)
} catch(error) {
reject(error)
}
}
return new Promise(executor)
}
return async
}
return promisify
})(),
exists: (() => {
const { $store, $db } = window
const exists = location => {
const executor = resolve => {
const key = location.slice(3), //No /a/
localKeys = $store.keys()
if(localKeys.includes(key))
return resolve(true)
const callback = (blank, dbKeys) => {
if(dbKeys.includes(key))
resolve(true)
else
resolve(false)
}
$db.keys(callback)
}
return new Promise(executor)
}
return exists
})(),
merge: (() => {
const merge = function self (...objects) {
let [ left, right ] = objects
right = { ...right } // Prevent overwriting source
for(const key in right) {
const source = right[key],
target = left[key]
if(target && source instanceof Object)
right[key] = self(target, source)
}
const output = Object.assign(left, right)
if(objects.length > 2) {
return self(output, ...objects.slice(2))
} else {
objects[0] = output
return output
}
}
return merge
})(),
constants: {
configLocation: '/a/.config/settings.json',
soundsLocation: '/a/.config/sounds/',
mimetypeRegex: /^data:([a-z0-9-]*)\/([a-z0-9-]*)[;,]/,
mimetypePatch: {
mime: {
ext: {
audio: {
mp3: "mp3"
}
}
},
ext: {
mime: {
mp3: "audio/mp3"
}
}
},
}
})
//dakedres was here ;3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment