Skip to content

Commit

Permalink
Merge pull request #87 from bennyz/merge-updates
Browse files Browse the repository at this point in the history
Integrate fixes
  • Loading branch information
bennyz authored Oct 5, 2022
2 parents 604e8d7 + 6b8e38e commit c421018
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 25 deletions.
1 change: 0 additions & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<!--Harmony Extensions Version-->
<HarmonyExtensionsVersion>3.1.0.61</HarmonyExtensionsVersion>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<Platforms>x64</Platforms>
Expand Down
6 changes: 6 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
---------------------------------------------------------------------------------------------------
Version: 1.1.12
Game Versions: e1.8.1,e1.8.0
* Update dependencies to fix 1.8.0 hotfix crash
* Fix faction selection issue in the kingdom diplomacy view
* Fix messenger teleportation issue
---------------------------------------------------------------------------------------------------
Version: 1.1.11
Game Versions: e1.8.0
* Compatibility with e1.8.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Diplomacy.DiplomaticAction

protected AbstractScoringModel(IDiplomacyScores scores) => Scores = scores;

public virtual ExplainedNumber GetScore(Kingdom ourKingdom, Kingdom otherKingdom, bool includeDesc = false)
public virtual ExplainedNumber GetScore(Kingdom otherKingdom, Kingdom ourKingdom, bool includeDesc = false)
{
var explainedNum = new ExplainedNumber(Scores.Base, includeDesc);

Expand Down
32 changes: 10 additions & 22 deletions src/Bannerlord.Diplomacy/Messengers/MessengerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using TaleWorlds.CampaignSystem.Encounters;
using TaleWorlds.CampaignSystem.GameState;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.Settlements.Locations;
using TaleWorlds.Core;
using TaleWorlds.Library;
Expand All @@ -31,7 +32,7 @@ internal sealed class MessengerManager : IMissionListener
private Messenger? _activeMessenger;
private Mission? _currentMission;

[SaveableField(1)] [UsedImplicitly] private List<Messenger> _messengers;
[SaveableField(1)][UsedImplicitly] private List<Messenger> _messengers;

public MBReadOnlyList<Messenger> Messengers { get; private set; }

Expand Down Expand Up @@ -184,31 +185,18 @@ public void StartDialogue(Hero targetHero, Messenger messenger)
else
targetParty = targetHero.PartyBelongedTo?.Party ?? targetHero.BornSettlement?.Party;

var settlement = targetHero.CurrentSettlement;

PlayerEncounter.Start();
PlayerEncounter.Current.SetupFields(heroParty, targetParty ?? heroParty);

Campaign.Current.CurrentConversationContext = ConversationContext.Default;
if (settlement != null)
{
PlayerEncounter.EnterSettlement();
Location locationOfCharacter = LocationComplex.Current.GetLocationOfCharacter(targetHero);
CampaignEventDispatcher.Instance.OnPlayerStartTalkFromMenu(targetHero);
_currentMission =
(Mission) PlayerEncounter.LocationEncounter.CreateAndOpenMissionController(locationOfCharacter, null, targetHero.CharacterObject);
}
else
{
var specialScene = "";
var sceneLevels = "";

_currentMission = (Mission) Campaign.Current.CampaignMissionManager.OpenConversationMission(
new ConversationCharacterData(Hero.MainHero.CharacterObject, heroParty, true),
new ConversationCharacterData(targetHero.CharacterObject, targetParty, true),
specialScene, sceneLevels);
}

var specialScene = "";
var sceneLevels = "";

// TODO: hack the scene to show the scene of the target instead of the player's
_currentMission = (Mission) Campaign.Current.CampaignMissionManager.OpenConversationMission(
new ConversationCharacterData(Hero.MainHero.CharacterObject, Hero.MainHero.PartyBelongedTo.Party, true),
new ConversationCharacterData(targetHero.CharacterObject, targetHero.PartyBelongedTo?.Party, true),
specialScene, sceneLevels);
_currentMission.AddListener(this);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Bannerlord.Diplomacy/PatchTools/PatchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ private PatchManager(string harmonyId)
new DefaultEncyclopediaFactionPagePatch(),
new RebelKingdomPatches(),
new KingdomManagementVMPatch(),
new MBBannerEditorGauntletScreenPatch()
new MBBannerEditorGauntletScreenPatch(),
new ViewModelPatch()
// ... Only 1 class left to convert to declarative patching.
};
}
Expand Down
60 changes: 60 additions & 0 deletions src/Bannerlord.Diplomacy/Patches/ViewModelPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using TaleWorlds.Library;
using Diplomacy.PatchTools;

using System.Collections.Generic;
using System.Reflection;
using System;
using TaleWorlds.CampaignSystem.ViewModelCollection.KingdomManagement.Diplomacy;

namespace Diplomacy.Patches
{
internal class ViewModelPatch : PatchClass<ViewModelPatch>
{
protected override IEnumerable<Patch> Prepare()
{
return new Patch[]
{
new Prefix(nameof(FixInvoke), new Reflect.Method(typeof(Common), "InvokeWithLog", new Type[]{typeof(MethodInfo), typeof(object), typeof(object[])})),
};
}

// TODO: this is a terrible hack, hopefully I will get assitance with https://github.com/BUTR/Bannerlord.UIExtenderEx/discussions/159
// and fix it properly
private static void FixInvoke(MethodInfo methodInfo, object obj, object[] args)
{
var instance = methodInfo.GetType().GetField("_instance", BindingFlags.NonPublic | BindingFlags.Instance);
if (instance != null && instance.GetValue(methodInfo) is ViewModelMixin.KingdomTruceItemVmMixin)
{
if (!Environment.StackTrace.Contains("OnRefresh"))
{
var MixinInstance = (ViewModelMixin.KingdomTruceItemVmMixin) instance.GetValue(methodInfo);
var BadVm = MixinInstance.GetType().BaseType.GetField("_vm", BindingFlags.Instance | BindingFlags.NonPublic);
// Replace VM with obj
BadVm.SetValue(MixinInstance, new WeakReference<KingdomTruceItemVM>((KingdomTruceItemVM) obj));

// Replace private _factionn reference
var vm = MixinInstance.GetType().GetField("_faction2", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
vm.SetValue(MixinInstance, ((KingdomTruceItemVM) obj).Faction2);
MixinInstance.GetType().GetProperty("DiplomacyProperties").SetValue(MixinInstance, new ViewModel.DiplomacyPropertiesVM(((KingdomTruceItemVM) obj).Faction1, ((KingdomTruceItemVM) obj).Faction2));
MixinInstance.OnRefresh();
}
}
else if (instance != null && instance.GetValue(methodInfo) is ViewModelMixin.KingdomWarItemVMMixin)
{
if (!Environment.StackTrace.Contains("OnRefresh"))
{
var MixinInstance = (ViewModelMixin.KingdomWarItemVMMixin) instance.GetValue(methodInfo);
var BadVm = MixinInstance.GetType().BaseType.GetField("_vm", BindingFlags.Instance | BindingFlags.NonPublic);
// Replace VM with obj
BadVm.SetValue(MixinInstance, new WeakReference<KingdomWarItemVM>((KingdomWarItemVM) obj));

// Replace private _factionn reference
var vm = MixinInstance.GetType().GetField("_faction2", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
vm.SetValue(MixinInstance, ((KingdomWarItemVM) obj).Faction2);
MixinInstance.GetType().GetProperty("DiplomacyProperties").SetValue(MixinInstance, new ViewModel.DiplomacyPropertiesVM(((KingdomWarItemVM) obj).Faction1, ((KingdomWarItemVM) obj).Faction2));
MixinInstance.OnRefresh();
}
}
}
}
}

0 comments on commit c421018

Please sign in to comment.