/* zlib License 跳出一方天地 (C) 2022-2023 Lucas This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1.The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; using UnityEngine.UI; public class GlobalGameDataManager : MonoBehaviour { // Global public struct GameData { public string name; public int meters; } private static GameData[] _gameDatas; // For Ranking Use private static string _dataPath; private static bool _initialized = false; private void Awake() { var flag = false; if (_initialized) return; _gameDatas = new GameData[4]; _dataPath = Application.persistentDataPath + "/rank.dat"; // Read from file if (File.Exists(_dataPath)) { // Read 3 Lines from local data using var streamReader = new StreamReader(_dataPath); for (var i = 0; i < 3; ++i) { try { var temp = streamReader.ReadLine()!.Split(' '); _gameDatas[i].name = temp[0]; _gameDatas[i].meters = int.Parse(temp[1]); } catch (Exception e) { print(e); print(_dataPath); flag = true; break; } } } if (flag) { if (File.Exists(_dataPath)) File.Delete(_dataPath); File.Create(_dataPath); _gameDatas = new GameData[4]; for (var i = 0; i < 4; ++i) { _gameDatas[i].name = "Unknown"; _gameDatas[i].meters = 0; } } _initialized = true; } public static bool NeedUpdateName(int score) { RecentScore = score; var tmpArray = new int[4]; for (var i = 0; i < 3; ++i) tmpArray[i] = _gameDatas[i].meters; tmpArray[3] = score; Array.Sort(tmpArray, (lhs, rhs) => rhs - lhs); return score != tmpArray[3]; } public static void UpdateGameData(GameData data) { // First, place New GameData in the back of the array _gameDatas[3] = data; // Then, sort _gameData according to meters Array.Sort(_gameDatas, (lhs, rhs) => rhs.meters - lhs.meters); WriteGameData(); } private static void WriteGameData() { using var streamWriter = new StreamWriter(_dataPath, false); for (var i = 0; i < 3; ++i) { streamWriter.WriteLine(_gameDatas[i].name + ' ' + _gameDatas[i].meters); } } // Call this only in MainScene public static int GetScore() { var distanceText = GameObject.FindGameObjectWithTag("DistanceText"); var score = distanceText.GetComponent().text; var result = int.Parse(score[9..^1]); print(result); return result; } public static IEnumerable GameDataList { get { var list = new List(); for (var i = 0; i < 3; ++i) list.Add(_gameDatas[i]); return list; } } public static int RecentScore { get; private set; } }