1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-26 10:49:17 +00:00

can upload binary files, include with incbin

This commit is contained in:
Steven Hugg 2018-11-30 10:43:23 -05:00
parent 6e41dcd6c2
commit 4bbef9365c
3 changed files with 54 additions and 40 deletions

View File

@ -84,7 +84,7 @@ export class CodeProject {
} else { } else {
// for .asm -- [.]include "file" // for .asm -- [.]include "file"
// for .c -- #include "file" // for .c -- #include "file"
var re2 = /^\s+([.#]?include)\s+"(.+?)"/gmi; var re2 = /^\s+([.#]?include|incbin)\s+"(.+?)"/gmi;
while (m = re2.exec(text)) { while (m = re2.exec(text)) {
this.pushAllFiles(files, m[2]); this.pushAllFiles(files, m[2]);
} }

View File

@ -5,13 +5,13 @@
import $ = require("jquery"); import $ = require("jquery");
import * as bootstrap from "bootstrap"; import * as bootstrap from "bootstrap";
import { CodeProject } from "./project"; import { CodeProject } from "./project";
import { WorkerResult, WorkerOutput, VerilogOutput, SourceFile, WorkerError } from "./workertypes"; import { WorkerResult, WorkerOutput, VerilogOutput, SourceFile, WorkerError, FileData } from "./workertypes";
import { ProjectWindows } from "./windows"; import { ProjectWindows } from "./windows";
import { Platform, Preset, DebugSymbols } from "./baseplatform"; import { Platform, Preset, DebugSymbols } from "./baseplatform";
import { PLATFORMS } from "./emu"; import { PLATFORMS } from "./emu";
import * as Views from "./views"; import * as Views from "./views";
import { createNewPersistentStore } from "./store"; import { createNewPersistentStore } from "./store";
import { getFilenameForPath, getFilenamePrefix, highlightDifferences, invertMap, byteArrayToString, compressLZG } from "./util"; import { getFilenameForPath, getFilenamePrefix, highlightDifferences, invertMap, byteArrayToString, compressLZG, byteArrayToUTF8 } from "./util";
import { StateRecorderImpl } from "./recorder"; import { StateRecorderImpl } from "./recorder";
// external libs (TODO) // external libs (TODO)
@ -259,19 +259,31 @@ function _uploadNewFile(e) {
function handleFileUpload(files: File[]) { function handleFileUpload(files: File[]) {
console.log(files); console.log(files);
var index = 0; var index = 0;
var gotoMainFile = (files.length == 1);
function uploadNextFile() { function uploadNextFile() {
var f = files[index++]; var f = files[index++];
if (!f) { if (!f) {
console.log("Done uploading"); console.log("Done uploading");
gotoNewLocation(); if (gotoMainFile)
gotoNewLocation(); // TODO: upload w/o starting new project?
else
alert("Files uploaded.");
} else { } else {
var path = "local/" + f.name; var path = "local/" + f.name;
var reader = new FileReader(); var reader = new FileReader();
reader.onload = function(e) { reader.onload = function(e) {
var data = (<any>e.target).result; var arrbuf = (<any>e.target).result as ArrayBuffer;
var data : FileData = new Uint8Array(arrbuf);
// convert to UTF8, unless it's a binary file (TODO)
if (path.endsWith("bin")) {
gotoMainFile = false;
} else {
data = byteArrayToUTF8(data);
}
// store in local forage
store.setItem(path, data, function(err, result) { store.setItem(path, data, function(err, result) {
if (err) if (err)
console.log(err); alert("Error uploading " + path + ": " + err);
else { else {
console.log("Uploaded " + path + " " + data.length + " bytes"); console.log("Uploaded " + path + " " + data.length + " bytes");
if (index == 1) if (index == 1)
@ -280,7 +292,7 @@ function handleFileUpload(files: File[]) {
} }
}); });
} }
reader.readAsText(f); reader.readAsArrayBuffer(f); // read as binary
} }
} }
if (files) uploadNextFile(); if (files) uploadNextFile();
@ -460,6 +472,7 @@ function _downloadAllFilesZipFile(e) {
store.keys( (err, keys : string[]) => { store.keys( (err, keys : string[]) => {
if (err) throw err; if (err) throw err;
keys.forEach((path) => { keys.forEach((path) => {
// TODO: handle binary files
store.getItem(path, (err, text) => { store.getItem(path, (err, text) => {
if (text) { if (text) {
zip.file(fixFilename(getFilenameForPath(path)), text); zip.file(fixFilename(getFilenameForPath(path)), text);

View File

@ -258,39 +258,8 @@ export function lzgmini() {
} }
// Get the decoded string from an UTF-8 encoded array // Get the decoded string from an UTF-8 encoded array
this.getStringUTF8 = function():string this.getStringUTF8 = function():string {
{ return byteArrayToUTF8(outdata);
var str = "";
if (outdata != null)
{
var charLUT = new Array();
for (var i = 0; i < 128; ++i)
charLUT[i] = String.fromCharCode(i);
var c;
var outlen = outdata.length;
for (var i = 0; i < outlen;)
{
c = outdata[i++];
if (c < 128)
{
str += charLUT[c];
}
else
{
if ((c > 191) && (c < 224))
{
c = ((c & 31) << 6) | (outdata[i++] & 63);
}
else
{
c = ((c & 15) << 12) | ((outdata[i] & 63) << 6) | (outdata[i+1] & 63);
i += 2;
}
str += String.fromCharCode(c);
}
}
}
return str;
} }
} }
@ -314,6 +283,38 @@ export function byteArrayToString(outdata : number[] | Uint8Array) : string {
return str; return str;
} }
export function byteArrayToUTF8(outdata : number[] | Uint8Array) : string {
var str = "";
var charLUT = new Array();
for (var i = 0; i < 128; ++i)
charLUT[i] = String.fromCharCode(i);
var c;
var outlen = outdata.length;
for (var i = 0; i < outlen;)
{
c = outdata[i++];
if (c < 128)
{
str += charLUT[c];
}
else
{
if ((c > 191) && (c < 224))
{
c = ((c & 31) << 6) | (outdata[i++] & 63);
}
else
{
c = ((c & 15) << 12) | ((outdata[i] & 63) << 6) | (outdata[i+1] & 63);
i += 2;
if (c == 0xfeff) continue; // ignore BOM
}
str += String.fromCharCode(c);
}
}
return str;
}
export function removeBOM(s:string) { export function removeBOM(s:string) {
if (s.charCodeAt(0) === 0xFEFF) { if (s.charCodeAt(0) === 0xFEFF) {
s = s.substr(1); s = s.substr(1);