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);
        }
    }
Beispiel #2
0
    public bool Import(byte[] buffer, VAOption options)
    {
        if (buffer == null)
        {
            return(false);
        }
        Reset(options);
        int min_iso_size = 48;

        if (buffer.Length < min_iso_size)
        {
            return(false);
        }

        using (MemoryStream ms_iso = new MemoryStream(buffer))
        {
            BinaryReader r = new BinaryReader(ms_iso);

            // Header
            string check_str = r.ReadString();                  // r, string
            if (check_str != "VCISO")
            {
                r.Close();
                return(false);
            }
            int l = 0;
            m_HeadInfo.Version  = r.ReadInt32();                // r, int
            m_HeadInfo.Category = (EVCCategory)(r.ReadInt32()); // r, int
            string author = "";
            if (m_HeadInfo.Version >= 0x02020000)
            {
                author = r.ReadString();
            }
            m_HeadInfo.Name = r.ReadString();                   // r, string
            m_HeadInfo.Desc = r.ReadString();                   // r, string
            string remarks = "";
            if (m_HeadInfo.Version >= 0x02020000)
            {
                remarks = r.ReadString();
            }
            m_HeadInfo.xSize = r.ReadInt32();    // r, int
            m_HeadInfo.ySize = r.ReadInt32();    // r, int
            m_HeadInfo.zSize = r.ReadInt32();    // r, int
            l = r.ReadInt32();                   // r, int
            m_HeadInfo.IconTex = r.ReadBytes(l); // r, byte[]
            //m_HeadInfo.EnsureIconTexValid();

            m_HeadInfo.Author  = "";
            m_HeadInfo.Remarks = "";
            if (m_HeadInfo.Version >= 0x02020000)
            {
                for (int c = 0; c < author.Length; ++c)
                {
                    m_HeadInfo.Author += (char)(author[c] ^ (char)(0xAC));
                }
                for (int c = 0; c < remarks.Length; ++c)
                {
                    m_HeadInfo.Remarks += (char)(remarks[c] ^ (char)(0xAC));
                }
            }

            switch (m_HeadInfo.Version)
            {
            case 0x02000000:
            case 0x02010000:
            case 0x02020000:
            case 0x02020001:
            {
                // Counts
                int mat_cnt = r.ReadInt32();                    // r, int
                int dcl_cnt = 0;
                if (m_HeadInfo.Version >= 0x02010000)
                {
                    dcl_cnt = r.ReadInt32();                    // r, int
                }
                int com_cnt = r.ReadInt32();                    // r, int
                int vxl_cnt = r.ReadInt32();                    // r, int
                int clr_cnt = r.ReadInt32();                    // r, int

                // Materials
                for (int i = 0; i < mat_cnt; ++i)
                {
                    ulong guid = r.ReadUInt64();                        // r, ulong;
                    if (guid != 0)
                    {
                        l = r.ReadInt32();                              // r, int
                        byte[] mat_buffer = r.ReadBytes(l);             // r, byte[]

                        // Option
                        // If for editor, find mats in VCEAssetMgr for the iso, or create mats in VCEAssetMgr.
                        if (m_Options.ForEditor)
                        {
                            //if ( VCEAssetMgr.s_Materials.ContainsKey(guid) )
                            //{
                            //    m_Materials[i] = VCEAssetMgr.s_Materials[guid];
                            //}
                            //else
                            //{
                            //    VAMaterial vcmat = new VAMaterial ();
                            //    vcmat.Import(mat_buffer);
                            //    VCEAssetMgr.s_TempMaterials.Add(guid, vcmat);
                            //    m_Materials[i] = vcmat;
                            //}
                        }
                        // If not for editor, these materials are belong to the iso.
                        else
                        {
                            m_Materials[i] = new VAMaterial();
                            m_Materials[i].Import(mat_buffer);
                        }
                    }
                }
                // Decals
                for (int i = 0; i < dcl_cnt; ++i)
                {
                    ulong guid = r.ReadUInt64();                        // r, ulong;
                    if (guid != 0)
                    {
                        l = r.ReadInt32();                              // r, int
                        byte[] dcl_buffer = r.ReadBytes(l);             // r, byte[]

                        // Option
                        // If for editor, find decals in VCEAssetMgr for the iso, or create decals in VCEAssetMgr.
                        if (m_Options.ForEditor)
                        {
                            if (VCEAssetMgr.s_Decals.ContainsKey(guid))
                            {
                                m_DecalAssets[i] = VCEAssetMgr.s_Decals[guid];
                            }
                            else
                            {
                                VCDecalAsset vcdcl = new VCDecalAsset();
                                vcdcl.Import(dcl_buffer);
                                VCEAssetMgr.s_TempDecals.Add(guid, vcdcl);
                                m_DecalAssets[i] = vcdcl;
                            }
                        }
                        // If not for editor, these decals are belong to the iso.
                        else
                        {
                            m_DecalAssets[i] = new VCDecalAsset();
                            m_DecalAssets[i].Import(dcl_buffer);
                        }
                    }
                }

                // Read compressed data
                //
                using (MemoryStream ms_zip = new MemoryStream())
                {
                    l = r.ReadInt32();                          // r, int
                    ms_zip.Write(r.ReadBytes(l), 0, l);         // r, byte[]	zip, byte[]

                    // Decompress data
                    //
                    using (MemoryStream ms_unzip = new MemoryStream())
                    {
                        ms_zip.Seek((long)(0), SeekOrigin.Begin);
                        IonicZlib.Decompress(ms_zip, ms_unzip);
                        ms_unzip.Seek((long)(0), SeekOrigin.Begin);
                        BinaryReader r_unzip = new BinaryReader(ms_unzip);

                        // Components
                        for (int i = 0; i < com_cnt; ++i)
                        {
                            l = r_unzip.ReadInt32();                                    // unzip, int
                            if (l > 0)
                            {
                                byte[]          com_buffer = r_unzip.ReadBytes(l);                              // unzip, byte[]
                                EVCComponent    com_type   = (EVCComponent)(r_unzip.ReadInt32());               // unzip, int
                                VCComponentData cdata      = VCComponentData.Create(com_type, com_buffer);
                                if (cdata != null)
                                {
                                    //cdata.m_CurrIso = this;
                                    m_Components.Add(cdata);
                                }
                            }
                        }

                        if (m_HeadInfo.Version >= 0x02020001)
                        {
                            ulong sig = r_unzip.ReadUInt64();
                            if (m_HeadInfo.HeadSignature != sig)
                            {
                                Debug.LogError("Check sig failed");
                                return(false);
                            }
                        }

                        // Voxels
                        for (int i = 0; i < vxl_cnt; ++i)
                        {
                            int    key = r_unzip.ReadInt32();                           // unzip, int
                            ushort val = r_unzip.ReadUInt16();                          // unzip, ushort
                            m_Voxels[key] = (VCVoxel)(val);
                        }

                        // Colors
                        for (int i = 0; i < clr_cnt; ++i)
                        {
                            int     key = r_unzip.ReadInt32();                          // unzip, int
                            Color32 val;
                            val.r = r_unzip.ReadByte();                                 // unzip, byte
                            val.g = r_unzip.ReadByte();                                 // unzip, byte
                            val.b = r_unzip.ReadByte();                                 // unzip, byte
                            val.a = r_unzip.ReadByte();                                 // unzip, byte
                            m_Colors.Add(key, val);
                        }
                        r_unzip.Close();
                    }
                }
                break;
            }

            default: return(false);
            }
            r.Close();
            return(true);
        }
    }