Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- namespace WR
- {
- internal static class Program
- {
- private const string BkpSuffix = "_BKP";
- private const string WormsreloadedExe = "WormsReloaded.exe";
- private const string LanguageFile = "DataPC/Language/AllTextWR.bin";
- private static readonly byte[] Orgepattern = new byte[] {0x41, 0x6c, 0x6c, 0x54, 0x65, 0x78, 0x74, 0x25, 0x73};
- private static readonly byte[] ReplacePattern = new byte[]
- {0x57, 0x52};
- private static void Main()
- {
- try
- {
- string curentDir = Directory.GetCurrentDirectory();
- string wrExeFile = Path.Combine(curentDir, WormsreloadedExe);
- if (!File.Exists(wrExeFile))
- {
- Console.WriteLine("Can't find " + WormsreloadedExe);
- return;
- }
- if (!File.Exists(wrExeFile + BkpSuffix))
- {
- File.Copy(wrExeFile, wrExeFile + BkpSuffix);
- }
- Stream exeStream = new FileStream(wrExeFile, FileMode.Open, FileAccess.ReadWrite);
- Console.WriteLine("Searching for pattern '" + Encoding.UTF8.GetString(Orgepattern) + "' in " +
- WormsreloadedExe);
- long pos = ReadTo(Orgepattern, exeStream);
- if (pos == -1)
- {
- Console.WriteLine("Can't find pattern!");
- return;
- }
- Console.WriteLine("Found! Modyfying file... " + Encoding.UTF8.GetString(Orgepattern) + " -> " +
- Encoding.UTF8.GetString(ReplacePattern));
- exeStream.Seek(-ReplacePattern.Length, SeekOrigin.Current);
- exeStream.Write(ReplacePattern, 0, ReplacePattern.Length);
- Console.WriteLine("Done!");
- exeStream.Flush();
- exeStream.Close();
- if (!File.Exists(Path.Combine(curentDir, LanguageFile)))
- {
- Console.WriteLine("Remember about creating " + LanguageFile + " !");
- }
- }
- catch (Exception)
- {
- Console.WriteLine("Something went teribbly wrong...");
- }
- Console.Read();
- }
- private static long ReadTo(byte[] pattern, Stream stream)
- {
- int[] f = KmpFailureFunction(pattern);
- int j = 0;
- int tmp = stream.ReadByte();
- if (tmp < 0)
- return tmp;
- byte t = (byte) tmp;
- while (stream.Position < stream.Length + 1)
- {
- if (pattern[j] == t)
- {
- if (j == pattern.Length - 1)
- return stream.Position - pattern.Length;
- tmp = stream.ReadByte();
- if (tmp < 0)
- return tmp;
- t = (byte) tmp;
- j++;
- }
- else if (j > 0)
- {
- j = f[j - 1];
- }
- else
- {
- tmp = stream.ReadByte();
- if (tmp < 0)
- return tmp;
- t = (byte) tmp;
- }
- }
- return -1;
- }
- private static int[] KmpFailureFunction(byte[] pattern)
- {
- int i = 1;
- int j = 0;
- int[] f = new int[pattern.Length];
- if (f.Length > 0)
- f[0] = 0;
- while (i < pattern.Length)
- {
- if (pattern[j] == pattern[i])
- {
- f[i] = j + 1;
- i++;
- j++;
- }
- else if (j > 0)
- {
- j = f[j - 1];
- }
- else
- {
- f[i] = 0;
- i++;
- }
- }
- return f;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement