|
1 | | -using BTD_Mod_Helper.Extensions; |
| 1 | +using BTD_Mod_Helper.Api; |
| 2 | +using BTD_Mod_Helper.Extensions; |
2 | 3 | using CommandLine; |
3 | 4 | using HarmonyLib; |
4 | 5 | using Il2CppAssets.Scripts.Models; |
5 | 6 | using Il2CppAssets.Scripts.Models.Bloons; |
6 | 7 | using Il2CppAssets.Scripts.Models.Bloons; |
7 | 8 | using Il2CppAssets.Scripts.Unity; |
| 9 | +using Il2CppAssets.Scripts.Unity.UI_New.InGame; |
8 | 10 | using Il2CppAssets.Scripts.Unity.UI_New.InGame.BloonMenu; |
| 11 | +using Il2CppSystem.Collections.Generic; |
9 | 12 | using MelonLoader; |
10 | 13 | using System.Linq; |
| 14 | +using UnityEngine; |
| 15 | +using UnityEngine.UI; |
11 | 16 |
|
12 | 17 | namespace BloonFactory.Patches |
13 | 18 | { |
14 | | - [HarmonyPatch(typeof(BloonMenu), nameof(BloonMenu.CreateBloonButtons))] |
15 | | - public static class BloonMenu_CreateBloonButtons_Patch |
16 | | - { |
17 | | - [HarmonyPrefix] |
18 | | - public static void Prefix(BloonMenu __instance, Il2CppSystem.Collections.Generic.List<BloonModel> sortedBloons) |
19 | | - { |
20 | | - foreach (var bloon in CustomBloon.Bloons) |
21 | | - { |
22 | | - if (!sortedBloons.Any(a => a.id == bloon.BloonTemplate.TemplateId) && !bloon.BloonTemplate.IsQueueForDeletion) |
23 | | - { |
24 | | - sortedBloons.Add(Game.instance.model.GetBloon(bloon.BloonTemplate.TemplateId)); |
25 | | - } |
26 | | - } |
27 | | - } |
28 | | - } |
29 | | - |
30 | 19 | [HarmonyPatch(typeof(GameModel), nameof(GameModel.GetBloon))] |
31 | 20 | internal static class GameModel_GetBloon |
32 | 21 | { |
33 | 22 | [HarmonyPostfix] |
34 | 23 | private static bool Prefix(GameModel __instance, string id, ref BloonModel __result) |
35 | 24 | { |
36 | | - if (__instance.bloonsByName == null || __instance.bloonsByName.ContainsKey(id)) |
| 25 | + if (__instance.bloons == null || __instance.bloons.Any(a => a.id == id)) |
37 | 26 | return true; |
38 | 27 |
|
39 | 28 | string newId = id.Replace("Regrow", "").Replace("Camo", "").Replace("Fortified", ""); |
40 | | - if (__instance.bloonsByName.TryGetValue(newId, out var value)) |
| 29 | + var bloon = __instance.bloons.FirstOrDefault(a => a.id == newId); |
| 30 | + if (bloon != null) |
| 31 | + { |
| 32 | + __result = bloon; |
| 33 | + if (__result == null) |
| 34 | + MelonLogger.Msg("its null tho"); |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + if (__instance.bloons.Any(b => b.id.Contains(id))) |
41 | 39 | { |
42 | | - __result = value; |
| 40 | + __result = __instance.bloons.First(b => b.id.Contains(id)); |
43 | 41 | return false; |
44 | 42 | } |
45 | 43 |
|
46 | 44 | return true; |
47 | 45 | } |
48 | 46 | } |
| 47 | + [HarmonyPatch(typeof(BloonMenu), nameof(BloonMenu.CreateBloonButtons))] |
| 48 | + public static class BloonMenu_CreateBloonButtons_Patch |
| 49 | + { |
| 50 | + const string name = "BLOONFACTORYBLOONSPAWNER"; |
| 51 | + |
| 52 | + [HarmonyPrefix] |
| 53 | + private static void Prefix(BloonMenu __instance, ref List<BloonModel> sortedBloons) |
| 54 | + { |
| 55 | + sortedBloons = sortedBloons.Where(b => !CustomBloon.Bloons.Any(a => a.BloonTemplate.TemplateId == b.id)); |
| 56 | + } |
| 57 | + |
| 58 | + [HarmonyPostfix] |
| 59 | + private static void Postfix(BloonMenu __instance) |
| 60 | + { |
| 61 | + foreach (GameObject child in __instance.bloonButtonContainer.GetChildren()) |
| 62 | + { |
| 63 | + if (child.name == name) |
| 64 | + { |
| 65 | + GameObject.Destroy(child); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + foreach (var bloon in CustomBloon.Bloons) |
| 70 | + { |
| 71 | + var obj = GameObject.Instantiate(__instance.spawnBloonButtonPrefab, __instance.bloonButtonContainer.transform); |
| 72 | + obj.name = name; |
| 73 | + obj.RemoveComponent<SpawnBloonButton>(); |
| 74 | + |
| 75 | + BloonModel model = InGame.instance.bridge.Model.GetBloon(bloon.BloonTemplate.TemplateId); |
| 76 | + |
| 77 | + obj.transform.FindChild("Icon").GetComponent<Image>().SetSprite(model.icon); |
| 78 | + |
| 79 | + obj.GetComponent<Button>().AddOnClick(() => |
| 80 | + { |
| 81 | + int spacing = int.Parse(__instance.bloonRate.text); |
| 82 | + int count = int.Parse(__instance.bloonCount.text); |
| 83 | + InGame.instance.bridge.SpawnBloons(InGame.instance.bridge.Model.CreateBloonEmissions(model, count, spacing * 5), 1, 0); |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
49 | 88 | } |
0 commit comments