Beispiel #1
0
        /// <summary>
        /// Writes the message instance to the stream using the content type provided
        /// </summary>
        /// <param name="options">Options specific to writing this message and/or content type</param>
        /// <param name="contentType">The mime type of the content to be written</param>
        /// <param name="output">The stream to write the message to</param>
        /// <remarks> If you do not dispose of ICodedOutputStream some formats may yield incomplete output </remarks>
        public static ICodedOutputStream CreateOutputStream(MessageFormatOptions options, string contentType, Stream output)
        {
            ICodedOutputStream codedOutput = ContentTypeToOutputStream(contentType, options, output);

            if (codedOutput is JsonFormatWriter)
            {
                JsonFormatWriter writer = (JsonFormatWriter)codedOutput;
                if (options.FormattedOutput)
                {
                    writer.Formatted();
                }
            }
            else if (codedOutput is XmlFormatWriter)
            {
                XmlFormatWriter writer = (XmlFormatWriter)codedOutput;
                if (options.FormattedOutput)
                {
                    XmlWriterSettings settings = new XmlWriterSettings()
                    {
                        CheckCharacters    = false,
                        NewLineHandling    = NewLineHandling.Entitize,
                        OmitXmlDeclaration = true,
                        Encoding           = new UTF8Encoding(false),
                        Indent             = true,
                        IndentChars        = "    ",
                    };
                    // Don't know how else to change xml writer options?
                    codedOutput = writer = XmlFormatWriter.CreateInstance(XmlWriter.Create(output, settings));
                }
                writer.RootElementName = options.XmlWriterRootElementName;
                writer.Options         = options.XmlWriterOptions;
            }

            return(codedOutput);
        }
        // Thank you Unity for invoke "support"
        private void SaveServerManualWithoutInvoke(object sender, DoWorkEventArgs e)
        {
            try
            {
                //s.DoSave(ref builder);
                SaveScene(ref builder);
                SaveInstances(ref builder);
                timestamp2.Stop();
                timestamp3 = SystemTimestamp.Restart;
                fsave      = builder.Build();
                timestamp3.Stop();
                int num = fsave.SceneObjectCount + fsave.InstanceObjectCount;
                if (save.friendly)
                {
                    using (FileStream stream = File.Open(path + ".json", FileMode.Create, FileAccess.Write))
                    {
                        JsonFormatWriter writer = JsonFormatWriter.CreateInstance(stream);
                        writer.Formatted();
                        writer.WriteMessage(fsave);
                    }
                }

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

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