-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWorldCompSaveHandler.cs
More file actions
176 lines (151 loc) · 7.82 KB
/
WorldCompSaveHandler.cs
File metadata and controls
176 lines (151 loc) · 7.82 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
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
using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Linq;
using Verse;
namespace TechAdvancing
{
internal class WorldCompSaveHandler : WorldComponent
{
private Dictionary<string, int> ConfigValues = new Dictionary<string, int>();
internal List<string> GetConfigValueNames => this.ConfigValues.Keys.ToList();
/// <summary>
/// Stores all the pawns that joined along with their old Faction
/// </summary>
public Dictionary<Pawn, Faction> ColonyPeople = new Dictionary<Pawn, Faction>(); //pawn , ORIGINAL faction
/// <summary>
/// Stores per-tab settings for cost modifications. Key is tab defName, value is whether cost modifications apply. DEFAULT VALUE IS TRUE.
/// </summary>
public Dictionary<string, bool> TabCostModificationSettings = new Dictionary<string, bool>();
public bool isInitialized;
public WorldCompSaveHandler(World world) : base(world) // Rimworld will initialize this on world load!
{
this.isInitialized = false;
}
public void LoadValuesForUpgrade(Dictionary<string, int> cfgVals, Dictionary<Pawn, Faction> colonyPpl)
{
this.ConfigValues = cfgVals;
this.ColonyPeople = colonyPpl;
}
public bool IsValueSaved(string key) { return this.ConfigValues.ContainsKey(key); }
public void RemoveConfigValue(string key) { this.ConfigValues.Remove(key); }
/// <summary>
/// Checks if cost modifications should apply to a given research tab
/// </summary>
/// <param name="tabDefName">The defName of the research tab</param>
/// <returns>True if cost modifications should apply, false otherwise</returns>
public bool ShouldApplyCostModifications(string tabDefName)
{
if (string.IsNullOrEmpty(tabDefName))
return true; // Default to true for null tabs
if (this.TabCostModificationSettings.TryGetValue(tabDefName, out bool value))
return value;
// Default to true for tabs not in the dictionary
return true;
}
public void TA_ExposeData(ConfigTabValueSavedAttribute attrib, ref int value, TA_Expose_Mode mode = TA_Expose_Mode.Load)
{
string saveName = attrib.SaveName;
object defaultValue = attrib.DefaultValue;
if (mode == TA_Expose_Mode.Save)
{
LogOutput.WriteLogMessage(Errorlevel.Debug, "Adding " + saveName + " : " + value + " to save dictionary");
if (this.ConfigValues.ContainsKey(saveName))
{
this.ConfigValues.Remove(saveName);
}
this.ConfigValues.Add(saveName, value);
}
else if (mode == TA_Expose_Mode.Load)
{
if (this.ConfigValues.TryGetValue(saveName, out int tempval))
{
value = tempval;
}
else if (this.ConfigValues.TryGetValue(Enum.GetNames(typeof(TA_Expose_Name)).Contains(saveName) ? ((int)Enum.Parse(typeof(TA_Expose_Name), saveName)).ToString() : saveName, out tempval)) // TODO remove backwards compatability fallback
{
value = tempval;
LogOutput.WriteLogMessage(Errorlevel.Information, "Value " + saveName + " was loaded via fallback. (A new save system is in place. But this message shouldnt appear anymore after saving)");
}
else
{
LogOutput.WriteLogMessage(Errorlevel.Information, "Value " + saveName + " is not present. Using default value.");
if (defaultValue == null)
LogOutput.WriteLogMessage(Errorlevel.Error, $"Default value of {saveName} is null!");
else
value = (int)defaultValue;
}
LogOutput.WriteLogMessage(Errorlevel.Debug, "Successfully loaded " + saveName + " : " + value + " from save dictionary.");
}
}
public override void ExposeData()
{
LogOutput.WriteLogMessage(Errorlevel.Debug, $"Loading begun. Factiondef techlevel: {Find.FactionManager.OfPlayer.def.techLevel}");
if (Scribe.mode == LoadSaveMode.LoadingVars)
{
if (TA_ResearchManager.originalTechlevelCache.ContainsKey(Find.FactionManager.OfPlayer.Name))
{
var correctTl = TA_ResearchManager.originalTechlevelCache[Find.FactionManager.OfPlayer.Name];
LogOutput.WriteLogMessage(Errorlevel.Information, $"The playerfaction is the same as one which was previously loaded. " +
$"Resetting the techlevel to what it was before we changed it. Current faction techlevel: {Find.FactionManager.OfPlayer.def.techLevel} New (correct) techlevel: {correctTl}");
Find.FactionManager.OfPlayer.def.techLevel = correctTl;
}
else
{
LogOutput.WriteLogMessage(Errorlevel.Debug, $"Scribe mode is LoadingVars. The playerfaction is new, adding it to cache, with techlevel {Find.FactionManager.OfPlayer.def.techLevel}.");
TA_ResearchManager.originalTechlevelCache.Add(Find.FactionManager.OfPlayer.Name, Find.FactionManager.OfPlayer.def.techLevel);
}
}
TechAdvancing_Config_Tab.worldCompSaveHandler = this;
base.ExposeData();
Scribe_Collections.Look(ref this.ConfigValues, "TA_Expose_Numbers", LookMode.Value, LookMode.Value);
int isPplDictSaved = 1;
//LogOutput.WriteLogMessage(Errorlevel.Information, "val:" + isPplDictSaved.ToString());
Scribe_Values.Look(ref isPplDictSaved, "TA_Expose_People_isSaved", -1, true);
//LogOutput.WriteLogMessage(Errorlevel.Information, "val:" + isPplDictSaved.ToString());
if (this.ColonyPeople != null)
{
this.ColonyPeople.RemoveAll(x => x.Key == null);
}
if (isPplDictSaved == 1)
{
Scribe_Collections.Look(ref this.ColonyPeople, "TA_Expose_People", LookMode.Reference, LookMode.Reference);
//LogOutput.WriteLogMessage(Errorlevel.Information, "Read TA_ExposePeople");
}
// Load tab cost modification settings
Scribe_Collections.Look(ref this.TabCostModificationSettings, "TA_Expose_TabCostSettings", LookMode.Value, LookMode.Value);
if (this.TabCostModificationSettings == null)
{
this.TabCostModificationSettings = new Dictionary<string, bool>();
}
// Set default for Anomaly tab if not already saved
if (!this.TabCostModificationSettings.ContainsKey("Anomaly"))
{
this.TabCostModificationSettings["Anomaly"] = false;
LogOutput.WriteLogMessage(Errorlevel.Debug, "Setting Anomaly tab cost modifications to disabled by default");
}
TechAdvancing_Config_Tab.ExposeData(TA_Expose_Mode.Load);
if (this.ColonyPeople == null)
{
this.ColonyPeople = new Dictionary<Pawn, Faction>();
}
LogOutput.WriteLogMessage(Errorlevel.Information, "Loading finished.");
TA_ResearchManager.FlushCfg();
}
}
public enum TA_Expose_Mode
{
Save,
Load
}
public enum TA_Expose_Name // TODO Remove soon
{
Conditionvalue_A,
Conditionvalue_B,
Conditionvalue_B_s,
baseTechlvlCfg,
configCheckboxNeedTechColonists,
configCheckboxDisableCostMultiplicatorCap
}
}