コード例 #1
0
ファイル: FileStreamOptions.cs プロジェクト: z77ma/runtime
        public void SettingsArePropagated(FileMode mode, FileAccess access, FileOptions fileOptions)
        {
            string filePath = GetTestFilePath();

            if (mode == FileMode.Open)
            {
                File.Create(filePath).Dispose();
            }

            bool canRead  = (access & FileAccess.Read) != 0;
            bool canWrite = (access & FileAccess.Write) != 0;
            bool isAsync  = (fileOptions & FileOptions.Asynchronous) != 0;

            var options = new FileStreamOptions
            {
                Mode    = mode,
                Access  = access,
                Options = fileOptions
            };

            Validate(new FileStream(filePath, options), filePath, isAsync, canRead, canWrite);
            Validate(File.Open(filePath, options), filePath, isAsync, canRead, canWrite);
            Validate(new FileInfo(filePath).Open(options), filePath, isAsync, canRead, canWrite);

            if (canWrite)
            {
                Validate((FileStream) new StreamWriter(filePath, options).BaseStream, filePath, isAsync, canRead, canWrite);
                Validate((FileStream) new StreamWriter(filePath, Encoding.UTF8, options).BaseStream, filePath, isAsync, canRead, canWrite);
            }

            if (canRead)
            {
                Validate((FileStream) new StreamReader(filePath, options).BaseStream, filePath, isAsync, canRead, canWrite);
                Validate((FileStream) new StreamReader(filePath, Encoding.UTF8, false, options).BaseStream, filePath, isAsync, canRead, canWrite);
            }
コード例 #2
0
        private async Task SaveToFileAsync(Stream stream, string path)
        {
            var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"Provided path ({path}) is not valid.", nameof(path));

            Directory.CreateDirectory(directory);

            // On Windows, saving the file will fail if the file is hidden or readonly
            FileSystem.SetAttributes(path, false, false);

            var fileStreamOptions = new FileStreamOptions()
            {
                Mode              = FileMode.Create,
                Access            = FileAccess.Write,
                Share             = FileShare.None,
                PreallocationSize = stream.Length,
                Options           = FileOptions.Asynchronous
            };

            var filestream = new FileStream(path, fileStreamOptions);

            await using (filestream.ConfigureAwait(false))
            {
                await stream.CopyToAsync(filestream).ConfigureAwait(false);
            }

            if (ConfigurationManager.Configuration.SaveMetadataHidden)
            {
                SetHidden(path, true);
            }
        }
コード例 #3
0
        public static void LackOfWriteAccess_ThrowsArgumentException()
        {
            var readOptions = new FileStreamOptions {
                Access = FileAccess.Read
            };

            AssertExtensions.Throws <ArgumentException>("options", () => new StreamWriter("path", readOptions));
            AssertExtensions.Throws <ArgumentException>("options", () => new StreamWriter("path", Encoding.UTF8, readOptions));
        }
コード例 #4
0
ファイル: BaseXmlSaver.cs プロジェクト: ximliu/jellyfin
        /// <inheritdoc />
        public async Task SaveAsync(BaseItem item, CancellationToken cancellationToken)
        {
            var path      = GetSavePath(item);
            var directory = Path.GetDirectoryName(path) ?? throw new InvalidDataException($"Provided path ({path}) is not valid.");

            Directory.CreateDirectory(directory);

            // On Windows, savint the file will fail if the file is hidden or readonly
            FileSystem.SetAttributes(path, false, false);

            var fileStreamOptions = new FileStreamOptions()
            {
                Mode   = FileMode.Create,
                Access = FileAccess.Write,
                Share  = FileShare.None
            };

            var filestream = new FileStream(path, fileStreamOptions);

            await using (filestream.ConfigureAwait(false))
            {
                var settings = new XmlWriterSettings
                {
                    Indent   = true,
                    Encoding = Encoding.UTF8,
                    Async    = true
                };

                var writer = XmlWriter.Create(filestream, settings);
                await using (writer.ConfigureAwait(false))
                {
                    var root = GetRootElementName(item);

                    await writer.WriteStartDocumentAsync(true).ConfigureAwait(false);

                    await writer.WriteStartElementAsync(null, root, null).ConfigureAwait(false);

                    var baseItem = item;

                    if (baseItem != null)
                    {
                        await AddCommonNodesAsync(baseItem, writer).ConfigureAwait(false);
                    }

                    await WriteCustomElementsAsync(item, writer).ConfigureAwait(false);

                    await writer.WriteEndElementAsync().ConfigureAwait(false);

                    await writer.WriteEndDocumentAsync().ConfigureAwait(false);
                }
            }

            if (ConfigurationManager.Configuration.SaveMetadataHidden)
            {
                SetHidden(path, true);
            }
        }
コード例 #5
0
        public static void LackOfReadAccess_ThrowsArgumentException()
        {
            var noReadAccess = new FileStreamOptions {
                Access = FileAccess.Write
            };

            AssertExtensions.Throws <ArgumentException>("options", () => new StreamReader("path", noReadAccess));
            AssertExtensions.Throws <ArgumentException>("options", () => new StreamReader("path", Encoding.UTF8, false, noReadAccess));
            AssertExtensions.Throws <ArgumentException>("options", () => new StreamReader("path", Encoding.UTF8, true, noReadAccess));
        }
コード例 #6
0
    /// <summary>
    /// Opens a file in the specified mode and other options.
    /// </summary>
    /// <param name="options">An object that describes optional file stream parameters to use.</param>
    /// <returns>A file opened in the specified mode, access options and shared options.</returns>
    /// <exception cref="SecurityException">The caller does not have the required permission.</exception>
    /// <exception cref="FileNotFoundException">The file is not found.</exception>
    /// <exception cref="UnauthorizedAccessException">The file is read-only.</exception>
    /// <exception cref="DirectoryNotFoundException">The specified path is invalid, such as being on an unmapped drive.</exception>
    /// <exception cref="IOException">The file is already open.</exception>
    public FileStream Open(FileStreamOptions options)
    {
        var file = Source;

        if (file == null)
        {
            throw new FileNotFoundException("The file does not specify.");
        }
        return(options == null?file.Open(FileMode.OpenOrCreate) : file.Open(options));
    }
コード例 #7
0
        public FileStream GetPort(string name)
        {
            lock (sync)
            {
                if (ports.TryGetValue(name, out var port))
                {
                    return(port);
                }

                var options = new FileStreamOptions {
                    BufferSize = 0, Access = FileAccess.ReadWrite, Mode = FileMode.Open
                };
                port = File.Open(name, options);
                ports.Add(name, port);
                return(port);
            }
        }
コード例 #8
0
    public static void Main()
    {
        string destinationPath = "destination.dll";

        var openForReading = new FileStreamOptions {
            Mode = FileMode.Open
        };

        using var source = new FileStream(typeof(PreallocationSizeExample).Assembly.Location, openForReading);

        var createForWriting = new FileStreamOptions
        {
            Mode              = FileMode.CreateNew,
            Access            = FileAccess.Write,
            PreallocationSize = source.Length // specify size up-front
        };

        using var destination = new FileStream(destinationPath, createForWriting);

        source.CopyTo(destination); // copies the contents of the assembly file into the destination file
    }
コード例 #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="System.IO.FileStream" /> class with the specified creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, additional file options and the allocation size.
 /// </summary>
 /// <remarks><see cref="System.IO.FileStream(string,System.IO.FileStreamOptions)"/> for information about exceptions.</remarks>
 public FileStream Open(FileStreamOptions options) => File.Open(NormalizedPath, options);
コード例 #10
0
ファイル: CodeGen.cs プロジェクト: Sarofc/tabtool
        internal static void MakeCsharpFile(ExcelData excelData, string codepath)
        {
            //const string k_CsFileName = "CsvData.cs";
            //string csfile = codepath + k_CsFileName;

            var csfile = codepath;

            var sb = new StringBuilder(2048);

            var unit           = new CodeCompileUnit();
            var tableNamespace = new CodeNamespace("Saro.Table");

            unit.Namespaces.Add(tableNamespace);
            tableNamespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
            tableNamespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            tableNamespace.Imports.Add(new CodeNamespaceImport("System.IO"));
            tableNamespace.Imports.Add(new CodeNamespaceImport("System.Text"));

            var enumDefineList = new List <int>();

            #region item class

            var itemClass = new CodeTypeDeclaration(excelData.GetEntityClassName());
            itemClass.TypeAttributes = System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Sealed;
            tableNamespace.Types.Add(itemClass);

            int index = -1;
            enumDefineList.Clear();
            foreach (var header in excelData.header)
            {
                index++;
                if (header.define == TableHelper.HeaderFilter.k_ENUM_KEY)
                {
                    enumDefineList.Add(index);
                }
                if (TableHelper.IgnoreHeader(header))
                {
                    continue;
                }

                if (!TableHelper.s_TypeLut.TryGetValue(header.fieldTypeName, out Type t))
                {
                    throw new Exception("type is not support: " + header.fieldTypeName);
                }
                var memberFiled = new CodeMemberField(t, header.fieldName);
                memberFiled.Attributes = MemberAttributes.Public;

                memberFiled.Comments.Add(new CodeCommentStatement("<summary>", true));
                memberFiled.Comments.Add(new CodeCommentStatement(header.fieldComment, true));
                memberFiled.Comments.Add(new CodeCommentStatement("</summary>", true));
                itemClass.Members.Add(memberFiled);
            }

            #endregion

            #region table class

            var tableClass = new CodeTypeDeclaration(excelData.GetWrapperClassName());
            tableClass.TypeAttributes = System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Sealed;
            tableClass.BaseTypes.Add(new CodeTypeReference("BaseTable", new CodeTypeReference[] { new CodeTypeReference(excelData.GetEntityClassName()), new CodeTypeReference(excelData.GetWrapperClassName()) }));
            tableNamespace.Types.Add(tableClass);

            #region load method
            {
                var loadMethod = new CodeMemberMethod();
                tableClass.Members.Add(loadMethod);
                loadMethod.Name       = "Load";
                loadMethod.ReturnType = new CodeTypeReference(typeof(bool));
                loadMethod.Attributes = MemberAttributes.Override | MemberAttributes.Public;

                sb.AppendLine("\t\t\tif (m_Loaded) return true;");
                sb.AppendLine($"\t\t\tvar bytes = GetBytes(\"{excelData.tablName}.txt\");");
                sb.AppendLine();
                sb.AppendLine("\t\t\tusing (var ms = new MemoryStream(bytes, false))");
                sb.AppendLine("\t\t\t{");
                sb.AppendLine("\t\t\t\tusing (var br = new BinaryReader(ms))");
                sb.AppendLine("\t\t\t\t{");
                sb.AppendLine("\t\t\t\t\tvar version = br.ReadInt32();//version");
                sb.AppendLine("\t\t\t\t\tif (version != TableLoader.k_DataVersion)\n\t\t\t\t\t\tthrow new System.Exception($\"table error version. file:{version}  exe:{TableLoader.k_DataVersion}\");\n");
                sb.AppendLine("\t\t\t\t\tvar dataLen = br.ReadInt32();");
                sb.AppendLine("\t\t\t\t\tfor (int i = 0; i < dataLen; i++)");
                sb.AppendLine("\t\t\t\t\t{");
                sb.AppendLine($"\t\t\t\t\t\tvar data = new {excelData.GetEntityClassName()}();");
                bool first = true;
                foreach (var header in excelData.header)
                {
                    if (TableHelper.IgnoreHeader(header))
                    {
                        continue;
                    }

                    if (!TableHelper.s_TypeLut.TryGetValue(header.fieldTypeName, out Type t))
                    {
                        throw new Exception("type is not support: " + header.fieldTypeName);
                    }
                    if (t == typeof(byte))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadByte();");
                    }
                    else if (t == typeof(int))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadInt32();");
                    }
                    else if (t == typeof(long))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadInt64();");
                    }
                    else if (t == typeof(float))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadSingle();");
                    }
                    else if (t == typeof(bool))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadBoolean();");
                    }
                    else if (t == typeof(string))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadString();");
                    }
                    else if (t == typeof(byte[]))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new byte[len];");
                        sb.AppendLine($"\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}[j] = br.ReadByte();");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                    else if (t == typeof(int[]))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new int[len];");
                        sb.AppendLine($"\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}[j] = br.ReadInt32();");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                    else if (t == typeof(long[]))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new long[len];");
                        sb.AppendLine("\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}[j] = br.ReadInt64();");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                    else if (t == typeof(float[]))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new float[len];");
                        sb.AppendLine("\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}[j] = br.ReadSingle();");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                    else if (t == typeof(Dictionary <int, int>))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new Dictionary<int, int>(len);");
                        sb.AppendLine("\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tvar key = br.ReadInt32();");
                        sb.AppendLine($"\t\t\t\t\t\t\tvar val = br.ReadInt32();");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}.Add(key, val);");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                }
                sb.AppendLine("\t\t\t\t\t\tvar _key = br.ReadUInt64();");
                sb.AppendLine("\t\t\t\t\t\tm_Datas[_key] = data;");
                sb.AppendLine("\t\t\t\t\t}");
                sb.AppendLine("\t\t\t\t}");
                sb.AppendLine("\t\t\t}");
                sb.AppendLine("\t\t\tm_Loaded = true;");
                loadMethod.Statements.Add(new CodeSnippetStatement(sb.ToString()));
                loadMethod.Statements.Add(new CodeMethodReturnStatement(
                                              new CodeSnippetExpression("true")));

                sb.Clear();
            }
            #endregion

            #region loadasync method
            {
                var loadAsyncMethod = new CodeMemberMethod();
                tableClass.Members.Add(loadAsyncMethod);
                loadAsyncMethod.Name       = "LoadAsync";
                loadAsyncMethod.ReturnType = new CodeTypeReference("async System.Threading.Tasks.ValueTask<bool>");
                loadAsyncMethod.Attributes = MemberAttributes.Override | MemberAttributes.Public;

                sb.AppendLine("\t\t\tif (m_Loaded) return true;");
                sb.AppendLine($"\t\t\tvar bytes = await GetBytesAsync(\"{excelData.tablName}.txt\");");
                sb.AppendLine();
                sb.AppendLine("\t\t\tusing (var ms = new MemoryStream(bytes, false))");
                sb.AppendLine("\t\t\t{");
                sb.AppendLine("\t\t\t\tusing (var br = new BinaryReader(ms))");
                sb.AppendLine("\t\t\t\t{");
                sb.AppendLine("\t\t\t\t\tvar version = br.ReadInt32();//version");
                sb.AppendLine("\t\t\t\t\tif (version != TableLoader.k_DataVersion)\n\t\t\t\t\t\tthrow new System.Exception($\"table error version. file:{version}  exe:{TableLoader.k_DataVersion}\");\n");
                sb.AppendLine("\t\t\t\t\tvar dataLen = br.ReadInt32();");
                sb.AppendLine("\t\t\t\t\tfor (int i = 0; i < dataLen; i++)");
                sb.AppendLine("\t\t\t\t\t{");
                sb.AppendLine($"\t\t\t\t\t\tvar data = new {excelData.GetEntityClassName()}();");
                bool first = true;
                foreach (var header in excelData.header)
                {
                    if (TableHelper.IgnoreHeader(header))
                    {
                        continue;
                    }

                    if (!TableHelper.s_TypeLut.TryGetValue(header.fieldTypeName, out Type t))
                    {
                        throw new Exception("type is not support: " + header.fieldTypeName);
                    }
                    if (t == typeof(byte))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadByte();");
                    }
                    else if (t == typeof(int))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadInt32();");
                    }
                    else if (t == typeof(long))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadInt64();");
                    }
                    else if (t == typeof(float))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadSingle();");
                    }
                    else if (t == typeof(bool))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadBoolean();");
                    }
                    else if (t == typeof(string))
                    {
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = br.ReadString();");
                    }
                    else if (t == typeof(byte[]))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new byte[len];");
                        sb.AppendLine($"\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}[j] = br.ReadByte();");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                    else if (t == typeof(int[]))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new int[len];");
                        sb.AppendLine($"\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}[j] = br.ReadInt32();");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                    else if (t == typeof(long[]))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new long[len];");
                        sb.AppendLine("\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}[j] = br.ReadInt64();");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                    else if (t == typeof(float[]))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new float[len];");
                        sb.AppendLine("\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}[j] = br.ReadSingle();");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                    else if (t == typeof(Dictionary <int, int>))
                    {
                        if (first)
                        {
                            sb.AppendLine($"\t\t\t\t\t\tvar len = br.ReadByte();");
                            first = false;
                        }
                        else
                        {
                            sb.AppendLine($"\t\t\t\t\t\tlen = br.ReadByte();");
                        }
                        sb.AppendLine($"\t\t\t\t\t\tdata.{header.fieldName} = new Dictionary<int, int>(len);");
                        sb.AppendLine("\t\t\t\t\t\tfor (int j = 0; j < len; j++)");
                        sb.AppendLine("\t\t\t\t\t\t{");
                        sb.AppendLine($"\t\t\t\t\t\t\tvar key = br.ReadInt32();");
                        sb.AppendLine($"\t\t\t\t\t\t\tvar val = br.ReadInt32();");
                        sb.AppendLine($"\t\t\t\t\t\t\tdata.{header.fieldName}.Add(key, val);");
                        sb.AppendLine("\t\t\t\t\t\t}");
                    }
                }
                sb.AppendLine("\t\t\t\t\t\tvar _key = br.ReadUInt64();");
                sb.AppendLine("\t\t\t\t\t\tm_Datas[_key] = data;");
                sb.AppendLine("\t\t\t\t\t}");
                sb.AppendLine("\t\t\t\t}");
                sb.AppendLine("\t\t\t}");
                sb.AppendLine("\t\t\tm_Loaded = true;");
                loadAsyncMethod.Statements.Add(new CodeSnippetStatement(sb.ToString()));
                loadAsyncMethod.Statements.Add(new CodeMethodReturnStatement(
                                                   new CodeSnippetExpression("true")));

                sb.Clear();
            }
            #endregion

            #region query method
            {
                var keyCount        = excelData.GetKeyCount();
                var keyNames        = excelData.GetKeyNames();
                var combinedKeyName = "__combinedkey";
                if (keyCount == 1)
                {
                    sb.AppendLine($"\t\tpublic static {excelData.GetEntityClassName()} Query(int {keyNames[0]})");
                    sb.AppendLine("\t\t{");
                    sb.AppendLine($"\t\t\tvar {combinedKeyName} = KeyHelper.GetKey({keyNames[0]});");
                }
                else if (keyCount == 2)
                {
                    sb.AppendLine($"\t\tpublic static {excelData.GetEntityClassName()} Query(int {keyNames[0]}, int {keyNames[1]})");
                    sb.AppendLine("\t\t{");
                    sb.AppendLine($"\t\t\tvar {combinedKeyName} = KeyHelper.GetKey({keyNames[0]}, {keyNames[1]});");
                }
                else if (keyCount == 3)
                {
                    sb.AppendLine($"\t\tpublic static {excelData.GetEntityClassName()} Query(int {keyNames[0]}, int {keyNames[1]}, int {keyNames[2]})");
                    sb.AppendLine("\t\t{");
                    sb.AppendLine($"\t\t\tvar {combinedKeyName} = KeyHelper.GetKey({keyNames[0]}, {keyNames[1]}, {keyNames[2]});");
                }
                else if (keyCount == 4)
                {
                    sb.AppendLine($"\t\tpublic static {excelData.GetEntityClassName()} Query(int {keyNames[0]}, int {keyNames[1]}, int {keyNames[2]}, int {keyNames[3]})");
                    sb.AppendLine("\t\t{");
                    sb.AppendLine($"\t\t\tvar {combinedKeyName} = KeyHelper.GetKey({keyNames[0]}, {keyNames[1]}, {keyNames[2]}, {keyNames[3]});");
                }

                sb.AppendLine($"\t\t\tif (!Get().Load()) throw new System.Exception(\"load table failed.type: \" + nameof({excelData.GetEntityClassName()}));");
                sb.AppendLine($"\t\t\tif (Get().m_Datas.TryGetValue({combinedKeyName}, out {excelData.GetEntityClassName()} t))");
                sb.AppendLine("\t\t\t{");
                sb.AppendLine("\t\t\t\treturn t;");
                sb.AppendLine("\t\t\t}");
                sb.AppendLine($"\t\t\tthrow new System.Exception(\"null table. type: \" + nameof({excelData.GetEntityClassName()}));");
                sb.AppendLine("\t\t}");

                var queryMethod = new CodeSnippetTypeMember(sb.ToString());

                tableClass.Members.Add(queryMethod);

                sb.Clear();
            }
            #endregion


            #region queryasync method
            {
                var keyCount        = excelData.GetKeyCount();
                var keyNames        = excelData.GetKeyNames();
                var combinedKeyName = "__combinedkey";
                if (keyCount == 1)
                {
                    sb.AppendLine($"\t\tpublic static async System.Threading.Tasks.ValueTask<{excelData.GetEntityClassName()}> QueryAsync(int {keyNames[0]})");
                    sb.AppendLine("\t\t{");
                    sb.AppendLine($"\t\t\tvar {combinedKeyName} = KeyHelper.GetKey({keyNames[0]});");
                }
                else if (keyCount == 2)
                {
                    sb.AppendLine($"\t\tpublic static async System.Threading.Tasks.ValueTask<{excelData.GetEntityClassName()}> QueryAsync(int {keyNames[0]}, int {keyNames[1]})");
                    sb.AppendLine("\t\t{");
                    sb.AppendLine($"\t\t\tvar {combinedKeyName} = KeyHelper.GetKey({keyNames[0]}, {keyNames[1]});");
                }
                else if (keyCount == 3)
                {
                    sb.AppendLine($"\t\tpublic static async System.Threading.Tasks.ValueTask<{excelData.GetEntityClassName()}> QueryAsync(int {keyNames[0]}, int {keyNames[1]}, int {keyNames[2]})");
                    sb.AppendLine("\t\t{");
                    sb.AppendLine($"\t\t\tvar {combinedKeyName} = KeyHelper.GetKey({keyNames[0]}, {keyNames[1]}, {keyNames[2]});");
                }
                else if (keyCount == 4)
                {
                    sb.AppendLine($"\t\tpublic static async System.Threading.Tasks.ValueTask<{excelData.GetEntityClassName()}> QueryAsync(int {keyNames[0]}, int {keyNames[1]}, int {keyNames[2]}, int {keyNames[3]})");
                    sb.AppendLine("\t\t{");
                    sb.AppendLine($"\t\t\tvar {combinedKeyName} = KeyHelper.GetKey({keyNames[0]}, {keyNames[1]}, {keyNames[2]}, {keyNames[3]});");
                }

                sb.AppendLine($"\t\t\tvar result = await Get().LoadAsync();");
                sb.AppendLine($"\t\t\tif (!result) throw new System.Exception(\"load table failed.type: \" + nameof({excelData.GetEntityClassName()}));");
                sb.AppendLine($"\t\t\tif (Get().m_Datas.TryGetValue({combinedKeyName}, out {excelData.GetEntityClassName()} t))");
                sb.AppendLine("\t\t\t{");
                sb.AppendLine("\t\t\t\treturn t;");
                sb.AppendLine("\t\t\t}");
                sb.AppendLine($"\t\t\tthrow new System.Exception(\"null table. type: \" + nameof({excelData.GetEntityClassName()}));");
                sb.AppendLine("\t\t}");

                var queryMethod = new CodeSnippetTypeMember(sb.ToString());

                tableClass.Members.Add(queryMethod);

                sb.Clear();
            }
            #endregion


            #region PrintTable method

            var printTableMethod = new CodeMemberMethod();
            tableClass.Members.Add(printTableMethod);
            printTableMethod.Name       = "PrintTable";
            printTableMethod.ReturnType = new CodeTypeReference(typeof(string));
            printTableMethod.Attributes = MemberAttributes.Final | MemberAttributes.Public;

            sb.AppendLine("\t\t\tStringBuilder sb = null;");

            sb.AppendLine("#if ENABLE_TABLE_LOG");

            sb.AppendLine("\t\t\tsb = new StringBuilder(2048);");

            sb.AppendLine("\t\t\tforeach (var data in m_Datas.Values)");
            sb.AppendLine("\t\t\t{");
            foreach (var header in excelData.header)
            {
                if (TableHelper.IgnoreHeader(header))
                {
                    continue;
                }

                if (!TableHelper.s_TypeLut.TryGetValue(header.fieldTypeName, out Type t))
                {
                    throw new Exception("type is not support: " + header.fieldTypeName);
                }

                if (t.IsValueType ||
                    t == typeof(string))
                {
                    sb.AppendLine($"\t\t\t\tsb.Append(data.{header.fieldName}).Append(\"\\t\");");
                }
                else if (t == typeof(byte[]) ||
                         t == typeof(int[]) ||
                         t == typeof(long[]) ||
                         t == typeof(float[]))
                {
                    sb.AppendLine($"\t\t\t\tsb.Append(string.Join(\",\", data.{header.fieldName})).Append(\"\\t\");");
                }
                else if (t == typeof(Dictionary <int, int>))
                {
                    sb.AppendLine($"\t\t\t\tsb.Append(string.Join(\",\", data.{header.fieldName})).Append(\"\\t\");");
                }
            }
            sb.AppendLine("\t\t\t\tsb.AppendLine();");
            sb.AppendLine("\t\t\t}");

            sb.AppendLine("#endif");

            printTableMethod.Statements.Add(new CodeSnippetStatement(sb.ToString()));
            printTableMethod.Statements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression("sb?.ToString()")));

            sb.Clear();

            #endregion

            #endregion

            #region enum define

            if (enumDefineList.Count > 0)
            {
                var enumType = new CodeTypeDeclaration(excelData.GetEnumName());
                enumType.IsEnum = true;
                tableNamespace.Types.Add(enumType);
                for (int j = 0; j < excelData.rowValues.Count; j++)
                {
                    for (int k = 0; k < enumDefineList.Count; k++)
                    {
                        sb.Append(excelData.rowValues[j][enumDefineList[k]]);
                        if (k != enumDefineList.Count - 1)
                        {
                            sb.Append("_");
                        }
                    }
                    enumType.Members.Add(new CodeMemberField()
                    {
                        Name = sb.ToString(), InitExpression = new CodePrimitiveExpression(int.Parse(excelData.rowValues[j][0]))
                    });
                    sb.Clear();
                }
            }

            #endregion

            var options = new FileStreamOptions {
                Mode = FileMode.Create, Access = FileAccess.Write, Share = FileShare.ReadWrite
            };

            var tw = new IndentedTextWriter(new StreamWriter(csfile, options), "\t");
            {
                var provider = new CSharpCodeProvider();
                tw.WriteLine("//------------------------------------------------------------------------------");
                tw.WriteLine("// File   : {0}", Path.GetFileName(codepath));
                tw.WriteLine("// Author : Saro");
                tw.WriteLine("// Time   : {0}", DateTime.Now.ToString());
                tw.WriteLine("//------------------------------------------------------------------------------");
                provider.GenerateCodeFromCompileUnit(unit, tw, new CodeGeneratorOptions()
                {
                    BracingStyle = "C"
                });
                tw.Close();
            }
        }
コード例 #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="System.IO.FileStream" /> class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, additional file options and the allocation size.
 /// </summary>
 /// <remarks><see cref="System.IO.FileStream(string,System.IO.FileStreamOptions)"/> for information about exceptions.</remarks>
 public static FileStream Open(string path, FileStreamOptions options) => new FileStream(path, options);