Esempio n. 1
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();
        }
Esempio n. 2
0
        static public int importFile(string path)
        {
            string[] texts   = Agemo.ReadFile(path + ".txt", _destEncoding);
            StreamEx ssource = new StreamEx(path, FileMode.Open, FileAccess.Read);

            Int32 header = ssource.ReadInt32BigEndian();

            if (header != 0x00444D47)
            {
                throw new Exception("不支持的文件头");
            }

            ssource.Position = 0x14;
            Int32 tagCount   = ssource.ReadInt32BigEndian();
            Int32 textCount  = ssource.ReadInt32BigEndian();
            Int32 textOffset = ssource.ReadInt32BigEndian() + tagCount * 8 + 0x30;

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

            ssource.Position = 0;
            sdest.WriteFromStream(ssource, textOffset);

            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);
                }
                sdest.WriteString(texts[i], _sourceEncoding.GetByteCount(texts[i]) + 1, _sourceEncoding);
            }

            Int32 textLength = (int)(sdest.Position - textOffset);

            sdest.Position = 0x20;
            sdest.WriteInt32BigEndian(textLength);

            sdest.Close();

            return(texts.Length);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            if (args.Count() == 0)
            {
                return;
            }

            BSDJPN   bsdjpn   = new BSDJPN();
            Encoding encoding = Encoding.GetEncoding("gbk");
            string   f        = args[0];

            if (f.ToLower().EndsWith(".sdbjpn"))
            {
                KeyValuePair <string, string>[] pairs = bsdjpn.Read(f);
                string ff = f.Substring(0, f.Length - 6);
                Agemo.WriteFile(ff + "idx", encoding, from p in pairs select p.Key);
                Agemo.WriteFile(ff + "txt", encoding, from p in pairs select p.Value + "{END}");
            }

            else if (f.ToLower().EndsWith(".txt"))
            {
                string   ff     = f.Substring(0, f.Length - 3);
                string[] keys   = Agemo.ReadFile(ff + "idx", encoding);
                string[] blocks = Agemo.ReadFile(f, encoding);

                KeyValuePair <string, string>[] pairs = keys.Select(
                    (key, idx) => new KeyValuePair <string, string>
                        (key, blocks[idx].Length >= 5 ? blocks[idx].Substring(0, blocks[idx].Length - 5) : blocks[idx]))
                                                        .ToArray();

                if (args.Count() >= 2)
                {
                    bsdjpn.AddConvertChar(args[1]);
                }

                bsdjpn.Write(ff + "SDBJPN", pairs);
            }
        }
Esempio n. 5
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);
        }