This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
MainWindow.xaml.cs
276 lines (246 loc) · 12 KB
/
MainWindow.xaml.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
using System;
using System.IO;
using Steamworks;
using System.Windows;
using Inferno_Mod_Manager.Controller;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Controls;
using Inferno_Mod_Manager.Utils;
using Microsoft.Win32;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Media;
using System.Security;
using Inferno_Mod_Manager.MelonMods;
namespace Inferno_Mod_Manager
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static MainWindow Instance;
public MainWindow()
{
WindowStyle = WindowStyle.None;
AllowsTransparency = true;
InitializeComponent();
}
private void Window_Initialized(object sender, EventArgs e)
{
Instance = this;
WebDownloader.IfBlankSet();
WebDownloader.GetAllData();
SetupRepo();
RefreshModList();
RefreshDownloadsList();
RefreshAchievements();
}
public Mod MakeNewMod(string path, bool enabled, string name = "") => new() {
Name = name.Equals("") ? Path.GetFileNameWithoutExtension(path) : name,
Type = Path.GetExtension(path),
CanonicalLocation = path,
Enabled = enabled
};
public void ChangeExistingMod(Mod mod, string path, bool enabled) {
mod.CanonicalLocation = path;
mod.Enabled = enabled;
}
public void RefreshModList() {
stackPanel.Children.Clear();
var modFiles = Storage.GetModFiles(Storage.InstallDir + @"\Mods", Storage.InstallDir + @"\Mods\Inferno");
for (var i = 0; i < modFiles.Length; i++) {
Mod mod;
var fvi = FileVersionInfo.GetVersionInfo(modFiles[i]);
if (fvi == null || fvi.OriginalFilename == null)
mod = MakeNewMod(modFiles[i], true);
else {
mod = ModManifest.Instance ^ Path.GetFileNameWithoutExtension(modFiles[i]);
if (mod != ModManifest.TemplateMod)
ChangeExistingMod(mod, modFiles[i], true);
else {
mod = ModManifest.Instance ^ fvi.OriginalFilename.Split('.')[0];
if (mod != ModManifest.TemplateMod)
ChangeExistingMod(mod, modFiles[i], true);
else
mod = MakeNewMod(modFiles[i], true);
}
}
stackPanel.Children.Add(new ModPanel(mod));
}
var disableModFiles = Storage.GetModFiles(Storage.InstallDir + @"\Mods\Disabled", Storage.InstallDir + @"\Mods\Inferno\Disabled");
for (var i = 0; i < disableModFiles.Length; i++) {
Mod disabledMod;
var fvi = FileVersionInfo.GetVersionInfo(disableModFiles[i]);
if (fvi == null || fvi.OriginalFilename == null)
disabledMod = MakeNewMod(disableModFiles[i], false);
else {
disabledMod = ModManifest.Instance ^ Path.GetFileNameWithoutExtension(disableModFiles[i]);
if (disabledMod != ModManifest.TemplateMod)
ChangeExistingMod(disabledMod, disableModFiles[i], false);
else {
disabledMod = ModManifest.Instance ^ fvi.OriginalFilename.Split('.')[0];
if (disabledMod != ModManifest.TemplateMod)
ChangeExistingMod(disabledMod, disableModFiles[i], false);
else
disabledMod = MakeNewMod(disableModFiles[i], false);
}
}
stackPanel.Children.Add(new ModPanel(disabledMod));
}
}
private void ParseDownloadsList() {
WebDownloader.IfBlankSet();
foreach (var a in WebDownloader.GetAllData()) {
var aa = JsonConvert.DeserializeObject<List<Mod>>(a, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
foreach (var aaa in aa)
if (aaa != null && !Storage.ModsList.Contains(aaa))
Storage.ModsList.Add(aaa);
}
}
public void RefreshDownloadsList()
{
stackPanelDownload.Children.Clear();
try {
ParseDownloadsList();
} catch (Exception e) {
MessageBox.Show($"Error! {e.GetType().FullName}", "Clearing cached repos");
WebDownloader.Repos = new();
ParseDownloadsList();
}
for (var i = 0; i < Storage.ModsList.Count; i++)
{
var modData = Storage.ModsList[i];
if ((ModManifest.Instance ^ modData.Name) == ModManifest.TemplateMod)
stackPanelDownload.Children.Add(new DownloadPanel(modData));
}
}
public void RefreshAchievements() {
achievementsTreeView.Items.Clear();
foreach (var achievement in SteamUserStats.Achievements)
{
var ap = new AchievementPanel() { ToolTip = "???" };
ap.__As_0x32459 = achievement;
ap.achName = achievement.Name;
if (achievement.GetIcon().HasValue)
try
{
var img = achievement.GetIcon().Value;
var rgba = new RGBASource(img.Data, checked((int)img.Width));
if (!achievement.State)
for (var i = 0; i < img.Data.Length; i += 4)
{
var gray = (byte)((img.Data[i] + img.Data[i + 1] + img.Data[i + 2]) / 3);
img.Data[i] = gray;
img.Data[i + 1] = gray;
img.Data[i + 2] = gray;
}
ap.achievementName.Text = new StringBuilder(achievement.Name).ToString();
ap.achievementName.Foreground = new SolidColorBrush(Colors.White);
ap.ToolTip = new ToolTip() { Content = achievement.Description };
ap.achievementImage.Source = rgba;
achievementsTreeView.Items.Add(ap);
continue;
}
catch (Exception) {achievementsTreeView.Items.Add(ap);}
ap.achievementName.Text = achievement.Name;
ap.achievementName.Foreground = new SolidColorBrush(Colors.White);
ap.ToolTip = new ToolTip() { Content = achievement.Description };
achievementsTreeView.Items.Add(ap);
}
achievementsTreeView.Items.SortDescriptions.Add(new("achName", ListSortDirection.Ascending));
}
private void SetupRepo()
{
if (!File.Exists(Storage.repo))
{
File.Create(Storage.repo).Close();
File.WriteAllText(Storage.repo, JsonConvert.SerializeObject(WebDownloader.Repos));
}
else {WebDownloader.Repos = JsonConvert.DeserializeObject<List<string>>(File.ReadAllText(Storage.repo));}
}
public static void SetupSettings()
{
if (!File.Exists(Storage.usr))
{
if (!Directory.Exists(Storage.dir))
{
Directory.CreateDirectory(Storage.dir);
}
File.Create(Storage.usr).Close();
File.WriteAllText(Storage.usr, JsonConvert.SerializeObject(Storage.Settings));
}
else {Storage.Settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(Storage.usr));}
}
private void Window_Closing(object sender, CancelEventArgs e) => SteamClient.Shutdown();
private void Add_Repo_Button_Click(object sender, RoutedEventArgs e) {
try {
var inputDialog = new OneAnswerInputDialogWindow("Please enter a new Repo:");
if (inputDialog.ShowDialog() == true) {
WebDownloader.Repos.Add(inputDialog.Answer);
} else {
MessageBox.Show("Repo Addition Cancelled!");
RefreshModList();
return;
}
File.WriteAllText(Storage.repo, JsonConvert.SerializeObject(WebDownloader.Repos));
RefreshDownloadsList();
}
catch (Exception a) {MessageBox.Show("Couldn't Add the repo!\n" + a.Message, "ERROR!");}
}
private void Add_Mod_Button_Click(object sender, RoutedEventArgs e)
{
string Name, Author, Description, Tags, PNGUrl, Version;
try
{
var inputDialogName = new InputDialogSample("Please enter the Name of the mod:", "Please enter the Description of the mod:", "Please enter the Author of the mod:", "Please enter the Tags of the mod:", "Please enter the Version number of the mod:", "Please enter the URL for the image of the mod (If there is not one leave blank):");
if (inputDialogName.ShowDialog() ?? true)
{
Name = inputDialogName.Answer;
Description = inputDialogName.Answer2;
Author = inputDialogName.Answer3;
Tags = inputDialogName.Answer4;
Version = inputDialogName.Answer5;
PNGUrl = inputDialogName.Answer6;
} else {
MessageBox.Show("Mod Addition Cancelled!");
return;
}
var file = BrowseForFile("Please select the mod to import!");
if (Path.GetExtension(file).Contains("dll")) {
MelonHandler.GetMelonAttrib(file, out var att);
if (att != null)
if ((ModManifest.Instance ^ att.Name) != ModManifest.TemplateMod || (ModManifest.Instance ^ Path.GetFileName(file)) != ModManifest.TemplateMod)
throw new("Mod already exists!");
}
var dd = new Mod { Name = Name, Author = Author, Description = Description, Type = Path.GetExtension(file), DownloadUrl = "null.com", PNGUrl = PNGUrl, Tags = Tags, Version = Version };
File.Copy(file, Storage.InstallDir + @"\Mods\" + Name + Path.GetExtension(file));
ModManifest.Instance += dd;
RefreshModList();
}
catch (Exception a) { MessageBox.Show("Couldn't Add the mod!\n" + a.Message, "ERROR!"); }
}
public static string BrowseForFile(string title)
{
var fileDiag = new OpenFileDialog
{
Title = title,
DefaultExt = "dll",
Filter = "Dynamic Link Library(*.dll)|*.dll|Inferno API Mods|*.inferno",
Multiselect = false
};
if (fileDiag.ShowDialog() ?? true)
return fileDiag.FileName;
else
return "";
}
private void LaunchGame_Click(object sender, RoutedEventArgs e) => ProcessHelpers.RunWithRPC(new(Storage.InstallDir + @"\BloonsTD6.exe"), App.btd6ModdedRP);
private void LaunchGameNoMods_Click(object sender, RoutedEventArgs e) => ProcessHelpers.RunWithRPC(new(Storage.InstallDir + @"\BloonsTD6.exe", "--no-mods"), App.btd6RP);
private void Window_Loaded(object sender, RoutedEventArgs e) => InfernoChecker.PI_as_0x000003(InfernoChecker.PI_as_0x000002(InfernoChecker.PI_as_0x000001()));
private void DiscordButton_Click(object sender, RoutedEventArgs e) => Process.Start("https://discord.gg/D7v6h3KSQN");
}
}