-
Notifications
You must be signed in to change notification settings - Fork 0
/
CK2World.cs
365 lines (325 loc) · 13.3 KB
/
CK2World.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using Microsoft.VisualBasic.FileIO;
using PdxFile;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CK2Helper
{
public class CK2World
{
private readonly string GAME_PATH = @"C:\Program Files (x86)\Steam\steamapps\common\Crusader Kings II\";
public Dictionary<string, CK2Title> CK2Titles { get; set; }
public Dictionary<string, CK2Title> CK2IndependentTitles { get; set; }
public Dictionary<string, CK2Title> CK2TopLevelVassals { get; set; }
public Dictionary<int, CK2CharacterBase> CK2Characters { get; set; }
public Dictionary<int, CK2Dynasty> CK2Dynasties { get; set; }
public List<CK2Traits> CK2Traits { get; set; }
public Dictionary<string, CK2Religion> CK2Religions { get; set; }
public Dictionary<string, CK2Culture> CK2Cultures { get; set; }
public Dictionary<string, CK2CultureGroup> CK2CultureGroups { get; private set; }
public Bitmap ProvinceMap { get; set; }
public string Date { get; set; }
public Dictionary<int, CK2Province> CK2Provinces { get; set; }
public Dictionary<int, CK2Province> CK2ProvinceDupes { get; set; }
///// <summary>
///// maps ck2 province IDs to the county title ID
///// </summary>
//public Dictionary<string, string> CK2ProvCounties { get; set; }
///// <summary>
///// same as CK2ProvCounties but for those annoying cases were PDX put 2 provinces with the same ID in the history files
///// </summary>
//public Dictionary<string, string> CK2DupeProvCounties { get; set; }
/// <summary>
/// maps ck2 province colour to the ID
/// </summary>
public Dictionary<int, string> CK2ProvColours { get; set; }
/// <summary>
/// Maps ck2 province IDs to their positions
/// </summary>
public Dictionary<string, List<Point>> CK2ProvPositions { get; set; }
public Dictionary<string, string> Localisation { get; set; }
public string ModPath { get; set; }
public Dictionary<string, CK2Building> CK2Buildings { get; set; }
public List<Task> TaskPool { get; internal set; }
public CK2World(string modPath)
{
Console.WriteLine("Loading CK2 data...");
ModPath = modPath;
TaskPool = new List<Task>();
LoadLocalisation();
LoadTraits();
LoadReligion();
LoadCulture();
LoadDynasties();
LoadProvCounties();
LoadBuildings();
}
private void LoadLocalisation()
{
Localisation = new Dictionary<string, string>();
var localeFiles = GetFilesFor("localisation");
foreach (var file in localeFiles)
{
LoadLocalisationFile(file);
}
}
private void LoadLocalisationFile(string path)
{
using (var file = new StreamReader(path, Encoding.Default))
{
var key = new StringBuilder();
var value = new StringBuilder();
var readKey = true;
var readValue = false;
var comment = true;
//var inQuotes = false;
while (!file.EndOfStream)
{
var ch = Convert.ToChar(file.Read());
if (Environment.NewLine.Contains(ch))
{
readValue = false;
readKey = true;
if (key.Length > 0 && value.Length > 0 && !Localisation.ContainsKey(key.ToString()))
{
Localisation.Add(key.ToString(), value.ToString());
}
key = new StringBuilder();
value = new StringBuilder();
comment = false;
continue;
}
if (ch == '#' || comment)
{
comment = true;
continue;
}
if (ch == ';')
{
if (readValue)
{
readValue = false;
}
if (readKey)
{
readKey = false;
readValue = true;
}
continue;
}
if (readKey)
{
key.Append(ch);
}
if (readValue)
{
value.Append(ch);
}
}
if (key.Length > 0 && value.Length > 0)
{
Localisation.Add(key.ToString(), value.ToString());
}
}
}
private void LoadDynasties()
{
Console.WriteLine("Loading static CK2 dynasties...");
CK2Dynasties = new Dictionary<int, CK2Dynasty>();
var dynFiles = GetFilesFor(@"common\dynasties");
foreach (var dynFile in dynFiles)
{
var data = PdxSublist.ReadFile(dynFile);
data.ForEachSublist(sub =>
{
CK2Dynasties[int.Parse(sub.Key)] = new CK2Dynasty(this, sub.Value);
});
}
}
private void LoadBuildings()
{
Console.WriteLine("Loading CK2 buildings...");
CK2Buildings = new Dictionary<string, CK2Building>();
var buildingFiles = GetFilesFor("common\\buildings");
foreach (var file in buildingFiles)
{
var data = PdxSublist.ReadFile(file);
data.ForEachSublist(sub =>
{
sub.Value.ForEachSublist(building =>
{
CK2Buildings.Add(building.Key, new CK2Building(building.Value));
});
});
}
}
private void LoadProvCounties()
{
Console.WriteLine("Loading CK2 provinces...");
// CK2ProvCounties = new Dictionary<string, string>();
// CK2DupeProvCounties = new Dictionary<string, string>();
CK2Provinces = new Dictionary<int, CK2Province>();
CK2ProvinceDupes = new Dictionary<int, CK2Province>();
var provFiles = GetFilesFor("history\\provinces");
foreach (var provFile in provFiles)
{
var data = PdxSublist.ReadFile(provFile);
var provID = int.Parse(new Regex("[0-9]+").Match(Path.GetFileName(provFile)).Value);
var prov = new CK2Province(provID, this, data);
if (CK2Provinces.ContainsKey(provID)) {
CK2ProvinceDupes.Add(provID, prov);
} else {
CK2Provinces.Add(provID, prov);
}
// if (CK2ProvCounties.ContainsKey(provID))
// {
// CK2DupeProvCounties.Add(provID, data.KeyValuePairs["title"]);
// }
// else
// {
// CK2ProvCounties.Add(provID, data.KeyValuePairs["title"]);
// }
}
var mapFiles = GetFilesFor("map");
var provMapFile = mapFiles.Single(m => Path.GetFileName(m) == "provinces.bmp");
ProvinceMap = new Bitmap(provMapFile);
CK2ProvColours = new Dictionary<int, string>();
var defFile = mapFiles.Single(m => Path.GetFileName(m) == "definition.csv");
using (TextFieldParser parser = new TextFieldParser(defFile))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(";");
parser.ReadFields();
while (!parser.EndOfData)
{
//Process row
string[] fields = parser.ReadFields();
var col = Color.FromArgb(int.Parse(fields[1]), int.Parse(fields[2]), int.Parse(fields[3]));
CK2ProvColours[col.ToArgb()] = fields[0];
}
}
Console.WriteLine("Loading province map...");
CK2ProvPositions = new Dictionary<string, List<Point>>();
var bmp = ProvinceMap;
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
var pxColour = bmp.GetPixel(x, y).ToArgb();
if (CK2ProvColours.ContainsKey(pxColour))
{
var provID = CK2ProvColours[pxColour];
if (!CK2ProvPositions.ContainsKey(provID))
{
CK2ProvPositions[provID] = new List<Point>();
}
CK2ProvPositions[provID].Add(new Point(x, y));
}
}
}
}
internal void AddIndependentTitle(CK2Title ck2Title)
{
CK2IndependentTitles[ck2Title.Name] = ck2Title;
}
private void LoadCulture()
{
Console.WriteLine("Loading CK2 cultures...");
CK2Cultures = new Dictionary<string, CK2Culture>();
CK2CultureGroups = new Dictionary<string, CK2CultureGroup>();
var cultureFiles = GetFilesFor(@"common\cultures");
foreach (var file in cultureFiles)
{
var cultureGroups = PdxSublist.ReadFile(file);
cultureGroups.ForEachSublist(culGroup =>
{
var group = new CK2CultureGroup(culGroup.Key);
CK2CultureGroups[culGroup.Key] = group;
culGroup.Value.ForEachSublist(rel =>
{
if (rel.Key != "graphical_cultures")
{
CK2Cultures[rel.Key] = new CK2Culture(rel.Value, this, group);
}
});
});
}
}
private void LoadReligion()
{
Console.WriteLine("Loading CK2 religions...");
CK2Religions = new Dictionary<string, CK2Religion>();
var religionFiles = GetFilesFor(@"common\religions");
foreach (var file in religionFiles)
{
var religionGroups = PdxSublist.ReadFile(file);
religionGroups.ForEachSublist(relGroup =>
{
if (relGroup.Key == "secret_religion_visibility_trigger")
{
return;
}
relGroup.Value.ForEachSublist(rel =>
{
if (!(rel.Key == "color" || rel.Key == "male_names" || rel.Key == "female_names"))
{
CK2Religions[rel.Key] = new CK2Religion(rel.Key);
}
});
});
}
}
private void LoadTraits()
{
Console.WriteLine("Loading CK2 traits...");
CK2Traits = new List<CK2Traits>();
CK2Traits.Add(null);
var traitFiles = GetFilesFor(@"common\traits");
traitFiles.Sort();
foreach (var file in traitFiles)
{
var traits = PdxSublist.ReadFile(file);
traits.ForEachSublist(trait =>
{
CK2Traits.Add(new CK2Traits(trait));
});
}
}
public List<string> GetFilesFor(string path)
{
var modPath = Path.Combine(ModPath, path);
var gameFiles = Directory.GetFiles(Path.Combine(GAME_PATH, path));
var modFileNames = Directory.Exists(modPath) ? Directory.GetFiles(modPath).Select(Path.GetFileName) : new string[] { };
var files = new List<string>();
foreach (var name in gameFiles)
{
if (modFileNames.Contains(Path.GetFileName(name)))
{
files.Add(Path.Combine(modPath, Path.GetFileName(name)));
}
else
{
files.Add(name);
}
}
foreach (var name in modFileNames)
{
var modFilePath = Path.Combine(modPath, Path.GetFileName(name));
if (!files.Contains(modFilePath))
{
files.Add(modFilePath);
}
}
return files;
}
public void AddTitle(CK2Title title)
{
CK2Titles.Add(title.Name, title);
}
}
}