Jump to content

TheNet and TheWorld documentation?


Recommended Posts

Hey guys, I'm trying to work on a dedicated server client for DST, I've got most of it figured it out but in order for it to have any real features, I need to know what all TheNet and TheWorld actually do. It's my understanding that they're C++ classes, which is why they're not in the source code. So does anyone have even a semi-comprehensive list of what these are?

Link to comment
Share on other sites

They're userdata objects on the LUA-side that have bindings and proxies on the C-side.

Think of them like singletons.

 

TheWorld is the stage in which the map data and world data exists, and has a lot of networked variables.

TheNet is a multitool handler for all things networking related.

 

You can read each object's functions list from a snippet I posted at:

As for any data about the objects such as internal mechanics on the C-side, I think you'll have to find those out for yourself with a debugger and time.

Link to comment
Share on other sites

I think starting with luaL_newmetatable() (lauxlib.h) and lua_register() (lua.h) is the right direction. These two functions are called when registering a C++ class to lua. DST_steam.exe is statically linked to the lua library. (most likely 5.1.5, maybe 5.2.X, "loop in gettable" appeared in DST_steam.exe but only present in lua library 5.2.X and below). 

It's quite obvious which functions are lua library functions and which are game's. Lua library functions are all _cdecl i.e. caller clears stack and in game mostly _thiscall or _stdcall/_fastcall, both require that callee clears stack.

The objective is to find the two lua library calls above and backtrace to "TheWorld" class.

Since lua registration requires the name of the object. I did a quick search in dst_steam.exe, there is one definition of char* "TheWorld" and one global reference.

.......
push    offset aTheworld ; "TheWorld"
push    0FFFFD8EEh ; -10002, i.e. LUA_GLOBALSINDEX defined in lua.h
push    esi ; must be lua_State*
call    sub_700850 ; lua_getfield(esi, -10002, "TheWorld")
......

After some investigation, sub_700850() is lua_getfield(). This is probably a call to lua_getglobal(L, "TheWorld"), see in lua.h:

#define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, (s))

Well.. this is fun. This actually implies that TheWorld is defined in Lua and referenced in C/C++. So I disproved my assumption.

After taking a closer look at source scripts.. just as I suspected: in main.lua:

global("TheWorld")
TheWorld = nil

and in world.lua, MakeWorld():

......
local inst = CreateEntity()
 
assert(TheWorld == nil)
TheWorld = inst
.........
inst.ismastersim = TheNet:GetIsMasterSimulation()
inst.ismastershard = inst.ismastersim and not TheShard:IsSlave()
........
inst.entity:AddTransform()
inst.entity:AddMap()
inst.entity:AddPathfinder()
inst.entity:AddGroundCreep()
inst.entity:AddSoundEmitter()
..........

Conclusion: TheWorld is indeed defined in Lua scripts not C++/C.

Edit: For your question, you probably misunderstood. You can actually find the list of functions under TheWorld. See entityscript.lua since "TheWorld = CreateEntity()". In fact, CreateEntity() is defined in mainfunctions.lua:

function CreateEntity()
local ent = TheSim:CreateEntity()
local guid = ent:GetGUID()
local scr = EntityScript(ent)
Ents[guid] = scr
NumEnts = NumEnts + 1
return scr
end

The interesting part is TheSim:CreateEntity(). I think TheSim is actually from the C/C++ side because a lot of functions under TheSim is not defined in the script.

EDIT2: In DST_steam.exe:

......
push eax
push esi
call sub_4AB9C0
push offset aThesim ; "TheSim"
push 0FFFFD8EEh
push esi
call sub_700A80
......

After some diggin, sub_700A80() is lua_setfield(), which confirms that TheSim is actually from the C side. So to find the list of functions under TheWorld, which is created by TheSim, I think you need to look into TheSim:CreateEntity(), which is C code.

I recommend you look into sub_4AB9C0() - the function call before lua_setfield():

sub_4AB9C0()
......
mov ecx, off_7CD7AC
push ecx
push offset aSMissingMetata ; "%s missing metatable"
push esi
call sub_701270
.....

Looks like one of the execution path complains about missing metatable. Metatable is used to register C++ classes to lua and contains member functions of a class. This is a good starting point. You can backtrace the parameters and see how the metatable is defined and populated, thereby ultimately finding the member functions. But without debugging symbols it's some serious commitment. You basically have to "guess" what each function does. Or you can attach a debugger and break on some member functions to match them with lua function calls.

Edited by secXsQuared
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...