示例#1
0
        internal static void InitDefaultMethod()
        {
            if (DualityApp.ExecEnvironment == DualityApp.ExecutionEnvironment.Editor)
            {
                defaultMethod = FormattingMethod.Xml;
            }
            else
            {
                defaultMethod = FormattingMethod.Binary;
            }

            if (Directory.Exists(DualityApp.DataDirectory))
            {
                foreach (string anyResource in Directory.EnumerateFiles(DualityApp.DataDirectory, "*" + Resource.FileExt, SearchOption.AllDirectories))
                {
                    using (FileStream stream = File.OpenRead(anyResource))
                    {
                        try
                        {
                            defaultMethod = XmlFormatter.IsXmlStream(stream) ? FormattingMethod.Xml : FormattingMethod.Binary;
                            break;
                        }
                        catch (Exception) {}
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Creates a new Formatter using the specified stream for I/O.
        /// </summary>
        /// <param name="stream">The stream to use.</param>
        /// <param name="method">
        /// The formatting method to prefer. If <see cref="FormattingMethod.Unknown"/> is specified, if the stream
        /// is read- and seekable, auto-detection is used. Otherwise, the <see cref="DefaultMethod">default formatting method</see> is used.
        /// </param>
        /// <returns>A newly created Formatter meeting the specified criteria.</returns>
        public static Formatter Create(Stream stream, FormattingMethod method = FormattingMethod.Unknown)
        {
            if (method == FormattingMethod.Unknown)
            {
                if (stream.CanRead && stream.CanSeek && stream.Length > 0)
                {
                    if (XmlFormatter.IsXmlStream(stream))
                    {
                        method = FormattingMethod.Xml;
                    }
                    else
                    {
                        method = FormattingMethod.Binary;
                    }
                }
                else
                {
                    method = defaultMethod;
                }
            }

            if (method == FormattingMethod.Xml)
            {
                return(new XmlFormatter(stream));
            }
            else
            {
                return(new BinaryFormatter(stream));
            }
        }