Skip to content

Commit

Permalink
Custom messaging, extension methods, removed debug build
Browse files Browse the repository at this point in the history
* Added asa api mod utils interface.

* Added messaging manager skeleton, added extension classes and headers

* Changed function to return by reference to alter directly in cpp.

* Add messaging manager implementations, bump version to 1.1

* Fixed some things, added Asa api mod messaging manager skeleton

* Added some more messages function to the asa api utils mod messaging manager

* Add config to choose the default manager (must be defined in base api). Added check for requirements if server is ready

* Fix to get default value if config entry does not exist

* Remove useless comment

* Fixed several bugs

* Fixed default messaging reading

* Changed function params to take a struct rather than a lot of single params

* Removed Debug configuration to avoid causing confusion to people trying to build it.
  • Loading branch information
Pelayori authored Jan 3, 2024
1 parent 7d1e366 commit d842648
Show file tree
Hide file tree
Showing 21 changed files with 842 additions and 101 deletions.
6 changes: 0 additions & 6 deletions AsaApi.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AsaApi", "AsaApi\AsaApi.vcx
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Lethal|x64 = Lethal|x64
Lethal|x86 = Lethal|x86
Release|x64 = Release|x64
Expand All @@ -17,10 +15,6 @@ Global
Wooly|x86 = Wooly|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1586E56E-B700-4F77-814E-A0150454949B}.Debug|x64.ActiveCfg = Debug|x64
{1586E56E-B700-4F77-814E-A0150454949B}.Debug|x64.Build.0 = Debug|x64
{1586E56E-B700-4F77-814E-A0150454949B}.Debug|x86.ActiveCfg = Debug|Win32
{1586E56E-B700-4F77-814E-A0150454949B}.Debug|x86.Build.0 = Debug|Win32
{1586E56E-B700-4F77-814E-A0150454949B}.Lethal|x64.ActiveCfg = Lethal|x64
{1586E56E-B700-4F77-814E-A0150454949B}.Lethal|x64.Build.0 = Lethal|x64
{1586E56E-B700-4F77-814E-A0150454949B}.Lethal|x86.ActiveCfg = Lethal|Win32
Expand Down
6 changes: 6 additions & 0 deletions AsaApi/AsaApi.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,11 @@ copy "$(SolutionDir)$(PlatformName)\$(ConfigurationName)\$(ProjectName).pdb" "F:
<ClInclude Include="Core\Public\API\ARK\Actor.h" />
<ClInclude Include="Core\Public\API\ARK\Ark.h" />
<ClInclude Include="Core\Public\API\ARK\Buff.h" />
<ClInclude Include="Core\Public\API\ARK\ExtensionsDeclaration.h" />
<ClInclude Include="Core\Public\API\ARK\ExtensionsDefinition.h" />
<ClInclude Include="Core\Public\API\ARK\GameMode.h" />
<ClInclude Include="Core\Public\API\ARK\Inventory.h" />
<ClInclude Include="Core\Public\API\ARK\ModParamStructs.h" />
<ClInclude Include="Core\Public\API\ARK\Other.h" />
<ClInclude Include="Core\Public\API\ARK\PrimalStructure.h" />
<ClInclude Include="Core\Public\API\ARK\UE.h" />
Expand Down Expand Up @@ -1128,6 +1131,9 @@ copy "$(SolutionDir)$(PlatformName)\$(ConfigurationName)\$(ProjectName).pdb" "F:
<ClInclude Include="Core\Public\API\UE\Windows\WindowsSemaphore.h" />
<ClInclude Include="Core\Public\API\UE\Windows\WindowsSystemIncludes.h" />
<ClInclude Include="Core\Public\Ark\ArkApiUtils.h" />
<ClInclude Include="Core\Public\Ark\AsaApiUtilsMessagingManager.h" />
<ClInclude Include="Core\Public\Ark\MessagingManager.h" />
<ClInclude Include="Core\Public\AsaApiModUtils.hpp" />
<ClInclude Include="Core\Public\IApiUtils.h" />
<ClInclude Include="Core\Public\ICommands.h" />
<ClInclude Include="Core\Public\IHooks.h" />
Expand Down
18 changes: 18 additions & 0 deletions AsaApi/AsaApi.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,24 @@
<ClInclude Include="Core\Public\API\ARK\PrimalStructure.h">
<Filter>Source Files\Core\Public\API\ARK</Filter>
</ClInclude>
<ClInclude Include="Core\Public\Ark\MessagingManager.h">
<Filter>Source Files\Core\Public\Ark</Filter>
</ClInclude>
<ClInclude Include="Core\Public\AsaApiModUtils.hpp">
<Filter>Source Files\Core\Public</Filter>
</ClInclude>
<ClInclude Include="Core\Public\API\ARK\ModParamStructs.h">
<Filter>Source Files\Core\Public\API\ARK</Filter>
</ClInclude>
<ClInclude Include="Core\Public\API\ARK\ExtensionsDeclaration.h">
<Filter>Source Files\Core\Public\API\ARK</Filter>
</ClInclude>
<ClInclude Include="Core\Public\API\ARK\ExtensionsDefinition.h">
<Filter>Source Files\Core\Public\API\ARK</Filter>
</ClInclude>
<ClInclude Include="Core\Public\Ark\AsaApiUtilsMessagingManager.h">
<Filter>Source Files\Core\Public\Ark</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="AsaApi.def">
Expand Down
89 changes: 89 additions & 0 deletions AsaApi/Core/Private/Ark/ApiUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "ApiUtils.h"

#include "../IBaseApi.h"
#include "Ark/MessagingManager.h"
#include "Ark/AsaApiUtilsMessagingManager.h"
#include <fstream>
#include "json.hpp"

namespace AsaApi
{
Expand All @@ -9,6 +13,10 @@ namespace AsaApi
void ApiUtils::SetWorld(UWorld* uworld)
{
u_world_ = uworld;

std::shared_ptr<MessagingManager> manager = ReadApiMessagingManager();
manager->SetWorldContext(uworld);
SetMessagingManagerInternal("Default", manager);
}

UWorld* ApiUtils::GetWorld() const
Expand Down Expand Up @@ -113,6 +121,87 @@ namespace AsaApi
return cheatmanager_;
}

std::shared_ptr<MessagingManager> ApiUtils::GetMessagingManagerInternal(const FString& forPlugin) const
{
auto iter = messaging_managers_.find(forPlugin);
if (forPlugin.IsEmpty()
|| iter == messaging_managers_.end())
{
return messaging_managers_.find("Default")->second;
}
return iter->second;
}

void ApiUtils::SetMessagingManagerInternal(const FString& forPlugin, std::shared_ptr<MessagingManager> manager)
{
if (manager == nullptr)
throw std::invalid_argument("MessagingManager cannot be null");

messaging_managers_[forPlugin] = manager;
manager->SetWorldContext(u_world_);

if (GetStatus() == ServerStatus::Ready)
CheckMessagingManagersRequirements();
}

void ApiUtils::RemoveMessagingManagerInternal(const FString& forPlugin)
{
if (forPlugin.IsEmpty())
return;

messaging_managers_.erase(forPlugin);
}

void ApiUtils::CheckMessagingManagersRequirements()
{
for (auto it = messaging_managers_.begin(); it != messaging_managers_.end(); /*manual increm*/)
{
auto& manager = it->second;
if (!manager)
{
it = messaging_managers_.erase(it);
continue;
}
else
{
std::optional<std::string> error = manager->MeetsRequirementsToWork();
if (error.has_value())
{
TArray<FString> parsedName;
it->first.ParseIntoArray(parsedName, L"\\", true);
Log::GetLog()->error("Custom messaging error for '{}', using normal messaging. Reason: {}", parsedName.Last().ToString(), error.value());
it = messaging_managers_.erase(it);
continue;
}
else
{
manager->SetWorldContext(u_world_);
++it;
}
}
}
}

std::shared_ptr<MessagingManager> ApiUtils::ReadApiMessagingManager()
{
const std::string config_path = AsaApi::Tools::GetCurrentDir() + "/config.json";
std::ifstream file{ config_path };
if (!file.is_open())
return std::make_shared<MessagingManager>();

nlohmann::json config;
file >> config;
file.close();

std::string messaging_manager_name = config["settings"].value("DefaultMessaging", "Default");
if (messaging_manager_name == "Default")
return std::make_shared<MessagingManager>();
else if (messaging_manager_name == "AsaApiUtilsMod")
return std::make_shared<AsaApiUtilsMessagingManager>();
else
return std::make_shared<MessagingManager>(); // fall back to default
}

// Free function
IApiUtils& GetApiUtils()
{
Expand Down
9 changes: 9 additions & 0 deletions AsaApi/Core/Private/Ark/ApiUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ namespace AsaApi
void SetPlayerController(AShooterPlayerController* player_controller);
void RemovePlayerController(AShooterPlayerController* player_controller);

std::shared_ptr<MessagingManager> GetMessagingManagerInternal(const FString& forPlugin) const override;
void SetMessagingManagerInternal(const FString& forPlugin, std::shared_ptr<MessagingManager> manager) override;
void RemoveMessagingManagerInternal(const FString& forPlugin);
void CheckMessagingManagersRequirements();

private:
std::shared_ptr<MessagingManager> ReadApiMessagingManager();

private:
UWorld* u_world_{ nullptr };
AShooterGameMode* shooter_game_mode_{ nullptr };
ServerStatus status_{ 0 };
UShooterCheatManager* cheatmanager_{ nullptr };
std::unordered_map<const FString, AShooterPlayerController*, FStringHash, FStringEqual> eos_id_map_;
std::unordered_map<const FString, std::shared_ptr<MessagingManager>, FStringHash, FStringEqual> messaging_managers_;
};
} // namespace AsaApi
2 changes: 1 addition & 1 deletion AsaApi/Core/Private/Ark/ArkBaseApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace API
{
constexpr float api_version = 1.02;
constexpr float api_version = 1.1;

ArkBaseApi::ArkBaseApi()
: commands_(std::make_unique<AsaApi::Commands>()),
Expand Down
2 changes: 2 additions & 0 deletions AsaApi/Core/Private/Ark/HooksImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ namespace AsaApi
break;
}
}

dynamic_cast<ApiUtils&>(*API::game_api->GetApiUtils()).CheckMessagingManagersRequirements();
}

void Hook_AShooterPlayerController_ServerSendChatMessage_Impl(AShooterPlayerController* player_controller, FString* message, int mode, int senderPlatform)
Expand Down
2 changes: 2 additions & 0 deletions AsaApi/Core/Private/PluginManager/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "../IBaseApi.h"
#include <Timer.h>
#include "../Ark/ApiUtils.h"

namespace API
{
Expand Down Expand Up @@ -171,6 +172,7 @@ namespace API
}

API::Timer::Get().UnloadTimersFromModule(FString(full_dll_path).Replace(L"/", L"\\"));
dynamic_cast<AsaApi::ApiUtils&>(*API::game_api->GetApiUtils()).RemoveMessagingManagerInternal(FString(full_dll_path).Replace(L"/", L"\\"));

const BOOL result = FreeLibrary((*iter)->h_module);
if (result == 0)
Expand Down
4 changes: 2 additions & 2 deletions AsaApi/Core/Private/Tools/Timer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "Timer.h"

#include <Timer.h>

#include "../IBaseApi.h"

#include <Timer.h>

namespace API
{
Timer::Timer()
Expand Down
33 changes: 2 additions & 31 deletions AsaApi/Core/Public/API/ARK/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ struct UStaticMeshComponent : UMeshComponent
bool ShouldRecreateProxyOnUpdateTransform() { return NativeCall<bool>(this, "UStaticMeshComponent.ShouldRecreateProxyOnUpdateTransform()"); }
};

struct AActor : UPrimalActor
struct AActor : UPrimalActor, ActorExtensions
{
// Fields

Expand Down Expand Up @@ -1462,35 +1462,6 @@ struct AActor : UPrimalActor
void MulticastDrawDebugLine_Implementation(const UE::Math::TVector<double>* TextLocation, const FString* Text, AActor* TestBaseActor) { NativeCall<void, const UE::Math::TVector<double>*, const FString*, AActor*>(this, "AActor.MulticastDrawDebugLine_Implementation(UE::Math::TVector<double>,UE::Math::TVector<double>,FLinearColor,float,float,bool)", TextLocation, Text, TestBaseActor); }
void MulticastDrawDebugLineTraceHitResult_Implementation(const FHitResult* Hit, UE::Math::TVector<double>* TraceStart, UE::Math::TVector<double>* TraceEnd) { NativeCall<void, const FHitResult*, UE::Math::TVector<double>*, UE::Math::TVector<double>*>(this, "AActor.MulticastDrawDebugLineTraceHitResult_Implementation(FHitResult,UE::Math::TVector<double>,UE::Math::TVector<double>,FLinearColor,FLinearColor,float,float,bool,FLinearColor,float,float,bool)", Hit, TraceStart, TraceEnd); }
//UPathFollowingComponent* FindComponentByClass<class UPathFollowingComponent>() { return NativeCall<UPathFollowingComponent*>(this, "AActor.FindComponentByClass<class UPathFollowingComponent>()"); }

// Extensions

FORCEINLINE FVector GetActorForwardVector()
{
USceneComponent* RootComponent = this->RootComponentField().Get();
if (RootComponent)
{
return RootComponent->ComponentToWorldField().GetUnitAxis(EAxis::X);
}

return FVector::ZeroVector;
}

FORCEINLINE bool IsA(UClass* SomeBase)
{
return this->ClassPrivateField()->IsChildOf(SomeBase);
}

FORCEINLINE FVector GetLocation()
{
auto* root = this->RootComponentField().Get();
if (root)
{
UE::Math::TTransform<double> transform = root->ComponentToWorldField();
return transform.GetLocation();
}
return FVector();
}
};

struct AVolume : AActor // : ABrush
Expand Down Expand Up @@ -2310,7 +2281,7 @@ struct APrimalController : AController
};


struct APlayerController : APrimalController
struct APlayerController : APrimalController, PlayerControllerExtensions
{
// Fields

Expand Down
5 changes: 4 additions & 1 deletion AsaApi/Core/Public/API/ARK/Ark.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "../UE/Math/ColorList.h"
#include "UE.h"

#include "ExtensionsDeclaration.h"
//#include "Tribe.h"
#include "Actor.h"
#include "Other.h"
Expand All @@ -33,8 +34,10 @@
//#include "GameState.h"
#include "PrimalStructure.h"
#include "Buff.h"
#include "ModParamStructs.h"

#include "../../IApiUtils.h"
#include "../../ICommands.h"
#include "IHooks.h"
#include "Tools.h"
#include "Tools.h"
#include "ExtensionsDefinition.h"
36 changes: 36 additions & 0 deletions AsaApi/Core/Public/API/ARK/ExtensionsDeclaration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "UE.h"
#include <Math/MathFwd.h>
#include <Containers/UnrealString.h>

struct ActorExtensions
{
/**
* \brief Returns the forward direction vector (length 1.0) from the actor's point of view.
* \return The forward direction vector (length 1.0) from the actor's point of view.
*/
FVector GetActorForwardVector();

/**
* \brief Returns if the actor is from SomeBase or a subclass of SomeBase.
* \param SomeBase The base class to check against.
* \return true if the actor is from SomeBase or a subclass of SomeBase.
*/
bool IsA(UClass* SomeBase);

/**
* \brief Returns the actor's location in world space.
* \return The actor's location in world space.
*/
FVector GetLocation();
};

struct PlayerControllerExtensions
{
/**
* \brief Returns the player's EOS id (platform unique identifier)
* \return The player's EOS id (platform unique identifier)
*/
FString GetEOSId();
};
Loading

0 comments on commit d842648

Please sign in to comment.