Repository navigation
Use class database MonoBehaviour header for TypeTreeGenerator nodes #403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
1915rising
wants to merge
2
commits into
K0lb3:master
Choose a base branch
from
1915rising:fix/388-monobehaviour-generator-header
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| """Regression tests for #388.""" | ||
|
|
||
| import struct | ||
| from types import SimpleNamespace | ||
|
|
||
| from UnityPy.enums import ClassIDType | ||
| from UnityPy.files.ObjectReader import ObjectReader | ||
| from UnityPy.helpers.Tpk import get_typetree_node | ||
| from UnityPy.helpers.TypeTreeHelper import read_typetree, write_typetree | ||
| from UnityPy.helpers.TypeTreeNode import TypeTreeNode | ||
| from UnityPy.helpers.UnityVersion import UnityVersion | ||
| from UnityPy.streams import EndianBinaryReader, EndianBinaryWriter | ||
|
|
||
| ALIGN = 0x4000 | ||
| VERSION = "2022.3.0f1" | ||
| SCRIPT_PATH_ID = 0x1122334455667788 | ||
|
|
||
|
|
||
| def class_db_node() -> TypeTreeNode: | ||
| return get_typetree_node(ClassIDType.MonoBehaviour, UnityVersion.from_str(VERSION)) | ||
|
|
||
|
|
||
| def clone_node(node: TypeTreeNode) -> TypeTreeNode: | ||
| return TypeTreeNode( | ||
| m_Level=node.m_Level, | ||
| m_Type=node.m_Type, | ||
| m_Name=node.m_Name, | ||
| m_ByteSize=node.m_ByteSize, | ||
| m_Version=node.m_Version, | ||
| m_Children=[clone_node(child) for child in node.m_Children], | ||
| m_TypeFlags=node.m_TypeFlags, | ||
| m_MetaFlag=node.m_MetaFlag, | ||
| m_Index=node.m_Index, | ||
| ) | ||
|
|
||
|
|
||
| def make_generated_node() -> TypeTreeNode: | ||
| """The class-db node with the generator's align flags, plus the script's own fields.""" | ||
| node = clone_node(class_db_node()) | ||
| for child in node.m_Children: | ||
| if child.m_Name in ("m_GameObject", "m_Script", "m_Name"): | ||
| child.m_MetaFlag = (child.m_MetaFlag or 0) | ALIGN | ||
| elif child.m_Name == "m_Enabled": | ||
| child.m_MetaFlag = (child.m_MetaFlag or 0) & ~ALIGN | ||
| for typ, name, flag in (("int", "count", 0), ("bool", "flag", ALIGN), ("int", "after", 0)): | ||
| node.m_Children.append( | ||
| TypeTreeNode( | ||
| m_Level=1, m_Type=typ, m_Name=name, m_ByteSize=0, m_Version=0, m_Children=[], m_MetaFlag=flag | ||
| ) | ||
| ) | ||
| return node | ||
|
|
||
|
|
||
| def aligned_string(value: str) -> bytes: | ||
| data = value.encode() | ||
| return struct.pack("<i", len(data)) + data + bytes(-len(data) % 4) | ||
|
|
||
|
|
||
| # a Sample MonoBehaviour as Unity serializes it: | ||
| # m_Enabled is followed by 3 bytes of padding | ||
| SAMPLE_DATA = ( | ||
| struct.pack("<iq", 0, 1) # m_GameObject | ||
| + struct.pack("<B3x", 1) # m_Enabled | ||
| + struct.pack("<iq", 0, SCRIPT_PATH_ID) # m_Script | ||
| + aligned_string("sample") # m_Name | ||
| + struct.pack("<i", 5) # count | ||
| + struct.pack("<B3x", 1) # flag | ||
| + struct.pack("<i", 9) # after | ||
| ) | ||
|
|
||
|
|
||
| def make_reader(monkeypatch, generated_node: TypeTreeNode) -> ObjectReader: | ||
| script = SimpleNamespace(m_Namespace="", m_ClassName="Sample", m_AssemblyName="Game") | ||
| monobehaviour = SimpleNamespace(m_Script=SimpleNamespace(deref_parse_as_object=lambda: script)) | ||
| monkeypatch.setattr( | ||
| ObjectReader, | ||
| "parse_monobehaviour_head", | ||
| lambda self, mb_node=None: monobehaviour, | ||
| ) | ||
| generator = SimpleNamespace(get_nodes_up=lambda assembly_name, fullname: generated_node) | ||
| reader = object.__new__(ObjectReader) | ||
| reader.assets_file = SimpleNamespace( | ||
| environment=SimpleNamespace(typetree_generator=generator), | ||
| version=UnityVersion.from_str(VERSION), | ||
| ) | ||
| return reader | ||
|
|
||
|
|
||
| def header_flags(node: TypeTreeNode): | ||
| return [(child.m_Name, bool((child.m_MetaFlag or 0) & ALIGN)) for child in node.m_Children[:4]] | ||
|
|
||
|
|
||
| def test_generated_node_header_is_replaced(monkeypatch): | ||
| reader = make_reader(monkeypatch, make_generated_node()) | ||
| node = reader.generate_monobehaviour_node(class_db_node()) | ||
|
|
||
| assert header_flags(node) == [ | ||
| ("m_GameObject", False), | ||
| ("m_Enabled", True), | ||
| ("m_Script", False), | ||
| ("m_Name", False), | ||
| ] | ||
| # the script's own fields are kept, untouched | ||
| assert [child.m_Name for child in node.m_Children[4:]] == ["count", "flag", "after"] | ||
|
|
||
|
|
||
| def test_generated_node_reads_and_saves_m_script(monkeypatch): | ||
| reader = make_reader(monkeypatch, make_generated_node()) | ||
| node = reader.generate_monobehaviour_node(class_db_node()) | ||
|
|
||
| value = read_typetree(node, EndianBinaryReader(SAMPLE_DATA, "<"), as_dict=True, check_read=False) | ||
| assert value["m_Script"]["m_PathID"] == SCRIPT_PATH_ID | ||
| assert (value["count"], value["flag"], value["after"]) == (5, True, 9) | ||
|
|
||
| writer = EndianBinaryWriter(b"", "<") | ||
| write_typetree(value, node, writer) | ||
| assert writer.bytes == SAMPLE_DATA |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.