-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBTD6Retargeter.cs
More file actions
98 lines (87 loc) · 3.1 KB
/
BTD6Retargeter.cs
File metadata and controls
98 lines (87 loc) · 3.1 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
using System;
using System.IO;
using System.Linq;
using MelonLoader;
using MelonLoader.Utils;
namespace BTD6EpicGamesModCompat;
internal static class Btd6Retargeter
{
private static MelonAssembly GetAssembly(string fileName)
{
try
{
return MelonAssembly.LoadMelonAssembly(fileName, false);
}
catch (Exception e)
{
Plugin.Logger.Error(e);
return null;
}
}
private static int GetPriority(MelonAssembly assembly)
{
try
{
return MelonUtils.PullAttributeFromAssembly<MelonPriorityAttribute>(assembly.Assembly)?.Priority ?? 0;
}
catch (Exception e)
{
Plugin.Logger.Error(e);
return 0;
}
}
public static void Retarget()
{
Plugin.Logger.WriteSpacer();
Plugin.Logger.Msg("Loading mods from " + MelonEnvironment.ModsDirectory + "...");
Plugin.Logger.Msg(ConsoleColor.Magenta, "------------------------------");
Plugin.Logger.WriteSpacer();
Plugin.Logger.Msg("Retargeting mods...");
Plugin.Logger.Msg(ConsoleColor.Magenta, "------------------------------");
var assemblies = from modFile in Directory.GetFiles(MelonEnvironment.ModsDirectory)
select !Path.HasExtension(modFile) || !Path.GetExtension(modFile).Equals(".dll")
? null
: GetAssembly(modFile)
into melon
where melon != null
orderby GetPriority(melon)
select melon;
foreach (var assembly in assemblies)
{
try
{
assembly.LoadMelons();
foreach (var melon in assembly.LoadedMelons.Where(melon => melon is not null))
{
var alreadyHasEpicGamesInAttribute = false;
var steamIndex = -1;
for (int i = 0; i < melon.Games.Length; i++)
{
var game = melon.Games[i];
if (game.Developer.Equals("Ninja Kiwi") && game.Name.Contains("BloonsTD6"))
{
alreadyHasEpicGamesInAttribute |= game.Name.Contains("-Epic");
if (!alreadyHasEpicGamesInAttribute)
{
steamIndex = i;
break;
}
}
}
if (!alreadyHasEpicGamesInAttribute && steamIndex != -1)
{
melon.Games[steamIndex] = new MelonGameAttribute("Ninja Kiwi", "BloonsTD6-Epic");
Plugin.Logger.Msg(
$"Retargeted [{melon.Info.Name} v{melon.Info.Version} by {melon.Info.Author}] to BloonsTD6-Epic");
}
}
assembly.UnregisterMelons(silent: true);
assembly.LoadMelons();
}
catch (Exception e)
{
Plugin.Logger.Error(e);
}
}
}
}