-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_characters.py
193 lines (176 loc) · 7.05 KB
/
list_characters.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
# Copyright (C) 2012 PiB <pixelbound@gmail.com>
#
# EQuilibre is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import struct
import sys
import os
import re
from s3d import S3DArchive
from wld import WLDData
class WLDActor(object):
DefaultSkin = "00"
def __init__(self, name, palette):
self.name = name
self.skins = {}
self.pieces = []
self.palette = palette
def newSkin(self, palName):
skin = WLDModelSkin(palName)
self.skins[palName] = skin
if palName != WLDActor.DefaultSkin:
defaultSkin = self.skins["00"]
for part in defaultSkin.parts:
skin.parts.append(part)
return skin
def pieceByName(self, pieceName):
for piece in self.pieces:
if piece.name == pieceName:
return piece
class WLDModelPiece(object):
def __init__(self, pieceID, pieceName):
self.ID = pieceID
self.name = pieceName
self.skins = set()
class WLDModelSkin(object):
def __init__(self, palName):
self.palName = palName
self.parts = []
self.materials = {}
def replacePart(self, meshDef, actorName, partName):
for i in range(0, len(self.parts)):
part = self.parts[i]
oldActorName, oldPartName, oldPalName = explodeMeshName(part.name)
if partName == oldPartName:
self.parts[i] = meshDef
return
# No base part found, just append it.
self.parts.append(meshDef)
matNameExp = re.compile("^\w{5}\d{4}_MDF$");
meshNameExp = re.compile("^(\w{3})(.*)(\d{2})_DMSPRITEDEF$");
def explodeMaterialName(defName):
# e.g. defName == 'ORCCH0201_MDF'
# 'ORC' : character
# 'CH' : piece (part 1)
# '02' : palette ID
# '01' : piece (part 2)
if matNameExp.match(defName):
return defName[0:3], defName[3:5] + defName[7:9], defName[5:7]
else:
return None, None, None
def explodeMeshName(defName):
# e.g. defName == 'ELEHE00_DMSPRITEDEF'
# 'ELE' : character
# 'HE' : mesh
# '00' : skin ID
m = meshNameExp.match(defName)
if m:
return m.group(1), m.group(2), m.group(3)
else:
return None, None, None
def combineMaterialName(actorName, palName, pieceName):
return actorName + pieceName[0:2] + palName + pieceName[2:4] + "_MDF"
def listCharacters(s3dPath, wldName):
with S3DArchive(s3dPath) as a:
wld = WLDData.fromArchive(a, wldName)
actors = {}
importCharacters(wld, actors)
importCharacterPalettes(wld, actors)
dumpCharacters(wld, actors)
def dumpCharacters(wld, actors):
for actorName in sorted(actors):
actor = actors[actorName]
print("%s (%d pieces, %d skins)" % (actorName, len(actor.pieces), len(actor.skins)))
for piece in actor.pieces:
matName = actor.palette.Textures[piece.ID].name
actorName, pieceName, palName = explodeMaterialName(matName)
palNames = sorted(piece.skins)
print("+-- Piece %02d -> %s [%s]" % (piece.ID, piece.name, ", ".join(palNames)))
for skinName in sorted(actor.skins):
skin = actor.skins[skinName]
meshNames = sorted(mesh.name.replace("_DMSPRITEDEF", "") for mesh in skin.parts)
print("+-- Skin %s -> [%s]" % (skinName, ", ".join(meshNames)))
def findMainMesh(actorDef, actorName):
if not actorDef.models:
raise Exception("Actor '%s' has no mesh." % actorName)
mainMeshName = "%s_DMSPRITEDEF" % actorName
for meshRef in actorDef.listModels():
mesh = meshRef.Mesh
if mesh and (mesh.name == mainMeshName) and hasattr(mesh, "Fragment1"):
palette = mesh.Fragment1
if not palette:
raise Exception("Mesh '%s' has no palette." % firstMesh.name)
return mesh, palette
return None, None
def importCharacters(wld, actors):
for actorDef in wld.fragmentsByType(0x14):
actorName = actorDef.name.replace("_ACTORDEF", "")
mainMesh, mainPalette = findMainMesh(actorDef, actorName)
if not mainMesh:
print("Warning: could not find main mesh for actor '%s'." % actorName)
continue
actor = WLDActor(actorName, mainPalette)
actors[actorName] = actor
for i, matDef in enumerate(mainPalette.Textures):
actorName2, pieceName, palName = explodeMaterialName(matDef.name)
if not actorName2:
print("Warning: could not split material name '%s' of actor %s, piece %d" % (matDef.name, actorName, i))
pieceName = matDef.name.replace("_MDF","")
actor.pieces.append(WLDModelPiece(i, pieceName))
skin = actor.newSkin(WLDActor.DefaultSkin)
for model in actorDef.listModels():
meshDef = model.Mesh
skin.parts.append(meshDef)
for i in range(0, meshDef.PolygonTexCount):
pieceID = meshDef.polygonsByTex[i][1]
piece = actor.pieces[pieceID]
piece.skins.add(WLDActor.DefaultSkin)
def importCharacterPalettes(wld, actors):
# look for alternate materials
for matDef in wld.fragmentsByType(0x30):
actorName, pieceName, palName = explodeMaterialName(matDef.name)
if not actorName in actors:
continue
actor = actors[actorName]
try:
skin = actor.skins[palName]
except KeyError:
skin = actor.newSkin(palName)
piece = actor.pieceByName(pieceName)
if piece:
piece.skins.add(palName)
# look for alternate meshes (e.g. heads)
for meshDef in wld.fragmentsByType(0x36):
actorName, partName, palName = explodeMeshName(meshDef.name)
if not actorName in actors:
continue
actor = actors[actorName]
try:
skin = actor.skins[palName]
except KeyError:
skin = actor.newSkin(palName)
skin.replacePart(meshDef, actorName, partName)
for i in range(0, meshDef.PolygonTexCount):
pieceID = meshDef.polygonsByTex[i][1]
piece = actor.pieces[pieceID]
piece.skins.add(palName)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage: %s <.S3D file> [WLD name]" % sys.argv[0])
s3dPath = sys.argv[1]
if len(sys.argv) < 3:
wldName = os.path.basename(s3dPath).replace(".s3d", ".wld")
else:
wldName = sys.argv[2]
listCharacters(s3dPath, wldName)