2021-07-27 18:36:49 +08:00
|
|
|
|
using Assets.Scripts;
|
2021-07-28 14:59:21 +08:00
|
|
|
|
using Assets.Scripts.Apis.Models;
|
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2021-07-27 18:36:49 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
|
|
public class DownloadWorkoutsController : PFUIPanel
|
|
|
|
|
|
{
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
|
Transform Btn, Loading,Exit,BtnStart;
|
|
|
|
|
|
ScrollRect scroll;
|
|
|
|
|
|
string path;
|
|
|
|
|
|
[SerializeField] Transform log;
|
2021-07-28 14:59:21 +08:00
|
|
|
|
AppDownloadInfo wpf;
|
|
|
|
|
|
public override void Show()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Show();
|
2021-07-29 15:45:44 +08:00
|
|
|
|
Initial();
|
2021-07-28 14:59:21 +08:00
|
|
|
|
Btn = transform.Find("Panel/BtnConfirm");
|
|
|
|
|
|
BtnStart = transform.Find("Panel/BtnStart");
|
|
|
|
|
|
Loading = transform.Find("Panel/LoadingContainer");
|
2021-07-28 21:09:34 +08:00
|
|
|
|
//scroll = transform.Find("Panel/Scroll View").GetComponent<ScrollRect>();
|
2021-07-28 14:59:21 +08:00
|
|
|
|
Exit = transform.Find("Panel/Exit");
|
|
|
|
|
|
BtnStart.GetComponent<Button>().enabled = true;
|
|
|
|
|
|
BtnStart.GetComponent<Button>().interactable = true;
|
|
|
|
|
|
SetType(1);
|
2021-07-29 15:45:44 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private async void Initial()
|
|
|
|
|
|
{
|
|
|
|
|
|
var res = await ConfigHelper.userApi.GetAppVersion();
|
|
|
|
|
|
if (res.result)
|
|
|
|
|
|
{
|
|
|
|
|
|
wpf = res.data.WPF;
|
|
|
|
|
|
}
|
|
|
|
|
|
//if (File.Exists(path) && wpf.Version == PlayerPrefs.GetString("exeVersion_w"))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// BtnStart.Find("Text").GetComponent<Text>().text = "Install";
|
|
|
|
|
|
//}
|
|
|
|
|
|
//else
|
|
|
|
|
|
//{
|
|
|
|
|
|
// BtnStart.Find("Text").GetComponent<Text>().text = "Start";
|
|
|
|
|
|
//}
|
2021-07-28 14:59:21 +08:00
|
|
|
|
}
|
2021-07-27 18:36:49 +08:00
|
|
|
|
void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
Btn = transform.Find("Panel/BtnConfirm");
|
|
|
|
|
|
BtnStart = transform.Find("Panel/BtnStart");
|
|
|
|
|
|
Loading = transform.Find("Panel/LoadingContainer");
|
2021-07-28 21:09:34 +08:00
|
|
|
|
//scroll = transform.Find("Panel/Scroll View").GetComponent<ScrollRect>();
|
2021-07-27 18:36:49 +08:00
|
|
|
|
Exit = transform.Find("Panel/Exit");
|
2021-07-28 14:59:21 +08:00
|
|
|
|
SetType(1);
|
2021-07-27 18:36:49 +08:00
|
|
|
|
UIManager.AddEvent(Exit.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
|
|
|
|
|
|
{
|
2021-07-28 14:59:21 +08:00
|
|
|
|
//UIManager.CloseConfirm();
|
2021-07-28 18:33:52 +08:00
|
|
|
|
//UIManager.ShowConfirm("1", "2", () => { });
|
|
|
|
|
|
if (request != null)
|
2021-07-28 14:59:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
request.Abort();
|
|
|
|
|
|
}
|
2021-07-28 18:33:52 +08:00
|
|
|
|
if (_fs != null)
|
2021-07-28 14:59:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
_fs.Close();
|
|
|
|
|
|
}
|
2021-07-28 18:33:52 +08:00
|
|
|
|
if (_netStream != null)
|
2021-07-28 14:59:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
_netStream.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
gameObject.SetActive(false);
|
2021-07-27 18:36:49 +08:00
|
|
|
|
});
|
2021-07-28 14:59:21 +08:00
|
|
|
|
|
2021-07-27 18:36:49 +08:00
|
|
|
|
//Button.GetComponent<Button>().enabled = false;
|
|
|
|
|
|
//Button.GetComponent<Button>().interactable = false;
|
2021-07-28 21:09:34 +08:00
|
|
|
|
path = Application.temporaryCachePath + "/PowerFunWorkouts.exe";
|
|
|
|
|
|
Debug.Log(path);
|
2021-07-27 18:36:49 +08:00
|
|
|
|
UIManager.AddEvent(Btn.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick, (b) =>
|
|
|
|
|
|
{
|
2021-07-28 14:59:21 +08:00
|
|
|
|
//Application.Quit();
|
2021-07-29 13:55:33 +08:00
|
|
|
|
OpenFile(path);
|
2021-07-27 18:36:49 +08:00
|
|
|
|
});
|
2021-07-29 15:45:44 +08:00
|
|
|
|
|
2021-07-28 14:59:21 +08:00
|
|
|
|
UIManager.AddEvent(BtnStart.gameObject, UnityEngine.EventSystems.EventTriggerType.PointerClick,async (_base) =>
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
2021-07-29 15:45:44 +08:00
|
|
|
|
if (wpf != null)
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
2021-07-29 15:45:44 +08:00
|
|
|
|
BtnStart.GetComponent<Button>().enabled = false;
|
|
|
|
|
|
BtnStart.GetComponent<Button>().interactable = false;
|
|
|
|
|
|
Debug.Log(path);
|
|
|
|
|
|
StartCoroutine(DownLoadExe(wpf.Url, path, (p, isComplete) =>
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
2021-07-29 15:45:44 +08:00
|
|
|
|
//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)
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
2021-07-29 15:45:44 +08:00
|
|
|
|
SetType(3);
|
|
|
|
|
|
PlayerPrefs.SetString("exeVersion_w", wpf.Version);
|
|
|
|
|
|
new FileInfo(path + ".pfdownload").MoveTo(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
2021-07-27 18:36:49 +08:00
|
|
|
|
}
|
2021-07-28 14:59:21 +08:00
|
|
|
|
//if (App.UpdateObject != null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// BtnStart.GetComponent<Button>().enabled = false;
|
|
|
|
|
|
// BtnStart.GetComponent<Button>().interactable = false;
|
|
|
|
|
|
// StartCoroutine(DownLoadExe(App.UpdateObject.Url, path, (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);
|
|
|
|
|
|
// if (isComplete)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// SetType(3);
|
|
|
|
|
|
|
|
|
|
|
|
// PlayerPrefs.SetString("exeVersion", App.UpdateObject.Code);
|
|
|
|
|
|
// new FileInfo(path + ".pfdownload").MoveTo(path);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }));
|
|
|
|
|
|
//}
|
2021-07-27 18:36:49 +08:00
|
|
|
|
});
|
|
|
|
|
|
//transform
|
2021-07-28 14:59:21 +08:00
|
|
|
|
//if (App.UpdateObject != null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// for (var i = 0; i < App.UpdateObject.UpdateLog.Count; i++)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var newLog = Instantiate<Transform>(log);
|
|
|
|
|
|
// newLog.GetComponent<DownloadLog>().Initial(App.UpdateObject.UpdateLog[i]);
|
|
|
|
|
|
// if (i == App.UpdateObject.UpdateLog.Count - 1)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// newLog.GetComponent<DownloadLog>().HideLine();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// newLog.SetParent(scroll.content);
|
|
|
|
|
|
// newLog.localScale = Vector3.one;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
2021-07-27 18:36:49 +08:00
|
|
|
|
}
|
2021-07-28 14:59:21 +08:00
|
|
|
|
bool stop = false;
|
|
|
|
|
|
HttpWebRequest request;
|
|
|
|
|
|
Stream _fs, _netStream;
|
2021-07-27 18:36:49 +08:00
|
|
|
|
/*下载文件*/
|
2021-07-28 21:09:34 +08:00
|
|
|
|
IEnumerator DownLoadExe(string url, string desFileName, Action<double, bool> OnDownloadProgressEvent)
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
string version = null;
|
2021-07-28 14:59:21 +08:00
|
|
|
|
if (wpf != null)
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
2021-07-28 14:59:21 +08:00
|
|
|
|
version = wpf.Version;
|
2021-07-27 18:36:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (File.Exists(desFileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
//if(PlayerPrefs.GetString("exeVersion")==)
|
|
|
|
|
|
if (version == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-07-28 14:59:21 +08:00
|
|
|
|
if (version == PlayerPrefs.GetString("exeVersion_w"))
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
2021-07-28 14:59:21 +08:00
|
|
|
|
//Application.Quit();
|
2021-07-29 13:55:33 +08:00
|
|
|
|
OpenFile(desFileName);
|
2021-07-27 18:36:49 +08:00
|
|
|
|
yield break;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(desFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//File.Delete(desFileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (File.Exists(desFileName + ".pfdownload"))
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(desFileName + ".pfdownload");
|
|
|
|
|
|
}
|
|
|
|
|
|
SetType(2);
|
2021-07-28 21:09:34 +08:00
|
|
|
|
//request = (HttpWebRequest)HttpWebRequest.Create(url);
|
|
|
|
|
|
//request.Timeout = 5000;
|
2021-07-28 14:59:21 +08:00
|
|
|
|
|
2021-07-28 21:09:34 +08:00
|
|
|
|
//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))
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
2021-07-28 21:09:34 +08:00
|
|
|
|
var operation = uwr.SendWebRequest();
|
|
|
|
|
|
|
|
|
|
|
|
while (!operation.isDone)
|
2021-07-27 18:36:49 +08:00
|
|
|
|
{
|
2021-07-28 21:09:34 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* 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);
|
2021-07-27 18:36:49 +08:00
|
|
|
|
yield return null;
|
|
|
|
|
|
}
|
2021-07-28 21:09:34 +08:00
|
|
|
|
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;
|
2021-07-27 18:36:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-07-29 13:55:33 +08:00
|
|
|
|
void OpenFile(string p)
|
|
|
|
|
|
{
|
2021-07-29 15:45:44 +08:00
|
|
|
|
Utils.ExecFile(p);
|
|
|
|
|
|
Application.Quit();
|
|
|
|
|
|
//UIManager.ShowConfirm("Switch to PowerFun Workouts?", "We will close the Powerfun and start PowerFun Workouts.", () =>
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
|
|
//});
|
2021-07-29 13:55:33 +08:00
|
|
|
|
}
|
2021-07-27 18:36:49 +08:00
|
|
|
|
void SetType(int a)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (a)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
// true 显示 button false 显示loading
|
|
|
|
|
|
Btn.gameObject.SetActive(false);
|
|
|
|
|
|
Loading.gameObject.SetActive(false);
|
|
|
|
|
|
BtnStart.gameObject.SetActive(true);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
Btn.gameObject.SetActive(false);
|
|
|
|
|
|
Loading.gameObject.SetActive(true);
|
|
|
|
|
|
BtnStart.gameObject.SetActive(false);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
Btn.gameObject.SetActive(true);
|
|
|
|
|
|
Loading.gameObject.SetActive(false);
|
|
|
|
|
|
BtnStart.gameObject.SetActive(false);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|