-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtils.cs
More file actions
244 lines (199 loc) · 8.88 KB
/
Copy pathUtils.cs
File metadata and controls
244 lines (199 loc) · 8.88 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
static class Utils {
// technically mebibytes
public static string BytesToMegabytes(long byteCount) {
return (byteCount / 1049000.0).ToString("F1");
}
public static void ExtractFilesFromZip(string zipPath, string extractPath) {
using (ZipArchive archive = ZipFile.OpenRead(zipPath)) {
foreach (var entry in archive.Entries) {
// skip directories themselves
if (string.IsNullOrEmpty(entry.Name)) {
continue;
}
// don't process subdirectories
if (entry.FullName.Contains('/') || entry.FullName.Contains('\\')) {
continue;
}
// ensure destination directory exists
Directory.CreateDirectory(extractPath);
string destinationPath = Path.Combine(extractPath, entry.Name);
try {
entry.ExtractToFile(destinationPath, true);
} catch (Exception ex) {
Console.WriteLine($"Failed to extract to file: {ex}");
}
}
}
}
public static void ExtractFolderFromZip(string zipPath, string folderName, string extractPath) {
// normalize slashes
folderName = folderName.Replace('\\', '/').TrimEnd('/') + "/";
using (ZipArchive archive = ZipFile.OpenRead(zipPath)) {
foreach (var entry in archive.Entries) {
// skip anything not in the target folder
if (!entry.FullName.StartsWith(folderName, StringComparison.OrdinalIgnoreCase)) {
continue;
}
// get relative path inside the target folder
string relativePath = entry.FullName.Substring(folderName.Length);
// skip root folder itself
if (string.IsNullOrEmpty(relativePath)) {
continue;
}
string destinationPath = Path.Combine(extractPath, relativePath);
// prevent path traversal
string fullPath = Path.GetFullPath(destinationPath);
if (!fullPath.StartsWith(Path.GetFullPath(extractPath), StringComparison.OrdinalIgnoreCase)) {
continue;
}
// if it's a directory, create it
if (string.IsNullOrEmpty(entry.Name)) {
Directory.CreateDirectory(fullPath);
} else {
// ensure directory exists
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
entry.ExtractToFile(fullPath, true);
}
}
}
}
public static void RefreshFolderFromZip(string zipPath, string folderName, string extractPath) {
folderName = folderName.Replace('\\', '/').TrimEnd('/') + "/";
using (ZipArchive archive = ZipFile.OpenRead(zipPath)) {
// track directories we’ve already ensured exist
HashSet<string> createdDirs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var entry in archive.Entries) {
if (!entry.FullName.StartsWith(folderName, StringComparison.OrdinalIgnoreCase)) {
continue;
}
string relativePath = entry.FullName.Substring(folderName.Length);
if (string.IsNullOrEmpty(relativePath)) {
continue;
}
string destinationPath = Path.Combine(extractPath, folderName, relativePath);
string fullPath = Path.GetFullPath(destinationPath);
string rootFullPath = Path.GetFullPath(extractPath);
// safety check
if (!fullPath.StartsWith(rootFullPath, StringComparison.OrdinalIgnoreCase)) {
continue;
}
// handle directory
if (string.IsNullOrEmpty(entry.Name)) {
if (!Directory.Exists(fullPath)) {
Directory.CreateDirectory(fullPath);
}
continue;
}
// ensure directory exists
string dir = Path.GetDirectoryName(fullPath);
if (!string.IsNullOrEmpty(dir) && !createdDirs.Contains(dir)) {
Directory.CreateDirectory(dir);
createdDirs.Add(dir);
}
// this is KEY: delete existing file before extracting
if (File.Exists(fullPath)) {
File.Delete(fullPath);
}
// extract fresh file
entry.ExtractToFile(fullPath);
}
}
}
public static bool IsSteamOS() {
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
return false;
}
const string osRelease = "/etc/os-release";
if (!File.Exists(osRelease))
return false;
try {
string text = File.ReadAllText(osRelease);
return text.Contains("ID=steamos", StringComparison.OrdinalIgnoreCase)
|| text.Contains("NAME=\"SteamOS\"", StringComparison.OrdinalIgnoreCase);
} catch {
return false;
}
}
public static bool IsRunningFromAppBundle() {
return AppContext.BaseDirectory.Contains(".app/") && RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
}
public static string GetGameFilename() {
string extension = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
extension = ".exe";
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
extension = ".app";
}
return $"sm64coopdx{extension}";
}
public static string GetGamePath() {
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
string gamePath = AppContext.BaseDirectory;
if (Program.tempUpdater) {
// update is always installed to /Applications/ so directly override var
gamePath = Path.Combine("/Applications/",GetGameFilename());
} else if (IsRunningFromAppBundle()) {
// truncate to .app if we are running from an app bundle
int appIndex = gamePath.IndexOf(".app", StringComparison.Ordinal);
gamePath = gamePath.Substring(0, appIndex + 4);
}
return gamePath;
} else {
return Path.Combine(Program.gamePath, GetGameFilename());
}
}
public static string GetAppDataPath() {
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
return Path.Combine(AppContext.BaseDirectory, IsRunningFromAppBundle() ? "/Contents/MacOS/" : "");
} else {
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "coopdx-updater");
}
}
// borrowed from coop-compiler
public static async Task<string> GetAsync(string url) {
using (var client = new HttpClient()) {
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36");
// HTTP GET
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode) {
string result = await response.Content.ReadAsStringAsync();
return result;
}
}
return null;
}
public static string SloppyExtract(string json, string field) {
json = json.Replace(" ", "");
string fieldJson = "\"" + field + "\":";
if (!json.Contains(fieldJson)) { return null; }
string after = json.Split(new string[] { fieldJson }, 2, StringSplitOptions.RemoveEmptyEntries)[1];
string[] valueSplit = after.Split('"');
if (valueSplit.Count() < 2) { return null; }
return valueSplit[1];
}
public static async Task<string> GetSloppyAsync(string url, string parameter) {
string jsonString = await GetAsync(url);
if (jsonString == null) { return null; }
return SloppyExtract(jsonString, parameter);
}
public static void StartGame() {
Directory.SetCurrentDirectory(Program.gamePath);
Process.Start(new ProcessStartInfo {
FileName = GetGamePath(),
Arguments = "--skip-update-check",
UseShellExecute = true
});
}
}