DataArray.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace FacilityFlag
  6. {
  7. public class DataArray<TKey, TValue>
  8. {
  9. public DataArray()
  10. {
  11. this.m_TempDataArray = new Dictionary<TKey, TValue>();
  12. }
  13. public DataArray(Dictionary<TKey, TValue> dataArray)
  14. {
  15. this.m_TempDataArray = dataArray;
  16. }
  17. public void Serialize(BinaryWriter brWrite, Converter<TKey, byte[]> converter_key, Converter<TValue, byte[]> converter_value)
  18. {
  19. brWrite.Write("CM3D21_TEMP_FLAG");
  20. int count = this.m_TempDataArray.Count;
  21. List<TKey> list = new List<TKey>(this.m_TempDataArray.Keys);
  22. List<TValue> list2 = new List<TValue>(this.m_TempDataArray.Values);
  23. brWrite.Write(count);
  24. for (int i = 0; i < count; i++)
  25. {
  26. byte[] array = converter_key(list[i]);
  27. brWrite.Write(array, 0, array.Length);
  28. array = converter_value(list2[i]);
  29. brWrite.Write(array, 0, array.Length);
  30. }
  31. }
  32. public void Deserialize(BinaryReader brRead, Func<BinaryReader, TKey> func_read_key, Func<BinaryReader, TValue> func_read_value)
  33. {
  34. string text = brRead.ReadString();
  35. NDebug.Assert(text == "CM3D21_TEMP_FLAG", string.Format("セーブデータのヘッダーが不正な値です\n{0}", text));
  36. this.Clear();
  37. int num = brRead.ReadInt32();
  38. for (int i = 0; i < num; i++)
  39. {
  40. TKey key = func_read_key(brRead);
  41. TValue value = func_read_value(brRead);
  42. this.Add(key, value, true);
  43. }
  44. }
  45. public static bool Skip(BinaryReader brRead, Func<BinaryReader, TKey> func_read_key, Func<BinaryReader, TValue> func_read_value)
  46. {
  47. long position = 0L;
  48. bool result;
  49. try
  50. {
  51. position = brRead.BaseStream.Position;
  52. string text = brRead.ReadString();
  53. if (text != "CM3D21_TEMP_FLAG")
  54. {
  55. throw new Exception(string.Format("セーブデータのヘッダーが不正な値です\n{0}", text));
  56. }
  57. int num = brRead.ReadInt32();
  58. for (int i = 0; i < num; i++)
  59. {
  60. func_read_key(brRead);
  61. func_read_value(brRead);
  62. }
  63. result = true;
  64. }
  65. catch (Exception ex)
  66. {
  67. Debug.LogWarning(ex.Message + "\n関数呼び出し前の位置へ戻ります。");
  68. brRead.BaseStream.Position = position;
  69. result = false;
  70. }
  71. return result;
  72. }
  73. public int Count()
  74. {
  75. return this.m_TempDataArray.Count;
  76. }
  77. public bool Contains(TKey key)
  78. {
  79. return this.m_TempDataArray.ContainsKey(key);
  80. }
  81. public bool Add(TKey key, TValue value, bool enableOverWrite = true)
  82. {
  83. if (this.Contains(key))
  84. {
  85. if (!enableOverWrite)
  86. {
  87. return false;
  88. }
  89. this.m_TempDataArray[key] = value;
  90. }
  91. else
  92. {
  93. this.m_TempDataArray.Add(key, value);
  94. }
  95. return true;
  96. }
  97. public TValue Get(TKey key, bool enableCheckWarning = false)
  98. {
  99. if (this.Contains(key))
  100. {
  101. return this.m_TempDataArray[key];
  102. }
  103. NDebug.Warning(!enableCheckWarning, string.Format("フラグ「{0}」は存在しません。", key));
  104. return default(TValue);
  105. }
  106. public bool Remove(TKey key, bool enableCheckWarning = false)
  107. {
  108. if (this.Contains(key))
  109. {
  110. this.m_TempDataArray.Remove(key);
  111. return true;
  112. }
  113. NDebug.Warning(!enableCheckWarning, string.Format("フラグ「{0}」は存在しません。", key));
  114. return false;
  115. }
  116. public void Clear()
  117. {
  118. this.m_TempDataArray.Clear();
  119. }
  120. public void Loop(Action<int, TKey, TValue> callback)
  121. {
  122. int count = this.m_TempDataArray.Count;
  123. List<TKey> list = new List<TKey>(this.m_TempDataArray.Keys);
  124. List<TValue> list2 = new List<TValue>(this.m_TempDataArray.Values);
  125. for (int i = 0; i < count; i++)
  126. {
  127. callback(i, list[i], list2[i]);
  128. }
  129. }
  130. public Dictionary<TKey, TValue> Copy()
  131. {
  132. return new Dictionary<TKey, TValue>(this.m_TempDataArray);
  133. }
  134. public void DebugLogConsole()
  135. {
  136. }
  137. protected readonly Dictionary<TKey, TValue> m_TempDataArray;
  138. }
  139. }