PageRenderTime 158ms CodeModel.GetById 75ms app.highlight 41ms RepoModel.GetById 38ms app.codeStats 0ms

/WorldView/ResourceManager.cs

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