Skip to content

Commit

Permalink
Nullable enable + crosshair fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Designer225 committed Dec 30, 2023
1 parent bf3bed8 commit 376f123
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion FixedBanditSpawning/D225MiscFixesSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace FixedBanditSpawning
{
static class D225MiscFixesSettingsUtil
{
private static ID225MiscFixesSettings instance;
private static ID225MiscFixesSettings? instance;

private static FileInfo ConfigFile { get; } = new FileInfo(Path.Combine(BasePath.Name, "Modules", "D225.MiscFixes.config.xml"));

Expand Down
4 changes: 3 additions & 1 deletion FixedBanditSpawning/FixedBanditSpawning.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<PlatformTarget>x64</PlatformTarget>
<OutputPath>..\bin\Win64_Shipping_Client\</OutputPath>
<AssemblyTitle>FixedBanditSpawning</AssemblyTitle>
<Product>FixedBanditSpawning</Product>
<Copyright>Copyright © Designer225 2023</Copyright>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Version>1.1.19.105</Version>
<Version>1.1.20.110</Version>
<DebugType>full</DebugType>
<PackageOutputPath>$(OutputPath)</PackageOutputPath>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bannerlord.MCM" Version="5.9.1" />
Expand Down
7 changes: 4 additions & 3 deletions FixedBanditSpawning/LocationCharacterConstructorPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public static void Prefix(AgentData agentData)
{
try
{
AgeModel ageModel = Campaign.Current?.Models?.AgeModel;
var ageModel = Campaign.Current?.Models?.AgeModel;
if (ageModel == default) return;

BasicCharacterObject character = agentData.AgentCharacter;
if (character is CharacterObject)
{
CultureObject culture = (character as CharacterObject).Culture;
var culture = (character as CharacterObject)?.Culture;
if (culture == default) return;

int randMin = agentData.AgentAge;
Expand Down Expand Up @@ -133,7 +133,8 @@ public static void Postfix(ref bool __result)
BasicCharacterObject character = Campaign.Current.ConversationManager.OneToOneConversationAgent.Character;
if (character is CharacterObject)
{
CultureObject culture = (character as CharacterObject).Culture;
var culture = (character as CharacterObject)?.Culture;
if (culture is null) return;
__result = character == culture.TownsmanInfant || character == culture.TownswomanInfant
|| character == culture.TownsmanChild || character == culture.TownswomanChild
|| character == culture.VillagerMaleChild || character == culture.VillagerFemaleChild;
Expand Down
47 changes: 43 additions & 4 deletions FixedBanditSpawning/SubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using TaleWorlds.CampaignSystem.ComponentInterfaces;
using TaleWorlds.CampaignSystem.CampaignBehaviors;
using TaleWorlds.MountAndBlade.View.Tableaus;
using TaleWorlds.MountAndBlade.GauntletUI.Mission;
using TaleWorlds.ScreenSystem;

namespace FixedBanditSpawning
{
Expand Down Expand Up @@ -250,22 +252,59 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
}
}

[HarmonyPatch(typeof(Agent), "OnWeaponReloadPhaseChange")]
static class OnWeaponReloadPhaseChangePatch
[HarmonyPatch(typeof(MissionGauntletCrosshair))]
static class MissionGauntletCrosshairPatches
{
private static MethodInfo? IsMissionScreenUsingCustomCameraMethod;
private static Func<bool>? _methodDelegate;

public static bool Prepare()
{
if (D225MiscFixesSettingsUtil.Instance.FixMachineGunCrosshair)
{
IsMissionScreenUsingCustomCameraMethod = AccessTools.Method(typeof(MissionGauntletCrosshair), "IsMissionScreenUsingCustomCamera");
if (IsMissionScreenUsingCustomCameraMethod is null) return false;
Debug.Print("[FixedBanditSpawning] Fixing crossbow crosshairs");
return true;
}
return false;
}

public static void Prefix(Agent __instance, EquipmentIndex slotIndex, ref short reloadPhase)
[HarmonyPatch("OnCreateView")]
[HarmonyPostfix]
public static void OnCreateViewPostfix(MissionGauntletCrosshair __instance)
{
if (IsMissionScreenUsingCustomCameraMethod is null) return;
_methodDelegate = AccessTools.MethodDelegate<Func<bool>>(IsMissionScreenUsingCustomCameraMethod, __instance);
}

[HarmonyPatch("OnDestroyView")]
[HarmonyPrefix]
public static void OnCreateViewPrefix() => _methodDelegate = default;

[HarmonyPatch("GetShouldCrosshairBeVisible")]
[HarmonyPostfix]
public static void GetShouldCrosshairBeVisiblePostfix(MissionGauntletCrosshair __instance, ref bool __result)
{
if (__instance.Equipment[slotIndex].Ammo > 0) reloadPhase = 2;
if (_methodDelegate is null) return;

if (__instance.Mission.MainAgent == null) return;
var wieldedWeapon = __instance.Mission.MainAgent.WieldedWeapon;
if (BannerlordConfig.DisplayTargetingReticule)
{
switch (__instance.Mission.Mode)
{
case MissionMode.Conversation:
case MissionMode.CutScene:
break;
default:
if (!ScreenManager.GetMouseVisibility() && !wieldedWeapon.IsEmpty
&& wieldedWeapon.CurrentUsageItem.IsRangedWeapon
&& !__instance.MissionScreen.IsViewingCharacter() && !_methodDelegate())
__result = __result || wieldedWeapon.Ammo > 0;
break;
}
}
}
}
}
2 changes: 1 addition & 1 deletion SubModule.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Module>
<Name value="Designer225's Miscellaneous Fixes"/>
<Id value="FixedBanditSpawning"/>
<Version value="v1.1.19.105"/>
<Version value="v1.1.20.110"/>
<SingleplayerModule value="true"/>
<MultiplayerModule value="false"/>
<Official value="false"/>
Expand Down

0 comments on commit 376f123

Please sign in to comment.