Beispiel #1
0
        public static float Get(string key, Version version)
        {
            if (GameDataSystem.IsReady() == true)
            {
                var crc = new VersionCrc(version);

                float[] values;
                if (GameDataSystem.valuesByVersion.TryGetValue(crc, out values) == true)
                {
                    var keys = GameDataSystem.GetKeys();
                    if (keys != null && key != null)
                    {
                        var index = System.Array.IndexOf(keys, key.ToLower());
                        if (index >= 0)
                        {
                            return(values[index]);
                        }
                    }
                }

                //Debug.LogWarningFormat("[ GameData ] Key was not found: {0}", key);
            }
            else
            {
                if (Application.isPlaying == true)
                {
                    Debug.LogWarningFormat("[ GameData ] System not ready. Do not use `GameData.Get()` method while/before system starting. You can check it's state by `GameData.IsReady()` call. Key: `{0}`.", key);
                }
            }

            return(0f);
        }
Beispiel #2
0
        public static bool HasKey(string key, Version version)
        {
            if (GameDataSystem.IsReady() == true)
            {
                var crc = new VersionCrc(version);

                float[] values;
                if (GameDataSystem.valuesByVersion.TryGetValue(crc, out values) == true)
                {
                    var keys = GameDataSystem.GetKeys();
                    if (keys != null && key != null)
                    {
                        var index = System.Array.IndexOf(keys, key.ToLower());
                        if (index >= 0)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Beispiel #3
0
        public static void TryToSaveCSV(string data, bool loadCacheOnFail = true)
        {
            try {
                var parsed = CSVParser.ReadCSV(data);

                if (GameDataSystem.currentVersion == default(Version))
                {
                    var defaultVersion = parsed[0][0];
                    GameDataSystem.currentVersion = new Version(defaultVersion);

                    if (GameDataSystem.instance == null || GameDataSystem.instance.logEnabled == true)
                    {
                        WindowSystemLogger.Warning(GameDataSystem.GetName(), string.Format("Default version is used: {0}", GameDataSystem.currentVersion));
                    }
                }
                GameDataSystem.defaultVersion = GameDataSystem.currentVersion;

                var keysCount = 0;

                #region KEYS
                var keys = new List <string>();
                for (int i = 0; i < parsed.Count; ++i)
                {
                    if (i == 0)
                    {
                        continue;
                    }

                    var row = parsed[i];
                    var str = row[0].Trim();
                    if (string.IsNullOrEmpty(str) == true)
                    {
                        //str = string.Empty;
                        continue;
                    }

                    keys.Add(str.ToLower());

                    ++keysCount;
                }

                GameDataSystem.keys = keys.ToArray();
                #endregion

                var verCount = 0;

                #region VERSIONS
                var versions = new List <Version>();
                for (int i = 0; i < parsed[0].Length; ++i)
                {
                    if (i == 0)
                    {
                        continue;
                    }

                    var col     = parsed[0][i];
                    var version = new Version(col);
                    versions.Add(version);

                    if (version == GameDataSystem.defaultVersion)
                    {
                        GameDataSystem.currentVersionNumber = int.Parse(parsed[1][i]);
                    }

                    ++verCount;
                }

                GameDataSystem.versions = versions.ToArray();
                #endregion

                #region VALUES
                var values = new Dictionary <VersionCrc, float[]>();
                for (int j = 0; j < versions.Count; ++j)
                {
                    var version = versions[j];
                    var crc     = new VersionCrc(version);

                    var output = new List <float>();
                    for (int i = 0; i < parsed.Count; ++i)
                    {
                        if (i == 0)
                        {
                            continue;
                        }

                        var value = parsed[i][j + 1];
                        if (string.IsNullOrEmpty(value.Trim()) == true)
                        {
                            //value = "0";
                            continue;
                        }

                        value = value.Replace(",", ".").Replace(" ", string.Empty);

                        var col = float.Parse(value);
                        output.Add(col);
                    }

                    if (values.ContainsKey(crc) == false)
                    {
                        values.Add(crc, output.ToArray());
                    }
                }

                GameDataSystem.valuesByVersion = values;
                #endregion

                var path = GameDataSystem.GetCachePath();
                                #if STORAGE_NOT_SUPPORTED
                PlayerPrefs.SetString(path, data);
                                #else
                System.IO.File.WriteAllText(path, data);
                                #endif

                                #if UNITY_EDITOR
                path = GameDataSystem.GetBuiltinCachePath();
                System.IO.File.WriteAllText(path, data);
                                #endif

                GameDataSystem.currentVersion = GameDataSystem.defaultVersion;

                if (GameDataSystem.instance == null || GameDataSystem.instance.logEnabled == true)
                {
                    WindowSystemLogger.Log(GameDataSystem.GetName(), string.Format("Loaded version {3}. Cache saved to: {0}, Keys: {1}, Versions: {2}, Version: {4}", path, keysCount, verCount, GameDataSystem.GetCurrentVersionId(), GameDataSystem.currentVersion));
                }

                GameDataSystem.isReady = true;
            } catch (System.Exception ex) {
                if (GameDataSystem.instance == null || GameDataSystem.instance.logEnabled == true)
                {
                    // Nothing to do: failed to parse
                    WindowSystemLogger.Error(GameDataSystem.GetName(), string.Format("Parser error: {0}\n{1}", ex.Message, ex.StackTrace));
                }

                if (loadCacheOnFail == true)
                {
                    GameDataSystem.TryToLoadCache();
                }
            }
        }