Пример #1
0
        public static void SaveTrainerSpriteToFreeSpace(int index, byte[] sprite, Color[] pallete)
        {
            var    sOffset = int.Parse(Ini.GetString(Conversions.ToString(MMainFunctions.GetIniFileLocation()), MainObject.Header, "TrainerImageTable", ""), System.Globalization.NumberStyles.HexNumber) + index * 8;
            var    pOffset = int.Parse(Ini.GetString(Conversions.ToString(MMainFunctions.GetIniFileLocation()), MainObject.Header, "TrainerPaletteTable", ""), System.Globalization.NumberStyles.HexNumber) + index * 8;
            string imgString;
            string palString;

            byte[] imgBytes;
            byte[] palBytes;
            string imgNewOffset;
            string palNewOffset;

            string LocalCompressLz77String()
            {
                var argsrcString = WichuRomFunctions.ConvertByteArrayToString(ref sprite); var ret = WichuRomFunctions.CompressLz77String(ref argsrcString); return(ret);
            }

            var argstr = LocalCompressLz77String();

            imgBytes = WichuRomFunctions.ConvertStringToByteArray(ref argstr);
            string LocalCompressLz77String1()
            {
                var argsrcString = WichuSpriteFunctions.ConvertPaletteToString(ref pallete); var ret = WichuRomFunctions.CompressLz77String(ref argsrcString); return(ret);
            }

            var argstr1 = LocalCompressLz77String1();

            palBytes     = WichuRomFunctions.ConvertStringToByteArray(ref argstr1);
            imgString    = ByteFunctions.ByteArrayToHexString(imgBytes);
            palString    = ByteFunctions.ByteArrayToHexString(palBytes);
            imgNewOffset = modSearchFreeSpace.SearchFreeSpaceFourAligned(MainObject.LoadedRom, 0xFF, (long)(Strings.Len(imgString) / 2d), Conversions.ToLong("&H" + Ini.GetString(Conversions.ToString(MMainFunctions.GetIniFileLocation()), MainObject.Header, "StartSearchingForSpaceOffset", "800000"))).ToString();
            var argStart = Conversions.ToInteger(imgNewOffset);

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref argStart, ref imgString);
            string LocalReverseHex()
            {
                var argHexData = Conversion.Hex(Conversions.ToDouble(imgNewOffset) + 134217728d); var ret = HexFunctions.ReverseHex(ref argHexData); return(ret);
            }

            var argData = LocalReverseHex();

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref sOffset, ref argData);
            palNewOffset = modSearchFreeSpace.SearchFreeSpaceFourAligned(MainObject.LoadedRom, 0xFF, (long)(Strings.Len(palString) / 2d), Conversions.ToLong("&H" + Ini.GetString(Conversions.ToString(MMainFunctions.GetIniFileLocation()), MainObject.Header, "StartSearchingForSpaceOffset", "800000"))).ToString();
            var argStart1 = Conversions.ToInteger(palNewOffset);

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref argStart1, ref palString);
            string LocalReverseHex1()
            {
                var argHexData = Conversion.Hex(Conversions.ToDouble(palNewOffset) + 134217728d); var ret = HexFunctions.ReverseHex(ref argHexData); return(ret);
            }

            var argData1 = LocalReverseHex1();

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref pOffset, ref argData1);
        }
Пример #2
0
        public static void SaveAnimationSpriteToFreeSpace(int pokemonindex, byte[] sprite)
        {
            var    sOffset = int.Parse(Ini.GetString(Conversions.ToString(MMainFunctions.GetIniFileLocation()), MainObject.Header, "PokemonAnimations", ""), System.Globalization.NumberStyles.HexNumber) + pokemonindex * 8; // Pointer to Pokemon front sprites, + 8 = Bulbasaur.
            string imgString;

            byte[] imgBytes;
            string imgNewOffset;

            string LocalCompressLz77String()
            {
                var argsrcString = WichuRomFunctions.ConvertByteArrayToString(ref sprite);
                var ret          = WichuRomFunctions.CompressLz77String(ref argsrcString);

                return(ret);
            }

            var argstr = LocalCompressLz77String();

            imgBytes     = WichuRomFunctions.ConvertStringToByteArray(ref argstr);
            imgString    = ByteFunctions.ByteArrayToHexString(imgBytes);
            imgNewOffset = modSearchFreeSpace.SearchFreeSpaceFourAligned(MainObject.LoadedRom, 0xFF, (long)(Strings.Len(imgString) / 2d), Conversions.ToLong("&H" + Ini.GetString(Conversions.ToString(MMainFunctions.GetIniFileLocation()), MainObject.Header, "StartSearchingForSpaceOffset", "800000"))).ToString();


            var argStart = Conversions.ToInteger(imgNewOffset);

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref argStart, ref imgString);

            string LocalReverseHex()
            {
                var argHexData = Conversion.Hex(Conversions.ToDouble(imgNewOffset) + 134217728d);
                var ret        = HexFunctions.ReverseHex(ref argHexData);

                return(ret);
            }

            var argData = LocalReverseHex();

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref sOffset, ref argData);
        }
Пример #3
0
        public static bool SaveCry(Cry crytosave, int cryTable)
        {
            if (crytosave.Offset == 0)
            {
                return(false);
            }
            // var lookup = new byte[] { 0x0, 0x1, 0x4, 0x9, 0x10, 0x19, 0x24, 0x31, 0xC0, 0xCF, 0xDC, 0xE7, 0xF0, 0xF7, 0xFC, 0xFF };
            var lookup = new sbyte[] { 0, 1, 4, 9, 16, 25, 36, 49, -64, -49, -36, -25, -16, -9, -4, -1 };

            crytosave.Compressed = false;

            // copy cry data to be written
            var data = new List <byte>();

            if (crytosave.Compressed)
            {
                //Interaction.MsgBox("This should not be enabled!");
                Environment.Exit(0);
            }

            // ' data is compressed in blocks of 1 + 0x20 bytes at a time
            // ' first byte is normal signed PCM data
            // ' following 0x20 bytes are compressed based on previous value
            // ' (for a value not in lookup table, closest value will be chosen instead)
            // 'Console.WriteLine("compressed");

            // ' each block has 0x40 samples
            // Dim blockCount = crytosave.Data.Length / &H40
            // If crytosave.Data.Length Mod &H40 > 0 Then
            // blockCount += 1
            // End If

            // ' truncates the length of the last block
            // ' so we don't waste space
            // Dim lastBlockSize = crytosave.Data.Length - crytosave.Data.Length / &H40 * &H40
            // If lastBlockSize = 0 Then
            // lastBlockSize = &H21
            // Else
            // lastBlockSize = 1 + (lastBlockSize / 2) + (If(lastBlockSize Mod 2 = 0, 0, 1))
            // End If

            // Dim blocks = New Byte(blockCount - 1)() {}
            // For n As Integer = 0 To blockCount - 1
            // ' create new block
            // If n < blockCount - 1 Then
            // blocks(n) = New Byte(32) {}
            // Else
            // blocks(n) = New Byte(lastBlockSize - 1) {}
            // End If

            // Dim i As Integer = n * &H40
            // Dim k As Integer = 0

            // If i < crytosave.Data.Length Then
            // ' set first value
            // blocks(n)(k) = BitConverter.GetBytes(crytosave.Data(i))(0)
            // End If

            // k = k + 1

            // Dim pcm As SByte

            // If i < crytosave.Data.Length Then

            // pcm = crytosave.Data(i)

            // End If

            // i = i + 1

            // Dim j As Integer = 1
            // While j < &H40 And i < crytosave.Data.Length
            // ' get current sample
            // Dim sample As SByte = crytosave.Data(i)

            // i = i + 1

            // ' difference between previous sample and this
            // Dim diff As Integer = sample - pcm

            // ' check for a perfect match in lookup table
            // Dim lookupI = -1
            // For x As Integer = 0 To 15
            // If lookup(x) = diff Then
            // lookupI = x
            // Exit For
            // End If
            // Next

            // ' search for the closest match in the table
            // If lookupI = -1 Then
            // Dim bestDiff As Integer = 255
            // For x As Integer = 0 To 15
            // If Math.Abs(CInt(lookup(x)) - diff) < bestDiff Then
            // lookupI = x
            // bestDiff = Math.Abs(lookup(x) - diff)
            // End If
            // Next
            // End If

            // ' set value in block
            // ' on an odd value, increase position in block
            // If j Mod 2 = 0 Then
            // blocks(n)(k) = blocks(n)(k) Or CByte(lookupI << 4)
            // Else
            // blocks(n)(k) = blocks(n)(k) Or CByte(lookupI)

            // k = k + 1

            // End If

            // ' set previous
            // pcm = sample
            // j += 1
            // End While
            // Next

            // For n As Integer = 0 To blockCount - 1
            // data.AddRange(blocks(n))
            // Next
            else
            {
                // uncompressed, copy directly to data
                // Console.WriteLine("uncompressed");
                foreach (var s in crytosave.Data)
                {
                    data.Add((byte)(s & 0xFF));
                }
            }

            //// determine if cry requires repointing
            //if (crytosave.Size < data.Count)
            //{
            //    var result = MessageBox.Show("The Cry will be written to free space and the pointer will be repointed. Would you like to do that?", "Repoint?", MessageBoxButtons.YesNo);
            //    if (result == DialogResult.Yes)
            //    {
            //        var result2 = MessageBox.Show("Fill the old cry with free space?", "Delete old cry?", MessageBoxButtons.YesNo);
            //        if (result2 == DialogResult.Yes)
            //        {
            //            string argData = Conversions.ToString(MainObject.MakeFreeSpaceString(crytosave.Size + 16));
            //            HexFunctions.WriteHEX(ref MainObject.LoadedRom, ref crytosave.Offset, ref argData);
            //        }

            //        // set new cry offset
            //        crytosave.Offset = (int)modSearchFreeSpace.SearchFreeSpaceFourAligned(MainObject.LoadedRom, 0xFF, data.Count, Conversions.ToLong("&H" + INI.GetString(Conversions.ToString(MainObject.GetIniFileLocation()), MainObject.Header, "StartSearchingForSpaceOffset", "800000")));
            //    }
            //    else
            //    {
            //        return false;
            //    }
            //}

            // write cry


            string LocalReverseHex()
            {
                var argHexData = Strings.Right("0000" + (ushort)(crytosave.Compressed ? 1 : 0), 4); var ret = HexFunctions.ReverseHex(ref argHexData); return(ret);
            }

            var argData1 = LocalReverseHex();

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref crytosave.Offset, ref argData1);
            string LocalReverseHex1()
            {
                var argHexData = Strings.Right("0000" + (ushort)(crytosave.Looped ? 0x4000 : 0), 4); var ret = HexFunctions.ReverseHex(ref argHexData); return(ret);
            }

            var argStart = crytosave.Offset + 2;
            var argData2 = LocalReverseHex1();

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref argStart, ref argData2);
            string LocalReverseHex2()
            {
                var argHexData = Strings.Right("00000000" + (crytosave.SampleRate << 10), 8); var ret = HexFunctions.ReverseHex(ref argHexData); return(ret);
            }

            var argStart1 = crytosave.Offset + 4;
            var argData3  = LocalReverseHex2();

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref argStart1, ref argData3);
            string LocalReverseHex3()
            {
                var argHexData = Strings.Right("00000000" + crytosave.LoopStart, 8); var ret = HexFunctions.ReverseHex(ref argHexData); return(ret);
            }

            var argStart2 = crytosave.Offset + 8;
            var argData4  = LocalReverseHex3();

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref argStart2, ref argData4);
            string LocalReverseHex4()
            {
                var argHexData = Strings.Right("00000000" + (crytosave.Data.Length - 1), 8); var ret = HexFunctions.ReverseHex(ref argHexData); return(ret);
            }

            var argStart3 = crytosave.Offset + 12;
            var argData5  = LocalReverseHex4();

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref argStart3, ref argData5);

            // WriteHEX(LoadedROM, crytosave.Offset, ReverseHEX(VB.Right("0000" & CUShort(If(crytosave.Compressed, 1, 0)), 4)))
            // WriteHEX(LoadedROM, crytosave.Offset + 2, ReverseHEX(VB.Right("0000" & CUShort(If(crytosave.Looped, &H4000, 0)), 4)))
            // WriteHEX(LoadedROM, crytosave.Offset + 4, ReverseHEX(VB.Right("00000000" & (crytosave.SampleRate << 10), 8)))
            // WriteHEX(LoadedROM, crytosave.Offset + 8, ReverseHEX(VB.Right("00000000" & (crytosave.LoopStart), 8)))
            // WriteHEX(LoadedROM, crytosave.Offset + 12, ReverseHEX(VB.Right("00000000" & (crytosave.Data.Length - 1), 8)))

            var tempbuff  = ByteFunctions.ByteArrayToHexString(data.ToArray());
            var argStart4 = crytosave.Offset + 16;

            HexFunctions.WriteHex(ref MainObject.LoadedRom, ref argStart4, ref tempbuff);

            // write cry table entry

            // WriteHEX(LoadedROM, cryTable + (crytosave.Index * 12), ReverseHEX(If(crytosave.Compressed, "00003C20", "00003C00")))
            // WriteHEX(LoadedROM, cryTable + (crytosave.Index * 12) + 4, ReverseHEX(VB.Right("00000000" & Hex(crytosave.Offset), 8)))
            // WriteHEX(LoadedROM, cryTable + (crytosave.Index * 12) + 8, "FF00FF")


            // rom.WriteUInt16(CUShort(If(Cry.Compressed, 1, 0)))
            // rom.WriteUInt16(CUShort(If(Cry.Looped, &H4000, 0)))
            // rom.WriteInt32(Cry.SampleRate << 10)
            // rom.WriteInt32(Cry.LoopStart)
            // rom.WriteInt32(Cry.Data.Length - 1)
            // rom.WriteBytes(data.ToArray())

            // ' write cry table entry
            // rom.Seek(cryTable + Cry.Index * 12)
            // rom.WriteUInt32(If(Cry.Compressed, &H3C20UI, &H3C00UI))
            // rom.WritePointer(Cry.Offset)
            // rom.WriteUInt32(&HFF00FFUI)

            // ' write growl table entry
            // rom.Seek(growlTable + Cry.Index * 12)
            // rom.WriteUInt32(If(Cry.Compressed, &H3C30UI, &H3C00UI))
            // ' !!! not sure if 00 should be used for uncompressed
            // rom.WritePointer(Cry.Offset)
            // rom.WriteUInt32(&HFF00FFUI)
            return(true);
        }