diff --git a/Campofinale/Resource/ResourceLoader.cs b/Campofinale/Resource/ResourceLoader.cs new file mode 100644 index 0000000..acb231f --- /dev/null +++ b/Campofinale/Resource/ResourceLoader.cs @@ -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(); + 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 GetAllTableCfgTypes() + { + return Assembly.GetExecutingAssembly() + .GetTypes() + .Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttribute() != 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 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 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 + } + } +} diff --git a/Campofinale/Resource/ResourceManager.cs b/Campofinale/Resource/ResourceManager.cs index ae726a4..f40fc32 100644 --- a/Campofinale/Resource/ResourceManager.cs +++ b/Campofinale/Resource/ResourceManager.cs @@ -28,19 +28,19 @@ namespace Campofinale.Resource public class ResourceManager { public static Dictionary sceneAreaTable = new(); - public static StrIdNumTable strIdNumTable = new StrIdNumTable(); - public static Dictionary characterTable = new(); - public static Dictionary systemJumpTable = new(); + public static StrIdNumTable strIdNumTable = new StrIdNumTable();// + public static Dictionary characterTable = new(); // + public static Dictionary systemJumpTable = new(); // public static Dictionary settlementBasicDataTable = new(); public static Dictionary blocMissionTable = new(); - public static MissionAreaTable missionAreaTable = new(); + public static MissionAreaTable missionAreaTable = new(); // public static Dictionary dialogTextTable = new(); public static Dictionary gameSystemConfigTable = new(); public static Dictionary wikiGroupTable = new(); public static Dictionary blocUnlockTable = new(); public static Dictionary gameMechanicTable = new(); public static Dictionary weaponBasicTable= new(); - public static Dictionary blocDataTable = new(); + public static Dictionary blocDataTable = new(); // public static Dictionary itemTable = new(); public static Dictionary domainDataTable = new(); public static Dictionary collectionTable = new(); @@ -59,16 +59,16 @@ namespace Campofinale.Resource public static Dictionary spaceShipCharBehaviourTable = new(); public static Dictionary spaceshipRoomInsTable = new(); public static Dictionary dungeonTable = new(); - public static Dictionary levelGradeTable = new(); + public static Dictionary levelGradeTable = new(); // public static Dictionary rewardTable = new(); public static Dictionary adventureTaskTable = new(); - public static StrIdNumTable dialogIdTable = new(); + public static DialogIdTable dialogIdTable = new();// public static Dictionary levelShortIdTable = new(); public static Dictionary factoryBuildingTable = new(); public static Dictionary facSTTNodeTable = new(); public static Dictionary facSTTLayerTable = new(); - public static Dictionary itemTypeTable = new(); - public static InteractiveTable interactiveTable = new(); + public static Dictionary itemTypeTable = new(); // + public static InteractiveTable interactiveTable = new(); // public static List levelDatas = new(); public static List interactiveData = new(); @@ -104,7 +104,7 @@ namespace Campofinale.Resource dialogTextTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/DialogTextTable.json")); gameSystemConfigTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/GameSystemConfigTable.json")); wikiGroupTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/WikiGroupTable.json")); - dialogIdTable = JsonConvert.DeserializeObject(ReadJsonFile("Json/GameplayConfig/DialogIdTable.json")); + dialogIdTable = JsonConvert.DeserializeObject(ReadJsonFile("Json/GameplayConfig/DialogIdTable.json")); blocUnlockTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/BlocUnlockTable.json")); gameMechanicTable= JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/GameMechanicTable.json")); weaponBasicTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/WeaponBasicTable.json")); @@ -138,8 +138,9 @@ namespace Campofinale.Resource itemTypeTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/ItemTypeTable.json")); interactiveTable = JsonConvert.DeserializeObject(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 list; } - public class InteractiveTable - { - public Dictionary 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 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 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; - public string charId; - public int weaponType; - public string engName; - public int rarity; - - } + public class Attributes { public int breakStage; diff --git a/Campofinale/Resource/Table/BlocDataTable.cs b/Campofinale/Resource/Table/BlocDataTable.cs index d8c910f..2068ba9 100644 --- a/Campofinale/Resource/Table/BlocDataTable.cs +++ b/Campofinale/Resource/Table/BlocDataTable.cs @@ -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; } diff --git a/Campofinale/Resource/Table/CharacterTable.cs b/Campofinale/Resource/Table/CharacterTable.cs new file mode 100644 index 0000000..0038c6d --- /dev/null +++ b/Campofinale/Resource/Table/CharacterTable.cs @@ -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; + public string charId; + public int weaponType; + public string engName; + public int rarity; + + } +} diff --git a/Campofinale/Resource/Table/DialogIdTable.cs b/Campofinale/Resource/Table/DialogIdTable.cs new file mode 100644 index 0000000..c492af4 --- /dev/null +++ b/Campofinale/Resource/Table/DialogIdTable.cs @@ -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 + { + } +} diff --git a/Campofinale/Resource/Table/InteractiveTable.cs b/Campofinale/Resource/Table/InteractiveTable.cs new file mode 100644 index 0000000..0b015b6 --- /dev/null +++ b/Campofinale/Resource/Table/InteractiveTable.cs @@ -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 interactiveDataDict = new(); + + public class InteractiveTemplate + { + public string templateId; + } + } +} diff --git a/Campofinale/Resource/Table/ItemTypeTable.cs b/Campofinale/Resource/Table/ItemTypeTable.cs new file mode 100644 index 0000000..aa223d7 --- /dev/null +++ b/Campofinale/Resource/Table/ItemTypeTable.cs @@ -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; + } +} diff --git a/Campofinale/Resource/Table/LevelGradeTable.cs b/Campofinale/Resource/Table/LevelGradeTable.cs index fe78c01..265fb44 100644 --- a/Campofinale/Resource/Table/LevelGradeTable.cs +++ b/Campofinale/Resource/Table/LevelGradeTable.cs @@ -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 grades; diff --git a/Campofinale/Resource/Table/MissionAreaTable.cs b/Campofinale/Resource/Table/MissionAreaTable.cs index 54dc6a9..e60ea4c 100644 --- a/Campofinale/Resource/Table/MissionAreaTable.cs +++ b/Campofinale/Resource/Table/MissionAreaTable.cs @@ -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> m_areas; } diff --git a/Campofinale/Resource/Table/StrIdNumTable.cs b/Campofinale/Resource/Table/StrIdNumTable.cs new file mode 100644 index 0000000..47a2e13 --- /dev/null +++ b/Campofinale/Resource/Table/StrIdNumTable.cs @@ -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 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; + } +} diff --git a/Campofinale/Resource/Table/SystemJumpTable.cs b/Campofinale/Resource/Table/SystemJumpTable.cs new file mode 100644 index 0000000..e9f9154 --- /dev/null +++ b/Campofinale/Resource/Table/SystemJumpTable.cs @@ -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; + } +} diff --git a/Campofinale/Resource/TableCfgResource.cs b/Campofinale/Resource/TableCfgResource.cs new file mode 100644 index 0000000..9f75734 --- /dev/null +++ b/Campofinale/Resource/TableCfgResource.cs @@ -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 + } +}