mirror of
https://github.com/sehugg/8bitworkshop.git
synced 2024-11-22 14:33:51 +00:00
can upload binary files, include with incbin
This commit is contained in:
parent
6e41dcd6c2
commit
4bbef9365c
@ -84,7 +84,7 @@ export class CodeProject {
|
||||
} else {
|
||||
// for .asm -- [.]include "file"
|
||||
// for .c -- #include "file"
|
||||
var re2 = /^\s+([.#]?include)\s+"(.+?)"/gmi;
|
||||
var re2 = /^\s+([.#]?include|incbin)\s+"(.+?)"/gmi;
|
||||
while (m = re2.exec(text)) {
|
||||
this.pushAllFiles(files, m[2]);
|
||||
}
|
||||
|
25
src/ui.ts
25
src/ui.ts
@ -5,13 +5,13 @@
|
||||
import $ = require("jquery");
|
||||
import * as bootstrap from "bootstrap";
|
||||
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 { Platform, Preset, DebugSymbols } from "./baseplatform";
|
||||
import { PLATFORMS } from "./emu";
|
||||
import * as Views from "./views";
|
||||
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";
|
||||
|
||||
// external libs (TODO)
|
||||
@ -259,19 +259,31 @@ function _uploadNewFile(e) {
|
||||
function handleFileUpload(files: File[]) {
|
||||
console.log(files);
|
||||
var index = 0;
|
||||
var gotoMainFile = (files.length == 1);
|
||||
function uploadNextFile() {
|
||||
var f = files[index++];
|
||||
if (!f) {
|
||||
console.log("Done uploading");
|
||||
gotoNewLocation();
|
||||
if (gotoMainFile)
|
||||
gotoNewLocation(); // TODO: upload w/o starting new project?
|
||||
else
|
||||
alert("Files uploaded.");
|
||||
} else {
|
||||
var path = "local/" + f.name;
|
||||
var reader = new FileReader();
|
||||
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) {
|
||||
if (err)
|
||||
console.log(err);
|
||||
alert("Error uploading " + path + ": " + err);
|
||||
else {
|
||||
console.log("Uploaded " + path + " " + data.length + " bytes");
|
||||
if (index == 1)
|
||||
@ -280,7 +292,7 @@ function handleFileUpload(files: File[]) {
|
||||
}
|
||||
});
|
||||
}
|
||||
reader.readAsText(f);
|
||||
reader.readAsArrayBuffer(f); // read as binary
|
||||
}
|
||||
}
|
||||
if (files) uploadNextFile();
|
||||
@ -460,6 +472,7 @@ function _downloadAllFilesZipFile(e) {
|
||||
store.keys( (err, keys : string[]) => {
|
||||
if (err) throw err;
|
||||
keys.forEach((path) => {
|
||||
// TODO: handle binary files
|
||||
store.getItem(path, (err, text) => {
|
||||
if (text) {
|
||||
zip.file(fixFilename(getFilenameForPath(path)), text);
|
||||
|
53
src/util.ts
53
src/util.ts
@ -258,11 +258,33 @@ export function lzgmini() {
|
||||
}
|
||||
|
||||
// Get the decoded string from an UTF-8 encoded array
|
||||
this.getStringUTF8 = function():string
|
||||
{
|
||||
this.getStringUTF8 = function():string {
|
||||
return byteArrayToUTF8(outdata);
|
||||
}
|
||||
}
|
||||
|
||||
export function stringToByteArray(s:string) : Uint8Array {
|
||||
var a = new Uint8Array(s.length);
|
||||
for (var i=0; i<s.length; i++)
|
||||
a[i] = s.charCodeAt(i);
|
||||
return a;
|
||||
}
|
||||
|
||||
export function byteArrayToString(outdata : number[] | Uint8Array) : string {
|
||||
var str = "";
|
||||
if (outdata != null) {
|
||||
var charLUT = new Array();
|
||||
for (var i = 0; i < 256; ++i)
|
||||
charLUT[i] = String.fromCharCode(i);
|
||||
var outlen = outdata.length;
|
||||
for (var i = 0; i < outlen; i++)
|
||||
str += charLUT[outdata[i]];
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
export function byteArrayToUTF8(outdata : number[] | Uint8Array) : string {
|
||||
var str = "";
|
||||
if (outdata != null)
|
||||
{
|
||||
var charLUT = new Array();
|
||||
for (var i = 0; i < 128; ++i)
|
||||
charLUT[i] = String.fromCharCode(i);
|
||||
@ -285,32 +307,11 @@ export function lzgmini() {
|
||||
{
|
||||
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 stringToByteArray(s:string) : Uint8Array {
|
||||
var a = new Uint8Array(s.length);
|
||||
for (var i=0; i<s.length; i++)
|
||||
a[i] = s.charCodeAt(i);
|
||||
return a;
|
||||
}
|
||||
|
||||
export function byteArrayToString(outdata : number[] | Uint8Array) : string {
|
||||
var str = "";
|
||||
if (outdata != null) {
|
||||
var charLUT = new Array();
|
||||
for (var i = 0; i < 256; ++i)
|
||||
charLUT[i] = String.fromCharCode(i);
|
||||
var outlen = outdata.length;
|
||||
for (var i = 0; i < outlen; i++)
|
||||
str += charLUT[outdata[i]];
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user