コード例 #1
0
ファイル: Formatter.cs プロジェクト: sorceron2000/duality
        /// <summary>
        /// Creates a new MetaFormat 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 MetaFormat Formatter meeting the specified criteria.</returns>
        public static Formatter CreateMeta(Stream stream, FormattingMethod method = FormattingMethod.Unknown)
        {
            if (method == FormattingMethod.Unknown)
            {
                if (stream.CanRead && stream.CanSeek && stream.Length > 0)
                {
                    if (XmlFormatterBase.IsXmlStream(stream))
                    {
                        method = FormattingMethod.Xml;
                    }
                    else
                    {
                        method = FormattingMethod.Binary;
                    }
                }
                else
                {
                    method = defaultMethod;
                }
            }

            if (method == FormattingMethod.Xml)
            {
                return(new MetaFormat.XmlMetaFormatter(stream));
            }
            else
            {
                return(new MetaFormat.BinaryMetaFormatter(stream));
            }
        }
コード例 #2
0
ファイル: Formatter.cs プロジェクト: sorceron2000/duality
        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 = XmlFormatterBase.IsXmlStream(stream) ? FormattingMethod.Xml : FormattingMethod.Binary;
                            break;
                        }
                        catch (Exception) {}
                    }
                }
            }
        }