PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/WorldView/Global.cs

#
C# | 155 lines | 116 code | 33 blank | 6 comment | 14 complexity | 34711e9701fb144b2a889f9b7f342433 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Drawing;
  4using System.Globalization;
  5using System.IO;
  6using MoreTerra.Structures.TerraInfo;
  7
  8namespace MoreTerra
  9{
 10	public class Global
 11	{
 12		private static Global instance;
 13		private static readonly Object mutex = new Object();
 14
 15		private TerraInfo info;
 16
 17		// Added to allow certain things to never happen when the console is on.
 18		// Mainly pop-up forms and Dialogs.
 19		private Boolean runningConsole = false;
 20
 21		// This is here to stop handler code from running when we do not wish it to.
 22		// Checkboxes have a bad habit of running when we set their checked state in the code.
 23		private Boolean blockCustomHandlers = false;
 24
 25		// Our constants and other static readonly variables.
 26		public const Int32 CurrentVersion = 36;
 27
 28		public const string Credits = @"TJChap2840, Vib Rib, Infinite Monkeys, Dr VideoGames 0031, " + 
 29			"Musluk, Sanktanglia, Metamorf.\r\n\r\nAnd special thanks to kdfb for donating a copy of the game!";
 30                                       
 31		public const int ChestMaxItems = 20;
 32		public const int ChestMaxNumber = 1000;
 33		public const Int32 SignMaxNumber = 1000;
 34		public const Int32 SignMaxSize = 1500;
 35
 36		public const Int32 OldLightingCrop = 24;
 37		public const Int32 NewLightingCrop = 41;
 38
 39		public static readonly string ApplicationRootDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MoreTerra");
 40
 41		public static readonly string ApplicationLogDirectory = System.IO.Path.Combine(ApplicationRootDirectory, "Logs");
 42		public static readonly string ApplicationResourceDirectory = System.IO.Path.Combine(ApplicationRootDirectory, "Resources");
 43
 44		public static readonly string ApplicationUserSettingsFile = System.IO.Path.Combine(ApplicationRootDirectory, "UserSettings.xml");
 45
 46		public static readonly string[] OldProgramNames = { "TerrariaWorldViewer", "MoreTerrra" };
 47
 48		#region Constructors
 49		private Global()
 50		{
 51
 52		}
 53		#endregion
 54
 55		#region Initlialization
 56		public String Initialize()
 57		{
 58			info = new TerraInfo();
 59
 60			String errors = info.LoadInfo(Properties.Resources.Items);
 61
 62			if (errors != String.Empty)
 63				return errors;
 64
 65			return String.Empty;
 66		}
 67
 68		public static Global Instance
 69		{
 70			get
 71			{
 72				lock (mutex)
 73				{
 74					if (instance == null)
 75						instance = new Global();
 76				}
 77				return instance;
 78			}
 79		}
 80		#endregion
 81
 82		#region Get/Set functions
 83		public Boolean InConsole
 84		{
 85			get
 86			{
 87				return runningConsole;
 88			}
 89			set
 90			{
 91				runningConsole = value;
 92			}
 93		}
 94
 95		public TerraInfo Info
 96		{
 97			get
 98			{
 99				return info;
100			}
101			set
102			{
103				info = value;
104			}
105		}
106
107		public Boolean SkipEvents
108		{
109			get
110			{
111				return blockCustomHandlers;
112			}
113			set
114			{
115				blockCustomHandlers = value;
116			}
117		}
118
119		#endregion
120
121		#region Helper Functions
122		public static Boolean TryParseColor(String colorString, out Color ret)
123		{
124			Byte red, green, blue;
125			ret = Color.Black;
126
127			// One for # and three sets of two characters.
128			if (colorString.Length != 7)
129				return false;
130
131			if (colorString.IndexOf('#') != 0)
132				return false;
133
134			if (Byte.TryParse(colorString.Substring(1, 2), NumberStyles.HexNumber, null, out red) == false)
135				return false;
136
137			if (Byte.TryParse(colorString.Substring(3, 2), NumberStyles.HexNumber, null, out green) == false)
138				return false;
139
140			if (Byte.TryParse(colorString.Substring(5, 2), NumberStyles.HexNumber, null, out blue) == false)
141				return false;
142
143			ret = Color.FromArgb(red, green, blue);
144
145			return true;
146		}
147
148		public static String ToColorString(Color color)
149		{
150			return String.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
151		}
152		#endregion
153
154	}
155}