using Bright.Serialization;
using System.IO;
using GameBase;
using GameConfig;
using SimpleJSON;
using UnityEngine;
///
/// 配置加载器
///
public class ConfigLoader:Singleton
{
private bool _init = false;
private Tables _tables;
public Tables Tables
{
get
{
if (!_init)
{
_init = true;
Load();
}
return _tables;
}
}
///
/// 加载配置
///
public void Load()
{
var tablesCtor = typeof(Tables).GetConstructors()[0];
var loaderReturnType = tablesCtor.GetParameters()[0].ParameterType.GetGenericArguments()[1];
System.Delegate loader = loaderReturnType == typeof(ByteBuf)
? new System.Func(LoadByteBuf)
: (System.Delegate)new System.Func(LoadJson);
_tables = (Tables)tablesCtor.Invoke(new object[] { loader });
}
///
/// 加载Json配置。
///
/// FileName
/// JSONNode
private JSONNode LoadJson(string file)
{
#if UNITY_EDITOR
var ret = File.ReadAllText($"{Application.dataPath}/../GenerateDatas/json/{file}.json", System.Text.Encoding.UTF8);
#else
var textAssets = GameModule.Resource.LoadAsset($"{SettingsUtils.FrameworkGlobalSettings.ConfigFolderName}{file}.json");
var ret = textAssets.text;
#endif
return JSON.Parse(ret);
}
///
/// 加载二进制配置。
///
/// FileName
/// ByteBuf
private ByteBuf LoadByteBuf(string file)
{
byte[] ret = null;
#if UNITY_EDITOR
ret = File.ReadAllBytes($"{Application.dataPath}/../GenerateDatas/bytes/{file}.bytes");
#else
var textAssets = GameModule.Resource.LoadAsset($"{SettingsUtils.FrameworkGlobalSettings.ConfigFolderName}{file}.bytes");
ret = textAssets.bytes;
#endif
return new ByteBuf(ret);
}
}