Example #1
0
 public void Disconnect(bool SendNotification = true)
 {
     if (this.IsOnline)
     {
         //Logger.LogError("Same? " + Thread.CurrentThread.ManagedThreadId + " - " +  Bootstrap.CurrentThread.ManagedThreadId);
         if (Thread.CurrentThread.ManagedThreadId != Util.GetUtil().MainThreadID)
         {
             //Logger.LogError("Nope, invoking");
             Loom.QueueOnMainThread(() =>
             {
                 this.Disconnect(SendNotification);
             });
             return;
         }
         Server.GetServer().RemovePlayer(uid);
         this.ourPlayer.netUser.Kick(NetError.Facepunch_Kick_RCON, false);
         IsDisconnecting = true;
     }
 }
        // funcoes normais
        private void SaveServer(object sender, DoWorkEventArgs e)
        {
            Logger.Log("SaveServer");
            try
            {
                Logger.Log("SaveScene...");
                SaveScene(ref builder);
                Logger.Log("SaveInstances...");
                SaveInstances(ref builder);
                Logger.Log("Next saves...");

                timestamp2.Stop();
                timestamp3 = SystemTimestamp.Restart;
                fsave      = builder.Build();
                timestamp3.Stop();
                int num = fsave.SceneObjectCount + fsave.InstanceObjectCount;

                Logger.Log("Salvando arquivo..." + path + " e seus backup.");

                SystemTimestamp timestamp5 = timestamp4 = SystemTimestamp.Restart;
                using (FileStream stream2 = File.Open(path + ".new", FileMode.Create, FileAccess.Write))
                {
                    fsave.WriteTo(stream2);
                    stream2.Flush();
                }

                timestamp4.Stop();
                if (File.Exists(path + ".old." + (SaveCopies + 1)))
                {
                    File.Delete(path + ".old." + (SaveCopies + 1));
                }

                for (int i = SaveCopies; i >= 0; i--)
                {
                    if (File.Exists(path + ".old." + i))
                    {
                        File.Move(path + ".old." + i, path + ".old." + (i + 1));
                    }
                }

                if (File.Exists(path))
                {
                    File.Move(path, path + ".old.0");
                }

                if (File.Exists(path + ".new"))
                {
                    File.Move(path + ".new", path);
                }
                timestamp5.Stop();
                restart.Stop();

                if (Hooks.IsShuttingDown)
                {
                    ServerIsSaving = false;
                    Logger.Log(string.Concat(new object[] { " Saved ", num, " Object(s). Took ", restart.ElapsedSeconds, " seconds." }));
                    //ProjectX.ProjectX.BroadCast(ProjectX.ProjectX.configServer.NameServer, "O servidor salvou [color green]" + num + "[/color] Objetos do mapa");
                    return;
                }

                Loom.QueueOnMainThread(() =>
                {
                    if (save.profile)
                    {
                        object[] args = new object[]
                        {
                            num, timestamp2.ElapsedSeconds,
                            timestamp2.ElapsedSeconds / restart.ElapsedSeconds, timestamp3.ElapsedSeconds,
                            timestamp3.ElapsedSeconds / restart.ElapsedSeconds, timestamp4.ElapsedSeconds,
                            timestamp4.ElapsedSeconds / restart.ElapsedSeconds, timestamp5.ElapsedSeconds,
                            timestamp5.ElapsedSeconds / restart.ElapsedSeconds, restart.ElapsedSeconds,
                            restart.ElapsedSeconds / restart.ElapsedSeconds
                        };
                        Logger.Log(string.Format(" Saved {0} Object(s) [times below are in elapsed seconds]\r\n  Logic:\t{1,-16:0.000000}\t{2,7:0.00%}\r\n  Build:\t{3,-16:0.000000}\t{4,7:0.00%}\r\n  Stream:\t{5,-16:0.000000}\t{6,7:0.00%}\r\n  All IO:\t{7,-16:0.000000}\t{8,7:0.00%}\r\n  Total:\t{9,-16:0.000000}\t{10,7:0.00%}", args));
                    }
                    else
                    {
                        Logger.Log(string.Concat(new object[] { " Saved ", num, " Object(s). Took ", restart.ElapsedSeconds, " seconds." }));
                        //ProjectX.ProjectX.BroadCast(ProjectX.ProjectX.configServer.NameServer, "O servidor salvou [color green]" + num + "[/color] Objetos do mapa");
                    }

                    Hooks.OnServerSaveEvent(num, restart.ElapsedSeconds);
                    ServerIsSaving = false;
                    LastSaveTime   = DateTime.Now;

                    // Process the unprocessed hashset values here without causing HashSet modified error.
                    List <ServerSave> RemovableKeys = new List <ServerSave>();

                    foreach (ServerSave x in UnProcessedSaves.Keys)
                    {
                        try
                        {
                            if (UnProcessedSaves.ContainsKey(x))
                            {
                                byte value = UnProcessedSaves[x];
                                if (value == 1)
                                {
                                    if (ServerSaveManager.Instances.registers.Add(x))
                                    {
                                        ServerSaveManager.Instances.ordered.Add(x);
                                    }

                                    ServerSaveManager.Instances.ordered.Add(x);
                                }
                                else
                                {
                                    if (ServerSaveManager.Instances.registers.Remove(x))
                                    {
                                        ServerSaveManager.Instances.ordered.Remove(x);
                                    }
                                }

                                RemovableKeys.Add(x);
                            }
                        }
                        catch (KeyNotFoundException ex)
                        {
                            Logger.LogError("[RegisterHook KeyNotFoundException] " + ex);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError("[RegisterHook Error] " + ex);
                        }
                    }

                    foreach (var x in RemovableKeys)
                    {
                        UnProcessedSaves.Remove(x);
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.LogError("[ServerSaveHandler Error 0x2] " + ex);
                ServerIsSaving     = false;
                LastSaveTime       = DateTime.Now;
                NextServerSaveTime = LastSaveTime.AddMinutes(ServerSaveTime);
                if (StopServerOnSaveFail)
                {
                    Logger.LogWarning("[Fougerite WorldSave] We have caught an error. Killing server as requested.");
                    Process.GetCurrentProcess().Kill();
                }
            }
            finally
            {
                if (e != null && sender != null)
                {
                    Invoke(nameof(SalvarMapaComBackgroundWorker), ServerSaveTime * 60);
                }
            }
        }