123 lines
3.3 KiB
C#
123 lines
3.3 KiB
C#
/*
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
|
|
namespace game.data
|
|
{
|
|
public interface IDataService
|
|
{
|
|
public Task Init();
|
|
public T GetTable<T>();
|
|
}
|
|
|
|
public class LubanDataService : IDataService
|
|
{
|
|
private Dictionary<Type, object> tableMap;
|
|
|
|
public T GetTable<T>()
|
|
{
|
|
if (tableMap.TryGetValue(typeof(T), out var table))
|
|
{
|
|
return (T)table;
|
|
}
|
|
return default(T);
|
|
}
|
|
|
|
public async Task Init()
|
|
{
|
|
tableMap = new Dictionary<Type, object>();
|
|
|
|
// addr:json map
|
|
var rawDataMap = new Dictionary<string, string>();
|
|
var opMap = new Dictionary<string, AsyncOperationHandle<TextAsset>>();
|
|
|
|
var props = typeof(cfg.Tables).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
|
var addrs = props.Select(_ => _.Name.ToLower());
|
|
|
|
foreach (var addr in addrs)
|
|
{
|
|
var op = Addressables.LoadAssetAsync<TextAsset>(addr);
|
|
opMap.Add(addr, op);
|
|
}
|
|
|
|
await Task.WhenAll(opMap.Values.Select(_ => _.Task));
|
|
|
|
var tables = new cfg.Tables(
|
|
tableName => SimpleJSON.JSON.Parse(opMap[tableName].Result.text)
|
|
);
|
|
|
|
foreach (var prop in props)
|
|
{
|
|
tableMap.Add(prop.PropertyType, prop.GetValue(tables));
|
|
}
|
|
|
|
foreach (var kv in opMap)
|
|
{
|
|
Addressables.Release(kv.Value);
|
|
}
|
|
|
|
await Task.Yield();
|
|
}
|
|
}
|
|
|
|
public static class DataServiceExtensions
|
|
{
|
|
public static cfg.MapData GetMapData(this IDataService service, int id)
|
|
{
|
|
return service.GetTable<cfg.TbMapData>()[id];
|
|
}
|
|
|
|
public static cfg.FishData GetFishData(this IDataService service, int id)
|
|
{
|
|
var fishData = service.GetTable<cfg.TbFishData>();
|
|
|
|
if (!fishData.DataMap.ContainsKey(id))
|
|
{
|
|
return fishData.DataList[0];
|
|
}
|
|
|
|
return fishData[id];
|
|
}
|
|
|
|
public static cfg.RodData GetRodData(this IDataService service, int id)
|
|
{
|
|
var fishRodData = service.GetTable<cfg.TbRodData>();
|
|
if (!fishRodData.DataMap.ContainsKey(id))
|
|
{
|
|
return fishRodData.DataList[0];
|
|
}
|
|
|
|
return fishRodData[id];
|
|
}
|
|
|
|
public static cfg.AlbumExchange GetAlbumExchange(this IDataService service, int id)
|
|
{
|
|
return service.GetTable<cfg.TbAlbumExchange>().GetOrDefault(id);
|
|
}
|
|
|
|
public static cfg.Item GetItemData(this IDataService service, int id)
|
|
{
|
|
return service.GetTable<cfg.TbItem>().GetOrDefault(id);
|
|
}
|
|
|
|
public static string GetItemIconName(this IDataService service, int id)
|
|
{
|
|
var item = service.GetTable<cfg.TbItem>();
|
|
if (!item.DataMap.ContainsKey(id))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
return item[id].Icon;
|
|
}
|
|
|
|
}
|
|
}
|
|
*/
|