Example #1
0
        public static MSGFile ReadMSGFile(string path)
        {
            if (!File.Exists(path))
            {
                Console.WriteLine("The file {0} not exists.", path);
                return(null);
            }

            var msgFile = new MSGFile();

            BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open));

            /************ READ TCTC ************/
            msgFile.HasTCRC = msgFile.ReadTCRC(reader);

            /************ READ TEXT ************/
            if (msgFile.HasTCRC)
            {
                msgFile.TEXT           = reader.ReadInt32();
                msgFile.TEXT_BlockSize = reader.ReadInt32();
                msgFile.CRCs.Add(16 + msgFile.TCRC_BlockSize + msgFile.TEXT_BlockSize,
                                 new CRC()
                {
                    Checksum = (uint)msgFile.TEXT,
                    Pointer  = msgFile.TEXT_BlockSize
                });

                msgFile.Texts = new Dictionary <int, char[]>();
                int lastOffset = -1;
                foreach (var pointer in msgFile.CRCs.Keys.OrderBy(o => o))
                {
                    if (lastOffset == -1)
                    {
                        lastOffset = pointer;
                        continue;
                    }
                    reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin);
                    // 15-03-16
                    //char[] text = reader.ReadChars(pointer - lastOffset);
                    List <char> characters = new List <char>();
                    int         n          = 0;
                    while (n < 2)
                    {
                        char c = reader.ReadChar();
                        if (c == '\0')
                        {
                            characters.Add(c);
                            n++;
                            continue;
                        }
                        n = 0;
                        characters.Add(c);
                    }
                    char[] text = characters.ToArray();
                    // FIN 15-03-16
                    lastOffset = pointer;
                    msgFile.Texts.Add(lastOffset, text);
                }
                msgFile.HasTEXT = true;
            }

            /************ READ NPCT ************/
            msgFile.HasNPCT = msgFile.ReadNPCT(reader);

            /************ READ NAME ************/
            msgFile.HasNAME = msgFile.ReadNAME(reader);

            reader.Close();

            msgFile.Path = path;
            return(msgFile);
        }
Example #2
0
        public static MSGFile ReadRepackFiles(string dataPath, string textPath)
        {
            Console.WriteLine($"Repack {textPath}> Checking .text and .data files...");
            if (!File.Exists(dataPath))
            {
                Console.WriteLine("The file {0} not exists.", dataPath);
                return(null);
            }
            if (!File.Exists(textPath))
            {
                Console.WriteLine("The file {0} not exists.", textPath);
                return(null);
            }

            MSGFile msgFile = new MSGFile();

            /************ READ TCRC ************/
            Console.WriteLine($"Repack {textPath}> Reading TCRC block...");
            BinaryReader reader = new BinaryReader(File.Open(dataPath, FileMode.Open));

            msgFile.HasTCRC = msgFile.ReadTCRC(reader);

            /************ READ TEXT ************/
            Console.WriteLine($"Repack {textPath}> Reading TEXT block...");
            if (msgFile.HasTCRC)
            {
                msgFile.TEXT = TEXTVALUE;
                // JRH 2018-08-17
                //BinaryReader textReader = new BinaryReader(File.Open(textPath, FileMode.Open), Encoding.Default);
                BinaryReader textReader = new BinaryReader(File.Open(textPath, FileMode.Open), EncodingExtension.GetUTF8WithoutBOM());
                // FIN JRH
                char[] chars      = textReader.ReadChars((int)textReader.BaseStream.Length);
                string text       = new string(chars);
                var    texts      = text.Split(new string[] { "\n<end>\n\n<end>\n\n" }, StringSplitOptions.None);
                var    charArrays = texts.Select(s => s.Replace("\n<end>\n", "\0").ToCharArray().Concat(new char[] { '\0', '\0' }).ToArray())
                                    .ToArray();

                msgFile.Texts = new Dictionary <int, char[]>();
                CRC[] oldPointers = msgFile.CRCs.OrderBy(o => o.Key).Select(s => s.Value).ToArray();
                msgFile.CRCs.Clear();
                // JRH 2018-08-17
                int newPointers = charArrays.Length - 1;
                if (oldPointers.Length != newPointers)
                {
                    Console.WriteLine($"\tPointer's quanity has changed from {oldPointers.Length} to {newPointers}.");
                }
                msgFile.TCRC_BlockSize = newPointers * 8;
                // FIN JRH
                int n = 0;
                for (int i = 0; i < newPointers; i++)
                {
                    var txt = charArrays[i];
                    int key = n + 16 + msgFile.TCRC_BlockSize;
                    msgFile.Texts.Add(key, txt);
                    msgFile.CRCs.Add(key, new CRC()
                    {
                        Checksum = oldPointers[i].Checksum,
                        Pointer  = n
                    });
                    // JRH 2018-08-17
                    //n += txt.Length;
                    n += txt.Length + txt.Count(c => c == 'ñ' || c == 'á' || c == 'é' || c == 'í' || c == 'ó' || c == 'ú' || c == 'Ñ' || c == 'Á' || c == 'É' || c == 'Í' || c == 'Ó' || c == 'Ú' || c == '¿' || c == '¡');
                    // FIN JRH
                }
                msgFile.TEXT_BlockSize = n;
                textReader.Close();
                msgFile.HasTEXT = true;
            }

            /************ READ NPCT ************/
            Console.WriteLine($"Repack {textPath}> Reading NPCT block...");
            msgFile.HasNPCT = msgFile.ReadNPCT(reader);

            /************ READ NAME ************/
            Console.WriteLine($"Repack {textPath}> Reading NAME block...");
            msgFile.HasNAME = msgFile.ReadNAME(reader);
            reader.Close();

            msgFile.Path = textPath.Replace(".text", ".msg");
            return(msgFile);
        }