Skip to content

Commit

Permalink
Gang gang
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbyte committed Nov 3, 2023
1 parent f78b664 commit 54f89ab
Show file tree
Hide file tree
Showing 26 changed files with 659 additions and 775 deletions.
13 changes: 1 addition & 12 deletions BG3Extender/Extender/Client/ScriptExtenderClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#define STATIC_HOOK(name) decltype(bg3se::ecl::ScriptExtender::name) * decltype(bg3se::ecl::ScriptExtender::name)::gHook;
STATIC_HOOK(gameStateWorkerStart_)
STATIC_HOOK(gameStateChangedEvent_)
STATIC_HOOK(gameStateMachineUpdate_)

#include <Extender/Shared/ThreadedExtenderState.inl>
Expand Down Expand Up @@ -70,10 +69,6 @@ void ScriptExtender::Initialize()
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());

if (lib.ecl__GameStateEventManager__ExecuteGameStateChangedEvent != nullptr) {
gameStateChangedEvent_.Wrap(lib.ecl__GameStateEventManager__ExecuteGameStateChangedEvent);
}

if (lib.ecl__GameStateThreaded__GameStateWorker__DoWork != nullptr) {
gameStateWorkerStart_.Wrap(lib.ecl__GameStateThreaded__GameStateWorker__DoWork);
}
Expand All @@ -84,7 +79,6 @@ void ScriptExtender::Initialize()

DetourTransactionCommit();

gameStateChangedEvent_.SetPostHook(&ScriptExtender::OnGameStateChanged, this);
gameStateWorkerStart_.SetWrapper(&ScriptExtender::GameStateWorkerWrapper, this);
gameStateMachineUpdate_.SetPostHook(&ScriptExtender::OnUpdate, this);
}
Expand Down Expand Up @@ -122,13 +116,8 @@ bool IsLoadingState(GameState state)
|| state == GameState::ModReceiving;
}

void ScriptExtender::OnGameStateChanged(void * self, GameState fromState, GameState toState)
void ScriptExtender::OnGameStateChanged(GameState fromState, GameState toState)
{
if (self != *GetStaticSymbols().ecl__gGameStateEventManager) {
gExtender->GetServer().OnGameStateChanged(self, (esv::GameState)fromState, (esv::GameState)toState);
return;
}

if (gExtender->GetConfig().SendCrashReports) {
// We need to initialize the crash reporter after the game engine has started,
// otherwise the game will overwrite the top level exception filter
Expand Down
4 changes: 1 addition & 3 deletions BG3Extender/Extender/Client/ScriptExtenderClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ class ScriptExtender : public ThreadedExtenderState

void UpdateServerProgress(STDString const& status);
void UpdateClientProgress(STDString const& status);
void OnGameStateChanged(GameState fromState, GameState toState);

// HACK - we need to expose this so it can be added to the CrashReporter whitelist
enum class GameStateWorkerStartTag {};
enum class GameStateMachcineUpdateTag {};
enum class GameStateChangedEventTag {};
WrappableFunction<GameStateWorkerStartTag, void(void*)> gameStateWorkerStart_;
WrappableFunction<GameStateMachcineUpdateTag, void(void*, GameTime*)> gameStateMachineUpdate_;
WrappableFunction<GameStateChangedEventTag, void(void*, GameState, GameState)> gameStateChangedEvent_;

private:
ExtenderConfig& config_;
Expand All @@ -78,7 +77,6 @@ class ScriptExtender : public ThreadedExtenderState
NetworkManager network_;

void OnBaseModuleLoaded(void * self);
void OnGameStateChanged(void * self, GameState fromState, GameState toState);
void GameStateWorkerWrapper(void (*wrapped)(void*), void* self);
void OnUpdate(void* self, GameTime* time);
void OnIncLocalProgress(void* self, int progress, char const* state);
Expand Down
73 changes: 73 additions & 0 deletions BG3Extender/Extender/ScriptExtender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void ShutdownCrashReporting();

decltype(ScriptExtender::CoreLibInit)* decltype(ScriptExtender::CoreLibInit)::gHook;
decltype(ScriptExtender::AppUpdatePaths)* decltype(ScriptExtender::AppUpdatePaths)::gHook;
decltype(ScriptExtender::AppLoadGraphicSettings)* decltype(ScriptExtender::AppLoadGraphicSettings)::gHook;

std::unique_ptr<ScriptExtender> gExtender;

Expand Down Expand Up @@ -82,6 +83,11 @@ void ScriptExtender::Initialize()
AppUpdatePaths.SetPostHook(&ScriptExtender::OnAppUpdatePaths, this);
}

if (GetStaticSymbols().App__LoadGraphicSettings != nullptr) {
AppLoadGraphicSettings.Wrap(GetStaticSymbols().App__LoadGraphicSettings);
AppLoadGraphicSettings.SetPostHook(&ScriptExtender::OnAppLoadGraphicSettings, this);
}

DetourTransactionCommit();

#if !defined(OSI_NO_DEBUGGER)
Expand Down Expand Up @@ -260,6 +266,73 @@ void ScriptExtender::OnAppUpdatePaths(void * self)
}
}

class ClientEventManagerHook
{
public:
struct GameStates
{
ecl::GameState From;
ecl::GameState To;
};

virtual ~ClientEventManagerHook()
{}

virtual bool OnGameStateChanged(GameStates& states)
{
gExtender->GetClient().OnGameStateChanged(states.From, states.To);
return true;
}

virtual bool Unknown()
{
return false;
}

uint64_t dummy{ 0 };
};

class ServerEventManagerHook
{
public:
struct GameStates
{
esv::GameState From;
esv::GameState To;
};

virtual ~ServerEventManagerHook()
{}

virtual bool OnGameStateChanged(GameStates& states)
{
gExtender->GetServer().OnGameStateChanged(states.From, states.To);
return true;
}

virtual bool Unknown()
{
return false;
}

uint64_t dummy{ 0 };
};

void ScriptExtender::OnAppLoadGraphicSettings(App * self)
{
auto clientEvtMgr = GetStaticSymbols().ecl__gGameStateEventManager;
if (clientEvtMgr && *clientEvtMgr) {
auto client = GameAlloc<ClientEventManagerHook>();
(*clientEvtMgr)->Callbacks.push_back(&client->dummy);
}

auto serverEvtMgr = GetStaticSymbols().esv__gGameStateEventManager;
if (serverEvtMgr && *serverEvtMgr) {
auto server = GameAlloc<ServerEventManagerHook>();
(*serverEvtMgr)->Callbacks.push_back(&server->dummy);
}
}

void ScriptExtender::OnBaseModuleLoaded(void * self)
{
}
Expand Down
3 changes: 3 additions & 0 deletions BG3Extender/Extender/ScriptExtender.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class ScriptExtender
WrappableFunction<CoreLibInitTag, CoreLibInitProc> CoreLibInit;
enum class AppUpdatePathsTag {};
WrappableFunction<AppUpdatePathsTag, AppUpdatePathsProc> AppUpdatePaths;
enum class AppLoadGraphicSettingsTag {};
WrappableFunction<AppLoadGraphicSettingsTag, App::LoadGraphicSettingsProc> AppLoadGraphicSettings;

private:
esv::ScriptExtender server_;
Expand Down Expand Up @@ -149,6 +151,7 @@ class ScriptExtender

void OnCoreLibInit(void * self);
void OnAppUpdatePaths(void * self);
void OnAppLoadGraphicSettings(App* self);
void OnBaseModuleLoaded(void * self);
void OnModuleLoadStarted(TranslatedStringRepository* self);
void OnStatsLoad(stats::RPGStats::LoadProc* wrapped, stats::RPGStats* mgr, Array<STDString>* paths);
Expand Down
2 changes: 1 addition & 1 deletion BG3Extender/Extender/Server/ScriptExtenderServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ bool IsLoadingState(GameState state)
|| state == GameState::ReloadStory;
}

void ScriptExtender::OnGameStateChanged(void * self, GameState fromState, GameState toState)
void ScriptExtender::OnGameStateChanged(GameState fromState, GameState toState)
{
#if defined(DEBUG_SERVER_CLIENT)
DEBUG("esv::ScriptExtender::OnGameStateChanged(): %s -> %s",
Expand Down
2 changes: 1 addition & 1 deletion BG3Extender/Extender/Server/ScriptExtenderServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ScriptExtender : public ThreadedExtenderState
void Initialize();
void PostStartup();
void Shutdown();
void OnGameStateChanged(void* self, GameState fromState, GameState toState);
void OnGameStateChanged(GameState fromState, GameState toState);

inline bool HasExtensionState() const
{
Expand Down
2 changes: 1 addition & 1 deletion BG3Extender/Extender/Shared/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void Hooks::Startup()
}

auto& lib = gExtender->GetEngineHooks();
lib.RPGStats__ParseStructureFolder.SetPostHook(&Hooks::OnParseStructureFolder, this);
lib.RPGStats__PreParseDataFolder.SetPreHook(&Hooks::OnParseStructureFolder, this);
eocnet__ClientConnectMessage__Serialize.SetWrapper(&Hooks::OnClientConnectMessage, this);

loaded_ = true;
Expand Down
Loading

3 comments on commit 54f89ab

@FedoraStorm
Copy link

Choose a reason for hiding this comment

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

Truly a gang gang moment, my favorite part is when Norbyte said "it's Norbyten' time!" and Norbyted all over the place.

@kaskeloten
Copy link

Choose a reason for hiding this comment

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

Ice cream so good!

@T2av
Copy link

@T2av T2av commented on 54f89ab Nov 3, 2023

Choose a reason for hiding this comment

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

Hee Haw You got me feeling like a cowgirl

Please sign in to comment.