Skip to content
Draft
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
43 changes: 22 additions & 21 deletions source/GenerateTypeTreeTransfer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GenerateTypeTreeTransfer : public TransferBase<R, V>
GenerateTypeTreeTransfer(TypeTree<R, V> &t, TransferInstructionFlags<R, V> options, void *objectPtr, int32_t objectDataSize) :
m_TypeTree(t),
m_ActiveFather(nullptr),
m_ObjectPtr(static_cast<char *>(objectPtr)),
m_ObjectPtr((char*)(objectPtr)),
m_ObjectSize(objectDataSize)
{
this->m_Flags = options;
Expand All @@ -45,28 +45,29 @@ class GenerateTypeTreeTransfer : public TransferBase<R, V>

DEFINE_REVISION(class, GenerateTypeTreeTransfer, Revision::V5_0_0) : public TransferBase<R, V>
{
TypeTree<R, V> &m_TypeTree;
TypeTreeIterator<R, V> m_ActiveFather;
char *m_ObjectPtr;
int32_t m_ObjectSize;
char *m_ScriptingObjectPtr = nullptr;
int32_t m_ScriptingObjectSize = 0;
int32_t m_Index = 0;
int32_t m_SimulatedByteOffset = 0;
bool m_DidErrorAlignment = false;
bool m_RequireTypelessData = false;
// i'm lazy and they refactored the entire thing :)
// size grew by about 0x30 bytes
uint8_t blob[0xc8];
public:
GenerateTypeTreeTransfer(TypeTree<R, V> &t, TransferInstructionFlags<R, V> options, void *objectPtr, int32_t objectDataSize) :
m_TypeTree(t),
m_ActiveFather(nullptr, nullptr, 0),
m_ObjectPtr(static_cast<char *>(objectPtr)),
m_ObjectSize(objectDataSize)
GenerateTypeTreeTransfer(TypeTree<R, V> *t, TransferInstructionFlags<R, V> options, void *objectPtr, int32_t objectDataSize)
{
this->m_Flags = options;
memset(blob, 0, sizeof(blob));

if (t.GetData())
{
t.GetData()->SetGenerationFlags(options);
}
*reinterpret_cast<int64_t *>((char*)(this) + 0x5c) = 0x4d;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MemLabelId::String

*reinterpret_cast<int32_t *>((char*)(this) + 0x64) = -1;
*((char*)(this) + 0x50) = 0x18;
*((char*)(this) + 0x58) = 1;
*reinterpret_cast<int64_t **>((char*)(this) + 0x80) = reinterpret_cast<int64_t*>(t);
*reinterpret_cast<void **>((char*)(this) + 8) = objectPtr;
*reinterpret_cast<int64_t *>((char*)(this) + 0xa8) = objectDataSize;
*reinterpret_cast<int64_t *>((char*)(this) + 0xb0) = options;
}

virtual int64_t GetCachePosition() {
return -1;
}

virtual void Skip(uint64_t n) {
*((char*)(this) + 0xcd) = 0;
}
};
9 changes: 8 additions & 1 deletion source/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,16 +597,23 @@ DEFINE_REVISION_VARIANT(class, Object, Revision::V2022_2_0, Variant::RuntimeDev)
virtual void VirtualRedirectTransfer(class SafeBinaryRead &) = 0;
};

DEFINE_REVISION_VARIANT(class, Object, Revision::V2022_2_0, Variant::Editor)
DEFINE_REVISION_VARIANT(class, Object, Revision::V2022_3_0, Variant::Editor)
{
using RTTI = ::RTTI<R, Variant::Editor>;
using GenerateTypeTreeTransfer = ::GenerateTypeTreeTransfer<R, Variant::Editor>;
public:
virtual ~Object() = 0;
virtual void SetIsDone(bool) = 0;
virtual bool IsDone() = 0;
virtual void SetCompleted(bool) = 0;
virtual bool IsCompleted() = 0;
virtual void* GetOrCreate_THX_Proxy() = 0;
virtual void Create_THX_Proxy() = 0;
virtual void MainThreadCleanup() = 0;
virtual void AwakeFromLoad(AwakeFromLoadMode) = 0;
virtual void AwakeFromLoadThreaded() = 0;
virtual void CheckConsistency() = 0;
virtual void SetObjectReadyForUpdate() = 0;
virtual void Reset() = 0;
virtual void SmartReset() = 0;
virtual bool IsAScriptedObject() = 0;
Expand Down
4 changes: 2 additions & 2 deletions source/binary_output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ class DumpedTypeTreeWriter
#undef IF_HAS_ENUM_FLAG

public:
void Add(const RTTI* rtti, const TypeTree& tree, const TransferInstructionFlags& flags, char const* commonStringBuffer)
void Add(const RTTI* rtti, const TypeTree& tree, const TransferInstructionFlags& flags, char const* commonStringBuffer, void* object)
{
DumpedTypeTree dumpedTree{};

ConvertRTTI(rtti, dumpedTree.RTTI);

ConvertTransferInstructionFlags(flags, dumpedTree.TransferFlags);

if (!rtti->isAbstract && rtti->factory)
if (!rtti->isAbstract && rtti->factory && object)
{
const auto stringBuffer = tree.StringsBuffer();
const auto nodes = tree.Nodes();
Expand Down
1 change: 1 addition & 0 deletions source/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ inline std::optional<Variant> ExecutableNameToVariant(const std::string_view nam
constexpr std::array kExecutableVariants = {
std::make_tuple("Unity.exe", Variant::Editor),
std::make_tuple("Unity.com", Variant::Editor),
std::make_tuple("Tuanjie.exe", Variant::Editor),
};

for (const auto& [executableName, variant] : kExecutableVariants)
Expand Down
17 changes: 13 additions & 4 deletions source/dumper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class Dumper : public IDumper
if ((x.Protection & ExecutableSection::kSectionProtectionRead) == 0)
return false;

if (x.Data.size() < sizeof(RuntimeTypeArray))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: without this check, sections that are less than the size of the runtime array will loop indefinitely.

Linux has a bunch of tiny < 1KiB sections, maybe Android too under certain situations.

return false;

return true;
}))
{
Expand Down Expand Up @@ -145,6 +148,7 @@ class Dumper : public IDumper
}
}

PlatformImpl.DebugLog("Failed to find common string buffer");
return nullptr;
}
public:
Expand All @@ -169,14 +173,19 @@ class Dumper : public IDumper
TypeTreeShareableData data(label);
TypeTree tree(&data, label);

Object * object = nullptr;
if (!pRTTI->isAbstract && pRTTI->factory)
{
Object *object = pRTTI->factory(label, kCreateObjectDefault);
GenerateTypeTreeTransfer transfer(tree, flags, object, pRTTI->size);
object->VirtualRedirectTransfer(transfer);
object = pRTTI->factory(label, kCreateObjectDefault);
// "THX_Proxy" (what is this) can't be created, so it returns nothing.
// but it also is not abstract, curious!
if (object) {
GenerateTypeTreeTransfer transfer(&tree, flags, object, pRTTI->size);
object->VirtualRedirectTransfer(transfer);
}
}

Writer.Add(pRTTI, tree, flags, pTable);
Writer.Add(pRTTI, tree, flags, pTable, object);
}

PlatformImpl.DebugLog("Dumped types, now writing to file");
Expand Down
133 changes: 130 additions & 3 deletions source/linux/linux_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,134 @@
#include <cstdarg>
#include <cstring>
#include <filesystem>
#include <span>
#include <string_view>
#include <fstream>
#include <vector>

#include <link.h>
#include <dlfcn.h>
#include <iostream>
#include <signal.h>

#include "common.hpp"
#include "executable.hpp"
#include "dumper.hpp"

int main()
template<Revision R, Variant V>
class LinuxDumper
{
std::cout << "not implemented :(" << "\n";
return -1;
public:
std::span<ExecutableSection> GetExecutableSections()
{
if (CachedSections.empty())
{
struct ElfInfo {
std::span<const ElfW(Phdr)> Sections;
uintptr_t BaseAddress{};
};
ElfInfo libraryInfo{};

const auto findResult = dl_iterate_phdr([](dl_phdr_info* info, size_t size, void* context) -> int {
if (info->dlpi_name[0] == 0 ||
std::string_view(info->dlpi_name).ends_with("/UnityPlayer.so") || std::string_view(info->dlpi_name).ends_with("/TuanjiePlayer.so")) {
const auto result = static_cast<ElfInfo*>(context);
result->Sections = std::span(info->dlpi_phdr, info->dlpi_phnum);
result->BaseAddress = info->dlpi_addr;
return true;
}

return false;
}, &libraryInfo);

if (!findResult) {
DebugLog("Failed to find unity :(");
return {};
}

for (const auto& phdr : libraryInfo.Sections) {
const auto base = phdr.p_vaddr + libraryInfo.BaseAddress;
const auto size = phdr.p_memsz;

auto protection = 0;
if (phdr.p_flags & PF_R)
protection |= ExecutableSection::kSectionProtectionRead;

if (phdr.p_flags & PF_W)
protection |= ExecutableSection::kSectionProtectionWrite;

if (phdr.p_flags & PF_X)
protection |= ExecutableSection::kSectionProtectionExecute;

CachedSections.emplace_back(std::span(reinterpret_cast<char*>(base), size), protection);
}
}

return CachedSections;
}

static std::ofstream CreateOutputFile(char const *filename)
{
// Create the output file in the current directory.
// NOTE: Should this be made configurable? i.e. through an environment variable?
const auto path = std::filesystem::current_path() / filename;
dprintf(2, "[TypeTreeRipper] writing '%s'", path.c_str());
return std::ofstream(path, std::ios::out | std::ios::binary);
}

static void DebugLog(char const *message)
{
dprintf(2, "[TypeTreeRipper] %s\n", message);
}
private:
std::vector<ExecutableSection> CachedSections;
};

typedef int (*orig_vfprintf_type)(FILE *stream, const char *format, va_list ap);

static Revision CachedRevision = Revision::Count;

int vfprintf(FILE *stream, const char *format, va_list ap) {
orig_vfprintf_type orig_vfprintf = (orig_vfprintf_type)dlsym(RTLD_NEXT, "vfprintf");

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LD_PRELOAD=libTypeTreeRipper.so ./Unity


char tmp_buffer[0x100];
va_list cap;
va_copy(cap, ap);
vsnprintf(tmp_buffer, sizeof(tmp_buffer), format, cap);
va_end(cap);
tmp_buffer[0xff] = 0;
// "engine version:" -> Runtime: Version
// "Editor version:" -> Editor: version
// is there a better way??
if (CachedRevision == Revision::Count && strstr(tmp_buffer, "engine version:")) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Count comparison is necessary because the process forks into a player runtime, making the editor dump from the editor module with player structs.

Likely a better way to determine this.

const auto str = std::string(tmp_buffer);
if (const auto parsedRevision = VersionStringToRevision(str.substr(str.find(':') + 1)); parsedRevision.has_value())
{
CachedRevision = parsedRevision.value();
RunDumper<LinuxDumper>(CachedRevision, Variant::Runtime);
// this is likely too early.
dprintf(2, "[TypeTreeRipper] parsed runtime revision");
raise(SIGKILL);
} else {
dprintf(2, "[TypeTreeRipper] failed to parse revision");
}
} else if (CachedRevision == Revision::Count && strstr(tmp_buffer, "Editor version:")) {
const auto str = std::string(tmp_buffer);
if (const auto parsedRevision = VersionStringToRevision(str.substr(str.find(':') + 1)); parsedRevision.has_value())
{
CachedRevision = parsedRevision.value();
dprintf(2, "[TypeTreeRipper] parsed editor revision");
} else {
dprintf(2, "[TypeTreeRipper] failed to parse revision");
}
}

// note: this is late, it also re-initializes managers which can crash unity if it happens during mono reset
// luckily this needle is printed after that
if (CachedRevision != Revision::Count && strstr(tmp_buffer, "Finished resetting the current domain")) {
RunDumper<LinuxDumper>(CachedRevision, Variant::Editor);
raise(SIGKILL);
}

return orig_vfprintf(stream, format, ap);
}
9 changes: 6 additions & 3 deletions source/windows/windows_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ namespace
for (const auto &module : {
L"UnityPlayer.dll",
L"Unity.dll",
L"Unity.exe"
L"Unity.exe",
L"TuanjiePlayer.dll",
L"Tuanjie.dll",
L"Tuanjie.exe"
})
{
const auto hModule = GetModuleHandleW(module);
Expand Down Expand Up @@ -166,14 +169,14 @@ namespace
const auto unityModuleName = std::filesystem::path(unityModuleFilepath.get()).filename().string();

// UnityPlayer.dll is either Runtime or RuntimeDev
if (unityModuleName == "UnityPlayer.dll")
if (unityModuleName == "UnityPlayer.dll" || unityModuleName == "TuanjiePlayer.dll")
{
return Variant::Runtime;
}

// Unity.dll is always the editor
// (Unity.exe is checked in the executable check)
if (unityModuleName == "Unity.dll")
if (unityModuleName == "Unity.dll" || unityModuleName == "Tuanjie.dll")
{
return Variant::Editor;
}
Expand Down