-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathProgram.cs
96 lines (82 loc) · 2.85 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using UnpackMiColorFace.Helpers;
namespace UnpackMiColorFace
{
class Program
{
public const string library = "UnpackMiColorFace.Lib.Magick.NET-Q16-AnyCPU.dll";
public const string libraryCommon = "UnpackMiColorFace.Lib.XiaomiWatch.Common.dll";
[STAThread]
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolver);
MainBody(args);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void MainBody(string[] args)
{
if (args.Count() == 0)
{
Console.WriteLine("usage: UnpackMiColorFace example.bin");
return;
}
string filename = args[0];
if (!File.Exists(filename))
{
Console.WriteLine($"File {filename} is not found.");
return;
}
try
{
Unpacker.Exec(filename);
if (LogHelper.GotError)
Console.ReadKey();
}
catch (MissingFieldException)
{
Console.WriteLine("Seems wrong file passed,\r\nPlease check a source file is correct Watchface");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine("Got unexcepted error,\r\nPlease check a source file is correct Watchface");
Console.ReadKey();
}
}
private static Assembly AssemblyResolver(object sender, ResolveEventArgs args)
{
Assembly asm = null;
asm = LoadAssembly(args, "Magick", library);
if (asm != null) return asm;
asm = LoadAssembly(args, "XiaomiWatch", libraryCommon);
if (asm != null) return asm;
return asm;
}
private static Assembly LoadAssembly(ResolveEventArgs args, string name, string libName)
{
var assembly = Assembly.GetExecutingAssembly();
if (args.Name.Contains(name))
{
using (var stream = assembly.GetManifestResourceStream(libName))
{
using (var reader = new BinaryReader(stream))
{
byte[] rawAssembly = new byte[stream.Length];
reader.Read(rawAssembly, 0, rawAssembly.Length);
Assembly asm = Assembly.Load(rawAssembly);
return asm;
}
}
}
return null;
}
}
}