forked from andrewfullard/PyGCEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryCreator.py
More file actions
372 lines (307 loc) · 14.8 KB
/
RepositoryCreator.py
File metadata and controls
372 lines (307 loc) · 14.8 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
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
366
367
368
369
370
371
372
import os
import pandas as pd
from tqdm import tqdm
from gameObjects.gameObjectRepository import GameObjectRepository
from gameObjects.planet import Planet
from gameObjects.traderoute import TradeRoute
from gameObjects.campaign import Campaign
from gameObjects.faction import Faction
from gameObjects.startingForce import StartingForce
from xmlTools.xmlreader import XMLReader
from xmlTools.xmlstructure import XMLStructure
from util import getObject
class RepositoryCreator:
"""Creates a Repository of GameObjects from input XMLs"""
def __init__(self):
self.repository: GameObjectRepository = GameObjectRepository()
self.__folder: str = ""
self.__xml: XMLReader = XMLReader()
def getNamesRootsFromXML(self, rootsList, tag: str) -> list:
"""Takes a list of XML roots and a tag to search for
and returns the Names and Roots of GameObjects in the list"""
names = []
roots = []
for root in rootsList:
names.extend(self.__xml.getNamesFromXML(root))
roots.extend(root.iter(tag))
return names, roots
def addPlanetsFromXML(self, planetRoots) -> None:
"""Takes a list of Planet GameObject XML roots and adds
them to the repository with x and y positions"""
for planetRoot in planetRoots:
planetNames = self.__xml.getNamesFromXML(planetRoot)
for name in tqdm(planetNames):
newplanet = Planet(name)
newplanet.variantOf = self.__xml.getVariantOfValue(name, planetRoot)
coordinates = self.__xml.getLocation(name, planetRoot)
if coordinates == None:
newplanet.x, newplanet.y = None, None
else:
newplanet.x, newplanet.y = coordinates
# TODO better way than this hack to convert to int
newplanet.starbaseLevel = int(
float(
self.__xml.getObjectProperty(
name, planetRoot, ".//Max_Space_Base"
)
)
)
shipyard_list = {
"TEXT_PLANET_LIGHT": "Light Frigate",
"TEXT_PLANET_HEAVY": "Heavy Frigate",
"TEXT_PLANET_CAPITAL": "Capital",
"TEXT_PLANET_DREAD": "Dreadnaught"
}
shipyard = self.__xml.getObjectProperty(name, planetRoot, ".//Planet_Ability_Name")
newplanet.shipyardLevel = shipyard_list.get(shipyard, "No Shipyard Defined")
supports_list = {
"TEXT_RESOURCE_SUPPORTS_CLONING": "Cloning",
"TEXT_RESOURCE_SUPPORTS_CLONING_SUPPORTS_CREW_ACADEMY": "Cloning | Academy",
"TEXT_RESOURCE_SUPPORTS_CLONING_SUPPORTS_MINING": "Cloning | Mining",
"TEXT_RESOURCE_SUPPORTS_CREW_ACADEMY": "Academy",
"TEXT_RESOURCE_SUPPORTS_MINING": "Mining",
"TEXT_RESOURCE_SUPPORTS_MINING_SUPPORTS_TRADE": "Mining | Trade Hub",
"TEXT_RESOURCE_SUPPORTS_TRADE": "Trade Hub"
}
structure = self.__xml.getObjectProperty(name, planetRoot, ".//Encyclopedia_Weather_Name")
newplanet.SupportsStructure = supports_list.get(structure, "None")
newplanet.spaceStructureSlots = int(
float(
self.__xml.getObjectProperty(
name, planetRoot, ".//Special_Structures_Space"
)
)
)
newplanet.groundStructureSlots = int(
float(
self.__xml.getObjectProperty(
name, planetRoot, ".//Special_Structures_Land"
)
)
)
income_value = self.__xml.getObjectProperty(
name, planetRoot, ".//Planet_Credit_Value"
)
if income_value:
newplanet.income = int(float(income_value))
if coordinates == None:
print(
"Planet "
+ name
+ " not added to repository, missing coordinates"
)
continue
else:
self.repository.addPlanet(newplanet)
def addTradeRoutesFromXML(self, tradeRouteRoots) -> None:
"""Takes a list of Trade Route GameObject XML roots and adds
them to the repository with start and end planets"""
for tradeRouteRoot in tradeRouteRoots:
tradeRouteNames = self.__xml.getNamesFromXML(tradeRouteRoot)
for name in tqdm(tradeRouteNames):
newroute = TradeRoute(name)
newroute.start, newroute.end = self.__xml.getStartEnd(
name, self.repository.planets, tradeRouteRoot
)
self.repository.addTradeRoute(newroute)
def addFactionsFromXML(self, factionRoots) -> None:
"""Takes a list of Faction GameObject XML roots and adds
them to the repository"""
for factionRoot in factionRoots:
factionInfo = self.__xml.getFactionInfo(factionRoot)
for name, basic_ai, color, playable in factionInfo:
newFaction = Faction(name)
newFaction.color = color
newFaction.aiplayer = basic_ai
newFaction.playable = playable
self.repository.addFaction(newFaction)
def addCampaignsFromXML(self, campaignNames, campaignRoots) -> None:
"""Takes a list of Campaign GameObject XML roots and their names, and adds
them to the repository, after finding their planets and trade routes"""
current_campaign_set = ""
for (campaign, campaignRoot) in zip(campaignNames, campaignRoots):
setName = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Campaign_Set"
)
startingActivePlayer = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Starting_Active_Player"
).strip()
print("Loading campaign", campaign, "from set", setName)
if setName != current_campaign_set:
current_campaign_set = setName
newCampaign = Campaign(campaign)
else:
# MP campaigns don't have a starting active player
if startingActivePlayer:
self.repository.getCampaignBySetName(setName).playableFactions.add(self.repository.getFactionByName(startingActivePlayer))
continue
newCampaign.setName = setName
if startingActivePlayer:
newCampaign.playableFactions.add(self.repository.getFactionByName(startingActivePlayer))
newCampaignPlanets = set()
newCampaignTradeRoutes = set()
newCampaignStartingForces = list()
campaignPlanetNames = self.__xml.getListFromXMLRoot(
campaignRoot, ".//Locations"
)
new_campaign_locations = campaignPlanetNames
newCampaign.sortOrder = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Sort_Order"
)
newCampaign.textID = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Text_ID"
)
newCampaign.descriptionText = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Description_Text"
)
newCampaign.eraStart = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Era_Start"
)
useDefaultForcesText = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Use_Default_Forces"
)
newCampaign.useDefaultForces = self.__xml.stringToBool(useDefaultForcesText)
newCampaign.rebelStoryName = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Rebel_Story_Name"
)
newCampaign.empireStoryName = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Empire_Story_Name"
)
newCampaign.underworldStoryName = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Underworld_Story_Name"
)
newCampaign.storyName = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Story_Name"
)
newCampaign.isListed = self.__xml.getValueFromXMLRoot(
campaignRoot, ".//Is_Listed"
)
campaignTradeRouteNames = self.__xml.getListFromXMLRoot(
campaignRoot, ".//Trade_Routes"
)
campaignStartingForces = self.__xml.getMultiTag(
campaignRoot, ".//Starting_Forces"
)
for p in campaignPlanetNames:
newPlanet = getObject(p, self.repository.planets)
newCampaignPlanets.add(newPlanet)
for t in campaignTradeRouteNames:
newRoute = getObject(t, self.repository.tradeRoutes)
newCampaignTradeRoutes.add(newRoute)
for s in campaignStartingForces:
startingForcesEntry = self.getStartingForces(s)
newCampaignStartingForces.append(startingForcesEntry)
newCampaign.planets = newCampaignPlanets
newCampaign.tradeRoutes = newCampaignTradeRoutes
# TODO sum up identical entries into the Amount column
newCampaign.startingForces = pd.DataFrame(
newCampaignStartingForces,
columns=["Planet", "Era", "Owner", "ObjectType", "Amount"],
)
print("Found ", len(newCampaignPlanets), "planets and ", len(newCampaignTradeRoutes), "trade routes")
self.repository.addCampaign(newCampaign)
def runPlanetVariantOfCheck(self) -> None:
for planet in tqdm(self.repository.planets):
if (planet.x is None) or (planet.y is None):
print(planet.name + " needs parent coordinates")
if planet.variantOf != "":
parent = self.getPlanetParentWithCoordinates(planet)
planet.x = parent.x
planet.y = parent.y
print(
planet.name
+ " now uses "
+ parent.name
+ " coordinates!"
+ parent.x.__str__()
+ ", "
+ parent.y.__str__()
)
else:
print(planet.name + " has no parent!")
def getPlanetParentWithCoordinates(self, planet) -> Planet:
p = self.repository.getPlanetByName(planet.variantOf)
if p is not None:
if (p.x is None) & (p.y is None):
if p.variantOf == "":
return None
else:
return self.getPlanetParentWithCoordinates(p)
else:
return p
else:
return None
def getStartingForces(self, entry: str) -> StartingForce:
"""Produces a starting forces object from an XML entry"""
entry = entry.replace(",", " ")
entry = entry.split()
if len(entry) == 3:
factionName = entry[0]
planetName = entry[1]
unitName = entry[2]
return [planetName, 0, factionName, unitName, 1]
else:
print("Malformed starting forces entry ", entry)
return ["Empty", 0, "Neutral", "Empty", 1]
def getStartingForcesLibrary(self, libraryURL: str):
startingForcesLibrary = pd.read_csv(libraryURL)
current_planet = None
current_era = 0
for index, row in tqdm(startingForcesLibrary.iterrows()):
if row["Planet"] != current_planet:
current_planet = row["Planet"]
if row["Era"] != current_era:
current_era = row["Era"]
if not pd.isna(row["ReuseEra"]):
era_to_reuse = row["ReuseEra"]
reuse_filter = (startingForcesLibrary.Era == era_to_reuse) & (startingForcesLibrary.Planet == current_planet)
data_to_add = startingForcesLibrary[reuse_filter].copy()
data_to_add = data_to_add.assign(Era=current_era)
startingForcesLibrary = pd.concat([startingForcesLibrary, data_to_add])
continue
startingForcesLibrary.reset_index()
startingForcesLibrary.drop(["ReuseEra"], inplace=True, axis=1)
startingForcesLibrary.dropna(inplace=True)
return startingForcesLibrary
def constructRepository(
self, folder: str, startingForcesLibraryURL: str
) -> GameObjectRepository:
"""Reads a mod Data folder and searches the XML metafiles within
Creates a repository with planets, trade routes and campaigns"""
self.__folder = folder
self.__startingForcesLibraryURL = startingForcesLibraryURL
XMLStructure.dataFolder = self.__folder
gameObjectFile = self.__folder + "/XML/GameObjectFiles.XML"
campaignFile = self.__folder + "/XML/CampaignFiles.XML"
tradeRouteFile = self.__folder + "/XML/TradeRouteFiles.XML"
factionFile = self.__folder + "/XML/FactionFiles.XML"
planetRoots = self.__xml.findPlanetsFiles(gameObjectFile)
tradeRouteRoots = self.__xml.findMetaFileRefs(tradeRouteFile)
factionRoots = self.__xml.findMetaFileRefs(factionFile)
campaignRootList = self.__xml.findMetaFileRefs(campaignFile)
if os.path.exists(gameObjectFile):
print("\nLoading Planets")
planetRoots = self.__xml.findPlanetsFiles(gameObjectFile)
self.addPlanetsFromXML(planetRoots)
if os.path.exists(tradeRouteFile):
print("\nLoading Trade Routes")
tradeRouteRoots = self.__xml.findMetaFileRefs(tradeRouteFile)
self.addTradeRoutesFromXML(tradeRouteRoots)
if os.path.exists(factionFile):
print("\nLoading Factions")
factionRoots = self.__xml.findMetaFileRefs(factionFile)
self.addFactionsFromXML(factionRoots)
if os.path.exists(campaignFile):
print("\nLoading Campigns")
campaignRootList = self.__xml.findMetaFileRefs(campaignFile)
campaignNames, campaignRoots = self.getNamesRootsFromXML(
campaignRootList, "Campaign"
)
self.addCampaignsFromXML(campaignNames, campaignRoots)
print("\nChecking for planet variants")
self.runPlanetVariantOfCheck()
print("\nLoading starting forces")
self.repository.startingForcesLibrary = self.getStartingForcesLibrary(
self.__startingForcesLibraryURL
)
return self.repository