-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cs
178 lines (149 loc) · 8.44 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace ERSaveManager
{
public class Utils
{
private static readonly string defaultJpeg = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABaAKADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5i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cNA2kdqSiimIKKKKACiiigAooooAKKKKACiiigAooooAcgRmw7lR64zTisWBiQk9/l6VHRQBLshwf3p6f3KCkXaU/wDfFRUUASFY+P3v/jtIqxlctIQfTbTKKAJCkeOJcnH92mMADwcj6UlFABRRRQAUUUUAFFFFAH//2Q==";
[DllImport("USER32.DLL")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("USER32.DLL")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
public static readonly string data_ersm = "data.ersm";
public static readonly string source_ersm = "source.ersm";
public static readonly string delta_ersm = "delta.ersm";
public static readonly string new_ersm = "new.ersm";
public static readonly string snapshot_jpg = "snapshot.jpg";
public static string Sha1Sum(string fileName)
{
var sha1 = SHA1.Create();
var hash = sha1.ComputeHash(File.ReadAllBytes(fileName));
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
public static string CreateSnapshot(SaveItem save)
{
if (!File.Exists(source_ersm))
{
var src = Convert.FromBase64String(save.InitData);
File.WriteAllBytes(source_ersm, GZip.decompress(src));
}
var fi = new FileInfo(source_ersm);
var oldFile = fi.FullName;
var newFile = save.FileName;
Process.Start(new ProcessStartInfo
{
FileName = "xdelta3.exe",
Arguments = $"-A -f -e -s \"{oldFile}\" \"{newFile}\" {delta_ersm}",
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = Environment.CurrentDirectory
}).WaitForExit();
if (File.Exists(delta_ersm))
{
var data = File.ReadAllBytes(delta_ersm);
File.Delete(delta_ersm);
return Convert.ToBase64String(data);
}
else
{
return String.Empty;
}
}
public static bool RestoreSnapshot(SaveItem save, int index)
{
if (!File.Exists(source_ersm))
{
var src = Convert.FromBase64String(save.InitData);
File.WriteAllBytes(source_ersm, GZip.decompress(src));
}
var fi = new FileInfo(source_ersm);
var oldFile = fi.FullName;
var record = save.Slots[index];
File.WriteAllBytes(delta_ersm, Convert.FromBase64String(record.Data));
Process.Start(new ProcessStartInfo
{
FileName = "xdelta3.exe",
Arguments = $"-f -d -s \"{oldFile}\" {delta_ersm} {new_ersm}",
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = Environment.CurrentDirectory
}).WaitForExit();
File.Delete(delta_ersm);
if (Sha1Sum(new_ersm) == record.Hash)
{
var bakFile = Environment.CurrentDirectory + "\\" + Path.GetFileName(save.FileName) + ".bak";
var path = Path.GetDirectoryName(save.FileName);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
if (File.Exists(save.FileName))
File.Replace(new_ersm, save.FileName, bakFile);
else
File.Move(new_ersm, save.FileName);
return true;
}
return false;
}
public static string ScreenShot()
{
var procList = Process.GetProcessesByName("eldenring");
if (procList.Length != 1)
{
return defaultJpeg;
}
var proc = procList[0];
ShowWindowAsync(proc.MainWindowHandle, 1);
SetForegroundWindow(proc.MainWindowHandle);
Thread.Sleep(1000);
var rect = new Rect();
GetWindowRect(proc.MainWindowHandle, ref rect);
var width = rect.right - rect.left;
var height = rect.bottom - rect.top;
using (var source = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
using (var g1 = Graphics.FromImage(source))
{
g1.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
using (var convert = new Bitmap(160, 90))
{
using (var g2 = Graphics.FromImage(convert))
{
g2.DrawImage(source, 0, 0, convert.Width, convert.Height);
}
convert.Save(snapshot_jpg, ImageFormat.Jpeg);
}
}
}
if (File.Exists(snapshot_jpg))
{
var data = File.ReadAllBytes(snapshot_jpg);
File.Delete(snapshot_jpg);
return Convert.ToBase64String(data);
}
else
{
return defaultJpeg;
}
}
}
}