Skip to content

Commit

Permalink
ConsoleExtension: savecam and loadcam commands
Browse files Browse the repository at this point in the history
  • Loading branch information
SirCxyrtyx committed Jun 1, 2020
1 parent e606ea0 commit 4153990
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
70 changes: 70 additions & 0 deletions ConsoleExtension/ConsoleExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@
#include "../ME3SDK/SdkHeaders.h"
#include "../detours/detours.h"

#include "SimpleSerializer.h"

#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "detours.lib") //Library needed for Hooking part.


#define LOGGING 0

#if LOGGING
ME3TweaksASILogger logger("ConsoleExtension v1", "ConsoleExtension.txt", false);
ScreenLogger screenLogger(L"ConsoleExtension v1");
#endif

auto savedCamsFileName = "savedCams";

wstringstream& operator<<(wstringstream& ss, const FString& fStr)
{
Expand All @@ -30,6 +39,35 @@ wstringstream& operator<<(wstringstream& ss, const FString& fStr)
return ss;
}

FTPOV cachedPOV;
FTPOV povToLoad;
bool shouldSetCamPOV;

FTPOV savedPOVs [10];

void HandleConsoleCommand(const wstring &cmd)
{
if (cmd.rfind(L"savecam ") == 0 && cmd.length() == 9)
{
const int i = _wtoi(&cmd[8]);
if (i >= 0 && i < 10)
{
savedPOVs[i] = cachedPOV;
writeCamArray(savedCamsFileName, savedPOVs);
}
}
else if (cmd.rfind(L"loadcam ") == 0 && cmd.length() == 9)
{
const int i = _wtoi(&cmd[8]);
if (i >= 0 && i < 10)
{
readCamArray(savedCamsFileName, savedPOVs);
povToLoad = savedPOVs[i];
shouldSetCamPOV = true;
}
}
}

void __fastcall HookedPE(UObject *pObject, void *edx, UFunction *pFunction, void *pParms, void *pResult)
{
const auto funcName = pFunction->GetFullName();
Expand All @@ -41,17 +79,40 @@ void __fastcall HookedPE(UObject *pObject, void *edx, UFunction *pFunction, void
const auto console = static_cast<USFXConsole*>(pObject);
wstringstream ss;
ss << console->TypedStr;
HandleConsoleCommand(ss.str());
#if LOGGING
ss << endl;
const wstring msg = ss.str();
logger.writeToDiskOnly(msg, true);
screenLogger.LogMessage(msg);
#endif

}
}
#if LOGGING
else if (!strcmp(funcName, "Function SFXGame.BioHUD.PostRender"))
{
const auto hud = static_cast<ABioHUD*>(pObject);
screenLogger.PostRenderer(hud);
}
#endif

else if (IsA<ABioPlayerController>(pObject) && isPartOf(pFunction->GetFullName(), "Function SFXGame.BioPlayerController.PlayerTick"))
{
const auto playerController = static_cast<ABioPlayerController*>(pObject);
cachedPOV = playerController->PlayerCamera->CameraCache.POV;
}
else if (shouldSetCamPOV && !strcmp(funcName, "Function Engine.Camera.UpdateCamera"))
{
const auto camera = static_cast<ACamera*>(pObject);
if (IsA<AActor>(camera->PCOwner))
{
shouldSetCamPOV = false;
const auto actor = static_cast<AActor*>(camera->PCOwner);
actor->location = povToLoad.location;
actor->Rotation = povToLoad.Rotation;
}
}
ProcessEvent(pObject, pFunction, pParms, pResult);
}

Expand All @@ -61,6 +122,15 @@ void onAttach()
DetourUpdateThread(GetCurrentThread()); //This command set the current working thread to the game current thread.
DetourAttach(&(PVOID&)ProcessEvent, HookedPE); //This command will start your Hook.
DetourTransactionCommit();

if (_access_s(savedCamsFileName, 0) == 0) // does file exist
{
readCamArray(savedCamsFileName, savedPOVs);
}
else
{
writeCamArray(savedCamsFileName, savedPOVs);
}
}

BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
Expand Down
5 changes: 5 additions & 0 deletions ConsoleExtension/ConsoleExtension.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<SDLCheck>false</SDLCheck>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<LanguageStandard>Default</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -80,6 +81,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;KismetLogger_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>Default</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -101,6 +103,9 @@
<ItemGroup>
<ClCompile Include="ConsoleExtension.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="SimpleSerializer.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
21 changes: 21 additions & 0 deletions ConsoleExtension/SimpleSerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <fstream>


template<typename T>
void writeCamArray(const std::string& file_name, T* data)
{
std::ofstream out;
out.open(file_name, std::ios::binary);
out.write(reinterpret_cast<char*>(data), sizeof(T) * 10);
out.close();
};

template<typename T>
void readCamArray(const std::string& file_name, T* data)
{
std::ifstream in;
in.open(file_name, std::ios::binary);
in.read(reinterpret_cast<char*>(data), 10 * sizeof(T));
in.close();
};
4 changes: 2 additions & 2 deletions ME3SDK/ScreenLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>
#include "SdkHeaders.h"
using namespace std;
using byte = unsigned char;
using BYTE = unsigned char;

//adapted from WarrantyVoider's ME3OnTheHook
class ScreenLogger
Expand All @@ -13,7 +13,7 @@ class ScreenLogger

static void RenderText(wchar_t* msg, const float x, const float y, const float r, const float g, const float b, UCanvas* can)
{
can->SetDrawColor(byte(r) * 255, byte(g) * 255, byte(b) * 255, 255);
can->SetDrawColor(BYTE(r) * 255, BYTE(g) * 255, BYTE(b) * 255, 255);
can->SetPos(x, y);
FLinearColor drawColor;
drawColor.R = r;
Expand Down

0 comments on commit 4153990

Please sign in to comment.