PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/WorldView/ResourceManager.cs

#
C# | 247 lines | 189 code | 38 blank | 20 comment | 17 complexity | 0df3f2323d6c52acd2860cf43159a9dd MD5 | raw file
  1. using System;
  2. using System.Drawing.Imaging;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Drawing;
  6. using MoreTerra.Structures;
  7. using MoreTerra.Utilities;
  8. using MoreTerra.Structures.TerraInfo;
  9. namespace MoreTerra
  10. {
  11. public sealed class ResourceManager
  12. {
  13. static ResourceManager instance = null;
  14. static readonly object mutex = new object();
  15. private Dictionary<String, Bitmap> markerBitmaps;
  16. private Dictionary<String, Bitmap> customMarkerBitmaps;
  17. private Boolean useCustom;
  18. private ResourceManager()
  19. {
  20. useCustom = false;
  21. this.markerBitmaps = new Dictionary<String, Bitmap>();
  22. this.customMarkerBitmaps = new Dictionary<String, Bitmap>();
  23. }
  24. public static ResourceManager Instance
  25. {
  26. get
  27. {
  28. lock (mutex)
  29. {
  30. if (instance == null)
  31. {
  32. instance = new ResourceManager();
  33. }
  34. return instance;
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// Initialize the resource directory
  40. /// </summary>
  41. public void Initialize()
  42. {
  43. // Check to see if root folder directory exists
  44. if (!Directory.Exists(Global.ApplicationRootDirectory))
  45. {
  46. String oldRoot;
  47. String newRoot = Global.ApplicationRootDirectory;
  48. Directory.CreateDirectory(Global.ApplicationRootDirectory);
  49. foreach(String s in Global.OldProgramNames)
  50. {
  51. oldRoot = System.IO.Path.Combine(Environment.GetFolderPath(
  52. Environment.SpecialFolder.ApplicationData), s);
  53. if (Directory.Exists(oldRoot))
  54. {
  55. MoveAndCombineDirectories(oldRoot, newRoot);
  56. Directory.Delete(oldRoot);
  57. }
  58. }
  59. }
  60. if (!Directory.Exists(Global.ApplicationLogDirectory))
  61. {
  62. // Create it
  63. Directory.CreateDirectory(Global.ApplicationLogDirectory);
  64. }
  65. if (!Directory.Exists(Global.ApplicationResourceDirectory))
  66. {
  67. // Create it
  68. Directory.CreateDirectory(Global.ApplicationResourceDirectory);
  69. }
  70. // Load
  71. LoadMarkers();
  72. // Copy all the markers
  73. ValidateMarkerResources(false);
  74. // Load custom markers from the files.
  75. LoadCustomMarkers();
  76. }
  77. /// <summary>
  78. /// Copy the markers externally to the resource directory
  79. /// </summary>
  80. private void ValidateMarkerResources(Boolean overwrite)
  81. {
  82. foreach (KeyValuePair<String, Bitmap> kvp in markerBitmaps)
  83. {
  84. // if it doesnt exist recopy
  85. if((!File.Exists(Path.Combine(Global.ApplicationResourceDirectory,
  86. string.Format("{0}.png", kvp.Key)))) || overwrite == true)
  87. {
  88. SaveMarkerToResourceDirectory(kvp.Value, kvp.Key);
  89. }
  90. }
  91. }
  92. private void LoadMarkers()
  93. {
  94. foreach (KeyValuePair<String, List<MarkerInfo>> kvp in Global.Instance.Info.MarkerSets)
  95. {
  96. Bitmap b = (Bitmap)Properties.Resources.ResourceManager.GetObject(kvp.Key);
  97. markerBitmaps.Add(kvp.Key, b);
  98. foreach (MarkerInfo mi in kvp.Value)
  99. {
  100. b = (Bitmap)Properties.Resources.ResourceManager.GetObject(mi.markerImage);
  101. markerBitmaps.Add(mi.markerImage, b);
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// Loads Markers
  107. /// </summary>
  108. private void LoadCustomMarkers()
  109. {
  110. // Changed to FileStream as new Bitmap locks the image down.
  111. // Once loaded why do we need to keep the file from changing?
  112. FileStream stream;
  113. foreach (KeyValuePair<String, Bitmap> kvp in markerBitmaps)
  114. {
  115. string markerPath = GetMarkerPath(kvp.Key);
  116. stream = new FileStream(markerPath, FileMode.Open);
  117. this.customMarkerBitmaps.Add(kvp.Key, new Bitmap(stream));
  118. stream.Close();
  119. }
  120. }
  121. public void ResetCustomMarkers()
  122. {
  123. this.customMarkerBitmaps.Clear();
  124. ValidateMarkerResources(true);
  125. LoadCustomMarkers();
  126. }
  127. public Bitmap GetMarker(MarkerType markerType)
  128. {
  129. return this.GetMarker(Enum.GetName(typeof(MarkerType), markerType));
  130. }
  131. public Bitmap GetMarker(String markerName)
  132. {
  133. if (useCustom == false)
  134. {
  135. if (this.markerBitmaps.ContainsKey(markerName))
  136. return this.markerBitmaps[markerName];
  137. }
  138. else
  139. {
  140. if (this.customMarkerBitmaps.ContainsKey(markerName))
  141. return this.customMarkerBitmaps[markerName];
  142. }
  143. throw new ApplicationException(string.Format("Tile type {0} does not have an associated Marker", markerName));
  144. }
  145. private string GetMarkerPath(string markerName)
  146. {
  147. return Path.Combine(Global.ApplicationResourceDirectory, string.Format("{0}.png", markerName));
  148. }
  149. private void SaveMarkerToResourceDirectory(Bitmap marker, string name)
  150. {
  151. marker.Save(Path.Combine(Global.ApplicationResourceDirectory, string.Format("{0}.png", name)), ImageFormat.Png);
  152. }
  153. private void MoveAndCombineDirectories(String oldDir, String newDir)
  154. {
  155. String[] oldDirectories;
  156. String[] oldFiles;
  157. String newFileName;
  158. String newDirectoryName;
  159. #region MoveFiles
  160. oldFiles = Directory.GetFiles(oldDir);
  161. foreach (String file in oldFiles)
  162. {
  163. newFileName = Path.Combine(newDir, Path.GetFileName(file));
  164. if (File.Exists(newFileName))
  165. {
  166. // Find which one is newer and delete the older one.
  167. if (File.GetLastWriteTime(file) > File.GetLastWriteTime(newFileName))
  168. {
  169. File.Delete(newFileName);
  170. File.Move(file, newFileName);
  171. }
  172. else
  173. {
  174. File.Delete(file);
  175. }
  176. } else {
  177. File.Move(file, newFileName);
  178. }
  179. }
  180. #endregion
  181. oldDirectories = Directory.GetDirectories(oldDir);
  182. foreach (String dir in oldDirectories)
  183. {
  184. newDirectoryName = Path.Combine(newDir, Path.GetFileName(dir));
  185. if (Directory.Exists(newDirectoryName))
  186. {
  187. MoveAndCombineDirectories(dir, newDirectoryName);
  188. Directory.Delete(dir);
  189. }
  190. else
  191. {
  192. // It does not exist so let's just move it.
  193. Directory.Move(dir, newDirectoryName);
  194. }
  195. }
  196. }
  197. public Boolean Custom
  198. {
  199. get
  200. {
  201. return useCustom;
  202. }
  203. set
  204. {
  205. useCustom = value;
  206. }
  207. }
  208. }
  209. }