Esempio n. 1
0
        public override bool TryApplyPatch(out string failureReason)
        {
            string fileName;

            if (GameFile == GameFile.PakFile)
            {
                fileName = Chunk + ".pak";
            }
            else
            {
                fileName = GameFile.GetFileName();
            }

            string path = Directory.GetFiles(GameDirectory, fileName, SearchOption.AllDirectories).FirstOrDefault();

            if (path == null)
            {
                failureReason = $"Can't find '{fileName}'";
                return(false);
            }

            string originalBytesString = OriginalBytes.Replace("0x", "").Replace(" ", "").Replace("-", "");
            string newBytesString      = NewBytes.Replace("0x", "").Replace(" ", "").Replace("-", "");

            if (originalBytesString.Length % 2 != 0 || newBytesString.Length % 2 != 0)
            {
                failureReason = "String of bytes does not have the correct length";
                return(false);
            }

            byte[] originalBytes = originalBytesString.ConvertHexStringToBytes();
            byte[] newBytes      = newBytesString.ConvertHexStringToBytes();

            using (var fs = File.Open(path, FileMode.Open))
            {
                if (fs.Length < Offset)
                {
                    failureReason = "Invalid offset";
                    return(false);
                }
                else
                {
                    fs.Seek(Offset, SeekOrigin);

                    // Verify original bytes to make sure we're in the right place.
                    for (int i = 0; i < originalBytes.Length; i++)
                    {
                        if (originalBytes[i] != fs.ReadByte())
                        {
                            failureReason = $"Byte at offset {Offset} in '{fileName}' is different than expected";
                            return(false);
                        }
                    }

                    fs.Seek(Offset, SeekOrigin);
                    fs.Write(newBytes);
                }
            }

            failureReason = "";
            return(true);
        }
Esempio n. 2
0
 /// <summary>
 /// Applies the detour.
 /// </summary>
 public void Apply()
 {
     WriteBufferProtected(OriginalFunctionAddress, NewBytes.ToArray());
     IsApplied = true;
 }