Skip to content

Commit

Permalink
v31.1 and Hero Screen fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
doombubbles committed Apr 21, 2022
1 parent f6d18f4 commit a4c904e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 12 deletions.
45 changes: 45 additions & 0 deletions BTD Mod Helper Core/Api/MoreAccessTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using MelonLoader;

namespace BTD_Mod_Helper.Api
{
/// <summary>
/// Further methods along the lines of Harmony's <see cref="AccessTools"/>
/// </summary>
public static class MoreAccessTools
{
/// <summary>
/// Safely gets the MethodInfo for a method within a nested class. This is recommended to use because over
/// directly targeting it with typeof and nameof because the nested class names can change randomly.
/// </summary>
/// <param name="outerType">The outer type whose name won't change</param>
/// <param name="nestedTypeName">The name of nested type, not including the _s</param>
/// <param name="methodName">The desired method name within the nested type</param>
/// <param name="index">If multiple nested classes share a name portion, use the one at this index, default 0</param>
/// <returns>The MethodInfo, or null alongside a console warning if one couldn't be found</returns>
public static MethodInfo SafeGetNestedClassMethod(Type outerType, string nestedTypeName, string methodName, int index = 0)
{
var innerTypes = outerType.GetNestedTypes().Where(type => type.Name.Contains($"_{nestedTypeName}_")).ToArray();

if (!innerTypes.Any() || index >= innerTypes.Length || index < 0)
{
MelonLogger.Warning($"Failed to find nested type {nestedTypeName} within {outerType.Name} with index {0}");
return null;
}

var innerType = innerTypes[index];

return AccessTools.Method(innerType, methodName);
}

/// <inheritdoc cref="SafeGetNestedClassMethod"/>
public static bool TryGetNestedClassMethod(Type outerType, string nestedClassName, string methodName, out MethodInfo method, int index = 0)
{
method = SafeGetNestedClassMethod(outerType, nestedClassName, methodName, index);
return method != null;
}
}
}
17 changes: 17 additions & 0 deletions BTD Mod Helper Core/Api/Towers/ModHero.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,27 @@ internal override string TowerId(int[] tiers)
/// The other hero that has the same colored name in the Heroes menu as you want to use
/// </summary>
public virtual string NameStyle => TowerType.Ezili;

/// <summary>
/// The other hero that has the same glow color in the Heroes menu as you want to use
/// </summary>
public virtual string GlowStyle => TowerType.Ezili;

/// <summary>
/// The other hero that has the same background color in the Heroes menu as you want to use
/// </summary>
public virtual string BackgroundStyle => TowerType.Ezili;

public virtual string Button => GetType().Name + "-Button";

public virtual SpriteReference ButtonReference => GetSpriteReference(Button);

/// <summary>
/// The name of the png to try to find for the new hero select screen button
/// </summary>
public virtual string Square => GetType().Name + "-Square";

public virtual SpriteReference SquareReference => GetSpriteReference(Square);

/// <summary>
/// The total number of levels this hero has. Do not set this to anything other than number of ModHeroLevels
Expand Down
2 changes: 1 addition & 1 deletion BTD Mod Helper Core/Api/Towers/ModTowerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ internal static void FinalizeHero(ModHero modHero)
new HeroDetailsModel(modHero.Id, index, 20, 1, 0, 0, 0, null, false);
Game.instance.model.AddHeroDetails(heroDetailsModel, index);

var skinModel = new HeroSkinModel(modHero.Id, modHero.ButtonReference, modHero.ButtonReference,
var skinModel = new HeroSkinModel(modHero.Id, modHero.ButtonReference, modHero.SquareReference,
modHero.Id, modHero.Id + " Short Description", modHero.Id + " Description", 0, true,
new Il2CppReferenceArray<SwapTowerSpriteModel>(0),
new Il2CppReferenceArray<SwapTowerGraphicModel>(0),
Expand Down
1 change: 1 addition & 0 deletions BTD Mod Helper Core/BTD Mod Helper Core.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Api\Guard.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\Helpers\ActionHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\ModContent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\MoreAccessTools.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\Towers\ModHero.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\Towers\ModHeroLevel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Api\Towers\ModVanillaContent.cs" />
Expand Down
2 changes: 1 addition & 1 deletion BTD Mod Helper Core/ModHelperData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public static class ModHelperData
{
public const string currentVersion = "2.4.7";
public const string currentVersion = "2.4.8";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using Assets.Scripts.Unity.UI_New.GameOver;
using Assets.Scripts.Unity.UI_New.InGame;
using Assets.Scripts.Utils;
using BTD_Mod_Helper.Api;
using HarmonyLib;
using MelonLoader;

namespace BTD_Mod_Helper.Patches.ModdedClientChecking
{
Expand All @@ -24,16 +26,16 @@ private static IEnumerable<MethodBase> TargetMethods()
yield return AccessTools.Method(typeof(InGame), nameof(InGame.OnVictory));
yield return AccessTools.Method(typeof(OnlineProfileUpdater), nameof(OnlineProfileUpdater.LateUpdate));

yield return AccessTools.Method(typeof(OnlineProfileManager._Upload_d__13),
nameof(OnlineProfileManager._Upload_d__13.MoveNext));
yield return AccessTools.Method(typeof(Btd6Player._LoadOnlineData_d__38),
nameof(Btd6Player._LoadOnlineData_d__38.MoveNext));
yield return AccessTools.Method(typeof(BossVictoryScreen._Open_d__24),
nameof(BossVictoryScreen._Open_d__24.MoveNext));
yield return AccessTools.Method(typeof(BossEventScreenPlayPanel._Open_d__24),
nameof(BossEventScreenPlayPanel._Open_d__24.MoveNext));
yield return AccessTools.Method(typeof(BossEventScreen._Open_d__43),
nameof(BossEventScreen._Open_d__43.MoveNext));
if (MoreAccessTools.TryGetNestedClassMethod(typeof(OnlineProfileManager), "Upload", "MoveNext", out var m))
yield return m;
if (MoreAccessTools.TryGetNestedClassMethod(typeof(Btd6Player), "LoadOnlineData", "MoveNext", out m))
yield return m;
if (MoreAccessTools.TryGetNestedClassMethod(typeof(BossVictoryScreen), "Open", "MoveNext", out m))
yield return m;
if (MoreAccessTools.TryGetNestedClassMethod(typeof(BossEventScreenPlayPanel), "Open", "MoveNext", out m))
yield return m;
if (MoreAccessTools.TryGetNestedClassMethod(typeof(BossEventScreen), "Open", "MoveNext", out m))
yield return m;
}

[HarmonyPrefix]
Expand Down
21 changes: 21 additions & 0 deletions BloonsTD6 Mod Helper/Patches/UI/TitleScreen_Start.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using Assets.Main.Scenes;
using Assets.Scripts.Data;
using Assets.Scripts.Data.Global;
using Assets.Scripts.Models.TowerSets.Mods;
using Assets.Scripts.Unity;
using BTD_Mod_Helper.Api;
Expand Down Expand Up @@ -53,6 +55,25 @@ internal static void Postfix()
}
}

foreach (var modHero in ModContent.GetContent<ModHero>())
{
try
{
var heroSprites = GameData.Instance.heroSprites;
heroSprites.heroSprite.Add(new HeroSprite
{
heroId = modHero.Id,
heroFontMaterial = heroSprites.GetFontMaterialRef(modHero.NameStyle),
backgroundBanner = heroSprites.GetBannerRef(modHero.GlowStyle),
backgroundColourTintOverride = heroSprites.GetBannerColourTintRef(modHero.BackgroundStyle)
});
}
catch (Exception e)
{
MelonLogger.Error(e);
}
}

}
}
}

0 comments on commit a4c904e

Please sign in to comment.