CsvCommonDatabaseIDType.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. namespace wf
  4. {
  5. public abstract class CsvCommonDatabaseIDType<Data>
  6. {
  7. public abstract string csvTopCommonName { get; }
  8. public abstract string typeNameForErrorLog { get; }
  9. public int Count
  10. {
  11. get
  12. {
  13. this.Initialize();
  14. return this.commonIdManager.idMap.Count;
  15. }
  16. }
  17. public bool Contains(int id)
  18. {
  19. return this.commonIdManager.idMap.ContainsKey(id);
  20. }
  21. public Data GetData(int id)
  22. {
  23. this.Initialize();
  24. NDebug.Assert(this.basicDatas.ContainsKey(id), string.Concat(new object[]
  25. {
  26. this.typeNameForErrorLog,
  27. "\nID[",
  28. id,
  29. "]のデータは存在しません"
  30. }));
  31. return this.basicDatas[id];
  32. }
  33. public List<Data> GetAllDatas(bool onlyEnabled)
  34. {
  35. this.Initialize();
  36. List<Data> list = new List<Data>();
  37. foreach (KeyValuePair<int, KeyValuePair<string, string>> keyValuePair in this.commonIdManager.idMap)
  38. {
  39. if (!onlyEnabled || this.commonIdManager.enabledIdList.Contains(keyValuePair.Key))
  40. {
  41. list.Add(this.basicDatas[keyValuePair.Key]);
  42. }
  43. }
  44. return list;
  45. }
  46. public bool IsEnabled(int id)
  47. {
  48. this.Initialize();
  49. return this.commonIdManager.enabledIdList.Contains(id);
  50. }
  51. public virtual bool Initialize()
  52. {
  53. if (this.commonIdManager != null)
  54. {
  55. return false;
  56. }
  57. this.commonIdManager = new CsvCommonIdManager(this.csvTopCommonName, this.typeNameForErrorLog, CsvCommonIdManager.Type.IdOnly, null);
  58. this.basicDatas = this.CreateData(this.commonIdManager);
  59. return true;
  60. }
  61. protected abstract Dictionary<int, Data> CreateData(CsvCommonIdManager createIds);
  62. public CsvCommonDatabaseIDType<Data>.CsvPair OpenCsvFromNameSuffix(string nameSuffix)
  63. {
  64. return this.OpenCsv(this.csvTopCommonName + "_" + nameSuffix + ".csv");
  65. }
  66. public CsvCommonDatabaseIDType<Data>.CsvPair OpenCsv(string fileName)
  67. {
  68. bool flag;
  69. return new CsvCommonDatabaseIDType<Data>.CsvPair(fileName, ref flag);
  70. }
  71. protected CsvCommonIdManager commonIdManager;
  72. protected Dictionary<int, Data> basicDatas;
  73. public class CsvPair : IDisposable
  74. {
  75. public CsvPair(string fileName, out bool openResult)
  76. {
  77. this.file = GameUty.FileSystem.FileOpen(fileName);
  78. this.csv = new CsvParser();
  79. openResult = (this.file != null && this.csv.Open(this.file));
  80. }
  81. public CsvPair(string fileName)
  82. {
  83. this.file = GameUty.FileSystem.FileOpen(fileName);
  84. this.csv = new CsvParser();
  85. this.csv.Open(this.file);
  86. }
  87. public CsvPair()
  88. {
  89. }
  90. ~CsvPair()
  91. {
  92. this.Dispose();
  93. }
  94. public void Dispose()
  95. {
  96. if (this.isDisposed)
  97. {
  98. return;
  99. }
  100. this.isDisposed = true;
  101. if (this.file != null)
  102. {
  103. this.file.Dispose();
  104. }
  105. if (this.csv != null)
  106. {
  107. this.csv.Dispose();
  108. }
  109. this.file = null;
  110. this.csv = null;
  111. GC.SuppressFinalize(this);
  112. }
  113. public AFileBase file;
  114. public CsvParser csv;
  115. private bool isDisposed;
  116. }
  117. }
  118. }