AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
XChartsMgr.cs
1 
2 
3 /************************************************/
4 /* */
5 /* Copyright (c) 2018 - 2021 monitor1394 */
6 /* https://github.com/monitor1394 */
7 /* */
8 /************************************************/
9 
10 using System.Text;
11 using System.Collections;
12 using System.Collections.Generic;
13 using System.Text.RegularExpressions;
14 using UnityEngine;
15 using UnityEngine.Networking;
16 using UnityEngine.SceneManagement;
17 using System.IO;
18 #if UNITY_EDITOR
19 using UnityEditor;
20 #endif
21 
22 namespace XCharts
23 {
25  {
26  public string version = "";
27  public int date = 0;
28  public int checkdate = 0;
29  public string desc = "";
30  public string homepage = "";
31  }
32 
33  [ExecuteInEditMode]
34  public class XChartsMgr : MonoBehaviour
35  {
36  internal static string _version = "2.6.0";
37  internal static int _versionDate = 20211230;
38  public static string version { get { return _version; } }
39  public static int versionDate { get { return _versionDate; } }
40  public static string fullVersion { get { return version + "-" + versionDate; } }
41 
42  [SerializeField] private string m_NowVersion;
43  [SerializeField] private string m_NewVersion;
44  [SerializeField] private List<BaseChart> m_ChartList = new List<BaseChart>();
45  [SerializeField] internal Dictionary<string, ChartTheme> m_ThemeDict = new Dictionary<string, ChartTheme>();
46  [SerializeField] internal List<string> m_ThemeNames = new List<string>();
47  private static XChartsMgr m_XCharts;
48 
49  public static XChartsMgr Instance
50  {
51  get
52  {
53  if (m_XCharts == null)
54  {
55  m_XCharts = FindObjectOfType<XChartsMgr>();
56  if (m_XCharts == null)
57  {
58  var obj = GameObject.Find("_xcharts_");
59  if (obj == null) obj = new GameObject("_xcharts_");
60  m_XCharts = obj.AddComponent<XChartsMgr>();
61  }
62  m_XCharts.m_NowVersion = fullVersion;
63  }
64  return m_XCharts;
65  }
66  }
67 
68  private void Awake()
69  {
70  if (m_XCharts != null)
71  {
72  GameObject.DestroyImmediate(this);
73  return;
74  }
75  SerieLabelPool.ClearAll();
76  m_ChartList.Clear();
77  XThemeMgr.ReloadThemeList();
78  }
79 
80  public string changeLog { get; private set; }
81  public string newVersion { get { return m_NewVersion; } }
82  public string nowVersion { get { return m_NowVersion; } }
83  public string desc { get; private set; }
84  public string homepage { get; private set; }
85  public int newDate { get; private set; }
86  public int newCheckDate { get; private set; }
87  public bool isCheck { get; private set; }
88  public bool isNetworkError { get; private set; }
89  public string networkError { get; private set; }
90 
91  public bool needUpdate
92  {
93  get
94  {
95  return !isNetworkError && newDate > versionDate;
96  }
97  }
98 
99  public void CheckVersion()
100  {
101  StartCoroutine(GetVersion());
102  }
103 
104  IEnumerator GetVersion()
105  {
106  isCheck = true;
107  isNetworkError = false;
108  networkError = "";
109  var url = "https://raw.githubusercontent.com/monitor1394/unity-ugui-XCharts/master/Assets/XCharts/package.json";
110  var web = UnityWebRequest.Get(url);
111 #if UNITY_2017_3_OR_NEWER
112  yield return web.SendWebRequest();
113 #else
114  yield return web.Send();
115 #endif
116  CheckVersionWebRequest(web);
117  if (isNetworkError)
118  {
119  url = "https://gitee.com/monitor1394/unity-ugui-XCharts/raw/master/Assets/XCharts/package.json";
120  web = UnityWebRequest.Get(url);
121 #if UNITY_2017_3_OR_NEWER
122  yield return web.SendWebRequest();
123 #else
124  yield return web.Send();
125 #endif
126  CheckVersionWebRequest(web);
127  }
128  if (needUpdate)
129  {
130  url = "https://raw.githubusercontent.com/monitor1394/unity-ugui-XCharts/master/Assets/XCharts/CHANGELOG.md";
131  web = UnityWebRequest.Get(url);
132 #if UNITY_2017_3_OR_NEWER
133  yield return web.SendWebRequest();
134 #else
135  yield return web.Send();
136 #endif
137  if (!CheckLogWebRequest(web))
138  {
139  url = "https://gitee.com/monitor1394/unity-ugui-XCharts/raw/master/Assets/XCharts/CHANGELOG.md";
140  web = UnityWebRequest.Get(url);
141 #if UNITY_2017_3_OR_NEWER
142  yield return web.SendWebRequest();
143 #else
144  yield return web.Send();
145 #endif
146  CheckLogWebRequest(web);
147  }
148  }
149  isCheck = false;
150  }
151 
152  private void CheckVersionWebRequest(UnityWebRequest web)
153  {
154  if (IsWebRequestError(web))
155  {
156  isNetworkError = true;
157  networkError = web.error;
158  m_NewVersion = "-";
159  }
160  else if (web.responseCode == 200)
161  {
162  isNetworkError = false;
163  var cv = JsonUtility.FromJson<XChartsVersion>(web.downloadHandler.text);
164  m_NewVersion = cv.version + "-" + cv.date;
165  newDate = cv.date;
166  newCheckDate = cv.checkdate;
167  desc = cv.desc;
168  homepage = cv.homepage;
169  }
170  else
171  {
172  isNetworkError = true;
173  if (web.responseCode > 0)
174  networkError = web.responseCode.ToString();
175  if (!string.IsNullOrEmpty(web.error))
176  networkError += "," + web.error;
177  if (string.IsNullOrEmpty(networkError))
178  {
179  networkError = "-";
180  }
181  m_NewVersion = "-";
182  }
183  web.Dispose();
184  }
185 
186  private bool CheckLogWebRequest(UnityWebRequest web)
187  {
188  bool success = false;
189  if (web.responseCode == 200)
190  {
191  CheckLog(web.downloadHandler.text);
192  success = true;
193  }
194  web.Dispose();
195  return success;
196  }
197 
198  private void CheckLog(string text)
199  {
200  StringBuilder sb = new StringBuilder();
201  var temp = text.Split('\n');
202  var regex = new Regex(".*(\\d{4}\\.\\d{2}\\.\\d{2}).*");
203  var checkDate = versionDate;
204  foreach (var t in temp)
205  {
206  if (regex.IsMatch(t))
207  {
208  var mat = regex.Match(t);
209  var date = mat.Groups[1].ToString().Replace(".", "");
210  int logDate;
211  if (int.TryParse(date, out logDate))
212  {
213  if (logDate >= checkDate)
214  {
215  sb.Append(t).Append("\n");
216  }
217  else
218  {
219  break;
220  }
221  }
222  }
223  else
224  {
225  sb.Append(t).Append("\n");
226  }
227  }
228  changeLog = sb.ToString();
229  }
230 
231  public bool IsWebRequestError(UnityWebRequest request)
232  {
233 #if UNITY_5
234  return request.isError && request.responseCode < 400;
235 #elif UNITY_2017_1
236  return request.isError && !request.isHttpError;
237 #elif UNITY_2020_2
238  return (int)request.result > 1;
239 #else
240  return request.isNetworkError;
241 #endif
242  }
243 
244  void OnEnable()
245  {
246  SceneManager.sceneUnloaded += OnSceneLoaded;
247  }
248 
249  private void OnDisable()
250  {
251  SceneManager.sceneUnloaded -= OnSceneLoaded;
252  }
253 
254  void OnSceneLoaded(Scene scene)
255  {
256  SerieLabelPool.ClearAll();
257  }
258 
259  public void AddChart(BaseChart chart)
260  {
261  var sameNameChart = GetChart(chart.chartName);
262  if (sameNameChart != null)
263  {
264  var path = ChartHelper.GetFullName(sameNameChart.transform);
265  Debug.LogError("A chart named `" + chart.chartName + "` already exists:" + path);
266  }
267  if (!ContainsChart(chart))
268  {
269  m_ChartList.Add(chart);
270  }
271  }
272 
273  public BaseChart GetChart(string chartName)
274  {
275  if (string.IsNullOrEmpty(chartName)) return null;
276  return m_ChartList.Find(chart => chartName.Equals(chart.chartName));
277  }
278 
279  public List<BaseChart> GetCharts(string chartName)
280  {
281  if (string.IsNullOrEmpty(chartName)) return null;
282  return m_ChartList.FindAll(chart => chartName.Equals(chartName));
283  }
284 
285  public void RemoveChart(string chartName)
286  {
287  if (string.IsNullOrEmpty(chartName)) return;
288  m_ChartList.RemoveAll(chart => chartName.Equals(chart.chartName));
289  }
290 
291  public bool ContainsChart(string chartName)
292  {
293  if (string.IsNullOrEmpty(chartName)) return false;
294  return GetCharts(chartName) != null;
295  }
296 
297  public bool ContainsChart(BaseChart chart)
298  {
299  return m_ChartList.Contains(chart);
300  }
301 
302  public static void RemoveAllChartObject()
303  {
304  if (Instance.m_ChartList.Count == 0)
305  {
306  return;
307  }
308  foreach (var chart in Instance.m_ChartList)
309  {
310  if (chart != null)
311  chart.RemoveChartObject();
312  }
313  }
314 
315  public static string GetPackageFullPath()
316  {
317  string packagePath = Path.GetFullPath("Packages/com.monitor1394.xcharts");
318  if (Directory.Exists(packagePath))
319  {
320  return packagePath;
321  }
322  packagePath = Path.GetFullPath("Assets/..");
323  if (Directory.Exists(packagePath))
324  {
325  // Search default location for development package
326  if (File.Exists(packagePath + "/Assets/Packages/com.monitor1394.xcharts/package.json"))
327  {
328  return packagePath + "/Assets/Packages/com.monitor1394.xcharts";
329  }
330 
331  // Search for default location of normal XCharts AssetStore package
332  if (File.Exists(packagePath + "/Assets/XCharts/package.json"))
333  {
334  return packagePath + "/Assets/XCharts";
335  }
336 
337  // Search for potential alternative locations in the user project
338  string[] matchingPaths = Directory.GetDirectories(packagePath, "XCharts", SearchOption.AllDirectories);
339  string path = ValidateLocation(matchingPaths, packagePath);
340  if (path != null) return Path.Combine(packagePath, path);
341  }
342 
343  return null;
344  }
345 
346  private static string ValidateLocation(string[] paths, string projectPath)
347  {
348  for (int i = 0; i < paths.Length; i++)
349  {
350  if (File.Exists(paths[i] + "/package.json"))
351  {
352  string folderPath = paths[i].Replace(projectPath, "");
353  folderPath = folderPath.TrimStart('\\', '/');
354  return folderPath;
355  }
356  }
357 
358  return null;
359  }
360 
361 #if UNITY_EDITOR
362  public static void EnableTextMeshPro()
363  {
364  DefineSymbolsUtil.AddGlobalDefine("dUI_TextMeshPro");
365  RemoveAllChartObject();
366  }
367 
368  public static void DisableTextMeshPro()
369  {
370  DefineSymbolsUtil.RemoveGlobalDefine("dUI_TextMeshPro");
371  RemoveAllChartObject();
372  }
373 
374  public static bool IsExistTMPAssembly()
375  {
376 
377 #if UNITY_2018_1_OR_NEWER
378  foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies(UnityEditor.Compilation.AssembliesType.Player))
379  {
380  if (assembly.name.Equals("Unity.TextMeshPro")) return true;
381  }
382 #elif UNITY_2017_3_OR_NEWER
383  foreach (var assembly in UnityEditor.Compilation.CompilationPipeline.GetAssemblies())
384  {
385  if (assembly.name.Equals("Unity.TextMeshPro")) return true;
386  }
387 #endif
388  return false;
389  }
390 
391  public static bool ModifyTMPRefence(bool removeTMP = false)
392  {
393  var packagePath = XChartsMgr.GetPackageFullPath();
394  if (!ModifyTMPRefence(packagePath + "/Runtime/XCharts.Runtime.asmdef", removeTMP)) return false;
395  if (!ModifyTMPRefence(packagePath + "/Editor/XCharts.Editor.asmdef", removeTMP)) return false;
396  return true;
397  }
398 
399  private static bool ModifyTMPRefence(string asmdefPath, bool removeTMP = false)
400  {
401  if (!File.Exists(asmdefPath))
402  {
403  Debug.LogError("AddTMPRefence ERROR: can't find: " + asmdefPath);
404  return false;
405  }
406  try
407  {
408  var dest = new List<string>();
409  var refs = new List<string>();
410  var lines = File.ReadAllLines(asmdefPath);
411  var referencesStart = false;
412  var addedTMP = false;
413  var removedTMP = false;
414  var tmpName = "\"Unity.TextMeshPro\"";
415  var refCount = 0;
416  foreach (var line in lines)
417  {
418  if (string.IsNullOrEmpty(line)) continue;
419  if (line.Contains("\"references\": ["))
420  {
421  dest.Add(line);
422  referencesStart = true;
423  }
424  else if (referencesStart)
425  {
426  if (line.Contains("],"))
427  {
428  referencesStart = false;
429  if (refCount > 0)
430  {
431  var old = dest[dest.Count - 1];
432  if (old.EndsWith(","))
433  dest[dest.Count - 1] = old.Substring(0, old.Length - 1);
434  }
435  if (!removeTMP && !refs.Contains(tmpName))
436  {
437  if (refs.Count > 0)
438  dest[dest.Count - 1] = dest[dest.Count - 1] + ",";
439  dest.Add(" " + tmpName);
440  dest.Add(line);
441  addedTMP = true;
442  }
443  else
444  {
445  dest.Add(line);
446  }
447  }
448  else
449  {
450  if (removeTMP)
451  {
452  if (!line.Contains(tmpName))
453  {
454  dest.Add(line);
455  refCount++;
456  }
457  else
458  {
459  removedTMP = true;
460  }
461  }
462  else
463  {
464  dest.Add(line);
465  refs.Add(line.Trim());
466  }
467  }
468  }
469  else
470  {
471  dest.Add(line);
472  }
473  }
474  if (addedTMP || removedTMP)
475  {
476  File.WriteAllText(asmdefPath, string.Join("\n", dest.ToArray()));
477  AssetDatabase.SaveAssets();
478  AssetDatabase.Refresh();
479  }
480  return true;
481  }
482  catch (System.Exception e)
483  {
484  Debug.LogError("AddTMPRefence ERROR:" + e.Message);
485  return false;
486  }
487  }
488 #endif
489  }
490 }
XCharts
Definition: RewardChart.cs:14
XCharts.XChartsVersion
Definition: XChartsMgr.cs:24
XCharts.XChartsMgr
Definition: XChartsMgr.cs:34
XCharts.BaseChart.chartName
string chartName
The name of chart.
Definition: BaseChart_API.cs:27
XCharts.BaseChart
The base class of all charts. 所有Chart的基类。
Definition: BaseChart_API.cs:21