private static void UpdateServer()
        {
            NetplayCounter++;
            if (NetplayCounter > 3600)
            {
                NetMessage.SendData(7);
                NetMessage.SyncNPCHomes();
                //NetMessage.SyncPlayers();
                NetplayCounter = 0;
            }
            if (NetplayCounter % 30 == 0)
            {
                for (int i = 0; i < 255; i++)
                {
                    var player = players[i];
                    var rows   = player.rowsToRectify;
                    if (player.Active && rows.Count > 0)
                    {
                        bool locked = false;
                        try
                        {
                            locked = Monitor.TryEnter(rows);
                            if (!locked)
                            {
                                //ProgramLog.Debug.Log ("not acquired!");
                                continue;
                            }

                            var conn = player.Connection;
                            if (conn == null)
                            {
                                continue;
                            }

                            int Y1 = int.MaxValue;
                            int Y2 = 0;
                            int X1 = int.MaxValue;
                            int X2 = 0;

                            var  msg  = NetMessage.PrepareThreadInstance();
                            bool warn = false;

                            if (rows.Count <= 150)
                            {
                                foreach (var kv in rows)
                                {
                                    var y  = kv.Key;
                                    var x1 = (int)(kv.Value >> 16);
                                    var x2 = (int)(kv.Value & 0xffff);

                                    if (x1 > x2)
                                    {
                                        continue;
                                    }

                                    var len = x2 - x1 + 1;

                                    if (len > 200)
                                    {
                                        warn = true;
                                        break;
                                    }

                                    if (y < Y1)
                                    {
                                        Y1 = y;
                                    }
                                    if (y > Y2)
                                    {
                                        Y2 = y;
                                    }
                                    if (x1 < X1)
                                    {
                                        X1 = x1;
                                    }
                                    if (x2 > X2)
                                    {
                                        X2 = x2;
                                    }

                                    if (conn.CompressionVersion == 1)
                                    {
                                        msg.TileRowCompressed(len, x1, y);
                                    }
                                    else
                                    {
                                        msg.SendTileRow(len, x1, y);
                                    }
                                }
                            }
                            else
                            {
                                warn = true;
                            }

                            rows.Clear();

                            if (warn)
                            {
                                msg.Clear();
                                msg.PlayerChat(255, "<Server> Your view of the map is out of sync.", 255, 128, 128);
                                msg.Send(conn);
                            }
                            else if (msg.Written > 0)
                            {
                                msg.SendTileConfirm(X1 / 200, Y1 / 150, X2 / 200, Y2 / 150);
                                msg.Send(conn);
                            }
                        }
                        finally
                        {
                            if (locked)
                            {
                                Monitor.Exit(rows);
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < 255; i++)
            {
                if (players[i].Active && NetPlay.slots[i].state >= SlotState.CONNECTED)
                {
                    NetPlay.slots[i].SpamUpdate();
                }
            }

            Math.IEEERemainder((double)NetplayCounter, 60.0);
            if (Math.IEEERemainder((double)NetplayCounter, 360.0) == 0.0)
            {
                bool flag2 = true;
                int  num   = lastItemUpdate;
                int  num2  = 0;
                while (flag2)
                {
                    num++;
                    if (num >= 200)
                    {
                        num = 0;
                    }
                    num2++;
                    if (!item[num].Active || item[num].Owner == 255)
                    {
                        NetMessage.SendData(21, -1, -1, "", num);
                    }
                    if (num2 >= maxItemUpdates || num == lastItemUpdate)
                    {
                        flag2 = false;
                    }
                }
                lastItemUpdate = num;
            }

            for (int i = 0; i < 200; i++)
            {
                if (item[i].Active && (item[i].Owner == 255 || !players[item[i].Owner].Active))
                {
                    item[i].FindOwner(i);
                }
            }

            for (int i = 0; i < 255; i++)
            {
                //                if (Netplay.slots[i].state >= SlotState.CONNECTED)
                //                {
                //                    //Netplay.slots[i].timeOut++;
                //                    if (/*!stopTimeOuts && */Netplay.slots[i].timeOut > 60 * timeOut)
                //                    {
                //                        Netplay.slots[i].Kick ("Timed out.");
                //                    }
                //                }

                Player player = players[i];
                if (player.Active)
                {
                    int sectionX = NetPlay.GetSectionX((int)(player.Position.X / 16f));
                    int sectionY = NetPlay.GetSectionY((int)(player.Position.Y / 16f));
                    int num3     = 0;
                    for (int j = sectionX - 1; j < sectionX + 2; j++)
                    {
                        for (int k = sectionY - 1; k < sectionY + 2; k++)
                        {
                            if (j >= 0 && j < maxSectionsX && k >= 0 && k < maxSectionsY)
                            {
                                if (!NetPlay.slots[i].tileSection[j, k])
                                {
                                    num3++;
                                }
                            }
                        }
                    }

                    if (num3 > 0)
                    {
                        int num4 = num3 * 150;
                        NetMessage.SendData(9, i, -1, "Receiving tile data", num4);
                        NetPlay.slots[i].statusText2 = "is receiving tile data";
                        NetPlay.slots[i].statusMax  += num4;
                        for (int j = sectionX - 1; j < sectionX + 2; j++)
                        {
                            for (int k = sectionY - 1; k < sectionY + 2; k++)
                            {
                                if (j >= 0 && j < maxSectionsX && k >= 0 && k < maxSectionsY)
                                {
                                    if (!NetPlay.slots[i].tileSection[j, k])
                                    {
                                        NetMessage.SendSection(i, j, k);
                                        NetMessage.SendData(11, i, -1, "", j, (float)k, (float)j, (float)k);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        var conn = player.Connection;
                        if (conn != null)
                        {
                            conn.Flush();
                        }
                    }
                }
                else
                {
                    var conn = player.Connection;
                    if (conn != null)
                    {
                        conn.Flush();
                    }
                }
            }
        }
        public static void Initialize()
        {
            if (Program.properties != null)
            {
                stopSpawns     = Program.properties.StopNPCSpawning;
                SpawnsOverride = Program.properties.NPCSpawnsOverride;
            }

            NPC.ClearNames();
            NPC.SetNames();

            //foreach (var buff in new int[] { 20, 24, 31, 39 })
            //    pvpBuff[buff] = true;

            foreach (var buff in new int[] { 20, 21, 22, 23, 24, 25, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 })
            {
                debuff[buff] = true;
            }

            foreach (var i in new int[] { 3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 24, 26, 27, 28, 29,
                                          31, 33, 34, 35, 36, 42, 50, 55, 61, 71, 72, 73, 74, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89,
                                          90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 110, 113, 114, 125, 126,
                                          128, 129, 132, 133, 134, 135, 136, 137, 138, 139, 141, 142, 143, 144, 149 })
            {
                tileFrameImportant[i] = true;
            }

            foreach (var i in new int[] { 63, 64, 65, 66, 67, 68, 130, 131 })
            {
                tileStone[i] = true;
            }

            foreach (var i in new int[] { 0, 1, 2, 6, 7, 8, 9, 10, 22, 23, 25, 30, 32, 37, 38, 39, 40, 41, 43, 44, 45,
                                          46, 47, 48, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 70, 75, 76, 107, 108, 109, 111, 112,
                                          115, 116, 117, 118, 119, 120, 121, 122, 123, 130, 131, 137, 138, 140, 145, 146, 147, 148 })
            {
                tileBlockLight[i] = true;
            }

            foreach (var i in new int[] { 0, 1, 2, 6, 7, 8, 9, 10, 19, 22, 23, 25, 30, 37, 38, 39, 40, 41, 43, 44, 45, 46,
                                          47, 48, 53, 54, 56, 57, 58, 59, 60, 63, 64, 65, 66, 67, 68, 70, 75, 76, 107, 108, 109, 111, 112, 116, 117,
                                          118, 119, 120, 121, 122, 123, 127, 130, 137, 138, 140, 145, 146, 147, 148 })
            {
                tileSolid[i] = true;
            }

            foreach (var i in new int[] { 1, 6, 7, 8, 9, 22, 25, 30, 37, 38, 39, 40, 41, 43, 44, 45, 46, 47, 53, 56, 107,
                                          108, 111, 112, 116, 117, 118, 119, 120, 121, 122, 123, 140, 145, 146, 147, 148 })
            {
                tileMergeDirt[i] = true;
            }

            foreach (var i in new int[] { 3, 24, 28, 32, 51, 52, 61, 62, 69, 71, 73, 74, 82, 83, 84, 110, 113, 115 })
            {
                tileCut[i] = true;
            }

            foreach (var i in new int[] { 3, 4, 24, 32, 50, 61, 69, 73, 74, 82, 83, 84, 110, 113 })
            {
                tileNoFail[i] = true;
            }

            foreach (var i in new int[] { 1, 4, 5, 6, 10, 11, 12, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31 })
            {
                wallHouse[i] = true;
            }

            foreach (var i in new int[] { 82, 83, 84 })
            {
                tileAlch[i] = true;
            }

            foreach (var i in new int[] { 3, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 27, 28, 29, 32, 33, 34, 35, 36,
                                          42, 49, 50, 52, 55, 61, 62, 69, 71, 72, 73, 74, 79, 80, 81, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98,
                                          99, 100, 101, 102, 103, 104, 106, 110, 113, 115, 125, 126 })
            {
                tileLavaDeath[i] = true;
            }

            foreach (var i in new int[] { 3, 4, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 27, 50, 86, 87, 88, 89, 90, 91, 92,
                                          93, 94, 95, 96, 97, 98, 99, 101, 102, 110, 114 })
            {
                tileNoAttach[i] = true;
            }

            foreach (var i in new int[] { 41, 43, 44 })
            {
                tileDungeon[i] = true;
            }

            foreach (var i in new int[] { 14, 18, 19, 87, 88, 101, 114 })
            {
                tileTable[i] = true;
            }

            foreach (var i in new int[] { 4, 51, 93, 98 })
            {
                tileWaterDeath[i] = true;
            }


            foreach (var i in new int[] { 8, 12 })
            {
                tileShine[i] = 1000;
            }
            foreach (var i in new int[] { 9 })
            {
                tileShine[i] = 1050;
            }
            foreach (var i in new int[] { 7 })
            {
                tileShine[i] = 1100;
            }
            foreach (var i in new int[] { 6, 22 })
            {
                tileShine[i] = 1150;
            }
            foreach (var i in new int[] { 21 })
            {
                tileShine[i] = 1200;
            }
            foreach (var i in new int[] { 122 })
            {
                tileShine[i] = 1800;
            }
            foreach (var i in new int[] { 121 })
            {
                tileShine[i] = 1850;
            }
            foreach (var i in new int[] { 45 })
            {
                tileShine[i] = 1900;
            }
            foreach (var i in new int[] { 46 })
            {
                tileShine[i] = 2000;
            }
            foreach (var i in new int[] { 47 })
            {
                tileShine[i] = 2100;
            }
            foreach (var i in new int[] { 63, 64, 65, 66, 67, 68, 108 })
            {
                tileShine[i] = 900;
            }
            foreach (var i in new int[] { 111 })
            {
                tileShine[i] = 850;
            }
            foreach (var i in new int[] { 107 })
            {
                tileShine[i] = 950;
            }
            foreach (var i in new int[] { 129 })
            {
                tileShine[i] = 300;
            }
            foreach (var i in new int[] { 109, 110, 117, 116 })
            {
                tileShine[i] = 9000;
            }
            foreach (var i in new int[] { 118 })
            {
                tileShine[i] = 8000;
            }
            foreach (var i in new int[] { 125 })
            {
                tileShine[i] = 600;
            }


            foreach (var i in new int[] { 14, 16, 18, 19, 87, 88, 101, 114 })
            {
                tileSolidTop[i] = true;
            }


            for (int l = 0; l < Item.MAX_ITEMS + 1; l++)
            {
                item[l] = new Item();
            }
            for (int m = 0; m < NPC.MAX_NPCS + 1; m++)
            {
                npcs[m]        = new NPC();
                npcs[m].whoAmI = m;
            }
            for (int i = 0; i < MAX_PLAYERS + 1; i++)
            {
                players[i] = new Player();
            }
            for (int num2 = 0; num2 < 1001; num2++)
            {
                projectile[num2] = new Projectile();
            }
            for (int num10 = 0; num10 < Liquid.resLiquid; num10++)
            {
                liquid[num10] = new Liquid();
            }
            for (int num11 = 0; num11 < 10000; num11++)
            {
                liquidBuffer[num11] = new LiquidBuffer();
            }

            teamColor[0] = new Color(255, 255, 255);
            teamColor[1] = new Color(230, 40, 20);
            teamColor[2] = new Color(20, 200, 30);
            teamColor[3] = new Color(75, 90, 255);
            teamColor[4] = new Color(200, 180, 0);

            NetPlay.Init();
        }
Ejemplo n.º 3
0
        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();

                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);

                try
                {
                    if (UpdateManager.performProcess())
                    {
                        ProgramLog.Log("Restarting into new update!");
                        return;
                    }
                }
                catch (UpdateCompleted)
                {
                    throw;
                }
                catch (Exception e)
                {
                    ProgramLog.Log(e, "Error updating");
                }

                LoadMonitor.Start();

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

                ProgramLog.Log("Starting permissions manager");
                permissionManager = new PermissionManager();

                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);

                commandParser = new CommandParser();
                commandParser.ReadPermissionNodes();

                ProgramLog.Log("Loading plugins...");
                Terraria_Server.Plugins.PluginManager.Initialize(Statics.PluginPath, Statics.LibrariesPath);

                var ctx = new HookContext()
                {
                    Sender = new ConsoleSender()
                };

                var eArgs = new HookArgs.ServerStateChange()
                {
                    ServerChangeState = ServerState.INITIALIZING
                };

                HookPoints.ServerStateChange.Invoke(ref ctx, ref eArgs);
                PluginManager.LoadPlugins();
                ProgramLog.Log("Plugins loaded: " + PluginManager.PluginCount);

                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;
                    }

                    ctx = new HookContext
                    {
                        Sender = World.Sender,
                    };

                    eArgs = new HookArgs.ServerStateChange
                    {
                        ServerChangeState = ServerState.GENERATING
                    };

                    HookPoints.ServerStateChange.Invoke(ref ctx, ref eArgs);

                    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);
                    }

                    Terraria_Server.Main.maxTilesX = worldX;
                    Terraria_Server.Main.maxTilesY = worldY;

                    WorldIO.clearWorld();
                    Terraria_Server.Main.Initialize();
                    if (properties.UseCustomGenOpts)
                    {
                        WorldGen.numDungeons = properties.DungeonAmount;
                        WorldModify.ficount  = properties.FloatingIslandAmount;
                    }
                    else
                    {
                        WorldGen.numDungeons = 1;
                        WorldModify.ficount  = (int)((double)Terraria_Server.Main.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);
                }

                ctx = new HookContext
                {
                    Sender = World.Sender,
                };

                eArgs = new HookArgs.ServerStateChange
                {
                    ServerChangeState = ServerState.LOADING
                };

                HookPoints.ServerStateChange.Invoke(ref ctx, ref eArgs);

                // 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.InitializeData(world, properties.MaxPlayers,
                                      Statics.DataPath + Path.DirectorySeparatorChar + "whitelist.txt",
                                      Statics.DataPath + Path.DirectorySeparatorChar + "banlist.txt",
                                      Statics.DataPath + Path.DirectorySeparatorChar + "oplist.txt");
                NetPlay.password   = properties.Password;
                NetPlay.serverPort = properties.Port;
                NetPlay.serverSIP  = properties.ServerIP;
                Terraria_Server.Main.Initialize();

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

                WorldIO.LoadWorld(Server.World.SavePath);

                ctx = new HookContext
                {
                    Sender = World.Sender,
                };

                eArgs = new HookArgs.ServerStateChange
                {
                    ServerChangeState = ServerState.LOADED
                };

                HookPoints.ServerStateChange.Invoke(ref ctx, ref eArgs);

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

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

                while (!NetPlay.ServerUp)
                {
                }

                ProgramLog.Console.Print("You can now insert Commands.");

                while (!Statics.Exit)
                {
                    try
                    {
                        string line = Console.ReadLine();
                        if (line.Length > 0)
                        {
                            commandParser.ParseConsoleCommand(line);
                        }
                    }
                    catch (ExitException e)
                    {
                        ProgramLog.Log(e.Message);
                        break;
                    }
                    catch (Exception e)
                    {
                        ProgramLog.Log(e, "Issue parsing console command");
                    }
                }

                while (WorldModify.saveLock || NetPlay.ServerUp)
                {
                    Thread.Sleep(100);
                }

                ProgramLog.Log("Exiting...");
                Thread.Sleep(1000);
            }
            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());
            }

            Thread.Sleep(500);
            ProgramLog.Log("Log end.");
            ProgramLog.Close();

            RemoteConsole.RConServer.Stop();
        }
Ejemplo n.º 4
0
        public static void Initialize()
        {
            //if (Main.webProtect)
            //{
            //    getAuth();
            //    while (!Main.webAuth)
            //    {
            //        Statics.IsActive = false;
            //    }
            //}

            Main.stopSpawns = Program.properties.StopNPCSpawning;

            Main.tileShine[6]  = 1150;
            Main.tileShine[7]  = 1100;
            Main.tileShine[8]  = 1000;
            Main.tileShine[9]  = 1050;
            Main.tileShine[12] = 1000;
            Main.tileShine[21] = 1000;
            Main.tileShine[22] = 1150;
            Main.tileShine[45] = 1900;
            Main.tileShine[46] = 2000;
            Main.tileShine[47] = 2100;
            Main.tileShine[63] = 900;
            Main.tileShine[64] = 900;
            Main.tileShine[65] = 900;
            Main.tileShine[66] = 900;
            Main.tileShine[67] = 900;
            Main.tileShine[68] = 900;

            foreach (int i in new int[] { 20, 21, 22, 23, 24, 25 })
            {
                Main.debuff[i] = true;
            }

            foreach (int i in new int[] { 3, 24, 28, 32, 51, 52, 61, 62, 69, 71, 73, 74, 82, 83, 84 })
            {
                Main.tileCut[i] = true;
            }

            foreach (int i in new int[] { 82, 83, 84 })
            {
                Main.tileAlch[i] = true;
            }

            foreach (int i in new int[] { 3, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 24, 26, 27, 28, 29,
                                          31, 33, 34, 35, 36, 42, 50, 55, 61, 71, 72, 73, 74, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88,
                                          89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 })
            {
                Main.tileFrameImportant[i] = true;
            }

            foreach (int i in new int[] { 3, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 27, 28, 29, 32, 33,
                                          34, 35, 36, 42, 49, 50, 52, 55, 61, 62, 69, 71, 72, 73, 74, 79, 80, 81, 86, 87, 88, 89, 91, 92,
                                          93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 })
            {
                Main.tileLavaDeath[i] = true;
            }

            foreach (int i in new int[] { 0, 1, 2, 6, 7, 8, 9, 10, 22, 23, 25, 30, 32, 37, 38, 39, 40, 41, 43, 44,
                                          45, 46, 47, 48, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 70, 75, 76 })
            {
                Main.tileBlockLight[i] = true;
            }

            foreach (int i in new int[] { 14, 16, 18, 19, 87, 88, 101 })
            {
                Main.tileSolidTop[i] = true;
            }

            foreach (int i in new int[] { 3, 4, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 27, 50, 86, 87, 88, 89,
                                          90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 101, 102 })
            {
                Main.tileNoAttach[i] = true;
            }

            foreach (int i in new int[] { 14, 18, 19, 87, 88, 101 })
            {
                Main.tileTable[i] = true;
            }

            foreach (int i in new int[] { 4, 51, 93, 98 })
            {
                Main.tileWaterDeath[i] = true;
            }

            foreach (int i in new int[] { 1, 4, 5, 6, 10, 11, 12, 16, 17, 18, 19, 20 })
            {
                Main.wallHouse[i] = true;
            }

            foreach (int i in new int[] { 3, 4, 24, 32, 50, 61, 69, 73, 74, 82, 83, 84 })
            {
                Main.tileNoFail[i] = true;
            }

            foreach (int i in new int[] { 0, 1, 2, 6, 7, 8, 9, 10, 19, 22, 23, 25, 30, 37, 38, 39, 40, 41, 43, 44, 45, 46,
                                          47, 48, 53, 54, 56, 57, 58, 59, 60, 63, 64, 65, 66, 67, 68, 70, 75, 76 })
            {
                Main.tileSolid[i] = true;
            }

            foreach (int i in new int[] { 3, 4, 5, 11 })
            {
                Main.tileSolid[i] = false;
            }

            foreach (int i in new int[] { 63, 64, 65, 66, 67, 68 })
            {
                Main.tileStone[i] = false;
            }

            foreach (int i in new int[] { 41, 43, 44 })
            {
                Main.tileDungeon[i] = true;
            }
            for (int l = 0; l < 201; l++)
            {
                Main.item[l] = new Item();
            }
            for (int m = 0; m < NPC.MAX_NPCS + 1; m++)
            {
                Main.npcs[m]        = new NPC();
                Main.npcs[m].whoAmI = m;
            }
            for (int i = 0; i < MAX_PLAYERS + 1; i++)
            {
                Main.players[i] = new Player();
            }
            for (int num2 = 0; num2 < 1001; num2++)
            {
                Main.projectile[num2] = new Projectile();
            }
            for (int num10 = 0; num10 < Liquid.resLiquid; num10++)
            {
                Main.liquid[num10] = new Liquid();
            }
            for (int num11 = 0; num11 < 10000; num11++)
            {
                Main.liquidBuffer[num11] = new LiquidBuffer();
            }

            Main.teamColor[0] = new Color(255, 255, 255);
            Main.teamColor[1] = new Color(230, 40, 20);
            Main.teamColor[2] = new Color(20, 200, 30);
            Main.teamColor[3] = new Color(75, 90, 255);
            Main.teamColor[4] = new Color(200, 180, 0);

            NPC.SetNames();

            NetPlay.Init();
        }
        public static void Main(string[] args)
        {
            Thread.CurrentThread.Name = "Main";

            //header: Terraria's Dedicated Server Mod. (1.1.2 #36) ~ Build: 37 [CodeName]
            string codeName = Statics.CODENAME.Length > 0 ? String.Format(" [{0}]", Statics.CODENAME) : String.Empty;
            string MODInfo  = String.Format(
                "Terraria's Dedicated Server Mod. ({0} #{1}) ~ Build: {2}{3}",
                Statics.VERSION_NUMBER,
                Statics.CURRENT_TERRARIA_RELEASE,
                Statics.BUILD,
                codeName
                );

            try
            {
                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);

                using (var prog = new ProgressLogger(1, "Loading language definitions"))
                    Languages.LoadClass(Collections.Registries.LANGUAGE_FILE);

                if (Languages.Startup_Initializing == null)
                {
                    ProgramLog.Error.Log("Please update the language file, either by deleting or finding another online.");
                    Console.ReadKey(true);
                    return;
                }

                ProgramLog.Log("{0} {1}", Languages.Startup_Initializing, MODInfo);

                ProgramLog.Log(Languages.Startup_SettingUpPaths);
                if (!SetupPaths())
                {
                    return;
                }

                Platform.InitPlatform();

                ProgramLog.Log(Languages.Startup_SettingUpProperties);
                bool propertiesExist = File.Exists("server.properties");
                SetupProperties();

                if (!propertiesExist)
                {
                    ProgramLog.Console.Print(Languages.Startup_NoPropertiesFileFound);
                    if (Console.ReadLine().ToLower() == "y")
                    {
                        //ProgramLog.Console.Print(Languages.Startup_PropertiesCreationComplete);
                        ProgramLog.Log(Languages.ExitRequestCommand);
                        //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(Languages.Startup_IssueDeletingPID);
                            if (Console.ReadLine().ToLower() == "n")
                            {
                                ProgramLog.Console.Print(Languages.Startup_PressAnyKeyToExit);
                                Console.ReadKey(true);
                                return;
                            }
                            Issue = true;
                        }
                    }
                    if (!Issue)
                    {
                        try
                        {
                            File.WriteAllText(PIDFile, ProcessUID);
                        }
                        catch (Exception)
                        {
                            ProgramLog.Console.Print(Languages.Startup_IssueCreatingPID);
                            if (Console.ReadLine().ToLower() == "n")
                            {
                                ProgramLog.Console.Print(Languages.Startup_PressAnyKeyToExit);
                                Console.ReadKey(true);
                                return;
                            }
                        }
                        ProgramLog.Log(Languages.Startup_PIDCreated + ProcessUID);
                    }
                }

                ParseArgs(args);

                try
                {
                    if (UpdateManager.PerformProcess())
                    {
                        ProgramLog.Log(Languages.Startup_RestartingIntoNewUpdate);
                        return;
                    }
                }
                catch (UpdateCompleted)
                {
                    throw;
                }
                catch (Exception e)
                {
                    ProgramLog.Log(e, Languages.Startup_ErrorUpdating);
                }

                LoadMonitor.Start();

                ProgramLog.Log(Languages.Startup_StartingRCON);
                RemoteConsole.RConServer.Start("rcon_logins.properties");

                ProgramLog.Log(Languages.Startup_StartingPermissions);
                permissionManager = new PermissionManager();

                ProgramLog.Log(Languages.Startup_PreparingServerData);

                using (var prog = new ProgressLogger(1, Languages.Startup_LoadingItemDefinitions))
                    Collections.Registries.Item.Load();
                using (var prog = new ProgressLogger(1, Languages.Startup_LoadingNPCDefinitions))
                    Collections.Registries.NPC.Load(Collections.Registries.NPC_FILE);
                using (var prog = new ProgressLogger(1, Languages.Startup_LoadingProjectileDefinitions))
                    Collections.Registries.Projectile.Load(Collections.Registries.PROJECTILE_FILE);

                //if (Languages.IsOutOfDate())
                //    ProgramLog.Error.Log(
                //        String.Format("{0}\n{1}",
                //        Languages.Startup_LanguageFileOOD, Languages.Startup_LanguageFileUpdate)
                //        , true);

                commandParser = new CommandParser();
                commandParser.ReadPermissionNodes();

                LoadPlugins();

                /* Save access languages - once only */
                Languages.Save(Collections.Registries.LANGUAGE_FILE);

                HookContext ctx;
                HookArgs.ServerStateChange eArgs;

                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(Languages.Startup_PressAnyKeyToExit);
                        Console.ReadKey(true);
                        return;
                    }

                    ctx = new HookContext
                    {
                        Sender = World.Sender,
                    };

                    eArgs = new HookArgs.ServerStateChange
                    {
                        ServerChangeState = ServerState.GENERATING
                    };

                    HookPoints.ServerStateChange.Invoke(ref ctx, ref eArgs);

                    ProgramLog.Log("{0} '{1}'", Languages.Startup_GeneratingWorld, worldFile);

                    string seed = properties.Seed;
                    if (seed == "-1")
                    {
                        seed = WorldModify.genRand.Next(Int32.MaxValue).ToString();
                        ProgramLog.Log("{0} {1}", Languages.Startup_GeneratedSeed, 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("{0} {1}x{2}", Languages.Startup_WorldSizingError, (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("{0} {1}x{2}", Languages.Startup_GeneratingWithCustomSize, worldX, worldY);
                    }

                    Terraria_Server.Main.maxTilesX = worldX;
                    Terraria_Server.Main.maxTilesY = worldY;

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

                ctx = new HookContext
                {
                    Sender = World.Sender,
                };

                eArgs = new HookArgs.ServerStateChange
                {
                    ServerChangeState = ServerState.LOADING
                };

                HookPoints.ServerStateChange.Invoke(ref ctx, ref eArgs);

                // 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("{0} {1}x{2}", Languages.Startup_WorldSizingError, (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("{0} {1}x{2}", Languages.Startup_GeneratingWithCustomSize, worldXtiles, worldXtiles);
                }

                World.SavePath = worldFile;

                Server.InitializeData(properties.MaxPlayers,
                                      Statics.DataPath + Path.DirectorySeparatorChar + "whitelist.txt",
                                      Statics.DataPath + Path.DirectorySeparatorChar + "banlist.txt",
                                      Statics.DataPath + Path.DirectorySeparatorChar + "oplist.txt");
                NetPlay.password   = properties.Password;
                NetPlay.serverPort = properties.Port;
                NetPlay.serverSIP  = properties.ServerIP;
                Terraria_Server.Main.Initialize();

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

                WorldIO.LoadWorld(null, null, World.SavePath);

                ctx = new HookContext
                {
                    Sender = World.Sender,
                };

                eArgs = new HookArgs.ServerStateChange
                {
                    ServerChangeState = ServerState.LOADED
                };

                HookPoints.ServerStateChange.Invoke(ref ctx, ref eArgs);

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

                ProgramLog.Log(Languages.Startup_StartingTheServer);
                NetPlay.StartServer();

                while (!NetPlay.ServerUp)
                {
                }

                ThreadPool.QueueUserWorkItem(CommandThread);
                ProgramLog.Console.Print(Languages.Startup_YouCanNowInsertCommands);

                while (WorldModify.saveLock || NetPlay.ServerUp || Restarting)
                {
                    Thread.Sleep(100);
                }

                ProgramLog.Log(Languages.Startup_Exiting);
                Thread.Sleep(1000);
            }
            catch (UpdateCompleted) { }
            catch (Exception e)
            {
                try
                {
                    using (StreamWriter streamWriter = new StreamWriter(Statics.DataPath + Path.DirectorySeparatorChar + "crashlog.txt", true))
                    {
                        streamWriter.WriteLine(DateTime.Now);
                        streamWriter.WriteLine(String.Format("{0} {1}", Languages.Startup_CrashlogGeneratedBy, MODInfo));
                        streamWriter.WriteLine(e);
                        streamWriter.WriteLine();
                    }
                    ProgramLog.Log(e, Languages.Startup_ProgramCrash);
                    ProgramLog.Log("{0} crashlog.txt -> http://tdsm.org/", Languages.Startup_PleaseSend);
                }
                catch { }
            }

            if (properties != null && File.Exists(properties.PIDFile.Trim()))
            {
                File.Delete(properties.PIDFile.Trim());
            }

            Thread.Sleep(500);
            ProgramLog.Log(Languages.Startup_LogEnd);
            ProgramLog.Close();

            RemoteConsole.RConServer.Stop();
        }