Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.85 KB | None | 0 0
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace TrucksuPatcher
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.         [DllImport("kernel32.dll", SetLastError = true)]
  24.         [return: MarshalAs(UnmanagedType.I1)]
  25.         private static extern bool CreateSymbolicLink(
  26.         string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags);
  27.  
  28.         private enum SymbolicLink
  29.         {
  30.             File = 0,
  31.             Directory = 1
  32.         }
  33.  
  34.  
  35.         public string CopyFolder;
  36.         private List<string> de4dot = new List<string>(new string[] { "de4dot.exe", "de4dot.exe.config" });
  37.  
  38.         private List<string> de4dotdll =
  39.             new List<string>(new string[]
  40.             {
  41.                 "AssemblyData.dll", "de4dot.code.dll", "de4dot.cui.dll", "de4dot.mdecrypt.dll", "de4dot.blocks.dll",
  42.                 "dnlib.dll"
  43.             });
  44.  
  45.         private List<string> de4dotcopy =
  46.             new List<string>(new string[]
  47.             {
  48.                 "COPYING", "LICENSE.de4dot.txt", "LICENSE.dnlib.txt", "LICENSE.ICSharpCode.SharpZipLib.txt",
  49.                 "LICENSE.lzmat.txt", "LICENSE.QuickLZ.txt", "LICENSE.randomc.txt"
  50.             });
  51.  
  52.  
  53.         FolderBrowserDialog fbd = new FolderBrowserDialog();
  54.  
  55.         private void Form1_Load(object sender, EventArgs e)
  56.         {
  57.             RegistryKey osuKey = Registry.ClassesRoot.OpenSubKey("osu\\DefaultIcon");
  58.             string osuPath = osuKey.GetValue("").ToString().Split('"')[1] + "\\osu!.exe";
  59.             DirectoryInfo osuDir = Directory.GetParent(osuPath);
  60.             path.Text = Convert.ToString(osuDir);
  61.         }
  62.  
  63.         private void destBtn_Click(object sender, EventArgs e)
  64.         {
  65.             DialogResult result = fbd.ShowDialog();
  66.  
  67.             if (!string.IsNullOrWhiteSpace(fbd.SelectedPath))
  68.             {
  69.                 string[] files = Directory.GetFiles(fbd.SelectedPath);
  70.  
  71.                 //System.Windows.Forms.MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
  72.                 path.Text = fbd.SelectedPath;
  73.             }
  74.         }
  75.  
  76.         private void patchBtn_Click(object sender, EventArgs e)
  77.         {
  78.             UnpackDe4dot(System.IO.Path.GetDirectoryName(openFileDialog1.FileName));
  79.             if (
  80.                 System.IO.File.Exists(System.IO.Path.GetDirectoryName(openFileDialog1.FileName) +
  81.                                       "\\LICENSES\\LICENSE.randomc.txt"))
  82.             {
  83.                 RunProcess();
  84.  
  85.                 //p.Start(System.IO.Path.GetDirectoryName(openFileDialog1.FileName) + "\\de4dot.exe", openFileDialog1.FileName);
  86.             }
  87.             string destination = path.Text;
  88.             osuPatch(destination);
  89.         }
  90.  
  91.         private void RunProcess()
  92.         {
  93.             StringBuilder outputBuilder = new StringBuilder();
  94.             ;
  95.  
  96.             ProcessStartInfo processStartInfo = new ProcessStartInfo();
  97.             processStartInfo.UseShellExecute = false;
  98.             processStartInfo.CreateNoWindow = true;
  99.             processStartInfo.RedirectStandardOutput = true;
  100.             processStartInfo.RedirectStandardInput = true;
  101.             processStartInfo.Arguments = openFileDialog1.FileName;
  102.             processStartInfo.FileName = System.IO.Path.GetDirectoryName(openFileDialog1.FileName) + "\\de4dot.exe";
  103.  
  104.             Process p = new Process();
  105.             p.StartInfo = processStartInfo;
  106.  
  107.             p.EnableRaisingEvents = true;
  108.             // attach the event handler for OutputDataReceived before starting the process
  109.             p.OutputDataReceived += new DataReceivedEventHandler
  110.                 (
  111.                 delegate (object sender, DataReceivedEventArgs e)
  112.                 {
  113.                     // append the new data to the data already read-in
  114.                     outputBuilder.Append(e.Data);
  115.                 }
  116.                 );
  117.  
  118.  
  119.             label3.Text = "Unpacking...";
  120.             p.Start();
  121.  
  122.             p.BeginOutputReadLine();
  123.             p.WaitForExit();
  124.             p.CancelOutputRead();
  125.  
  126.             label3.Text = outputBuilder.ToString();
  127.         }
  128.         private void UnpackDe4dot(string Dir)
  129.         {
  130.             string prefix = "NosuePatcher.Resources";
  131.  
  132.             System.IO.Directory.CreateDirectory(Dir + "\\bin");
  133.             System.IO.Directory.CreateDirectory(Dir + "\\LICENSES");
  134.  
  135.             ExtractEmbeddedResource(Dir, prefix, de4dot);
  136.             ExtractEmbeddedResource(Dir + "\\bin", prefix, de4dotdll);
  137.             ExtractEmbeddedResource(Dir + "\\LICENSES", prefix, de4dotcopy);
  138.         }
  139.  
  140.         private static void ExtractEmbeddedResource(string outputDir, string resourceLocation, List<string> files)
  141.         {
  142.             foreach (string file in files)
  143.             {
  144.                 using (
  145.                     System.IO.Stream stream =
  146.                         System.Reflection.Assembly.GetExecutingAssembly()
  147.                             .GetManifestResourceStream(resourceLocation + @"." + file))
  148.                 {
  149.                     using (
  150.                         System.IO.FileStream fileStream =
  151.                             new System.IO.FileStream(System.IO.Path.Combine(outputDir, file), System.IO.FileMode.Create)
  152.                         )
  153.                     {
  154.                         for (int i = 0; i < stream.Length; i++)
  155.                         {
  156.                             fileStream.WriteByte((byte)stream.ReadByte());
  157.                         }
  158.                         fileStream.Close();
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.  
  164.         public bool IsDirectoryEmpty(string path)
  165.         {
  166.             return !Directory.EnumerateFileSystemEntries(path).Any();
  167.         }
  168.  
  169.         private void osuPatch(string Path)
  170.         {
  171.             if (System.IO.File.Exists(Path + "\\osu!-cleaned.exe"))
  172.             {
  173.                 byte[] osu = System.IO.File.ReadAllBytes(Path + "\\osu!-cleaned.exe");
  174.                 //c.ppy.sh
  175.                 var bancho = new byte[]
  176.                 {0x63, 0x00, 0x2e, 0x00, 0x70, 0x00, 0x70, 0x00, 0x79, 0x00, 0x2e, 0x00, 0x73, 0x00, 0x68};
  177.                 //c1.ppy.sh
  178.                 var bancho2 = new byte[]
  179.                 {0x63, 0x00, 0x31, 0x00, 0x2e, 0x00, 0x70, 0x00, 0x70, 0x00, 0x79, 0x00, 0x2e, 0x00, 0x73, 0x00, 0x68};
  180.                 //osu.ppy.sh
  181.                 var osush = new byte[]
  182.                 {
  183.                     0x6f, 0x00, 0x73, 0x00, 0x75, 0x00, 0x2e, 0x00, 0x70, 0x00, 0x70, 0x00, 0x79, 0x00, 0x2e, 0x00, 0x73,
  184.                     0x00,
  185.                     0x68
  186.                 };
  187.                 //a.ppy.sh
  188.                 var ash = new byte[]
  189.                 {0x61, 0x00, 0x2e, 0x00, 0x70, 0x00, 0x70, 0x00, 0x79, 0x00, 0x2e, 0x00, 0x73, 0x00, 0x68};
  190.                 //---------------------------------------------------------------------------------------------\\
  191.  
  192.                 //ash -> a
  193.                 var atrucksu = new byte[]
  194.                 {
  195.                     0x61, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x72, 0x00, 0x75, 0x00, 0x63, 0x00, 0x6b, 0x00, 0x73, 0x00, 0x75,
  196.                     0x00, 0x2e, 0x00, 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00
  197.                 };
  198.                 //bancho -> c
  199.                 var ctrucksu = new byte[]
  200.                 {
  201.                     0x63, 0x00, 0x31, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x72, 0x00, 0x75, 0x00, 0x63, 0x00, 0x6b, 0x00, 0x73,
  202.                     0x00, 0x75, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00
  203.                 };
  204.                 //bancho2 -> c1
  205.                 var c1trucksu = new byte[]
  206.                 {
  207.                     0x63, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x72, 0x00, 0x75, 0x00, 0x63, 0x00, 0x6b, 0x00, 0x73, 0x00, 0x75,
  208.                     0x00, 0x2e, 0x00, 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00
  209.                 };
  210.                 //osush -> osu
  211.                 var osutrucksu = new byte[]
  212.                 {
  213.                     0x6f, 0x00, 0x73, 0x00, 0x75, 0x00, 0x2e, 0x00, 0x74, 0x00, 0x72, 0x00, 0x75, 0x00, 0x63, 0x00, 0x6b,
  214.                     0x00, 0x73, 0x00, 0x75, 0x00, 0x2e, 0x00,0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00
  215.                 };
  216.  
  217.                 osu = Replace(osu, bancho, ctrucksu);
  218.                 osu = Replace(osu, bancho2, c1trucksu);
  219.                 osu = Replace(osu, osush, osutrucksu);
  220.                 osu = Replace(osu, ash, atrucksu);
  221.  
  222.                 System.IO.File.WriteAllBytes(Path + "\\trucksu!.exe", osu);
  223.             }
  224.         }
  225.             private static byte[] Replace(byte[] input, byte[] pattern, byte[] replacement)
  226.         {
  227.             if (pattern.Length == 0)
  228.             {
  229.                 return input;
  230.             }
  231.  
  232.             List<byte> result = new List<byte>();
  233.  
  234.             int i;
  235.  
  236.             for (i = 0; i <= input.Length - pattern.Length; i++)
  237.             {
  238.                 bool foundMatch = true;
  239.                 for (int j = 0; j < pattern.Length; j++)
  240.                 {
  241.                     if (input[i + j] != pattern[j])
  242.                     {
  243.                         foundMatch = false;
  244.                         break;
  245.                     }
  246.                 }
  247.  
  248.                 if (foundMatch)
  249.                 {
  250.                     result.AddRange(replacement);
  251.                     i += pattern.Length - 1;
  252.                 }
  253.                 else
  254.                 {
  255.                     result.Add(input[i]);
  256.                 }
  257.             }
  258.  
  259.             for (; i < input.Length; i++)
  260.             {
  261.                 result.Add(input[i]);
  262.             }
  263.  
  264.             return result.ToArray();
  265.         }
  266.  
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement