-
Notifications
You must be signed in to change notification settings - Fork 0
/
#itemDB.py
122 lines (102 loc) · 3.04 KB
/
#itemDB.py
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
# -*- coding: utf-8 -*-
import os
import codecs
import sys
import re
import datetime
from lxml import etree
def getTextID(txt):
txtgp = re.search(r"_LT\[xml.[^\.]*.([0-9]+)\]", txt)
if txtgp is None:
return ""
return int(txtgp.group(1))
dataDB = {}
targetNames = [
"itemdb",
"itemdb_etc",
"itemdb_mainequip",
"itemdb_script",
"itemdb_subequip",
"itemdb_weapon"
]
outputName = "Mabi_Item"
for targetName in targetNames:
targetTXTName = targetName
localeName = ""
dataDBText = {}
hasTXT = False
hasXML = False
fileList = os.listdir("./data/")
for fileN in fileList:
if hasXML and hasTXT:
break
txtNameMatch = re.match(targetTXTName+".([a-zA-Z]*).txt", fileN)
if txtNameMatch is not None:
targetTXTName = fileN
localeName = txtNameMatch.group(1)
hasTXT = True
continue
xmlNameMatch = re.match(targetName+".xml", fileN)
if xmlNameMatch is not None:
hasXML = True
continue
if hasTXT is False:
print("Missing "+targetTXTName+" TXT file.")
sys.exit()
if hasXML is False:
print("Missing "+targetName+" XML file.")
sys.exit()
today = datetime.datetime.now().strftime("%Y%m%d")
outdir = os.getcwd()+"/patch-"+localeName+"-"+today+"/mod/"
print("Output: " + outdir)
try:
os.makedirs(outdir)
except:
pass
#targetName.XXXXX.txt
infilename = targetTXTName
try:
fi = codecs.open("./data/" + infilename,'r', encoding='utf-16')
line = fi.readline()
fi.seek(0)
except:
fi.close()
fi = codecs.open("./data/" + infilename,'r', encoding='utf-8-sig')
for line in fi:
oline = re.match(r"([0-9]{0,8})\t(([^\r])+)\r\n", line)
if oline is not None:
dataDBText[int(oline.group(1))] = oline.group(2)
fi.close()
print(infilename + " processed.")
#targetName.xml
infilename = targetName + ".xml"
parser = etree.XMLParser(recover=True)
tree = etree.parse("./data/" + infilename, parser)
root = tree.getroot()
for ele in list(root):
ID = int(ele.attrib["ID"])
LocalNameID = getTextID(ele.attrib["Text_Name1"])
if "Text_Name0" in ele.attrib:
Name = ele.attrib["Text_Name0"]
else:
Name = ""
LocalName = dataDBText.get(LocalNameID,"")
finalName = "No." + str(ID)
if LocalName != "":
finalName = LocalName
elif Name != "":
finalName = Name
if ID in dataDB.keys():
if "Locale" in ele.attrib:
if ele.attrib["Locale"] != localeName:
continue
dataDB[ID] = finalName
print(infilename + " processed.")
dataIDs = list(dataDB.keys())
dataIDs.sort()
fo = codecs.open(outdir+outputName+".ini", 'w', encoding="utf-16")
fo.write("["+outputName+"]\r\n")
for key in dataIDs:
fo.write(str(key)+"="+dataDB[key]+"\r\n")
fo.close()
print(outputName + ".ini generated.")