Created
April 5, 2018 01:11
-
-
Save alextd/d8c9ce2f51416d2bd9929b4800aab48f to your computer and use it in GitHub Desktop.
RimWorld mod utility to create a sample translation file for strings in def xmls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import re | |
import shutil | |
#usage: | |
#python translateDefs.py ModFolder | |
# | |
# Reads Def Xmls for reportString | |
# Creates sample XML files for translation in Languages/Sample/DefInjected/*Def/ | |
# Replaces those files with each run | |
# | |
# TODO: | |
# label and desc | |
# Handle tags in lists | |
xmlBegin = """<?xml version="1.0" encoding="utf-8" ?> | |
<LanguageData> | |
""" | |
xmlEnd = """ | |
</LanguageData>""" | |
def fuckinMakeTheDirectoryOkay(outfilename): | |
try: | |
os.makedirs(os.path.dirname(outfilename)) | |
except: | |
pass | |
endDefTagRE = re.compile(r"</[^<>]*Def>") | |
defTagRE = re.compile(r"<([^/<>]*Def)>") | |
defNameRE = re.compile(r"<defName>([^<>]*)</defName>") | |
reportStringRE = re.compile(r"<reportString>([^<>]*)</reportString>") | |
def doDefInjected(dir): | |
if not os.path.isdir(dir): | |
print "'" + dir + "' is not a directory" | |
sys.exit(0) | |
defsDir = os.path.join(dir, "Defs"); | |
defs = {} | |
for dname, dirs, files in os.walk(defsDir): | |
for fname in files: | |
if fname.split(os.extsep)[-1] != "xml": | |
continue | |
print " --- FILE: " + fname | |
fpath = os.path.join(dname, fname) | |
with open(fpath, 'r+') as f: | |
curDef = "" | |
curDefName = "" | |
for line in f: | |
match = re.search(defTagRE, line) | |
if match is not None: | |
curDef = match.group(1) | |
if curDef not in defs: | |
defs[curDef] = [] | |
continue | |
match = re.search(defNameRE, line) | |
if match is not None: | |
if curDef is not "": | |
curDefName = match.group(1) | |
continue | |
match = re.search(reportStringRE, line) | |
if match is not None: | |
if curDefName is not "": | |
defs[curDef].append((curDefName, match.group(1))) | |
continue | |
match = re.search(endDefTagRE, line) | |
if match is not None: | |
curDef = "" | |
continue | |
defInjectedDir = os.path.join(dir, "Languages\Sample\DefInjected") | |
for defTag, list in defs.iteritems(): | |
defTagDir = os.path.join(defInjectedDir, defTag) | |
defTagXml = os.path.join(defTagDir, "Sample"+defTag+"Injected.xml") | |
fuckinMakeTheDirectoryOkay(defTagXml) | |
print " --- Writing Sample to " + defTagXml | |
with open(defTagXml, "w") as deftagfile: | |
deftagfile.write(xmlBegin) | |
for defName, reportString in list: | |
deftagfile.write(" <{0}.reportString>{1}</{0}.reportString>\n" .format (defName, reportString)) | |
deftagfile.write(xmlEnd) | |
if len(sys.argv) == 1: | |
print "need dir as argv" | |
sys.exit(0) | |
doDefInjected(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment