下载速度,修改文案

This commit is contained in:
Caiyanpeng committed 2021-07-28 21:09:48 +08:00
1 parent abf913b467
commit 5c96b10385
12 files changed
+203 -380

No files matched your search

@@ -29,7 +29,7 @@ public class DownloadController : PFUIPanel
SetType(1);
//Button.GetComponent<Button>().enabled = false;
//Button.GetComponent<Button>().interactable = false;
path = Application.persistentDataPath + "/PowerFun.exe";
path = Application.temporaryCachePath + "/PowerFun.exe";
UIManager.AddEvent(Btn.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
{
Application.Quit();
@@ -41,10 +41,10 @@ public class DownloadController : PFUIPanel
{
BtnStart.GetComponent<Button>().enabled = false;
BtnStart.GetComponent<Button>().interactable = false;
StartCoroutine(DownLoadExe(App.UpdateObject.Url, path, (a, b, isComplete) =>
StartCoroutine(DownLoadExe(App.UpdateObject.Url, path, (p, isComplete) =>
{
transform.Find("Panel/LoadingContainer/Text").GetComponent<Text>().text = $"Downloaded {a.ToString("#0")}M / {b.ToString("#0")}M";// string.Format(, a, b);
transform.Find("Panel/LoadingContainer/Loading").GetComponent<Image>().fillAmount = (float)(a / b);
transform.Find("Panel/LoadingContainer/Text").GetComponent<Text>().text = $"Downloaded {Math.Round(p * 100, 0)}%";// string.Format(, a, b);
transform.Find("Panel/LoadingContainer/Loading").GetComponent<Image>().fillAmount = (float)p;
if (isComplete)
{
SetType(3);
@@ -73,7 +73,7 @@ public class DownloadController : PFUIPanel
}
}
/*下载文件*/
IEnumerator DownLoadExe(string url, string desFileName, Action<double, double, bool> OnDownloadProgressEvent)
IEnumerator DownLoadExe(string url, string desFileName, Action<double, bool> OnDownloadProgressEvent)
{
string version = null;
if (App.UpdateObject != null)
@@ -108,33 +108,75 @@ public class DownloadController : PFUIPanel
File.Delete(desFileName + ".pfdownload");
}
SetType(2);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = 5000;
WebResponse response = request.GetResponse();
//UnityWebRequest request = UnityWebRequest.Get(url);
//yield return request.SendWebRequest();
int packLength = 1024 * 20;
int nReadSize = 0;
byte[] nbytes = new byte[packLength];
double dDownloadedLength = 0, dTotalLength = 0;
long countLength = response.ContentLength;
using (FileStream fs = new FileStream(desFileName + ".pfdownload", FileMode.Create))
using (Stream netStream = response.GetResponseStream())
//HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
//request.Timeout = 5000;
//WebResponse response = request.GetResponse();
////UnityWebRequest request = UnityWebRequest.Get(url);
////yield return request.SendWebRequest();
//int packLength = 1024 * 20;
//int nReadSize = 0;
//byte[] nbytes = new byte[packLength];
//double dDownloadedLength = 0, dTotalLength = 0;
//long countLength = response.ContentLength;
//using (FileStream fs = new FileStream(desFileName + ".pfdownload", FileMode.Create))
//using (Stream netStream = response.GetResponseStream())
//{
// nReadSize = netStream.Read(nbytes, 0, packLength);
// while (nReadSize > 0)
// {
// fs.Write(nbytes, 0, nReadSize);
// nReadSize = netStream.Read(nbytes, 0, packLength);
// dDownloadedLength = fs.Length * 1.0 / (1024 * 1024);
// dTotalLength = countLength * 1.0 / (1024 * 1024);
// OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, false);
// //string ss = string.Format("已下载 {0:F}M / {1:F}M", dDownloadedLength, dTotalLength);
// //Debug.Log(ss);
// yield return null;
// }
//}
//OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, true);
using (var uwr = UnityWebRequest.Get(url))
{
nReadSize = netStream.Read(nbytes, 0, packLength);
while (nReadSize > 0)
var operation = uwr.SendWebRequest();
while (!operation.isDone)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = netStream.Read(nbytes, 0, packLength);
dDownloadedLength = fs.Length * 1.0 / (1024 * 1024);
dTotalLength = countLength * 1.0 / (1024 * 1024);
OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, false);
//string ss = string.Format("已下载 {0:F}M / {1:F}M", dDownloadedLength, dTotalLength);
//Debug.Log(ss);
/*
* as BugFinder metnioned in the comments
* what you want to track is uwr.downloadProgress
*/
var downloadDataProgress = uwr.downloadProgress * 100;
/*
* use a float division here
* I don't know what type downloadDataProgress is
* but if it is an int than you will always get
* an int division <somethingSmallerThan100>/100 = 0
*/
//progressBar.fillAmount = downloadDataProgress / 100.0f;
OnDownloadProgressEvent.Invoke(uwr.downloadProgress, false);
Debug.Log("Download: " + downloadDataProgress);
yield return null;
}
ByteArrayToFile(path + ".pfdownload", uwr.downloadHandler.data);
OnDownloadProgressEvent.Invoke(1, true);
}
}
public bool ByteArrayToFile(string fileName, byte[] byteArray)
{
try
{
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(byteArray, 0, byteArray.Length);
return true;
}
}
catch (Exception ex)
{
Debug.LogError(ex);
return false;
}
OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, true);
}
void SetType(int a)
{
@@ -24,7 +24,7 @@ public class DownloadWorkoutsController : PFUIPanel
Btn = transform.Find("Panel/BtnConfirm");
BtnStart = transform.Find("Panel/BtnStart");
Loading = transform.Find("Panel/LoadingContainer");
scroll = transform.Find("Panel/Scroll View").GetComponent<ScrollRect>();
//scroll = transform.Find("Panel/Scroll View").GetComponent<ScrollRect>();
Exit = transform.Find("Panel/Exit");
BtnStart.GetComponent<Button>().enabled = true;
BtnStart.GetComponent<Button>().interactable = true;
@@ -35,7 +35,7 @@ public class DownloadWorkoutsController : PFUIPanel
Btn = transform.Find("Panel/BtnConfirm");
BtnStart = transform.Find("Panel/BtnStart");
Loading = transform.Find("Panel/LoadingContainer");
scroll = transform.Find("Panel/Scroll View").GetComponent<ScrollRect>();
//scroll = transform.Find("Panel/Scroll View").GetComponent<ScrollRect>();
Exit = transform.Find("Panel/Exit");
SetType(1);
UIManager.AddEvent(Exit.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
@@ -59,14 +59,15 @@ public class DownloadWorkoutsController : PFUIPanel
//Button.GetComponent<Button>().enabled = false;
//Button.GetComponent<Button>().interactable = false;
path = Application.persistentDataPath + "/PowerFunWorkouts.exe";
path = Application.temporaryCachePath + "/PowerFunWorkouts.exe";
Debug.Log(path);
UIManager.AddEvent(Btn.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
{
//Application.Quit();
UIManager.ShowConfirm("Confirm", @"Switch to PowerFun Workouts? We will close the Powerfun and start PowerFun Workouts", () =>
UIManager.ShowConfirm("Confirm", "Switch to PowerFun Workouts?\nWe will close the Powerfun and start PowerFun Workouts.", () =>
{
Utils.ExecFile(path);
UIManager.CloseConfirm();
Application.Quit();
});
});
UIManager.AddEvent(BtnStart.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick,async (_base) =>
@@ -80,11 +81,11 @@ public class DownloadWorkoutsController : PFUIPanel
BtnStart.GetComponent<Button>().enabled = false;
BtnStart.GetComponent<Button>().interactable = false;
Debug.Log(path);
StartCoroutine(DownLoadExe(wpf.Url, path, (a, b, isComplete) =>
StartCoroutine(DownLoadExe(wpf.Url, path, (p, isComplete) =>
{
Debug.Log($"{a} {b} {isComplete}");
transform.Find("Panel/LoadingContainer/Text").GetComponent<Text>().text = $"Downloaded {a.ToString("#0")}M / {b.ToString("#0")}M";// string.Format(, a, b);
transform.Find("Panel/LoadingContainer/Loading").GetComponent<Image>().fillAmount = (float)(a / b);
//Debug.Log($"{a} {b} {isComplete}");
transform.Find("Panel/LoadingContainer/Text").GetComponent<Text>().text = $"Downloaded {Math.Round(p * 100,0)}%";// string.Format(, a, b);
transform.Find("Panel/LoadingContainer/Loading").GetComponent<Image>().fillAmount = (float)p;
if (isComplete)
{
SetType(3);
@@ -132,7 +133,7 @@ public class DownloadWorkoutsController : PFUIPanel
HttpWebRequest request;
Stream _fs, _netStream;
/*下载文件*/
IEnumerator DownLoadExe(string url, string desFileName, Action<double, double, bool> OnDownloadProgressEvent)
IEnumerator DownLoadExe(string url, string desFileName, Action<double, bool> OnDownloadProgressEvent)
{
string version = null;
if (wpf != null)
@@ -151,7 +152,12 @@ public class DownloadWorkoutsController : PFUIPanel
if (version == PlayerPrefs.GetString("exeVersion_w"))
{
//Application.Quit();
Utils.ExecFile(desFileName);
UIManager.ShowConfirm("Confirm", "Switch to PowerFun Workouts?\nWe will close the Powerfun and start PowerFun Workouts.", () =>
{
Utils.ExecFile(desFileName);
Application.Quit();
});
yield break;
}
else
@@ -167,36 +173,81 @@ public class DownloadWorkoutsController : PFUIPanel
File.Delete(desFileName + ".pfdownload");
}
SetType(2);
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = 5000;
//request = (HttpWebRequest)HttpWebRequest.Create(url);
//request.Timeout = 5000;
WebResponse response = request.GetResponse();
//UnityWebRequest request = UnityWebRequest.Get(url);
//yield return request.SendWebRequest();
int packLength = 1024 * 20;
int nReadSize = 0;
byte[] nbytes = new byte[packLength];
double dDownloadedLength = 0, dTotalLength = 0;
long countLength = response.ContentLength;
using (FileStream fs = new FileStream(desFileName + ".pfdownload", FileMode.Create))
using (Stream netStream = response.GetResponseStream())
//WebResponse response = request.GetResponse();
////UnityWebRequest request = UnityWebRequest.Get(url);
////yield return request.SendWebRequest();
//int packLength = 1024 * 20;
//int nReadSize = 0;
//byte[] nbytes = new byte[packLength];
//double dDownloadedLength = 0, dTotalLength = 0;
//long countLength = response.ContentLength;
//using (FileStream fs = new FileStream(desFileName + ".pfdownload", FileMode.Create))
//using (Stream netStream = response.GetResponseStream())
//{
// _fs = fs;
// _netStream = netStream;
// nReadSize = netStream.Read(nbytes, 0, packLength);
// while (nReadSize > 0)
// {
// fs.Write(nbytes, 0, nReadSize);
// nReadSize = netStream.Read(nbytes, 0, packLength);
// dDownloadedLength = fs.Length * 1.0 / (1024 * 1024);
// dTotalLength = countLength * 1.0 / (1024 * 1024);
// OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, false);
// //string ss = string.Format("已下载 {0:F}M / {1:F}M", dDownloadedLength, dTotalLength);
// //Debug.Log(ss);
// yield return null;
// }
//}
//OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, true);
using (var uwr = UnityWebRequest.Get(url))
{
_fs = fs;
_netStream = netStream;
nReadSize = netStream.Read(nbytes, 0, packLength);
while (nReadSize > 0)
var operation = uwr.SendWebRequest();
while (!operation.isDone)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = netStream.Read(nbytes, 0, packLength);
dDownloadedLength = fs.Length * 1.0 / (1024 * 1024);
dTotalLength = countLength * 1.0 / (1024 * 1024);
OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, false);
//string ss = string.Format("已下载 {0:F}M / {1:F}M", dDownloadedLength, dTotalLength);
//Debug.Log(ss);
/*
* as BugFinder metnioned in the comments
* what you want to track is uwr.downloadProgress
*/
var downloadDataProgress = uwr.downloadProgress * 100;
/*
* use a float division here
* I don't know what type downloadDataProgress is
* but if it is an int than you will always get
* an int division <somethingSmallerThan100>/100 = 0
*/
//progressBar.fillAmount = downloadDataProgress / 100.0f;
OnDownloadProgressEvent.Invoke(uwr.downloadProgress,false);
Debug.Log("Download: " + downloadDataProgress);
yield return null;
}
ByteArrayToFile(path+".pfdownload", uwr.downloadHandler.data);
OnDownloadProgressEvent.Invoke(1, true);
}
}
public bool ByteArrayToFile(string fileName, byte[] byteArray)
{
try
{
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(byteArray, 0, byteArray.Length);
return true;
}
}
catch (Exception ex)
{
Debug.LogError(ex);
return false;
}
OnDownloadProgressEvent.Invoke(dDownloadedLength, dTotalLength, true);
}
void SetType(int a)
{