-
Notifications
You must be signed in to change notification settings - Fork 51
/
RCLONE.cs
336 lines (290 loc) · 14.4 KB
/
RCLONE.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using JR.Utils.GUI.Forms;
using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Text;
using System.Windows.Forms;
namespace AndroidSideloader
{
internal class RCLONE
{
// Kill RCLONE Processes that were started from Rookie by looking for child processes.
public static void killRclone()
{
var parentProcessId = Process.GetCurrentProcess().Id;
var processes = Process.GetProcessesByName("rclone");
foreach (var process in processes)
{
try
{
using (ManagementObject obj = new ManagementObject($"win32_process.handle='{process.Id}'"))
{
obj.Get();
var ppid = Convert.ToInt32(obj["ParentProcessId"]);
if (ppid == parentProcessId)
{
process.Kill();
}
}
}
catch (Exception ex)
{
_ = Logger.Log($"Exception occured while attempting to shut down RCLONE with exception message: {ex.Message}", LogLevel.ERROR);
}
}
}
// For custom configs that use a password
public static void Init()
{
string PwTxtPath = Path.Combine(Environment.CurrentDirectory, "rclone\\pw.txt");
if (File.Exists(PwTxtPath))
{
rclonepw = File.ReadAllText(PwTxtPath);
}
}
// Change if you want to use a config
public static string downloadConfigPath = "vrp.download.config";
public static string uploadConfigPath = "vrp.upload.config";
public static string rclonepw = "";
private static readonly Process rclone = new Process();
// Run an RCLONE Command that accesses the Download Config.
public static ProcessOutput runRcloneCommand_DownloadConfig(string command)
{
if (MainForm.isOffline)
{
return new ProcessOutput("", "No internet");
}
ProcessOutput prcoutput = new ProcessOutput();
// Rclone output is unicode, else it will show garbage instead of unicode characters
rclone.StartInfo.StandardOutputEncoding = Encoding.UTF8;
string originalCommand = command;
// set configpath if there is any
if (downloadConfigPath.Length > 0)
{
command += $" --config {downloadConfigPath}";
}
// set rclonepw
if (rclonepw.Length > 0)
{
command += " --ask-password=false";
}
string logcmd = Utilities.StringUtilities.RemoveEverythingBeforeFirst(command, "rclone.exe");
if (logcmd.Contains($"\"{Properties.Settings.Default.CurrentLogPath}\""))
{
logcmd = logcmd.Replace($"\"{Properties.Settings.Default.CurrentLogPath}\"", $"\"{Properties.Settings.Default.CurrentLogName}\"");
}
if (logcmd.Contains(Environment.CurrentDirectory))
{
logcmd = logcmd.Replace($"{Environment.CurrentDirectory}", $"CurrentDirectory");
}
_ = Logger.Log($"Running Rclone command: {logcmd}");
rclone.StartInfo.FileName = Environment.CurrentDirectory + "\\rclone\\rclone.exe";
rclone.StartInfo.Arguments = command;
rclone.StartInfo.RedirectStandardInput = true;
rclone.StartInfo.RedirectStandardError = true;
rclone.StartInfo.RedirectStandardOutput = true;
rclone.StartInfo.WorkingDirectory = Environment.CurrentDirectory + "\\rclone";
rclone.StartInfo.CreateNoWindow = true;
// Display RCLONE Window if the binary is being run in Debug Mode.
if (MainForm.debugMode)
{
rclone.StartInfo.CreateNoWindow = false;
}
rclone.StartInfo.UseShellExecute = false;
_ = rclone.Start();
rclone.StandardInput.WriteLine(command);
rclone.StandardInput.Flush();
rclone.StandardInput.Close();
string output = rclone.StandardOutput.ReadToEnd();
string error = rclone.StandardError.ReadToEnd();
rclone.WaitForExit();
if (error.Contains("There is not enough space"))
{
_ = FlexibleMessageBox.Show(Program.form, $"There isn't enough disk space to download this game.\r\nPlease ensure you have at least 200MB more the game size available in {Environment.CurrentDirectory} and try again.",
"NOT ENOUGH SPACE",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return new ProcessOutput("Download failed.", "");
}
// Switch mirror upon matching error output.
if (error.Contains("400 Bad Request") || error.Contains("cannot fetch token") || error.Contains("authError") || error.Contains("quota") || error.Contains("exceeded") || error.Contains("directory not found") || error.Contains("Failed to"))
{
string oldRemote = MainForm.currentRemote;
try
{
Program.form.SwitchMirrors();
}
catch
{
return new ProcessOutput("All mirrors are on quota or down...", "All mirrors are on quota or down...");
}
prcoutput = runRcloneCommand_DownloadConfig(originalCommand.Replace(oldRemote, MainForm.currentRemote));
}
else
{
prcoutput.Output = output;
prcoutput.Error = error;
}
if (!output.Contains("Game Name;Release Name;") && !output.Contains("package:") && !output.Contains(".meta"))
{
if (!string.IsNullOrWhiteSpace(error))
{
_ = Logger.Log($"Rclone error: {error}\n", LogLevel.ERROR);
}
if (!string.IsNullOrWhiteSpace(output))
{
_ = Logger.Log($"Rclone Output: {output}");
}
}
return prcoutput;
}
public static ProcessOutput runRcloneCommand_UploadConfig(string command)
{
ProcessOutput prcoutput = new ProcessOutput();
// Rclone output is unicode, else it will show garbage instead of unicode characters
rclone.StartInfo.StandardOutputEncoding = Encoding.UTF8;
// set configpath if there is any
if (uploadConfigPath.Length > 0)
{
command += $" --config {uploadConfigPath}";
}
string logcmd = Utilities.StringUtilities.RemoveEverythingBeforeFirst(command, "rclone.exe");
if (logcmd.Contains($"\"{Properties.Settings.Default.CurrentLogPath}\""))
{
logcmd = logcmd.Replace($"\"{Properties.Settings.Default.CurrentLogPath}\"", $"\"{Properties.Settings.Default.CurrentLogName}\"");
}
if (logcmd.Contains(Environment.CurrentDirectory))
{
logcmd = logcmd.Replace($"{Environment.CurrentDirectory}", $"CurrentDirectory");
}
_ = Logger.Log($"Running Rclone command: {logcmd}");
command += " --checkers 0 --no-check-dest --retries 1";
rclone.StartInfo.FileName = Environment.CurrentDirectory + "\\rclone\\rclone.exe";
rclone.StartInfo.Arguments = command;
rclone.StartInfo.RedirectStandardInput = true;
rclone.StartInfo.RedirectStandardError = true;
rclone.StartInfo.RedirectStandardOutput = true;
rclone.StartInfo.WorkingDirectory = Environment.CurrentDirectory + "\\rclone";
rclone.StartInfo.CreateNoWindow = true;
// Display RCLONE Window if the binary is being run in Debug Mode.
if (MainForm.debugMode)
{
rclone.StartInfo.CreateNoWindow = false;
}
rclone.StartInfo.UseShellExecute = false;
_ = rclone.Start();
rclone.StandardInput.WriteLine(command);
rclone.StandardInput.Flush();
rclone.StandardInput.Close();
string output = rclone.StandardOutput.ReadToEnd();
string error = rclone.StandardError.ReadToEnd();
rclone.WaitForExit();
// if there is one of these errors, we switch the mirrors
if (error.Contains("400 Bad Request") || error.Contains("cannot fetch token") || error.Contains("authError") || error.Contains("quota") || error.Contains("exceeded") || error.Contains("directory not found") || error.Contains("Failed to"))
{
_ = Logger.Log(error, LogLevel.ERROR);
return new ProcessOutput("Upload Failed.", "Upload failed.");
}
else
{
prcoutput.Output = output;
prcoutput.Error = error;
}
if (!output.Contains("Game Name;Release Name;") && !output.Contains("package:") && !output.Contains(".meta"))
{
if (!string.IsNullOrWhiteSpace(error))
{
_ = Logger.Log($"Rclone error: {error}\n", LogLevel.ERROR);
}
if (!string.IsNullOrWhiteSpace(output))
{
_ = Logger.Log($"Rclone Output: {output}");
}
}
return prcoutput;
}
public static ProcessOutput runRcloneCommand_PublicConfig(string command)
{
if (MainForm.isOffline)
{
return new ProcessOutput("", "No internet");
}
ProcessOutput prcoutput = new ProcessOutput();
// Rclone output is unicode, else it will show garbage instead of unicode characters
rclone.StartInfo.StandardOutputEncoding = Encoding.UTF8;
string logcmd = Utilities.StringUtilities.RemoveEverythingBeforeFirst(command, "rclone.exe");
if (logcmd.Contains($"\"{Properties.Settings.Default.CurrentLogPath}\""))
{
logcmd = logcmd.Replace($"\"{Properties.Settings.Default.CurrentLogPath}\"", $"\"{Properties.Settings.Default.CurrentLogName}\"");
}
if (logcmd.Contains(Environment.CurrentDirectory))
{
logcmd = logcmd.Replace($"{Environment.CurrentDirectory}", $"CurrentDirectory");
}
_ = Logger.Log($"Running Rclone command: {logcmd}");
//set http source & args
command += $" --http-url {MainForm.PublicConfigFile.BaseUri} {MainForm.PublicMirrorExtraArgs}";
rclone.StartInfo.FileName = Environment.CurrentDirectory + "\\rclone\\rclone.exe";
rclone.StartInfo.Arguments = command;
rclone.StartInfo.RedirectStandardInput = true;
rclone.StartInfo.RedirectStandardError = true;
rclone.StartInfo.RedirectStandardOutput = true;
rclone.StartInfo.WorkingDirectory = Environment.CurrentDirectory + "\\rclone";
rclone.StartInfo.CreateNoWindow = true;
// Display RCLONE Window if the binary is being run in Debug Mode.
if (MainForm.debugMode)
{
rclone.StartInfo.CreateNoWindow = false;
}
rclone.StartInfo.UseShellExecute = false;
_ = rclone.Start();
rclone.StandardInput.WriteLine(command);
rclone.StandardInput.Flush();
rclone.StandardInput.Close();
string output = rclone.StandardOutput.ReadToEnd();
string error = rclone.StandardError.ReadToEnd();
rclone.WaitForExit();
if (error.Contains("There is not enough space"))
{
_ = FlexibleMessageBox.Show(Program.form, $"There isn't enough disk space to download this game.\r\nPlease ensure you have at least 2x the game size available in {Environment.CurrentDirectory} and try again.",
"NOT ENOUGH SPACE",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return new ProcessOutput("Download failed.", string.Empty);
}
if (error.Contains("Only one usage of each socket address (protocol/network address/port) is normally permitted")) {
_ = Logger.Log(error, LogLevel.ERROR);
return new ProcessOutput("Failed to fetch from public mirror.", "Failed to fetch from public mirror.\nYou may have a running RCLONE Task!\nCheck your Task Manager, Sort by Network Usage, and kill the process Rsync for Cloud Storage/Rclone");
}
else if (error.Contains("400 Bad Request")
|| error.Contains("cannot fetch token")
|| error.Contains("authError")
|| error.Contains("quota")
|| error.Contains("exceeded")
|| error.Contains("directory not found")
|| error.Contains("Failed to"))
{
_ = Logger.Log(error, LogLevel.ERROR);
return new ProcessOutput("Failed to fetch from public mirror.", "Failed to fetch from public mirror.");
}
else
{
prcoutput.Output = output;
prcoutput.Error = error;
}
if (!output.Contains("Game Name;Release Name;") && !output.Contains("package:") && !output.Contains(".meta"))
{
if (!string.IsNullOrWhiteSpace(error))
{
_ = Logger.Log($"Rclone error: {error}\n", LogLevel.ERROR);
}
if (!string.IsNullOrWhiteSpace(output))
{
_ = Logger.Log($"Rclone Output: {output}");
}
}
return prcoutput;
}
}
}