Skip to content
This repository was archived by the owner on Jan 1, 2025. It is now read-only.

Commit e479c66

Browse files
authored
Make LoadObject and FindObject consistent (#16)
1 parent 874dfb1 commit e479c66

File tree

8 files changed

+73
-33
lines changed

8 files changed

+73
-33
lines changed

bl2-sdk/BL2-SDK.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
namespace BL2SDK
1717
{
1818
static UConsole * gameConsole = nullptr;
19-
static UWillowGameEngine * willowGameEngine = nullptr;
20-
static UEngine * gameEngine = nullptr;
2119

2220
bool injectedCallNext = false;
2321
bool logAllProcessEvent = true;
@@ -325,7 +323,7 @@ namespace BL2SDK
325323

326324
if (Settings::DeveloperModeEnabled())
327325
{
328-
GameHooks::EngineHookManager->Register("Function WillowGame.WillowGameViewportClient.InputKey", "DevInputKeyHook", &devInputKeyHook);
326+
GameHooks::EngineHookManager->Register("WillowGame.WillowGameViewportClient.InputKey", "DevInputKeyHook", &devInputKeyHook);
329327
Logging::LogF("[Internal] Developer mode key hook enabled\n");
330328
}
331329

@@ -376,7 +374,7 @@ namespace BL2SDK
376374
// gameConsole->ConsoleKey = FName("Tilde");
377375

378376
GameHooks::UnrealScriptHookManager->RemoveStaticHook(function, "StartupSDK");
379-
GameHooks::EngineHookManager->Register("Function WillowGame.WillowGameViewportClient.PostRender", "GetCanvas", getCanvasPostRender);
377+
GameHooks::EngineHookManager->Register("WillowGame.WillowGameViewportClient.PostRender", "GetCanvas", getCanvasPostRender);
380378
//GameHooks::UnrealScriptHookManager->Register("Function GearboxFramework.LeviathanService.OnSparkInitialized", "CheckSpark", &SparkReady);
381379

382380
return true;
@@ -392,7 +390,7 @@ namespace BL2SDK
392390
LogAllProcessEventCalls(false);
393391
LogAllUnrealScriptCalls(false);
394392

395-
GameHooks::UnrealScriptHookManager->Register("Function Engine.Console.Initialized", "StartupSDK", GameReady);
393+
GameHooks::UnrealScriptHookManager->Register("Engine.Console.Initialized", "StartupSDK", GameReady);
396394
//GameHooks::UnrealScriptHookManager->Register("Function Engine.Interaction.NotifyGameSessionEnded", "ExitGame", &cleanup);
397395
}
398396

@@ -430,13 +428,13 @@ namespace BL2SDK
430428
}
431429
if (!Class)
432430
return nullptr;
433-
return BL2SDK::pStaticConstructObject(Class, Outer, Name, SetFlags, InternalSetFlags, Template, Error, InstanceGraph, bAssumeTemplateIsArchetype);
431+
return BL2SDK::pStaticConstructObject(Class, Outer, Name, SetFlags | 0b1, InternalSetFlags, Template, Error, InstanceGraph, bAssumeTemplateIsArchetype);
434432
};
435433

436434
UObject *GetEngine()
437435
{
438436
if (!engine)
439-
engine = UObject::FindStr("WillowGameEngine", "Transient.WillowGameEngine");
437+
engine = UObject::Find("WillowGameEngine", "Transient.WillowGameEngine");
440438
return engine;
441439
}
442440
}

bl2-sdk/CEngineHookManager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void CEngineHookManager::AddVirtualHook(const std::string& funcName, const tFunc
1818
VirtualHooks.emplace(funcName, newMap);
1919
}
2020

21-
//Logging::LogF("[CEngineHookManager] (%s) Hook \"%s\" added as virtual hook for \"%s\"\n", this->DebugName.c_str(), hookPair.first.c_str(), funcName.c_str());
21+
Logging::LogF("[CEngineHookManager] (%s) Hook \"%s\" added as virtual hook for \"%s\"\n", this->DebugName.c_str(), hookPair.first.c_str(), funcName.c_str());
2222
}
2323

2424
void CEngineHookManager::AddStaticHook(UFunction* function, const tFuncNameHookPair& hookPair)
@@ -37,7 +37,7 @@ void CEngineHookManager::AddStaticHook(UFunction* function, const tFuncNameHookP
3737
StaticHooks.emplace(function, newMap);
3838
}
3939

40-
//Logging::LogF("[CEngineHookManager] (%s) Hook \"%s\" added as static hook for \"%s\"\n", this->DebugName.c_str(), hookPair.first.c_str(), function->GetFullName().c_str());
40+
Logging::LogF("[CEngineHookManager] (%s) Hook \"%s\" added as static hook for \"%s\"\n", this->DebugName.c_str(), hookPair.first.c_str(), function->GetFullName().c_str());
4141
}
4242

4343
bool CEngineHookManager::RemoveFromTable(tHookMap& hookTable, const std::string& funcName, const std::string& hookName)
@@ -66,7 +66,7 @@ void CEngineHookManager::Register(const std::string& funcName, const std::string
6666
tFuncNameHookPair hookPair = std::make_pair(hookName, funcHook);
6767

6868
// Find func
69-
UFunction* function = (UFunction *)UObject::FindStr("Function", funcNameChar);
69+
UFunction* function = (UFunction *)UObject::UObject::Find("Function", funcNameChar);
7070
if (function == nullptr)
7171
{
7272
// The function was not found, so we need to create a virtual hook for it
@@ -84,7 +84,7 @@ bool CEngineHookManager::Remove(const std::string& funcName, const std::string&
8484
char funcNameChar[255];
8585
strcpy(funcNameChar, funcName.c_str());
8686

87-
UFunction* function = (UFunction *)UObject::FindStr("Function", funcNameChar);
87+
UFunction* function = (UFunction *)UObject::UObject::Find("Function", funcNameChar);
8888
if (function == nullptr)
8989
{
9090
// Function wasn't found, so virtual hook removal time!
@@ -123,7 +123,7 @@ bool CEngineHookManager::RemoveStaticHook(UFunction* function, const std::string
123123
return false;
124124
}
125125

126-
return RemoveFromTable(iHooks->second, function->GetFullName(), hookName);
126+
return RemoveFromTable(iHooks->second, function->GetObjectName(), hookName);
127127
}
128128

129129
void CEngineHookManager::ResolveVirtualHooks(UFunction* function)
@@ -132,7 +132,7 @@ void CEngineHookManager::ResolveVirtualHooks(UFunction* function)
132132
if (VirtualHooks.size() > 0)
133133
{
134134
//std::string funcName = GetFuncName(pFunction); TODO: Use this instead of the ugly other thing
135-
std::string funcName = function->GetFullName();
135+
std::string funcName = function->GetObjectName();
136136

137137
tiVirtualHooks iVHooks = VirtualHooks.find(funcName);
138138
if (iVHooks != VirtualHooks.end())

bl2-sdk/CPythonInterface.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ PYBIND11_EMBEDDED_MODULE(bl2sdk, m)
8080
Export_pystes_TArray(m);
8181
m.def("Log", [](std::string in) { Logging::Log(in.c_str(), in.length()); });
8282
m.def("LoadPackage", &BL2SDK::LoadPackage, py::arg("filename"), py::arg("flags") = 0, py::arg("force") = false);
83-
m.def("FindObject", UObject::Find, py::return_value_policy::reference);
84-
m.def("FindObject", UObject::FindStr, py::return_value_policy::reference);
85-
m.def("ConstructObject", &BL2SDK::ConstructObject, "Construct Objects", py::arg("Class"), py::arg("Outer") = BL2SDK::GetEngine()->Outer, py::arg("Name") = FName(), py::arg("SetFlags") = 0x201, py::arg("InternalSetFlags") = 0x00, py::arg("Template") = (UObject*)nullptr, py::arg("Error") = (FOutputDevice *)nullptr, py::arg("InstanceGraph") = (void*)nullptr, py::arg("bAssumeTemplateIsArchetype") = (int)0, py::return_value_policy::reference);
83+
m.def("FindObject", [](char *ClassName, char *ObjectFullName) { return UObject::Find(ClassName, ObjectFullName); }, py::return_value_policy::reference);
84+
m.def("FindObject", [](UClass *Class, char *ObjectFullName) { return UObject::Find(Class, ObjectFullName); }, py::return_value_policy::reference);
85+
m.def("LoadObject", [](char *ClassName, char *ObjectFullName) { return UObject::Load(ClassName, ObjectFullName); }, py::return_value_policy::reference);
86+
m.def("LoadObject", [](UClass *Class, char *ObjectFullName) { return UObject::Load(Class, ObjectFullName); }, py::return_value_policy::reference);
87+
m.def("ConstructObject", &BL2SDK::ConstructObject, "Construct Objects", py::arg("Class"), py::arg("Outer") = BL2SDK::GetEngine()->Outer, py::arg("Name") = FName(), py::arg("SetFlags") = 0x1, py::arg("InternalSetFlags") = 0x00, py::arg("Template") = (UObject*)nullptr, py::arg("Error") = (FOutputDevice *)nullptr, py::arg("InstanceGraph") = (void*)nullptr, py::arg("bAssumeTemplateIsArchetype") = (int)0, py::return_value_policy::reference);
8688
m.def("ConstructObject", [](char *ClassName, UObject* Outer, FName Name, unsigned int SetFlags, unsigned int InternalSetFlags, UObject* Template, FOutputDevice *Error, void* InstanceGraph, int bAssumeTemplateIsArchetype) {
8789
return BL2SDK::ConstructObject(UObject::FindClass(ClassName), Outer, Name, SetFlags, InternalSetFlags, Template, Error, InstanceGraph, bAssumeTemplateIsArchetype);
88-
}, "Construct Objects", py::arg("Class"), py::arg("Outer") = BL2SDK::GetEngine()->Outer, py::arg("Name") = FName(), py::arg("SetFlags") = 0x201, py::arg("InternalSetFlags") = 0x00, py::arg("Template") = (UObject*)nullptr, py::arg("Error") = (FOutputDevice *)nullptr, py::arg("InstanceGraph") = (void*)nullptr, py::arg("bAssumeTemplateIsArchetype") = (int)0, py::return_value_policy::reference);
90+
}, "Construct Objects", py::arg("Class"), py::arg("Outer") = BL2SDK::GetEngine()->Outer, py::arg("Name") = FName(), py::arg("SetFlags") = 0x1, py::arg("InternalSetFlags") = 0x00, py::arg("Template") = (UObject*)nullptr, py::arg("Error") = (FOutputDevice *)nullptr, py::arg("InstanceGraph") = (void*)nullptr, py::arg("bAssumeTemplateIsArchetype") = (int)0, py::return_value_policy::reference);
8991
m.def("RegisterEngineHook", &RegisterEngineHook);
9092
m.def("GetEngine", &BL2SDK::GetEngine, py::return_value_policy::reference);
9193
m.def("RegisterScriptHook", &RegisterScriptHook);
@@ -141,8 +143,8 @@ CPythonInterface::CPythonInterface()
141143
m_modulesInitialized = false;
142144
InitializeState();
143145

144-
GameHooks::EngineHookManager->Register("Function WillowGame.WillowGameViewportClient:Tick", "PythonGCTick", &PythonGCTick);
145-
GameHooks::UnrealScriptHookManager->Register("Function Engine.Console.ShippingConsoleCommand", "CheckPythonCommand", &CheckPythonCommand);
146+
GameHooks::EngineHookManager->Register("WillowGame.WillowGameViewportClient:Tick", "PythonGCTick", &PythonGCTick);
147+
GameHooks::UnrealScriptHookManager->Register("Engine.Console.ShippingConsoleCommand", "CheckPythonCommand", &CheckPythonCommand);
146148
}
147149

148150
CPythonInterface::~CPythonInterface()
@@ -154,7 +156,7 @@ CPythonInterface::~CPythonInterface()
154156

155157
CleanupState();
156158

157-
GameHooks::EngineHookManager->Remove("Function WillowGame.WillowGameViewportClient:Tick", "PythonGCTick");
159+
GameHooks::EngineHookManager->Remove("WillowGame.WillowGameViewportClient:Tick", "PythonGCTick");
158160
}
159161

160162
void CPythonInterface::InitializeState()

bl2-sdk/CScriptHookManager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void CScriptHookManager::AddVirtualHook(const std::string& funcName, const tFunc
1818
VirtualHooks.emplace(funcName, newMap);
1919
}
2020

21-
//Logging::LogF("[CScriptHookManager] (%s) Hook \"%s\" added as virtual hook for \"%s\"\n", this->DebugName.c_str(), hookPair.first.c_str(), funcName.c_str());
21+
Logging::LogF("[CScriptHookManager] (%s) Hook \"%s\" added as virtual hook for \"%s\"\n", this->DebugName.c_str(), hookPair.first.c_str(), funcName.c_str());
2222
}
2323

2424
void CScriptHookManager::AddStaticHook(UFunction* function, const tFuncNameHookPair& hookPair)
@@ -37,7 +37,7 @@ void CScriptHookManager::AddStaticHook(UFunction* function, const tFuncNameHookP
3737
StaticHooks.emplace(function, newMap);
3838
}
3939

40-
//Logging::LogF("[CScriptHookManager] (%s) Hook \"%s\" added as static hook for \"%s\"\n", this->DebugName.c_str(), hookPair.first.c_str(), function->GetFullName().c_str());
40+
Logging::LogF("[CScriptHookManager] (%s) Hook \"%s\" added as static hook for \"%s\"\n", this->DebugName.c_str(), hookPair.first.c_str(), function->GetFullName().c_str());
4141
}
4242

4343
bool CScriptHookManager::RemoveFromTable(tHookMap& hookTable, const std::string& funcName, const std::string& hookName)
@@ -66,7 +66,7 @@ void CScriptHookManager::Register(const std::string& funcName, const std::string
6666
tFuncNameHookPair hookPair = std::make_pair(hookName, funcHook);
6767

6868
// Find func
69-
UFunction* function = (UFunction *)UObject::FindStr("Function", funcNameChar);
69+
UFunction* function = (UFunction *)UObject::Find("Function", funcNameChar);
7070
if (function == nullptr)
7171
{
7272
// The function was not found, so we need to create a virtual hook for it
@@ -84,7 +84,7 @@ bool CScriptHookManager::Remove(const std::string& funcName, const std::string&
8484
char funcNameChar[255];
8585
strcpy(funcNameChar, funcName.c_str());
8686

87-
UFunction* function = (UFunction *)UObject::FindStr("Function", funcNameChar);
87+
UFunction* function = (UFunction *)UObject::Find("Function", funcNameChar);
8888
if (function == nullptr)
8989
{
9090
// Function wasn't found, so virtual hook removal time!
@@ -123,7 +123,7 @@ bool CScriptHookManager::RemoveStaticHook(UFunction* function, const std::string
123123
return false;
124124
}
125125

126-
return RemoveFromTable(iHooks->second, function->GetFullName(), hookName);
126+
return RemoveFromTable(iHooks->second, function->GetObjectName(), hookName);
127127
}
128128

129129
void CScriptHookManager::ResolveVirtualHooks(UFunction* function)
@@ -132,7 +132,7 @@ void CScriptHookManager::ResolveVirtualHooks(UFunction* function)
132132
if (VirtualHooks.size() > 0)
133133
{
134134
//std::string funcName = GetFuncName(pFunction); TODO: Use this instead of the ugly other thing
135-
std::string funcName = function->GetFullName();
135+
std::string funcName = function->GetObjectName();
136136

137137
tiVirtualHooks iVHooks = VirtualHooks.find(funcName);
138138
if (iVHooks != VirtualHooks.end())

bl2-sdk/Core_classes.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,30 @@ class UObject
201201
std::string GetName();
202202
std::string GetNameCPP();
203203
std::string GetFullName();
204-
static UObject* Find(UClass *ClassToLoad, const std::string& ObjectFullName)
204+
std::string GetObjectName();
205+
static UObject* Load(UClass *ClassToLoad, const std::string& ObjectFullName)
205206
{
206-
return BL2SDK::GetEngine()->Outer->DynamicLoadObject(FString((char *)ObjectFullName.c_str()), ClassToLoad, true);
207+
return GObjObjects()->Data[0]->DynamicLoadObject(FString((char *)ObjectFullName.c_str()), ClassToLoad, true);
207208
}
208209

209-
static UObject* FindStr(const std::string& ClassName, const std::string& ObjectFullName)
210+
static UObject* Load(const std::string& ClassName, const std::string& ObjectFullName)
210211
{
211212
UClass *classToLoad = FindClass((char *)ClassName.c_str());
212213
if (classToLoad)
213-
return Find(classToLoad, ObjectFullName);
214+
return Load(classToLoad, ObjectFullName);
215+
return nullptr;
216+
}
217+
218+
static UObject* Find(UClass *Class, const std::string& ObjectFullName)
219+
{
220+
return GObjObjects()->Data[0]->FindObject(FString((char *)ObjectFullName.c_str()), Class);
221+
}
222+
223+
static UObject* Find(const std::string& ClassName, const std::string& ObjectFullName)
224+
{
225+
UClass *classToFind = FindClass((char *)ClassName.c_str(), true);
226+
if (classToFind)
227+
return Find(classToFind, ObjectFullName);
214228
return nullptr;
215229
}
216230

bl2-sdk/Core_functions.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ std::string UObject::GetNameCPP()
7777
return cOutBuffer;
7878
}
7979

80+
81+
std::string UObject::GetObjectName()
82+
{
83+
if (this->Class && this->Outer)
84+
{
85+
char cOutBuffer[256] = { NULL };
86+
87+
if (this->Outer->Outer)
88+
{
89+
strcat_s(cOutBuffer, this->Outer->Outer->GetName().c_str());
90+
strcat_s(cOutBuffer, ".");
91+
strcat_s(cOutBuffer, this->Outer->GetName().c_str());
92+
strcat_s(cOutBuffer, ".");
93+
strcat_s(cOutBuffer, this->GetName().c_str());
94+
}
95+
else
96+
{
97+
strcat_s(cOutBuffer, this->Outer->GetName().c_str());
98+
strcat_s(cOutBuffer, ".");
99+
strcat_s(cOutBuffer, this->GetName().c_str());
100+
}
101+
102+
return cOutBuffer;
103+
}
104+
105+
return (char*)"(null)";
106+
}
107+
80108
std::string UObject::GetFullName()
81109
{
82110
if (this->Class && this->Outer)

bl2-sdk/Logging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ namespace Logging
108108
void InitializeGameConsole()
109109
{
110110
// There should only be 1 instance so we should be right to just use it in this way
111-
UConsole* console = (UConsole *)UObject::FindStr("WillowConsole", "Transient.WillowGameEngine_0:WillowGameViewportClient_0.WillowConsole_0");
111+
UConsole* console = (UConsole *)UObject::Find("WillowConsole", "Transient.WillowGameEngine_0:WillowGameViewportClient_0.WillowConsole_0");
112112

113113
if (console != nullptr)
114114
{

bl2-sdk/pydefs/Core_classes.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ void Export_pystes_Core_classes(py::module &m)
1010
.def_static("GObjObjects", &UObject::GObjObjects, py::return_value_policy::reference)
1111
.def_static("FindClass", &UObject::FindClass, py::arg("ClassName"), py::arg("Lookup") = false, py::return_value_policy::reference)
1212
.def_static("FindObjectsRegex", &UObject::FindObjectsRegex, py::return_value_policy::reference)
13-
.def_static("Find", &UObject::Find, py::return_value_policy::reference)
1413
.def_static("FindObjectsContaining", &UObject::FindObjectsContaining, py::return_value_policy::reference)
1514
.def_readwrite("HashNext", &UObject::HashNext)
1615
.def_readwrite("ObjectFlags", &UObject::ObjectFlags)
@@ -23,7 +22,6 @@ void Export_pystes_Core_classes(py::module &m)
2322
.def_readwrite("Outer", &UObject::Outer, py::return_value_policy::reference)
2423
.def_readwrite("Name", &UObject::Name)
2524
.def_readwrite("Class", &UObject::Class, py::return_value_policy::reference)
26-
.def("FindObject", &UObject::FindObject, py::return_value_policy::reference)
2725
.def("GetName", &UObject::GetName)
2826
.def("GetNameCPP", &UObject::GetNameCPP)
2927
.def("GetFullName", &UObject::GetFullName)

0 commit comments

Comments
 (0)