SavePlayer() public static method

public static SavePlayer ( PlayerFileData playerFile, bool skipMapSave = false ) : void
playerFile PlayerFileData
skipMapSave bool
return void
Example #1
0
 private static void InnerClientLoop()
 {
     try
     {
         NetMessage.buffer[17].Reset();
         int num = -1;
         while (!disconnect)
         {
             if (Connection.Socket.IsConnected())
             {
                 if (NetMessage.buffer[17].checkBytes)
                 {
                     NetMessage.CheckBytes();
                 }
                 Connection.IsActive = true;
                 if (Connection.State == 0)
                 {
                     Main.statusText  = Language.GetTextValue("Net.FoundServer");
                     Connection.State = 1;
                     NetMessage.SendData(1);
                 }
                 if (Connection.State == 2 && num != Connection.State)
                 {
                     num             = Connection.State;
                     Main.statusText = Language.GetTextValue("Net.SendingPlayerData");
                 }
                 if (Connection.State == 3 && num != Connection.State)
                 {
                     num             = Connection.State;
                     Main.statusText = Language.GetTextValue("Net.RequestingWorldInformation");
                 }
                 if (Connection.State == 4)
                 {
                     WorldGen.worldCleared = false;
                     Connection.State      = 5;
                     if (Main.cloudBGActive >= 1f)
                     {
                         Main.cloudBGAlpha = 1f;
                     }
                     else
                     {
                         Main.cloudBGAlpha = 0f;
                     }
                     Main.windSpeed = Main.windSpeedSet;
                     Cloud.resetClouds();
                     Main.cloudAlpha = Main.maxRaining;
                     WorldGen.clearWorld();
                     if (Main.mapEnabled)
                     {
                         Main.Map.Load();
                     }
                 }
                 if (Connection.State == 5 && Main.loadMapLock)
                 {
                     float num2 = (float)Main.loadMapLastX / (float)Main.maxTilesX;
                     Main.statusText = string.Concat(Lang.gen[68], " ", (int)(num2 * 100f + 1f), "%");
                 }
                 else if (Connection.State == 5 && WorldGen.worldCleared)
                 {
                     Connection.State = 6;
                     Main.player[Main.myPlayer].FindSpawn();
                     NetMessage.SendData(8, -1, -1, "", Main.player[Main.myPlayer].SpawnX, Main.player[Main.myPlayer].SpawnY);
                 }
                 if (Connection.State == 6 && num != Connection.State)
                 {
                     Main.statusText = Language.GetTextValue("Net.RequestingTileData");
                 }
                 if (!Connection.IsReading && !disconnect && Connection.Socket.IsDataAvailable())
                 {
                     Connection.IsReading = true;
                     Connection.Socket.AsyncReceive(Connection.ReadBuffer, 0, Connection.ReadBuffer.Length, Connection.ClientReadCallBack);
                 }
                 if (Connection.StatusMax > 0 && Connection.StatusText != "")
                 {
                     if (Connection.StatusCount >= Connection.StatusMax)
                     {
                         Main.statusText        = Language.GetTextValue("Net.StatusComplete", Connection.StatusText);
                         Connection.StatusText  = "";
                         Connection.StatusMax   = 0;
                         Connection.StatusCount = 0;
                     }
                     else
                     {
                         Main.statusText = Connection.StatusText + ": " + (int)((float)Connection.StatusCount / (float)Connection.StatusMax * 100f) + "%";
                     }
                 }
                 Thread.Sleep(1);
             }
             else if (Connection.IsActive)
             {
                 Main.statusText = Language.GetTextValue("Net.LostConnection");
                 disconnect      = true;
             }
             num = Connection.State;
         }
         try
         {
             Connection.Socket.Close();
         }
         catch
         {
         }
         if (!Main.gameMenu)
         {
             Main.SwitchNetMode(0);
             Player.SavePlayer(Main.ActivePlayerFileData);
             Main.ActivePlayerFileData.StopPlayTimer();
             Main.gameMenu = true;
             Main.menuMode = 14;
         }
         NetMessage.buffer[17].Reset();
         if (Main.menuMode == 15 && Main.statusText == Language.GetTextValue("Net.LostConnection"))
         {
             Main.menuMode = 14;
         }
         if (Connection.StatusText != "" && Connection.StatusText != null)
         {
             Main.statusText = Language.GetTextValue("Net.LostConnection");
         }
         Connection.StatusCount = 0;
         Connection.StatusMax   = 0;
         Connection.StatusText  = "";
         Main.SwitchNetMode(0);
     }
     catch (Exception value)
     {
         try
         {
             using (StreamWriter streamWriter = new StreamWriter("client-crashlog.txt", true))
             {
                 streamWriter.WriteLine(DateTime.Now);
                 streamWriter.WriteLine(value);
                 streamWriter.WriteLine("");
             }
         }
         catch
         {
         }
         disconnect = true;
     }
     if (Netplay.OnDisconnect != null)
     {
         Netplay.OnDisconnect();
     }
 }
Example #2
0
        public static void ClientLoop(object threadContext)
        {
            Netplay.ResetNetDiag();
            Main.ServerSideCharacter = false;
            if (Main.rand == null)
            {
                Main.rand = new Random((int)DateTime.Now.Ticks);
            }
            if (WorldGen.genRand == null)
            {
                WorldGen.genRand = new Random((int)DateTime.Now.Ticks);
            }
            Main.player[Main.myPlayer].hostile = false;
            Main.clientPlayer = (Player)Main.player[Main.myPlayer].clientClone();
            for (int i = 0; i < 255; i++)
            {
                if (i != Main.myPlayer)
                {
                    Main.player[i] = new Player();
                }
            }
            Main.menuMode = 10;
            Main.menuMode = 14;
            if (!Main.autoPass)
            {
                Main.statusText = "Connecting to " + Netplay.serverIP;
            }
            Main.netMode       = 1;
            Netplay.disconnect = false;
            Netplay.clientSock = new ClientSock();
            Netplay.clientSock.tcpClient.NoDelay = true;
            Netplay.clientSock.readBuffer        = new byte[1024];
            Netplay.clientSock.writeBuffer       = new byte[1024];
            bool flag = true;

            while (flag)
            {
                flag = false;
                try
                {
                    Netplay.clientSock.tcpClient.Connect(Netplay.serverIP, Netplay.serverPort);
                    Netplay.clientSock.networkStream = Netplay.clientSock.tcpClient.GetStream();
                    flag = false;
                }
                catch
                {
                    if (!Netplay.disconnect && Main.gameMenu)
                    {
                        flag = true;
                    }
                }
            }
            NetMessage.buffer[256].Reset();
            int num = -1;

            while (!Netplay.disconnect)
            {
                if (Netplay.clientSock.tcpClient.Connected)
                {
                    if (NetMessage.buffer[256].checkBytes)
                    {
                        NetMessage.CheckBytes(256);
                    }
                    Netplay.clientSock.active = true;
                    if (Netplay.clientSock.state == 0)
                    {
                        Main.statusText          = "Found server";
                        Netplay.clientSock.state = 1;
                        NetMessage.SendData(1, -1, -1, "", 0, 0f, 0f, 0f, 0);
                    }
                    if (Netplay.clientSock.state == 2 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "Sending player data...";
                    }
                    if (Netplay.clientSock.state == 3 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "Requesting world information";
                    }
                    if (Netplay.clientSock.state == 4)
                    {
                        WorldGen.worldCleared    = false;
                        Netplay.clientSock.state = 5;
                        if (Main.cloudBGActive >= 1f)
                        {
                            Main.cloudBGAlpha = 1f;
                        }
                        else
                        {
                            Main.cloudBGAlpha = 0f;
                        }
                        Main.windSpeed = Main.windSpeedSet;
                        Cloud.resetClouds();
                        Main.cloudAlpha = Main.maxRaining;
                        WorldGen.clearWorld();
                        if (Main.mapEnabled)
                        {
                            Map.loadMap();
                        }
                    }
                    if (Netplay.clientSock.state == 5 && Main.loadMapLock)
                    {
                        float num2 = (float)Main.loadMapLastX / (float)Main.maxTilesX;
                        Main.statusText = string.Concat(new object[]
                        {
                            Lang.gen[68],
                            " ",
                            (int)(num2 * 100f + 1f),
                            "%"
                        });
                    }
                    else
                    {
                        if (Netplay.clientSock.state == 5 && WorldGen.worldCleared)
                        {
                            Netplay.clientSock.state = 6;
                            Main.player[Main.myPlayer].FindSpawn();
                            NetMessage.SendData(8, -1, -1, "", Main.player[Main.myPlayer].SpawnX, (float)Main.player[Main.myPlayer].SpawnY, 0f, 0f, 0);
                        }
                    }
                    if (Netplay.clientSock.state == 6 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "Requesting tile data";
                    }
                    if (!Netplay.clientSock.locked && !Netplay.disconnect && Netplay.clientSock.networkStream.DataAvailable)
                    {
                        Netplay.clientSock.locked = true;
                        Netplay.clientSock.networkStream.BeginRead(Netplay.clientSock.readBuffer, 0, Netplay.clientSock.readBuffer.Length, new AsyncCallback(Netplay.clientSock.ClientReadCallBack), Netplay.clientSock.networkStream);
                    }
                    if (Netplay.clientSock.statusMax > 0 && Netplay.clientSock.statusText != "")
                    {
                        if (Netplay.clientSock.statusCount >= Netplay.clientSock.statusMax)
                        {
                            Main.statusText = Netplay.clientSock.statusText + ": Complete!";
                            Netplay.clientSock.statusText  = "";
                            Netplay.clientSock.statusMax   = 0;
                            Netplay.clientSock.statusCount = 0;
                        }
                        else
                        {
                            Main.statusText = string.Concat(new object[]
                            {
                                Netplay.clientSock.statusText,
                                ": ",
                                (int)((float)Netplay.clientSock.statusCount / (float)Netplay.clientSock.statusMax * 100f),
                                "%"
                            });
                        }
                    }
                    Thread.Sleep(1);
                }
                else
                {
                    if (Netplay.clientSock.active)
                    {
                        Main.statusText    = "Lost connection";
                        Netplay.disconnect = true;
                    }
                }
                num = Netplay.clientSock.state;
            }
            try
            {
                Netplay.clientSock.networkStream.Close();
                if (Netplay.clientSock.tcpClient.Connected)
                {
                    Netplay.clientSock.networkStream = Netplay.clientSock.tcpClient.GetStream();
                }
            }
            catch
            {
            }
            if (!Main.gameMenu)
            {
                Main.netMode = 0;
                Player.SavePlayer(Main.player[Main.myPlayer], Main.playerPathName, false, false);
                Main.gameMenu = true;
                Main.menuMode = 14;
            }
            NetMessage.buffer[256].Reset();
            if (Main.menuMode == 15 && Main.statusText == "Lost connection")
            {
                Main.menuMode = 14;
            }
            if (Netplay.clientSock.statusText != "" && Netplay.clientSock.statusText != null)
            {
                Main.statusText = "Lost connection";
            }
            Netplay.clientSock.statusCount = 0;
            Netplay.clientSock.statusMax   = 0;
            Netplay.clientSock.statusText  = "";
            Main.netMode = 0;
        }
Example #3
0
        private static void InnerClientLoop()
        {
            NetMessage.buffer[256].Reset();
            int num = -1;

            while (!Netplay.disconnect)
            {
                if (Netplay.Connection.Socket.IsConnected())
                {
                    if (NetMessage.buffer[256].checkBytes)
                    {
                        NetMessage.CheckBytes(256);
                    }
                    Netplay.Connection.IsActive = true;
                    if (Netplay.Connection.State == 0)
                    {
                        Main.statusText          = "Found server";
                        Netplay.Connection.State = 1;
                        NetMessage.SendData(1, -1, -1, "", 0, 0f, 0f, 0f, 0, 0, 0);
                    }
                    if (Netplay.Connection.State == 2 && num != Netplay.Connection.State)
                    {
                        Main.statusText = "Sending player data...";
                    }
                    if (Netplay.Connection.State == 3 && num != Netplay.Connection.State)
                    {
                        Main.statusText = "Requesting world information";
                    }
                    if (Netplay.Connection.State == 4)
                    {
                        WorldGen.worldCleared    = false;
                        Netplay.Connection.State = 5;
                        if (Main.cloudBGActive >= 1f)
                        {
                            Main.cloudBGAlpha = 1f;
                        }
                        else
                        {
                            Main.cloudBGAlpha = 0f;
                        }
                        Main.windSpeed = Main.windSpeedSet;
                        Cloud.resetClouds();
                        Main.cloudAlpha = Main.maxRaining;
                        WorldGen.clearWorld();
                        if (Main.mapEnabled)
                        {
                            Main.Map.Load();
                        }
                    }
                    if (Netplay.Connection.State == 5 && Main.loadMapLock)
                    {
                        float num2 = (float)Main.loadMapLastX / (float)Main.maxTilesX;
                        Main.statusText = string.Concat(new object[]
                        {
                            Lang.gen[68],
                            " ",
                            (int)(num2 * 100f + 1f),
                            "%"
                        });
                    }
                    else if (Netplay.Connection.State == 5 && WorldGen.worldCleared)
                    {
                        Netplay.Connection.State = 6;
                        Main.player[Main.myPlayer].FindSpawn();
                        NetMessage.SendData(8, -1, -1, "", Main.player[Main.myPlayer].SpawnX, (float)Main.player[Main.myPlayer].SpawnY, 0f, 0f, 0, 0, 0);
                    }
                    if (Netplay.Connection.State == 6 && num != Netplay.Connection.State)
                    {
                        Main.statusText = "Requesting tile data";
                    }
                    if (!Netplay.Connection.IsReading && !Netplay.disconnect && Netplay.Connection.Socket.IsDataAvailable())
                    {
                        Netplay.Connection.IsReading = true;
                        Netplay.Connection.Socket.AsyncReceive(Netplay.Connection.ReadBuffer, 0, Netplay.Connection.ReadBuffer.Length, new SocketReceiveCallback(Netplay.Connection.ClientReadCallBack), null);
                    }
                    if (Netplay.Connection.StatusMax > 0 && Netplay.Connection.StatusText != "")
                    {
                        if (Netplay.Connection.StatusCount >= Netplay.Connection.StatusMax)
                        {
                            Main.statusText = Netplay.Connection.StatusText + ": Complete!";
                            Netplay.Connection.StatusText  = "";
                            Netplay.Connection.StatusMax   = 0;
                            Netplay.Connection.StatusCount = 0;
                        }
                        else
                        {
                            Main.statusText = string.Concat(new object[]
                            {
                                Netplay.Connection.StatusText,
                                ": ",
                                (int)((float)Netplay.Connection.StatusCount / (float)Netplay.Connection.StatusMax * 100f),
                                "%"
                            });
                        }
                    }
                    Thread.Sleep(1);
                }
                else if (Netplay.Connection.IsActive)
                {
                    Main.statusText    = "Lost connection";
                    Netplay.disconnect = true;
                }
                num = Netplay.Connection.State;
            }
            try
            {
                Netplay.Connection.Socket.Close();
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex);
                System.Diagnostics.Debugger.Break();

                System.Diagnostics.Debugger.Break();
#endif
            }
            if (!Main.gameMenu)
            {
                Main.SwitchNetMode(0);
                Player.SavePlayer(Main.ActivePlayerFileData, false);
                Main.ActivePlayerFileData.StopPlayTimer();
                Main.gameMenu = true;
                Main.menuMode = 14;
            }
            NetMessage.buffer[256].Reset();
            if (Main.menuMode == 15 && Main.statusText == "Lost connection")
            {
                Main.menuMode = 14;
            }
            if (Netplay.Connection.StatusText != "" && Netplay.Connection.StatusText != null)
            {
                Main.statusText = "Lost connection";
            }
            Netplay.Connection.StatusCount = 0;
            Netplay.Connection.StatusMax   = 0;
            Netplay.Connection.StatusText  = "";
            Main.SwitchNetMode(0);
            if (Netplay.OnDisconnect != null)
            {
                Netplay.OnDisconnect();
            }
        }
Example #4
0
 private static void InnerClientLoop()
 {
     try
     {
         NetMessage.buffer[256].Reset();
         int num1 = -1;
         while (!Netplay.disconnect)
         {
             if (Netplay.Connection.Socket.IsConnected())
             {
                 if (NetMessage.buffer[256].checkBytes)
                 {
                     NetMessage.CheckBytes(256);
                 }
                 Netplay.Connection.IsActive = true;
                 if (Netplay.Connection.State == 0)
                 {
                     Main.statusText          = "Found server";
                     Netplay.Connection.State = 1;
                     NetMessage.SendData(1, -1, -1, "", 0, 0.0f, 0.0f, 0.0f, 0, 0, 0);
                 }
                 if (Netplay.Connection.State == 2 && num1 != Netplay.Connection.State)
                 {
                     Main.statusText = "Sending player data...";
                 }
                 if (Netplay.Connection.State == 3 && num1 != Netplay.Connection.State)
                 {
                     Main.statusText = "Requesting world information";
                 }
                 if (Netplay.Connection.State == 4)
                 {
                     WorldGen.worldCleared    = false;
                     Netplay.Connection.State = 5;
                     Main.cloudBGAlpha        = (double)Main.cloudBGActive < 1.0 ? 0.0f : 1f;
                     Main.windSpeed           = Main.windSpeedSet;
                     Cloud.resetClouds();
                     Main.cloudAlpha = Main.maxRaining;
                     WorldGen.clearWorld();
                     if (Main.mapEnabled)
                     {
                         Main.Map.Load();
                     }
                 }
                 if (Netplay.Connection.State == 5 && Main.loadMapLock)
                 {
                     float num2 = (float)Main.loadMapLastX / (float)Main.maxTilesX;
                     Main.statusText = string.Concat(new object[4]
                     {
                         (object)Lang.gen[68],
                         (object)" ",
                         (object)(int)((double)num2 * 100.0 + 1.0),
                         (object)"%"
                     });
                 }
                 else if (Netplay.Connection.State == 5 && WorldGen.worldCleared)
                 {
                     Netplay.Connection.State = 6;
                     Main.player[Main.myPlayer].FindSpawn();
                     NetMessage.SendData(8, -1, -1, "", Main.player[Main.myPlayer].SpawnX, (float)Main.player[Main.myPlayer].SpawnY, 0.0f, 0.0f, 0, 0, 0);
                 }
                 if (Netplay.Connection.State == 6 && num1 != Netplay.Connection.State)
                 {
                     Main.statusText = "Requesting tile data";
                 }
                 if (!Netplay.Connection.IsReading && !Netplay.disconnect && Netplay.Connection.Socket.IsDataAvailable())
                 {
                     Netplay.Connection.IsReading = true;
                     Netplay.Connection.Socket.AsyncReceive(Netplay.Connection.ReadBuffer, 0, Netplay.Connection.ReadBuffer.Length, new SocketReceiveCallback(Netplay.Connection.ClientReadCallBack), (object)null);
                 }
                 if (Netplay.Connection.StatusMax > 0 && Netplay.Connection.StatusText != "")
                 {
                     if (Netplay.Connection.StatusCount >= Netplay.Connection.StatusMax)
                     {
                         Main.statusText = Netplay.Connection.StatusText + ": Complete!";
                         Netplay.Connection.StatusText  = "";
                         Netplay.Connection.StatusMax   = 0;
                         Netplay.Connection.StatusCount = 0;
                     }
                     else
                     {
                         Main.statusText = string.Concat(new object[4]
                         {
                             (object)Netplay.Connection.StatusText,
                             (object)": ",
                             (object)(int)((double)Netplay.Connection.StatusCount / (double)Netplay.Connection.StatusMax * 100.0),
                             (object)"%"
                         });
                     }
                 }
                 Thread.Sleep(1);
             }
             else if (Netplay.Connection.IsActive)
             {
                 Main.statusText    = "Lost connection";
                 Netplay.disconnect = true;
             }
             num1 = Netplay.Connection.State;
         }
         try
         {
             Netplay.Connection.Socket.Close();
         }
         catch
         {
         }
         if (!Main.gameMenu)
         {
             Main.SwitchNetMode(0);
             Player.SavePlayer(Main.ActivePlayerFileData, false);
             Main.ActivePlayerFileData.StopPlayTimer();
             Main.gameMenu = true;
             Main.menuMode = 14;
         }
         NetMessage.buffer[256].Reset();
         if (Main.menuMode == 15 && Main.statusText == "Lost connection")
         {
             Main.menuMode = 14;
         }
         if (Netplay.Connection.StatusText != "" && Netplay.Connection.StatusText != null)
         {
             Main.statusText = "Lost connection";
         }
         Netplay.Connection.StatusCount = 0;
         Netplay.Connection.StatusMax   = 0;
         Netplay.Connection.StatusText  = "";
         Main.SwitchNetMode(0);
     }
     catch (Exception ex)
     {
         try
         {
             using (StreamWriter streamWriter = new StreamWriter("client-crashlog.txt", true))
             {
                 streamWriter.WriteLine((object)DateTime.Now);
                 streamWriter.WriteLine((object)ex);
                 streamWriter.WriteLine("");
             }
         }
         catch
         {
         }
         Netplay.disconnect = true;
     }
     if (Netplay.OnDisconnect == null)
     {
         return;
     }
     Netplay.OnDisconnect();
 }
Example #5
0
 private static void InnerClientLoop()
 {
     try
     {
         NetMessage.buffer[256].Reset();
         int num1 = -1;
         while (!Netplay.Disconnect)
         {
             if (Netplay.Connection.Socket.IsConnected())
             {
                 Netplay.Connection.IsActive = true;
                 if (Netplay.Connection.State == 0)
                 {
                     Main.statusText          = Language.GetTextValue("Net.FoundServer");
                     Netplay.Connection.State = 1;
                     NetMessage.SendData(1, -1, -1, (NetworkText)null, 0, 0.0f, 0.0f, 0.0f, 0, 0, 0);
                 }
                 if (Netplay.Connection.State == 2 && num1 != Netplay.Connection.State)
                 {
                     Main.statusText = Language.GetTextValue("Net.SendingPlayerData");
                 }
                 if (Netplay.Connection.State == 3 && num1 != Netplay.Connection.State)
                 {
                     Main.statusText = Language.GetTextValue("Net.RequestingWorldInformation");
                 }
                 if (Netplay.Connection.State == 4)
                 {
                     WorldGen.worldCleared    = false;
                     Netplay.Connection.State = 5;
                     Main.cloudBGAlpha        = (double)Main.cloudBGActive < 1.0 ? 0.0f : 1f;
                     Main.windSpeedCurrent    = Main.windSpeedTarget;
                     Cloud.resetClouds();
                     Main.cloudAlpha = Main.maxRaining;
                     WorldGen.clearWorld();
                     if (Main.mapEnabled)
                     {
                         Main.Map.Load();
                     }
                 }
                 if (Netplay.Connection.State == 5 && Main.loadMapLock)
                 {
                     float num2 = (float)Main.loadMapLastX / (float)Main.maxTilesX;
                     Main.statusText = Lang.gen[68].Value + " " + (object)(int)((double)num2 * 100.0 + 1.0) + "%";
                 }
                 else if (Netplay.Connection.State == 5 && WorldGen.worldCleared)
                 {
                     Netplay.Connection.State = 6;
                     Main.player[Main.myPlayer].FindSpawn();
                     NetMessage.SendData(8, -1, -1, (NetworkText)null, Main.player[Main.myPlayer].SpawnX, (float)Main.player[Main.myPlayer].SpawnY, 0.0f, 0.0f, 0, 0, 0);
                 }
                 if (Netplay.Connection.State == 6 && num1 != Netplay.Connection.State)
                 {
                     Main.statusText = Language.GetTextValue("Net.RequestingTileData");
                 }
                 if (!Netplay.Connection.IsReading && !Netplay.Disconnect && Netplay.Connection.Socket.IsDataAvailable())
                 {
                     Netplay.Connection.IsReading = true;
                     Netplay.Connection.Socket.AsyncReceive(Netplay.Connection.ReadBuffer, 0, Netplay.Connection.ReadBuffer.Length, new SocketReceiveCallback(Netplay.Connection.ClientReadCallBack), (object)null);
                 }
                 if (Netplay.Connection.StatusMax > 0 && Netplay.Connection.StatusText != "")
                 {
                     if (Netplay.Connection.StatusCount >= Netplay.Connection.StatusMax)
                     {
                         Main.statusText = Language.GetTextValue("Net.StatusComplete", (object)Netplay.Connection.StatusText);
                         Netplay.Connection.StatusText  = "";
                         Netplay.Connection.StatusMax   = 0;
                         Netplay.Connection.StatusCount = 0;
                     }
                     else
                     {
                         Main.statusText = Netplay.Connection.StatusText + ": " + (object)(int)((double)Netplay.Connection.StatusCount / (double)Netplay.Connection.StatusMax * 100.0) + "%";
                     }
                 }
                 Thread.Sleep(1);
             }
             else if (Netplay.Connection.IsActive)
             {
                 Main.statusText    = Language.GetTextValue("Net.LostConnection");
                 Netplay.Disconnect = true;
             }
             num1 = Netplay.Connection.State;
         }
         try
         {
             Netplay.Connection.Socket.Close();
         }
         catch
         {
         }
         if (!Main.gameMenu)
         {
             Main.gameMenu = true;
             Main.SwitchNetMode(0);
             MapHelper.noStatusText = true;
             Player.SavePlayer(Main.ActivePlayerFileData, false);
             Player.ClearPlayerTempInfo();
             Main.ActivePlayerFileData.StopPlayTimer();
             SoundEngine.StopTrackedSounds();
             MapHelper.noStatusText = false;
             Main.menuMode          = 14;
         }
         NetMessage.buffer[256].Reset();
         if (Main.menuMode == 15 && Main.statusText == Language.GetTextValue("Net.LostConnection"))
         {
             Main.menuMode = 14;
         }
         if (Netplay.Connection.StatusText != "" && Netplay.Connection.StatusText != null)
         {
             Main.statusText = Language.GetTextValue("Net.LostConnection");
         }
         Netplay.Connection.StatusCount = 0;
         Netplay.Connection.StatusMax   = 0;
         Netplay.Connection.StatusText  = "";
         Main.SwitchNetMode(0);
     }
     catch (Exception ex)
     {
         try
         {
             using (StreamWriter streamWriter = new StreamWriter("client-crashlog.txt", true))
             {
                 streamWriter.WriteLine((object)DateTime.Now);
                 streamWriter.WriteLine((object)ex);
                 streamWriter.WriteLine("");
             }
         }
         catch
         {
         }
         Netplay.Disconnect = true;
     }
     if (Netplay.OnDisconnect == null)
     {
         return;
     }
     Netplay.OnDisconnect();
 }
Example #6
0
        public static void ClientLoop(object threadContext)
        {
            Netplay.ResetNetDiag();
            if (Main.rand == null)
            {
                Main.rand = new Random((int)DateTime.Now.Ticks);
            }
            if (WorldGen.genRand == null)
            {
                WorldGen.genRand = new Random((int)DateTime.Now.Ticks);
            }
            Main.player[Main.myPlayer].hostile = false;
            Main.clientPlayer = (Player)Main.player[Main.myPlayer].clientClone();
            for (int i = 0; i < 255; i++)
            {
                if (i != Main.myPlayer)
                {
                    Main.player[i] = new Player();
                }
            }
            Main.menuMode = 10;
            Main.menuMode = 14;
            if (!Main.autoPass)
            {
                Main.statusText = "正在连接到 " + Netplay.serverIP;
            }
            Main.netMode       = 1;
            Netplay.disconnect = false;
            Netplay.clientSock = new ClientSock();
            Netplay.clientSock.tcpClient.NoDelay = true;
            Netplay.clientSock.readBuffer        = new byte[1024];
            Netplay.clientSock.writeBuffer       = new byte[1024];
            bool flag = true;

            while (flag)
            {
                flag = false;
                try
                {
                    Netplay.clientSock.tcpClient.Connect(Netplay.serverIP, Netplay.serverPort);
                    Netplay.clientSock.networkStream = Netplay.clientSock.tcpClient.GetStream();
                    flag = false;
                }
                catch
                {
                    if (!Netplay.disconnect && Main.gameMenu)
                    {
                        flag = true;
                    }
                }
            }
            NetMessage.buffer[256].Reset();
            int num = -1;

            while (!Netplay.disconnect)
            {
                if (Netplay.clientSock.tcpClient.Connected)
                {
                    if (NetMessage.buffer[256].checkBytes)
                    {
                        NetMessage.CheckBytes(256);
                    }
                    Netplay.clientSock.active = true;
                    if (Netplay.clientSock.state == 0)
                    {
                        Main.statusText          = "连接到服务器";
                        Netplay.clientSock.state = 1;
                        NetMessage.SendData(1, -1, -1, "", 0, 0f, 0f, 0f, 0);
                    }
                    if (Netplay.clientSock.state == 2 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "发送角色数据...";
                    }
                    if (Netplay.clientSock.state == 3 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "向服务器请求世界信息";
                    }
                    if (Netplay.clientSock.state == 4)
                    {
                        WorldGen.worldCleared    = false;
                        Netplay.clientSock.state = 5;
                        WorldGen.clearWorld();
                    }
                    if (Netplay.clientSock.state == 5 && WorldGen.worldCleared)
                    {
                        Netplay.clientSock.state = 6;
                        Main.player[Main.myPlayer].FindSpawn();
                        NetMessage.SendData(8, -1, -1, "", Main.player[Main.myPlayer].SpawnX, (float)Main.player[Main.myPlayer].SpawnY, 0f, 0f, 0);
                    }
                    if (Netplay.clientSock.state == 6 && num != Netplay.clientSock.state)
                    {
                        Main.statusText = "向服务器请求物块数据";
                    }
                    if (!Netplay.clientSock.locked && !Netplay.disconnect && Netplay.clientSock.networkStream.DataAvailable)
                    {
                        Netplay.clientSock.locked = true;
                        Netplay.clientSock.networkStream.BeginRead(Netplay.clientSock.readBuffer, 0, Netplay.clientSock.readBuffer.Length, new AsyncCallback(Netplay.clientSock.ClientReadCallBack), Netplay.clientSock.networkStream);
                    }
                    if (Netplay.clientSock.statusMax > 0 && Netplay.clientSock.statusText != "")
                    {
                        if (Netplay.clientSock.statusCount >= Netplay.clientSock.statusMax)
                        {
                            Main.statusText = Netplay.clientSock.statusText + ": 完成!";
                            Netplay.clientSock.statusText  = "";
                            Netplay.clientSock.statusMax   = 0;
                            Netplay.clientSock.statusCount = 0;
                        }
                        else
                        {
                            Main.statusText = string.Concat(new object[]
                            {
                                Netplay.clientSock.statusText,
                                ": ",
                                (int)((float)Netplay.clientSock.statusCount / (float)Netplay.clientSock.statusMax * 100f),
                                "%"
                            });
                        }
                    }
                    Thread.Sleep(1);
                }
                else if (Netplay.clientSock.active)
                {
                    Main.statusText    = "失去连接";
                    Netplay.disconnect = true;
                }
                num = Netplay.clientSock.state;
            }
            try
            {
                Netplay.clientSock.networkStream.Close();
                Netplay.clientSock.networkStream = Netplay.clientSock.tcpClient.GetStream();
            }
            catch
            {
            }
            if (!Main.gameMenu)
            {
                Main.netMode = 0;
                Player.SavePlayer(Main.player[Main.myPlayer], Main.playerPathName);
                Main.gameMenu = true;
                Main.menuMode = 14;
            }
            NetMessage.buffer[256].Reset();
            if (Main.menuMode == 15 && Main.statusText == "失去连接")
            {
                Main.menuMode = 14;
            }
            if (Netplay.clientSock.statusText != "" && Netplay.clientSock.statusText != null)
            {
                Main.statusText = "失去连接";
            }
            Netplay.clientSock.statusCount = 0;
            Netplay.clientSock.statusMax   = 0;
            Netplay.clientSock.statusText  = "";
            Main.netMode = 0;
        }
Example #7
0
        public static void ClientLoop(object threadContext)
        {
            ResetNetDiag();
            if (Main.rand == null)
            {
                Main.rand = new Random((int)DateTime.Now.Ticks);
            }
            if (WorldGen.genRand == null)
            {
                WorldGen.genRand = new Random((int)DateTime.Now.Ticks);
            }
            Main.player[Main.myPlayer].hostile = false;
            Main.clientPlayer = (Player)Main.player[Main.myPlayer].clientClone();
            for (int i = 0; i < 0xff; i++)
            {
                if (i != Main.myPlayer)
                {
                    Main.player[i] = new Player();
                }
            }
            Main.menuMode = 10;
            Main.menuMode = 14;
            if (!Main.autoPass)
            {
                Main.statusText = "Connecting to " + serverIP;
            }
            Main.netMode = 1;
            disconnect   = false;
            clientSock   = new ClientSock();
            clientSock.tcpClient.NoDelay = true;
            clientSock.readBuffer        = new byte[0x400];
            clientSock.writeBuffer       = new byte[0x400];
            bool flag = true;

            while (flag)
            {
                flag = false;
                try
                {
                    clientSock.tcpClient.Connect(serverIP, serverPort);
                    clientSock.networkStream = clientSock.tcpClient.GetStream();
                    flag = false;
                    continue;
                }
                catch
                {
                    if (!disconnect && Main.gameMenu)
                    {
                        flag = true;
                    }
                    continue;
                }
            }
            NetMessage.buffer[0x100].Reset();
            for (int j = -1; !disconnect; j = clientSock.state)
            {
                if (clientSock.tcpClient.Connected)
                {
                    if (NetMessage.buffer[0x100].checkBytes)
                    {
                        NetMessage.CheckBytes(0x100);
                    }
                    clientSock.active = true;
                    if (clientSock.state == 0)
                    {
                        Main.statusText  = "Found server";
                        clientSock.state = 1;
                        NetMessage.SendData(1, -1, -1, "", 0, 0f, 0f, 0f, 0);
                    }
                    if ((clientSock.state == 2) && (j != clientSock.state))
                    {
                        Main.statusText = "Sending player data...";
                    }
                    if ((clientSock.state == 3) && (j != clientSock.state))
                    {
                        Main.statusText = "Requesting world information";
                    }
                    if (clientSock.state == 4)
                    {
                        WorldGen.worldCleared = false;
                        clientSock.state      = 5;
                        WorldGen.clearWorld();
                    }
                    if ((clientSock.state == 5) && WorldGen.worldCleared)
                    {
                        clientSock.state = 6;
                        Main.player[Main.myPlayer].FindSpawn();
                        NetMessage.SendData(8, -1, -1, "", Main.player[Main.myPlayer].SpawnX, (float)Main.player[Main.myPlayer].SpawnY, 0f, 0f, 0);
                    }
                    if ((clientSock.state == 6) && (j != clientSock.state))
                    {
                        Main.statusText = "Requesting tile data";
                    }
                    if ((!clientSock.locked && !disconnect) && clientSock.networkStream.DataAvailable)
                    {
                        clientSock.locked = true;
                        clientSock.networkStream.BeginRead(clientSock.readBuffer, 0, clientSock.readBuffer.Length, new AsyncCallback(clientSock.ClientReadCallBack), clientSock.networkStream);
                    }
                    if ((clientSock.statusMax > 0) && (clientSock.statusText != ""))
                    {
                        if (clientSock.statusCount >= clientSock.statusMax)
                        {
                            Main.statusText        = clientSock.statusText + ": Complete!";
                            clientSock.statusText  = "";
                            clientSock.statusMax   = 0;
                            clientSock.statusCount = 0;
                        }
                        else
                        {
                            Main.statusText = string.Concat(new object[] { clientSock.statusText, ": ", (int)((((float)clientSock.statusCount) / ((float)clientSock.statusMax)) * 100f), "%" });
                        }
                    }
                    Thread.Sleep(1);
                }
                else if (clientSock.active)
                {
                    Main.statusText = "Lost connection";
                    disconnect      = true;
                }
            }
            try
            {
                clientSock.networkStream.Close();
                clientSock.networkStream = clientSock.tcpClient.GetStream();
            }
            catch
            {
            }
            if (!Main.gameMenu)
            {
                Main.netMode = 0;
                Player.SavePlayer(Main.player[Main.myPlayer], Main.playerPathName);
                Main.gameMenu = true;
                Main.menuMode = 14;
            }
            NetMessage.buffer[0x100].Reset();
            if ((Main.menuMode == 15) && (Main.statusText == "Lost connection"))
            {
                Main.menuMode = 14;
            }
            if ((clientSock.statusText != "") && (clientSock.statusText != null))
            {
                Main.statusText = "Lost connection";
            }
            clientSock.statusCount = 0;
            clientSock.statusMax   = 0;
            clientSock.statusText  = "";
            Main.netMode           = 0;
        }