-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubModule.cs
252 lines (226 loc) · 8.63 KB
/
SubModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using HarmonyLib;
using SandBox.GauntletUI;
using SandBox.View.Map;
using SandBox.ViewModelCollection.Nameplate;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.GameState;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.ViewModelCollection.Encyclopedia;
using TaleWorlds.CampaignSystem.ViewModelCollection.Inventory;
using TaleWorlds.Core;
using TaleWorlds.Engine.Screens;
using TaleWorlds.InputSystem;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.MountAndBlade;
using TaleWorlds.ScreenSystem;
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedType.Global
// ReSharper disable UnusedType.Local
// ReSharper disable InconsistentNaming
namespace MapFind
{
public class SubModule : MBSubModuleBase
{
private static MapEncyclopediaView mapEncyclopediaView;
private static GauntletClanScreen gauntletClanScreen;
private readonly Harmony harmony = new Harmony("ca.gnivler.bannerlord.MapFind");
protected override void OnSubModuleLoad()
{
Log("Startup " + DateTime.Now.ToShortTimeString());
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
private static void Log(object input)
{
//FileLog.Log($"[MapFind] {input ?? "null"}");
}
private static void SetCameraToSettlement(string stringID)
{
try
{
Log($"SetCameraToSettlement({stringID})");
CloseAnyOpenWindows();
MapScreen.Instance.FastMoveCameraToPosition(Settlement.Find(stringID).Position2D);
Traverse.Create(MapScreen.Instance).Method("UpdateMapCamera").GetValue();
}
catch (Exception ex)
{
Log(ex);
}
}
private static void CloseAnyOpenWindows()
{
if (mapEncyclopediaView.IsEncyclopediaOpen)
{
mapEncyclopediaView.CloseEncyclopedia();
}
if (gauntletClanScreen != null && gauntletClanScreen.IsActive)
{
Traverse.Create(gauntletClanScreen).Method("CloseClanScreen").GetValue();
}
}
private static string ConvertLinkToSettlementName(string linkString)
{
var match = Regex.Match(linkString, @"Settlement-(.*)""><b>", RegexOptions.CultureInvariant);
var settlementName = match.Groups[1].Value;
return settlementName;
}
[HarmonyPatch(typeof(EncyclopediaNavigatorVM), "ExecuteLink")]
public static class EncyclopediaConceptPageVMExecuteLinkPatch
{
private static bool Prefix(object target)
{
try
{
Log($"EncyclopediaNavigatorVM.ExecuteLink({target})");
var shift = Input.IsKeyDown(InputKey.LeftShift) || Input.IsKeyDown(InputKey.RightShift);
if (shift)
{
string linkName = default;
var type = target.GetType().Name;
switch (type)
{
case "Settlement":
{
linkName = ((Settlement)target).StringId;
break;
}
case "Hero":
{
linkName = ((Hero)target).LastSeenPlace.StringId;
break;
}
case "Kingdom":
{
linkName = ((Kingdom)target).Leader.LastSeenPlace.StringId;
break;
}
case "Clan":
{
linkName = ((Clan)target).Leader.LastSeenPlace.StringId;
break;
}
}
SetCameraToSettlement(linkName);
return false;
}
}
catch (Exception ex)
{
Log(ex);
}
return true;
}
}
[HarmonyPatch(typeof(MapEncyclopediaView), MethodType.Constructor)]
public static class MapEncyclopediaViewCtorPatch
{
private static void Postfix(MapEncyclopediaView __instance)
{
try
{
Log("MapEncyclopediaView");
mapEncyclopediaView = __instance;
}
catch (Exception ex)
{
Log(ex);
}
}
}
[HarmonyPatch(typeof(GauntletClanScreen), MethodType.Constructor, typeof(ClanState))]
public static class GauntletClanScreenCtorPatch
{
private static void Postfix(GauntletClanScreen __instance)
{
try
{
Log("GauntletClanScreenCtorPatch");
gauntletClanScreen = __instance;
}
catch (Exception ex)
{
Log(ex);
}
}
}
// shift click settlement to move there and camera-follow
// ctrl-shift click to move
// unmodified click is "centre camera" (return true)
[HarmonyPatch(typeof(SettlementNameplateVM), "ExecuteSetCameraPosition")]
public static class SettlementNameplateVMExecuteSetCameraPositionPatch
{
public static bool Prefix(SettlementNameplateVM __instance)
{
try
{
if (Traverse.Create(MapScreen.Instance).Field("_mapState").GetValue<MapState>().AtMenu) return true;
var shift = Input.IsKeyDown(InputKey.LeftShift) || Input.IsKeyDown(InputKey.RightShift);
if (shift)
{
var ctrl = Input.IsKeyDown(InputKey.LeftControl) || Input.IsKeyDown(InputKey.RightControl);
MobileParty.MainParty.SetMoveGoToSettlement(__instance.Settlement);
Campaign.Current.TimeControlMode = CampaignTimeControlMode.StoppableFastForward;
if (ctrl)
{
MapScreen.Instance.CurrentCameraFollowMode = MapScreen.CameraFollowMode.Free;
}
else
{
MapScreen.Instance.CurrentCameraFollowMode = MapScreen.CameraFollowMode.FollowParty;
}
return false;
}
}
catch (Exception ex)
{
Log(ex);
}
return true;
}
}
protected override void OnApplicationTick(float dt)
{
// thanks Cheyron for the fix
if (!(ScreenManager.TopScreen is MapScreen))
{
return;
}
if ((Input.IsKeyDown(InputKey.LeftControl) || Input.IsKeyDown(InputKey.RightControl))
&&
Input.IsKeyPressed(InputKey.F12))
{
var textInquiryData = new TextInquiryData(
new TextObject("Enter settlement name").ToString(),
"",
true,
true,
"Find",
"Cancel",
GoToSettlementByString,
null);
InformationManager.ShowTextInquiry(textInquiryData, true);
}
}
private static void GoToSettlementByString(string name)
{
var matches = Settlement.FindAll(x =>
x.Name.ToString().ToLower().StartsWith(name)).ToList();
if (matches.Count == 0)
{
return;
}
// prefer an exact match over a substring
var settlement = matches.Any(x => x.ToString().Length == name.Length)
? matches.First(x => x.ToString().Length == name.Length)
: matches.OrderByDescending(x => x.ToString().Length).First();
SetCameraToSettlement(settlement.StringId);
}
}
}