Skip to content

Commit

Permalink
add RPC events
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed Sep 24, 2023
1 parent 740dbed commit e3f3f8f
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 3 deletions.
19 changes: 19 additions & 0 deletions c-api/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,16 @@ void Core_SetMigrationDistance(alt::ICore* core, uint32_t limit)
{
core->SetMigrationDistance(limit);
}

void Core_TriggerClientRPCAnswer(alt::ICore* core, alt::IPlayer* target, uint16_t answerID, alt::MValueConst* args[],
int size, const char* error)
{
alt::MValueArgs mValues = alt::MValueArgs(size);
for (int i = 0; i < size; i++) {
ToMValueArg(mValues, core, args[i], i);
}
core->TriggerClientRPCAnswer(target, answerID, mValues, error);
}
#endif

#ifdef ALT_CLIENT_API
Expand Down Expand Up @@ -1998,6 +2008,15 @@ void Core_GetAllWeaponData(alt::ICore* core, uint32_t weaponHashes[], uint64_t s
weaponHashes[i] = weaponData[i]->GetNameHash();
}
}

uint16_t Core_TriggerServerRPCEvent(alt::ICore* core, const char* ev, alt::MValueConst* args[], int size)
{
alt::MValueArgs mValues = alt::MValueArgs(size);
for (int i = 0; i < size; i++) {
ToMValueArg(mValues, core, args[i], i);
}
return core->TriggerServerRPCEvent(ev, mValues);
}
#endif

CAPI_END()
5 changes: 4 additions & 1 deletion c-api/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,7 @@ EXPORT_SERVER uint32_t Core_GetColShapeTickRate(alt::ICore* core);
EXPORT_SERVER void Core_SetColShapeTickRate(alt::ICore* core, uint32_t limit);

EXPORT_SERVER uint32_t Core_GetMigrationDistance(alt::ICore* core);
EXPORT_SERVER void Core_SetMigrationDistance(alt::ICore* core, uint32_t limit);
EXPORT_SERVER void Core_SetMigrationDistance(alt::ICore* core, uint32_t limit);

EXPORT_SERVER void Core_TriggerClientRPCAnswer(alt::ICore* server, alt::IPlayer* target, uint16_t answerID, alt::MValueConst* args[], int size, const char* error);
EXPORT_CLIENT uint16_t Core_TriggerServerRPCEvent(alt::ICore* server, const char* ev, alt::MValueConst* args[], int size);
27 changes: 26 additions & 1 deletion c-api/event.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
#include "event.h"

#include "mvalue.h"
#include "utils/macros.h"

CAPI_START()

#ifdef ALT_SERVER_API

void Event_WeaponDamageEvent_SetDamageValue(alt::CEvent* event, uint32_t damageValue) {
((alt::CWeaponDamageEvent*) event)->SetDamageValue(damageValue);
dynamic_cast<alt::CWeaponDamageEvent*>(event)->SetDamageValue(damageValue);
}

uint8_t Event_ClientScriptRPCEvent_WillAnswer(alt::CEvent* event)
{
auto rpcEvent = dynamic_cast<alt::CClientScriptRPCEvent*>(event);
return rpcEvent->WillAnswer();
}

uint8_t Event_ClientScriptRPCEvent_Answer(alt::CEvent* event, alt::ICore* core, alt::MValueConst* args[], int size)
{
alt::MValueArgs mValues = alt::MValueArgs(size);
for (int i = 0; i < size; i++) {
ToMValueArg(mValues, core, args[i], i);
}
auto rpcEvent = dynamic_cast<alt::CClientScriptRPCEvent*>(event);
return rpcEvent->Answer(mValues);
}

uint8_t Event_ClientScriptRPCEvent_AnswerWithError(alt::CEvent* event, const char* error)
{
auto rpcEvent = dynamic_cast<alt::CClientScriptRPCEvent*>(event);
return rpcEvent->AnswerWithError(error);
}
#endif

Expand Down Expand Up @@ -90,6 +113,8 @@ SetDelegate(PlayerBulletHit);
SetDelegate(VoiceConnection);
SetDelegate(AudioEvent);

SetDelegate(ServerScriptRPCAnswer)

#endif

CAPI_END()
4 changes: 4 additions & 0 deletions c-api/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

EXPORT_SHARED void Event_Cancel(alt::CCancellableEvent* event);
EXPORT_SERVER void Event_WeaponDamageEvent_SetDamageValue(alt::CEvent* event, uint32_t damageValue);
EXPORT_SERVER uint8_t Event_ClientScriptRPCEvent_WillAnswer(alt::CEvent* event);
EXPORT_SERVER uint8_t Event_ClientScriptRPCEvent_Answer(alt::CEvent* event, alt::MValueConst* args[], int size);
EXPORT_SERVER uint8_t Event_ClientScriptRPCEvent_AnswerWithError(alt::CEvent* event, const char* error);
EXPORT_SHARED uint8_t Event_WasCancelled(alt::CCancellableEvent* event);

#ifdef ALT_CLIENT_API
Expand Down Expand Up @@ -92,4 +95,5 @@ EXPORT_CLIENT void Event_SetPlayerStartLeaveVehicleDelegate(CSharpResourceImpl*

EXPORT_CLIENT void Event_SetPlayerBulletHitDelegate(CSharpResourceImpl* resource, /** ClientEvents.PlayerBulletHitModuleDelegate */ PlayerBulletHitDelegate_t delegate);
EXPORT_CLIENT void Event_SetVoiceConnectionDelegate(CSharpResourceImpl* resource, /** ClientEvents.VoiceConnectionModuleDelegate */ VoiceConnectionDelegate_t delegate);
EXPORT_CLIENT void Event_SetServerScriptRPCAnswerDelegate(CSharpResourceImpl* resource, /** ClientEvents.ServerScriptRPCAnswerModuleDelegate */ ServerScriptRPCAnswerDelegate_t delegate);
#endif
14 changes: 13 additions & 1 deletion c-api/func_table.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "func_table.h"

inline uint64_t capiHash = 3088291357268716232UL;
inline uint64_t capiHash = 11229478147648242923UL;
inline uint64_t capiHashes[] = {
0,
#ifdef ALT_CLIENT_API
Expand Down Expand Up @@ -245,6 +245,7 @@ inline uint64_t capiHashes[] = {
1817218062913323235UL,
4092140335578989631UL,
718150788563346996UL,
5920144219377072122UL,
3268039739443301173UL,
17753040748478874447UL,
5389506501733691988UL,
Expand Down Expand Up @@ -286,6 +287,7 @@ inline uint64_t capiHashes[] = {
8121512912272945641UL,
1513529985252499227UL,
5521055548998327457UL,
13554094634858443749UL,
8576321635222028243UL,
12745100726667735891UL,
11607888672861240667UL,
Expand Down Expand Up @@ -1374,6 +1376,7 @@ inline uint64_t capiHashes[] = {
4821179867491879744UL,
9578627964183564598UL,
14557546483922608997UL,
9224620724544415249UL,
4092140335578989631UL,
17237210604041123822UL,
11965009764904998252UL,
Expand All @@ -1390,6 +1393,9 @@ inline uint64_t capiHashes[] = {
6004628797499736605UL,
12798418058428333585UL,
8026011842118229214UL,
9846613369628943706UL,
2674464925851684396UL,
5065438074644693875UL,
18440396865533386791UL,
2348810001298180138UL,
13016512038826983106UL,
Expand Down Expand Up @@ -1969,6 +1975,7 @@ inline void* capiPointers[] = {
(void*) Core_ToggleVoiceInput,
(void*) Core_TriggerServerEvent,
(void*) Core_TriggerServerEventUnreliable,
(void*) Core_TriggerServerRPCEvent,
(void*) Core_TriggerWebViewEvent,
(void*) Core_UnloadYtyp,
(void*) Core_WorldToScreen,
Expand Down Expand Up @@ -2010,6 +2017,7 @@ inline void* capiPointers[] = {
(void*) Event_SetRemoveBaseObjectDelegate,
(void*) Event_SetRmlEventDelegate,
(void*) Event_SetServerEventDelegate,
(void*) Event_SetServerScriptRPCAnswerDelegate,
(void*) Event_SetStreamSyncedMetaChangeDelegate,
(void*) Event_SetSyncedMetaChangeDelegate,
(void*) Event_SetTaskChangeDelegate,
Expand Down Expand Up @@ -3098,6 +3106,7 @@ inline void* capiPointers[] = {
(void*) Core_TriggerClientEventUnreliable,
(void*) Core_TriggerClientEventUnreliableForAll,
(void*) Core_TriggerClientEventUnreliableForSome,
(void*) Core_TriggerClientRPCAnswer,
(void*) Core_TriggerServerEvent,
(void*) Core_UnregisterMetric,
(void*) Entity_AttachToEntity,
Expand All @@ -3114,6 +3123,9 @@ inline void* capiPointers[] = {
(void*) Entity_SetStreamed,
(void*) Entity_SetStreamSyncedMetaData,
(void*) Entity_SetVisible,
(void*) Event_ClientScriptRPCEvent_Answer,
(void*) Event_ClientScriptRPCEvent_AnswerWithError,
(void*) Event_ClientScriptRPCEvent_WillAnswer,
(void*) Event_WeaponDamageEvent_SetDamageValue,
(void*) Metric_Begin,
(void*) Metric_End,
Expand Down
20 changes: 20 additions & 0 deletions client/src/runtime/CSharpResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,24 @@ void CSharpResourceImpl::OnEvent(const alt::CEvent* ev)
size);
break;
}
case alt::CEvent::Type::SERVER_SCRIPT_RPC_ANSWER_EVENT:
{
auto serverScriptRPCAnswerEvent = dynamic_cast<const alt::CServerScriptRPCAnswerEvent*>(ev);

auto answers = serverScriptRPCAnswerEvent->GetAnswer();
auto size = answers.size();
auto constAnswers = new alt::MValueConst*[size];

for (uint64_t i = 0; i < size; i++)
{
constAnswers[i] = &answers[i];
}
OnServerScriptRPCAnswerDelegate(serverScriptRPCAnswerEvent->GetAnswerID(),
constAnswers,
size,
serverScriptRPCAnswerEvent->GetAnswerError().c_str());
break;
}
default:
{
std::cout << "Unhandled client event #" << static_cast<int>(ev->GetType()) << " got called" << std::endl;
Expand Down Expand Up @@ -1012,4 +1030,6 @@ void CSharpResourceImpl::ResetDelegates() {
OnPlayerBulletHitDelegate = [](auto var, auto var2, auto var3, auto var4) {};

OnVoiceConnectionDelegate = [](auto var) {};

OnServerScriptRPCAnswerDelegate = [](auto var, auto var2, auto var3, auto var4) {};
}
2 changes: 2 additions & 0 deletions client/src/runtime/CSharpResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class CSharpResourceImpl : public alt::IResource::Impl

VoiceConnectionDelegate_t OnVoiceConnectionDelegate = nullptr;

ServerScriptRPCAnswerDelegate_t OnServerScriptRPCAnswerDelegate = nullptr;


bool MakeClient(alt::IResource::CreationInfo* info, std::vector<std::string> files)
{
Expand Down
1 change: 1 addition & 0 deletions client/src/runtime/eventDelegates.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ typedef void (* PlayerStartLeaveVehicleDelegate_t)(void* target, void* player, u
typedef void (* PlayerBulletHitDelegate_t)(uint32_t weapon, void* victim, alt::IBaseObject::Type victimType, position_t pos);

typedef void (* VoiceConnectionDelegate_t)(uint8_t state);
typedef void (* ServerScriptRPCAnswerDelegate_t)(uint16_t answerId, alt::MValueConst** answer, uint64_t size, const char* answerError);
6 changes: 6 additions & 0 deletions server/include/CSharpResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ typedef void (* PedHealDelegate_t)(alt::IPed* target, uint16_t oldHealth, uint16
typedef void (* PlayerStartTalkingDelegate_t)(alt::IPlayer* player);
typedef void (* PlayerStopTalkingDelegate_t)(alt::IPlayer* player);

typedef void (* ClientScriptRPCDelegate_t)(const alt::CEvent* event, alt::IPlayer* target, const char* name, alt::MValueConst** args, uint64_t size, uint16_t answerID);

class CSharpResourceImpl : public alt::IResource::Impl {
void OnEvent(const alt::CEvent* ev) override;

Expand Down Expand Up @@ -351,6 +353,8 @@ class CSharpResourceImpl : public alt::IResource::Impl {
PlayerStartTalkingDelegate_t OnPlayerStartTalkingDelegate = nullptr;
PlayerStopTalkingDelegate_t OnPlayerStopTalkingDelegate = nullptr;

ClientScriptRPCDelegate_t OnClientScriptRPCDelegate = nullptr;

std::vector<CustomInvoker*> invokers;
std::mutex invokersLock = {};
CoreClr* coreClr;
Expand Down Expand Up @@ -571,3 +575,5 @@ EXPORT void CSharpResourceImpl_SetPedHealDelegate(CSharpResourceImpl* resource,

EXPORT void CSharpResourceImpl_SetPlayerStartTalkingDelegate(CSharpResourceImpl* resource, PlayerStartTalkingDelegate_t delegate);
EXPORT void CSharpResourceImpl_SetPlayerStopTalkingDelegate(CSharpResourceImpl* resource, PlayerStopTalkingDelegate_t delegate);

EXPORT void CSharpResourceImpl_SetClientScriptRPCDelegate(CSharpResourceImpl* resource, ClientScriptRPCDelegate_t delegate);
33 changes: 33 additions & 0 deletions server/src/CSharpResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ void CSharpResourceImpl::ResetDelegates()
OnPedDamageDelegate = [](auto var, auto var2, auto var3, auto var4, auto var5, auto var6) {};
OnPedDeathDelegate = [](auto var, auto var2, auto var3, auto var4) {};
OnPedHealDelegate = [](auto var, auto var2, auto var3, auto var4, auto var5) {};

OnClientScriptRPCDelegate = [](auto var, auto var2, auto var3, auto var4, auto var5, auto var6) {};
}

bool CSharpResourceImpl::Start()
Expand Down Expand Up @@ -836,6 +838,32 @@ case alt::CEvent::Type::SYNCED_META_CHANGE:
OnPlayerStopTalkingDelegate(player);
break;
}
case alt::CEvent::Type::CLIENT_SCRIPT_RPC_EVENT:
{
auto clientScriptRPCEvent = dynamic_cast<const alt::CClientScriptRPCEvent*>(ev);

auto target = clientScriptRPCEvent->GetTarget();
if (target == nullptr) return;

auto name = clientScriptRPCEvent->GetName();
auto args = clientScriptRPCEvent->GetArgs();
auto size = args.size();
auto constArgs = new alt::MValueConst*[size];

for (uint64_t i = 0; i < size; i++)
{
constArgs[i] = &args[i];
}

OnClientScriptRPCDelegate(clientScriptRPCEvent,
target,
name.c_str(),
constArgs,
size,
clientScriptRPCEvent->GetAnswerID()
);
break;
}
default:
{
std::cout << "Unhandled server event #" << static_cast<int>(ev->GetType()) << " got called" << std::endl;
Expand Down Expand Up @@ -1377,6 +1405,11 @@ void CSharpResourceImpl_SetPlayerStopTalkingDelegate(CSharpResourceImpl* resourc
resource->OnPlayerStopTalkingDelegate = delegate;
}

void CSharpResourceImpl_SetClientScriptRPCDelegate(CSharpResourceImpl* resource, ClientScriptRPCDelegate_t delegate)
{
resource->OnClientScriptRPCDelegate = delegate;
}

bool CSharpResourceImpl::MakeClient(alt::IResource::CreationInfo* info, std::vector<std::string> files)
{
const std::string clientMain = resource->GetClientMain();
Expand Down

0 comments on commit e3f3f8f

Please sign in to comment.