Sc2ad

Unity dumper script used for dumping scene hierarchies

Mar 17th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include "../include/main.hpp"
  2.  
  3. FILE* fp;
  4.  
  5. #define PATH "/sdcard/Android/data/com.beatgames.beatsaber/files/logdump.txt"
  6.  
  7. #define RUN_METHOD0(outp, inp, name) \
  8. do { \
  9.     if (!il2cpp_utils::RunMethod(outp, inp, name)) { \
  10.         log(CRITICAL, "Failed to run method!"); \
  11.         std::abort(); \
  12.     } \
  13. } while(0);
  14.  
  15. #define RUN_METHOD1(outp, inp, name, arg1) \
  16. do { \
  17.     if (!il2cpp_utils::RunMethod(outp, inp, name, arg1)) { \
  18.         log(CRITICAL, "Failed to run method!"); \
  19.         std::abort(); \
  20.     } \
  21. } while(0);
  22.  
  23. void write_info(std::string str) {
  24.     fwrite(str.data(), str.length() + 1, 1, fp);
  25. }
  26.  
  27. void DumpParents(std::string prefix, Il2CppObject* parentTransform) {
  28.     // Get children
  29.     int childCount;
  30.     RUN_METHOD0(&childCount, parentTransform, "get_childCount");
  31.     Il2CppString* parentName;
  32.     RUN_METHOD0(&parentName, parentTransform, "get_name");
  33.     write_info(prefix + to_utf8(csstrtostr(parentName)) + " Children: " + std::to_string(childCount));
  34.     Il2CppObject* child;
  35.  
  36.     for (int i = 0; i < childCount; i++) {
  37.         RUN_METHOD1(&child, parentTransform, "GetChild", i);
  38.         if (child) {
  39.             DumpParents(prefix + "-", child);
  40.         }
  41.     }
  42. }
  43.  
  44. // Iterates over all GameObjects in the scene, dumps information about them to file
  45. void DumpAll() {
  46.     fp = fopen(PATH, "w");
  47.  
  48.     Il2CppArray* arr;
  49.     static auto typeObject = il2cpp_utils::GetSystemType("UnityEngine", "GameObject");
  50.     RUN_METHOD1(&arr, il2cpp_utils::GetClassFromName("UnityEngine", "Resources"), "FindObjectsOfTypeAll", typeObject);
  51.     Il2CppObject* transform;
  52.     for (il2cpp_array_size_t i = 0; i < arr->bounds->length; i++) {
  53.         auto go = il2cpp_array_get(arr, Il2CppObject*, 0);
  54.         if (go != nullptr) {
  55.             RUN_METHOD0(&transform, go, "get_transform");
  56.             if (transform != nullptr) {
  57.                 RUN_METHOD0(&transform, transform, "get_parent");
  58.                 if (transform != nullptr) {
  59.                     DumpParents("", transform);
  60.                 }
  61.             } else {
  62.                 Il2CppString* goName;
  63.                 RUN_METHOD0(&goName, go, "get_name");
  64.                 write_info(to_utf8(csstrtostr(goName)) + " has no transform!");
  65.             }
  66.         } else {
  67.             write_info("GameObject is null!");
  68.         }
  69.     }
  70.  
  71.     fclose(fp);
  72. }
Add Comment
Please, Sign In to add comment