154 lines
3.7 KiB
C#
154 lines
3.7 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using GameCore;
|
|
using System.Collections;
|
|
using System.IO;
|
|
using System;
|
|
|
|
namespace GameLogic
|
|
{
|
|
public class MConfig
|
|
{
|
|
Hashtable info;
|
|
|
|
public MConfig(string json)
|
|
{
|
|
info = MiniJSON.jsonDecode(json) as Hashtable;
|
|
}
|
|
|
|
public string ToJson()
|
|
{
|
|
return MiniJSON.jsonEncode(info);
|
|
}
|
|
|
|
public string GetInfo(string key)
|
|
{
|
|
if (info.ContainsKey(key))
|
|
{
|
|
return info[key] as string;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void RemoveInfo(string key)
|
|
{
|
|
if (info.ContainsKey(key))
|
|
{
|
|
info.Remove(key);
|
|
}
|
|
}
|
|
|
|
public void SetInfo(string key, string value)
|
|
{
|
|
info[key] = value;
|
|
}
|
|
}
|
|
public class ConfigManager : Singleton<ConfigManager>
|
|
{
|
|
|
|
/// <summary>
|
|
/// 版本号文件
|
|
/// </summary>
|
|
const string configFile = "config";
|
|
/// <summary>
|
|
/// 版本文件路径
|
|
/// </summary>
|
|
string configFilePath
|
|
{
|
|
get
|
|
{
|
|
return AppConst.PersistentDataPath + configFile + ".txt";
|
|
}
|
|
}
|
|
|
|
MConfig StreamingInfo;
|
|
MConfig PersistentInfo;
|
|
MConfig NetInfo;
|
|
// 初始化 获取本地的数据
|
|
public void Init()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(configFilePath))
|
|
{
|
|
PersistentInfo = new MConfig(File.ReadAllText(configFilePath, System.Text.Encoding.UTF8));
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(e);
|
|
}
|
|
|
|
try
|
|
{
|
|
StreamingInfo = new MConfig(Resources.Load<TextAsset>(configFile).text);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(e);
|
|
}
|
|
}
|
|
|
|
public void SetNetInfo(string info)
|
|
{
|
|
|
|
try
|
|
{
|
|
NetInfo = new MConfig(info);
|
|
SaveToFiles();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError(e);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存到文件
|
|
/// </summary>
|
|
void SaveToFiles()
|
|
{
|
|
PersistentInfo = NetInfo;
|
|
string directoryPath = Path.GetDirectoryName(configFilePath);
|
|
if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
|
|
File.WriteAllText(configFilePath, PersistentInfo.ToJson(), System.Text.Encoding.UTF8);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public string GetConfigInfo(string key)
|
|
{
|
|
string v = GetConfigNetInfo(key);
|
|
if (v != null) return v;
|
|
v = GetConfigPersistentInfo(key);
|
|
if (v != null) return v;
|
|
v = GetConfigStreamingInfo(key);
|
|
if (v != null) return v;
|
|
return null;
|
|
}
|
|
public string GetConfigNetInfo(string key)
|
|
{
|
|
if (NetInfo == null) return null;
|
|
string v = NetInfo.GetInfo(key);
|
|
return v;
|
|
}
|
|
public string GetConfigPersistentInfo(string key)
|
|
{
|
|
if (PersistentInfo == null) return null;
|
|
string v = PersistentInfo.GetInfo(key);
|
|
return v;
|
|
}
|
|
public string GetConfigStreamingInfo(string key)
|
|
{
|
|
if (StreamingInfo == null) return null;
|
|
string v = StreamingInfo.GetInfo(key);
|
|
return v;
|
|
}
|
|
|
|
|
|
}
|
|
}
|