SanAndreasUnity/Assets/Scripts/Utilities/Config.cs

162 lines
3.9 KiB
C#
Raw Normal View History

2021-01-26 22:29:34 +00:00
using Newtonsoft.Json.Linq;
2020-05-31 17:07:22 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
namespace SanAndreasUnity.Utilities
{
public static class Config
{
public const string const_game_dir = "game_dir";
2021-10-21 09:01:39 +00:00
public static string UserConfigFileName => "config.user.json";
2020-05-31 17:07:22 +00:00
2021-10-21 08:53:57 +00:00
public static string ConfigFilesDirectoryPath
2020-05-31 17:07:22 +00:00
{
get {
#if UNITY_EDITOR || UNITY_STANDALONE
return Directory.GetCurrentDirectory ();
#else
return Application.persistentDataPath;
#endif
}
}
2021-10-21 09:01:39 +00:00
public static string UserConfigFilePath => Path.Combine(ConfigFilesDirectoryPath, UserConfigFileName);
2020-05-31 17:07:22 +00:00
2021-10-21 08:53:57 +00:00
public static string GamePath => GetPath (const_game_dir);
2020-05-31 17:07:22 +00:00
2021-10-21 08:53:57 +00:00
public static string DataPath
2020-05-31 17:07:22 +00:00
{
get
{
#if UNITY_EDITOR
return Path.Combine(Directory.GetCurrentDirectory(), "Data");
#elif UNITY_STANDALONE
return Path.Combine(Application.dataPath, "Data");
#else
return Path.Combine(Application.persistentDataPath, "Data");
#endif
}
}
private static JObject _root = new JObject ();
private static JObject _user = new JObject ();
private static readonly Dictionary<string, string> _substitutions = new Dictionary<string, string> ();
private static TVal ConvertVal<TVal>(JToken val)
{
2021-10-21 08:50:23 +00:00
// note that if you pass string[] as type, it will fail on IL2CPP
2020-05-31 17:07:22 +00:00
try
{
return (TVal)Convert.ChangeType(val, typeof(TVal));
}
catch
{
return val.ToObject<TVal>();
}
}
2021-10-21 08:50:23 +00:00
private static TVal Get<TVal>(string key)
2020-05-31 17:07:22 +00:00
{
var userVal = _user[key];
if (userVal != null)
2021-10-21 08:50:23 +00:00
return ConvertVal<TVal>(userVal);
2020-05-31 17:07:22 +00:00
return ConvertVal<TVal>(_root[key]);
}
2021-10-21 08:50:23 +00:00
public static string GetString(string key)
{
return Get<string>(key);
}
public static int GetInt(string key)
{
return Get<int>(key);
}
public static bool GetBool(string key)
{
return Get<bool>(key);
}
2020-05-31 17:07:22 +00:00
private static string GetSubstitution(string key)
{
if (_substitutions.ContainsKey(key)) return _substitutions[key];
string subs;
if (key == "data_dir")
{
subs = DataPath;
}
else
{
2021-10-21 08:50:23 +00:00
subs = ReplaceSubstitutions(GetString(key));
2020-05-31 17:07:22 +00:00
}
_substitutions.Add(key, subs);
return subs;
}
private static readonly Regex _regex = new Regex(@"\$\{(?<key>[a-z0-9_]+)\}", RegexOptions.Compiled);
private static string ReplaceSubstitutions(string value)
{
return _regex.Replace(value, x => GetSubstitution(x.Groups["key"].Value));
}
public static string GetPath(string key)
{
2021-10-21 08:50:23 +00:00
return ReplaceSubstitutions(GetString(key));
2020-05-31 17:07:22 +00:00
}
public static string[] GetPaths(string key)
{
return Get<JArray>(key)
.Select(x => ReplaceSubstitutions((string)x))
.ToArray();
}
public static void SetString (string key, string value)
{
_user [key] = value;
}
public static void Load ()
{
_root = new JObject ();
_user = new JObject ();
_substitutions.Clear ();
2019-07-21 17:52:16 +00:00
_root = JObject.Parse (Resources.Load<TextAsset>("config").text);
2020-05-31 17:07:22 +00:00
2021-10-21 09:01:39 +00:00
if (File.Exists (UserConfigFilePath))
2020-05-31 17:07:22 +00:00
{
2021-10-21 09:01:39 +00:00
_user = JObject.Parse (File.ReadAllText (UserConfigFilePath));
2020-05-31 17:07:22 +00:00
}
}
public static void SaveUserConfig ()
{
2021-10-21 09:01:39 +00:00
File.WriteAllText (UserConfigFilePath, _user.ToString (Newtonsoft.Json.Formatting.Indented));
2020-05-31 17:07:22 +00:00
}
public static void SaveUserConfigSafe ()
{
F.RunExceptionSafe (() => SaveUserConfig ());
}
}
}