예제 #1
0
파일: Program.cs 프로젝트: oldmud0/AiroRom
        private static bool SearchAndModify(GameFile file)
        {
            // It is better not to try to modify it.
            if (file.Name == "ARM7.bin")
            {
                return(false);
            }

            Console.WriteLine("Searching in {0}", file.Name);
            bool found = false;

            string[] noParams = new string[0];

            // 1.A Check if it's encoded
            bool isEncoded;

            // Take ARM as compressed always, if the file finally
            // it's not compressed the tool will do nothing
            bool isArm9 = file is ArmFile;

            isEncoded = isArm9;

            // If it's an overlay is easy to know when it's compressed
            OverlayFile overlay = file as OverlayFile;

            if (overlay != null)
            {
                isEncoded = overlay.IsEncoded;
            }

            // 1.B Decode if so
            if (isEncoded)
            {
                // Get the original size
                long encodedSize = file.Length;

                SetEncodingFormat(file, true);
                file.Format.Read();             // Get data from file
                file.Format.Import(noParams);   // Call to the decoding program
                file.Format.Write();            // Overwrite data file
                file.Format.Dispose();

                // Check if the file was decoded
                // else, it is not encoded and must not be
                // encoded again
                // This can happen only with ARM9 file
                isEncoded = encodedSize != file.Length;
                if (!isEncoded)
                {
                    Console.WriteLine("\tFile {0} is not encoded.", file.Name);
                }
            }
            else
            {
                // Get a stream to work with and set to the file
                // This allow read & write operations
                // In the case of decoding this is already done
                DataStream stream = new DataStream(new MemoryStream(), 0, 0);
                file.Stream.WriteTo(stream);
                file.ChangeStream(stream);
            }

            // 2 While get the end of the file
            //	2.A Search next value & Edit the value
            int pos = 0;

            while (pos != -1)
            {
                pos = Search(file.Stream, pos);
                if (pos != -1)
                {
                    found = true;
                    pos   = Replace(file.Stream, pos);
                }
            }

            // Write if it was decoded return to original state
            if (isEncoded)
            {
                SetEncodingFormat(file, false);
                file.Format.Read();             // Get data from file
                file.Format.Import(noParams);   // Call to encoding program
                file.Format.Write();            // Overwrite data file
                file.Format.Dispose();
            }

            return(found);
        }