Advertisement
Guest User

Untitled

a guest
Jul 13th, 2011
1,582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.24 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. namespace WR
  6. {
  7.     internal static class Program
  8.     {
  9.         private const string BkpSuffix = "_BKP";
  10.         private const string WormsreloadedExe = "WormsReloaded.exe";
  11.         private const string LanguageFile = "DataPC/Language/AllTextWR.bin";
  12.         private static readonly byte[] Orgepattern = new byte[] {0x41, 0x6c, 0x6c, 0x54, 0x65, 0x78, 0x74, 0x25, 0x73};
  13.  
  14.         private static readonly byte[] ReplacePattern = new byte[]
  15.                                                             {0x57, 0x52};
  16.  
  17.         private static void Main()
  18.         {
  19.             try
  20.             {
  21.                 string curentDir = Directory.GetCurrentDirectory();
  22.                 string wrExeFile = Path.Combine(curentDir, WormsreloadedExe);
  23.                 if (!File.Exists(wrExeFile))
  24.                 {
  25.                     Console.WriteLine("Can't find " + WormsreloadedExe);
  26.                     return;
  27.                 }
  28.  
  29.                 if (!File.Exists(wrExeFile + BkpSuffix))
  30.                 {
  31.                     File.Copy(wrExeFile, wrExeFile + BkpSuffix);
  32.                 }
  33.  
  34.                 Stream exeStream = new FileStream(wrExeFile, FileMode.Open, FileAccess.ReadWrite);
  35.                 Console.WriteLine("Searching for pattern '" + Encoding.UTF8.GetString(Orgepattern) + "' in " +
  36.                                   WormsreloadedExe);
  37.                 long pos = ReadTo(Orgepattern, exeStream);
  38.                 if (pos == -1)
  39.                 {
  40.                     Console.WriteLine("Can't find pattern!");
  41.                     return;
  42.                 }
  43.                 Console.WriteLine("Found! Modyfying file... " + Encoding.UTF8.GetString(Orgepattern) + " -> " +
  44.                                   Encoding.UTF8.GetString(ReplacePattern));
  45.                 exeStream.Seek(-ReplacePattern.Length, SeekOrigin.Current);
  46.                 exeStream.Write(ReplacePattern, 0, ReplacePattern.Length);
  47.                 Console.WriteLine("Done!");
  48.                 exeStream.Flush();
  49.                 exeStream.Close();
  50.                 if (!File.Exists(Path.Combine(curentDir, LanguageFile)))
  51.                 {
  52.                     Console.WriteLine("Remember about creating " + LanguageFile + " !");
  53.                 }
  54.             }
  55.             catch (Exception)
  56.             {
  57.                 Console.WriteLine("Something went teribbly wrong...");
  58.             }
  59.             Console.Read();
  60.         }
  61.  
  62.         private static long ReadTo(byte[] pattern, Stream stream)
  63.         {
  64.             int[] f = KmpFailureFunction(pattern);
  65.             int j = 0;
  66.             int tmp = stream.ReadByte();
  67.             if (tmp < 0)
  68.                 return tmp;
  69.             byte t = (byte) tmp;
  70.  
  71.             while (stream.Position < stream.Length + 1)
  72.             {
  73.                 if (pattern[j] == t)
  74.                 {
  75.                     if (j == pattern.Length - 1)
  76.                         return stream.Position - pattern.Length;
  77.  
  78.                     tmp = stream.ReadByte();
  79.                     if (tmp < 0)
  80.                         return tmp;
  81.                     t = (byte) tmp;
  82.                     j++;
  83.                 }
  84.                 else if (j > 0)
  85.                 {
  86.                     j = f[j - 1];
  87.                 }
  88.                 else
  89.                 {
  90.                     tmp = stream.ReadByte();
  91.                     if (tmp < 0)
  92.                         return tmp;
  93.                     t = (byte) tmp;
  94.                 }
  95.             }
  96.             return -1;
  97.         }
  98.  
  99.         private static int[] KmpFailureFunction(byte[] pattern)
  100.         {
  101.             int i = 1;
  102.             int j = 0;
  103.             int[] f = new int[pattern.Length];
  104.             if (f.Length > 0)
  105.                 f[0] = 0;
  106.             while (i < pattern.Length)
  107.             {
  108.                 if (pattern[j] == pattern[i])
  109.                 {
  110.                     f[i] = j + 1;
  111.                     i++;
  112.                     j++;
  113.                 }
  114.                 else if (j > 0)
  115.                 {
  116.                     j = f[j - 1];
  117.                 }
  118.                 else
  119.                 {
  120.                     f[i] = 0;
  121.                     i++;
  122.                 }
  123.             }
  124.             return f;
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement