PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/WorldView/Program.cs

#
C# | 222 lines | 164 code | 36 blank | 22 comment | 20 complexity | 0250e49ff65e860a77ddedacbd5e1ec2 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. using NDesk.Options;
  8. using MoreTerra.Utilities;
  9. using MoreTerra.Structures;
  10. using MoreTerra.Structures.TerraInfo;
  11. using System.Xml;
  12. using System.Net;
  13. using System.IO.Compression;
  14. using System.Globalization;
  15. //public class Win32
  16. //{
  17. // /// <summary>
  18. // /// Allocates a new console for current process.
  19. // /// </summary>
  20. // [DllImport("kernel32.dll")]
  21. // public static extern bool AllocConsole();
  22. // /// <summary>
  23. // /// Frees the console.
  24. // /// </summary>
  25. // [DllImport("kernel32.dll")]
  26. // public static extern bool FreeConsole();
  27. //}
  28. namespace MoreTerra
  29. {
  30. static class Program
  31. {
  32. [DllImport("kernel32.dll")]
  33. static extern bool AttachConsole(int dwProcessId);
  34. private const int ATTACH_PARENT_PROCESS = -1;
  35. static void ReadUpdate()
  36. {
  37. WebClient wc = new WebClient();
  38. String page = wc.DownloadString("http://moreterra.codeplex.com/wikipage?title=Updates");
  39. String header = "<div class=\"wikidoc\">";
  40. Int32 start = page.IndexOf(header) + header.Length;
  41. Int32 end = page.IndexOf("</div>", start);
  42. String versionString = String.Empty;
  43. versionString = page.Substring(start, end - start);
  44. versionString = versionString.Replace("&amp;", "&");
  45. XmlDocument xmlDoc = new XmlDocument();
  46. xmlDoc.Load(versionString);
  47. MessageBox.Show(xmlDoc.InnerXml);
  48. }
  49. [STAThread]
  50. static void Main(string[] args)
  51. {
  52. String error = Global.Instance.Initialize();
  53. #if DEBUG
  54. if (error != String.Empty)
  55. {
  56. MessageBox.Show(error);
  57. return;
  58. }
  59. #endif
  60. // Initialize Managers
  61. try
  62. {
  63. TileProperties.Initialize();
  64. }
  65. catch (Exception ex)
  66. {
  67. MessageBox.Show("I failed Initilaizing TileProperties: " + ex.Message);
  68. }
  69. if (TestingFunction() == false)
  70. return;
  71. try
  72. {
  73. ResourceManager.Instance.Initialize();
  74. }
  75. catch (Exception ex)
  76. {
  77. MessageBox.Show("I failed Initilaizing Resource Manager: " + ex.Message);
  78. }
  79. try
  80. {
  81. SettingsManager.Instance.Initialize();
  82. }
  83. catch (Exception ex)
  84. {
  85. MessageBox.Show("I failed Initilaizing Settings Manager: " + ex.Message);
  86. }
  87. if (args.Length == 0) //started from windows
  88. {
  89. Application.EnableVisualStyles();
  90. Application.SetCompatibleTextRenderingDefault(false);
  91. Application.Run(new FormWorldView());
  92. SettingsManager.Instance.Shutdown();
  93. }
  94. else
  95. {
  96. //for (int i = 1; i < args.Length; i += 2)
  97. //{
  98. // MessageBox.Show(args[i - 1] + args[i]);
  99. //}
  100. // See if we are running in Mono and if so do not do the Attach.
  101. // Ugly but at least it lets the code run.
  102. Type t = Type.GetType("Mono.Runtime");
  103. if (t == null)
  104. AttachConsole(ATTACH_PARENT_PROCESS);
  105. bool show_help = false;
  106. string worldPath = string.Empty;
  107. string mapPath = string.Empty;
  108. string configName = string.Empty;
  109. var p = new OptionSet() {
  110. { "w|world=", "The path to the {WORLD} to map.",
  111. v => worldPath = v },
  112. { "o|output=", "The path to the {OUTPUT} file where the map PNG will be written.",
  113. v => mapPath = v},
  114. { "h|help", "Show this message and exit.",
  115. v => show_help = v != null },
  116. { "c|config=", "The named UserSettings to use for this run.",
  117. v => configName = v }
  118. };
  119. List<string> extra = new List<string>();
  120. try
  121. {
  122. extra = p.Parse(args);
  123. }
  124. catch (OptionException e)
  125. {
  126. Console.WriteLine(e.Message);
  127. Console.WriteLine("Try '" + System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath) + " --help' for more information.");
  128. }
  129. if (show_help || args.Contains<string>("-?") || worldPath == string.Empty || mapPath == string.Empty)
  130. {
  131. Console.WriteLine("Generates a PNG from a Terraria World file (*.wld).");
  132. Console.WriteLine();
  133. Console.WriteLine("Usage:");
  134. Console.WriteLine(" " + System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath) + "NoGui.bat [option1] <path1> [option2] <path2>");
  135. Console.WriteLine();
  136. Console.WriteLine("Options:");
  137. p.WriteOptionDescriptions(Console.Out);
  138. Console.WriteLine();
  139. Console.WriteLine("Example:");
  140. Console.WriteLine();
  141. Console.WriteLine(" " + System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath) + "NoGui.bat " +
  142. "-w \"C:\\Terraria Worlds\\world1.wld\" -o \"C:\\Terraria Maps\\World 1.png\"");
  143. }
  144. else if (worldPath == string.Empty || !File.Exists(worldPath))
  145. {
  146. Console.WriteLine("The World file could not be found: " + worldPath);
  147. }
  148. else
  149. {
  150. if (!string.IsNullOrWhiteSpace(configName))
  151. {
  152. SettingsManager.Instance.CurrentSettingsName = configName;
  153. }
  154. Console.WriteLine(Environment.NewLine + "Generating map from:" +
  155. Environment.NewLine + " " + worldPath);
  156. WorldMapper mapper = new WorldMapper();
  157. Global.Instance.InConsole = true;
  158. #if !DEBUG
  159. try
  160. {
  161. #endif
  162. mapper.Initialize();
  163. Console.WriteLine("Reading World file...");
  164. mapper.OpenWorld();
  165. mapper.ProcessWorld(worldPath, null);
  166. Console.WriteLine("World file closed. Generating PNG...");
  167. mapper.CreatePreviewPNG(mapPath, null);
  168. Console.WriteLine("Done! Saved to: " + Environment.NewLine + " " + mapPath);
  169. #if !DEBUG
  170. }
  171. catch (Exception ex)
  172. {
  173. Console.WriteLine("Error: " + ex.ToString());
  174. }
  175. #endif
  176. }
  177. }
  178. }
  179. // This is for single run code for running tests on new code.
  180. // Returning false will cause the program to cease.
  181. public static Boolean TestingFunction()
  182. {
  183. return true;
  184. }
  185. }
  186. }