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

/WorldView/Global.cs

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