-
Notifications
You must be signed in to change notification settings - Fork 19
/
SubModule.cs
152 lines (125 loc) · 4.69 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using BannerlordCheats.Extensions;
using BannerlordCheats.Localization;
using HarmonyLib;
using JetBrains.Annotations;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.ModuleManager;
using TaleWorlds.MountAndBlade;
namespace BannerlordCheats
{
[UsedImplicitly]
public class SubModule : MBSubModuleBase
{
private static bool PatchesApplied = false;
public override void OnGameInitializationFinished(Game game)
{
base.OnGameInitializationFinished(game);
if (!(game.GameType is Campaign) || SubModule.PatchesApplied)
{
return;
}
var harmony = new Harmony("BannerlordCheats");
var assembly = typeof(SubModule).Assembly;
var types = AccessTools.GetTypesFromAssembly(assembly);
var failedPatches = new List<string>();
foreach (var type in types)
{
try
{
new PatchClassProcessor(harmony, type).Patch();
}
catch (HarmonyException)
{
failedPatches.Add(type.Name);
}
}
SubModule.PatchesApplied = true;
if (failedPatches.Any())
{
InformationManager.ShowInquiry(new InquiryData(
L10N.GetText("ModFailedLoadWarningTitle"),
L10N.GetTextFormat("ModFailedLoadWarningMessage", string.Join(Environment.NewLine, failedPatches)),
true,
false,
L10N.GetText("ModWarningMessageConfirm"),
null,
null,
null));
}
}
internal static void LogError(Exception e, Type type)
{
string errorFilePath;
try
{
errorFilePath = SubModule.CreateErrorFile(e, type);
}
catch
{
return;
}
try
{
InformationManager.ShowInquiry(new InquiryData(
L10N.GetText("ModExceptionTitle"),
L10N.GetTextFormat("ModExceptionMessage", errorFilePath),
true,
false,
L10N.GetText("ModWarningMessageConfirm"),
null,
null,
null));
}
catch
{
try
{
Message.Show(L10N.GetTextFormat("ModExceptionMessage", errorFilePath), Colors.Red);
}
catch
{
// If this fails everything is lost anyways
}
}
}
private static string CreateErrorFile(Exception e, Type type = null)
{
var errorFileName = $"Error-{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.txt";
var assemblyLocation = Assembly.GetAssembly(typeof(SubModule)).Location;
var location = Path.GetDirectoryName(assemblyLocation);
var errorFilePath = Path.Combine(location, errorFileName);
var errorMessage = new StringBuilder();
errorMessage.AppendLine("Thanks a lot for helping to improve this mod!");
errorMessage.AppendLine("You could drop the contents of this file into https://pastebin.com/ and post a link to the file");
errorMessage.AppendLine("in the NexusMods posts page at https://www.nexusmods.com/mountandblade2bannerlord/mods/1839?tab=posts");
errorMessage.AppendLine();
errorMessage.AppendLine("Modules:");
foreach (var module in ModuleHelper.GetModules())
{
errorMessage.AppendLine($"{module.Name} {module.Version}");
}
if (type != null)
{
errorMessage.AppendLine();
errorMessage.AppendLine("Harmony Patch:");
var patch = type.GetCustomAttribute<HarmonyPatch>();
errorMessage.AppendLine($"Type: {type.FullName}");
errorMessage.AppendLine($"Declaring Type: {patch.info.declaringType.FullName}");
errorMessage.AppendLine($"Method: {patch.info.methodName}");
}
errorMessage.AppendLine();
errorMessage.AppendLine("Exception:");
errorMessage.AppendLine(e.ToString());
File.WriteAllText(errorFilePath, errorMessage.ToString());
return errorFilePath;
}
}
}