示例#1
0
        public void WriteBlock()
        {
            TConsole.WriteBlock("Hello, world!");
            TConsole.WriteBlock("Test {0} {2} {1}", "Alpha", "Beta", "Gamma");

            var actual   = captureOut.ToString();
            var expected = "\nHello, world!\n{0}\nTest Alpha Gamma Beta\n{0}".LFormat(Environment.NewLine);

            Assert.AreEqual(expected, actual);
        }
示例#2
0
        void onAvatarAdd(Instance sender, Avatar avatar)
        {
            TConsole.WriteLineColored(ConsoleColor.Cyan, "*** {0} [SID#{1}] enters", avatar.Name, avatar.Session);

            lock (SyncMutex)
                Users.Add(avatar);

            if (AvatarEnter != null)
            {
                AvatarEnter(sender, avatar);
            }
        }
示例#3
0
        /// <summary>
        /// Sets up event handlers for command parsing and chat printing to console
        /// </summary>
        public void SetupCommands()
        {
            Chat += parseCommand;
            Chat += (s, a, m) =>
            {
                TConsole.WriteLineColored(ConsoleColor.White, " {0} | {1}", a.Name.PadRight(16), m);
            };

            Bot.Console += (s, c) =>
            {
                TConsole.WriteLineColored(ConsoleColor.White, "Console: {0} {1}", c.Name, c.Message);
            };
        }
示例#4
0
        public void WriteHighlighted()
        {
            Console.BackgroundColor = ConsoleColor.Gray;
            TConsole.WriteColored(ConsoleColor.Red, "Hello, world!");
            Assert.AreEqual(ConsoleColor.Gray, Console.BackgroundColor);
            TConsole.WriteColored(ConsoleColor.Blue, "Test {0} {2} {1}", "Alpha", "Beta", "Gamma");
            Assert.AreEqual(ConsoleColor.Gray, Console.BackgroundColor);

            var actual   = captureOut.ToString();
            var expected = "Hello, world!Test Alpha Gamma Beta";

            Assert.AreEqual(expected, actual);
        }
示例#5
0
        public void WriteBlockColored()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            TConsole.WriteBlockColored(ConsoleColor.Red, "Hello, world!");
            Assert.AreEqual(ConsoleColor.Yellow, Console.ForegroundColor);
            TConsole.WriteBlockColored(ConsoleColor.Blue, "Test {0} {2} {1}", "Alpha", "Beta", "Gamma");
            Assert.AreEqual(ConsoleColor.Yellow, Console.ForegroundColor);

            var actual   = captureOut.ToString();
            var expected = "\nHello, world!\n{0}\nTest Alpha Gamma Beta\n{0}".LFormat(Environment.NewLine);

            Assert.AreEqual(expected, actual);
        }
示例#6
0
 public void Mutex()
 {
     var tasks = new Task[]
     {
         new Task(() => TConsole.WriteBlock("{0}{1}", "Code Of", "Honor")),
         new Task(() => TConsole.WriteBlockColored(ConsoleColor.DarkYellow, "{0}{1}", "The Best", "Of Both Worlds")),
         new Task(() => TConsole.WriteColored(ConsoleColor.Blue, "{0}{1}", "The Master", "piece Society")),
         new Task(() => TConsole.WriteColored(ConsoleColor.Red, ConsoleColor.Green, "{0}{1}", "Encounter At", "Farpoint")),
         new Task(() => TConsole.WriteLineColored(ConsoleColor.Cyan, "{0}{1}", "Q", "Who?")),
         new Task(() => TConsole.WriteLineColored(ConsoleColor.Black, ConsoleColor.DarkGray, "{0}{1}", "The Measure", "Of A Man")),
         new Task(() => TConsole.WriteBlock("{0}{1}", "Code of", "Honor")),
     };
 }
示例#7
0
        public void WriteLineHighlighted()
        {
            Console.BackgroundColor = ConsoleColor.DarkMagenta;
            TConsole.WriteLineHighlighted(ConsoleColor.Red, "Hello, world!");
            Assert.AreEqual(ConsoleColor.DarkMagenta, Console.BackgroundColor);
            TConsole.WriteLineHighlighted(ConsoleColor.Blue, "Test {0} {2} {1}", "Alpha", "Beta", "Gamma");
            Assert.AreEqual(ConsoleColor.DarkMagenta, Console.BackgroundColor);

            var actual   = captureOut.ToString();
            var expected = "Hello, world!{0}Test Alpha Gamma Beta{0}".LFormat(Environment.NewLine);

            Assert.AreEqual(expected, actual);
        }
示例#8
0
        void onAvatarLeave(Instance sender, Avatar avatar)
        {
            TConsole.WriteLineColored(ConsoleColor.Cyan, "*** {0} [SID#{1}] leaves", avatar.Name, avatar.Session);

            var user = GetUser(avatar.Session);

            if (AvatarLeave != null)
            {
                AvatarLeave(sender, avatar);
            }

            lock (SyncMutex)
                Users.Remove(user);
        }
示例#9
0
        public void WriteColored_Both()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.White;
            TConsole.WriteColored(ConsoleColor.Red, ConsoleColor.Green, "Hello, world!");
            Assert.AreEqual(ConsoleColor.Green, Console.ForegroundColor);
            Assert.AreEqual(ConsoleColor.White, Console.BackgroundColor);
            TConsole.WriteColored(ConsoleColor.Blue, ConsoleColor.Yellow, "Test {0} {2} {1}", "Alpha", "Beta", "Gamma");
            Assert.AreEqual(ConsoleColor.Green, Console.ForegroundColor);
            Assert.AreEqual(ConsoleColor.White, Console.BackgroundColor);

            var actual   = captureOut.ToString();
            var expected = "Hello, world!Test Alpha Gamma Beta";

            Assert.AreEqual(expected, actual);
        }
        static void Main(string[] args)
        {
            try
            {
                Console.Title = "Terraria's Dedicated Server Mod. (" + Statics.versionNumber + " {" + Statics.currentRelease + "}) #"
                    + Statics.build + " r" + Statics.revision;

                Console.WriteLine("Setting up Paths.");
                if (!setupPaths())
                {
                    return;
                }

                Console.WriteLine("Initializing...");

                LuaManager.Initialise("Scripts/Default.lua");

                if (Statics.isLinux)
                {
                    Console.WriteLine("Detected Linux OS.");
                    Statics.platform = 1;
                }
                else if (Statics.isMac)
                {
                    Console.WriteLine("Detected Mac OS.");
                    Statics.platform = 2;
                }
                else if (Statics.isWindows == false)
                {
                    Console.WriteLine("Unknown OS.");
                    Statics.platform = 3;
                }

                tConsole = new TConsole(Statics.getDataPath + Statics.systemSeperator + "server.log");

                if (args != null && args.Length > 0)
                {
                    string CmdMessage = args[0].Trim();
                    if (CmdMessage.Length > 0)
                    {
                        // 0 for Ops
                        if (CmdMessage.ToLower().Equals("-ignoremessages:0"))
                        {
                            Statics.cmdMessages = false;
                        }
                    }
                }

                Program.tConsole.WriteLine("Setting up Properties.");
                if (!System.IO.File.Exists("server.properties"))
                {
                    Console.Write("Properties not found, Create and exit? [Y/n]: ");
                    if (Console.ReadLine().ToLower() == "y")
                    {
                        setupProperties();
                        Console.WriteLine("Complete, Press any Key to Exit...");
                        Console.ReadKey(true);
                        return;
                    }
                }
                setupProperties();

                Statics.debugMode = properties.debugMode();
                if (Statics.debugMode)
                {
                    Program.tConsole.WriteLine("CAUTION: Running Debug Mode! Unexpected errors may occur!");
                }

                Program.tConsole.WriteLine("Preparing Server Data...");

                string worldFile = properties.getInitialWorldPath();
                FileInfo file = new FileInfo(worldFile);

                if (!file.Exists)
                {
                    try
                    {
                        file.Directory.Create();

                    }
                    catch (Exception exception)
                    {
                        Program.tConsole.WriteLine(exception.ToString());
                        Program.tConsole.WriteLine("Press any key to continue...");
                        Console.ReadKey(true);
                        return;
                    }
                    Program.tConsole.WriteLine("Generating World '" + worldFile + "'");

                    int seed = properties.getSeed();
                    if (seed == -1)
                    {
                        Console.Write("Generating Seed...");
                        seed = new Random().Next(100);
                        Console.Write(seed.ToString() + "\n");
                    }

                    int worldX = properties.getMapSizes()[0];
                    int worldY = properties.getMapSizes()[1];
                    if (properties.isUsingCutomTiles())
                    {
                        int X = properties.getMaxTilesX();
                        int Y = properties.getMaxTilesY();
                        if (X > 0 && Y > 0)
                        {
                            worldX = X;
                            worldY = Y;
                        }

                        if (worldX < (int)World.MAP_SIZE.SMALL_X || worldY < (int)World.MAP_SIZE.SMALL_Y)
                        {
                            Program.tConsole.WriteLine("World dimensions need to be equal to or larger than " + (int)World.MAP_SIZE.SMALL_X + " by " + (int)World.MAP_SIZE.SMALL_Y + "; using built-in 'small'");
                            worldX = (int)((int)World.MAP_SIZE.SMALL_Y * 3.5);
                            worldY = (int)World.MAP_SIZE.SMALL_Y;
                        }

                        Program.tConsole.WriteLine("Generating World with Custom Map Size { " + worldX.ToString() +
                            ", " + worldY.ToString() + " }");
                    }

                    Server.maxTilesX = worldX;
                    Server.maxTilesY = worldY;
                    Server.tile = new Tile[Server.maxTilesX + 1, Server.maxTilesY + 1];

                    WorldGen.clearWorld();
                    (new Server()).Initialize();
                    if (properties.getUsingCustomGenOpts())
                    {
                        WorldGen.numDungeons = properties.getDungeonAmount();
                        WorldGen.ficount = properties.getFloatingIslandAmount();
                    }
                    else
                    {
                        WorldGen.numDungeons = 1;
                        WorldGen.ficount = (int)((double)Server.maxTilesX * 0.0008); //The Statics one was generating with default values, We want it to use the actual tileX for the world
                    }
                    WorldGen.generateWorld(seed);
                    WorldGen.saveWorld(worldFile, true);
                }

                int worldXtiles = properties.getMapSizes()[0];
                int worldYtiles = properties.getMapSizes()[1];
                if (properties.isUsingCutomTiles())
                {
                    int X = properties.getMaxTilesX();
                    int Y = properties.getMaxTilesY();
                    if (X > 0 && Y > 0)
                    {
                        worldXtiles = X;
                        worldYtiles = Y;
                    }

                    if (worldXtiles < (int)World.MAP_SIZE.SMALL_X || worldYtiles < (int)World.MAP_SIZE.SMALL_Y)
                    {
                        Program.tConsole.WriteLine("World dimensions need to be equal to or larger than " + (int)World.MAP_SIZE.SMALL_X + " by " + (int)World.MAP_SIZE.SMALL_Y + "; using built-in 'small'");
                        worldXtiles = (int)((int)World.MAP_SIZE.SMALL_Y * 3.5);
                        worldYtiles = (int)World.MAP_SIZE.SMALL_Y;
                    }

                    Program.tConsole.WriteLine("Using World with Custom Map Size { " + worldXtiles.ToString() +
                        ", " + worldYtiles.ToString() + " }");
                }

                World world = new World(worldXtiles, worldYtiles);
                world.setSavePath(worldFile);

                server = new Server(world, properties.getMaxPlayers(),
                    Statics.getDataPath + Statics.systemSeperator + "whitelist.txt",
                    Statics.getDataPath + Statics.systemSeperator + "banlist.txt",
                    Statics.getDataPath + Statics.systemSeperator + "oplist.txt");
                server.setOpPassword(properties.getOpPassword());
                server.setPort(properties.getPort());
                server.setIP(properties.getServerIP());
                server.Initialize();

                WorldGen.loadWorld();
                server.StartServer();

                updateThread = new Thread(Program.Updater);

                Statics.IsActive = true;
                while (!Statics.serverStarted) { }

                commandParser = new CommandParser(server);
                Program.tConsole.WriteLine("You can now insert Commands.");
                while (Statics.IsActive)
                {
                    try {
                        string line = Console.ReadLine().Trim().ToLower();
                        if (line.Length > 0)
                        {
                            commandParser.parseConsoleCommand(line, server);
                        }
                    } catch(Exception) {

                    }

                }
                while (Statics.serverStarted) { }
                Program.tConsole.WriteLine("Exiting...");
            }
            catch (Exception e)
            {
                try
                {
                    using (StreamWriter streamWriter = new StreamWriter(Statics.getDataPath + Statics.systemSeperator + "crashlog.txt", true))
                    {
                        streamWriter.WriteLine(DateTime.Now);
                        streamWriter.WriteLine("Crash Log Generated by TDSM #" + Statics.build + " r" + Statics.revision + " for " +
                            Statics.versionNumber + " {" + Statics.currentRelease + "}");
                        streamWriter.WriteLine(e);
                        streamWriter.WriteLine("");
                    }
                    Debug.WriteLine("Server crash: " + DateTime.Now);
                    Program.tConsole.WriteLine(e.Message);
                    Program.tConsole.WriteLine(e.StackTrace);
                    Program.tConsole.WriteLine(e.InnerException.Message);
                    Program.tConsole.WriteLine("");
                    Program.tConsole.WriteLine("Please send crashlog.txt to http://tdsm.org/");
                }
                catch
                {
                    //Program.tConsole.WriteLine("Lol You crashed your crash log, Good work.");
                }
            }
            if (Program.tConsole != null)
            {
                Program.tConsole.Close();
            }
        }
        public static void Main(String[] args)
        {
            try
            {
                String MODInfo = "Terraria's Dedicated Server Mod. (" + VERSION_NUMBER + " {" + Statics.CURRENT_TERRARIA_RELEASE + "}) #"
                    + Statics.BUILD;
                Console.Title = MODInfo;

                Console.WriteLine("Initializing " + MODInfo);

                Console.WriteLine("Setting up Paths.");
                if (!SetupPaths())
                {
                    return;
                }

                Platform.InitPlatform();
                tConsole = new TConsole(Statics.DataPath + Path.DirectorySeparatorChar + "server.log", Platform.Type);

                Program.tConsole.WriteLine("Setting up Properties.");
                bool propertiesExist = File.Exists("server.properties");
                SetupProperties();

                if (!propertiesExist)
                {
                    Console.Write("New properties file created. Would you like to exit for editing? [Y/n]: ");
                    if (Console.ReadLine().ToLower() == "y")
                    {
                        Console.WriteLine("Complete, Press any Key to Exit...");
                        Console.ReadKey(true);
                        return;
                    }
                }

                String PIDFile = properties.PIDFile.ToString();
                if (PIDFile.Length > 0)
                {
                    String ProcessUID = Process.GetCurrentProcess().Id.ToString();
                    bool Issue = false;
                    if (File.Exists(PIDFile))
                    {
                        try
                        {
                            File.Delete(PIDFile);
                        }
                        catch (Exception)
                        {
                            Console.Write("Issue deleting PID file, Continue? [Y/n]: ");
                            if (Console.ReadLine().ToLower() == "n")
                            {
                                Console.WriteLine("Press any Key to Exit...");
                                Console.ReadKey(true);
                                return;
                            }
                            Issue = true;
                        }
                    }
                    if (!Issue)
                    {
                        try
                        {
                            File.WriteAllText(PIDFile, ProcessUID);
                        }
                        catch (Exception)
                        {
                            Console.Write("Issue creating PID file, Continue? [Y/n]: ");
                            if (Console.ReadLine().ToLower() == "n")
                            {
                                Console.WriteLine("Press any Key to Exit...");
                                Console.ReadKey(true);
                                return;
                            }
                        }
                        Console.WriteLine("PID File Created, Process ID: " + ProcessUID);
                    }
                }

                ParseArgs(args);

            #if (DEBUG == false) //I'll comment this for each release, Updates are annoying when testing :3
                try
                {
                    if (UpdateManager.performProcess())
                    {
                        Program.tConsole.WriteLine("Restarting into new update!");
                        return;
                    }
                }
                catch (Exception e)
                {
                    Program.tConsole.WriteLine("Error updating!");
                    Program.tConsole.WriteLine(e.Message);
                }
            #endif

                Program.tConsole.WriteLine("Preparing Server Data...");

                String worldFile = properties.WorldPath;
                FileInfo file = new FileInfo(worldFile);

                if (!file.Exists)
                {
                    try
                    {
                        file.Directory.Create();
                    }
                    catch (Exception exception)
                    {
                        Program.tConsole.WriteLine(exception.ToString());
                        Program.tConsole.WriteLine("Press any key to continue...");
                        Console.ReadKey(true);
                        return;
                    }
                    Program.tConsole.WriteLine("Generating World '" + worldFile + "'");

                    int seed = properties.Seed;
                    if (seed == -1)
                    {
                        Console.Write("Generating Seed...");
                        seed = new Random().Next(100);
                        Console.Write(seed.ToString() + "\n");
                    }

                    int worldX = properties.getMapSizes()[0];
                    int worldY = properties.getMapSizes()[1];
                    if (properties.UseCustomTiles)
                    {
                        int X = properties.MaxTilesX;
                        int Y = properties.MaxTilesY;
                        if (X > 0 && Y > 0)
                        {
                            worldX = X;
                            worldY = Y;
                        }

                        if (worldX < (int)World.MAP_SIZE.SMALL_X || worldY < (int)World.MAP_SIZE.SMALL_Y)
                        {
                            Program.tConsole.WriteLine("World dimensions need to be equal to or larger than " + (int)World.MAP_SIZE.SMALL_X + " by " + (int)World.MAP_SIZE.SMALL_Y + "; using built-in 'small'");
                            worldX = (int)((int)World.MAP_SIZE.SMALL_Y * 3.5);
                            worldY = (int)World.MAP_SIZE.SMALL_Y;
                        }

                        Program.tConsole.WriteLine("Generating World with Custom Map Size { " + worldX.ToString() +
                            ", " + worldY.ToString() + " }");
                    }

                    Server.maxTilesX = worldX;
                    Server.maxTilesY = worldY;

                    Server.tile = new TileCollection(Server.maxTilesX, Server.maxTilesY);

                    WorldGen.clearWorld();
                    (new Server()).Initialize();
                    if (properties.UseCustomGenOpts)
                    {
                        WorldGen.numDungeons = properties.DungeonAmount;
                        WorldGen.ficount = properties.FloatingIslandAmount;
                    }
                    else
                    {
                        WorldGen.numDungeons = 1;
                        WorldGen.ficount = (int)((double)Server.maxTilesX * 0.0008); //The Statics one was generating with default values, We want it to use the actual tileX for the world
                    }
                    WorldGen.generateWorld(seed);
                    WorldGen.saveWorld(worldFile, true);
                }

                int worldXtiles = properties.getMapSizes()[0];
                int worldYtiles = properties.getMapSizes()[1];

                if (properties.UseCustomTiles)
                {
                    int X = properties.MaxTilesX;
                    int Y = properties.MaxTilesY;
                    if (X > 0 && Y > 0)
                    {
                        worldXtiles = X;
                        worldYtiles = Y;
                    }

                    if (worldXtiles < (int)World.MAP_SIZE.SMALL_X || worldYtiles < (int)World.MAP_SIZE.SMALL_Y)
                    {
                        Program.tConsole.WriteLine("World dimensions need to be equal to or larger than " + (int)World.MAP_SIZE.SMALL_X + " by " + (int)World.MAP_SIZE.SMALL_Y + "; using built-in 'small'");
                        worldXtiles = (int)((int)World.MAP_SIZE.SMALL_Y * 3.5);
                        worldYtiles = (int)World.MAP_SIZE.SMALL_Y;
                    }

                    Program.tConsole.WriteLine("Using World with Custom Map Size { " + worldXtiles.ToString() +
                        ", " + worldYtiles.ToString() + " }");
                }

                World world = new World(worldXtiles, worldYtiles);
                world.SavePath = worldFile;

                server = new Server(world, properties.MaxPlayers,
                    Statics.DataPath + Path.DirectorySeparatorChar + "whitelist.txt",
                    Statics.DataPath + Path.DirectorySeparatorChar + "banlist.txt",
                    Statics.DataPath + Path.DirectorySeparatorChar + "oplist.txt");
                server.OpPassword = properties.Password;
                server.Port = properties.Port;
                server.ServerIP = properties.ServerIP;
                server.Initialize();

                Server.tile = new TileCollection(worldXtiles, worldYtiles);
                WorldGen.loadWorld();

                tConsole.WriteLine("Starting the Server");
                server.StartServer();

                updateThread = new Thread(Program.UpdateLoop);
                updateThread.Name = "UpdateLoop";

                Statics.IsActive = true;
                while (!Statics.serverStarted) { }

                commandParser = new CommandParser(server);
                Program.tConsole.WriteLine("You can now insert Commands.");

                while (Statics.IsActive)
                {
                    try
                    {
                        String line = Console.ReadLine().Trim();
                        if (line.Length > 0)
                        {
                            commandParser.parseConsoleCommand(line, server);
                        }
                    }
                    catch (Exception e)
                    {
                        Program.tConsole.WriteLine("Issue parsing Console Command");
                        Program.tConsole.WriteLine(e.ToString());
                    }
                }
                while (Statics.serverStarted) { Thread.Sleep(10); }
                Program.tConsole.WriteLine("Exiting...");
                Program.tConsole.Close();
            }
            catch (Exception e)
            {
                try
                {
                    using (StreamWriter streamWriter = new StreamWriter(Statics.DataPath + Path.DirectorySeparatorChar + "crashlog.txt", true))
                    {
                        streamWriter.WriteLine(DateTime.Now);
                        streamWriter.WriteLine("Crash Log Generated by TDSM #" + Statics.BUILD + " for " + //+ " r" + Statics.revision + " for " +
                            VERSION_NUMBER + " {" + Statics.CURRENT_TERRARIA_RELEASE + "}");
                        streamWriter.WriteLine(e);
                        streamWriter.WriteLine("");
                    }
                    Program.tConsole.WriteLine("Server crash: " + DateTime.Now);
                    Program.tConsole.WriteLine(e.Message);
                    Program.tConsole.WriteLine(e.StackTrace);
                    Program.tConsole.WriteLine(e.InnerException.Message);
                    Program.tConsole.WriteLine("");
                    Program.tConsole.WriteLine("Please send crashlog.txt to http://tdsm.org/");
                }
                catch
                {
                }
            }
            if (Program.tConsole != null)
            {
                Program.tConsole.Close();
            }
        }
        public static void Main(String[] args)
        {
            Thread.CurrentThread.Name = "Main";
            try
            {
                String MODInfo = "Terraria's Dedicated Server Mod. (" + VERSION_NUMBER + " {" + Statics.CURRENT_TERRARIA_RELEASE + "}) #"
                    + Statics.BUILD;
                try
                {
                    Console.Title = MODInfo;
                }
                catch
                {

                }

                var lis = new Logging.LogTraceListener ();
                System.Diagnostics.Trace.Listeners.Clear ();
                System.Diagnostics.Trace.Listeners.Add (lis);
                System.Diagnostics.Debug.Listeners.Clear ();
                System.Diagnostics.Debug.Listeners.Add (lis);

                ProgramLog.Log ("Initializing " + MODInfo);

                ProgramLog.Log ("Setting up Paths.");
                if (!SetupPaths())
                {
                    return;
                }

                Platform.InitPlatform();
                tConsole = new TConsole (null, Platform.Type); //dummy

                ProgramLog.Log ("Setting up Properties.");
                bool propertiesExist = File.Exists("server.properties");
                SetupProperties();

                if (!propertiesExist)
                {
                    ProgramLog.Console.Print ("New properties file created. Would you like to exit for editing? [Y/n]: ");
                    if (Console.ReadLine().ToLower() == "y")
                    {
                        ProgramLog.Console.Print ("Complete, Press any Key to Exit...");
                        Console.ReadKey(true);
                        return;
                    }
                }

                var logFile = Statics.DataPath + Path.DirectorySeparatorChar + "server.log";
                ProgramLog.OpenLogFile (logFile);

                String PIDFile = properties.PIDFile.Trim();
                if (PIDFile.Length > 0)
                {
                    String ProcessUID = Process.GetCurrentProcess().Id.ToString();
                    bool Issue = false;
                    if (File.Exists(PIDFile))
                    {
                        try
                        {
                            File.Delete(PIDFile);
                        }
                        catch (Exception)
                        {
                            ProgramLog.Console.Print ("Issue deleting PID file, Continue? [Y/n]: ");
                            if (Console.ReadLine().ToLower() == "n")
                            {
                                ProgramLog.Console.Print ("Press any Key to Exit...");
                                Console.ReadKey(true);
                                return;
                            }
                            Issue = true;
                        }
                    }
                    if (!Issue)
                    {
                        try
                        {
                            File.WriteAllText(PIDFile, ProcessUID);
                        }
                        catch (Exception)
                        {
                            ProgramLog.Console.Print ("Issue creating PID file, Continue? [Y/n]: ");
                            if (Console.ReadLine().ToLower() == "n")
                            {
                                ProgramLog.Console.Print ("Press any Key to Exit...");
                                Console.ReadKey(true);
                                return;
                            }
                        }
                        ProgramLog.Log ("PID File Created, Process ID: " + ProcessUID);
                    }
                }

                ParseArgs(args);

            //#if (DEBUG == false) //I'll comment this for each release, Updates are annoying when testing :3
                try
                {
                    if (UpdateManager.performProcess())
                    {
                        ProgramLog.Log ("Restarting into new update!");
                        return;
                    }
                }
                catch (UpdateCompleted)
                {
                    throw;
                }
                catch (Exception e)
                {
                    ProgramLog.Log (e, "Error updating");
                }
            //#endif

                LoadMonitor.Start ();

                ProgramLog.Log ("Starting remote console server");
                RemoteConsole.RConServer.Start ("rcon_logins.properties");

                ProgramLog.Log ("Preparing Server Data...");

                using (var prog = new ProgressLogger (1, "Loading item definitions"))
                    Collections.Registries.Item.Load ();
                using (var prog = new ProgressLogger (1, "Loading NPC definitions"))
                    Collections.Registries.NPC.Load (Collections.Registries.NPC_FILE);
                using (var prog = new ProgressLogger (1, "Loading projectile definitions"))
                    Collections.Registries.Projectile.Load (Collections.Registries.PROJECTILE_FILE);

                String worldFile = properties.WorldPath;
                FileInfo file = new FileInfo(worldFile);

                if (!file.Exists)
                {
                    try
                    {
                        file.Directory.Create();
                    }
                    catch (Exception exception)
                    {
                        ProgramLog.Log (exception);
                        ProgramLog.Console.Print ("Press any key to continue...");
                        Console.ReadKey(true);
                        return;
                    }
                    ProgramLog.Log ("Generating World '{0}'", worldFile);

                    String seed = properties.Seed;
                    if (seed == "-1")
                    {
                        seed = new Random().Next(100).ToString();
                        ProgramLog.Log ("Generated seed: {0}", seed);
                    }

                    int worldX = properties.getMapSizes()[0];
                    int worldY = properties.getMapSizes()[1];
                    if (properties.UseCustomTiles)
                    {
                        int X = properties.MaxTilesX;
                        int Y = properties.MaxTilesY;
                        if (X > 0 && Y > 0)
                        {
                            worldX = X;
                            worldY = Y;
                        }

                        if (worldX < (int)World.MAP_SIZE.SMALL_X || worldY < (int)World.MAP_SIZE.SMALL_Y)
                        {
                            ProgramLog.Log ("World dimensions need to be equal to or larger than {0} by {1}; using built-in 'small'", (int)World.MAP_SIZE.SMALL_X, (int)World.MAP_SIZE.SMALL_Y);
                            worldX = (int)((int)World.MAP_SIZE.SMALL_Y * 3.5);
                            worldY = (int)World.MAP_SIZE.SMALL_Y;
                        }

                        ProgramLog.Log ("Generating world with custom map size: {0}x{1}", worldX, worldY);
                    }

                    Server.maxTilesX = worldX;
                    Server.maxTilesY = worldY;

                    WorldIO.clearWorld();
                    (new Server()).Initialize();
                    if (properties.UseCustomGenOpts)
                    {
                        WorldGen.numDungeons = properties.DungeonAmount;
                        WorldModify.ficount = properties.FloatingIslandAmount;
                    }
                    else
                    {
                        WorldGen.numDungeons = 1;
                        WorldModify.ficount = (int)((double)Server.maxTilesX * 0.0008); //The Statics one was generating with default values, We want it to use the actual tileX for the world
                    }
                    WorldGen.GenerateWorld(seed);
                    WorldIO.saveWorld(worldFile, true);
                }

                // TODO: read map size from world file instead of config
                int worldXtiles = properties.getMapSizes()[0];
                int worldYtiles = properties.getMapSizes()[1];

                if (properties.UseCustomTiles)
                {
                    int X = properties.MaxTilesX;
                    int Y = properties.MaxTilesY;
                    if (X > 0 && Y > 0)
                    {
                        worldXtiles = X;
                        worldYtiles = Y;
                    }

                    if (worldXtiles < (int)World.MAP_SIZE.SMALL_X || worldYtiles < (int)World.MAP_SIZE.SMALL_Y)
                    {
                        ProgramLog.Log ("World dimensions need to be equal to or larger than {0} by {1}; using built-in 'small'", (int)World.MAP_SIZE.SMALL_X, (int)World.MAP_SIZE.SMALL_Y);
                        worldXtiles = (int)((int)World.MAP_SIZE.SMALL_Y * 3.5);
                        worldYtiles = (int)World.MAP_SIZE.SMALL_Y;
                    }

                    ProgramLog.Log ("Using world with custom map size: {0}x{1}", worldXtiles, worldYtiles);
                }

                World world = new World(worldXtiles, worldYtiles);
                world.SavePath = worldFile;

                server = new Server(world, properties.MaxPlayers,
                    Statics.DataPath + Path.DirectorySeparatorChar + "whitelist.txt",
                    Statics.DataPath + Path.DirectorySeparatorChar + "banlist.txt",
                    Statics.DataPath + Path.DirectorySeparatorChar + "oplist.txt");
                server.OpPassword = properties.Password;
                server.Port = properties.Port;
                server.ServerIP = properties.ServerIP;
                server.Initialize();

                Server.maxTilesX = worldXtiles;
                Server.maxTilesY = worldYtiles;
                Server.maxSectionsX = worldXtiles / 200;
                Server.maxSectionsY = worldYtiles / 150;

                WorldIO.loadWorld();

                updateThread = new ProgramThread ("Updt", Program.UpdateLoop);

                ProgramLog.Log ("Starting the Server");
                server.StartServer();

                Statics.IsActive = true;
                while (!Statics.serverStarted) { }

                commandParser = new CommandParser(server);
                ProgramLog.Console.Print ("You can now insert Commands.");

                while (Statics.IsActive)
                {
                    try
                    {
                        String line = Console.ReadLine().Trim();
                        if (line.Length > 0)
                        {
                            commandParser.ParseConsoleCommand(line, server);
                        }
                    }
                    catch (Exception e)
                    {
                        ProgramLog.Log (e, "Issue parsing console command");
                    }
                }
                while (Statics.serverStarted) { Thread.Sleep(10); }
                ProgramLog.Log ("Exiting...");
                Program.tConsole.Close();
            }
            catch (UpdateCompleted)
            {
            }
            catch (Exception e)
            {
                try
                {
                    using (StreamWriter streamWriter = new StreamWriter(Statics.DataPath + Path.DirectorySeparatorChar + "crashlog.txt", true))
                    {
                        streamWriter.WriteLine(DateTime.Now);
                        streamWriter.WriteLine("Crash Log Generated by TDSM #" + Statics.BUILD + " for " + //+ " r" + Statics.revision + " for " +
                            VERSION_NUMBER + " {" + Statics.CURRENT_TERRARIA_RELEASE + "}");
                        streamWriter.WriteLine(e);
                        streamWriter.WriteLine("");
                    }
                    ProgramLog.Log(e, "Program crash");
                    ProgramLog.Log("Please send crashlog.txt to http://tdsm.org/");
                }
                catch
                {
                }
            }

            if (File.Exists(properties.PIDFile.Trim()))
            {
                File.Delete(properties.PIDFile.Trim());
            }

            if (Program.tConsole != null)
            {
                Program.tConsole.Close();
            }
            ProgramLog.Log ("Log end.");
            ProgramLog.Close();

            RemoteConsole.RConServer.Stop ();
        }
示例#13
0
        static Delegate CreateDelegate(string value)
        {
            if (value.ToLower().StartsWith("code:"))
            {
                // compile with CodeDom, return Action<UIConstructor>

                string source =
                    #region source
                    @"using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Xact;
using PoroCYon.XnaExtensions;
using TAPI.SDK.GUI.Controls;

namespace TAPI.SDK.GUI
{
    public static class UIConstructorScript
    {
        public static Action<UIConstructor> ScriptAction
        {
            get
            {
                return ExecuteScript;
            }
        }

        static void ExecuteScript(UIConstructor ui)
        {
            " + value + @"
        }
    }
}
";
                #endregion
                CodeDomProvider    cdp = new CSharpCodeProvider();
                CompilerParameters cp  = new CompilerParameters()
                {
                    GenerateExecutable      = false,
                    GenerateInMemory        = true,
                    CompilerOptions         = "/optimize",
                    IncludeDebugInformation = false,
                    OutputAssembly          = "TAPI.SDK.GUI.ScriptAssembly"
                };
                cp.ReferencedAssemblies.AddRange(new string[]
                {
                    "mscorlib.dll", "System.dll", "System.Core.dll", "System.Numerics.dll",
                    "Microsoft.Xna.Framework.dll", "Microsoft.Xna.Framework.Game.dll", "Microsoft.Xna.Framework.Graphics.dll", "Microsoft.Xna.Framework.Xact.dll",
                    "PoroCYon.XnaExtensions.dll", "tAPI.exe", "TAPI.SDK.dll", Assembly.GetCallingAssembly().FullName
                });

                CompilerResults cr = cdp.CompileAssemblyFromSource(cp, source);

                if (cr.Errors.HasErrors)
                {
                    for (int i = 0; i < cr.Errors.Count; i++)
                    {
                        TConsole.Print(cr.Errors[i]);
                    }

                    return(null);
                }

                return((Action <XmlUIConstructor>)cr.CompiledAssembly
                       .GetType("TAPI.SDK.GUI.UIConstructorScript", true, true)
                       .GetProperty("ScriptAction", BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Static)
                       .GetValue(null, null));
            }

            string[] split  = value.Split('.');
            string   @class = split[0];
            for (int i = 1; i < split.Length; i++)
            {
                @class += "." + split[i];
            }
            string method = split[split.Length - 1];

            Type t = Type.GetType(@class, false, true);
            if (t == null)
            {
                return(null);
            }

            MethodInfo mi = t.GetMethod(method, BindingFlags.IgnoreCase | BindingFlags.Static);
            if (mi == null)
            {
                return(null);
            }

            return(Delegate.CreateDelegate(Expression.GetDelegateType((from p in mi.GetParameters() select p.ParameterType)
                                                                      .Concat(new Type[1] {
                mi.ReturnType
            }).ToArray()), null, method, true, false));
        }