From 6bd67cc055a3e2ec9a7562536d8c51c11a2789d2 Mon Sep 17 00:00:00 2001
From: jj
dfhack.internal.getMemRanges()
Returns a sequence of tables describing virtual memory ranges of the process.
dfhack.internal.memmove(dest,src,count)
+Wraps the standard memmove function. Accepts both numbers and refs as pointers.
+dfhack.internal.memcmp(ptr1,ptr2,count)
+Wraps the standard memcmp function.
+dfhack.internal.memscan(haystack,count,step,needle,nsize)
+Searches for needle of nsize bytes in haystack, +using count steps of step bytes. +Returns: step_idx, sum_idx, found_ptr, or nil if not found.
+dfhack.internal.diffscan(old_data, new_data, start_idx, end_idx, eltsize[, oldval, newval, delta])
+Searches for differences between buffers at ptr1 and ptr2, as integers of size eltsize. +The oldval, newval or delta arguments may be used to specify additional constraints. +Returns: found_index, or nil if end reached.
+dfhack.internal.setAddress(name, value)
Sets the global address name. Returns the value of getAddress before the change.
dfhack.internal.getVTable(name)
+Returns the pre-extracted vtable address name, or nil.
+dfhack.internal.getBase()
Returns the base address of the process.
dfhack.internal.getVTable(name)
Returns the pre-extracted vtable address name, or nil.
dfhack.internal.getBase()
-Returns the base address of the process.
+dfhack.internal.getRebaseDelta()
+Returns the ASLR rebase offset of the DF executable.
dfhack.internal.getMemRanges()
Returns a sequence of tables describing virtual memory ranges of the process.
diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 3693070d0..092404e33 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1036,7 +1036,7 @@ static void *checkaddr(lua_State *L, int idx, bool allow_null = false) return rv; } -static uint32_t getRebaseDelta() { return Core::getInstance().vinfo->getRebaseDelta(); } +static int getRebaseDelta() { return Core::getInstance().vinfo->getRebaseDelta(); } static const LuaWrapper::FunctionReg dfhack_internal_module[] = { WRAP(getRebaseDelta), From f989ef213e69fc2806ac01fa8f98ad88e7e1fcde Mon Sep 17 00:00:00 2001 From: Alexander GavrilovSame as println; intended for errors. Uses red color and logs to stderr.log.
dfhack.color([color])
-Sets the current output color. If color is nil or -1, resets to default.
+Sets the current output color. If color is nil or -1, resets to default. +Returns the previous color value.
dfhack.is_interactive()
Checks if the thread can access the interactive console and returns true or false.
diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 092404e33..b0a085eca 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1074,9 +1074,9 @@ static int internal_setAddress(lua_State *L) } // Print via printerr, so that it is definitely logged to stderr.log. - addr -= Core::getInstance().vinfo->getRebaseDelta(); - std::string msg = stl_sprintf("DFHack utility functions are placed in the dfhack global tree.
-Currently it defines the following features:
+dfhack.print(args...)
Output tab-separated args as standard lua print would do, @@ -753,20 +762,11 @@ this, forcing the function to block on input with lock held.
string, global environment and command-line history file.If the interactive console is not accessible, returns nil, error.
dfhack.pcall(f[,args...])
-Invokes f via xpcall, using an error function that attaches -a stack trace to the error. The same function is used by SafeCall -in C++, and dfhack.safecall.
-The returned error is a table with separate message and -stacktrace string fields; it implements __tostring.
-safecall(f[,args...]), dfhack.safecall(f[,args...])
-Just like pcall, but also prints the error using printerr before -returning. Intended as a convenience function.
-dfhack.saferesume(coroutine[,args...])
-Compares to coroutine.resume like dfhack.safecall vs pcall.
-dfhack.run_script(name[,args...])
Run a lua script in hack/scripts/, as if it was started from dfhack command-line. The name argument should be the name stem, as would be used on the command line. @@ -782,6 +782,32 @@ the lock. It is safe to nest suspends.
to group operations together in one big critical section. A plugin can choose to run all lua code inside a C++-side suspend lock.dfhack.error(msg[,level[,verbose]])
+Throws a dfhack exception object with location and stack trace. +The verbose parameter controls whether the trace is printed by default.
+qerror(msg[,level])
+Calls dfhack.error() with verbose being false. Intended to +be used for user-caused errors in scripts, where stack traces are not +desirable.
+dfhack.pcall(f[,args...])
+Invokes f via xpcall, using an error function that attaches +a stack trace to the error. The same function is used by SafeCall +in C++, and dfhack.safecall.
+safecall(f[,args...]), dfhack.safecall(f[,args...])
+Just like pcall, but also prints the error using printerr before +returning. Intended as a convenience function.
+dfhack.saferesume(coroutine[,args...])
+Compares to coroutine.resume like dfhack.safecall vs pcall.
+dfhack.call_with_finalizer(num_cleanup_args,always,cleanup_fn[,cleanup_args...],fn[,args...])
Invokes fn with args, and after it returns or throws an
error calls cleanup_fn with cleanup_args. Any return values from
@@ -801,9 +827,40 @@ Implemented using call_with_final
dfhack.with_temp_object(obj,fn[,args...]) Calls fn(obj,args...), then finalizes with obj:delete(). dfhack.exception Metatable of error objects used by dfhack. The objects have the
+following properties: The location prefix string, or nil. The base message string. The stack trace string, or nil. A different exception object, or nil. The coroutine that has thrown the exception. Boolean, or nil; specifies if where and stacktrace should be printed. Converts the exception to string. dfhack.exception.verbose The default value of the verbose argument of err:tostring().
+
+
This api is intended for storing configuration options in the world itself. It probably should be restricted to data that is world-dependent.
Entries are identified by a string key, but it is also possible to manage @@ -838,7 +895,7 @@ functions can just copy values in memory without doing any actual I/O. However, currently every entry has a 180+-byte dead-weight overhead.
A material info record has fields:
type, index, material
@@ -881,8 +938,9 @@ Accept dfhack_material_category auto-assign table.Thin wrappers around C++ functions, similar to the ones for virtual methods. One notable difference is that these explicit wrappers allow argument count adjustment according to the usual lua rules, so trailing false/nil arguments @@ -911,7 +969,7 @@ can be omitted.
dfhack.gui.getCurViewscreen()
Returns the viewscreen that is current in the core.
@@ -947,7 +1005,7 @@ The is_bright boolean actually seems to invert the brightness.dfhack.job.cloneJobStruct(job)
Creates a deep copy of the given job.
@@ -984,7 +1042,7 @@ a lua list containing them.dfhack.units.getPosition(unit)
Returns true x,y,z of the unit, or nil if invalid; may be not equal to unit.pos if caged.
@@ -1038,7 +1096,7 @@ or raws. The ignore_noble boolean disables thedfhack.items.getPosition(item)
Returns true x,y,z of the item, or nil if invalid; may be not equal to item.pos if in inventory.
@@ -1081,7 +1139,7 @@ Returns false in case of error.dfhack.maps.getSize()
Returns map size in blocks: x, y, z
@@ -1122,7 +1180,7 @@ burrows, or the presence of invaders.dfhack.burrows.findByName(name)
Returns the burrow pointer or nil.
@@ -1157,7 +1215,7 @@ burrows, or the presence of invaders.dfhack.buildings.getSize(building)
Returns width, height, centerx, centery.
@@ -1297,7 +1355,7 @@ can be determined this way, constructBuildingdfhack.constructions.designateNew(pos,type,item_type,mat_index)
Designates a new construction at given position. If there already is @@ -1313,7 +1371,7 @@ Returns true, was_only_planned if removed; or false if none fo
These functions are intended for the use by dfhack developers, and are only documented here for completeness:
While plugins can create any number of interpreter instances, there is one special context managed by dfhack core. It is the only context that can receive events from DF and plugins.
@@ -1387,7 +1445,7 @@ Using timeout_active(id,nil) cancels the timerAn event is just a lua table with a predefined metatable that contains a __call metamethod. When it is invoked, it loops through the table with next and calls all contained values. @@ -1413,14 +1471,14 @@ order using dfhack.safecall.
DFHack plugins may export native functions and events to lua contexts. They are automatically imported by mkmodule('plugins.<name>'); this means that a lua module file is still necessary for require to read.
The following plugins have lua support.
Implements extended burrow manipulations.
Events:
The lua module file also re-exports functions from dfhack.burrows.
Does not export any native functions as of now. Instead, it calls lua code to perform the actual ordering of list items.
Contents
The current version of DFHack has extensive support for +the Lua scripting language, providing access to:
+Lua code can be used both for writing scripts, which +are treated by DFHack command line prompt almost as +native C++ commands, and invoked by plugins written in c++.
+This document describes native API available to Lua in detail. +For the most part it does not describe utility functions +implemented by Lua files located in hack/lua/...
+DF structures described by the xml files in library/xml are exported to lua code as a tree of objects and functions under the df global, which broadly maps to the df namespace in C++.
@@ -764,28 +780,8 @@ string, global environment and command-line history file.dfhack.run_script(name[,args...])
-Run a lua script in hack/scripts/, as if it was started from dfhack command-line. -The name argument should be the name stem, as would be used on the command line. -Note that the script is re-read from the file every time it is called, and errors -are propagated to the caller.
-dfhack.with_suspend(f[,args...])
-Calls f with arguments after grabbing the DF core suspend lock. -Suspending is necessary for accessing a consistent state of DF memory.
-Returned values and errors are propagated through after releasing -the lock. It is safe to nest suspends.
-Every thread is allowed only one suspend per DF frame, so it is best -to group operations together in one big critical section. A plugin -can choose to run all lua code inside a C++-side suspend lock.
-dfhack.error(msg[,level[,verbose]])
Throws a dfhack exception object with location and stack trace. @@ -808,25 +804,6 @@ returning. Intended as a convenience function.
dfhack.saferesume(coroutine[,args...])
Compares to coroutine.resume like dfhack.safecall vs pcall.
dfhack.call_with_finalizer(num_cleanup_args,always,cleanup_fn[,cleanup_args...],fn[,args...])
-Invokes fn with args, and after it returns or throws an -error calls cleanup_fn with cleanup_args. Any return values from -fn are propagated, and errors are re-thrown.
-The num_cleanup_args integer specifies the number of cleanup_args, -and the always boolean specifies if cleanup should be called in any case, -or only in case of an error.
-dfhack.with_finalize(cleanup_fn,fn[,args...])
-Calls fn with arguments, then finalizes with cleanup_fn. -Implemented using call_with_finalizer(0,true,...).
-dfhack.with_onerror(cleanup_fn,fn[,args...])
-Calls fn with arguments, then finalizes with cleanup_fn on any thrown error. -Implemented using call_with_finalizer(0,false,...).
-dfhack.with_temp_object(obj,fn[,args...])
-Calls fn(obj,args...), then finalizes with obj:delete().
-dfhack.exception
Metatable of error objects used by dfhack. The objects have the following properties:
@@ -859,6 +836,39 @@ following properties:dfhack.with_suspend(f[,args...])
+Calls f with arguments after grabbing the DF core suspend lock. +Suspending is necessary for accessing a consistent state of DF memory.
+Returned values and errors are propagated through after releasing +the lock. It is safe to nest suspends.
+Every thread is allowed only one suspend per DF frame, so it is best +to group operations together in one big critical section. A plugin +can choose to run all lua code inside a C++-side suspend lock.
+dfhack.call_with_finalizer(num_cleanup_args,always,cleanup_fn[,cleanup_args...],fn[,args...])
+Invokes fn with args, and after it returns or throws an +error calls cleanup_fn with cleanup_args. Any return values from +fn are propagated, and errors are re-thrown.
+The num_cleanup_args integer specifies the number of cleanup_args, +and the always boolean specifies if cleanup should be called in any case, +or only in case of an error.
+dfhack.with_finalize(cleanup_fn,fn[,args...])
+Calls fn with arguments, then finalizes with cleanup_fn. +Implemented using call_with_finalizer(0,true,...).
+dfhack.with_onerror(cleanup_fn,fn[,args...])
+Calls fn with arguments, then finalizes with cleanup_fn on any thrown error. +Implemented using call_with_finalizer(0,false,...).
+dfhack.with_temp_object(obj,fn[,args...])
+Calls fn(obj,args...), then finalizes with obj:delete().
+This api is intended for storing configuration options in the world itself. @@ -1470,15 +1480,45 @@ order using dfhack.safecall.
DFHack sets up the lua interpreter so that the built-in require +function can be used to load shared lua code from hack/lua/. +The dfhack namespace reference itself may be obtained via +require('dfhack'), although it is initially created as a +global by C++ bootstrap code.
+The following functions are provided:
+mkmodule(name)
+Creates an environment table for the module. Intended to be used as:
++local _ENV = mkmodule('foo') +... +return _ENV ++
If called the second time, returns the same table; thus providing reload support.
+reload(name)
+Reloads a previously require-d module "name" from the file. +Intended as a help for module development.
+dfhack.BASE_G
+This variable contains the root global environment table, which is +used as a base for all module and script environments. Its contents +should be kept limited to the standard Lua library and API described +in this document.
+DFHack plugins may export native functions and events to lua contexts. They are automatically imported by mkmodule('plugins.<name>'); this means that a lua module file is still necessary for require to read.
The following plugins have lua support.
Any files with the .lua extension placed into hack/scripts/* +are automatically used by the DFHack core as commands. The +matching command name consists of the name of the file sans +the extension.
+NOTE: Scripts placed in subdirectories still can be accessed, but +do not clutter the ls command list; thus it is preferred +for obscure developer-oriented scripts and scripts used by tools. +When calling such scripts, always use '/' as the separator for +directories, e.g. devel/lua-example.
+Scripts are re-read from disk every time they are used +(this may be changed later to check the file change time); however +the global variable values persist in memory between calls. +Every script gets its own separate environment for global +variables.
+Arguments are passed in to the scripts via the ... built-in +quasi-variable; when the script is called by the DFHack core, +they are all guaranteed to be non-nil strings.
+DFHack core invokes the scripts in the core context (see above); +however it is possible to call them from any lua code (including +from other scripts) in any context, via the same function the core uses:
+dfhack.run_script(name[,args...])
+Run a lua script in hack/scripts/, as if it was started from dfhack command-line. +The name argument should be the name stem, as would be used on the command line.
+Note that this function lets errors propagate to the caller.
+