Commit 9eb840f3 authored by Tommy Chan's avatar Tommy Chan

refactor

parent 9af1d9f8
......@@ -14,7 +14,10 @@
<link rel="stylesheet" type="text/css" href="css/select.css">
<link rel="stylesheet" type="text/css" href="css/input.css">
<script type="text/javascript" src="js/FileSaver.js" ></script>
<!-- <script type="text/javascript" src="js/FileSaver.js" ></script> -->
<script type="text/javascript" src="js/handleUpload.js"></script>
<script type="text/javascript" src="js/file.js"></script>
<script type="text/javascript" src="js/setting.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
......
function saveFile() {
if (getDataFromEditor()) {
let fileName = "new.xml";
if (file && file.name) {
fileName = file.name;
}
fileName = prompt("Please enter the file name", fileName); // FIXME Doesn't work in nodejs
if (!fileName) return;
let blob = new Blob([data], {
type: 'text/xml'
});
fs.writeFile(fileName, blob, function(err) {
if (err) {
return alert(err); // TODO Decent alert
}
else {
alert("The file was saved!"); // TODO Decent alert
}
});
}
}
function openFile() {
file_tmp = file;
file = document.getElementById("fileInput").files[0];
let fileName = escape(file.name);
let fileType = (file.type ? file.type : 'n/a');
let fileSize = file.size;
let lastModified = file.lastModifiedDate ? file.lastModifiedDate.toLocaleDateString() : 'n/a';
if (file.name.substring(file.name.lastIndexOf(".")) != ".xml") {
file = file_tmp;
document.getElementById("fileInput").value = "";
alert('XML file only.');
return false;
} else {
fileType = "text/xml";
}
document.getElementById('fileDetails').innerHTML =
'<strong>' + fileName + '</strong> (' + fileType + ') - ' +
fileSize + ' bytes, last modified: ' + lastModified;
return true;
}
function getDataFromFile() {
let reader = new FileReader();
reader.onload = function() {
data = reader.result;
let editor = ace.edit("XMLCode");
editor.setValue(data, -1);
editor.getSession().setUndoManager(new ace.UndoManager());
};
reader.readAsText(file);
}
function create_mod(filename) {
let story_ids = {
0: "234",
1: "219",
2: "398",
3: "226",
4: "299",
5: "305",
6: "454",
7: "170"
}
console.log(filename);
let target_dir = 'GenerateEXE/';
let target_file = `${target_dir}${filename}.xml`;
let target_file_story_list = `${target_dir}${filename}storylist.xml`;
let story_list = {
1:"First Match",
2:"Trap",
3:"Defeated",
4:"Break in",
5:"Prison break",
6:"Waken",
7:"Promises",
8:"Training",
9:"Betrayal",
10:"Death Match"
}
let story_number = document.getElementById("storynum").value;
let story_name = document.getElementById("storyname").value;
story_list[story_number] = story_name;
let storynumber = story_ids[story_number];
let storylistdata=`<storylist>
<story><storyid>story01</storyid><bgid>story01</bgid><storyname><en>1. ${story_list[1]}</en><b5>第一話 初戰</b5></storyname></story>
<story><storyid>story02</storyid><bgid>story02b</bgid><storyname><en>2. ${story_list[2]}</en><b5>第二話 陷阱</b5></storyname></story>
<story><storyid>story03</storyid><bgid>story03a</bgid><storyname><en>3. ${story_list[3]}</en><b5>第三話 潰敗</b5></storyname></story>
<story><storyid>story04</storyid><bgid>story04a</bgid><storyname><en>4. ${story_list[4]}</en><b5>第四話 潛入</b5></storyname></story>
<story><storyid>story05</storyid><bgid>prison2</bgid><storyname><en>5. ${story_list[5]}</en><b5>第五話 逃獄</b5></storyname></story>
<story><storyid>story06</storyid><bgid>story06c</bgid><storyname><en>6. ${story_list[6]}</en><b5>第六話 醒覺</b5></storyname></story>
<story><storyid>story07</storyid><bgid>desert</bgid><storyname><en>7. ${story_list[7]}</en><b5>第七話 約定</b5></storyname></story>
<story><storyid>story08</storyid><bgid>hill_grass</bgid><storyname><en>8. ${story_list[8]}</en><b5>第八話 特訓</b5></storyname></story>
<story><storyid>story09</storyid><bgid>forest_grass</bgid><storyname><en>9. ${story_list[9]}</en><b5>第九話 背叛</b5></storyname></story>
<story><storyid>story10</storyid><bgid>room1</bgid><storyname><en>10. ${story_list[10]}</en><b5>第十話 死戰</b5></storyname></story>
</storylist>
`;
let ffdec = spawn('java', ['-jar', `${__dirname}/ffdec/ffdec.jar`, '-replace', `${__dirname}/game/hf.swf`, `${__dirname}/${target_dir}${filename}.swf`, `${storynumber}`, `${__dirname}/${target_dir}${filename}.xml`, `${story_ids[0]}`, `${target_file_story_list}`]);
ffdec.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ffdec.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ffdec.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
// compression is missing -> repalce.sh 25
}
function getDataFromEditor() {
data = ace.edit("XMLCode").getValue();
if (!data) {
alert('Text area is empty.');
return false;
}
return true;
}
function uploadCode() {
// let http = new XMLHttpRequest();
// let url = "upload.php";
// let params = "code=" + data;
// http.open("POST", url, true);
// //Send the proper header information along with the request
// http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// http.onreadystatechange = function() { //Call a function when the state changes.
// if (http.readyState == 4 && http.status == 200) {
// alert(http.responseText);
// }
// }
// http.send(params);
// Generate Date String
let date = new Date();
let year = date.getFullYear().toString();
let month = date.getDate().toString();
let day = date.getDay().toString();
let hour = date.getHours().toString();
let minute = date.getMinutes().toString();
let second = date.getSeconds().toString();
let millis = date.getMilliseconds().toString();
let d = `${year}-${month}-${day}-${hour}-${minute}-${second}-${millis}`;
let target_dir = 'GenerateEXE/';
let target_file = `${target_dir}${d}.xml`;
let target_file_story_list = `${target_dir}${d}storylist.xml`;
let target_filename = d;
let target_filename_story_list = `${d}storylist.xml`;
let uploadOk = true;
let story_name = document.getElementById("storyname").value;
let story_list = {
1:"First Match",
2:"Trap",
3:"Defeated",
4:"Break in",
5:"Prison break",
6:"Waken",
7:"Promises",
8:"Training",
9:"Betrayal",
10:"Death Match"
};
let story_number = document.getElementById("storynum").value;
story_list[story_number] = story_name;
let storylistdata=`<storylist>
<story><storyid>story01</storyid><bgid>story01</bgid><storyname><en>1. ${story_list[1]}</en><b5>第一話 初戰</b5></storyname></story>
<story><storyid>story02</storyid><bgid>story02b</bgid><storyname><en>2. ${story_list[2]}</en><b5>第二話 陷阱</b5></storyname></story>
<story><storyid>story03</storyid><bgid>story03a</bgid><storyname><en>3. ${story_list[3]}</en><b5>第三話 潰敗</b5></storyname></story>
<story><storyid>story04</storyid><bgid>story04a</bgid><storyname><en>4. ${story_list[4]}</en><b5>第四話 潛入</b5></storyname></story>
<story><storyid>story05</storyid><bgid>prison2</bgid><storyname><en>5. ${story_list[5]}</en><b5>第五話 逃獄</b5></storyname></story>
<story><storyid>story06</storyid><bgid>story06c</bgid><storyname><en>6. ${story_list[6]}</en><b5>第六話 醒覺</b5></storyname></story>
<story><storyid>story07</storyid><bgid>desert</bgid><storyname><en>7. ${story_list[7]}</en><b5>第七話 約定</b5></storyname></story>
<story><storyid>story08</storyid><bgid>hill_grass</bgid><storyname><en>8. ${story_list[8]}</en><b5>第八話 特訓</b5></storyname></story>
<story><storyid>story09</storyid><bgid>forest_grass</bgid><storyname><en>9. ${story_list[9]}</en><b5>第九話 背叛</b5></storyname></story>
<story><storyid>story10</storyid><bgid>room1</bgid><storyname><en>10. ${story_list[10]}</en><b5>第十話 死戰</b5></storyname></story>
</storylist>
`;
if (story_number>7 || story_number<1) {
console.log("Sorry, you can only replace story 1-7."); //TODO User message
uploadOk = false;
} else {
story_number=parseInt(story_number);
}
if (story_name == "" || story_name == null) {
console.log("Please enter a name for your story."); //TODO User message
uploadOk = false;
}
if (data === undefined || data === null || data === '' || data.length == 0) {
console.log("Story cannot be empty."); //TODO User message
uploadOk = false;
}
// TODO: XML validation
fs.exists(target_file, exists => {
if (exists) {
console.log("Sorry, the story file already exists."); // User message
uploadOk = false;
}
});
if (data.length > 512 * 1000) {
console.log("Sorry, your story is too large."); // User message
uploadOk = false;
}
if (uploadOk) {
fs.open(target_file, 'w', (err, file) => {
if (err) {
console.log("Sorry, there was an error uplaoding your story."); // User message
throw err; // User message
}
});
fs.writeFile(target_file, data, err => {
if (err) {
console.log("Sorry, there was an error saving your story."); // User message
throw err; // User message
}
console.log("The story has been uploaded."); // User message
create_mod(target_filename);
});
//upload storylist
fs.open(target_file_story_list, 'w', (err, file) => {
if (err) {
console.log("Sorry, there was an error uplaoding your story."); // User message
throw err; // User message
}
});
fs.writeFile(target_file_story_list, storylistdata, err => {
if (err) {
console.log("Sorry, there was an error saving your story."); // User message
throw err; // User message
}
console.log("The story has been uploaded."); // User message
create_mod(target_filename);
})
} else {
console.log("Sorry, your story was not uploaded."); // User message
}
}
This diff is collapsed.
function changeTheme(t) {
let sel, theme;
if (t) {
theme = t;
} else {
sel = document.getElementById("theme_selector");
theme = sel.options[sel.selectedIndex].value;
}
if (theme) {
let fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", "ace/theme-" + theme + ".js");
if (typeof fileref != "undefined") {
document.getElementsByTagName("head")[0].appendChild(fileref);
}
let editor = ace.edit("XMLCode"); //Get editor instance
editor.setTheme("ace/theme/" + theme); //Set theme
}
}
function changeFontSize(f) {
let sel, size;
if (f) {
size = f;
} else {
sel = document.getElementById("fontsize_selector");
size = sel.options[sel.selectedIndex].value;
}
if (size) {
document.getElementById("XMLCode").style.fontSize = size;
}
}
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