forked from bladecoding/Terraria-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
206 lines (179 loc) · 7.54 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;
namespace TerrariaPatcher
{
internal class Program
{
static readonly string TmpDir = "apitmp";
#if CLIENT
static readonly string AssemblyName = "Terraria";
#elif SERVER
static readonly string AssemblyName = "TerrariaServer";
#else
#error Invalid Defines
#endif
private static void Main(string[] args)
{
try
{
if (Environment.OSVersion.Version.Major >= 6 && !IsAdministrator())
{
Console.WriteLine("TerrariaPatcher requires admin privileges.");
var selfProc = new Process();
selfProc.StartInfo.FileName = Assembly.GetExecutingAssembly().Location;
selfProc.StartInfo.Verb = "runas";
selfProc.Start();
return;
}
if (!File.Exists(AssemblyName + ".exe"))
{
Output(AssemblyName + ".exe not found");
Console.ReadLine();
return;
}
string md5;
using (var fs = new FileStream(AssemblyName + ".exe", FileMode.Open, FileAccess.Read, FileShare.Read))
{
md5 = MD5(fs);
}
string apitmp = Path.Combine(Environment.CurrentDirectory, "apitmp");
Output("Downloading Patches Information");
var patches = DownloadPatches();
if (!patches.Contains(md5))
{
Output(AssemblyName + " hash not found ({0})", md5);
Console.ReadLine();
return;
}
if (!Directory.Exists(TmpDir))
Directory.CreateDirectory(TmpDir);
Output("Downloading Diff");
string patch = DownloadPatch(md5);
File.WriteAllText(Path.Combine(TmpDir, AssemblyName + ".diff"), patch);
Output("Extracting Resources");
File.WriteAllBytes(Path.Combine(TmpDir, "ildasm.exe"), Properties.Resources.ildasm);
File.WriteAllBytes(Path.Combine(TmpDir, "IlDasmrc.dll"), Properties.Resources.IlDasmrc);
File.WriteAllBytes(Path.Combine(TmpDir, "patch.exe"), Properties.Resources.patch);
Output("Running IlDasm");
var proc = new Process();
proc.StartInfo.FileName = "apitmp/ildasm.exe";
proc.StartInfo.Arguments = AssemblyName + ".exe /output:" + Path.Combine(TmpDir, AssemblyName + ".il");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
proc.WaitForExit();
Log(proc.StandardOutput.ReadToEnd());
Log(proc.StandardError.ReadToEnd());
Output("Running Patch");
proc = new Process();
proc.StartInfo.FileName = "apitmp/patch.exe";
proc.StartInfo.Arguments = "-u -o New" + AssemblyName + ".il -i " + AssemblyName + ".diff";
proc.StartInfo.WorkingDirectory = apitmp;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
proc.WaitForExit();
Log(proc.StandardOutput.ReadToEnd());
Log(proc.StandardError.ReadToEnd());
if (File.Exists(Path.Combine(apitmp, "New" + AssemblyName + ".il.rej")))
Log("\n\n[[{0}]]\n\n", File.ReadAllText(Path.Combine(apitmp, "New" + AssemblyName + ".il.rej")));
Output("Running Ilasm");
proc = new Process();
proc.StartInfo.FileName = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "ilasm.exe");
proc.StartInfo.Arguments = "New" + AssemblyName + ".il /quiet /output=" + AssemblyName + ".exe /res=" + AssemblyName + ".res";
proc.StartInfo.WorkingDirectory = apitmp;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.Start();
proc.WaitForExit();
Log(proc.StandardOutput.ReadToEnd());
Log(proc.StandardError.ReadToEnd());
Output("Finishing");
if (File.Exists(Path.Combine(TmpDir, AssemblyName+".exe")))
{
File.Copy(Path.Combine(TmpDir, AssemblyName + ".exe"), AssemblyName + "API.exe", true);
Output("Success");
}
else
{
Output("Failed");
}
}
catch (Exception ex)
{
Log(ex.ToString());
Output("Exception: " + ex);
}
finally
{
if (Directory.Exists(TmpDir))
Directory.Delete(TmpDir, true);
}
Console.ReadLine();
}
public static bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private static void Output(string str)
{
if (str.Length < 1)
return;
Console.WriteLine(str);
Log(str);
}
private static void Output(string str, params object[] objs)
{
Output(string.Format(str, objs));
}
private static void Log(string str)
{
if (str.Length < 1)
return;
File.AppendAllText("patchlog.txt", str + "\n");
}
private static void Log(string str, params object[] objs)
{
Log(string.Format(str, objs));
}
private static List<string> DownloadPatches()
{
var ret = new List<string>();
string patches = new WebClient().DownloadString("http://dl.dropbox.com/u/29760911/" + AssemblyName + "Api/patches.txt");
var ps = patches.Split('\n', '\r');
foreach (var p in ps)
{
var kv = p.Split('|');
if (kv.Length != 2)
continue;
ret.Add(kv[1]);
}
return ret;
}
private static string DownloadPatch(string md5)
{
return new WebClient().DownloadString("http://dl.dropbox.com/u/29760911/" + AssemblyName + "Api/" + AssemblyName + "_" + md5 + ".diff");
}
private static string MD5(Stream stream)
{
using (var sha = MD5CryptoServiceProvider.Create())
{
var bytes = sha.ComputeHash(stream);
return bytes.Aggregate("", (s, b) => s + b.ToString("X2"));
}
}
}
}