Working on a better resource loader
This commit is contained in:
parent
dc3eace03b
commit
b0eb526818
116
Campofinale/Resource/ResourceLoader.cs
Normal file
116
Campofinale/Resource/ResourceLoader.cs
Normal file
@ -0,0 +1,116 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Campofinale.Resource
|
||||
{
|
||||
public class ResourceLoader
|
||||
{
|
||||
|
||||
public static void LoadTableCfg()
|
||||
{
|
||||
var tableCfgTypes = GetAllTableCfgTypes();
|
||||
|
||||
foreach (var type in tableCfgTypes)
|
||||
{
|
||||
var attr = type.GetCustomAttribute<TableCfgTypeAttribute>();
|
||||
string json = ReadJsonFile(attr.Name);
|
||||
FieldInfo field = GetResourceField(type);
|
||||
if (field != null && json.Length > 0)
|
||||
{
|
||||
object deserializedData = DeserializeJson(json, field.FieldType, type);
|
||||
field.SetValue(null, deserializedData);
|
||||
//Logger.Print($"Loaded {attr.Name} into {field.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
private static object DeserializeJson(string json, Type fieldType, Type valueType)
|
||||
{
|
||||
if (fieldType.IsGenericType)
|
||||
{
|
||||
var genericTypeDef = fieldType.GetGenericTypeDefinition();
|
||||
|
||||
if (genericTypeDef == typeof(Dictionary<,>))
|
||||
{
|
||||
var keyType = fieldType.GetGenericArguments()[0]; // String
|
||||
var valType = fieldType.GetGenericArguments()[1]; // Il tipo desiderato
|
||||
return JsonConvert.DeserializeObject(json, typeof(Dictionary<,>).MakeGenericType(keyType, valType));
|
||||
}
|
||||
else if (genericTypeDef == typeof(List<>))
|
||||
{
|
||||
return JsonConvert.DeserializeObject(json, typeof(List<>).MakeGenericType(valueType));
|
||||
}
|
||||
}
|
||||
|
||||
// Oggetto singolo
|
||||
return JsonConvert.DeserializeObject(json, valueType);
|
||||
}
|
||||
public static string ReadJsonFile(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return File.ReadAllText(path);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.PrintError($"Error occured while loading {path} Err: {e.Message}");
|
||||
ResourceManager.missingResources = true;
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static List<Type> GetAllTableCfgTypes()
|
||||
{
|
||||
return Assembly.GetExecutingAssembly()
|
||||
.GetTypes()
|
||||
.Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttribute<TableCfgTypeAttribute>() != null)
|
||||
.ToList();
|
||||
}
|
||||
public static FieldInfo GetResourceField(Type type)
|
||||
{
|
||||
var resourceManagerType = typeof(ResourceManager);
|
||||
var fields = resourceManagerType.GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||
|
||||
foreach (var field in fields)
|
||||
{
|
||||
var fieldType = field.FieldType;
|
||||
|
||||
if (fieldType.IsGenericType)
|
||||
{
|
||||
var genericTypeDef = fieldType.GetGenericTypeDefinition();
|
||||
|
||||
// Controlla se è un Dictionary<TKey, TValue> e se TValue è del tipo richiesto
|
||||
if (genericTypeDef == typeof(Dictionary<,>))
|
||||
{
|
||||
var valueType = fieldType.GetGenericArguments()[1]; // Ottiene TValue
|
||||
if (valueType == type)
|
||||
{
|
||||
return field;
|
||||
}
|
||||
}
|
||||
// Controlla se è una List<T> e se T è del tipo richiesto
|
||||
else if (genericTypeDef == typeof(List<>) && fieldType.GetGenericArguments()[0] == type)
|
||||
{
|
||||
return field;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Se il campo non è una collezione ma è direttamente del tipo richiesto
|
||||
if (fieldType == type)
|
||||
{
|
||||
return field;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null; // Nessun campo trovato
|
||||
}
|
||||
}
|
||||
}
|
@ -28,19 +28,19 @@ namespace Campofinale.Resource
|
||||
public class ResourceManager
|
||||
{
|
||||
public static Dictionary<string, SceneAreaTable> sceneAreaTable = new();
|
||||
public static StrIdNumTable strIdNumTable = new StrIdNumTable();
|
||||
public static Dictionary<string, CharacterTable> characterTable = new();
|
||||
public static Dictionary<string, SystemJumpTable> systemJumpTable = new();
|
||||
public static StrIdNumTable strIdNumTable = new StrIdNumTable();//
|
||||
public static Dictionary<string, CharacterTable> characterTable = new(); //
|
||||
public static Dictionary<string, SystemJumpTable> systemJumpTable = new(); //
|
||||
public static Dictionary<string, SettlementBasicDataTable> settlementBasicDataTable = new();
|
||||
public static Dictionary<string, BlocMissionTable> blocMissionTable = new();
|
||||
public static MissionAreaTable missionAreaTable = new();
|
||||
public static MissionAreaTable missionAreaTable = new(); //
|
||||
public static Dictionary<string, DialogTextTable> dialogTextTable = new();
|
||||
public static Dictionary<string, GameSystemConfigTable> gameSystemConfigTable = new();
|
||||
public static Dictionary<string, WikiGroupTable> wikiGroupTable = new();
|
||||
public static Dictionary<string, object> blocUnlockTable = new();
|
||||
public static Dictionary<string, GameMechanicTable> gameMechanicTable = new();
|
||||
public static Dictionary<string, WeaponBasicTable> weaponBasicTable= new();
|
||||
public static Dictionary<string, BlocDataTable> blocDataTable = new();
|
||||
public static Dictionary<string, BlocDataTable> blocDataTable = new(); //
|
||||
public static Dictionary<string, ItemTable> itemTable = new();
|
||||
public static Dictionary<string, DomainDataTable> domainDataTable = new();
|
||||
public static Dictionary<string, CollectionTable> collectionTable = new();
|
||||
@ -59,16 +59,16 @@ namespace Campofinale.Resource
|
||||
public static Dictionary<string, SpaceShipCharBehaviourTable> spaceShipCharBehaviourTable = new();
|
||||
public static Dictionary<string, SpaceshipRoomInsTable> spaceshipRoomInsTable = new();
|
||||
public static Dictionary<string, DungeonTable> dungeonTable = new();
|
||||
public static Dictionary<string, LevelGradeTable> levelGradeTable = new();
|
||||
public static Dictionary<string, LevelGradeTable> levelGradeTable = new(); //
|
||||
public static Dictionary<string, RewardTable> rewardTable = new();
|
||||
public static Dictionary<string, AdventureTaskTable> adventureTaskTable = new();
|
||||
public static StrIdNumTable dialogIdTable = new();
|
||||
public static DialogIdTable dialogIdTable = new();//
|
||||
public static Dictionary<string, LevelShortIdTable> levelShortIdTable = new();
|
||||
public static Dictionary<string, FactoryBuildingTable> factoryBuildingTable = new();
|
||||
public static Dictionary<string, FacSTTNodeTable> facSTTNodeTable = new();
|
||||
public static Dictionary<string, FacSTTLayerTable> facSTTLayerTable = new();
|
||||
public static Dictionary<int, ItemTypeTable> itemTypeTable = new();
|
||||
public static InteractiveTable interactiveTable = new();
|
||||
public static Dictionary<int, ItemTypeTable> itemTypeTable = new(); //
|
||||
public static InteractiveTable interactiveTable = new(); //
|
||||
public static List<LevelScene> levelDatas = new();
|
||||
public static List<InteractiveData> interactiveData = new();
|
||||
|
||||
@ -104,7 +104,7 @@ namespace Campofinale.Resource
|
||||
dialogTextTable = JsonConvert.DeserializeObject<Dictionary<string, DialogTextTable>>(ReadJsonFile("TableCfg/DialogTextTable.json"));
|
||||
gameSystemConfigTable = JsonConvert.DeserializeObject<Dictionary<string, GameSystemConfigTable>>(ReadJsonFile("TableCfg/GameSystemConfigTable.json"));
|
||||
wikiGroupTable = JsonConvert.DeserializeObject<Dictionary<string, WikiGroupTable>>(ReadJsonFile("TableCfg/WikiGroupTable.json"));
|
||||
dialogIdTable = JsonConvert.DeserializeObject<StrIdNumTable>(ReadJsonFile("Json/GameplayConfig/DialogIdTable.json"));
|
||||
dialogIdTable = JsonConvert.DeserializeObject<DialogIdTable>(ReadJsonFile("Json/GameplayConfig/DialogIdTable.json"));
|
||||
blocUnlockTable = JsonConvert.DeserializeObject<Dictionary<string, object>>(ReadJsonFile("TableCfg/BlocUnlockTable.json"));
|
||||
gameMechanicTable= JsonConvert.DeserializeObject<Dictionary<string, GameMechanicTable>>(ReadJsonFile("TableCfg/GameMechanicTable.json"));
|
||||
weaponBasicTable = JsonConvert.DeserializeObject<Dictionary<string, WeaponBasicTable>>(ReadJsonFile("TableCfg/WeaponBasicTable.json"));
|
||||
@ -138,8 +138,9 @@ namespace Campofinale.Resource
|
||||
itemTypeTable = JsonConvert.DeserializeObject<Dictionary<int, ItemTypeTable>>(ReadJsonFile("TableCfg/ItemTypeTable.json"));
|
||||
interactiveTable = JsonConvert.DeserializeObject<InteractiveTable>(ReadJsonFile("Json/Interactive/InteractiveTable.json"));
|
||||
LoadInteractiveData();
|
||||
LoadLevelDatas();
|
||||
|
||||
LoadLevelDatas();
|
||||
ResourceLoader.LoadTableCfg();
|
||||
|
||||
if (missingResources)
|
||||
{
|
||||
Logger.PrintWarn("Missing some resources. The gameserver will probably crash.");
|
||||
@ -408,15 +409,7 @@ namespace Campofinale.Resource
|
||||
{
|
||||
public List<WikiGroup> list;
|
||||
}
|
||||
public class InteractiveTable
|
||||
{
|
||||
public Dictionary<string, InteractiveTemplate> interactiveDataDict = new();
|
||||
|
||||
public class InteractiveTemplate
|
||||
{
|
||||
public string templateId;
|
||||
}
|
||||
}
|
||||
|
||||
public class WikiGroup
|
||||
{
|
||||
public string groupId;
|
||||
@ -785,19 +778,6 @@ namespace Campofinale.Resource
|
||||
public string settlementId;
|
||||
public string domainId;
|
||||
}
|
||||
public class StrIdNumTable
|
||||
{
|
||||
public StrIdDic skill_group_id;
|
||||
public StrIdDic item_id;
|
||||
public Dictionary<string, int> dialogStrToNum;
|
||||
public StrIdDic chapter_map_id;
|
||||
public StrIdDic char_voice_id;
|
||||
public StrIdDic char_doc_id;
|
||||
public StrIdDic area_id;
|
||||
public StrIdDic map_mark_temp_id;
|
||||
public StrIdDic wiki_id;
|
||||
public StrIdDic client_game_var_string_id;
|
||||
}
|
||||
public class GachaCharPoolTable
|
||||
{
|
||||
public string id;
|
||||
@ -825,10 +805,7 @@ namespace Campofinale.Resource
|
||||
{
|
||||
|
||||
}
|
||||
public class SystemJumpTable
|
||||
{
|
||||
public int bindSystem;
|
||||
}
|
||||
|
||||
public class StrIdDic
|
||||
{
|
||||
public Dictionary<string, int> dic;
|
||||
@ -845,11 +822,7 @@ namespace Campofinale.Resource
|
||||
public string enemyId;
|
||||
public string templateId;
|
||||
}
|
||||
public class ItemTypeTable
|
||||
{
|
||||
public int itemType;
|
||||
public ItemStorageSpace storageSpace;
|
||||
}
|
||||
|
||||
public class ItemTable
|
||||
{
|
||||
public ItemValuableDepotType valuableTabType;
|
||||
@ -921,15 +894,7 @@ namespace Campofinale.Resource
|
||||
public string id;
|
||||
public int count;
|
||||
}
|
||||
public class CharacterTable
|
||||
{
|
||||
public List<Attributes> attributes;
|
||||
public string charId;
|
||||
public int weaponType;
|
||||
public string engName;
|
||||
public int rarity;
|
||||
|
||||
}
|
||||
|
||||
public class Attributes
|
||||
{
|
||||
public int breakStage;
|
||||
|
@ -6,7 +6,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
public class BlocDataTable
|
||||
[TableCfgType("TableCfg/BlocDataTable.json", LoadPriority.LOW)]
|
||||
public class BlocDataTable : TableCfgResource
|
||||
{
|
||||
public string blocId;
|
||||
}
|
||||
|
20
Campofinale/Resource/Table/CharacterTable.cs
Normal file
20
Campofinale/Resource/Table/CharacterTable.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Campofinale.Resource.ResourceManager;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
[TableCfgType("TableCfg/CharacterTable.json", LoadPriority.LOW)]
|
||||
public class CharacterTable : TableCfgResource
|
||||
{
|
||||
public List<Attributes> attributes;
|
||||
public string charId;
|
||||
public int weaponType;
|
||||
public string engName;
|
||||
public int rarity;
|
||||
|
||||
}
|
||||
}
|
14
Campofinale/Resource/Table/DialogIdTable.cs
Normal file
14
Campofinale/Resource/Table/DialogIdTable.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Campofinale.Resource.ResourceManager;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
[TableCfgType("Json/GameplayConfig/DialogIdTable.json", LoadPriority.LOW)]
|
||||
public class DialogIdTable : StrIdNumTable
|
||||
{
|
||||
}
|
||||
}
|
19
Campofinale/Resource/Table/InteractiveTable.cs
Normal file
19
Campofinale/Resource/Table/InteractiveTable.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
[TableCfgType("Json/Interactive/InteractiveTable.json", LoadPriority.LOW)]
|
||||
public class InteractiveTable : TableCfgResource
|
||||
{
|
||||
public Dictionary<string, InteractiveTemplate> interactiveDataDict = new();
|
||||
|
||||
public class InteractiveTemplate
|
||||
{
|
||||
public string templateId;
|
||||
}
|
||||
}
|
||||
}
|
15
Campofinale/Resource/Table/ItemTypeTable.cs
Normal file
15
Campofinale/Resource/Table/ItemTypeTable.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
[TableCfgType("TableCfg/ItemTypeTable.json", LoadPriority.LOW)]
|
||||
public class ItemTypeTable
|
||||
{
|
||||
public int itemType;
|
||||
public ItemStorageSpace storageSpace;
|
||||
}
|
||||
}
|
@ -6,7 +6,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
public class LevelGradeTable
|
||||
[TableCfgType("TableCfg/LevelGradeTable.json", LoadPriority.LOW)]
|
||||
public class LevelGradeTable : TableCfgResource
|
||||
{
|
||||
public string name;
|
||||
public List<LevelGradeInfo> grades;
|
||||
|
@ -6,7 +6,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
public class MissionAreaTable
|
||||
[TableCfgType("Json/GameplayConfig/MissionAreaTable.json", LoadPriority.LOW)]
|
||||
public class MissionAreaTable : TableCfgResource
|
||||
{
|
||||
public Dictionary<string, Dictionary<string, object>> m_areas;
|
||||
}
|
||||
|
24
Campofinale/Resource/Table/StrIdNumTable.cs
Normal file
24
Campofinale/Resource/Table/StrIdNumTable.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static Campofinale.Resource.ResourceManager;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
[TableCfgType("TableCfg/StrIdNumTable.json", LoadPriority.LOW)]
|
||||
public class StrIdNumTable : TableCfgResource
|
||||
{
|
||||
public StrIdDic skill_group_id;
|
||||
public StrIdDic item_id;
|
||||
public Dictionary<string, int> dialogStrToNum;
|
||||
public StrIdDic chapter_map_id;
|
||||
public StrIdDic char_voice_id;
|
||||
public StrIdDic char_doc_id;
|
||||
public StrIdDic area_id;
|
||||
public StrIdDic map_mark_temp_id;
|
||||
public StrIdDic wiki_id;
|
||||
public StrIdDic client_game_var_string_id;
|
||||
}
|
||||
}
|
14
Campofinale/Resource/Table/SystemJumpTable.cs
Normal file
14
Campofinale/Resource/Table/SystemJumpTable.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Campofinale.Resource.Table
|
||||
{
|
||||
[TableCfgType("TableCfg/SystemJumpTable.json", LoadPriority.LOW)]
|
||||
public class SystemJumpTable
|
||||
{
|
||||
public int bindSystem;
|
||||
}
|
||||
}
|
36
Campofinale/Resource/TableCfgResource.cs
Normal file
36
Campofinale/Resource/TableCfgResource.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Campofinale.Resource
|
||||
{
|
||||
public abstract class TableCfgResource
|
||||
{
|
||||
|
||||
public void OnLoad()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
public class TableCfgTypeAttribute : Attribute
|
||||
{
|
||||
public string Name { get; }
|
||||
public LoadPriority Priority { get; }
|
||||
|
||||
public TableCfgTypeAttribute(string name, LoadPriority priority)
|
||||
{
|
||||
Name = name;
|
||||
Priority = priority;
|
||||
}
|
||||
}
|
||||
|
||||
public enum LoadPriority
|
||||
{
|
||||
HIGH,
|
||||
MEDIUM,
|
||||
LOW
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user