import argparse

import glob

import os

import copy

def setupArgs():

parser = argparse.ArgumentParser()

parser.add_argument("-l", "--location", help="Location of the save file directory",

default=r'C:\Users\User\Documents\Paradox Interactive\Crusader Kings II\save games')

parser.add_argument("-f", "--fileName", help="Name of the savefile")

parser.add_argument("-v", "--verbose", help="Verbosity of script", default=0, type=int)

parser.add_argument("-d", "--delete", help="Whether intermediate values should be deleted", default=True,

action='store_false')

tempArgs = parser.parse_args()

tempArgs.location += '\\'

if tempArgs.verbose > 5:

print(tempArgs)

if tempArgs.fileName is None:

fileList = glob.iglob(tempArgs.location + '*')

latestFile = max(fileList, key=os.path.getctime)

latestFile = latestFile.replace(tempArgs.location, '')

tempArgs.fileName = latestFile

return tempArgs

def determinePlayer(inFile, lineCount=0, outFile=None):

# returns player id by seraching for 'player' block then finding the 'id=' line

line = inFile.readline()

lineCount += 1

if outFile:

outFile.write(line)

blockName = 'player'

paramName = 'id='

while blockName not in line:

line = inFile.readline()

lineCount += 1

if outFile:

outFile.write(line)

while paramName not in line:

line = inFile.readline()

lineCount += 1

if outFile:

outFile.write(line)

if args.verbose > 5:

print("player loc = " + str(line.find(paramName) + len(paramName)))

if args.verbose > 1:

print("player = \'" + line[line.find(paramName) + len(paramName):-1] + "\'")

return line[line.find(paramName) + len(paramName):-1], lineCount

def readUntil(inFile, targStr, lineCount=0, outFile=None):

line = inFile.readline()

lineCount += 1

if outFile:

outFile.write(line)

while line != targStr:

line = inFile.readline()

lineCount += 1

if outFile:

outFile.write(line)

if line == '':

return lineCount, True

return lineCount, False

def processCourtier(inFile, playerNum, lineCount=0, outFile=None):

courtier = [inFile.readline()]

courtierNum = ''.join(filter(str.isdigit, courtier[0]))

lineCount += 1

keepCourtier = True

while courtier[-1] != '\t\t}

': # line representing end of courtier block

courtier.append(inFile.readline())

lineCount += 1

if (('\t\t\temp=' + playerNum + '

') in courtier) and (('\t\t\thost=' + playerNum + '

') in courtier):

for line in courtier:

if "\t\t\ttitle=" in line or \

"\t\t\tclaim=" in line or \

"\t\t\tjob=" in line or \

'\t\t\tfat=' + playerNum + '

' in line or \

'\t\t\tmott=' + playerNum + '

' in line:

if args.verbose > 3:

print(line)

break

else:

keepCourtier = checkBloodline(courtierNum)

if keepCourtier and outFile:

for line in courtier:

outFile.write(line)

if args.verbose > 3 :

print(courtierNum + ':\t' + courtier[2][7:-2])

return lineCount, courtierNum, keepCourtier # extract the courtier number

def checkBloodline(courtier):

with open(args.location + args.fileName, "r") as file:

lineCount, _ = readUntil(file, '\tbloodline=

')

courtierBloodline = False

line = file.readline()

while line != '\t}

':

if line == '\t\t\tmember=' + courtier + '

':

return True

line = file.readline()

return False

def readNextBracket(file, lineCount=0, outFile=None):

lineCount, Done = readUntil(file, '\t{

', lineCount, outFile)

if args.verbose > 3:

print("line = " + str(lineCount))

return lineCount, Done

def removeCourtierRelations(inFile, courtierNum, lineCount=0, outFile=None):

line = ''

while line != '\t}

':

while ((courtierNum not in line) and (line != '\t\t}

')):

line = inFile.readline()

lineCount += 1

if outFile:

outFile.write(line)

if ('rel_' + courtierNum + '=') in line:

lineCount, Done = readUntil(inFile, '\t\t}

', lineCount)

if Done:

break

elif (courtierNum in line):

lineCount, Done = readUntil(inFile, '\t\t\t}

', lineCount)

if Done:

break

line = inFile.readline() # advance line variable

lineCount += 1

if outFile:

outFile.write(line)

return lineCount

def removeCourtierAmbition(inFile, courtierNum, lineCount=0, outFile=None):

lineCount, Done = readUntil(inFile, '\tactive_ambition=

', lineCount, outFile)

if Done:

return lineCount, Done

ambitionBlock = [inFile.readline()]

lineCount += 1

keepBlock = True

while ambitionBlock[-1] != '\t}

':

line = inFile.readline()

lineCount += 1

if courtierNum in line:

keepBlock = False

ambitionBlock.append(line)

if outFile and keepBlock:

for line in ambitionBlock:

outFile.write(line)

return lineCount, Done

def main(startCourtier = None):

with open(args.location + args.outFile, "w") as tempFile:

with open(args.location + args.fileName, "r") as file:

lineCount = 0

player, lineCount = determinePlayer(file, lineCount, tempFile)

if args.verbose > 3:

print("line = " + str(lineCount))

# skip until courtiers

lineCount, Done = readUntil(file, '\tcharacter=

', lineCount, tempFile)

if Done:

if args.verbose > 3:

print("line = " + str(lineCount))

print("character not found")

return -1

lineCount, Done = readNextBracket(file, lineCount, tempFile)

if(startCourtier):

lineCount, Done = readUntil(file, '\t\t'+startCourtier+'=

', lineCount, tempFile)

keepCourtier = True

oldCourtierNum = None

courtierNum = None

while keepCourtier:

oldCourtierNum = copy.copy(courtierNum)

lineCount, courtierNum, keepCourtier = processCourtier(file, player, lineCount, tempFile)

if args.verbose > 3:

print("line = " + str(lineCount))

lineCount, Done = readUntil(file, '\trelation=

', lineCount, tempFile) # read the next bracket

lineCount, Done = readNextBracket(file, lineCount, tempFile)

if Done:

if args.verbose > 3:

print("line = " + str(lineCount))

print("relation not found")

return -1

lineCount = removeCourtierRelations(file, courtierNum, lineCount, tempFile)

if args.verbose > 3:

print("line = " + str(lineCount))

while not Done:

lineCount, Done = removeCourtierAmbition(file, courtierNum, lineCount, tempFile)

if args.verbose > 3:

print("line = " + str(lineCount))

return 0, courtierNum, oldCourtierNum

args = setupArgs()

firstFile = args.fileName

mainRes = 0

counter = 0

oldCourtier = None

while not mainRes:

args = setupArgs()

args.outFile = firstFile[0:-4]+'_'+str(counter)+firstFile[-4:]

mainRes, courtierNum, oldCourtier = main(oldCourtier)

counter +=1

print('iteration: '+str(counter)+'\tremoved courtier: '+courtierNum)

if args.delete:

if os.path.exists(args.location+firstFile[:-4]+'_'+str(counter-2)+firstFile[-4:]):