Clean up file downloading.
This commit is contained in:
@@ -102,9 +102,8 @@
|
||||
|
||||
function download()
|
||||
{
|
||||
window.external.downloadToCache("http://localhost/~paul/cnc-packages.zip","cnc-packages.zip","cnc-packages");
|
||||
document.getElementById("install-download").style.display = "none";
|
||||
document.getElementById("install-downloading").style.display = "";
|
||||
window.external.startDownload("cnc-packages");
|
||||
refreshDownloadButtons();
|
||||
}
|
||||
|
||||
function cancel()
|
||||
@@ -120,8 +119,15 @@
|
||||
}
|
||||
|
||||
function onLoad()
|
||||
{
|
||||
refreshSections();
|
||||
}
|
||||
|
||||
function refreshSections()
|
||||
{
|
||||
var installed = packagesInstalled();
|
||||
window.external.log("installed: "+installed);
|
||||
|
||||
document.getElementById("buttons-install").style.display = (installed == 0) ? "" : "none";
|
||||
document.getElementById("buttons-upgrade").style.display = (installed == 1) ? "" : "none";
|
||||
document.getElementById("buttons-play").style.display = (installed == 2) ? "" : "none";
|
||||
@@ -130,10 +136,39 @@
|
||||
// Select the correct subsection
|
||||
if (installed == 0)
|
||||
{
|
||||
var downloaded = window.external.existsInCache("cnc-packages.zip");
|
||||
window.external.log("downloaded: "+downloaded);
|
||||
document.getElementById("install-extract").style.display = downloaded ? "" : "none";
|
||||
document.getElementById("install-download").style.display = !downloaded ? "" : "none";
|
||||
window.external.registerDownload("cnc-packages", "http://localhost/~paul/cnc-packages.zip", "cnc-packages.zip");
|
||||
refreshDownloadButtons();
|
||||
}
|
||||
}
|
||||
|
||||
function refreshDownloadButtons()
|
||||
{
|
||||
document.getElementById("download-available").style.display = "none";
|
||||
document.getElementById("download-downloading").style.display = "none";
|
||||
document.getElementById("download-extract").style.display = "none";
|
||||
|
||||
// status can be NOT_REGISTERED, AVAILABLE, DOWNLOADING, COMPLETE, ERROR
|
||||
var status = window.external.downloadStatus("cnc-packages");
|
||||
window.external.log("status: "+status);
|
||||
|
||||
// Download complete, offer button to extract it
|
||||
if (status == "COMPLETE")
|
||||
{
|
||||
document.getElementById("download-extract").style.display = "";
|
||||
}
|
||||
|
||||
// Show the download progress
|
||||
else if (status == "DOWNLOADING")
|
||||
{
|
||||
document.getElementById("download-downloading").style.display = "";
|
||||
}
|
||||
|
||||
// Show the download button
|
||||
else if (status == "AVAILABLE" || status == "ERROR")
|
||||
{
|
||||
document.getElementById("download-available").style.display = "";
|
||||
|
||||
// Todo: set error message
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,29 +177,19 @@
|
||||
if (file != "cnc-packages")
|
||||
return;
|
||||
|
||||
var status = window.external.downloadStatus(file);
|
||||
if (status == "Complete")
|
||||
{
|
||||
document.getElementById("install-extract").style.display = "";
|
||||
document.getElementById("install-downloading").style.display = "none";
|
||||
}
|
||||
var total = window.external.bytesTotal(file);
|
||||
var downloaded = window.external.bytesCompleted(file);
|
||||
window.external.log(file+" = total: "+total+" downloaded: "+downloaded);
|
||||
|
||||
if (downloaded == -1)
|
||||
return;
|
||||
|
||||
var multiplier = 1/1024.0;
|
||||
var unit = "kB";
|
||||
|
||||
var max = max(total,downloaded); // total may be -1 if the server doesn't supply a size
|
||||
if (max > 1.1*1024*1024)
|
||||
{
|
||||
multiplier /= 1024;
|
||||
unit = "MB";
|
||||
}
|
||||
var multiplier = 1/1048576.0;
|
||||
total = (total*multiplier).toPrecision(2);
|
||||
downloaded = (downloaded*multiplier).toPrecision(2);
|
||||
|
||||
document.getElementById("download-status").value = downloaded+"/"+total+" "+unit;
|
||||
window.external.log("file: "+file+" "+downloaded+"/"+total+" ("+percent+"%)");
|
||||
document.getElementById("download-status").value = downloaded+"/"+total+" MB";
|
||||
refreshDownloadButtons();
|
||||
}
|
||||
|
||||
</script>
|
||||
@@ -186,19 +211,19 @@
|
||||
</ul>
|
||||
</div>
|
||||
</div -->
|
||||
<div id="buttons-install" class="buttons">
|
||||
<div id="buttons-install" class="buttons" style="display:none">
|
||||
<div class="desc">The original game data is required before you can play this mod.<br />
|
||||
Installing from web will install the minimal files required to play.
|
||||
</div>
|
||||
<div id="install-download">
|
||||
<div id="download-available" style="display:none">
|
||||
<input type="button" class="button" onclick="download();" value="Download" />
|
||||
</div>
|
||||
<div id="install-downloading" style="display:none">
|
||||
<div id="download-downloading" style="display:none">
|
||||
<input type="button" class="button" id="download-status" value="Initializing..." />
|
||||
<input type="button" class="button" onclick="cancel();" value="Cancel" />
|
||||
</div>
|
||||
<div id="install-extract">
|
||||
<input type="button" class="button" onclick="install();" value="Install" />
|
||||
<div id="download-extract" style="display:none">
|
||||
<input type="button" class="button" onclick="extract();" value="Install" />
|
||||
</div>
|
||||
</div>
|
||||
<div id="buttons-upgrade" class="buttons" style="display:none">
|
||||
|
||||
Reference in New Issue
Block a user