Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: How does dfhack work internally?  (Read 1250 times)

thecodethinker

  • Escaped Lunatic
    • View Profile
How does dfhack work internally?
« on: August 11, 2014, 02:39:22 am »

I'm going into my second year of College as a computer science student and after taking a look at what dfhack does I found myself intrigued by it. I'd really like to contribute to it but I can't seem to figure out how it does it just by looking at the code.

Can anyone tell me how dfhack works internally?

P.S. Sorry if this isn't the right place for this thread.... new here :3
Logged

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: How does dfhack work internally?
« Reply #1 on: August 11, 2014, 02:50:52 am »

It gets loaded as a dll (SDL.dll to be precise) thus works in the same address space as df itself. Then there is HUGE amount of xml's that get parsed by build system and headers and some code is generated from them. Due to fact that dfhack is in same process space and compiled by same compiler as df everything aligns.
There is a bit more trickery with virtual methods. As we could not create objects without having virtual tables, so dfhack reads them and when you create object from df it replaces new objects vtable with one from df.
Also there is a way to interpose vmethods (i.e. replace vmethod with a chain of vmethods from dfhack).

thecodethinker

  • Escaped Lunatic
    • View Profile
Re: How does dfhack work internally?
« Reply #2 on: August 11, 2014, 05:21:37 am »

I was wondering how it always had the same address.

If I wanted to start hacking dfhack... which source file would I want to start looking in?
Logged

Quietust

  • Bay Watcher
  • Does not suffer fools gladly
    • View Profile
    • QMT Productions
Re: How does dfhack work internally?
« Reply #3 on: August 11, 2014, 08:32:21 am »

I was wondering how it always had the same address.
The SDL version of Dwarf Fortress has Address Space Layout Randomization (ASLR) enabled, so it loads at a random address every time. However, it is possible for a Windows application to query its (possibly randomized) load address, so DFHack does this and then adjusts all of its known pointers accordingly.
Logged
P.S. If you don't get this note, let me know and I'll write you another.
It's amazing how dwarves can make a stack of bones completely waterproof and magmaproof.
It's amazing how they can make an entire floodgate out of the bones of 2 cats.