Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions UnityPy/export/ShaderConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import re
from itertools import groupby
from typing import TYPE_CHECKING, List, Optional, Tuple
from typing import TYPE_CHECKING, List, Optional, Tuple, TypeVar, Union

from ..enums import (
PassType,
Expand Down Expand Up @@ -35,6 +35,8 @@
///////////////////////////////////////////
"""[1:]

T = TypeVar("T")


def export_shader(m_Shader: Shader) -> str:
if m_Shader.m_SubProgramBlob: # 5.3 - 5.4
Expand All @@ -58,21 +60,27 @@ def ConvertSerializedShader(m_Shader: Shader) -> str:

platformNumber = len(m_Shader.platforms)
compressed_blob = bytes(m_Shader.compressedBlob)

def get_entry(array: Union[List[T], List[List[T]]], index: int) -> T:
item = array[index]
if isinstance(item, List):
return item[0]
return item

for i in range(platformNumber):
if i >= len(m_Shader.compressedLengths) or i >= len(
m_Shader.decompressedLengths
):
# m_Shader.platforms shouldn't be longer than m_shader.[de]compressedLengths, but it is
break
compressedSize = m_Shader.compressedLengths[i]
decompressedSize = m_Shader.decompressedLengths[i]

compressedBytes = compressed_blob[
int(m_Shader.offsets[i][0]) : int(m_Shader.offsets[i][0])
+ compressedSize[0]
]
compressedSize = get_entry(m_Shader.compressedLengths, i)
decompressedSize = get_entry(m_Shader.decompressedLengths, i)
offset = get_entry(m_Shader.offsets, i)

compressedBytes = compressed_blob[offset : offset + compressedSize]
decompressedBytes = CompressionHelper.decompress_lz4(
compressedBytes, decompressedSize[0]
compressedBytes, decompressedSize
)

shaderPrograms.append(
Expand Down Expand Up @@ -654,7 +662,10 @@ def Export(self) -> str:

sb.append("}\n")

if hasattr(self, "m_LocalKeywords") and len(self.m_LocalKeywords) > 0:
if (
getattr(self, "m_LocalKeywords") is not None
and len(self.m_LocalKeywords) > 0
):
sb.append("Local Keywords { ")
for keyword in self.m_LocalKeywords:
sb.append('"{0}" '.format(keyword))
Expand Down
4 changes: 2 additions & 2 deletions UnityPy/tools/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def exportMesh(obj: Mesh, fp: str, extension=".obj") -> List[int]:
return [(obj.assets_file, obj.path_id)]


def exporShader(obj: Shader, fp: str, extension=".txt") -> List[int]:
def exportShader(obj: Shader, fp: str, extension=".txt") -> List[int]:
if not extension:
extension = ".txt"
with open(f"{fp}{extension}", "wt", encoding="utf8", newline="") as f:
Expand Down Expand Up @@ -295,7 +295,7 @@ def exportGameObject(obj: GameObject, fp: str, extension: str = "") -> List[int]
ClassIDType.Font: exportFont,
ClassIDType.Mesh: exportMesh,
ClassIDType.MonoBehaviour: exportMonoBehaviour,
ClassIDType.Shader: exporShader,
ClassIDType.Shader: exportShader,
ClassIDType.TextAsset: exportTextAsset,
ClassIDType.Texture2D: exportTexture2D,
}
Expand Down