Skip to content
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

Snowyegret23 fix issue monobehaviour from assembly.py #273

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
53 changes: 28 additions & 25 deletions examples/MonoBehaviourFromAssembly/monobehaviour_from_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# https://github.com/K0lb3/TypeTreeGenerator
# requires .NET 5.0 SDK
# https://dotnet.microsoft.com/download/dotnet/5.0
# Requires .NET 3.1
# https://dotnet.microsoft.com/download/dotnet/3.1/runtime?cid=getdotnetcore
#
# pythonnet 2 and TypeTreeGenerator created with net4.8 works on Windows,
# so it can do without pythonnet_init,
Expand Down Expand Up @@ -45,32 +47,34 @@ def export_monobehaviours(asset_path: str, trees: dict):
except:
continue
for obj in env.objects:
if obj.type == "MonoBehaviour":
d = obj.read()
if obj.serialized_type and obj.serialized_type.node:
tree = obj.read_typetree()
else:
if not d.m_Script:
continue
if obj.type.name == "MonoBehaviour":
try:
if obj.serialized_type and obj.serialized_type.node:
tree = obj.read_typetree()
else:
if not obj.m_Script:
continue
# RIP, no referenced script
# can only dump raw
script = d.m_Script.read()
# on-demand solution without already dumped tree
#nodes = generate_tree(
# g, script.m_AssemblyName, script.m_ClassName, script.m_Namespace
#)
if script.m_ClassName not in trees:
# class not found in known trees,
# might have to add the classes of the other dlls
continue
nodes = FakeNode(**trees[script.m_ClassName])
tree = obj.read_typetree(nodes)

# save tree as json whereever you like
script = obj.m_Script.read()
# on-demand solution without already dumped tree
#nodes = generate_tree(
# g, script.m_AssemblyName, script.m_ClassName, script.m_Namespace
#)
if script.m_ClassName not in trees:
continue
# class not found in known trees,
# might have to add the classes of the other dlls
nodes = FakeNode(**trees[script.m_ClassName])
tree = obj.read_typetree(nodes)

# save tree as json whereever you like
except:
continue



def dump_assembly_trees(dll_folder: str, out_path: str):
def dump_assembly_trees(dll_folder: str, out_path: str = "typetrees.json"):
# init pythonnet, so that it uses the correct .net for the generator
pythonnet_init()
# create generator
Expand All @@ -81,9 +85,8 @@ def dump_assembly_trees(dll_folder: str, out_path: str):
# it's faster and easier overall to just fetch all at once
trees = generate_tree(g, "Assembly-CSharp.dll", "", "")

if out_path:
with open("typetrees.json", "wt", encoding="utf8") as f:
json.dump(trees, f, ensure_ascii=False)
with open(out_path, "wt", encoding="utf8") as f:
json.dump(trees, f, ensure_ascii=False)
return trees


Expand All @@ -94,7 +97,7 @@ def pythonnet_init():
from clr_loader import get_coreclr
from pythonnet import set_runtime

rt = get_coreclr(
rt = get_coreclr( runtime_config=
os.path.join(TYPETREE_GENERATOR_PATH, "TypeTreeGenerator.runtimeconfig.json")
)
set_runtime(rt)
Expand Down
Loading