Beispiel #1
0
    public byte[] Export()
    {
        using (MemoryStream ms_iso = new MemoryStream())
        {
            BinaryWriter w = new BinaryWriter(ms_iso);

            w.Write("VCISO");                   // w, string

            // Header
            m_HeadInfo.EnsureIconTexValid();
            w.Write(ISO_VERSION);                // w, int
            w.Write((int)(m_HeadInfo.Category)); // w, int
            string author = "";
            for (int c = 0; c < m_HeadInfo.Author.Length; ++c)
            {
                author += (char)(m_HeadInfo.Author[c] ^ (char)(0xAC));
            }
            w.Write(author);                    // w, string (v2.2)
            w.Write(m_HeadInfo.Name);           // w, string
            w.Write(m_HeadInfo.Desc);           // w, string
            string remarks = "";
            for (int c = 0; c < m_HeadInfo.Remarks.Length; ++c)
            {
                remarks += (char)(m_HeadInfo.Remarks[c] ^ (char)(0xAC));
            }
            w.Write(remarks);                                          // w, string (v2.2)
            w.Write(m_HeadInfo.xSize);                                 // w, int
            w.Write(m_HeadInfo.ySize);                                 // w, int
            w.Write(m_HeadInfo.zSize);                                 // w, int
            w.Write(m_HeadInfo.IconTex.Length);                        // w, int
            w.Write(m_HeadInfo.IconTex, 0, m_HeadInfo.IconTex.Length); // w, byte[]

            // Counts
            w.Write(MAT_ARR_CNT);               // w, int
            w.Write(DECAL_ARR_CNT);             // w, int (v2.1)
            w.Write(m_Components.Count);        // w, int
            w.Write(m_Voxels.Count);            // w, int
            w.Write(m_Colors.Count);            // w, int

            // Materials
            for (int i = 0; i < MAT_ARR_CNT; ++i)
            {
                if (m_Materials[i] != null)
                {
                    byte[] mat_buffer = m_Materials[i].Export();
                    w.Write(m_Materials[i].m_Guid);                     // w, ulong
                    w.Write(mat_buffer.Length);                         // w, int
                    w.Write(mat_buffer, 0, mat_buffer.Length);          // w, byte[]
                }
                else
                {
                    ulong zerolong = (ulong)(0);
                    w.Write(zerolong);                          // w, ulong
                }
            }

            // Decals (v2.1)
            for (int i = 0; i < DECAL_ARR_CNT; ++i)
            {
                if (m_DecalAssets[i] != null)
                {
                    byte[] dcl_buffer = m_DecalAssets[i].Export();
                    w.Write(m_DecalAssets[i].m_Guid);                   // w, ulong
                    w.Write(dcl_buffer.Length);                         // w, int
                    w.Write(dcl_buffer, 0, dcl_buffer.Length);          // w, byte[]
                }
                else
                {
                    ulong zerolong = (ulong)(0);
                    w.Write(zerolong);                          // w, ulong
                }
            }

            // Fill data will be Compressed ( Components, Voxels, Colors )
            //
            using (MemoryStream ms_zip = new MemoryStream())
            {
                BinaryWriter w_zip = new BinaryWriter(ms_zip);

                // Components
                foreach (VCComponentData cdata in m_Components)
                {
                    if (cdata != null)
                    {
                        byte[] com_buffer = cdata.Export();
                        w_zip.Write(com_buffer.Length);                         // zip, int
                        w_zip.Write(com_buffer, 0, com_buffer.Length);          // zip, byte[]
                        w_zip.Write((int)(cdata.m_Type));                       // zip, int
                    }
                    else
                    {
                        int zero = 0;
                        w.Write(zero);                          // w, int
                    }
                }

                w_zip.Write(m_HeadInfo.HeadSignature);

                // Voxels
                foreach (KeyValuePair <int, VCVoxel> kvp in m_Voxels)
                {
                    w_zip.Write(kvp.Key);                       // zip, int
                    w_zip.Write((ushort)(kvp.Value));           // zip, ushort
                }

                // Colors
                foreach (KeyValuePair <int, Color32> kvp in m_Colors)
                {
                    w_zip.Write(kvp.Key);                       // zip, int
                    w_zip.Write(kvp.Value.r);                   // zip, byte
                    w_zip.Write(kvp.Value.g);                   // zip, byte
                    w_zip.Write(kvp.Value.b);                   // zip, byte
                    w_zip.Write(kvp.Value.a);                   // zip, byte
                }

                // Compress data
                //
                using (MemoryStream ms_ziped = new MemoryStream())
                {
                    ms_zip.Seek((long)(0), SeekOrigin.Begin);
                    IonicZlib.Compress(ms_zip, ms_ziped);
                    w.Write((int)(ms_ziped.Length));                            // w, int
                    w.Write(ms_ziped.GetBuffer(), 0, (int)(ms_ziped.Length));   // w, byte[]
                }
                w_zip.Close();
            }
            w.Close();
            byte [] retval = ms_iso.ToArray();
            return(retval);
        }
    }