Files
XCharts/Runtime/Utility/XChartsMgr.cs

297 lines
9.2 KiB
C#
Raw Normal View History

2020-05-10 00:20:40 +08:00
/******************************************/
/* */
/* Copyright (c) 2018 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/******************************************/
2019-10-23 18:59:34 +08:00
using System.Text;
using System.Collections;
2020-05-10 00:20:40 +08:00
using System.Collections.Generic;
2019-10-23 18:59:34 +08:00
using System.Text.RegularExpressions;
2020-05-10 00:20:40 +08:00
using UnityEngine;
2020-04-09 08:02:46 +08:00
using UnityEngine.Networking;
2020-05-10 00:20:40 +08:00
using UnityEngine.SceneManagement;
2019-10-23 18:59:34 +08:00
namespace XCharts
{
class XChartsVersion
{
public string version = "";
public int date = 0;
public int checkdate = 0;
public string desc = "";
public string homepage = "";
}
2020-05-10 00:20:40 +08:00
[ExecuteInEditMode]
2019-10-23 18:59:34 +08:00
public class XChartsMgr : MonoBehaviour
{
2020-05-24 08:06:40 +08:00
public const string version = "1.5.0";
public const int date = 20200522;
2019-10-23 18:59:34 +08:00
[SerializeField] private string m_NowVersion;
[SerializeField] private string m_NewVersion;
[SerializeField] private List<BaseChart> m_ChartList = new List<BaseChart>();
2019-10-23 18:59:34 +08:00
private static XChartsMgr m_XCharts;
public static XChartsMgr Instance
{
get
{
if (m_XCharts == null)
{
var go = GameObject.Find("_xcharts_");
if (go == null)
{
go = new GameObject();
go.name = "_xcharts_";
if (Application.isPlaying)
{
DontDestroyOnLoad(go);
}
2019-10-23 18:59:34 +08:00
m_XCharts = go.AddComponent<XChartsMgr>();
}
else
{
m_XCharts = go.GetComponent<XChartsMgr>();
if (m_XCharts == null)
{
m_XCharts = go.AddComponent<XChartsMgr>();
}
}
2020-05-07 12:37:28 +08:00
m_XCharts.m_NowVersion = version + "_" + date;
2019-10-23 18:59:34 +08:00
}
return m_XCharts;
}
}
private XChartsMgr() { }
2019-11-04 13:09:06 +08:00
private void Awake()
{
SerieLabelPool.ClearAll();
m_ChartList.Clear();
2019-11-04 13:09:06 +08:00
}
2019-10-23 18:59:34 +08:00
public string changeLog { get; private set; }
public string newVersion { get { return m_NewVersion; } }
public string nowVersion { get { return m_NowVersion; } }
public string desc { get; private set; }
public string homepage { get; private set; }
public int newDate { get; private set; }
public int newCheckDate { get; private set; }
public bool isCheck { get; private set; }
2020-04-09 08:02:46 +08:00
public bool isNetworkError { get; private set; }
public string networkError { get; private set; }
2019-10-23 18:59:34 +08:00
public bool needUpdate
{
get
{
2020-05-24 08:06:40 +08:00
return !isNetworkError && !m_NowVersion.Equals(m_NewVersion);
2019-10-23 18:59:34 +08:00
}
}
public void CheckVersion()
2020-05-24 08:06:40 +08:00
{
StartCoroutine(GetVersion());
}
IEnumerator GetVersion()
2019-10-23 18:59:34 +08:00
{
isCheck = true;
2020-04-09 08:02:46 +08:00
isNetworkError = false;
networkError = "";
2020-05-24 08:06:40 +08:00
var url = "https://raw.githubusercontent.com/monitor1394/unity-ugui-XCharts/master/Assets/XCharts/package.json";
var web = UnityWebRequest.Get(url);
yield return web.SendWebRequest();
CheckVersionWebRequest(web);
if (isNetworkError)
{
url = "https://gitee.com/monitor1394/unity-ugui-XCharts/raw/master/Assets/XCharts/package.json";
web = UnityWebRequest.Get(url);
yield return web.SendWebRequest();
CheckVersionWebRequest(web);
}
if (needUpdate)
2019-10-23 18:59:34 +08:00
{
2020-05-24 08:06:40 +08:00
url = "https://raw.githubusercontent.com/monitor1394/unity-ugui-XCharts/master/Assets/XCharts/CHANGELOG.md";
web = UnityWebRequest.Get(url);
yield return web.SendWebRequest();
if (!CheckLogWebRequest(web))
{
url = "https://gitee.com/monitor1394/unity-ugui-XCharts/raw/master/Assets/XCharts/CHANGELOG.md";
web = UnityWebRequest.Get(url);
yield return web.SendWebRequest();
CheckLogWebRequest(web);
}
2019-10-23 18:59:34 +08:00
}
2020-05-24 08:06:40 +08:00
isCheck = false;
2019-10-23 18:59:34 +08:00
}
2020-05-24 08:06:40 +08:00
private void CheckVersionWebRequest(UnityWebRequest web)
2019-10-23 18:59:34 +08:00
{
if (IsNetworkError(web))
2020-04-09 08:02:46 +08:00
{
isNetworkError = true;
networkError = web.error;
m_NewVersion = "-";
}
else if (web.responseCode == 200)
2019-10-23 18:59:34 +08:00
{
2020-05-24 08:06:40 +08:00
isNetworkError = false;
2020-04-09 08:02:46 +08:00
var cv = JsonUtility.FromJson<XChartsVersion>(web.downloadHandler.text);
2020-05-24 08:06:40 +08:00
m_NewVersion = cv.version + "_" + cv.date;
2019-10-23 18:59:34 +08:00
newDate = cv.date;
newCheckDate = cv.checkdate;
desc = cv.desc;
homepage = cv.homepage;
}
2020-04-09 08:02:46 +08:00
else
{
isNetworkError = true;
if (web.responseCode > 0)
networkError = web.responseCode.ToString();
2020-05-24 08:06:40 +08:00
if (!string.IsNullOrEmpty(web.error))
networkError += "," + web.error;
if (string.IsNullOrEmpty(networkError))
{
2020-04-09 08:02:46 +08:00
networkError = "-";
2020-05-24 08:06:40 +08:00
}
2020-04-09 08:02:46 +08:00
m_NewVersion = "-";
}
2020-05-24 08:06:40 +08:00
web.Dispose();
2019-10-23 18:59:34 +08:00
}
2020-05-24 08:06:40 +08:00
private bool CheckLogWebRequest(UnityWebRequest web)
2019-10-23 18:59:34 +08:00
{
2020-05-24 08:06:40 +08:00
bool success = false;
if (web.responseCode == 200)
2019-10-23 18:59:34 +08:00
{
2020-04-09 08:02:46 +08:00
CheckLog(web.downloadHandler.text);
2020-05-24 08:06:40 +08:00
success = true;
2020-04-09 08:02:46 +08:00
}
2020-05-24 08:06:40 +08:00
web.Dispose();
return success;
2019-10-23 18:59:34 +08:00
}
private void CheckLog(string text)
{
StringBuilder sb = new StringBuilder();
var temp = text.Split('\n');
var regex = new Regex(".*(\\d{4}\\.\\d{2}\\.\\d{2}).*");
var checkDate = XChartsMgr.date;
foreach (var t in temp)
{
if (regex.IsMatch(t))
{
var mat = regex.Match(t);
var date = mat.Groups[1].ToString().Replace(".", "");
int logDate;
if (int.TryParse(date, out logDate))
{
if (logDate >= checkDate)
{
sb.Append(t).Append("\n");
}
else
{
break;
}
}
}
else
{
sb.Append(t).Append("\n");
}
}
changeLog = sb.ToString();
}
#if UNITY_5 || UNITY_2017_1
public bool IsNetworkError(UnityWebRequest request)
{
return request.isError && !IsHttpError(request);
}
#else
public bool IsNetworkError(UnityWebRequest request)
{
return request.isNetworkError;
}
#endif
#if UNITY_5
public bool IsHttpError(UnityWebRequest request)
{
return request.responseCode >= 400;
}
#else
public bool IsHttpError(UnityWebRequest request)
{
return request.isHttpError;
}
#endif
2020-05-10 00:20:40 +08:00
void OnEnable()
{
SceneManager.sceneUnloaded += OnSceneLoaded;
}
private void OnDisable()
{
SceneManager.sceneUnloaded -= OnSceneLoaded;
}
void OnSceneLoaded(Scene scene)
{
SerieLabelPool.ClearAll();
}
public void AddChart(BaseChart chart)
{
var sameNameChart = GetChart(chart.chartName);
if (sameNameChart != null)
{
var path = ChartHelper.GetFullName(sameNameChart.transform);
Debug.LogError("A chart named `" + chart.chartName + "` already exists:" + path);
}
if (!ContainsChart(chart))
{
m_ChartList.Add(chart);
}
2020-05-10 00:20:40 +08:00
}
public BaseChart GetChart(string chartName)
2020-05-10 00:20:40 +08:00
{
if (string.IsNullOrEmpty(chartName)) return null;
return m_ChartList.Find(chart => chartName.Equals(chart.chartName));
2020-05-10 00:20:40 +08:00
}
public List<BaseChart> GetCharts(string chartName)
2020-05-10 00:20:40 +08:00
{
if (string.IsNullOrEmpty(chartName)) return null;
return m_ChartList.FindAll(chart => chartName.Equals(chartName));
}
public void RemoveChart(string chartName)
{
if (string.IsNullOrEmpty(chartName)) return;
m_ChartList.RemoveAll(chart => chartName.Equals(chart.chartName));
}
public bool ContainsChart(string chartName)
{
if (string.IsNullOrEmpty(chartName)) return false;
return GetCharts(chartName) != null;
2020-05-10 00:20:40 +08:00
}
public bool ContainsChart(BaseChart chart)
2020-05-10 00:20:40 +08:00
{
return m_ChartList.Contains(chart);
2020-05-10 00:20:40 +08:00
}
2019-10-23 18:59:34 +08:00
}
}