Commit c58aa662 authored by David Gonçalves's avatar David Gonçalves 📖

asynchronous functions in order to fix loading animation

parent b16dda3f
......@@ -98,7 +98,8 @@ body::after {
* and our modal element will be visible
*/
.loading {
body.loading .loader,
body.loading .loader_bg {
overflow: hidden;
display: block;
}
......@@ -107,7 +108,7 @@ body::after {
* https://projects.lukehaas.me/css-loaders/
*/
.loader_bg {
display: block;
display: none;
position: fixed;
z-index: 999;
top: 0;
......@@ -117,7 +118,7 @@ body::after {
background-color: rgba(0,0,0,0.8);
}
.loader {
display: block;
display: none;
z-index: 1000;
color: #ffffff;
font-size: 10px;
......
......@@ -197,11 +197,10 @@
</div>
<!-- Place at bottom of page -->
<div class="loader_bg" id="loader_bg">
<div class="loader" id="loader"></div>
<div class="loader_bg">
<div class="loader"></div>
<p id="loading_text">Loading. Please wait...</p>
</div>
<input type="button" id="showloading" style="display:none;" />
</body>
......
......@@ -5,7 +5,7 @@
const path = require('path');
// Execute external program
const { spawnSync } = require('child_process');
const { spawn } = require('child_process');
const translate = require('google-translate-api');
......@@ -106,9 +106,6 @@ function generateExe() {
// TODO: Story XML validation with XLS
require("electron").remote.dialog.showSaveDialog({
title: window.lang.prompt_text,
defaultPath: exe_filename
......@@ -118,13 +115,7 @@ function generateExe() {
return;
}
//$('body').addClass("loading");
document.getElementById('showloading').click();
//$('#loader_bg').show();
//$('#loader').show();
//$('#generate_exe').val(window.lang.loading_text);
$('body').addClass("loading");
exe_filename = result;
......@@ -134,39 +125,38 @@ function generateExe() {
xml_storylist_filename = path.join(dir, xml_storylist_filename);
swf_filename = path.join(dir, swf_filename);
// MangaD note: Functions should be asynchronous because otherwise the loading animation will block
// Write Story XML file
try {
fs.writeFileSync(xml_story_filename, xmlData);
} catch(err) {
$('#loader_bg').hide();
$('#loader').hide();
console.log(err);
dialog.err(window.lang.writexml_error, window.lang.error);
return;
}
fs.writeFile(xml_story_filename, xmlData, function(err) {
if (err) {
$('body').removeClass("loading");
console.log(err);
dialog.err(window.lang.writexml_error, window.lang.error);
return;
}
// Write StoryList XML file
try {
fs.writeFileSync(xml_storylist_filename, storyListXML);
} catch(err) {
$('#loader_bg').hide();
$('#loader').hide();
fs.unlink(xml_story_filename, function(error) {
if (error) {
// Write StoryList XML file
fs.writeFile(xml_storylist_filename, storyListXML, function(err) {
if (err) {
fs.unlink(xml_story_filename, function(error) {
if (error) {
console.log(err);
dialog.err(window.lang.writexml_error, window.lang.error);
}
});
$('body').removeClass("loading");
console.log(err);
dialog.err(window.lang.writexml_error, window.lang.error);
return;
}
});
console.log(err);
dialog.err(window.lang.save_error, window.lang.error);
return;
}
// Create EXE file
compileExe();
// Create EXE file
compileExe();
});
});
//$('#generate_exe').val(window.lang.generate_exe);
});
}
......@@ -183,72 +173,82 @@ function compileExe() {
]
let storylist_id = "234";
// MangaD note: Functions should be asynchronous because otherwise the loading animation will block
// Replace Story XML
let ffdec;
try {
ffdec = spawnSync('java', ['-jar', `${__dirname}/ffdec/ffdec.jar`, '-replace',
`${__dirname}/game/hf.swf`, swf_filename, story_ids[story_number], xml_story_filename]);
} catch(err) {
console.log(err);
dialog.err(window.lang.replace_xml, window.lang.error);
return;
}
let ffdec = spawn('java', ['-jar', `${__dirname}/ffdec/ffdec.jar`, '-replace',
`${__dirname}/game/hf.swf`, swf_filename, story_ids[story_number], xml_story_filename]);
console.log(`stdout: ${ffdec.stdout}`);
console.log(`stderr: ${ffdec.stderr}`);
console.log(`exit: Child process exited with code ${ffdec.status}.`);
ffdec.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
// Replace Story List XML
try {
ffdec = spawnSync('java', ['-jar', `${__dirname}/ffdec/ffdec.jar`, '-replace',
ffdec.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ffdec.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
if (code != 0) {
dialog.err(window.lang.replace_xml, window.lang.error);
return;
}
// Replace Story List XML
let ffdec2 = spawn('java', ['-jar', `${__dirname}/ffdec/ffdec.jar`, '-replace',
swf_filename, swf_filename, storylist_id, xml_storylist_filename]);
} catch(err) {
console.log(err);
dialog.err(window.lang.replace_xml, window.lang.error);
return;
}
console.log(`stdout: ${ffdec.stdout}`);
console.log(`stderr: ${ffdec.stderr}`);
console.log(`exit: Child process exited with code ${ffdec.status}.`);
ffdec2.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
// Compress SWF
try {
ffdec = spawnSync('java', ['-jar', `${__dirname}/ffdec/ffdec.jar`,
'-compress', 'zlib', swf_filename, swf_filename + "_compressed"]);
} catch(err) {
console.log(err);
dialog.err(window.lang.compress_swf_error, window.lang.error);
return;
}
ffdec2.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
console.log(`stdout: ${ffdec.stdout}`);
console.log(`stderr: ${ffdec.stderr}`);
console.log(`exit: Child process exited with code ${ffdec.status}.`);
ffdec2.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
if (code != 0) {
dialog.err(window.lang.replace_xml, window.lang.error);
return;
}
try {
fs.unlinkSync(swf_filename);
} catch(err) {
console.log(err);
dialog.err(window.lang.del_swf_error, window.lang.error);
return;
}
// Compress SWF
let ffdec3 = spawn('java', ['-jar', `${__dirname}/ffdec/ffdec.jar`,
'-compress', 'zlib', swf_filename, swf_filename + "_compressed"]);
swf_filename = swf_filename + "_compressed";
ffdec3.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
swf2exe(swf_filename, exe_filename);
ffdec3.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ffdec3.on('exit', (code) => {
console.log(`child process exited with code ${code}`);
if (code != 0) {
dialog.err(window.lang.replace_xml, window.lang.error);
return;
}
try {
fs.unlinkSync(swf_filename);
} catch(err) {
$('body').removeClass("loading");
console.log(err);
dialog.err(window.lang.del_swf_error, window.lang.error);
return;
}
swf_filename = swf_filename + "_compressed";
swf2exe(swf_filename, exe_filename);
});
});
});
// Delete XML and SWF files
try {
fs.unlinkSync(xml_story_filename);
fs.unlinkSync(xml_storylist_filename);
fs.unlinkSync(swf_filename);
} catch(err) {
console.log(err);
dialog.err(window.lang.unlink_error, window.lang.error);
return;
}
dialog.info(window.lang.exe_success, window.lang.success);
}
......@@ -259,34 +259,48 @@ function swf2exe(swfFile, outputFile) {
let projectorData = fs.readFileSync(`${__dirname}/swf2exe/SA.exe`);
let swfData = fs.readFileSync(swfFile);
// Tommy was having an error "EBUSY resource busy or locked" when using the
// sync version of these functions
fs.writeFile(outputFile, projectorData, function (err) {
if (err) {
return console.log(err);
$('body').removeClass("loading");
dialog.err(window.lang.swf2exe_error, window.lang.error);
return console.log(err);
}
fs.appendFile(outputFile, swfData, function (err) {
if (err) {
return console.log(err);
$('body').removeClass("loading");
dialog.err(window.lang.swf2exe_error, window.lang.error);
return console.log(err);
}
fs.appendFile(outputFile, Buffer.from( new Uint8Array(footer) ), function (err) {
if (err) {
return console.log(err);
$('body').removeClass("loading");
dialog.err(window.lang.swf2exe_error, window.lang.error);
return console.log(err);
}
fs.appendFile(outputFile, Buffer.from( new Uint8Array(intToByteArray(swfData.length))), function (err) {
if (err) {
return console.log(err);
$('body').removeClass("loading");
dialog.err(window.lang.swf2exe_error, window.lang.error);
return console.log(err);
}
// Delete XML and SWF files
try {
fs.unlinkSync(xml_story_filename);
fs.unlinkSync(xml_storylist_filename);
fs.unlinkSync(swf_filename);
} catch(err) {
$('body').removeClass("loading");
console.log(err);
dialog.err(window.lang.unlink_error, window.lang.error);
return;
}
dialog.info(window.lang.exe_success, window.lang.success);
$('#loader_bg').hide();
$('#loader').hide();
console.log('OK');
$('body').removeClass("loading");
});
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment