Skip to content

Commit

Permalink
Add vehicle lights callback
Browse files Browse the repository at this point in the history
  • Loading branch information
psiberx committed Aug 18, 2024
1 parent 26c0071 commit 4bc6306
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/Red/TypeInfo/Resolving.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ inline const Handle<T>& Cast(const Handle<U>& aObject)
}

template<typename T, typename U = ISerializable>
inline const WeakHandle<T>& Cast(const WeakHandle<U>& aObject)
inline const WeakHandle<T>& CastWeak(const WeakHandle<U>& aObject)
{
static const WeakHandle<T> s_null;
return (aObject && aObject.instance->GetType()->IsA(Red::GetClass<T>()))
Expand Down
5 changes: 5 additions & 0 deletions scripts/Callback/Events/VehicleLightControlEvent.reds
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public native class VehicleLightControlEvent extends EntityLifecycleEvent {
public native func IsEnabled() -> Bool
public native func GetLightType() -> vehicleELightType
public native func IsLightType(lightType: vehicleELightType) -> Bool
}
4 changes: 3 additions & 1 deletion src/App/Callback/CallbackSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include "App/Callback/Controllers/EntityReassembleHook.hpp"
#include "App/Callback/Controllers/EntityUninitializeHook.hpp"
#include "App/Callback/Controllers/PlayerSpawnedHook.hpp"
#include "App/Callback/Controllers/RawInputHook.hpp"
#include "App/Callback/Controllers/ResourceLoadHook.hpp"
#include "App/Callback/Controllers/ResourceReadyHook.hpp"
#include "App/Callback/Controllers/RawInputHook.hpp"
#include "App/Callback/Controllers/VehicleLightControlHook.hpp"
#include "App/Callback/Events/GameSessionEvent.hpp"
#include "App/Scripting/ScriptableService.hpp"
#include "Red/InkSystem.hpp"
Expand All @@ -23,6 +24,7 @@ App::CallbackSystem::CallbackSystem()
RegisterController<EntityInitializeHook>();
RegisterController<EntityReassembleHook>();
RegisterController<EntityUninitializeHook>();
RegisterController<VehicleLightControlHook>();
RegisterController<PlayerSpawnedHook>();
RegisterController<ResourceLoadHook>();
RegisterController<ResourceReadyHook>();
Expand Down
39 changes: 39 additions & 0 deletions src/App/Callback/Controllers/VehicleLightControlHook.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "App/Callback/CallbackSystem.hpp"
#include "App/Callback/CallbackSystemController.hpp"
#include "App/Callback/Events/VehicleLightControlEvent.hpp"
#include "Core/Hooking/HookingAgent.hpp"
#include "Red/VehicleController.hpp"

namespace App
{
class VehicleLightControlHook
: public CallbackSystemController
, public Core::HookingAgent
{
public:
constexpr static auto EventName = Red::CName("Vehicle/ToggleLights");

Core::Map<Red::CName, Red::CName> GetEvents() override
{
return {{EventName, Red::GetTypeName<VehicleLightControlEvent>()}};
}

protected:
bool OnActivateHook() override
{
return IsHooked<Raw::VehicleController::ToggleLights>() || HookAfter<Raw::VehicleController::ToggleLights>(&OnToggleLights);
}

bool OnDeactivateHook() override
{
return !IsHooked<Raw::VehicleController::ToggleLights>() || Unhook<Raw::VehicleController::ToggleLights>();
}

inline static void OnToggleLights(Red::vehicleController* aController, bool aEnable, Red::vehicleELightType aLightType)
{
CallbackSystem::Get()->DispatchNativeEvent<VehicleLightControlEvent>(EventName, Red::AsWeakHandle(aController->owner), aEnable, aLightType);
}
};
}
38 changes: 38 additions & 0 deletions src/App/Callback/Events/VehicleLightControlEvent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "App/Callback/CallbackSystemEvent.hpp"
#include "App/Callback/Events/EntityLifecycleEvent.hpp"

namespace App
{
struct VehicleLightControlEvent : EntityLifecycleEvent
{
VehicleLightControlEvent() = default;

VehicleLightControlEvent(Red::CName aName, Red::WeakHandle<Red::Entity> aEntity,
bool aEnable, Red::vehicleELightType aLightType)
: EntityLifecycleEvent(aName, std::move(aEntity))
, enabled(aEnable)
, lightType(aLightType)
{
}

[[nodiscard]] bool IsLightType(Red::vehicleELightType aLightType) const
{
return (static_cast<uint32_t>(lightType) & static_cast<uint32_t>(aLightType)) != 0;
}

bool enabled;
Red::vehicleELightType lightType;

RTTI_IMPL_TYPEINFO(App::VehicleLightControlEvent);
RTTI_IMPL_ALLOCATOR();
};
}

RTTI_DEFINE_CLASS(App::VehicleLightControlEvent, {
RTTI_PARENT(App::EntityLifecycleEvent);
RTTI_METHOD(IsLightType);
RTTI_GETTER(enabled);
RTTI_GETTER(lightType);
});
2 changes: 1 addition & 1 deletion src/App/Callback/Targets/EntityTarget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct EntityTarget : CallbackSystemTarget

bool Supports(Red::CName aEventType) override
{
return aEventType == Red::GetTypeName<EntityLifecycleEvent>();
return Red::GetClass(aEventType)->IsA(Red::GetClass<EntityLifecycleEvent>());
}

static Red::Handle<EntityTarget> ID(Red::EntityID aEntityID)
Expand Down
2 changes: 2 additions & 0 deletions src/Red/Addresses/Library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ constexpr uint32_t ResourceSerializer_OnResourceReady = 1147149338;
constexpr uint32_t TagSystem_AssignTag = 1239092911;
constexpr uint32_t TagSystem_UnassignTag = 1875775378;

constexpr uint32_t VehicleController_ToggleLights = 3642563117;

constexpr uint32_t VehicleSystem_ToggleGarageVehicle = 3027836941;

constexpr uint32_t WorkspotSystem_GetWorkspotInstance = 2234523611;
Expand Down
8 changes: 8 additions & 0 deletions src/Red/VehicleController.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace Raw::VehicleController
{
constexpr auto ToggleLights = Core::RawFunc<
/* addr = */ Red::AddressLib::VehicleController_ToggleLights,
/* type = */ void (*)(Red::vehicleController* aController, bool aEnable, Red::vehicleELightType aLightType)>();
}
2 changes: 2 additions & 0 deletions src/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@
#include <RED4ext/Scripting/Natives/Generated/red/ResourceReferenceScriptToken.hpp>
#include <RED4ext/Scripting/Natives/Generated/red/TagSystem.hpp>
#include <RED4ext/Scripting/Natives/Generated/vehicle/BaseObject.hpp>
#include <RED4ext/Scripting/Natives/Generated/vehicle/Controller.hpp>
#include <RED4ext/Scripting/Natives/Generated/vehicle/ELightType.hpp>
#include <RED4ext/Scripting/Natives/Generated/vehicle/GarageVehicleID.hpp>
#include <RED4ext/Scripting/Natives/Generated/work/WorkEntryId.hpp>
#include <RED4ext/Scripting/Natives/Generated/work/WorkspotInstance.hpp>
Expand Down
2 changes: 1 addition & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set_xmakever("2.5.9")

set_project("Codeware")
set_version("1.12.3", {build = "%y%m%d%H%M"})
set_version("1.12.4", {build = "%y%m%d%H%M"})

set_arch("x64")
set_languages("cxx2a")
Expand Down

0 comments on commit 4bc6306

Please sign in to comment.