forked from PoPoWanObi/CharacterCreation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubModule.cs
178 lines (147 loc) · 7.49 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
#nullable enable
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TaleWorlds.Core;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.MountAndBlade;
using TaleWorlds.Engine.Screens;
using TaleWorlds.Engine.GauntletUI;
using TaleWorlds.GauntletUI.Data;
using TaleWorlds.CampaignSystem.ViewModelCollection.Encyclopedia;
using SandBox.GauntletUI;
using HarmonyLib;
using CharacterCreation.Models;
using CharacterCreation.Util;
using System.Linq;
using TaleWorlds.SaveSystem;
using TaleWorlds.SaveSystem.Load;
namespace CharacterCreation
{
public class SubModule : MBSubModuleBase
{
public static readonly string ModuleFolderName = "zzCharacterCreation";
public static readonly string strings = "strings";
internal static readonly TextObject LoadedModMessage = new TextObject("{=CharacterCreation_LoadedModMessage}Loaded Detailed Character Creation."),
EditAppearanceForHeroMessage = new TextObject("{=CharacterCreation_EditAppearanceForHeroMessage}Entering edit appearance for: "),
ErrorLoadingDccMessage = new TextObject("{=CharacterCreation_ErrorLoadingDccMessage}Error initializing Detailed Character Creation:");
private const string ExpectedActualAgeMessage = "{=CharacterCreation_ExpectedActualAgeMessage}[Debug] Hero {HERO_NAME} expected age: {AGE1}, actual age: {AGE2}";
public static CampaignTime TimeSinceLastSave { get; private set; }
public static CampaignTime GetDeltaTime(bool update = false)
{
CampaignTime deltaTime = CampaignTime.Now - TimeSinceLastSave;
if (update) TimeSinceLastSave = CampaignTime.Now;
return deltaTime;
}
public static string GetFormattedAgeDebugMessage(Hero hero, float expectedAge)
{
var attributes = new Dictionary<string, TextObject>
{
["HERO_NAME"] = hero.Name,
["AGE1"] = new TextObject(expectedAge),
["AGE2"] = new TextObject(hero.Age)
};
return new TextObject(ExpectedActualAgeMessage, attributes).ToString();
}
// Main
protected override void OnSubModuleLoad()
{
base.OnSubModuleLoad();
try
{
var harmony = new Harmony("mod.bannerlord.popowanobi.dcc");
harmony.PatchAll();
CompatibilityPatch.CreateCompatibilityPatches(harmony);
TaleWorlds.Core.FaceGen.ShowDebugValues = true; // Developer facegen
}
catch (Exception ex)
{
MessageBox.Show($"{ErrorLoadingDccMessage}\n{ex.Message} \n\n{ex.InnerException?.Message}");
}
}
//Registers before the first module appears (main menu)
protected override void OnBeforeInitialModuleScreenSetAsRoot()
{
base.OnBeforeInitialModuleScreenSetAsRoot();
if (_isLoaded) return;
InformationManager.DisplayMessage(new InformationMessage(LoadedModMessage.ToString(), ColorManager.Orange));
_isLoaded = true;
}
// Load our XML files
private static void LoadXMLFiles(CampaignGameStarter gameInitializer)
{
// Load our additional strings
gameInitializer.LoadGameTexts(BasePath.Name + "Modules/" + ModuleFolderName + "/ModuleData/" + strings + ".xml");
}
// Called when loading save game
public override void OnGameLoaded(Game game, object initializerObject)
{
CampaignGameStarter gameInitializer = (CampaignGameStarter)initializerObject;
LoadXMLFiles(gameInitializer);
TaleWorlds.Core.FaceGen.ShowDebugValues = true;
if (!(game.GameType is Campaign) || DCCSettings.Instance == null || !DCCSettings.Instance.DebugMode) return;
var player = game.ObjectManager.GetObjectTypeList<Hero>().FirstOrDefault(hero => hero.IsHumanPlayerCharacter);
InformationManager.DisplayMessage(new InformationMessage(GetFormattedAgeDebugMessage(player, player.Age), ColorManager.Red));
Debug.Print(GetFormattedAgeDebugMessage(player, player.Age));
}
// Called when starting new campaign
public override void OnNewGameCreated(Game game, object initializerObject)
{
CampaignGameStarter gameInitializer = (CampaignGameStarter)initializerObject;
LoadXMLFiles(gameInitializer);
}
protected override void OnGameStart(Game game, IGameStarter gameStarterObject)
{
base.OnGameStart(game, gameStarterObject);
if (!(game.GameType is Campaign) || !(gameStarterObject is CampaignGameStarter))
return;
AddModels(gameStarterObject as CampaignGameStarter);
TimeSinceLastSave = CampaignTime.Now;
game.AddGameHandler<AgingGameHandler>();
game.EventManager.RegisterEvent<EncyclopediaPageChangedEvent>(new EncyclopediaPageChangedAction(heroModel).OnEncyclopediaPageChanged);
}
private void AddModels(IGameStarter gameStarter)
{
gameStarter.AddModel(heroModel = new HeroBuilderModel());
if (DCCSettings.Instance != null && DCCSettings.Instance.CustomAgeModel)
gameStarter.AddModel(new Models.AgeModel());
}
private HeroBuilderVM? viewModel;
private EncyclopediaHeroPageVM? selectedHeroPage;
private HeroBuilderModel? heroModel;
private Hero? selectedHero;
private ScreenBase? gauntletLayerTopScreen;
private GauntletLayer? gauntletLayer;
private GauntletMovie? gauntletMovie;
private bool _isLoaded;
private class AgingGameHandler : GameHandler
{
public override void OnAfterSave()
{
}
public override void OnBeforeSave()
{
if (Game.Current == null || !(Game.Current.GameType is Campaign)) return;
//CampaignTime deltaTime = CampaignTime.Now - TimeSinceLastSave;
CampaignTime deltaTime = GetDeltaTime(true);
//double yearsElapsed = deltaTime.ToYears;
//TimeSinceLastSave = CampaignTime.Now;
foreach (Hero hero in Game.Current.ObjectManager.GetObjectTypeList<Hero>())
{
//TODO:: Why is this conflicting now???
/*ddouble newAge = hero.Age + yearsElapsed;
DynamicBodyProperties dynamicBodyProperties = new DynamicBodyProperties((float)newAge, hero.Weight, hero.Build);*/
DynamicBodyProperties dynamicBodyProperties = new DynamicBodyProperties(hero.Age, hero.Weight, hero.Build);
BodyProperties heroBodyProperties = new BodyProperties(dynamicBodyProperties, hero.BodyProperties.StaticProperties);
//BodyProperties heroBodyProperties = hero.BodyProperties;
//CharacterBodyManager.CopyDynamicBodyProperties(dynamicBodyProperties, heroBodyProperties.DynamicProperties);
hero.CharacterObject.UpdatePlayerCharacterBodyProperties(heroBodyProperties, hero.IsFemale);
if (hero.IsHumanPlayerCharacter && DCCSettings.Instance != null && DCCSettings.Instance.DebugMode)
InformationManager.DisplayMessage(new InformationMessage(GetFormattedAgeDebugMessage(hero, hero.Age), ColorManager.Red));
}
}
}
}
}