Beispiel #1
0
        public byte[] Export(StringEntry[] Strings)
        {
            ScriptHeader NewHeader = new ScriptHeader();

            Tools.CopyStruct(Header, ref NewHeader);

            MemoryStream UnkData    = new MemoryStream();
            MemoryStream OffsetData = new MemoryStream();
            MemoryStream StringData = new MemoryStream();

            MemoryStream Reader = new MemoryStream(Script);

            Reader.Seek(0x10, SeekOrigin.Begin);

            Algo.CopyStream(Reader, UnkData, NewHeader.UnkCount * 8);
            Reader.Close();

            StructWriter OffsetWriter = new StructWriter(OffsetData, false, Encoding);
            StructWriter StringWriter = new StructWriter(StringData, false, Encoding);


            for (uint i = 0; i < EntryCount; i++)
            {
                OffsetWriter.Write((uint)StringWriter.BaseStream.Length);
                StringWriter.WriteStruct(ref Strings[i]);
            }
            OffsetWriter.Seek(0, SeekOrigin.Begin);
            StringWriter.Seek(0, SeekOrigin.Begin);

            NewHeader.ScriptLength = (uint)(OffsetWriter.BaseStream.Length + StringWriter.BaseStream.Length + UnkData.Length);
            NewHeader.OffsetTable  = (uint)UnkData.Length;
            NewHeader.StringTable  = (uint)(UnkData.Length + OffsetData.Length);

            byte[] Output = new byte[0x10 + UnkData.Length + OffsetData.Length + StringData.Length];

            Tools.BuildStruct(ref NewHeader, false, Encoding).CopyTo(Output, 0);

            UnkData.ToArray().CopyTo(Output, 0x10);
            OffsetData.ToArray().CopyTo(Output, 0x10 + UnkData.Length);
            StringData.ToArray().CopyTo(Output, 0x10 + UnkData.Length + OffsetData.Length);

            UnkData.Close();
            StringWriter.Close();
            OffsetWriter.Close();

            return(Compress(Output));
        }
Beispiel #2
0
        //作者:Wetor
        //时间:2019.1.18
        public void PngToCZ1(string outfile)
        {
            Bitmap       Picture = new Bitmap(File.Open(outfile, FileMode.Open));
            StructWriter Writer  = new StructWriter(File.Open(outfile + ".cz1", FileMode.Create));
            CZ1Header    header;

            header.Signature    = "CZ1";
            header.HeaderLength = 0x10;
            header.Width        = (ushort)Picture.Width;
            header.Heigth       = (ushort)Picture.Height;
            header.Colorbits    = 8;
            Writer.WriteStruct(ref header);
            Writer.Seek(header.HeaderLength, SeekOrigin.Begin);


            Pixel32_BGRA Pixel = new Pixel32_BGRA();

            Pixel.R = 255;
            Pixel.G = 255;
            Pixel.B = 255;
            for (int k = 0; k < 256; k++)
            {
                Pixel.A = (byte)k;
                Writer.WriteStruct(ref Pixel);
            }

            byte[] bytes = new byte[Picture.Height * Picture.Width];
            int    i     = 0;

            for (int y = 0; y < Picture.Height; y++)
            {
                for (int x = 0; x < Picture.Width; x++)
                {
                    bytes[i] = Picture.GetPixel(x, y).A;
                    i++;
                }
            }


            int file_num = bytes.Length / 130554 + 1;
            //System.Diagnostics.Debug.WriteLine("{0} {1} {2}", file_num, listBytes.Count, 130554);
            List <int[]> out_list = new List <int[]>();

            Writer.Write(file_num);
            for (int k = 0; k < file_num; k++)
            {
                List <int>    listBytes    = new List <int>();
                StringBuilder decompressed = new StringBuilder();
                byte[]        tmp_bytes    = new byte[130554];
                if (k == file_num - 1)
                {
                    Array.Copy(bytes, k * 130554, tmp_bytes, 0, bytes.Length - k * 130554);
                }
                else
                {
                    Array.Copy(bytes, k * 130554, tmp_bytes, 0, 130554);
                }


                foreach (char kk in tmp_bytes)
                {
                    decompressed.Append(kk);
                }
                listBytes = LzwUtil.Compress(decompressed.ToString());
                out_list.Add(listBytes.ToArray());
                Writer.Write(listBytes.Count);
                //string tmp_str;
                System.Diagnostics.Debug.WriteLine("{0}", k);

                /*if (k== file_num - 1)
                 * {
                 *  tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, listBytes.Count - 130554 / 2 * k));
                 *  tmp_list.Insert(0, 0);
                 *  tmp_str = Decompress(tmp_list);
                 * }
                 * else
                 * {
                 *  tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, 130554 / 2));
                 *  tmp_list.Insert(0, 0);
                 *  tmp_str = Decompress(tmp_list);
                 * }*/
                if (k == file_num - 1)
                {
                    Writer.Write(bytes.Length - k * 130554);
                }
                else
                {
                    Writer.Write(130554);
                }
                //Writer.Write(0xFFFD);
            }

            /*List<byte> output = new List<byte>();
             * string str = Decompress(listBytes);
             * foreach (var c in str)
             * {
             *  output.Add((byte)c);
             * }
             * // Writer.Write(output.ToArray());
             * System.Diagnostics.Debug.WriteLine(output.Count);*/

            for (int k = 0; k < out_list.Count; k++)
            {
                for (int kk = 0; kk < out_list[k].Length; kk++)
                {
                    Writer.Write((UInt16)out_list[k][kk]);
                }
            }
            Writer.Close();
        }
Beispiel #3
0
        public override void FileImport(string path, string outpath = null)
        {
            CZ0HeaderInfo cz0HeaderInfo = null;
            string        infopath      = path.Replace(".png", ".json");

            if (File.Exists(infopath))
            {
                cz0HeaderInfo = JsonConvert.DeserializeObject <CZ0HeaderInfo>(File.ReadAllText(infopath));
            }
            CZOutputInfo czOutput = new CZOutputInfo();

            Bitmap       Picture = new Bitmap(new MemoryStream(File.ReadAllBytes(path)));
            StructWriter Writer  = new StructWriter(File.Open(path + ".cz0", FileMode.Create));
            CZ0Header    header;

            if (cz0HeaderInfo == null)
            {
                header.Signature    = "CZ0";
                header.HeaderLength = 0x40;
                header.Width        = (ushort)Picture.Width;
                header.Heigth       = (ushort)Picture.Height;
                header.Colorbits    = 32;
            }
            else
            {
                header = cz0HeaderInfo.cz0Header;
            }
            Writer.WriteStruct(ref header);
            Writer.Seek(header.HeaderLength, SeekOrigin.Begin);

            if (header.Colorbits == 32)
            {
                System.Diagnostics.Debug.WriteLine(32);
                for (int y = 0; y < Picture.Height; y++)
                {
                    for (int x = 0; x < Picture.Width; x++)
                    {
                        Writer.Write(Picture.GetPixel(x, y).R);
                        Writer.Write(Picture.GetPixel(x, y).G);
                        Writer.Write(Picture.GetPixel(x, y).B);
                        Writer.Write(Picture.GetPixel(x, y).A);
                    }
                }
            }
            else if (header.Colorbits == 24)
            {
                System.Diagnostics.Debug.WriteLine(24);
                for (int y = 0; y < Picture.Height; y++)
                {
                    for (int x = 0; x < Picture.Width; x++)
                    {
                        Writer.Write(Picture.GetPixel(x, y).R);
                        Writer.Write(Picture.GetPixel(x, y).G);
                        Writer.Write(Picture.GetPixel(x, y).B);
                    }
                }
            }
            else if (header.Colorbits == 8)
            {
                System.Diagnostics.Debug.WriteLine(8);
                List <Pixel32_BGRA> list = new List <Pixel32_BGRA>();
                for (int y = 0; y < Picture.Height; y++)
                {
                    for (int x = 0; x < Picture.Width; x++)
                    {
                        var color  = Picture.GetPixel(x, y);
                        var color2 = new Pixel32_BGRA();
                        color2.R = color.R;
                        color2.G = color.G;
                        color2.B = color.B;
                        color2.A = color.A;
                        if (!list.Contains(color2))
                        {
                            list.Add(color2);
                        }
                    }
                }

                //颜色大于256
                if (list.Count > 256)
                {
                    Console.WriteLine("Over 256 Color!!");
                    //调用pngquant png 8bit缩减
                    if (File.Exists("pngquant.exe") || File.Exists("pngquant"))
                    {
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            var psi = new ProcessStartInfo("pngquant.exe")
                            {
                                RedirectStandardOutput = true
                            };
                            psi.Arguments = " 256 " + path + " --ext tmp ";
                            var proc = Process.Start(psi);
                            if (proc == null)
                            {
                                Console.WriteLine("Can not exec.");
                            }
                            else
                            {
                                using (var sr = proc.StandardOutput)
                                {
                                    while (!sr.EndOfStream)
                                    {
                                        Console.WriteLine(sr.ReadLine());
                                    }

                                    if (!proc.HasExited)
                                    {
                                        proc.Kill();
                                    }
                                }
                            }
                            string pathtmp = path.Replace(".png", "tmp");
                            Picture = new Bitmap(new MemoryStream(File.ReadAllBytes(pathtmp)));
                            File.Delete(pathtmp);
                        }
                        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                        {
                            //TODO
                        }
                        else
                        {
                            //TODO
                        }

                        list.Clear();
                        for (int y = 0; y < Picture.Height; y++)
                        {
                            for (int x = 0; x < Picture.Width; x++)
                            {
                                var color  = Picture.GetPixel(x, y);
                                var color2 = new Pixel32_BGRA();
                                color2.R = color.R;
                                color2.G = color.G;
                                color2.B = color.B;
                                color2.A = color.A;
                                if (!list.Contains(color2))
                                {
                                    list.Add(color2);
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Need pngquant !! Download From https://pngquant.org/");
                    }
                }

                var pixel = new Pixel32_BGRA();
                for (int i = 0; i < list.Count; i++)
                {
                    pixel = list[i];
                    Writer.WriteStruct(ref pixel);
                }
                for (int i = 0; i < 256 - list.Count; i++)
                {
                    var pixelempty = new Pixel32_BGRA()
                    {
                        R = 0, G = 0, B = 0, A = 255
                    };
                    Writer.WriteStruct(ref pixelempty);
                }

                for (int y = 0; y < Picture.Height; y++)
                {
                    for (int x = 0; x < Picture.Width; x++)
                    {
                        var color  = Picture.GetPixel(x, y);
                        var color2 = new Pixel32_BGRA();
                        color2.R = color.R;
                        color2.G = color.G;
                        color2.B = color.B;
                        color2.A = color.A;
                        uint index = (uint)list.IndexOf(color2);
                        Writer.Write((byte)index);
                    }
                }
            }
            Writer.Close();
        }
Beispiel #4
0
        //作者:Wetor
        //时间:2019.1.18
        public void PngToCZ1(string outfile)
        {
            Bitmap       Picture = new Bitmap(File.Open(outfile, FileMode.Open));
            StructWriter Writer  = new StructWriter(File.Open(outfile + ".cz1", FileMode.Create));
            CZ1Header    header;

            header.Signature    = "CZ1";
            header.HeaderLength = 0x10;
            header.Width        = (ushort)Picture.Width;
            header.Heigth       = (ushort)Picture.Height;
            header.Colorbits    = 4;
            Writer.WriteStruct(ref header);
            Writer.Seek(header.HeaderLength, SeekOrigin.Begin);


            Pixel32 Pixel = new Pixel32();

            Pixel.R = 255;
            Pixel.G = 255;
            Pixel.B = 255;
            //FF FF FF 00
            //FF FF FF 18
            //FF FF FF 26
            //FF FF FF 35
            //FF FF FF 45
            //FF FF FF 55
            //FF FF FF 65
            //FF FF FF 75
            //FF FF FF 85
            //FF FF FF 96
            //FF FF FF A7
            //FF FF FF B8
            //FF FF FF C9
            //FF FF FF DB
            //FF FF FF EC
            //FF FF FF FF


            Pixel.A = (byte)0x00; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x18; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x26; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x35; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x45; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x55; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x65; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x75; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x85; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x96; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xA7; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xB8; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xC9; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xDB; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xEC; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xFF; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Queue <byte> queue = new Queue <byte>();
            int          i     = 0;

            for (int y = 0; y < Picture.Height; y++)
            {
                for (int x = 0; x < Picture.Width; x++)
                {
                    byte tmp = color256to16(Picture.GetPixel(x, y).A);
                    //bytes11[i] = (byte)list.IndexOf(tmp);
                    queue.Enqueue((byte)list.IndexOf(tmp));
                    i++;
                }
            }

            byte[] bytes2 = new byte[Picture.Height * Picture.Width / 2];

            for (int j = 0; j < bytes2.Length; j++)
            {
                var low4bit  = queue.Dequeue();
                var high4bit = queue.Dequeue();
                var b        = (uint)(high4bit << 4) + (uint)low4bit;
                bytes2[j] = (byte)b;
            }
            //foreach (var b in bytes2)
            //{
            //    int low4bit = b & 0x0F;
            //    int high4bit = (b & 0xF0) >> 4;
            //    queue.Enqueue(low4bit);
            //    queue.Enqueue(high4bit);
            //}

            int file_num = bytes2.Length / 130554 + 1;
            //System.Diagnostics.Debug.WriteLine("{0} {1} {2}", file_num, listBytes.Count, 130554);
            List <int[]> out_list = new List <int[]>();

            Writer.Write(file_num);
            for (int k = 0; k < file_num; k++)
            {
                List <int>    listBytes    = new List <int>();
                StringBuilder decompressed = new StringBuilder();
                byte[]        tmp_bytes    = new byte[130554];
                if (k == file_num - 1)
                {
                    Array.Copy(bytes2, k * 130554, tmp_bytes, 0, bytes2.Length - k * 130554);
                }
                else
                {
                    Array.Copy(bytes2, k * 130554, tmp_bytes, 0, 130554);
                }


                foreach (char kk in tmp_bytes)
                {
                    decompressed.Append(kk);
                }
                listBytes = LzwUtil.Compress(decompressed.ToString());
                out_list.Add(listBytes.ToArray());
                Writer.Write(listBytes.Count);
                //string tmp_str;
                System.Diagnostics.Debug.WriteLine("{0}", k);

                /*if (k== file_num - 1)
                 * {
                 *  tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, listBytes.Count - 130554 / 2 * k));
                 *  tmp_list.Insert(0, 0);
                 *  tmp_str = Decompress(tmp_list);
                 * }
                 * else
                 * {
                 *  tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, 130554 / 2));
                 *  tmp_list.Insert(0, 0);
                 *  tmp_str = Decompress(tmp_list);
                 * }*/
                if (k == file_num - 1)
                {
                    Writer.Write(bytes2.Length - k * 130554);
                }
                else
                {
                    Writer.Write(130554);
                }
                //Writer.Write(0xFFFD);
            }

            /*List<byte> output = new List<byte>();
             * string str = Decompress(listBytes);
             * foreach (var c in str)
             * {
             *  output.Add((byte)c);
             * }
             * // Writer.Write(output.ToArray());
             * System.Diagnostics.Debug.WriteLine(output.Count);*/

            for (int k = 0; k < out_list.Count; k++)
            {
                for (int kk = 0; kk < out_list[k].Length; kk++)
                {
                    Writer.Write((UInt16)out_list[k][kk]);
                }
            }
            Writer.Close();
        }
Beispiel #5
0
        //作者:Wetor
        //时间:2019.1.18
        public void PngToCZ1(string outfile)
        {
            CZOutputInfo czOutput = new CZOutputInfo();
            string       name     = outfile.Replace(".png", "").Replace(".Png", "").Replace(".PNG", "") + ".json";

            if (File.Exists(name))
            {
                czOutput = JsonConvert.DeserializeObject <CZOutputInfo>(File.ReadAllText(name));
            }

            Bitmap       Picture = new Bitmap(File.Open(outfile, FileMode.Open));
            StructWriter Writer  = new StructWriter(File.Open(outfile + ".cz1", FileMode.Create));
            CZ1Header    header;

            header.Signature    = "CZ1";
            header.HeaderLength = 0x10;
            header.Width        = (ushort)Picture.Width;
            header.Heigth       = (ushort)Picture.Height;
            header.Colorbits    = 4;
            Writer.WriteStruct(ref header);
            Writer.Seek(header.HeaderLength, SeekOrigin.Begin);


            Pixel32_BGRA Pixel = new Pixel32_BGRA();

            Pixel.R = 255;
            Pixel.G = 255;
            Pixel.B = 255;
            //FF FF FF 00
            //FF FF FF 18
            //FF FF FF 26
            //FF FF FF 35
            //FF FF FF 45
            //FF FF FF 55
            //FF FF FF 65
            //FF FF FF 75
            //FF FF FF 85
            //FF FF FF 96
            //FF FF FF A7
            //FF FF FF B8
            //FF FF FF C9
            //FF FF FF DB
            //FF FF FF EC
            //FF FF FF FF


            Pixel.A = (byte)0x00; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x18; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x26; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x35; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x45; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x55; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x65; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x75; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x85; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x96; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xA7; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xB8; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xC9; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xDB; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xEC; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xFF; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Queue <byte> queue = new Queue <byte>();
            int          i     = 0;

            for (int y = 0; y < Picture.Height; y++)
            {
                for (int x = 0; x < Picture.Width; x++)
                {
                    byte tmp = color256to16(Picture.GetPixel(x, y).A);
                    //bytes11[i] = (byte)list.IndexOf(tmp);
                    queue.Enqueue((byte)list.IndexOf(tmp));
                    i++;
                }
            }

            byte[] bytes2 = new byte[Picture.Height * Picture.Width];

            for (int j = 0; j < bytes2.Length / 2; j++)
            {
                var low4bit  = queue.Dequeue();
                var high4bit = queue.Dequeue();
                var b        = (uint)(high4bit << 4) + (uint)low4bit;
                bytes2[j] = (byte)b;
            }



            var ie = bytes2.ToList().GetEnumerator();
            List <List <int> > out_list = LzwUtil.Compress(ie, 0xFEFD);

            foreach (var item in out_list)
            {
                Console.WriteLine("add compressed count :" + item.Count);
                Console.WriteLine(item.Count);
                Console.WriteLine(LzwUtil.Decompress(item).Length);
            }



            //for (int k = 0; k < out_list.Count; k++)
            //{
            //    Writer.Write(out_list[k].Count);
            //    Writer.Write(LzwUtil.Decompress(out_list[k]).Length);
            //    List<byte> bytes = new List<byte>();
            //    foreach (var item in out_list[k])
            //    {
            //        UInt16 bb = (UInt16)item;
            //        bytes.Add(BitConverter.GetBytes(bb)[0]);
            //        bytes.Add(BitConverter.GetBytes(bb)[1]);
            //    }
            //    //string tmp_str;
            //    System.Diagnostics.Debug.WriteLine("{0}", k);
            //}
            int file_num = out_list.Count;

            Writer.Write(out_list.Count);

            for (int k = 0; k < out_list.Count; k++)
            {
                //if (k >= out_list.Count - 1)
                //{
                //    Writer.Write((UInt32)0);
                //    Writer.Write((UInt32)0);
                //}
                //else
                //{
                Writer.Write(out_list[k].Count);
                Writer.Write(LzwUtil.Decompress(out_list[k]).Length);
                //List<byte> bytes = new List<byte>();
                //foreach (var item in out_list[k])
                //{
                //    UInt16 bb = (UInt16)item;
                //    bytes.Add(BitConverter.GetBytes(bb)[0]);
                //    bytes.Add(BitConverter.GetBytes(bb)[1]);
                //}
                //string tmp_str;
                System.Diagnostics.Debug.WriteLine("{0}", k);
                //}
            }

            //Writer.Write(czOutput.filecount);
            //for (int k = 0; k < czOutput.filecount; k++)
            //{
            //    if (k >= out_list.Count - 1)
            //    {
            //        Writer.Write((UInt32)0);
            //        Writer.Write((UInt32)0);
            //    }
            //    else
            //    {
            //        Writer.Write(out_list[k].Count);
            //        Writer.Write(LzwUtil.Decompress(out_list[k]).Length);
            //        //List<byte> bytes = new List<byte>();
            //        //foreach (var item in out_list[k])
            //        //{
            //        //    UInt16 bb = (UInt16)item;
            //        //    bytes.Add(BitConverter.GetBytes(bb)[0]);
            //        //    bytes.Add(BitConverter.GetBytes(bb)[1]);
            //        //}
            //        //string tmp_str;
            //        System.Diagnostics.Debug.WriteLine("{0}", k);
            //    }
            //}
            uint totalsize_new = 0x10 + 4 * 16 + 4 + (uint)file_num * 4 * 2;
            uint totalsize_org = 0x10 + 4 * 16 + 4 + czOutput.filecount * 4 * 2;

            for (int k = 0; k < out_list.Count; k++)
            {
                for (int kk = 0; kk < out_list[k].Count; kk++)
                {
                    Writer.Write((UInt16)out_list[k][kk]);
                    totalsize_new += 2;
                }
            }

            totalsize_org += czOutput.TotalCompressedSize * 2;
            int diff = (int)(totalsize_org - totalsize_new);

            if (diff > 0)
            {
                diff = diff / 2;
                for (uint j = 0; j < diff; j++)
                {
                    Writer.Write((UInt16)0);
                }
            }
            else
            {
                Console.WriteLine("超长");
            }

            //int p = 0;
            //foreach (var blockinfo in czOutput.blockinfo)
            //{
            //    if (out_list.Count - 1 >= p)
            //    {
            //        for (int kk = 0; kk < out_list[p].Count; kk++)
            //        {
            //            Writer.Write((UInt16)out_list[p][kk]);
            //        }
            //        if (out_list[p].Count == blockinfo.CompressedSize)
            //        {
            //            Console.WriteLine("符合");
            //        }
            //        else
            //        {
            //            Int64 compressedcount = (Int64)out_list[p].Count - (Int64)blockinfo.CompressedSize;
            //            compressedcount = Math.Abs(compressedcount);
            //            Console.WriteLine("不符合补0-" + compressedcount);
            //            for (Int64 pp = 0; pp < compressedcount; pp++)
            //            {
            //                Writer.Write((UInt16)0);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        Console.WriteLine("不符合补0-" + blockinfo.CompressedSize);
            //        //整块补0
            //        for (Int64 pp = 0; pp < blockinfo.CompressedSize; pp++)
            //        {
            //            Writer.Write((UInt16)0);
            //        }
            //    }
            //    p++;
            //}
            Writer.Close();
        }
Beispiel #6
0
        public void Encode(Bitmap Texture, Stream Output, bool CloseStreams = true)
        {
            StructWriter Writer = new StructWriter(Output, (bool)IsBigEnddian);

            Writer.Write(0x2065727574786554);

            if (SteamVersion.Value)
            {
                Writer.Write(0x20202020);

                Writer.Write(0); //0xC, section length
                Writer.WriteRawType(Const.UINT64, SteamUnk);
                Writer.Write(0); //0x18, pnglen
                Writer.WriteRawType(Const.UINT16, (ushort)TexSize.Width);
                Writer.WriteRawType(Const.UINT16, (ushort)TexSize.Height);
            }
            else
            {
                Writer.Write(0); //0x8, blocklen
                Writer.WriteRawType(Const.UINT32, Flags);
                Writer.Write(0); //0x10, pnglen
                Writer.WriteRawType(Const.UINT32, (uint)TexSize.Width);
                Writer.WriteRawType(Const.UINT32, (uint)TexSize.Height);
            }

            if (Tiled)
            {
                Texture = Retile(Texture);
            }
            uint SectionLength;

            if (SteamVersion.Value)
            {
                byte[] Buffer = new byte[Texture.Width * Texture.Height * 4];
                fixed(void *pBuff = &Buffer[0])
                {
                    int   Stride = Texture.Width;
                    uint *pPixel = (uint *)pBuff;

                    unchecked
                    {
                        for (int i = 0; i < Buffer.Length / 4; i++)
                        {
                            int X = i % Stride;
                            int Y = i / Stride;

                            uint ARGB = (uint)Texture.GetPixel(X, Y).ToArgb();

                            //ARGB => ABGR
                            *pPixel++ = (ARGB & 0xFF00FF00) | ((ARGB & 0x00FF0000) >> 8 * 2) | ((ARGB & 0x000000FF) << 8 * 2);
                        }
                    }
                }

                if (Compressed)
                {
                    Buffer = LZSSCompress(Buffer);
                }

                Writer.Write(Buffer);

                SectionLength = (uint)Writer.BaseStream.Position;
                uint TexLen = SectionLength - 0x20;
                Writer.Seek(0xC, 0);
                Writer.WriteRawType(Const.UINT32, SectionLength);
                Writer.Seek(0x18, 0);
                Writer.WriteRawType(Const.UINT32, TexLen);
            }
            else
            {
                Texture.Save(Writer.BaseStream, ImageFormat.Png);
                SectionLength = (uint)Writer.BaseStream.Position;
                uint PngLen = SectionLength - 0x1C;
                Writer.Seek(0x8, 0);
                Writer.WriteRawType(Const.UINT32, SectionLength);
                Writer.Seek(0x10, 0);
                Writer.WriteRawType(Const.UINT32, PngLen);
            }

            Writer.Seek(SectionLength, 0);
            while (Writer.BaseStream.Position % (SteamVersion.Value ? 8 : 4) != 0)
            {
                Writer.Write((byte)0x0);
            }

            StructReader Reader = new StructReader(this.Texture, (bool)IsBigEnddian);

            Reader.Seek(SteamVersion.Value ? 0x0C : 0x08, 0);
            Reader.Seek(Reader.ReadRawType(Const.UINT32), 0);

            while (Reader.BaseStream.Position % (SteamVersion.Value ? 8 : 4) != 0)
            {
                Reader.ReadByte();
            }

            Reader.BaseStream.CopyTo(Writer.BaseStream);
            Writer.Flush();

            if (CloseStreams)
            {
                Writer.Close();
                Output?.Close();
            }
        }
Beispiel #7
0
        public byte[] Export(string Content)
        {
            dynamic NewVLC = new VLContent()
            {
                Dummy   = this.Content.Dummy,
                Content = Content
            };

            if (NoUnk14 && (Encrypt || Compress))
            {
                //The English Script of koichoco have a part of the header cutted off, so...
                //if you ignore this he corrupt when you encrypt or compress the script
                throw new Exception("This Script is Corrupted, You can't Export it with Compression or Encryption.");
            }

            SL2Header Header = new SL2Header();

            Tools.CopyStruct(this.Header, ref Header);
            Header.IsCompressed = Header.IsEncrypted = false;
            Header.DecLen       = 0;


            byte[] Output = Tools.BuildStruct(ref NewVLC, true, Encoding);

            if (Compress)
            {
                Output = CompressData(Output);
                Header.IsCompressed = true;
                Header.DecLen       = (uint)Encoding.GetByteCount(Content) + 0x18;//???
            }

            if (Encrypt)
            {
                Output             = EncryptData(Output);
                Header.IsEncrypted = true;
            }


            Header.Length = (uint)(Output.LongLength + (NoUnk14 ? 0x4C : 0x50));

            if (UpdateModifyDate)
            {
                Header.Day   = (byte)DateTime.Now.Day;
                Header.Month = (byte)DateTime.Now.Month;
                Header.Year  = (ushort)DateTime.Now.Year;
            }

            MemoryStream Data   = new MemoryStream();
            StructWriter Result = new StructWriter(Data, true, Encoding);

            Result.WriteStruct(ref Header);

            if (NoUnk14)
            {
                Result.Seek(-4, SeekOrigin.Current);
            }

            Result.Write(Output, 0, Output.Length);
            Output = Data.ToArray();
            Result.Close();
            Data.Close();

            return(Output);
        }