Skip to content

Commit

Permalink
Add import misses to DebugLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
SirCxyrtyx committed Sep 30, 2021
1 parent fe055b2 commit c0c5af6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
31 changes: 31 additions & 0 deletions ME3TweaksHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ std::string GuidToString(FGuid guid)
}
#endif

const std::wstring wstring_format(const wchar_t* const zcFormat, ...) {

// initialize use of the variable argument array
va_list vaArgs;
va_start(vaArgs, zcFormat);

// reliably acquire the size
// from a copy of the variable argument array
// and a functionally reliable call to mock the formatting
va_list vaArgsCopy;
va_copy(vaArgsCopy, vaArgs);
const int iLen = std::vswprintf(NULL, 0, zcFormat, vaArgsCopy);
va_end(vaArgsCopy);

// return a formatted string without risking memory mismanagement
// and without assuming any compiler or platform specific behavior
std::vector<wchar_t> zc(iLen + 1);
std::vswprintf(zc.data(), zc.size(), zcFormat, vaArgs);
va_end(vaArgs);
return std::wstring(zc.data(), iLen);
}

const std::string string_format(const char* const zcFormat, ...) {

// initialize use of the variable argument array
Expand Down Expand Up @@ -203,6 +225,15 @@ class ME3TweaksASILogger
}
}

void close()
{
if (log)
{
fflush(log);
fclose(log);
}
}

private:
int numLinesWritten = 0;
FILE* log;
Expand Down
8 changes: 7 additions & 1 deletion SDK/LE1SDK/SDK_HEADERS/Core_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,13 @@ UClass* UMetaData::pClassPointer = NULL;
class ULinker : public UObject
{
public:
unsigned char UnknownData00[ 0x14C ]; // 0x0060 (0x014C) MISSED OFFSET
class UPackage* LinkerRoot; // 0x0060 (0x0008)
unsigned char UnknownData00[ 0xA4 ]; // 0x0068 (0x00A4) MISSED OFFSET
struct TArray<FName> NameMap; // 0x011C (0x0010)
struct TArray<FObjectImport> ImportMap; // 0x011C (0x0010)
unsigned char UnknownData01[ 0x68 ]; // 0x012C (0x0068) MISSED OFFSET
struct FString Filename; // 0x0194 (0x0010)
unsigned char UnknownData02[0x8]; // 0x01A4 (0x0008) MISSED OFFSET

private:
static UClass* pClassPointer;
Expand Down
15 changes: 15 additions & 0 deletions SDK/LE1SDK/SdkHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ struct FScriptInterface
void* Interface;
};

struct FObjectResource
{
struct FName ObjectName;
int OuterIndex;
};

struct FObjectImport : public FObjectResource
{
struct FName ClassPackage;
struct FName ClassName;
class UObject* Object;
class ULinkerLoad* SourceLinker;
int SourceIndex;
};

/*
# ========================================================================================= #
# Includes
Expand Down
46 changes: 46 additions & 0 deletions SelfDebugger/DebugLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "../Common.h"
#include "../ME3TweaksHeader.h"

#define MYHOOK "DebugLogger_"

SPI_PLUGINSIDE_SUPPORT(L"DebugLogger", L"1.0.0", L"ME3Tweaks", SPI_GAME_LE1 | SPI_GAME_LE2 | SPI_GAME_LE3, SPI_VERSION_ANY);
SPI_PLUGINSIDE_PRELOAD;
SPI_PLUGINSIDE_SEQATTACH;
Expand All @@ -22,6 +24,23 @@ void WINAPI OutputDebugStringW_Hook(LPCWSTR lpcszString)
OutputDebugStringW_Orig(lpcszString);
writeMsg(L"%s", lpcszString); // string already has a newline on the end
logger.writeWideToLog(std::wstring_view{ lpcszString });
logger.flush();
}

typedef UObject* (*tCreateImport)(ULinkerLoad* Context, int UIndex);
tCreateImport CreateImport = nullptr;
tCreateImport CreateImport_orig = nullptr;
UObject* CreateImport_hook(ULinkerLoad* Context, int i)
{
UObject* object = CreateImport_orig(Context, i);
if (object == nullptr)
{
FObjectImport importEntry = Context->ImportMap(i);
writeln("Could not resolve #%d: %hs in file: %s", -i - 1, importEntry.ObjectName.GetName(), Context->Filename.Data);
logger.writeWideLineToLog(wstring_format(L"Could not resolve #%d: %hs in file: %s", -i - 1, importEntry.ObjectName.GetName(), Context->Filename.Data));
logger.flush();
}
return object;
}

SPI_IMPLEMENT_ATTACH
Expand All @@ -36,6 +55,23 @@ SPI_IMPLEMENT_ATTACH
return false;
}
writeln(L"Initialized DebugLogger");


auto _ = SDKInitializer::Instance();
writeln(L"Initializing CreateImport hook...");
if (auto const rc = InterfacePtr->FindPattern(reinterpret_cast<void**>(&CreateImport), "48 8b c4 55 41 54 41 55 41 56 41 57 48 8b ec 48 83 ec 70 48 c7 45 d0 fe ff ff ff 48 89 58 10 48 89 70 18 48 89 78 20 4c 63 e2");
rc != SPIReturn::Success)
{
writeln(L"Attach - failed to find CreateImport pattern: %d / %s", rc, SPIReturnToString(rc));
return false;
}
if (auto const rc = InterfacePtr->InstallHook(MYHOOK "CreateImport", CreateImport, CreateImport_hook, reinterpret_cast<void**>(&CreateImport_orig));
rc != SPIReturn::Success)
{
writeln(L"Attach - failed to hook CreateImport: %d / %s", rc, SPIReturnToString(rc));
return false;
}
writeln(L"Hooked CreateImport");
return true;
}

Expand All @@ -44,5 +80,15 @@ SPI_IMPLEMENT_DETACH
{
//DebugActiveProcessStop(GetCurrentProcessId());
Common::CloseConsole();
logger.close();
return true;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID) {
if (reason == DLL_PROCESS_DETACH)
{
logger.close();
}

return TRUE;
}

1 comment on commit c0c5af6

@Mgamerz
Copy link
Member

Choose a reason for hiding this comment

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

even bigger poggies

Please sign in to comment.