-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manager.cs
281 lines (258 loc) · 7.97 KB
/
Manager.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
using System.Collections.Generic;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace FlaxPlugMan;
public class Manager
{
private const string
ListUrl = "https://raw.githubusercontent.com/Crawcik/FlaxPluginManager/master/plugin_list.json",
ModuleDependency = "options.PrivateDependencies.Add(\"{0}\");";
private CancellationTokenSource _cancelToken;
public IReadOnlyList<PluginEntry> Plugins { get; private set; }
public string ProjectPath { get; private set; }
public bool IsProcessActive => _cancelToken is not null;
public event Action OnDownloadStarted, OnDownloadFinished;
public event Action<double> UpdateProgress;
public static Process StartGitProcess(string args, string path = "", bool stdout = false) => Process.Start(new ProcessStartInfo()
{
FileName = "git",
UseShellExecute = false,
WorkingDirectory = path,
RedirectStandardOutput = stdout,
Arguments = args
});
public async Task GetPluginList()
{
using var client = new HttpClient();
try
{
#if DEBUG
var result = await File.ReadAllTextAsync("plugin_list.json");
#else
var result = await client.GetStringAsync(ListUrl);
#endif
Plugins = JsonConvert.DeserializeObject<List<PluginEntry>>(result);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public async Task<bool> SetProject(string path)
{
ProjectPath = path;
foreach(var item in Plugins)
item.Installed = false;
try
{
var root = JObject.Parse(await File.ReadAllTextAsync(path));
foreach(var obj in root["References"])
{
var name = (string)obj["Name"];
var plugin = Plugins.FirstOrDefault(x => name.Contains(x.ProjectFile));
if(plugin is null)
continue;
plugin.CheckUi.IsChecked = true;
plugin.Installed = true;
plugin.SetPath(path, name);
var update = await IsUpdateNeeded(plugin);
plugin.UpdateUi.IsVisible = update;
plugin.TagUi.IsVisible = !update;
}
}
catch
{
await MessageBox.Show(null, "Error", "Project file is invalid!");
ProjectPath = null;
return false;
}
return true;
}
public void CancelAll() => _cancelToken?.Cancel();
public async Task DownloadAll(bool gitChecked)
{
OnDownloadStarted?.Invoke();
_cancelToken = new CancellationTokenSource();
var fileInfo = new FileInfo(ProjectPath);
var allSuccess = true;
try
{
// Download files
Download downloader = gitChecked ? new GitDownload() : new DirectDownload();
downloader.SetProgressDelegate(SetProgress);
var dirInfo = Directory.CreateDirectory(Path.Combine(fileInfo.DirectoryName, "Plugins"));
var lookup = Plugins.Where(x => x.CheckUi.IsChecked != x.Installed).ToLookup(x => x.CheckUi.IsChecked ?? false);
foreach (var item in lookup[true])
item.IsGitManaged = gitChecked;
allSuccess = await downloader.ProcessAll(lookup, dirInfo.FullName, _cancelToken.Token);
foreach (var item in lookup[false])
{
item.UpdateUi.IsVisible = false;
item.TagUi.IsVisible = true;
}
// Update project
var gameTarget = await UpdateFlaxProject();
await UpdateGameModules(gameTarget, fileInfo);
await MessageBox.Show(null, "Info", "Success!");
}
catch
{
await MessageBox.Show(null, "Error", "Updating project files failed! Check if they're valid");
}
if(!allSuccess)
await MessageBox.Show(null, "Error", "Some plugins failed to install");
_cancelToken = null;
OnDownloadFinished?.Invoke();
}
public async Task UpdatePlugin(PluginEntry item)
{
if(ProjectPath is null || !item.Installed || !item.IsGitManaged.HasValue)
return;
_cancelToken = new CancellationTokenSource();
item.UpdateStyle(true);
OnDownloadStarted.Invoke();
try
{
Download download = item.IsGitManaged.Value ? new GitDownload() : new DirectDownload();
download.SetProgressDelegate(SetProgress);
var success = await download.Update(item, _cancelToken.Token);
await MessageBox.Show(null, success ? "Info" : "Error", success ? "Success!" : "Updating failed!");
item.UpdateUi.IsVisible = !success;
item.TagUi.IsVisible = success;
}
catch(Exception exp)
{
Console.WriteLine(exp);
}
item.UpdateStyle(false);
OnDownloadFinished.Invoke();
_cancelToken = null;
}
public void SetProgress(double progress) => UpdateProgress?.Invoke(progress * 100);
private async Task<string> UpdateFlaxProject()
{
var root = JObject.Parse(await File.ReadAllTextAsync(ProjectPath));
var array = (JArray)root["References"];
array = new JArray(array.Where(x => !Plugins.Any(y => ((string)x["Name"]).Contains(y.ProjectFile))));
foreach (var item in Plugins.Where(x => x.Installed))
{
if(item.FlaxprojPath is null)
item.SetPath(ProjectPath, "$(ProjectPath)/Plugins/" + item.Name + '/' + item.ProjectFile);
var token = new JObject();
token.Add("Name", item.FlaxprojPath);
array.Add(token);
}
root["References"] = array;
var str = root.ToString(Formatting.Indented);
File.WriteAllText(ProjectPath, str);
return (string)root["GameTarget"];
}
private async Task UpdateGameModules(string gameTarget, FileInfo fileInfo)
{
var path = fileInfo.Directory.ToString();
var target = gameTarget is null ? "Game" : gameTarget.Replace("Target", null);
path = Path.Combine(path, "Source", target, target + ".Build.cs");
if (!File.Exists(path))
return;
using var stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite);
using var reader = new StreamReader(stream);
using var writer = new StreamWriter(stream);
// Read and arrange
var lines = new List<string>();
int startLineNum = 0, lineNum = 0;
var line = string.Empty;
bool platformDepSwitch = false;
while ((line = await reader.ReadLineAsync()) is not null)
{
// Finding function pos
if (startLineNum == 0)
{
if (line.Contains("public override void Setup(BuildOptions options)"))
startLineNum = lineNum + (line.Contains('{') ? 1 : 2);
}
else
{
var con = false;
// Finding old dependencies
if (line.Contains("Do not remove this comment (FlaxPlugMan)"))
platformDepSwitch = true;
if(platformDepSwitch)
{
con = true;
platformDepSwitch = !line.Contains("}");
}
else
{
foreach (var item in Plugins)
{
if (string.IsNullOrEmpty(item.ModuleName) || !line.Contains('"' + item.ModuleName + '"'))
continue;
con = true;
break;
}
}
if (con)
continue;
// Adding new dependencies
if (startLineNum == lineNum)
{
var platformSpecific = new Dictionary<string, List<string>>();
foreach (var item in Plugins.Where(x => x.Installed))
{
if (item.ModuleName is null)
continue;
if (item.Platforms is null)
{
lines.Add("\t\t" + string.Format(ModuleDependency, item.ModuleName));
continue;
}
foreach(var str in item.Platforms)
{
if(!platformSpecific.ContainsKey(str))
platformSpecific.Add(str, new List<string>());
platformSpecific[str].Add(item.ModuleName);
}
}
if (platformSpecific.Count > 0)
{
lines.Add("\t\tswitch (options.Platform.Target) // Do not remove this comment (FlaxPlugMan)");
lines.Add("\t\t{");
foreach (var pair in platformSpecific)
{
lines.Add("\t\t\tcase TargetPlatform." + pair.Key + ":");
foreach(var moduleName in pair.Value)
lines.Add("\t\t\t\t" + string.Format(ModuleDependency, moduleName));
lines.Add("\t\t\t\tbreak;");
}
lines.Add("\t\t}");
}
}
}
lines.Add(line);
lineNum++;
}
// Write to file
stream.Seek(0, SeekOrigin.Begin);
stream.SetLength(0);
foreach (var obj in lines)
await writer.WriteLineAsync(obj);
writer.Close();
}
private async Task<bool> IsUpdateNeeded(PluginEntry item)
{
if(ProjectPath is null || !item.Installed)
return false;
item.IsGitManaged = !File.Exists(item.VersionPath);
try
{
Download download = item.IsGitManaged.Value ? new GitDownload() : new DirectDownload();
return await download.CheckForUpdate(item);
}
catch(Exception exp)
{
Console.WriteLine(exp);
return false;
}
}
}