-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributePerLevelSubModule.cs
More file actions
84 lines (74 loc) · 2.33 KB
/
Copy pathAttributePerLevelSubModule.cs
File metadata and controls
84 lines (74 loc) · 2.33 KB
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
using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.GameComponents;
using TaleWorlds.Core;
using TaleWorlds.MountAndBlade;
namespace AttributePerLevel
{
public class AttributePerLevelSubModule : MBSubModuleBase
{
protected static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();
protected override void OnSubModuleLoad()
{
NLog.Config.LoggingConfiguration logConfig = new NLog.Config.LoggingConfiguration();
NLog.Targets.FileTarget logFile = new NLog.Targets.FileTarget(LogFileTarget()) { FileName = LogFilePath() };
logConfig.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, logFile);
NLog.LogManager.Configuration = logConfig;
}
protected virtual string LogFileTarget()
{
return "AttributePerLevelLogFile";
}
protected virtual string LogFilePath()
{
// The default, relative path will place the log in $(GameFolder)\bin\Win64_Shipping_Client\
return "AttributePerLevelLogFile.txt";
}
protected override void OnGameStart(Game game, IGameStarter gameStarterObject)
{
if (!(game.GameType is Campaign))
{
return;
}
Log.Info("OnGameStart");
AddModels(gameStarterObject);
}
protected virtual void AddModels(IGameStarter gameStarterObject)
{
ReplaceModel<DefaultCharacterDevelopmentModel, PerLevelCharacterModel>(gameStarterObject);
}
protected void ReplaceModel<TBaseType, TChildType>(IGameStarter gameStarterObject)
where TBaseType : GameModel
where TChildType : TBaseType
{
if (!(gameStarterObject.Models is IList<GameModel> models))
{
Log.Error("Models was not a list");
return;
}
bool found = false;
for (int index = 0; index < models.Count; ++index)
{
if (models[index] is TBaseType)
{
found = true;
if (models[index] is TChildType)
{
Log.Info($"Child model {typeof(TChildType).Name} found, skipping.");
}
else
{
Log.Info($"Base model {typeof(TBaseType).Name} found. Replacing with child model {typeof(TChildType).Name}");
models[index] = Activator.CreateInstance<TChildType>();
}
}
}
if (!found)
{
Log.Info($"Base model {typeof(TBaseType).Name} was not found. Adding child model {typeof(TChildType).Name}");
gameStarterObject.AddModel(Activator.CreateInstance<TChildType>());
}
}
}
}