Raised This Month: $ Target: $400
 0% 

Declare one hook in multiple cpp files


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kadet.89
Veteran Member
Join Date: Nov 2012
Location: Serbia
Old 05-27-2021 , 17:41   Declare one hook in multiple cpp files
Reply With Quote #1

I need to hook IServerEntity::SetModelIndex founction in multiple cpp files.
I do it this way in the first file:
SH_DECL_HOOK1_void(IServerEntity, SetModelIndex, SH_NOATTRIB, 0, int)
and in the second:
SH_DECL_HOOK1_void(IServerEntity, SetModelIndex, SH_NOATTRIB, 1, int)

I get this error:
PHP Code:
/usr/bin/lderrorfileone.omultiple definition of '__SourceHook_FHAddIServerEntitySetModelIndex(void*, SourceHook::ISourceHook::AddHookMode, bool, fastdelegate::FastDelegate1<int, void>)'
/usr/bin/ldfiletwo.oprevious definition here
/usr/bin/lderrorfileone.omultiple definition of '__SourceHook_FHRemoveIServerEntitySetModelIndex(void*, bool, fastdelegate::FastDelegate1<int, void>)'
/usr/bin/ldfiletwo.oprevious definition here
collect2
errorld returned 1 exit status 
Is there a beautiful way to solve this problem?
kadet.89 is offline
Send a message via Skype™ to kadet.89
RomanPort
New Member
Join Date: Oct 2021
Old 12-31-2021 , 04:16   Re: Declare one hook in multiple cpp files
Reply With Quote #2

Hello! I just encountered the same problem and searched on this board for help. Surprisingly, this is the only mention I could find. I was stumped on this for a while, but just found the solution: typedefs.

Like you, I had the same hook declared in two different files and I was also getting errors about objects already existing. The reason why is that the SH_DECL_HOOK macro actually declares some variables that you can't see, and there are two with identical names created when you have two of those definitions. Since the name of these variables is derived from the interface name and function, if we can change the name, it'll work fine.

That's where typedefs come in. Typedefs are basically just an alias for a type name. If you set up an alias for the interface in one of the two files with a different name, you can declare the hooks using that new name instead and it won't conflict. It doesn't matter what your alias is as long as it's different from the old name.

At the top of one of your two files, add this line, replacing the two parts of it:

Code:
typedef <EXISTING TYPE NAME> <YOUR NEW TYPE NAME>;
Then, in the rest of just that file, replace any names of the original type (IServerEntity in your case) with your new alias name in any hook-related macros. From there it should compile fine.

tl;dr;

Add this line to the top of ONE of the two files: typedef IServerEntity IServerEntityFileOne;
Then, replace "IServerEntity" with "IServerEntityFileOne" everywhere else hook-related in that one file.
RomanPort is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:23.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode