Skip to content

Commit

Permalink
Allow Unit Cap Adjustment
Browse files Browse the repository at this point in the history
- Creates new setting in MelonPreferences.cfg for UnitCap (default: 0 - disable unit caps)
  • Loading branch information
data-bomb committed Feb 13, 2024
1 parent 0f601aa commit 1f42d00
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions Si_NoUnitLimits/Si_NoUnitLimits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,59 @@ You should have received a copy of the GNU General Public License
using Si_NoUnitLimits;
using SilicaAdminMod;
using System;
using Newtonsoft.Json;

[assembly: MelonInfo(typeof(NoUnitLimits), "No Unit Limits", "1.0.0", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(NoUnitLimits), "No Unit Limits", "1.1.0", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonOptionalDependencies("Admin Mod")]

namespace Si_NoUnitLimits
{
public class NoUnitLimits : MelonMod
{
static MelonPreferences_Category _modCategory = null!;
static MelonPreferences_Entry<int> _UnitCap = null!;

public override void OnInitializeMelon()
{
_modCategory = MelonPreferences.CreateCategory("Silica");
_UnitCap = _modCategory.CreateEntry<int>("UnitCap", 0);
}

[HarmonyPatch(typeof(MP_Strategy), nameof(MP_Strategy.UpdateUnitCapMultFromSetting))]
private static class ApplyPatch_Strategy_UpdateUnitCapMultFromSetting
{
static void Postfix(MP_Strategy __instance)
{
try
{
if (__instance == null)
if (__instance == null || _UnitCap == null)
{
return;
}

#if NET6_0
__instance.UnitCapMultiplier = 0;
int unitCap = _UnitCap.Value;
if (unitCap < 0)
{
unitCap = 0;
}

#if NET6_0
__instance.UnitCapMultiplier = unitCap;
#else
PropertyInfo unitCapProperty = typeof(MP_Strategy).GetProperty("UnitCapMultiplier", BindingFlags.NonPublic | BindingFlags.Instance);
unitCapProperty.SetValue(__instance, (int)0);
unitCapProperty.SetValue(__instance, (int)unitCap);
#endif

MelonLogger.Msg("Setting unit cap to unlimited.");
if (unitCap == 0)
{
MelonLogger.Msg("Setting unit cap to unlimited.");
}
else
{
MelonLogger.Msg("Setting unit cap to: " + unitCap);
}

}
catch (Exception error)
{
Expand Down

0 comments on commit 1f42d00

Please sign in to comment.