SaveData.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace SceneNPCEdit
  6. {
  7. public static class SaveData
  8. {
  9. public static void Serialize(BinaryWriter binary)
  10. {
  11. binary.Write("CM3D2_NPCEDIT");
  12. binary.Write(1270);
  13. binary.Write(SaveData.presetData.Count);
  14. foreach (KeyValuePair<int, byte[]> keyValuePair in SaveData.presetData)
  15. {
  16. binary.Write(keyValuePair.Key);
  17. binary.Write(keyValuePair.Value.Length);
  18. binary.Write(keyValuePair.Value);
  19. }
  20. }
  21. public static void Deserialize(BinaryReader binary, int version)
  22. {
  23. SaveData.presetData.Clear();
  24. SaveData.presetDataCache.Clear();
  25. if (version < 1250)
  26. {
  27. return;
  28. }
  29. string a = binary.ReadString();
  30. NDebug.Assert(a == "CM3D2_NPCEDIT", "NPCEDITのヘッダーが不正です。");
  31. int num = binary.ReadInt32();
  32. int num2 = binary.ReadInt32();
  33. for (int i = 0; i < num2; i++)
  34. {
  35. int key = binary.ReadInt32();
  36. int count = binary.ReadInt32();
  37. byte[] value = binary.ReadBytes(count);
  38. if (!SaveData.presetData.ContainsKey(key))
  39. {
  40. SaveData.presetData.Add(key, value);
  41. }
  42. }
  43. }
  44. public static int GetID(CharacterMgr.NpcData targetNpcData)
  45. {
  46. foreach (EditCharacterDatabase.Data data in EditCharacterDatabase.GetAllDatas(true))
  47. {
  48. foreach (CharacterMgr.NpcData npcData in data.overRideTargetNpc)
  49. {
  50. if (npcData.uniqueName == targetNpcData.uniqueName)
  51. {
  52. return data.id;
  53. }
  54. }
  55. }
  56. return int.MinValue;
  57. }
  58. public static bool Contains(int id)
  59. {
  60. return SaveData.presetData.ContainsKey(id);
  61. }
  62. public static CharacterMgr.Preset GetPreset(int id)
  63. {
  64. CharacterMgr.Preset result = null;
  65. if (SaveData.presetDataCache.TryGetValue(id, out result))
  66. {
  67. return result;
  68. }
  69. if (SaveData.Contains(id))
  70. {
  71. byte[] buffer = SaveData.presetData[id];
  72. using (BinaryReader binaryReader = new BinaryReader(new MemoryStream(buffer), Encoding.UTF8))
  73. {
  74. CharacterMgr.Preset preset = GameMain.Instance.CharacterMgr.PresetLoad(binaryReader, string.Empty);
  75. SaveData.presetDataCache.Add(id, preset);
  76. result = preset;
  77. }
  78. }
  79. return result;
  80. }
  81. public static CharacterMgr.Preset GetPreset(CharacterMgr.NpcData npcData)
  82. {
  83. return SaveData.GetPreset(SaveData.GetID(npcData));
  84. }
  85. public static void SetPreset(int id, byte[] data)
  86. {
  87. SaveData.presetData[id] = data;
  88. if (SaveData.presetDataCache.ContainsKey(id))
  89. {
  90. SaveData.presetDataCache.Remove(id);
  91. }
  92. }
  93. public static void RemovePreset(int id)
  94. {
  95. SaveData.presetData.Remove(id);
  96. SaveData.presetDataCache.Remove(id);
  97. }
  98. public static Dictionary<int, byte[]> presetData = new Dictionary<int, byte[]>();
  99. public static Dictionary<int, CharacterMgr.Preset> presetDataCache = new Dictionary<int, CharacterMgr.Preset>();
  100. }
  101. }