commit 5f46a8502f900ee8eefe903486f70454487a6618 Author: Michael Murtaugh Date: Thu Jan 21 12:50:59 2021 +0100 webpack + parsetic diff --git a/package.json b/package.json new file mode 100644 index 0000000..900564b --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "ticparse.js", + "version": "1.0.0", + "description": "", + "main": "db.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "webpack": "^5.16.0", + "webpack-cli": "^4.4.0" + }, + "dependencies": { + "codemirror": "^5.59.2", + "d3-selection": "^2.0.0", + "filesize": "^6.1.0", + "indexed-pdb": "^1.1.2" + } +} diff --git a/src/inspect_tic.js b/src/inspect_tic.js new file mode 100644 index 0000000..23242af --- /dev/null +++ b/src/inspect_tic.js @@ -0,0 +1,78 @@ +import { openDB } from 'indexed-pdb' +import * as tic from './ticparse.js' +import * as filesize from 'filesize'; +import {select, selectAll} from "d3-selection"; +import * as CodeMirror from 'codemirror'; +import 'codemirror/mode/lua/lua.js'; +//require('codemirror/addon/fold/foldcode.js'); +//require('codemirror/addon/fold/foldgutter.js'); +//require('codemirror/addon/fold/markdown-fold.js') +//require("codemirror/theme/monokai.css") + + +// console.log("codemirror", codemirror); + +const DB_NAME = "/com.nesbox.tic/TIC-80", + DB_STORE = "FILE_DATA", + table = select("#listing").append("table"), + thead = table.append("thead").append("tr"), + tbody = table.append("tbody"), + code = CodeMirror(document.getElementById("code"), {"mode": "lua", "theme": "default"}); + +thead.selectAll("th").data(["name", "size", "time"]).enter().append("th").html(d => d); + + +(async () => { + try { + const db = await openDB(DB_NAME); + var objectStore = db.transaction([DB_STORE], "readonly").objectStore(DB_STORE); + var files = []; + + await objectStore.openCursor().then(function push_item(cursor) { + if (!cursor) { return } + // console.log("cursor.value", cursor.value); + var filename = cursor.key.substr("/com.nesbox.tic/TIC-80/".length); + if (!filename.startsWith(".")) { + // console.log(cursor.value.contents); + files.push({ + key: cursor.key, + filename: filename, + size: cursor.value.contents.length, + timestamp: cursor.value.timestamp + }); + } + return cursor.continue().then(push_item) + }) + + // console.log("files", files); + let tr = tbody.selectAll("tr").data(files).enter().append("tr"); + tr.append("td").append("a").attr("href", "#").html(d => d.filename).on("click", (e, d) => { + e.preventDefault(); + load_key(d.key); + }); + tr.append("td").html(d => filesize(d.size)); + tr.append("td").html(d => d.timestamp.toLocaleString()); + + } catch (error) { + console.log(error, 'any error during the process'); + } + +})() + +var cart; + +async function load_key (key) { + const db = await openDB(DB_NAME) + var objectStore = db.transaction([DB_STORE], "readonly").objectStore(DB_STORE); + var files = []; + + await objectStore.get(key).then((cursor) => { + // console.log("load_key", cursor); + window.cart = cursor.contents; + var cart = tic.parsetic(cursor.contents); + // console.log("code_textarea", code_textarea); + code.setValue(cart.chunks_by_type[tic.CHUNK_CODE].text); + }); + +} + diff --git a/src/ticparse.js b/src/ticparse.js new file mode 100644 index 0000000..c19b03c --- /dev/null +++ b/src/ticparse.js @@ -0,0 +1,46 @@ +// https://github.com/nesbox/TIC-80/wiki/TIC-File-Format +export const CHUNK_TILES = 1, + CHUNK_SPRITES = 2, + CHUNK_COVER = 3, + CHUNK_MAP = 4, + CHUNK_CODE = 5, + CHUNK_FLAGS = 6, + CHUNK_SAMPLES = 9, + CHUNK_WAVEFORM = 10, + CHUNK_PALETTE = 12, + CHUNK_MUSIC_TRACKS = 14, + CHUNK_MUSIC_PATTERNS = 15, + CHUNK_CODE_ZIP = 16, + LITTLE_ENDIAN = 1; + +export function parsetic (buf) { + var ret = {}, + data = new DataView(buf.buffer), + i=0, + bank, + ctype, + clen, + future_use; + + ret.chunks = []; + ret.chunks_by_type = {}; + while (i> 5; + ctype = buf[i] & 0b11111; + i+=1; + clen = data.getUint16(i, LITTLE_ENDIAN); + i+=2; + future_use = buf[i]; + i+=1; // byte reserved for future_use + var chunk = {bank: bank, ctype: ctype, length: clen, future_use: future_use}; + ret.chunks.push(chunk); + ret.chunks_by_type[ctype] = chunk; + chunk.data = new DataView(buf.buffer, i, clen); + if (ctype == CHUNK_CODE) { + var td = new TextDecoder(); + chunk.text = td.decode(chunk.data); + } + i += clen; + } + return ret; +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..c41e052 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,10 @@ +const path = require('path'); + +module.exports = { + entry: './src/inspect_tic.js', + mode: "development", + output: { + filename: 'inspect_tic.js', + path: path.resolve(__dirname, 'dist'), + }, +}; \ No newline at end of file