Write() public static méthode

public static Write ( this file, Action writer, FileMode, mode = FileMode.Create, FileAccess, access = FileAccess.Write, FileShare, share = FileShare.None ) : void
file this
writer Action
mode FileMode,
access FileAccess,
share FileShare,
Résultat void
        public bool SaveTextToFile(WorkspacePath filePath, string text, bool autoCreate = false)
        {
            if (Exists(filePath))
            {
                Delete(filePath);
            }

            // TODO need to look into how to clear the bytes before writing to it?
            var file = CreateFile(filePath);

            if (file != null)
            {
                var bytes = Encoding.ASCII.GetBytes(text);
                StreamExtensions.Write(file, bytes);

                file.Close();

                return(true);
            }

            return(false);
        }
Exemple #2
0
        public static void WriteMeshData(Stream stream, Pmo pmo)
        {
            bool hasSwappedToSecondModel = false;

            // Write Mesh Data.
            for (int j = 0; j < pmo.Meshes.Count; j++)
            {
                if (!hasSwappedToSecondModel && pmo.Meshes[j].MeshNumber == 1 && pmo.header.MeshOffset0 != 0)
                {
                    hasSwappedToSecondModel = true;

                    for (uint b = 0; b < 0xC; b++)
                    {
                        stream.Write((byte)0x00);
                    }

                    for (uint b = 0; stream.Position % 0x10 != 0; b++)
                    {
                        stream.Write((byte)0x00);
                    }
                }

                MeshChunks chunk = pmo.Meshes[j];

                Mapping.WriteObject <Pmo.MeshSection>(stream, chunk.SectionInfo);
                if (chunk.SectionInfo_opt1 != null)
                {
                    Mapping.WriteObject <Pmo.MeshSectionOptional1>(stream, chunk.SectionInfo_opt1);
                }
                if (chunk.SectionInfo_opt2 != null)
                {
                    Mapping.WriteObject <Pmo.MeshSectionOptional2>(stream, chunk.SectionInfo_opt2);
                }

                if (chunk.TriangleStripValues.Length > 0)
                {
                    for (int z = 0; z < chunk.TriangleStripValues.Length; z++)
                    {
                        stream.Write((ushort)chunk.TriangleStripValues[z]);
                    }
                }

                for (int k = 0; k < pmo.Meshes[j].SectionInfo.VertexCount; k++)
                {
                    long vertexStartPos       = stream.Position;
                    int  vertexIncreaseAmount = 0;

                    VertexFlags flags = Pmo.GetFlags(chunk.SectionInfo);

                    // Write Joints.
                    if (flags.WeightFormat != CoordinateFormat.NO_VERTEX)
                    {
                        for (int w = 0; w < flags.SkinningWeightsCount + 1; w++)
                        {
                            int currentIndex = w + (k * (flags.SkinningWeightsCount + 1));

                            switch (flags.WeightFormat)
                            {
                            case CoordinateFormat.NORMALIZED_8_BITS:
                                stream.Write((byte)(chunk.jointWeights[k].weights[currentIndex] * 127.0f));
                                break;

                            case CoordinateFormat.NORMALIZED_16_BITS:
                                stream.Write((byte)(chunk.jointWeights[k].weights[currentIndex] * 32767.0f));
                                break;

                            case CoordinateFormat.FLOAT_32_BITS:
                                StreamExtensions.Write(stream, chunk.jointWeights[k].weights[currentIndex]);
                                break;
                            }
                        }
                    }

                    // Write Texture Coords.
                    switch (flags.TextureCoordinateFormat)
                    {
                    case CoordinateFormat.NORMALIZED_8_BITS:
                        stream.Write((byte)(chunk.textureCoordinates[k].X * 128.0f));
                        stream.Write((byte)(chunk.textureCoordinates[k].Y * 128.0f));
                        break;

                    case CoordinateFormat.NORMALIZED_16_BITS:
                        vertexIncreaseAmount = ((0x2 - (Convert.ToInt32(stream.Position - vertexStartPos) & 0x1)) & 0x1);
                        for (int a = 0; a < vertexIncreaseAmount; a++)
                        {
                            stream.Write((byte)0xAB);
                        }

                        stream.Write((ushort)(chunk.textureCoordinates[k].X * 32768.0f));
                        stream.Write((ushort)(chunk.textureCoordinates[k].Y * 32768.0f));
                        break;

                    case CoordinateFormat.FLOAT_32_BITS:
                        vertexIncreaseAmount = ((0x4 - (Convert.ToInt32(stream.Position - vertexStartPos) & 0x3)) & 0x3);
                        for (int a = 0; a < vertexIncreaseAmount; a++)
                        {
                            stream.Write((byte)0xAB);
                        }

                        StreamExtensions.Write(stream, chunk.textureCoordinates[k].X);
                        StreamExtensions.Write(stream, chunk.textureCoordinates[k].Y);
                        break;
                    }

                    // Write colors.
                    switch (flags.ColorFormat)
                    {
                    case Pmo.ColorFormat.NO_COLOR:
                        break;

                    case Pmo.ColorFormat.BGR_5650_16BITS:
                        break;

                    case Pmo.ColorFormat.ABGR_5551_16BITS:
                        break;

                    case Pmo.ColorFormat.ABGR_4444_16BITS:
                        break;

                    case Pmo.ColorFormat.ABGR_8888_32BITS:
                        vertexIncreaseAmount = ((0x4 - (Convert.ToInt32(stream.Position - vertexStartPos) & 0x3)) & 0x3);
                        for (int a = 0; a < vertexIncreaseAmount; a++)
                        {
                            stream.Write((byte)0xAB);
                        }

                        stream.Write((byte)(chunk.colors[k].X));
                        stream.Write((byte)(chunk.colors[k].Y));
                        stream.Write((byte)(chunk.colors[k].Z));
                        stream.Write((byte)(chunk.colors[k].W));
                        break;
                    }

                    // Write vertices.
                    switch (flags.PositionFormat)
                    {
                    case CoordinateFormat.NORMALIZED_8_BITS:
                        StreamExtensions.Write(stream, (sbyte)(chunk.vertices[k].X * 128.0f));
                        StreamExtensions.Write(stream, (sbyte)(chunk.vertices[k].Y * 128.0f));
                        StreamExtensions.Write(stream, (sbyte)(chunk.vertices[k].Z * 128.0f));
                        break;

                    case CoordinateFormat.NORMALIZED_16_BITS:
                        vertexIncreaseAmount = ((0x2 - (Convert.ToInt32(stream.Position - vertexStartPos) & 0x1)) & 0x1);
                        for (int a = 0; a < vertexIncreaseAmount; a++)
                        {
                            stream.Write((byte)0xAB);
                        }

                        StreamExtensions.Write(stream, (short)(chunk.vertices[k].X * 32768.0f));
                        StreamExtensions.Write(stream, (short)(chunk.vertices[k].Y * 32768.0f));
                        StreamExtensions.Write(stream, (short)(chunk.vertices[k].Z * 32768.0f));
                        break;

                    case CoordinateFormat.FLOAT_32_BITS:
                        vertexIncreaseAmount = ((0x4 - (Convert.ToInt32(stream.Position - vertexStartPos) & 0x3)) & 0x3);
                        for (int a = 0; a < vertexIncreaseAmount; a++)
                        {
                            stream.Write((byte)0xAB);
                        }

                        StreamExtensions.Write(stream, chunk.vertices[k].X);
                        StreamExtensions.Write(stream, chunk.vertices[k].Y);
                        StreamExtensions.Write(stream, chunk.vertices[k].Z);
                        break;
                    }

                    int padding = ((int)vertexStartPos + chunk.SectionInfo.VertexSize) - (int)stream.Position;
                    for (int p = 0; p < padding; p++)
                    {
                        stream.Write((byte)0xAB);
                    }
                }

                // Remainder.
                uint remainder = (uint)stream.Position % 4;
                for (int p = 0; p < remainder; p++)
                {
                    stream.Write((byte)0x00);
                }

                if (j == (pmo.Meshes.Count - 1))
                {
                    for (uint b = 0; b < 0xC; b++)
                    {
                        try
                        {
                            stream.Write((byte)0x00);
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                        }
                    }


                    int pd = (int)stream.Position % 0x10;

                    for (int n = 0; pd != 0; n++)
                    {
                        try
                        {
                            stream.Write((byte)0x00);
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                        }

                        pd = (int)stream.Position % 0x10;
                    }
                }
            }
        }
        public void NullExceptionTest()
        {
            AssertThat(() => StreamExtensions.Write(stream, (byte[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((sbyte[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((ushort[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((short[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((char[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((uint[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((int[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((long[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((ulong[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write(null, 64)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((float[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((double[])null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write((string)null)).ThrowsExactlyException <ArgumentNullException>();

            AssertThat(() => stream.ReadString(null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.ReadString(64, null)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream.Write(string.Empty, null)).ThrowsExactlyException <ArgumentNullException>();

            Stream stream2 = null;

            AssertThat(() => StreamExtensions.Write(stream2, new byte[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new sbyte[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new ushort[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new short[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new char[] { 'a' })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new uint[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new int[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new long[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new ulong[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new BigInteger[] { new BigInteger(0) }, 64)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new float[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new double[] { 0 })).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(string.Empty)).ThrowsExactlyException <ArgumentNullException>();

            AssertThat(() => stream2.Write((byte)0)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write((sbyte)0)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write((ushort)0)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write((short)0)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write((char)0)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(0u)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(0)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(0L)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(0UL)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(new BigInteger(0), 64)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(0f)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.Write(0d)).ThrowsExactlyException <ArgumentNullException>();

            AssertThat(() => stream2.ReadUInt8()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadInt8()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadUInt16()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadInt16()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadUInt32()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadInt32()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadUInt64()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadInt64()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadFloat32()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadFloat64()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadBigInteger(64)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadChar()).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadString(64)).ThrowsExactlyException <ArgumentNullException>();
            AssertThat(() => stream2.ReadString()).ThrowsExactlyException <ArgumentNullException>();
        }
        public void SaveExporterFiles(Dictionary <string, byte[]> files)
        {
            // TODO the dictionary string should be converted into a Workspace path

            // Save all the files to the disk
            foreach (var file in files)
            {
                try
                {
                    // Anything that is not in the "/Workspace/" root is routed to save into the tmp directory
                    var path = WorkspacePath.Parse(file.Key);

                    //                    Console.WriteLine("Save Exported file " + file.Key + " to " + path);

                    Stream stream;


                    if (Exists(path))
                    {
                        stream = OpenFile(path, FileAccess.ReadWrite);
                    }
                    else
                    {
                        // Make sure we have the correct directory structure
                        if (path.IsFile)
                        {
                            // Get the parent path
                            var parent = path.ParentPath;

                            if (!Exists(parent))
                            {
                                this.CreateDirectoryRecursive(parent);
                            }
                        }

                        // Create a new file
                        // Create a new file
                        stream = CreateFile(path);
                    }

                    // TODO need to write to the file
                    if (file.Value != null)
                    {
                        // Clear the file contents before writing to it
                        stream.SetLength(0);

                        // Write the byte data to it
                        StreamExtensions.Write(stream, file.Value);

                        stream.Close();
                        // TODO make sure we dispose of the stream?
                        stream.Dispose();
                    }
                }

                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }