Ejemplo n.º 1
0
        public void Close(bool write)
        {
            if (!write)
            {
                return;
            }

            var fileWriter = new FileWriter();
            var headerSize = 5;

            fileWriter.WriteInt32(FileCount);
            for (int i = 0; i < FileCount; i++)
            {
                fileWriter.WriteString(this[i].Name);
                fileWriter.WriteInt32(this[i].Offset);
                fileWriter.WriteInt32(this[i].Size);
                headerSize += this[i].Name.Length + 9;
            }
            fileWriter.WriteByte(0x00);

            for (int i = 0; i < FileCount; i++)
            {
                fileWriter.Position = this[i].Offset;
                fileWriter.WriteByteArray(this[i].Bytes);
            }

            var uncompressed   = ((MemoryStream)fileWriter.BaseStream).ToArray();
            var compressedBody = LzmaUtils.Compress(uncompressed);

            fileWriter = new FileWriter();
            fileWriter.WriteString("UnityWeb");
            fileWriter.WriteInt32(3);
            fileWriter.WriteString("3.x.x");
            fileWriter.WriteString("4.6.1f1");
            fileWriter.WriteInt32(compressedBody.Length + 60);
            fileWriter.WriteInt32(60);
            fileWriter.WriteInt32(1);
            fileWriter.WriteInt32(1);
            fileWriter.WriteInt32(compressedBody.Length);
            fileWriter.WriteInt32(uncompressed.Length);
            fileWriter.WriteInt32(compressedBody.Length + 60);
            fileWriter.WriteInt32(headerSize);
            fileWriter.WriteByte(0x00);
            fileWriter.WriteByteArray(compressedBody);
            fileWriter.WriteByte(0x00);

            File.WriteAllBytes(FilePath, ((MemoryStream)fileWriter.BaseStream).ToArray());

            this.CompressedSize = fileWriter.BaseStream.Length;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves this <see cref="CsvTable"/> on disk with the specified path and with compression
        /// if specified.
        /// </summary>
        /// <param name="path">Path of the .csv to save to.</param>
        /// <param name="compressed">Whether to compress the file or not.</param>
        public void Save(string path, bool compressed)
        {
            var csvBuilder = new StringBuilder();

            for (int i = 0; i < Table.Columns.Count - 1; i++) // write column names.
            {
                csvBuilder.Append(Columns[i].ColumnName + ",");
            }
            csvBuilder.AppendLine(Columns[Columns.Count - 1].ColumnName);

            for (int i = 0; i < Table.Columns.Count; i++)                         // write TypesRow
            {
                var format   = i == Table.Columns.Count - 1 ? "{0}\r\n" : "{0},"; // check if last in array
                var dataType = Table.Columns[i].DataType;
                if (dataType == typeof(int))
                {
                    csvBuilder.AppendFormat(format, "int");
                    continue;
                }
                csvBuilder.AppendFormat(format, dataType.Name);
            }

            for (int i = 0; i < Table.Rows.Count; i++) // writes all rows
            {
                csvBuilder.AppendLine(string.Join(",", Table.Rows[i].ItemArray));
            }

            if (compressed) // checks compression
            {
                var bytes = LzmaUtils.Compress(Encoding.UTF8.GetBytes(csvBuilder.ToString()));
                using (var mem = new MemoryStream(bytes))
                {
                    mem.Write(bytes, 0, 9);
                    mem.Write(bytes, 12, bytes.Length - 13);
                    File.WriteAllBytes(path, mem.ToArray());
                }
            }
            else
            {
                File.WriteAllText(path, csvBuilder.ToString());
            }
        }