MMData.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using SimpleJSON;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace COM3D2.MultipleMaids.Util
  7. {
  8. internal static class MMData
  9. {
  10. private const string CM_EXTENSIONS_DB = "cm3d2_extensions.json";
  11. private const string UI_NAMES_DB = "ui_names.json";
  12. private const string BGM_DB = "bgm.json";
  13. private const string VOICES_DB = "personal_voices.json";
  14. public static Dictionary<string, string> Dances { get; private set; }
  15. public static Dictionary<string, string> Dogus { get; private set; }
  16. public static Dictionary<string, string> Particles { get; private set; }
  17. public static Dictionary<string, string> Items { get; private set; }
  18. public static Dictionary<string, string> Backgrounds { get; private set; }
  19. public static Dictionary<string, IVoice[]> NormalVoices { get; private set; }
  20. public static Dictionary<string, IVoice[]> HVoices { get; private set; }
  21. private static readonly Random rand = new Random();
  22. static MMData()
  23. {
  24. Dances = new Dictionary<string, string>();
  25. Dogus = new Dictionary<string, string>();
  26. Particles = new Dictionary<string, string>();
  27. Items = new Dictionary<string, string>();
  28. Backgrounds = new Dictionary<string, string>();
  29. NormalVoices = new Dictionary<string, IVoice[]>();
  30. HVoices = new Dictionary<string, IVoice[]>();
  31. }
  32. private static void ParseUINames(string location)
  33. {
  34. string uiNames = Path.Combine(location, UI_NAMES_DB);
  35. if (File.Exists(uiNames))
  36. {
  37. JSONNode uiNamesJson = JSON.Parse(File.ReadAllText(uiNames));
  38. Dogus = ParseObj(uiNamesJson, "dogu");
  39. Particles = ParseObj(uiNamesJson, "particles");
  40. Items = ParseObj(uiNamesJson, "items");
  41. Backgrounds = ParseObj(uiNamesJson, "bg");
  42. }
  43. }
  44. private static void ParseExtensions(string location)
  45. {
  46. string extensionsPath = Path.Combine(location, CM_EXTENSIONS_DB);
  47. if (File.Exists(extensionsPath))
  48. {
  49. JSONNode extensionsJson = JSON.Parse(File.ReadAllText(extensionsPath));
  50. foreach (string key in extensionsJson.Keys)
  51. {
  52. if ((key != "CM3D2_Legacy" || !GameUty.IsEnabledCompatibilityMode) && GameMain.Instance.BgMgr.CreateAssetBundle(key) == null)
  53. {
  54. continue;
  55. }
  56. Dogus.Merge(ParseObj(extensionsJson, "dogu"));
  57. Items.Merge(ParseObj(extensionsJson, "item"));
  58. Backgrounds.Merge(ParseObj(extensionsJson, "bg"));
  59. }
  60. }
  61. }
  62. private static void ParseBGM(string location)
  63. {
  64. string bgmPath = Path.Combine(location, BGM_DB);
  65. if (File.Exists(bgmPath))
  66. {
  67. JSONNode bgmJson = JSON.Parse(File.ReadAllText(bgmPath));
  68. Backgrounds = bgmJson.Linq.ToDictionary(k => k.Key, k => k.Value.ToString());
  69. }
  70. }
  71. private static void ParseVoices(string location)
  72. {
  73. void Parse(Dictionary<string, IVoice[]> target, JSONNode node, string type)
  74. {
  75. JSONNode normalVoices = node[type];
  76. if (!normalVoices)
  77. {
  78. return;
  79. }
  80. foreach (string key in normalVoices.Keys)
  81. {
  82. JSONArray obj = normalVoices[key].AsArray;
  83. IVoice[] voices = new IVoice[obj.Count];
  84. for (int i = 0; i < voices.Length; i++)
  85. {
  86. JSONNode voiceObj = obj[i];
  87. if (voiceObj.IsNumber)
  88. {
  89. voices[i] = new SingleVoice(voiceObj.AsInt);
  90. }
  91. else if (voiceObj.IsArray && voiceObj.Count == 2)
  92. {
  93. voices[i] = new VoiceRange(voiceObj[0].AsInt, voiceObj[1].AsInt);
  94. }
  95. }
  96. target[key] = voices;
  97. }
  98. }
  99. string personalVoicesPath = Path.Combine(location, VOICES_DB);
  100. if (File.Exists(VOICES_DB))
  101. {
  102. JSONNode voicesJson = JSON.Parse(File.ReadAllText(personalVoicesPath));
  103. Parse(NormalVoices, voicesJson, "normal");
  104. Parse(HVoices, voicesJson, "h");
  105. }
  106. }
  107. public static void LoadDatabase(string location)
  108. {
  109. ParseUINames(location);
  110. ParseExtensions(location);
  111. ParseVoices(location);
  112. ParseBGM(location);
  113. }
  114. public static void Merge<TKey, TVal>(this Dictionary<TKey, TVal> self, Dictionary<TKey, TVal> with)
  115. {
  116. if (with.Count == 0)
  117. {
  118. return;
  119. }
  120. foreach (KeyValuePair<TKey, TVal> keyVal in with)
  121. {
  122. self[keyVal.Key] = keyVal.Value;
  123. }
  124. }
  125. public static Dictionary<string, string> ParseObj(JSONNode obj, string key)
  126. {
  127. return !obj[key] ? new Dictionary<string, string>() : obj[key].Linq.ToDictionary(k => k.Key, k => k.Value.ToString());
  128. }
  129. public static int PickRandomVoice(Dictionary<string, IVoice[]> voiceStorage, string personality)
  130. {
  131. if (!voiceStorage.TryGetValue(personality, out IVoice[] voices))
  132. {
  133. return -1;
  134. }
  135. return voices[rand.Next(voices.Length)].PickRandomVoice();
  136. }
  137. }
  138. }