Skip to content

Commit 5fa465a

Browse files
committed
Sprite fix, test and auto deploy
1 parent 0182226 commit 5fa465a

4 files changed

Lines changed: 94 additions & 29 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Test and Publish
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
deploy:
7+
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
os: [
12+
"ubuntu-latest",
13+
"macOS-latest",
14+
"windows-latest"
15+
]
16+
python-version: [
17+
"3.8",
18+
"3.7",
19+
"3.6",
20+
]
21+
architecture: ["x86", "x64"]
22+
23+
timeout-minutes: 30
24+
25+
runs-on: ${{ matrix.os }}
26+
name: ${{ matrix.os }} ${{ matrix.architecture }} - ${{ matrix.python-version }}
27+
28+
steps:
29+
- uses: actions/checkout@v2
30+
31+
- name: Set up Python ${{ matrix.python-version }}
32+
uses: actions/setup-python@v1
33+
with:
34+
python-version: ${{ matrix.python-version }}
35+
36+
- name: Install dependencies
37+
run: |
38+
python -m pip install --upgrade pip
39+
pip install --upgrade pip wheel setuptools twine
40+
pip install Cython pytest pillow
41+
42+
- name: Install module
43+
run: |
44+
python setup.py install
45+
46+
- name: Test with pytest
47+
run: |
48+
pytest -v -s
49+
50+
- name: Build and publish
51+
if: success() && runner.os != 'Linux' && github.event_name == 'push'
52+
env:
53+
TWINE_USERNAME: __token__
54+
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
55+
run: |
56+
python setup.py sdist bdist_wheel
57+
twine upload dist/* --skip-existing

‎UnityPy/export/SpriteHelper.py‎

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_image(sprite, texture, alpha_texture) -> Image:
3939
def get_image_from_sprite(m_Sprite) -> Image:
4040
atlas = None
4141
if m_Sprite.m_SpriteAtlas:
42-
atlas = m_Sprite.m_SpriteAtlas
42+
atlas = m_Sprite.m_SpriteAtlas.read()
4343
elif m_Sprite.m_AtlasTags:
4444
# looks like the direct pointer is empty, let's try to find the Atlas via its name
4545
for obj in m_Sprite.assets_file.objects.values():
@@ -84,44 +84,37 @@ def get_image_from_sprite(m_Sprite) -> Image:
8484
# Tight
8585

8686
# create mask to keep only the polygon
87-
points = get_points(m_Sprite.m_RD)
88-
points = normalize_points(points, m_Sprite.m_PixelsToUnits)
8987
mask = Image.new("1", sprite_image.size, color=0)
9088
draw = ImageDraw.ImageDraw(mask)
91-
draw.polygon(points, fill=1)
89+
for triangle in get_triangles(m_Sprite):
90+
draw.polygon(triangle, fill=1)
9291

9392
# apply the mask
94-
empty_img = Image.new(sprite_image.mode, sprite_image.size, color=0)
95-
sprite_image = Image.composite(sprite_image, empty_img, mask)
93+
sprite_image.putalpha(mask)
9694

9795
return sprite_image.transpose(Image.FLIP_TOP_BOTTOM)
9896

9997

100-
def normalize_points(points, mod):
101-
min_x = min(p.X for p in points)
102-
min_y = min(p.Y for p in points)
103-
return [
104-
((p.X - min_x) * mod, (p.Y - min_y) * mod)
105-
for p in points
106-
]
107-
108-
109-
def get_points(m_RD):
98+
def get_triangles(m_Sprite):
11099
"""
111-
returns the vertices of the sprite
100+
returns the triangles of the sprite polygon
112101
"""
102+
m_RD = m_Sprite.m_RD
103+
104+
# read the raw points
105+
points = []
113106
if hasattr(m_RD, "vertices"): # 5.6 down
114107
vertices = [v.pos for v in m_RD.vertices]
115-
# points = [
116-
# vertices[index]
117-
# for index in range(m_RD.indices)
118-
# ]
108+
points = [
109+
vertices[index]
110+
for index in range(m_RD.indices)
111+
]
119112
else: # 5.6 and up
120113
m_Channel = m_RD.m_VertexData.m_Channels[0] # kShaderChannelVertex
121114
m_Stream = m_RD.m_VertexData.m_Streams[m_Channel.stream]
122115

123116
vertexReader = EndianBinaryReader(m_RD.m_VertexData.m_DataSize, endian='<')
124-
# indexReader = EndianBinaryReader(m_RD.m_IndexBuffer, endian='<')
117+
indexReader = EndianBinaryReader(m_RD.m_IndexBuffer, endian='<')
125118

126119
for subMesh in m_RD.m_SubMeshes:
127120
vertexReader.Position = m_Stream.offset + subMesh.firstVertex * m_Stream.stride + m_Channel.offset
@@ -131,9 +124,24 @@ def get_points(m_RD):
131124
vertices.append(vertexReader.read_vector3())
132125
vertexReader.Position = vertexReader.Position + m_Stream.stride - 12
133126

134-
# indexReader.Position = subMesh.firstByte
127+
indexReader.Position = subMesh.firstByte
135128

136-
# for _ in range(subMesh.indexCount):
137-
# points.append(vertices[indexReader.read_u_short() - subMesh.firstVertex])
129+
for _ in range(subMesh.indexCount):
130+
points.append(vertices[indexReader.read_u_short() - subMesh.firstVertex])
138131

139-
return vertices
132+
# normalize the points
133+
# shift the whole point matrix into the positive space
134+
# multiply them with a factor to scale them to the image
135+
min_x = min(p.X for p in points)
136+
min_y = min(p.Y for p in points)
137+
factor = m_Sprite.m_PixelsToUnits
138+
points = [
139+
((p.X - min_x) * factor, (p.Y - min_y) * factor)
140+
for p in points
141+
]
142+
143+
# generate triangles from the given points
144+
return [
145+
points[i:i+3]
146+
for i in range(0, len(points), 3)
147+
]

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
setuptools.setup(
77
name="UnityPy",
88
packages=setuptools.find_packages(),
9-
version="1.2.6.9",
9+
version="1.2.6.10",
1010
author="K0lb3",
1111
description="A pythonic port of AssetStudio by Perfare",
1212
long_description=long_description,

‎tests/test_main.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_texture2d():
2020
for asset in am.assets.values():
2121
for obj in asset.objects.values():
2222
if obj.type == "Texture2D":
23-
obj.read().image
23+
obj.read().image.save("test.png")
2424

2525
def test_sprite():
2626
import UnityPy
@@ -29,4 +29,4 @@ def test_sprite():
2929
for asset in am.assets.values():
3030
for obj in asset.objects.values():
3131
if obj.type == "Sprite":
32-
obj.read().image
32+
obj.read().image.save("test.png")

0 commit comments

Comments
 (0)