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);
|
||||
|
67
src/util.ts
67
src/util.ts
@ -258,39 +258,8 @@ export function lzgmini() {
|
||||
}
|
||||
|
||||
// Get the decoded string from an UTF-8 encoded array
|
||||
this.getStringUTF8 = function():string
|
||||
{
|
||||
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;
|
||||
this.getStringUTF8 = function():string {
|
||||
return byteArrayToUTF8(outdata);
|
||||
}
|
||||
}
|
||||
|
||||
@ -314,6 +283,38 @@ export function byteArrayToString(outdata : number[] | Uint8Array) : string {
|
||||
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) {
|
||||
if (s.charCodeAt(0) === 0xFEFF) {
|
||||
s = s.substr(1);
|
||||
|
Loading…
Reference in New Issue
Block a user