forked from FlandreCirno/AzurLaneWikiUtilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.py
262 lines (239 loc) · 9.69 KB
/
Memory.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
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
# -*- coding: utf-8 -*-
import re, os
from slpp import slpp
import util
def getMemoryGroup():
return util.parseDataFile('memory_group')
def getMemoryTemplate():
return util.parseDataFile('memory_template')
def getWorldGroup():
return util.parseDataFile('world_collection_record_group')
def getWorldTemplate():
return util.parseDataFile('world_collection_record_template')
def getStory(filename, type = 1):
if type == 1:
with open(os.path.join(util.DataDirectory, 'gamecfg', 'story', filename), 'r', encoding='utf-8') as f:
content = f.read()
content = re.match(r".*?(\{.*\})" ,content, flags = re.DOTALL)[1]
output = slpp.decode(content)
return output
elif type == 2:
with open(os.path.join(util.DataDirectory, 'gamecfg', 'dungeon', filename), 'r', encoding='utf-8') as f:
content = f.read()
content = re.match(r".*?(\{.*\})" ,content, flags = re.DOTALL)[1]
dungeon = slpp.decode(content)
storylist = []
if 'beginStoy' in dungeon.keys():
storylist.append(dungeon['beginStoy'])
stage = dungeon['stages']
for wave in stage[0]['waves']:
if wave['triggerType'] == 3:
storylist.append(wave['triggerParams']['id'])
output = []
for story in storylist:
s = getStory(story.lower() + '.lua')
output.append(s)
return output
def getShipGroup():
return util.parseDataFile('ship_data_group')
def getShipStatistics():
return util.parseDataFile('ship_data_statistics')
def getShipTemplate():
return util.parseDataFile('ship_data_template')
def getNameCode():
content = util.parseDataFile('name_code')
if isinstance(content, dict):
content = content.values()
output = {}
for i in content:
output[i['id']] = i['name']
return output
def getShipSkinTemplate():
return util.parseDataFile('ship_skin_template',
filePath = os.path.join('sharecfg', 'ship_skin_template_sublist'), mode = 1)
def getShipName(skinID, skinTemplate, shipStatistics, shipTemplate):
shipGroup = skinTemplate[skinID]['ship_group']
for k, v in skinTemplate.items():
if shipGroup == v['ship_group'] and v['group_index'] == 0:
return v['name']
return skinTemplate[skinID]['name']
def getGroup(memoryGroup, worldGroup):
group = []
for k, v in memoryGroup.items():
group.append({'memories': v['memories'], 'title': v['title']})
for k, v in worldGroup.items():
group.append({'memories': v['child'], 'title': v['name_abbreviate']})
return group
def mergeMemoryTemplate(memoryTemplate, worldTemplate):
for k, v in worldTemplate.items():
memoryTemplate[k] = {'id': v['id'], 'type': 1, 'title': v['name'], 'story': v['story']}
def getMemory(memoryID, memoryTemplate):
output = {}
for k, v in memoryTemplate.items():
if v['id'] == memoryID:
output['title'] = v['title']
story = v['story'].lower()
output['type'] = v['type']
try:
output['story'] = getStory(story + '.lua', output['type'])
except:
print(output)
raise
return None
return output
def sanitizeMemory(memory, skinTemplate, shipStatistics, shipTemplate, nameCode):
output = {'title': parseNameCode(memory['title'], nameCode, AF = True), 'memory':[]}
if isinstance(memory['story'], list):
tempMemory = {'title': memory['title']}
for story in memory['story']:
tempMemory['story'] = story
segMemory = sanitizeMemory(tempMemory, skinTemplate, shipStatistics, shipTemplate, nameCode)
for m in segMemory['memory']:
output['memory'].append(m)
output['memory'].append({'type': 'break', 'words': None, 'name': None, 'actor': None, 'color': None, 'option':None})
output['memory'] = output['memory'][:-1]
return output
scripts = memory['story']['scripts']
if isinstance(scripts, dict):
scripts = scripts.values()
for script in scripts:
words = ''
type = None
name = ''
actor = None
color = None
option = None
if isinstance(script, dict) and 'sequence' in script.keys():
if isinstance(script['sequence'], dict):
script['sequence'] = script['sequence'].values()
for s in script['sequence']:
words += s[0] + '\n'
words = words[:-1]
type = 'sequence'
if len(words) == 0:
continue
elif isinstance(script, dict) and 'say' in script.keys():
words = script['say']
if 'actor' in script.keys():
actor = script['actor']
else:
actor = None
if 'nameColor' in script.keys():
color = script['nameColor']
else:
color = None
if 'options' in script.keys():
if not option:
option = {'options': []}
options = script['options']
if isinstance(options, dict):
options = options.values()
for o in options:
flag = ''
if 'flag' in o.keys():
flag = o['flag']
option['options'].append({'flag': flag, 'content': parseNameCode(o['content'], nameCode, AF = True)})
if 'optionFlag' in script.keys():
if not option:
option = {}
option['optionFlag'] = script['optionFlag']
if 'actorName' in script.keys():
name = script['actorName']
elif actor and actor > 0:
name = getShipName(actor, skinTemplate, shipStatistics, shipTemplate)
else:
name = ''
type = 'say'
else:
continue
words = re.sub(r'\<.*?\>', '', words)
words = parseNameCode(words, nameCode, AF = True)
name = parseNameCode(name, nameCode, AF = True)
output['memory'].append({'type': type, 'words': words, 'name': name, 'actor': actor, 'color': color, 'option': option})
return output
def buildGroup(group, skinTemplate, shipStatistics, shipTemplate, memoryTemplate, nameCode):
output = {'title': parseNameCode(group['title'], nameCode), 'memories':[]}
try:
for memoryID in group['memories']:
memory = getMemory(memoryID, memoryTemplate)
if memory:
memory = sanitizeMemory(memory, skinTemplate, shipStatistics, shipTemplate, nameCode)
else:
continue
output['memories'].append(memory)
except:
print(str(memory))
raise
return output
def parseNameCode(text, nameCode, AF = False):
def parsefunc(matchobj, nameCode = nameCode, AF = AF):
id = int(matchobj.group(1))
if id in nameCode.keys():
if AF:
return '{{AF|' + nameCode[id] + '}}'
else:
return nameCode[id]
else:
return matchobj.group(0)
return re.sub(r'\{namecode\:(\d+)\}', parsefunc, text)
def wikiPage(group):
output = '== ' + group['title'] + ' ==\n{{折叠面板|开始}}\n'
index = 1
for memory in group['memories']:
output += wikiParagraph(memory, index)
index += 1
output += '{{折叠面板|结束}}\n'
return output.replace('\\n', '\n')
def wikiParagraph(memory, index):
output = '{{折叠面板|标题=' + memory['title'] + '|选项=' + str(index) + '|主框=1|样式=primary|展开=否}}\n'
lastActor = None
for slide in memory['memory']:
output += wikiSlide(slide, lastActor)
lastActor = slide['name']
output += '{{折叠面板|内容结束}}\n\n'
return output
def wikiSlide(slide, lastActor):
output = ''
if slide['type'] == 'break':
return '<br>\n'
if slide['name'] == lastActor:
name = None
else:
name = slide['name']
if name:
if slide['color']:
output += '<span style="color:' + slide['color'] + ';">' + name + ':</span><br>\n'
else:
if len(name) > 0:
output += name + ':'
output += '<br>\n'
if slide['option'] and 'optionFlag' in slide['option'].keys():
output += "'''''<span style=" + '"color:black;"' + ">(选择项" + str(slide['option']['optionFlag']) + ")</span>'''''"
output += nowiki(slide['words']) + '<br>\n'
if slide['option'] and 'options' in slide['option'].keys():
for option in slide['option']['options']:
output += "'''''<span style=" + '"color:black;"' + ">选择项" + str(option['flag']) + ":"
output += nowiki(option['content']) + "</span>'''''<br>\n"
return output
def nowiki(text):
return re.sub(r'~~~~', '<nowiki>~~~~</nowiki>', text)
def wikiGenerate():
nameCode = getNameCode()
memoryGroup = getMemoryGroup()
memoryTemplate = getMemoryTemplate()
worldGroup = getWorldGroup()
worldTemplate = getWorldTemplate()
shipGroup = getShipGroup()
shipStatistics = getShipStatistics()
shipTemplate = getShipTemplate()
skinTemplate = getShipSkinTemplate()
groups = getGroup(memoryGroup, worldGroup)
mergeMemoryTemplate(memoryTemplate, worldTemplate)
groupsbuilt = []
for v in groups:
groupsbuilt.append(buildGroup(v, skinTemplate, shipStatistics, shipTemplate, memoryTemplate, nameCode))
for group in groupsbuilt:
with open(os.path.join(util.WikiDirectory, 'memories', group['title'].replace(':', '') + '.txt'), 'w+', encoding='utf-8') as f:
f.write(wikiPage(group))
if __name__ == "__main__":
wikiGenerate()