/src/HFM.Instances/UnitInfoContainer.cs

http://hfm-net.googlecode.com/ · C# · 195 lines · 104 code · 33 blank · 58 comment · 0 complexity · bba6c1b07c0f40a8d0cee943d7ec3b55 MD5 · raw file

  1. /*
  2. * HFM.NET - UnitInfo Container Class
  3. * Copyright (C) 2009-2010 Ryan Harlamert (harlam357)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; version 2
  8. * of the License. See the included file GPLv2.TXT.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Diagnostics;
  22. using System.IO;
  23. using ProtoBuf;
  24. using HFM.Framework;
  25. using HFM.Framework.DataTypes;
  26. namespace HFM.Instances
  27. {
  28. [ProtoContract]
  29. public class UnitInfoCollection
  30. {
  31. private readonly List<UnitInfo> _unitInfoList = new List<UnitInfo>();
  32. /// <summary>
  33. /// Serialized UnitInfo List
  34. /// </summary>
  35. [ProtoMember(1)]
  36. public List<UnitInfo> UnitInfoList
  37. {
  38. get { return _unitInfoList; }
  39. }
  40. }
  41. public interface IUnitInfoContainer
  42. {
  43. /// <summary>
  44. /// Add to the Container
  45. /// </summary>
  46. void Add(UnitInfo unit);
  47. /// <summary>
  48. /// Clear the Container
  49. /// </summary>
  50. void Clear();
  51. /// <summary>
  52. /// Retrieve from the Container
  53. /// </summary>
  54. UnitInfo RetrieveUnitInfo(DisplayInstance displayInstance);
  55. /// <summary>
  56. /// Read Binary File
  57. /// </summary>
  58. void Read();
  59. /// <summary>
  60. /// Write Binary File
  61. /// </summary>
  62. void Write();
  63. }
  64. public class UnitInfoContainer : IUnitInfoContainer
  65. {
  66. #region Fields
  67. /// <summary>
  68. /// UnitInfo Collection
  69. /// </summary>
  70. private UnitInfoCollection _collection;
  71. /// <summary>
  72. /// Preferences Interface
  73. /// </summary>
  74. private readonly IPreferenceSet _prefs;
  75. #endregion
  76. #region Constructor
  77. public UnitInfoContainer(IPreferenceSet prefs)
  78. {
  79. _prefs = prefs;
  80. }
  81. #endregion
  82. #region Implementation
  83. /// <summary>
  84. /// Add to the Container
  85. /// </summary>
  86. public void Add(UnitInfo unit)
  87. {
  88. _collection.UnitInfoList.Add(unit);
  89. }
  90. /// <summary>
  91. /// Clear the Container
  92. /// </summary>
  93. public void Clear()
  94. {
  95. _collection.UnitInfoList.Clear();
  96. }
  97. /// <summary>
  98. /// Retrieve from the Container
  99. /// </summary>
  100. /// <param name="displayInstance"></param>
  101. public UnitInfo RetrieveUnitInfo(DisplayInstance displayInstance)
  102. {
  103. return _collection.UnitInfoList.Find(displayInstance.Owns);
  104. }
  105. #endregion
  106. #region Serialization Support
  107. /// <summary>
  108. /// Read Binary File
  109. /// </summary>
  110. public void Read()
  111. {
  112. string filePath = Path.Combine(_prefs.ApplicationDataFolderPath, Constants.UnitInfoCacheFileName);
  113. _collection = Deserialize(filePath) ?? new UnitInfoCollection();
  114. }
  115. /// <summary>
  116. /// Write Binary File
  117. /// </summary>
  118. public void Write()
  119. {
  120. Serialize(_collection, Path.Combine(_prefs.ApplicationDataFolderPath, Constants.UnitInfoCacheFileName));
  121. }
  122. private static readonly object SerializeLock = typeof(UnitInfoCollection);
  123. public static void Serialize(UnitInfoCollection collection, string filePath)
  124. {
  125. DateTime start = HfmTrace.ExecStart;
  126. lock (SerializeLock)
  127. {
  128. using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
  129. {
  130. try
  131. {
  132. Serializer.Serialize(fileStream, collection);
  133. }
  134. catch (Exception ex)
  135. {
  136. HfmTrace.WriteToHfmConsole(ex);
  137. }
  138. }
  139. }
  140. HfmTrace.WriteToHfmConsole(TraceLevel.Verbose, start);
  141. }
  142. public static UnitInfoCollection Deserialize(string filePath)
  143. {
  144. DateTime start = HfmTrace.ExecStart;
  145. UnitInfoCollection collection = null;
  146. try
  147. {
  148. using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  149. {
  150. collection = Serializer.Deserialize<UnitInfoCollection>(fileStream);
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. HfmTrace.WriteToHfmConsole(ex);
  156. }
  157. HfmTrace.WriteToHfmConsole(TraceLevel.Verbose, start);
  158. return collection;
  159. }
  160. #endregion
  161. }
  162. }