PageRenderTime 29ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/WorldView/Program.cs

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