Ejemplo n.º 1
0
        static void zeroTo(StreamEx s, int offset)
        {
            int len = offset - (int)s.Position;

            byte[] b = new byte[len];
            s.Write(b);
        }
Ejemplo n.º 2
0
        static void zeroTo(StreamEx s, Int64 offset)
        {
            Int64 len = offset - s.Position;

            byte[] b = new byte[len];
            s.Write(b);
        }
Ejemplo n.º 3
0
        public static void Import(string input)
        {
            StreamEx s = new StreamEx(input, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            string[] txtVal = Agemo.ReadFile(input + ".txt", _agemoEncoding);
            StreamEx si     = new StreamEx(input + ".imp", System.IO.FileMode.Create, System.IO.FileAccess.Write);

            // section offset
            s.Position = 0x10;
            Int32 sec1Offset = s.ReadInt32BigEndian();
            Int32 sec2Offset = s.ReadInt32BigEndian();
            Int32 sec3Offset = s.ReadInt32BigEndian();

            // before text data
            s.Position = 0;
            byte[] beforeText = s.Read(sec3Offset);
            si.Write(beforeText);
            si.Flush();

            // text
            UInt16[] txtOffset = new UInt16[txtVal.Length];
            for (int i = 0; i < txtVal.Length; i++)
            {
                txtOffset[i] = (UInt16)si.Position;
                txtVal[i]    = txtVal[i].Substring(0, txtVal[i].Length - 5);

                for (int k = 0; k < _convertChar.Count; k++)
                {
                    txtVal[i] = txtVal[i].Replace(_convertChar[k].Value, _convertChar[k].Key);
                }

                for (int j = 0; j < txtVal[i].Length; j++)
                {
                    si.WriteUInt16BigEndian((UInt16)txtVal[i][j]);
                }
                si.WriteUInt16(0);
            }

            // text offset
            si.Position = sec1Offset;
            for (int i = 0; i < txtOffset.Length; i++)
            {
                si.Position += 6;
                si.WriteUInt16BigEndian(txtOffset[i]);
            }
            si.Close();
        }
Ejemplo n.º 4
0
        public static void unpack(string input)
        {
            StreamEx s = new StreamEx(input, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            string unpackdir = Path.GetFullPath(input) + "_unpack";

            if (!Directory.Exists(unpackdir))
            {
                Directory.CreateDirectory(unpackdir);
            }

            List <headerNode> headers = new List <headerNode>();

            while (s.Position < 0xc000)
            {
                headerNode h = new headerNode();
                h.fileName   = s.ReadSimpleString(0x20);
                h.fileOffset = s.ReadInt32() + 0xc000;
                h.fileLength = s.ReadInt32();
                s.Position  += 8;

                if (h.fileLength == 0)
                {
                    break;
                }

                headers.Add(h);
            }

            Console.WriteLine("文件头解析:共有{0}个文件", headers.Count);

            for (int i = 0; i < headers.Count; i++)
            {
                StreamEx sNew = new StreamEx(unpackdir + "\\" + headers[i].fileName, FileMode.Create, FileAccess.Write);

                Console.WriteLine("正在导出文件{0}/{1}:{2} ({3}<={4})", i + 1, headers.Count, headers[i].fileName, headers[i].fileOffset, headers[i].fileLength);
                s.Position = headers[i].fileOffset;
                byte[] d = s.Read(headers[i].fileLength);
                sNew.Write(d);
            }
        }
Ejemplo n.º 5
0
        static public int importFile(string path)
        {
            string[] texts = Agemo.ReadFile(path, _destEncoding);
            StreamEx s     = new StreamEx(path + ".scs", System.IO.FileMode.Create, System.IO.FileAccess.Write);

            s.WriteInt32BigEndian(texts.Length);
            s.Position += texts.Length * 4;
            Int32[] textOffset = new Int32[texts.Length];

            for (int i = 0; i < texts.Length; i++)
            {
                texts[i]      = texts[i].Remove(texts[i].Length - 5);
                textOffset[i] = (Int32)s.Position;
                // 处理字符集差异
                foreach (KeyValuePair <string, string> kvp in _convertChar)
                {
                    texts[i] = texts[i].Replace(kvp.Value, kvp.Key);
                }
                char[] chrs = texts[i].ToCharArray();

                for (int j = 0; j < chrs.Length; j++)
                {
                    if (_dicConvert.ContainsKey(chrs[j]))
                    {
                        chrs[j] = _dicConvert[chrs[j]];
                    }
                }
                s.Write(_sourceEncoding.GetBytes(chrs));
                s.WriteByte(0);
                //s.WriteString(texts[i], _sourceEncoding.GetByteCount(texts[i]) + 1, _sourceEncoding);
            }

            s.Position = 4;
            for (int i = 0; i < textOffset.Length; i++)
            {
                s.WriteInt32BigEndian(textOffset[i]);
            }

            return(texts.Length);
        }
Ejemplo n.º 6
0
        public static void Unpack(string input)
        {
            StreamEx s = new StreamEx(input, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            // 52141C7E
            Int32 magicNumber = s.ReadInt32();

            Int32 fileCount = s.ReadInt32();

            string[] fileName      = new string[fileCount];
            Int32[]  strangeNumber = new Int32[fileCount];
            Int32[]  fileLength    = new Int32[fileCount];
            Int32[]  fileOffset    = new Int32[fileCount];
            for (int i = 0; i < fileCount; i++)
            {
                fileName[i]      = s.ReadSimpleString(0x40);
                strangeNumber[i] = s.ReadInt32();
                fileLength[i]    = s.ReadInt32();
                fileOffset[i]    = s.ReadInt32();
                Int32 one = s.ReadInt32();
            }

            for (int i = 0; i < fileCount; i++)
            {
                s.Position = fileOffset[i];
                Int32  strangeNumberAgain = s.ReadInt32();
                Int32  fileLengthAgain    = s.ReadInt32();
                Int32  oneAgain           = s.ReadInt32();
                byte[] fileContent        = s.Read(fileLength[i] - 12);

                // unpack
                StreamEx unpackFile = new StreamEx(fileName[i], System.IO.FileMode.Create, System.IO.FileAccess.Write);
                unpackFile.Write(fileContent);
                unpackFile.Close();
            }
        }
Ejemplo n.º 7
0
        static public int importFile(string path)
        {
            string[] texts = Agemo.ReadFile(path + ".txt", _destEncoding);

            for (int i = 0; i < texts.Length; i++)
            {
                texts[i] = texts[i].Remove(texts[i].Length - 5);
                // 处理字符集差异
                foreach (KeyValuePair <string, string> kvp in _convertChar)
                {
                    texts[i] = texts[i].Replace(kvp.Value, kvp.Key);
                }
            }

            StreamEx ssource = new StreamEx(path, FileMode.Open, FileAccess.Read);
            StreamEx sdest   = new StreamEx(path + ".imp", System.IO.FileMode.Create, System.IO.FileAccess.Write);

            Int32 header = ssource.ReadInt32BigEndian();

            if (header == _fixHeaderType1)
            {
                Console.Write("[SETU]");
                sdest.WriteInt32BigEndian(_fixHeaderType1);
                sdest.WriteInt32(texts.Length);

                Int32[] textOffset = new Int32[texts.Length];

                for (int i = 0; i < texts.Length; i++)
                {
                    sdest.WriteInt32(0); // 长度占位写0
                }

                for (int i = 0; i < texts.Length; i++)
                {
                    if (texts[i].Length == 0)
                    {
                        textOffset[i] = 0;
                        continue;
                    }
                    textOffset[i] = (Int32)sdest.Position; // 文本偏移量
                    sdest.Write(_sourceEncoding.GetBytes(texts[i]));
                    sdest.WriteInt16(0);                   // 结束0
                }

                ssource.Position = ssource.Length - 139;
                sdest.WriteFromStream(ssource, 139);

                sdest.Position = 8;
                for (int i = 0; i < textOffset.Length; i++)
                {
                    sdest.WriteInt32(textOffset[i]);
                }
            }
            else if ((header >> 16) == _fixHeaderType2)
            {
                Console.Write("[0000]");
                sdest.WriteInt16(0);

                for (int i = 0; i < texts.Length; i++)
                {
                    sdest.Write(_sourceEncoding.GetBytes(texts[i]));
                    sdest.WriteInt16(0);
                }
            }
            else if (header == _fixHeaderType3)
            {
                Console.Write("[TCRC]");
                Int32 textBlockOffset = ssource.ReadInt32() + 8; // 指向"TEXT"

                ssource.Position = 0;
                sdest.WriteFromStream(ssource, textBlockOffset); // 写至索引结束
                sdest.WriteFromStream(ssource, 4);               // 写入"TEXT"
                Int32 sourceTextLength = ssource.ReadInt32();    // 原文本段长度
                sdest.WriteInt32(0);

                List <Int32> textIndexes = new List <Int32>();
                if (texts[0] != "{TEXT}")
                {
                    throw new Exception("文本分段错误");
                }

                int iText = 1;
                for (; iText < texts.Length && texts[iText] != "{NCPT}" && texts[iText] != "{NAME}"; iText++)
                {
                    textIndexes.Add((Int32)sdest.Position - textBlockOffset - 8);   // 获取偏移
                    sdest.Write(_sourceEncoding.GetBytes(texts[iText]));            // 写入文本
                    sdest.WriteInt32(0);                                            // 写入0结尾
                }
                Int32 destTextLength = (Int32)sdest.Position - textBlockOffset - 8; // 文本段长度

                sdest.Position = 8;
                for (int i = 0; i < textIndexes.Count; i++)
                {
                    sdest.Position += 4;
                    sdest.WriteInt32(textIndexes[i]);
                }
                if (sdest.Position != textBlockOffset)
                {
                    throw new Exception("文本段写入失败");
                }
                sdest.Position += 4;
                sdest.WriteInt32(destTextLength);

                if (iText < texts.Length)
                {
                    ssource.Position = textBlockOffset + 8 + sourceTextLength;
                    sdest.Position   = textBlockOffset + 8 + destTextLength;
                    if (texts[iText] == "{NCPT}")
                    {
                        ssource.Position += 4;
                        Int32 ncptLength = ssource.ReadInt32(); // get ncpt block length
                        ssource.Position -= 8;

                        sdest.WriteFromStream(ssource, ncptLength + 8);
                        iText++;
                    }
                    if (texts[iText] == "{NAME}")
                    {
                        sdest.WriteInt32BigEndian(0x4E414D45);
                        Int32 nameBlockLengthPosition = (Int32)sdest.Position;
                        sdest.WriteInt32(0); // 长度占位

                        sdest.WriteInt16(0);
                        for (iText = iText + 1; iText < texts.Length; iText++)
                        {
                            sdest.Write(_sourceEncoding.GetBytes(texts[iText]));
                            sdest.WriteInt16(0);
                        }
                        Int32 nameBlockLength = (Int32)sdest.Position - nameBlockLengthPosition - 4;
                        sdest.Position = nameBlockLengthPosition;
                        sdest.WriteInt32(nameBlockLength);
                    }
                }
            }
            else
            {
                throw new Exception("不支持的文件头");
            }
            sdest.Close();

            return(texts.Length);
        }
Ejemplo n.º 8
0
        private void Write(ZeroLengthStreamPasser sp, KeyValuePair <string, string>[] text)
        {
            StreamEx s = sp.GetStream();

            s.Position = 0;
            Int32 header = 0x8;
            Int32 count  = text.Length;

            s.WriteInt32BigEndian(header);
            s.WriteInt32BigEndian(count);

            Encoding encoding = Encoding.GetEncoding("utf-8");

            Int32[] keyaddresses = new Int32[count];
            Int32[] blockaddress = new Int32[count];

            s.Position = 0x8 + count * 8;


            for (int i = 0; i < count; i++)
            {
                if (text[i].Key.Length == 0)
                {
                    keyaddresses[i] = 0;
                }
                else
                {
                    keyaddresses[i] = (Int32)s.Position;
                    s.WriteString(text[i].Key, text[i].Key.Length + 1, encoding);
                }

                if (text[i].Value.Length == 0)
                {
                    blockaddress[i] = 0;
                }
                else
                {
                    blockaddress[i] = (Int32)s.Position;

                    string txt = text[i].Value;
                    for (int k = 0; k < _convertChar.Count; k++)
                    {
                        txt = txt.Replace(_convertChar[k].Value, _convertChar[k].Key);
                    }
                    char[] chrs = txt.ToCharArray();

                    for (int j = 0; j < chrs.Length; j++)
                    {
                        if (_dicConvert.ContainsKey(chrs[j]))
                        {
                            chrs[j] = _dicConvert[chrs[j]];
                        }
                    }


                    s.Write(encoding.GetBytes(chrs));
                    s.WriteByte(0);
                    //s.WriteString(txt.ToString(), encoding.GetByteCount(txt) + 1, encoding);
                }
            }

            s.Position = 0x8;
            for (int i = 0; i < count; i++)
            {
                s.WriteInt32BigEndian((keyaddresses[i] == 0) ? 0 : (keyaddresses[i] - (Int32)s.Position));
                s.WriteInt32BigEndian((blockaddress[i] == 0) ? 0 : (blockaddress[i] - (Int32)s.Position));
            }
        }
Ejemplo n.º 9
0
        public static bool Generate(
            List <char> chars, bool gdiRender, TextRenderingHint textRenderingHint,
            int totWidth, int totHeight,
            int charWidth, int charHeight,
            Font font, bool drawGrid, out Bitmap bmp, string savepath, string refFileName)
        {
            List <CharUnit> charUnits = new List <CharUnit>();

            for (int i = 0; i < chars.Count; i++)
            {
                CharUnit cu = new CharUnit();
                cu.charVal = chars[i].ToString();
//                 cu.x = i % (totWidth / charWidth) * charWidth;
//                 cu.y = i / (totWidth / charWidth) * charHeight;
//                 cu.actrualWidth = 0;

                charUnits.Add(cu);
            }

            int currentChannel = 0;

            bmp = new Bitmap(totWidth, totHeight);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                //g.PageUnit=GraphicsUnit.Pixel;
                g.TextRenderingHint = textRenderingHint;
                //g.SmoothingMode=SmoothingMode.HighQuality;
                StringFormat stringFormat = StringFormat.GenericTypographic;
                stringFormat.FormatFlags &= ~StringFormatFlags.MeasureTrailingSpaces;
                //stringFormat.LineAlignment = StringAlignment.Near;
                //stringFormat.Alignment = StringAlignment.Near;
                //stringFormat.FormatFlags = StringFormatFlags.

                g.FillRectangle(Brushes.Black, 0, 0, totWidth, totHeight);

                if (drawGrid)
                {
//                     for (int i = 0; i < totWidth; i += charWidth)
//                     {
//                         g.DrawLine(Pens.Red, i, 0, i, totHeight);
//                     }
                    for (int i = 0; i < totHeight; i += charHeight + 1)
                    {
                        g.DrawLine(Pens.Red, 0, i, totWidth, i);
                    }
                }

                Point currentPos = new Point(1, 1);
                foreach (CharUnit cu in charUnits)
                {
                    //string c = new string((char)cu.charVal, 1);
                    Rectangle cellArea = new Rectangle(
                        currentPos.X, currentPos.Y,
                        charHeight, charHeight);

                    Size charSz;
                    if (gdiRender)
                    {
                        // GDI渲染
                        charSz = TextRenderer.MeasureText(g, cu.charVal, font, cellArea.Size, TextFormatFlags.NoPadding);
                    }
                    else
                    {
                        // GDI+渲染
                        charSz = Size.Round(g.MeasureString(cu.charVal, font, charHeight * 2, stringFormat));
                    }

                    cu.actrualWidth = charSz.Width;
                    cu.visualWidth  = cu.actrualWidth;
                    cu.adjustX      = 0;

                    if (currentPos.X + cu.actrualWidth + 2 >= totWidth)
                    {
                        currentPos.X  = 1;
                        currentPos.Y += charHeight + 1;
                    }
                    if (currentPos.Y + charHeight + 1 >= totHeight)
                    {
                        // save
                        if (savepath != null)
                        {
                            bmp.Save(savepath + "\\" + refFileName + GetChannelText(currentChannel) + ".png",
                                     ImageFormat.Png);
                        }
                        g.FillRectangle(Brushes.Black, 0, 0, totWidth, totHeight);
                        currentChannel++;
                        currentPos.X = 1;
                        currentPos.Y = 1;
                    }
                    cu.x       = currentPos.X;
                    cu.y       = currentPos.Y;
                    cu.channel = currentChannel;

                    if (gdiRender)
                    {
                        // GDI渲染
                        TextRenderer.DrawText(g, cu.charVal, font, currentPos, Color.White, TextFormatFlags.NoPadding);
                    }
                    else
                    {
                        // GDI+渲染
                        g.DrawString(cu.charVal, font, Brushes.White, currentPos.X, currentPos.Y, stringFormat);
                    }

                    // 绘制边框
                    if (drawGrid)
                    {
                        g.DrawRectangle(Pens.White, currentPos.X, currentPos.Y, cu.actrualWidth, charHeight - 1);
                        Application.DoEvents();
                    }

                    currentPos.X += cu.actrualWidth + 2;
                }
            }

            if (savepath != null)
            {
                bmp.Save(savepath + "\\" + refFileName + GetChannelText(currentChannel) + ".png",
                         ImageFormat.Png);
                StreamEx s = new StreamEx(savepath + "\\" + refFileName + ".new", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                //
                s.Position = 8;
                s.Write(new byte[] {
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x8E, 0xB0, 0x16, 0x8C, 0x05, 0x55, 0x01, 0xFF
                });
                s.WriteInt32BigEndian(charHeight);
                s.WriteInt32BigEndian(chars.Count);
                s.WriteInt32BigEndian(totWidth);
                s.WriteInt32BigEndian(totHeight);
                s.WriteInt32BigEndian(0);

                //
                byte[] indexFixData = new byte[8] {
                    0xCF, 0xA7, 0xE0, 0xA1, 0x08, 0x55, 0x55, 0xFF
                };
                foreach (CharUnit cu in charUnits)
                {
                    s.Write(indexFixData);
                    byte[] sjisCod = TextEncoding.ShiftJIS.GetBytes(cu.charVal);
                    s.WriteInt32BigEndian(getCodeInt(sjisCod));
                    byte[] utf8Cod = TextEncoding.UTF8.GetBytes(cu.charVal);
                    s.WriteInt32BigEndian(getCodeInt(utf8Cod));
                    s.WriteInt32BigEndian(cu.x);
                    s.WriteInt32BigEndian(cu.y);
                    s.WriteInt32BigEndian(cu.actrualWidth);
                    s.WriteInt32BigEndian(cu.adjustX);
                    s.WriteInt32BigEndian(cu.visualWidth);
                    s.WriteInt32BigEndian(cu.channel);
                }

                //
                s.Write(new byte[] {
                    0xCC, 0x1C, 0x19, 0x19, 0x01, 0x01, 0xFF, 0xFF,
                    0x00, 0x00, 0x00, 0x02, 0xFD, 0x16, 0xE3, 0xB5,
                    0x03, 0x15, 0xFF, 0xFF, 0x00, 0x00, 0xC5, 0xB8,
                    0x00, 0x00, 0xC3, 0xBB, 0xFF, 0xFF, 0xFF, 0xFE,
                    0xFD, 0x16, 0xE3, 0xB5, 0x03, 0x15, 0xFF, 0xFF,
                    0x00, 0x00, 0xC5, 0xB8, 0x00, 0x00, 0xC3, 0xBC,
                    0xFF, 0xFF, 0xFF, 0xFE, 0x69, 0xA4, 0x8E, 0xC4,
                    0x00
                });
                while (s.Position % 0x10 != 0)
                {
                    s.WriteByte(0xff);
                }

                //
                int endBlockOffset = (int)s.Position;
                s.Write(new byte[] {
                    0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x05,
                    0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x19,
                    0x8E, 0xB0, 0x16, 0x8C, 0x00, 0x00, 0x00, 0x00,
                    0xCF, 0xA7, 0xE0, 0xA1, 0x00, 0x00, 0x00, 0x04,
                    0xCC, 0x1C, 0x19, 0x19, 0x00, 0x00, 0x00, 0x08,
                    0xFD, 0x16, 0xE3, 0xB5, 0x00, 0x00, 0x00, 0x10,
                    0x69, 0xA4, 0x8E, 0xC4, 0x00, 0x00, 0x00, 0x15,
                    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                    0x49, 0x4E, 0x46, 0x00, 0x43, 0x48, 0x52, 0x00,
                    0x4B, 0x45, 0x52, 0x4E, 0x49, 0x4E, 0x46, 0x00,
                    0x4B, 0x45, 0x52, 0x4E, 0x00, 0x45, 0x4E, 0x44,
                    0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
                    0x01, 0x74, 0x32, 0x62, 0xFE, 0x01, 0x00, 0x01,
                    0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
                });
                //
                s.Position = 0;
                s.WriteInt32BigEndian((int)s.Length / 0x28);
                s.WriteInt32BigEndian(endBlockOffset);
                s.Close();
            }

            return(true);
        }