/
LibrarySubController.cs
200 lines (180 loc) · 5.87 KB
/
LibrarySubController.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Verse;
namespace CommunityCoreLibrary.Controller
{
/// <summary>
/// This controller validates ModHelperDefs (one per mod, version, etc)
/// </summary>
internal class LibrarySubController : SubController
{
public override string Name
{
get
{
return "Library Core";
}
}
// Override sequence priorities
public override int ValidationPriority
{
get
{
return 100;
}
}
// Override sequence priorities
public override int InitializationPriority
{
get
{
return -100;
}
}
// Validate ModHelperDefs, CCL load order, CCL versioning
public override bool Validate()
{
// Hopefully...
var stringBuilder = new StringBuilder();
var rVal = true;
CCL_Log.CaptureBegin( stringBuilder );
// Limit one ModHelperDef per mod
// Create the ordered list by inserting dummies for mods which don't have one
var allMods = LoadedModManager.LoadedMods.ToList();
for( int i = 0; i < allMods.Count; i++ )
{
var modHelperDef = (ModHelperDef)null;
var mod = allMods[ i ];
var modHelperDefs = Find_Extensions.DefListOfTypeForMod<ModHelperDef>( mod );
if( !modHelperDefs.NullOrEmpty() )
{
if( modHelperDefs.Count > 1 )
{
stringBuilder.Append( "\t" + mod.name );
CCL_Log.AppendSectionNewLine( ref stringBuilder, "Multiple ModHelperDefs detected" );
rVal = false;
}
else
{
// Validate the def
modHelperDef = modHelperDefs.First();
if( !modHelperDef.IsValid )
{
// Don't do anything special with broken mods
stringBuilder.Append( "\t" + mod.name );
CCL_Log.AppendSectionNewLine( ref stringBuilder, "ModHelperDef is invalid" );
rVal = false;
}
else if( !modHelperDef.dummy )
{
// Don't show validation message for dummy defs
stringBuilder.Append( "\t" + mod.name );
CCL_Log.AppendSection( ref stringBuilder, "ModHelperDef" );
CCL_Log.AppendSectionNewLine( ref stringBuilder, "Passed validation, requesting v" + modHelperDef.minCCLVersion );
}
}
}
else if( rVal == true )
{
// Doesn't exist, create a dummy for logging but only
// create if we're not just checking for remaining errors
modHelperDef = new ModHelperDef();
modHelperDef.defName = mod.name + "_ModHelperDef";
modHelperDef.minCCLVersion = Version.Minimum.ToString();
modHelperDef.ModName = mod.name;
modHelperDef.Verbosity = Verbosity.NonFatalErrors;
modHelperDef.dummy = true;
}
if( rVal == true )
{
// No errors, def is valid or a dummy
// Associate the def with the mod (the dictionary is to go the other way)
modHelperDef.mod = mod;
// Insert it into it's ordered place in the lists
Controller.Data.Mods.Insert( i, mod );
Controller.Data.ModHelperDefs.Insert( i, modHelperDef );
// Add a dictionary entry
Controller.Data.DictModHelperDefs.Add( mod, modHelperDef );
}
}
// Should now be a complete pair of lists in mod load order
// as well as a dictionary of mods and their defs
#if DEVELOPER
//Dump ordered list of mods and their defs
string dump = "Mod load order:\n";
for( int i = 0; i < Controller.Data.Mods.Count; i++ )
{
dump += "\t[" + i + "] - " + Controller.Data.Mods[ i ].name + " - " + Controller.Data.ModHelperDefs[ i ].defName + ( Controller.Data.ModHelperDefs[ i ].dummy ? " - dummy" : "" ) + "\n";
}
CCL_Log.Write( dump );
#endif
if( rVal )
{
// Find CCL in the mod order
bool found = false;
int index = -1;
for( int i = 0; i < allMods.Count; ++i )
{
LoadedMod mod = allMods[ i ];
if( mod.name == Controller.Data.UnityObjectName )
{
found = true;
index = i;
break;
}
}
if( !found )
{
stringBuilder.Append( "\tUnable to find mod '" );
stringBuilder.Append( Controller.Data.UnityObjectName );
stringBuilder.AppendLine( "' in mod load order!" );
rVal = false;
}
else if( index != 1 )
{
stringBuilder.Append( "\t'" );
stringBuilder.Append( Controller.Data.UnityObjectName );
stringBuilder.AppendLine( "' must be second in mod load order, immediately after 'Core'! :: Current position is #" + ( index + 1 ).ToString() );
rVal = false;
}
LoadedMod CCL_Mod = Controller.Data.Mods[ index ];
ModHelperDef CCL_HelperDef = Find_Extensions.ModHelperDefForMod( CCL_Mod );
// Validate xml version with assembly version
var vc = Version.Compare( CCL_HelperDef.minCCLVersion );
if( vc != Version.VersionCompare.ExactMatch )
{
stringBuilder.AppendLine( "\tModHelperDef version mismatch for Community Core Library!" );
rVal = false;
}
// CCL rank is #2 in load order and def version matches library
Controller.Data.cclMod = CCL_Mod;
Controller.Data.cclHelperDef = CCL_HelperDef;
}
// Should be all good or up until the first error encountered
CCL_Log.CaptureEnd(
stringBuilder,
rVal ? "Validated" : "Errors during validation"
);
strReturn = stringBuilder.ToString();
// Return true if all mods OK, false if any failed validation
State = rVal ? SubControllerState.Validated : SubControllerState.ValidationError;
return rVal;
}
public override bool Initialize()
{
// Don't need to keep checking on this controller
if( !Window_ModConfigurationMenu.InitializeMCMs() )
{
strReturn = "Errors initializing Mod Configuration Menus";
State = SubControllerState.InitializationError;
return false;
}
strReturn = "Mod Configuration Menus initialized";
State = SubControllerState.Hybernating;
return true;
}
}
}