Skip to content

Commit

Permalink
Fix binary file reading
Browse files Browse the repository at this point in the history
  • Loading branch information
JannisX11 committed Dec 28, 2020
1 parent ed0afd6 commit 549b0cf
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions js/file_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,19 @@ Object.assign(Blockbench, {

var results = [];
var result_count = 0;
var i = 0;
var index = 0;
var errant;
if (isApp) {
while (i < files.length) {
while (index < files.length) {
(function() {
var this_i = i;
var file = files[i]
var this_i = index;
var file = files[index]
let readtype = options.readtype;
if (typeof readtype == 'function') {
readtype = readtype(file);
} else if (readtype != 'buffer' && readtype != 'binary') {
}
var binary = (readtype === 'buffer' || readtype === 'binary');
if (!readtype) {
readtype = 'text';
}

Expand Down Expand Up @@ -108,23 +110,10 @@ Object.assign(Blockbench, {
}
}
} else /*text*/ {
var load = function (data) {
if ((readtype != 'buffer' && readtype != 'binary') && data.charCodeAt(0) === 0xFEFF) {
data = data.substr(1)
}
results[this_i] = {
name: pathToName(file, true),
path: file,
content: data
}
result_count++;
if (result_count === files.length) {
cb(results)
}
}
var read_files;
var data;
console.log(file)
try {
read_files = fs.readFileSync(file, readtype == 'text' ? 'utf8' : undefined);
data = fs.readFileSync(file, readtype == 'text' ? 'utf8' : undefined);
} catch(err) {
console.log(err)
if (!errant && options.errorbox !== false) {
Expand All @@ -136,10 +125,29 @@ Object.assign(Blockbench, {
errant = true;
return;
}
load(read_files);
if (binary) {
var ab = new ArrayBuffer(data.length);
var view = new Uint8Array(ab);
for (var i = 0; i < data.length; ++i) {
view[i] = data[i];
}
data = ab;
}
if (!binary && data.charCodeAt(0) === 0xFEFF) {
data = data.substr(1)
}
results[this_i] = {
name: pathToName(file, true),
path: file,
content: data
}
result_count++;
if (result_count === files.length) {
cb(results)
}
}
})()
i++;
index++;
}
} else {
while (i < files.length) {
Expand Down

0 comments on commit 549b0cf

Please sign in to comment.