Comments (3)
So there are multiple issues at hand.
I want to detour calls to functions Foo, Bar, and Baz to my own implementations, but I want these functions to continue using the existing D2 logic for everything else so the mod can coexist with other mods and to use code that is known to be working. Half of this issue is something that will disappear as this codebase grows
There are multiple ways to do this, but essentially if you want to use D2Moo's patching mechanism you'll need to rely on doing 2 things:
- Detours the target function you want to replace
- Call the original functions instead of D2Moo's
The first one is easy to do with pretty much any patching mechanism, including D2Moo's.
The second one is a bit more tricky, you will need to either:
- Keep the current code, and patch D2Moo functions you call to call the original ones instead
- Change the code to call functions you manually loaded
If using D2Moo's patching mechanism, if you need to patch ordinal functions(or pointers) this couldn't be easier, just change the patch action as done here for datatables:
D2MOO/D2.Detours.patches/1.10f/D2Common.patch.cpp
Lines 43 to 44 in dff4668
If you need to patch something that is not an ordinal, you'll need to use the extra patch actions
D2MOO/D2.Detours.patches/1.10f/D2Common.patch.cpp
Line 1376 in dff4668
If you need to call the original function from your detoured version, you'll also want to use extra patch actions by specifying a pointer that can store the original function address, much like what the debugger does:
In theory, we wouldn't need to patch D2Moo to call the original code if:
- we were done writing/fixing all the functions
- we didn't care about other patches being installed by "3rd party" mods
One alternative would be to use only D2Moo's headers, write your own patching DLLs and/or mechanism, and only write the functions you want to patch. As soon as would try to call a function that does not exist in your DLL and that is not an imported ordinal, you would end up with a linker error saying it can't find the function. You could then implement it as a simple call to the original function that you loaded with GetProcAddress()
. This is what the macros D2FUNC
does (in a not really optimized/pretty way). Example in the debugger:
I hope this can help and answers your question. If I can help making the process easier, don't hesitate to suggest changes to the project. I know one of the current pain points is that currently one .dll can only patch one original .dll. (This was kind of done on purpose to avoid mixing code from multiple .dlls and export ordinals that shouldn't be)
from d2moo.
Reading the list I'd prefer the "subproject - Import D2MOO .dlls and patch the functions you want" If there was a vote that is what I'd choose
from d2moo.
I'm likely doing it wrong and forcing this project to do something it really isn't geared towards, but I had to end up going with copy-paste. I want to detour calls to functions Foo, Bar, and Baz to my own implementations, but I want these functions to continue using the existing D2 logic for everything else so the mod can coexist with other mods and to use code that is known to be working. Half of this issue is something that will disappear as this codebase grows
for instance
SKILLS_GetSkillsTxtRecord_t SKILLS_GetSkillsTxtRecord = nullptr;
...
SKILLS_GetSkillsTxtRecordFromSkill = (SKILLS_GetSkillsTxtRecordFromSkill_t)(D2CommonImageBase + 0x71540);
...
int32_t __fastcall My_SKILLS_SrvSt58_FireClaws(D2GameStrc* pGame, D2UnitStrc* pUnit, int32_t nSkillId, int32_t nSkillLevel)
{
D2SkillsTxt* pSkillsTxtRecord = SKILLS_GetSkillsTxtRecord(nSkillId); // Call the original SKILLS_GetSkillsTxtRecord
...
MyD2DamageStrc damage = {};
My_sub_6FD155E0(pGame, pUnit, pTarget, pSkillsTxtRecord, nSkillId, nSkillLevel, &damage, 0); // Call my custom implementation of sub_6FD155E0
...
}
Let's say I had a custom My_SKILLS_SrvSt58_FireClaws. I want it to still call SKILLS_GetSkillsTxtRecord from the loaded D2Common library instead of the D2Moo implementation (It's just an example, assume these are both from the same dll and a bit more complex). I do this because I want to call functions that other mods may have injected their own code into and because it's guaranteed to be a correct implementation since it's using the original logic.
I started off as "subproject - Configure CMake to use your own patch files and .def" and this was working well until I started hitting issues with other mods and running into not yet complete logic in this codebase. Am I doing something completely wrong, or do I just have a not so great usecase at the moment?
from d2moo.
Related Issues (20)
- Improve the debugger !
- Choose better names for D2RoomStrc and D2RoomExStrc
- Minion monster AI Param `att1/att2?` not working
- Minor naming updates to StatList functions
- SUnitEvent.h structs members need cleaning/renaming
- typos HOT 2
- padding in D2QuestDataStrc / D2QuestGUIDStrc HOT 1
- D2GAME_SpawnSuperUnique_6FC6F690
- MONSTER_InitializeStatsAndSkills
- sub_6FCFEDD0
- D2Game.0x6FC55360 HOT 5
- D2Game.0x6FCBB6C0
- D2Game.0x6FCF3000 HOT 2
- D2Game.0x6FC6FF10
- //D2Game.0x6FC6EC10
- //D2Game.0x6FC90010
- CLIENTS_RemoveClientFromListWithId may remove from wrong list
- `MONSTERREGION_InitializeAll` Uses Incorrect Sizes
- Use `const char*` as param to FnLeaveGame as it is indeed a null terminated string.
- `FnSetGameData` should be `FnCreateClientInfo = D2ClientInfoStrc*(__fastcall*)(D2ClientStrc* pClient);`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from d2moo.