Newer
Older
DungeonShooting / DungeonShooting_Godot / src / config / ExcelConfig.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using Godot;
  5.  
  6. namespace Config;
  7.  
  8. public static partial class ExcelConfig
  9. {
  10. /// <summary>
  11. /// Role.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  12. /// </summary>
  13. public static List<Role> Role_List { get; private set; }
  14. /// <summary>
  15. /// Role.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  16. /// </summary>
  17. public static Dictionary<string, Role> Role_Map { get; private set; }
  18.  
  19. /// <summary>
  20. /// Weapon.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  21. /// </summary>
  22. public static List<Weapon> Weapon_List { get; private set; }
  23. /// <summary>
  24. /// Weapon.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  25. /// </summary>
  26. public static Dictionary<string, Weapon> Weapon_Map { get; private set; }
  27.  
  28.  
  29. private static bool _init = false;
  30. /// <summary>
  31. /// 初始化所有配置表数据
  32. /// </summary>
  33. public static void Init()
  34. {
  35. if (_init) return;
  36. _init = true;
  37.  
  38. _InitRoleConfig();
  39. _InitWeaponConfig();
  40. }
  41. private static void _InitRoleConfig()
  42. {
  43. try
  44. {
  45. var text = _ReadConfigAsText("res://resource/config/Role.json");
  46. Role_List = JsonSerializer.Deserialize<List<Role>>(text);
  47. Role_Map = new Dictionary<string, Role>();
  48. foreach (var item in Role_List)
  49. {
  50. Role_Map.Add(item.Id, item);
  51. }
  52. }
  53. catch (Exception e)
  54. {
  55. GD.PrintErr(e.ToString());
  56. throw new Exception("初始化表'Role'失败!");
  57. }
  58. }
  59. private static void _InitWeaponConfig()
  60. {
  61. try
  62. {
  63. var text = _ReadConfigAsText("res://resource/config/Weapon.json");
  64. Weapon_List = JsonSerializer.Deserialize<List<Weapon>>(text);
  65. Weapon_Map = new Dictionary<string, Weapon>();
  66. foreach (var item in Weapon_List)
  67. {
  68. Weapon_Map.Add(item.Id, item);
  69. }
  70. }
  71. catch (Exception e)
  72. {
  73. GD.PrintErr(e.ToString());
  74. throw new Exception("初始化表'Weapon'失败!");
  75. }
  76. }
  77. private static string _ReadConfigAsText(string path)
  78. {
  79. var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  80. var asText = file.GetAsText();
  81. file.Dispose();
  82. return asText;
  83. }
  84. }