Merge branch 'master' of git://github.com/peterix/dfhack
commit
511fceff0a
@ -0,0 +1,499 @@
|
|||||||
|
##############
|
||||||
|
DFHack Lua API
|
||||||
|
##############
|
||||||
|
|
||||||
|
.. contents::
|
||||||
|
|
||||||
|
====================
|
||||||
|
DF structure wrapper
|
||||||
|
====================
|
||||||
|
|
||||||
|
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++.
|
||||||
|
|
||||||
|
**WARNING**: The wrapper provides almost raw access to the memory
|
||||||
|
of the game, so mistakes in manipulating objects are as likely to
|
||||||
|
crash the game as equivalent plain C++ code would be. E.g. NULL
|
||||||
|
pointer access is safely detected, but dangling pointers aren't.
|
||||||
|
|
||||||
|
Objects managed by the wrapper can be broadly classified into the following groups:
|
||||||
|
|
||||||
|
1. Typed object pointers (references).
|
||||||
|
|
||||||
|
References represent objects in DF memory with a known type.
|
||||||
|
|
||||||
|
In addition to fields and methods defined by the wrapped type,
|
||||||
|
every reference has some built-in properties and methods.
|
||||||
|
|
||||||
|
2. Untyped pointers
|
||||||
|
|
||||||
|
Represented as lightuserdata.
|
||||||
|
|
||||||
|
In assignment to a pointer NULL can be represented either as
|
||||||
|
``nil``, or a NULL lightuserdata; reading a NULL pointer field
|
||||||
|
returns ``nil``.
|
||||||
|
|
||||||
|
3. Named types
|
||||||
|
|
||||||
|
Objects in the ``df`` tree that represent identity of struct, class,
|
||||||
|
enum and bitfield types. They host nested named types, static
|
||||||
|
methods, builtin properties & methods, and, for enums and bitfields,
|
||||||
|
the bi-directional mapping between key names and values.
|
||||||
|
|
||||||
|
4. The ``global`` object
|
||||||
|
|
||||||
|
``df.global`` corresponds to the ``df::global`` namespace, and
|
||||||
|
behaves as a mix between a named type and a reference, containing
|
||||||
|
both nested types and fields corresponding to global symbols.
|
||||||
|
|
||||||
|
In addition to the ``global`` object and top-level types the ``df``
|
||||||
|
global also contains a few global builtin utility functions.
|
||||||
|
|
||||||
|
Typed object references
|
||||||
|
=======================
|
||||||
|
|
||||||
|
The underlying primitive lua object is userdata with a metatable.
|
||||||
|
Every structured field access produces a new userdata instance.
|
||||||
|
|
||||||
|
All typed objects have the following built-in features:
|
||||||
|
|
||||||
|
* ``ref1 == ref2``, ``tostring(ref)``
|
||||||
|
|
||||||
|
References implement equality by type & pointer value, and string conversion.
|
||||||
|
|
||||||
|
* ``pairs(ref)``
|
||||||
|
|
||||||
|
Returns an iterator for the sequence of actual C++ field names
|
||||||
|
and values. Fields are enumerated in memory order. Methods and
|
||||||
|
lua wrapper properties are not included in the iteration.
|
||||||
|
|
||||||
|
* ``ref._kind``
|
||||||
|
|
||||||
|
Returns one of: ``primitive``, ``struct``, ``container``,
|
||||||
|
or ``bitfield``, as appropriate for the referenced object.
|
||||||
|
|
||||||
|
* ``ref._type``
|
||||||
|
|
||||||
|
Returns the named type object or a string that represents
|
||||||
|
the referenced object type.
|
||||||
|
|
||||||
|
* ``ref:sizeof()``
|
||||||
|
|
||||||
|
Returns *size, address*
|
||||||
|
|
||||||
|
* ``ref:new()``
|
||||||
|
|
||||||
|
Allocates a new instance of the same type, and copies data
|
||||||
|
from the current object.
|
||||||
|
|
||||||
|
* ``ref:delete()``
|
||||||
|
|
||||||
|
Destroys the object with the C++ ``delete`` operator.
|
||||||
|
If destructor is not available, returns *false*.
|
||||||
|
|
||||||
|
**WARNING**: the lua reference object remains as a dangling
|
||||||
|
pointer, like a raw C++ pointer would.
|
||||||
|
|
||||||
|
* ``ref:assign(object)``
|
||||||
|
|
||||||
|
Assigns data from object to ref. Object must either be another
|
||||||
|
ref of a compatible type, or a lua table; in the latter case
|
||||||
|
special recursive assignment rules are applied.
|
||||||
|
|
||||||
|
* ``ref:_displace(index[,step])``
|
||||||
|
|
||||||
|
Returns a new reference with the pointer adjusted by index*step.
|
||||||
|
Step defaults to the natural object size.
|
||||||
|
|
||||||
|
Primitive references
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
References of the *_kind* ``'primitive'`` are used for objects
|
||||||
|
that don't fit any of the other reference types. Such
|
||||||
|
references can only appear as a value of a pointer field,
|
||||||
|
or as a result of calling the ``_field()`` method.
|
||||||
|
|
||||||
|
They behave as structs with one field ``value`` of the right type.
|
||||||
|
|
||||||
|
Struct references
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
Struct references are used for class and struct objects.
|
||||||
|
|
||||||
|
They implement the following features:
|
||||||
|
|
||||||
|
* ``ref.field``, ``ref.field = value``
|
||||||
|
|
||||||
|
Valid fields of the structure may be accessed by subscript.
|
||||||
|
|
||||||
|
Primitive typed fields, i.e. numbers & strings, are converted
|
||||||
|
to/from matching lua values. The value of a pointer is a reference
|
||||||
|
to the target, or nil/NULL. Complex types are represented by
|
||||||
|
a reference to the field within the structure; unless recursive
|
||||||
|
lua table assignment is used, such fields can only be read.
|
||||||
|
|
||||||
|
**NOTE:** In case of inheritance, *superclass* fields have precedence
|
||||||
|
over the subclass, but fields shadowed in this way can still
|
||||||
|
be accessed as ``ref['subclasstype.field']``.
|
||||||
|
This shadowing order is necessary because vtable-based classes
|
||||||
|
are automatically exposed in their exact type, and the reverse
|
||||||
|
rule would make access to superclass fields unreliable.
|
||||||
|
|
||||||
|
* ``ref._field(field)``
|
||||||
|
|
||||||
|
Returns a reference to a valid field. That is, unlike regular
|
||||||
|
subscript, it returns a reference to the field within the structure
|
||||||
|
even for primitive typed fields and pointers.
|
||||||
|
|
||||||
|
* ``ref:vmethod(args...)``
|
||||||
|
|
||||||
|
Named virtual methods are also exposed, subject to the same
|
||||||
|
shadowing rules.
|
||||||
|
|
||||||
|
* ``pairs(ref)``
|
||||||
|
|
||||||
|
Enumerates all real fields (but not methods) in memory
|
||||||
|
(= declaration) order.
|
||||||
|
|
||||||
|
Container references
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
Containers represent vectors and arrays, possibly resizable.
|
||||||
|
|
||||||
|
A container field can associate an enum to the container
|
||||||
|
reference, which allows accessing elements using string keys
|
||||||
|
instead of numerical indices.
|
||||||
|
|
||||||
|
Implemented features:
|
||||||
|
|
||||||
|
* ``ref._enum``
|
||||||
|
|
||||||
|
If the container has an associated enum, returns the matching
|
||||||
|
named type object.
|
||||||
|
|
||||||
|
* ``#ref``
|
||||||
|
|
||||||
|
Returns the *length* of the container.
|
||||||
|
|
||||||
|
* ``ref[index]``
|
||||||
|
|
||||||
|
Accesses the container element, using either a *0-based* numerical
|
||||||
|
index, or, if an enum is associated, a valid enum key string.
|
||||||
|
|
||||||
|
Accessing an invalid index is an error, but some container types
|
||||||
|
may return a default value, or auto-resize instead for convenience.
|
||||||
|
Currently this relaxed mode is implemented by df-flagarray aka BitArray.
|
||||||
|
|
||||||
|
* ``ref._field(index)``
|
||||||
|
|
||||||
|
Like with structs, returns a pointer to the array element, if possible.
|
||||||
|
Flag and bit arrays cannot return such pointer, so it fails with an error.
|
||||||
|
|
||||||
|
* ``pairs(ref)``, ``ipairs(ref)``
|
||||||
|
|
||||||
|
If the container has no associated enum, both behave identically,
|
||||||
|
iterating over numerical indices in order. Otherwise, ipairs still
|
||||||
|
uses numbers, while pairs tries to substitute enum keys whenever
|
||||||
|
possible.
|
||||||
|
|
||||||
|
* ``ref:resize(new_size)``
|
||||||
|
|
||||||
|
Resizes the container if supported, or fails with an error.
|
||||||
|
|
||||||
|
* ``ref:insert(index,item)``
|
||||||
|
|
||||||
|
Inserts a new item at the specified index. To add at the end,
|
||||||
|
use ``#ref`` as index.
|
||||||
|
|
||||||
|
* ``ref:erase(index)``
|
||||||
|
|
||||||
|
Removes the element at the given valid index.
|
||||||
|
|
||||||
|
Bitfield references
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Bitfields behave like special fixed-size containers.
|
||||||
|
The ``_enum`` property points to the bitfield type.
|
||||||
|
|
||||||
|
Numerical indices correspond to the shift value,
|
||||||
|
and if a subfield occupies multiple bits, the
|
||||||
|
``ipairs`` order would have a gap.
|
||||||
|
|
||||||
|
Named types
|
||||||
|
===========
|
||||||
|
|
||||||
|
Named types are exposed in the ``df`` tree with names identical
|
||||||
|
to the C++ version, except for the ``::`` vs ``.`` difference.
|
||||||
|
|
||||||
|
All types and the global object have the following features:
|
||||||
|
|
||||||
|
* ``type._kind``
|
||||||
|
|
||||||
|
Evaluates to one of ``struct-type``, ``class-type``, ``enum-type``,
|
||||||
|
``bitfield-type`` or ``global``.
|
||||||
|
|
||||||
|
* ``type._identity``
|
||||||
|
|
||||||
|
Contains a lightuserdata pointing to the underlying
|
||||||
|
DFHack::type_instance object.
|
||||||
|
|
||||||
|
Types excluding the global object also support:
|
||||||
|
|
||||||
|
* ``type:sizeof()``
|
||||||
|
|
||||||
|
Returns the size of an object of the type.
|
||||||
|
|
||||||
|
* ``type:new()``
|
||||||
|
|
||||||
|
Creates a new instance of an object of the type.
|
||||||
|
|
||||||
|
* ``type:is_instance(object)``
|
||||||
|
|
||||||
|
Returns true if object is same or subclass type, or a reference
|
||||||
|
to an object of same or subclass type. It is permissible to pass
|
||||||
|
nil, NULL or non-wrapper value as object; in this case the
|
||||||
|
method returns nil.
|
||||||
|
|
||||||
|
In addition to this, enum and bitfield types contain a
|
||||||
|
bi-directional mapping between key strings and values, and
|
||||||
|
also map ``_first_item`` and ``_last_item`` to the min and
|
||||||
|
max values.
|
||||||
|
|
||||||
|
Struct and class types with instance-vector attribute in the
|
||||||
|
xml have a ``type.find(key)`` function that wraps the find
|
||||||
|
method provided in C++.
|
||||||
|
|
||||||
|
Global functions
|
||||||
|
================
|
||||||
|
|
||||||
|
The ``df`` table itself contains the following functions and values:
|
||||||
|
|
||||||
|
* ``NULL``, ``df.NULL``
|
||||||
|
|
||||||
|
Contains the NULL lightuserdata.
|
||||||
|
|
||||||
|
* ``df.isnull(obj)``
|
||||||
|
|
||||||
|
Evaluates to true if obj is nil or NULL; false otherwise.
|
||||||
|
|
||||||
|
* ``df.isvalid(obj[,allow_null])``
|
||||||
|
|
||||||
|
For supported objects returns one of ``type``, ``voidptr``, ``ref``.
|
||||||
|
|
||||||
|
If *allow_null* is true, and obj is nil or NULL, returns ``null``.
|
||||||
|
|
||||||
|
Otherwise returns *nil*.
|
||||||
|
|
||||||
|
* ``df.sizeof(obj)``
|
||||||
|
|
||||||
|
For types and refs identical to ``obj:sizeof()``.
|
||||||
|
For lightuserdata returns *nil, address*
|
||||||
|
|
||||||
|
* ``df.new(obj)``, ``df.delete(obj)``, ``df.assign(obj, obj2)``
|
||||||
|
|
||||||
|
Equivalent to using the matching methods of obj.
|
||||||
|
|
||||||
|
* ``df._displace(obj,index[,step])``
|
||||||
|
|
||||||
|
For refs equivalent to the method, but also works with
|
||||||
|
lightuserdata (step is mandatory then).
|
||||||
|
|
||||||
|
* ``df.is_instance(type,obj)``
|
||||||
|
|
||||||
|
Equivalent to the method, but also allows a reference as proxy for its type.
|
||||||
|
|
||||||
|
Recursive table assignment
|
||||||
|
==========================
|
||||||
|
|
||||||
|
Recursive assignment is invoked when a lua table is assigned
|
||||||
|
to a C++ object or field, i.e. one of:
|
||||||
|
|
||||||
|
* ``ref:assign{...}``
|
||||||
|
* ``ref.field = {...}``
|
||||||
|
|
||||||
|
The general mode of operation is that all fields of the table
|
||||||
|
are assigned to the fields of the target structure, roughly
|
||||||
|
emulating the following code::
|
||||||
|
|
||||||
|
function rec_assign(ref,table)
|
||||||
|
for key,value in pairs(table) do
|
||||||
|
ref[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Since assigning a table to a field using = invokes the same
|
||||||
|
process, it is recursive.
|
||||||
|
|
||||||
|
There are however some variations to this process depending
|
||||||
|
on the type of the field being assigned to:
|
||||||
|
|
||||||
|
1. If the table contains an ``assign`` field, it is
|
||||||
|
applied first, using the ``ref:assign(value)`` method.
|
||||||
|
It is never assigned as a usual field.
|
||||||
|
|
||||||
|
2. When a table is assigned to a non-NULL pointer field
|
||||||
|
using the ``ref.field = {...}`` syntax, it is applied
|
||||||
|
to the target of the pointer instead.
|
||||||
|
|
||||||
|
If the pointer is NULL, the table is checked for a ``new`` field:
|
||||||
|
|
||||||
|
a. If it is *nil* or *false*, assignment fails with an error.
|
||||||
|
|
||||||
|
b. If it is *true*, the pointer is initialized with a newly
|
||||||
|
allocated object of the declared target type of the pointer.
|
||||||
|
|
||||||
|
c. Otherwise, ``table.new`` must be a named type, or an
|
||||||
|
object of a type compatible with the pointer. The pointer
|
||||||
|
is initialized with the result of calling ``table.new:new()``.
|
||||||
|
|
||||||
|
After this auto-vivification process, assignment proceeds
|
||||||
|
as if the pointer wasn't NULL.
|
||||||
|
|
||||||
|
Obviously, the ``new`` field inside the table is always skipped
|
||||||
|
during the actual per-field assignment processing.
|
||||||
|
|
||||||
|
3. If the target of the assignment is a container, a separate
|
||||||
|
rule set is used:
|
||||||
|
|
||||||
|
a. If the table contains neither ``assign`` nor ``resize``
|
||||||
|
fields, it is interpreted as an ordinary *1-based* lua
|
||||||
|
array. The container is resized to the #-size of the
|
||||||
|
table, and elements are assigned in numeric order::
|
||||||
|
|
||||||
|
ref:resize(#table);
|
||||||
|
for i=1,#table do ref[i-1] = table[i] end
|
||||||
|
|
||||||
|
b. Otherwise, ``resize`` must be *true*, *false*, or
|
||||||
|
an explicit number. If it is not false, the container
|
||||||
|
is resized. After that the usual struct-like 'pairs'
|
||||||
|
assignment is performed.
|
||||||
|
|
||||||
|
In case ``resize`` is *true*, the size is computed
|
||||||
|
by scanning the table for the largest numeric key.
|
||||||
|
|
||||||
|
This means that in order to reassign only one element of
|
||||||
|
a container using this system, it is necessary to use::
|
||||||
|
|
||||||
|
{ resize=false, [idx]=value }
|
||||||
|
|
||||||
|
Since nil inside a table is indistinguishable from missing key,
|
||||||
|
it is necessary to use ``df.NULL`` as a null pointer value.
|
||||||
|
|
||||||
|
This system is intended as a way to define a nested object
|
||||||
|
tree using pure lua data structures, and then materialize it in
|
||||||
|
C++ memory in one go. Note that if pointer auto-vivification
|
||||||
|
is used, an error in the middle of the recursive walk would
|
||||||
|
not destroy any objects allocated in this way, so the user
|
||||||
|
should be prepared to catch the error and do the necessary
|
||||||
|
cleanup.
|
||||||
|
|
||||||
|
================
|
||||||
|
DFHack utilities
|
||||||
|
================
|
||||||
|
|
||||||
|
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,
|
||||||
|
but without a newline.
|
||||||
|
|
||||||
|
* ``print(args...)``, ``dfhack.println(args...)``
|
||||||
|
|
||||||
|
A replacement of the standard library print function that
|
||||||
|
works with DFHack output infrastructure.
|
||||||
|
|
||||||
|
* ``dfhack.printerr(args...)``
|
||||||
|
|
||||||
|
Same 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.
|
||||||
|
|
||||||
|
* ``dfhack.is_interactive()``
|
||||||
|
|
||||||
|
Checks if the thread can access the interactive console and returns *true* or *false*.
|
||||||
|
|
||||||
|
* ``dfhack.lineedit([prompt[,history_filename]])``
|
||||||
|
|
||||||
|
If the thread owns the interactive console, shows a prompt
|
||||||
|
and returns the entered string. Otherwise returns *nil, error*.
|
||||||
|
|
||||||
|
* ``dfhack.interpreter([prompt[,env[,history_filename]]])``
|
||||||
|
|
||||||
|
Starts an interactive lua interpreter, using the specified prompt
|
||||||
|
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.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.
|
||||||
|
|
||||||
|
Persistent configuration storage
|
||||||
|
================================
|
||||||
|
|
||||||
|
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
|
||||||
|
multiple entries with the same key; their identity is determined by ``entry_id``.
|
||||||
|
Every entry has a mutable string ``value``, and an array of 7 mutable ``ints``.
|
||||||
|
|
||||||
|
* ``dfhack.persistent.get(key)``, ``entry:get()``
|
||||||
|
|
||||||
|
Retrieves a persistent config record with the given string key,
|
||||||
|
or refreshes an already retrieved entry. If there are multiple
|
||||||
|
entries with the same key, it is undefined which one is retrieved
|
||||||
|
by the first version of the call.
|
||||||
|
|
||||||
|
Returns entry, or *nil* if not found.
|
||||||
|
|
||||||
|
* ``dfhack.persistent.delete(key)``, ``entry:delete()``
|
||||||
|
|
||||||
|
Removes an existing entry. Returns *true* if succeeded.
|
||||||
|
|
||||||
|
* ``dfhack.persistent.get_all(key[,match_prefix])``
|
||||||
|
|
||||||
|
Retrieves all entries with the same key, or starting with key..'/'.
|
||||||
|
Calling ``get_all('',true)`` will match all entries.
|
||||||
|
|
||||||
|
If none found, returns nil; otherwise returns an array of entries.
|
||||||
|
|
||||||
|
* ``dfhack.persistent.save({key=str1, ...}[,new])``, ``entry:save([new])``
|
||||||
|
|
||||||
|
Saves changes in an entry, or creates a new one. Passing true as
|
||||||
|
new forces creation of a new entry even if one already exists;
|
||||||
|
otherwise the existing one is simply updated.
|
||||||
|
Returns *entry, did_create_new*
|
||||||
|
|
||||||
|
Since the data is hidden in data structures owned by the DF world,
|
||||||
|
and automatically stored in the save game, these save and retrieval
|
||||||
|
functions can just copy values in memory without doing any actual I/O.
|
||||||
|
However, currently every entry has a 180+-byte dead-weight overhead.
|
@ -0,0 +1,759 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
|
<meta name="generator" content="Docutils 0.8.1: http://docutils.sourceforge.net/" />
|
||||||
|
<title>DFHack Lua API</title>
|
||||||
|
<style type="text/css">
|
||||||
|
|
||||||
|
/*
|
||||||
|
:Author: David Goodger (goodger@python.org)
|
||||||
|
:Id: $Id: html4css1.css 7056 2011-06-17 10:50:48Z milde $
|
||||||
|
:Copyright: This stylesheet has been placed in the public domain.
|
||||||
|
|
||||||
|
Default cascading style sheet for the HTML output of Docutils.
|
||||||
|
|
||||||
|
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
|
||||||
|
customize this style sheet.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* used to remove borders from tables and images */
|
||||||
|
.borderless, table.borderless td, table.borderless th {
|
||||||
|
border: 0 }
|
||||||
|
|
||||||
|
table.borderless td, table.borderless th {
|
||||||
|
/* Override padding for "table.docutils td" with "! important".
|
||||||
|
The right padding separates the table cells. */
|
||||||
|
padding: 0 0.5em 0 0 ! important }
|
||||||
|
|
||||||
|
.first {
|
||||||
|
/* Override more specific margin styles with "! important". */
|
||||||
|
margin-top: 0 ! important }
|
||||||
|
|
||||||
|
.last, .with-subtitle {
|
||||||
|
margin-bottom: 0 ! important }
|
||||||
|
|
||||||
|
.hidden {
|
||||||
|
display: none }
|
||||||
|
|
||||||
|
a.toc-backref {
|
||||||
|
text-decoration: none ;
|
||||||
|
color: black }
|
||||||
|
|
||||||
|
blockquote.epigraph {
|
||||||
|
margin: 2em 5em ; }
|
||||||
|
|
||||||
|
dl.docutils dd {
|
||||||
|
margin-bottom: 0.5em }
|
||||||
|
|
||||||
|
object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Uncomment (and remove this text!) to get bold-faced definition list terms
|
||||||
|
dl.docutils dt {
|
||||||
|
font-weight: bold }
|
||||||
|
*/
|
||||||
|
|
||||||
|
div.abstract {
|
||||||
|
margin: 2em 5em }
|
||||||
|
|
||||||
|
div.abstract p.topic-title {
|
||||||
|
font-weight: bold ;
|
||||||
|
text-align: center }
|
||||||
|
|
||||||
|
div.admonition, div.attention, div.caution, div.danger, div.error,
|
||||||
|
div.hint, div.important, div.note, div.tip, div.warning {
|
||||||
|
margin: 2em ;
|
||||||
|
border: medium outset ;
|
||||||
|
padding: 1em }
|
||||||
|
|
||||||
|
div.admonition p.admonition-title, div.hint p.admonition-title,
|
||||||
|
div.important p.admonition-title, div.note p.admonition-title,
|
||||||
|
div.tip p.admonition-title {
|
||||||
|
font-weight: bold ;
|
||||||
|
font-family: sans-serif }
|
||||||
|
|
||||||
|
div.attention p.admonition-title, div.caution p.admonition-title,
|
||||||
|
div.danger p.admonition-title, div.error p.admonition-title,
|
||||||
|
div.warning p.admonition-title {
|
||||||
|
color: red ;
|
||||||
|
font-weight: bold ;
|
||||||
|
font-family: sans-serif }
|
||||||
|
|
||||||
|
/* Uncomment (and remove this text!) to get reduced vertical space in
|
||||||
|
compound paragraphs.
|
||||||
|
div.compound .compound-first, div.compound .compound-middle {
|
||||||
|
margin-bottom: 0.5em }
|
||||||
|
|
||||||
|
div.compound .compound-last, div.compound .compound-middle {
|
||||||
|
margin-top: 0.5em }
|
||||||
|
*/
|
||||||
|
|
||||||
|
div.dedication {
|
||||||
|
margin: 2em 5em ;
|
||||||
|
text-align: center ;
|
||||||
|
font-style: italic }
|
||||||
|
|
||||||
|
div.dedication p.topic-title {
|
||||||
|
font-weight: bold ;
|
||||||
|
font-style: normal }
|
||||||
|
|
||||||
|
div.figure {
|
||||||
|
margin-left: 2em ;
|
||||||
|
margin-right: 2em }
|
||||||
|
|
||||||
|
div.footer, div.header {
|
||||||
|
clear: both;
|
||||||
|
font-size: smaller }
|
||||||
|
|
||||||
|
div.line-block {
|
||||||
|
display: block ;
|
||||||
|
margin-top: 1em ;
|
||||||
|
margin-bottom: 1em }
|
||||||
|
|
||||||
|
div.line-block div.line-block {
|
||||||
|
margin-top: 0 ;
|
||||||
|
margin-bottom: 0 ;
|
||||||
|
margin-left: 1.5em }
|
||||||
|
|
||||||
|
div.sidebar {
|
||||||
|
margin: 0 0 0.5em 1em ;
|
||||||
|
border: medium outset ;
|
||||||
|
padding: 1em ;
|
||||||
|
background-color: #ffffee ;
|
||||||
|
width: 40% ;
|
||||||
|
float: right ;
|
||||||
|
clear: right }
|
||||||
|
|
||||||
|
div.sidebar p.rubric {
|
||||||
|
font-family: sans-serif ;
|
||||||
|
font-size: medium }
|
||||||
|
|
||||||
|
div.system-messages {
|
||||||
|
margin: 5em }
|
||||||
|
|
||||||
|
div.system-messages h1 {
|
||||||
|
color: red }
|
||||||
|
|
||||||
|
div.system-message {
|
||||||
|
border: medium outset ;
|
||||||
|
padding: 1em }
|
||||||
|
|
||||||
|
div.system-message p.system-message-title {
|
||||||
|
color: red ;
|
||||||
|
font-weight: bold }
|
||||||
|
|
||||||
|
div.topic {
|
||||||
|
margin: 2em }
|
||||||
|
|
||||||
|
h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
|
||||||
|
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
|
||||||
|
margin-top: 0.4em }
|
||||||
|
|
||||||
|
h1.title {
|
||||||
|
text-align: center }
|
||||||
|
|
||||||
|
h2.subtitle {
|
||||||
|
text-align: center }
|
||||||
|
|
||||||
|
hr.docutils {
|
||||||
|
width: 75% }
|
||||||
|
|
||||||
|
img.align-left, .figure.align-left, object.align-left {
|
||||||
|
clear: left ;
|
||||||
|
float: left ;
|
||||||
|
margin-right: 1em }
|
||||||
|
|
||||||
|
img.align-right, .figure.align-right, object.align-right {
|
||||||
|
clear: right ;
|
||||||
|
float: right ;
|
||||||
|
margin-left: 1em }
|
||||||
|
|
||||||
|
img.align-center, .figure.align-center, object.align-center {
|
||||||
|
display: block;
|
||||||
|
margin-left: auto;
|
||||||
|
margin-right: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.align-left {
|
||||||
|
text-align: left }
|
||||||
|
|
||||||
|
.align-center {
|
||||||
|
clear: both ;
|
||||||
|
text-align: center }
|
||||||
|
|
||||||
|
.align-right {
|
||||||
|
text-align: right }
|
||||||
|
|
||||||
|
/* reset inner alignment in figures */
|
||||||
|
div.align-right {
|
||||||
|
text-align: inherit }
|
||||||
|
|
||||||
|
/* div.align-center * { */
|
||||||
|
/* text-align: left } */
|
||||||
|
|
||||||
|
ol.simple, ul.simple {
|
||||||
|
margin-bottom: 1em }
|
||||||
|
|
||||||
|
ol.arabic {
|
||||||
|
list-style: decimal }
|
||||||
|
|
||||||
|
ol.loweralpha {
|
||||||
|
list-style: lower-alpha }
|
||||||
|
|
||||||
|
ol.upperalpha {
|
||||||
|
list-style: upper-alpha }
|
||||||
|
|
||||||
|
ol.lowerroman {
|
||||||
|
list-style: lower-roman }
|
||||||
|
|
||||||
|
ol.upperroman {
|
||||||
|
list-style: upper-roman }
|
||||||
|
|
||||||
|
p.attribution {
|
||||||
|
text-align: right ;
|
||||||
|
margin-left: 50% }
|
||||||
|
|
||||||
|
p.caption {
|
||||||
|
font-style: italic }
|
||||||
|
|
||||||
|
p.credits {
|
||||||
|
font-style: italic ;
|
||||||
|
font-size: smaller }
|
||||||
|
|
||||||
|
p.label {
|
||||||
|
white-space: nowrap }
|
||||||
|
|
||||||
|
p.rubric {
|
||||||
|
font-weight: bold ;
|
||||||
|
font-size: larger ;
|
||||||
|
color: maroon ;
|
||||||
|
text-align: center }
|
||||||
|
|
||||||
|
p.sidebar-title {
|
||||||
|
font-family: sans-serif ;
|
||||||
|
font-weight: bold ;
|
||||||
|
font-size: larger }
|
||||||
|
|
||||||
|
p.sidebar-subtitle {
|
||||||
|
font-family: sans-serif ;
|
||||||
|
font-weight: bold }
|
||||||
|
|
||||||
|
p.topic-title {
|
||||||
|
font-weight: bold }
|
||||||
|
|
||||||
|
pre.address {
|
||||||
|
margin-bottom: 0 ;
|
||||||
|
margin-top: 0 ;
|
||||||
|
font: inherit }
|
||||||
|
|
||||||
|
pre.literal-block, pre.doctest-block, pre.math {
|
||||||
|
margin-left: 2em ;
|
||||||
|
margin-right: 2em }
|
||||||
|
|
||||||
|
span.classifier {
|
||||||
|
font-family: sans-serif ;
|
||||||
|
font-style: oblique }
|
||||||
|
|
||||||
|
span.classifier-delimiter {
|
||||||
|
font-family: sans-serif ;
|
||||||
|
font-weight: bold }
|
||||||
|
|
||||||
|
span.interpreted {
|
||||||
|
font-family: sans-serif }
|
||||||
|
|
||||||
|
span.option {
|
||||||
|
white-space: nowrap }
|
||||||
|
|
||||||
|
span.pre {
|
||||||
|
white-space: pre }
|
||||||
|
|
||||||
|
span.problematic {
|
||||||
|
color: red }
|
||||||
|
|
||||||
|
span.section-subtitle {
|
||||||
|
/* font-size relative to parent (h1..h6 element) */
|
||||||
|
font-size: 80% }
|
||||||
|
|
||||||
|
table.citation {
|
||||||
|
border-left: solid 1px gray;
|
||||||
|
margin-left: 1px }
|
||||||
|
|
||||||
|
table.docinfo {
|
||||||
|
margin: 2em 4em }
|
||||||
|
|
||||||
|
table.docutils {
|
||||||
|
margin-top: 0.5em ;
|
||||||
|
margin-bottom: 0.5em }
|
||||||
|
|
||||||
|
table.footnote {
|
||||||
|
border-left: solid 1px black;
|
||||||
|
margin-left: 1px }
|
||||||
|
|
||||||
|
table.docutils td, table.docutils th,
|
||||||
|
table.docinfo td, table.docinfo th {
|
||||||
|
padding-left: 0.5em ;
|
||||||
|
padding-right: 0.5em ;
|
||||||
|
vertical-align: top }
|
||||||
|
|
||||||
|
table.docutils th.field-name, table.docinfo th.docinfo-name {
|
||||||
|
font-weight: bold ;
|
||||||
|
text-align: left ;
|
||||||
|
white-space: nowrap ;
|
||||||
|
padding-left: 0 }
|
||||||
|
|
||||||
|
h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
|
||||||
|
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
|
||||||
|
font-size: 100% }
|
||||||
|
|
||||||
|
ul.auto-toc {
|
||||||
|
list-style-type: none }
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="document" id="dfhack-lua-api">
|
||||||
|
<h1 class="title">DFHack Lua API</h1>
|
||||||
|
|
||||||
|
<div class="contents topic" id="contents">
|
||||||
|
<p class="topic-title first">Contents</p>
|
||||||
|
<ul class="simple">
|
||||||
|
<li><a class="reference internal" href="#df-structure-wrapper" id="id1">DF structure wrapper</a><ul>
|
||||||
|
<li><a class="reference internal" href="#typed-object-references" id="id2">Typed object references</a><ul>
|
||||||
|
<li><a class="reference internal" href="#primitive-references" id="id3">Primitive references</a></li>
|
||||||
|
<li><a class="reference internal" href="#struct-references" id="id4">Struct references</a></li>
|
||||||
|
<li><a class="reference internal" href="#container-references" id="id5">Container references</a></li>
|
||||||
|
<li><a class="reference internal" href="#bitfield-references" id="id6">Bitfield references</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li><a class="reference internal" href="#named-types" id="id7">Named types</a></li>
|
||||||
|
<li><a class="reference internal" href="#global-functions" id="id8">Global functions</a></li>
|
||||||
|
<li><a class="reference internal" href="#recursive-table-assignment" id="id9">Recursive table assignment</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
<li><a class="reference internal" href="#dfhack-utilities" id="id10">DFHack utilities</a><ul>
|
||||||
|
<li><a class="reference internal" href="#persistent-configuration-storage" id="id11">Persistent configuration storage</a></li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="df-structure-wrapper">
|
||||||
|
<h1><a class="toc-backref" href="#id1">DF structure wrapper</a></h1>
|
||||||
|
<p>DF structures described by the xml files in library/xml are exported
|
||||||
|
to lua code as a tree of objects and functions under the <tt class="docutils literal">df</tt> global,
|
||||||
|
which broadly maps to the <tt class="docutils literal">df</tt> namespace in C++.</p>
|
||||||
|
<p><strong>WARNING</strong>: The wrapper provides almost raw access to the memory
|
||||||
|
of the game, so mistakes in manipulating objects are as likely to
|
||||||
|
crash the game as equivalent plain C++ code would be. E.g. NULL
|
||||||
|
pointer access is safely detected, but dangling pointers aren't.</p>
|
||||||
|
<p>Objects managed by the wrapper can be broadly classified into the following groups:</p>
|
||||||
|
<ol class="arabic">
|
||||||
|
<li><p class="first">Typed object pointers (references).</p>
|
||||||
|
<p>References represent objects in DF memory with a known type.</p>
|
||||||
|
<p>In addition to fields and methods defined by the wrapped type,
|
||||||
|
every reference has some built-in properties and methods.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">Untyped pointers</p>
|
||||||
|
<p>Represented as lightuserdata.</p>
|
||||||
|
<p>In assignment to a pointer NULL can be represented either as
|
||||||
|
<tt class="docutils literal">nil</tt>, or a NULL lightuserdata; reading a NULL pointer field
|
||||||
|
returns <tt class="docutils literal">nil</tt>.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">Named types</p>
|
||||||
|
<p>Objects in the <tt class="docutils literal">df</tt> tree that represent identity of struct, class,
|
||||||
|
enum and bitfield types. They host nested named types, static
|
||||||
|
methods, builtin properties & methods, and, for enums and bitfields,
|
||||||
|
the bi-directional mapping between key names and values.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">The <tt class="docutils literal">global</tt> object</p>
|
||||||
|
<p><tt class="docutils literal">df.global</tt> corresponds to the <tt class="docutils literal"><span class="pre">df::global</span></tt> namespace, and
|
||||||
|
behaves as a mix between a named type and a reference, containing
|
||||||
|
both nested types and fields corresponding to global symbols.</p>
|
||||||
|
</li>
|
||||||
|
</ol>
|
||||||
|
<p>In addition to the <tt class="docutils literal">global</tt> object and top-level types the <tt class="docutils literal">df</tt>
|
||||||
|
global also contains a few global builtin utility functions.</p>
|
||||||
|
<div class="section" id="typed-object-references">
|
||||||
|
<h2><a class="toc-backref" href="#id2">Typed object references</a></h2>
|
||||||
|
<p>The underlying primitive lua object is userdata with a metatable.
|
||||||
|
Every structured field access produces a new userdata instance.</p>
|
||||||
|
<p>All typed objects have the following built-in features:</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref1 == ref2</tt>, <tt class="docutils literal">tostring(ref)</tt></p>
|
||||||
|
<p>References implement equality by type & pointer value, and string conversion.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">pairs(ref)</tt></p>
|
||||||
|
<p>Returns an iterator for the sequence of actual C++ field names
|
||||||
|
and values. Fields are enumerated in memory order. Methods and
|
||||||
|
lua wrapper properties are not included in the iteration.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref._kind</tt></p>
|
||||||
|
<p>Returns one of: <tt class="docutils literal">primitive</tt>, <tt class="docutils literal">struct</tt>, <tt class="docutils literal">container</tt>,
|
||||||
|
or <tt class="docutils literal">bitfield</tt>, as appropriate for the referenced object.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref._type</tt></p>
|
||||||
|
<p>Returns the named type object or a string that represents
|
||||||
|
the referenced object type.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref:sizeof()</tt></p>
|
||||||
|
<p>Returns <em>size, address</em></p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref:new()</tt></p>
|
||||||
|
<p>Allocates a new instance of the same type, and copies data
|
||||||
|
from the current object.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref:delete()</tt></p>
|
||||||
|
<p>Destroys the object with the C++ <tt class="docutils literal">delete</tt> operator.
|
||||||
|
If destructor is not available, returns <em>false</em>.</p>
|
||||||
|
<p><strong>WARNING</strong>: the lua reference object remains as a dangling
|
||||||
|
pointer, like a raw C++ pointer would.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref:assign(object)</tt></p>
|
||||||
|
<p>Assigns data from object to ref. Object must either be another
|
||||||
|
ref of a compatible type, or a lua table; in the latter case
|
||||||
|
special recursive assignment rules are applied.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">ref:_displace(index[,step])</span></tt></p>
|
||||||
|
<p>Returns a new reference with the pointer adjusted by index*step.
|
||||||
|
Step defaults to the natural object size.</p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<div class="section" id="primitive-references">
|
||||||
|
<h3><a class="toc-backref" href="#id3">Primitive references</a></h3>
|
||||||
|
<p>References of the <em>_kind</em> <tt class="docutils literal">'primitive'</tt> are used for objects
|
||||||
|
that don't fit any of the other reference types. Such
|
||||||
|
references can only appear as a value of a pointer field,
|
||||||
|
or as a result of calling the <tt class="docutils literal">_field()</tt> method.</p>
|
||||||
|
<p>They behave as structs with one field <tt class="docutils literal">value</tt> of the right type.</p>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="struct-references">
|
||||||
|
<h3><a class="toc-backref" href="#id4">Struct references</a></h3>
|
||||||
|
<p>Struct references are used for class and struct objects.</p>
|
||||||
|
<p>They implement the following features:</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref.field</tt>, <tt class="docutils literal">ref.field = value</tt></p>
|
||||||
|
<p>Valid fields of the structure may be accessed by subscript.</p>
|
||||||
|
<p>Primitive typed fields, i.e. numbers & strings, are converted
|
||||||
|
to/from matching lua values. The value of a pointer is a reference
|
||||||
|
to the target, or nil/NULL. Complex types are represented by
|
||||||
|
a reference to the field within the structure; unless recursive
|
||||||
|
lua table assignment is used, such fields can only be read.</p>
|
||||||
|
<p><strong>NOTE:</strong> In case of inheritance, <em>superclass</em> fields have precedence
|
||||||
|
over the subclass, but fields shadowed in this way can still
|
||||||
|
be accessed as <tt class="docutils literal"><span class="pre">ref['subclasstype.field']</span></tt>.
|
||||||
|
This shadowing order is necessary because vtable-based classes
|
||||||
|
are automatically exposed in their exact type, and the reverse
|
||||||
|
rule would make access to superclass fields unreliable.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref._field(field)</tt></p>
|
||||||
|
<p>Returns a reference to a valid field. That is, unlike regular
|
||||||
|
subscript, it returns a reference to the field within the structure
|
||||||
|
even for primitive typed fields and pointers.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">ref:vmethod(args...)</span></tt></p>
|
||||||
|
<p>Named virtual methods are also exposed, subject to the same
|
||||||
|
shadowing rules.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">pairs(ref)</tt></p>
|
||||||
|
<p>Enumerates all real fields (but not methods) in memory
|
||||||
|
(= declaration) order.</p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="container-references">
|
||||||
|
<h3><a class="toc-backref" href="#id5">Container references</a></h3>
|
||||||
|
<p>Containers represent vectors and arrays, possibly resizable.</p>
|
||||||
|
<p>A container field can associate an enum to the container
|
||||||
|
reference, which allows accessing elements using string keys
|
||||||
|
instead of numerical indices.</p>
|
||||||
|
<p>Implemented features:</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref._enum</tt></p>
|
||||||
|
<p>If the container has an associated enum, returns the matching
|
||||||
|
named type object.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">#ref</tt></p>
|
||||||
|
<p>Returns the <em>length</em> of the container.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref[index]</tt></p>
|
||||||
|
<p>Accesses the container element, using either a <em>0-based</em> numerical
|
||||||
|
index, or, if an enum is associated, a valid enum key string.</p>
|
||||||
|
<p>Accessing an invalid index is an error, but some container types
|
||||||
|
may return a default value, or auto-resize instead for convenience.
|
||||||
|
Currently this relaxed mode is implemented by df-flagarray aka BitArray.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref._field(index)</tt></p>
|
||||||
|
<p>Like with structs, returns a pointer to the array element, if possible.
|
||||||
|
Flag and bit arrays cannot return such pointer, so it fails with an error.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">pairs(ref)</tt>, <tt class="docutils literal">ipairs(ref)</tt></p>
|
||||||
|
<p>If the container has no associated enum, both behave identically,
|
||||||
|
iterating over numerical indices in order. Otherwise, ipairs still
|
||||||
|
uses numbers, while pairs tries to substitute enum keys whenever
|
||||||
|
possible.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref:resize(new_size)</tt></p>
|
||||||
|
<p>Resizes the container if supported, or fails with an error.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref:insert(index,item)</tt></p>
|
||||||
|
<p>Inserts a new item at the specified index. To add at the end,
|
||||||
|
use <tt class="docutils literal">#ref</tt> as index.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">ref:erase(index)</tt></p>
|
||||||
|
<p>Removes the element at the given valid index.</p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="bitfield-references">
|
||||||
|
<h3><a class="toc-backref" href="#id6">Bitfield references</a></h3>
|
||||||
|
<p>Bitfields behave like special fixed-size containers.
|
||||||
|
The <tt class="docutils literal">_enum</tt> property points to the bitfield type.</p>
|
||||||
|
<p>Numerical indices correspond to the shift value,
|
||||||
|
and if a subfield occupies multiple bits, the
|
||||||
|
<tt class="docutils literal">ipairs</tt> order would have a gap.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="named-types">
|
||||||
|
<h2><a class="toc-backref" href="#id7">Named types</a></h2>
|
||||||
|
<p>Named types are exposed in the <tt class="docutils literal">df</tt> tree with names identical
|
||||||
|
to the C++ version, except for the <tt class="docutils literal">::</tt> vs <tt class="docutils literal">.</tt> difference.</p>
|
||||||
|
<p>All types and the global object have the following features:</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first"><tt class="docutils literal">type._kind</tt></p>
|
||||||
|
<p>Evaluates to one of <tt class="docutils literal"><span class="pre">struct-type</span></tt>, <tt class="docutils literal"><span class="pre">class-type</span></tt>, <tt class="docutils literal"><span class="pre">enum-type</span></tt>,
|
||||||
|
<tt class="docutils literal"><span class="pre">bitfield-type</span></tt> or <tt class="docutils literal">global</tt>.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">type._identity</tt></p>
|
||||||
|
<p>Contains a lightuserdata pointing to the underlying
|
||||||
|
DFHack::type_instance object.</p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<p>Types excluding the global object also support:</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first"><tt class="docutils literal">type:sizeof()</tt></p>
|
||||||
|
<p>Returns the size of an object of the type.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">type:new()</tt></p>
|
||||||
|
<p>Creates a new instance of an object of the type.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">type:is_instance(object)</tt></p>
|
||||||
|
<p>Returns true if object is same or subclass type, or a reference
|
||||||
|
to an object of same or subclass type. It is permissible to pass
|
||||||
|
nil, NULL or non-wrapper value as object; in this case the
|
||||||
|
method returns nil.</p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<p>In addition to this, enum and bitfield types contain a
|
||||||
|
bi-directional mapping between key strings and values, and
|
||||||
|
also map <tt class="docutils literal">_first_item</tt> and <tt class="docutils literal">_last_item</tt> to the min and
|
||||||
|
max values.</p>
|
||||||
|
<p>Struct and class types with instance-vector attribute in the
|
||||||
|
xml have a <tt class="docutils literal">type.find(key)</tt> function that wraps the find
|
||||||
|
method provided in C++.</p>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="global-functions">
|
||||||
|
<h2><a class="toc-backref" href="#id8">Global functions</a></h2>
|
||||||
|
<p>The <tt class="docutils literal">df</tt> table itself contains the following functions and values:</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first"><tt class="docutils literal">NULL</tt>, <tt class="docutils literal">df.NULL</tt></p>
|
||||||
|
<p>Contains the NULL lightuserdata.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">df.isnull(obj)</tt></p>
|
||||||
|
<p>Evaluates to true if obj is nil or NULL; false otherwise.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">df.isvalid(obj[,allow_null])</span></tt></p>
|
||||||
|
<p>For supported objects returns one of <tt class="docutils literal">type</tt>, <tt class="docutils literal">voidptr</tt>, <tt class="docutils literal">ref</tt>.</p>
|
||||||
|
<p>If <em>allow_null</em> is true, and obj is nil or NULL, returns <tt class="docutils literal">null</tt>.</p>
|
||||||
|
<p>Otherwise returns <em>nil</em>.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">df.sizeof(obj)</tt></p>
|
||||||
|
<p>For types and refs identical to <tt class="docutils literal">obj:sizeof()</tt>.
|
||||||
|
For lightuserdata returns <em>nil, address</em></p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">df.new(obj)</tt>, <tt class="docutils literal">df.delete(obj)</tt>, <tt class="docutils literal">df.assign(obj, obj2)</tt></p>
|
||||||
|
<p>Equivalent to using the matching methods of obj.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">df._displace(obj,index[,step])</span></tt></p>
|
||||||
|
<p>For refs equivalent to the method, but also works with
|
||||||
|
lightuserdata (step is mandatory then).</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">df.is_instance(type,obj)</tt></p>
|
||||||
|
<p>Equivalent to the method, but also allows a reference as proxy for its type.</p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="recursive-table-assignment">
|
||||||
|
<h2><a class="toc-backref" href="#id9">Recursive table assignment</a></h2>
|
||||||
|
<p>Recursive assignment is invoked when a lua table is assigned
|
||||||
|
to a C++ object or field, i.e. one of:</p>
|
||||||
|
<ul class="simple">
|
||||||
|
<li><tt class="docutils literal"><span class="pre">ref:assign{...}</span></tt></li>
|
||||||
|
<li><tt class="docutils literal">ref.field = <span class="pre">{...}</span></tt></li>
|
||||||
|
</ul>
|
||||||
|
<p>The general mode of operation is that all fields of the table
|
||||||
|
are assigned to the fields of the target structure, roughly
|
||||||
|
emulating the following code:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
function rec_assign(ref,table)
|
||||||
|
for key,value in pairs(table) do
|
||||||
|
ref[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
</pre>
|
||||||
|
<p>Since assigning a table to a field using = invokes the same
|
||||||
|
process, it is recursive.</p>
|
||||||
|
<p>There are however some variations to this process depending
|
||||||
|
on the type of the field being assigned to:</p>
|
||||||
|
<ol class="arabic">
|
||||||
|
<li><p class="first">If the table contains an <tt class="docutils literal">assign</tt> field, it is
|
||||||
|
applied first, using the <tt class="docutils literal">ref:assign(value)</tt> method.
|
||||||
|
It is never assigned as a usual field.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">When a table is assigned to a non-NULL pointer field
|
||||||
|
using the <tt class="docutils literal">ref.field = <span class="pre">{...}</span></tt> syntax, it is applied
|
||||||
|
to the target of the pointer instead.</p>
|
||||||
|
<p>If the pointer is NULL, the table is checked for a <tt class="docutils literal">new</tt> field:</p>
|
||||||
|
<ol class="loweralpha simple">
|
||||||
|
<li>If it is <em>nil</em> or <em>false</em>, assignment fails with an error.</li>
|
||||||
|
<li>If it is <em>true</em>, the pointer is initialized with a newly
|
||||||
|
allocated object of the declared target type of the pointer.</li>
|
||||||
|
<li>Otherwise, <tt class="docutils literal">table.new</tt> must be a named type, or an
|
||||||
|
object of a type compatible with the pointer. The pointer
|
||||||
|
is initialized with the result of calling <tt class="docutils literal">table.new:new()</tt>.</li>
|
||||||
|
</ol>
|
||||||
|
<p>After this auto-vivification process, assignment proceeds
|
||||||
|
as if the pointer wasn't NULL.</p>
|
||||||
|
<p>Obviously, the <tt class="docutils literal">new</tt> field inside the table is always skipped
|
||||||
|
during the actual per-field assignment processing.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">If the target of the assignment is a container, a separate
|
||||||
|
rule set is used:</p>
|
||||||
|
<ol class="loweralpha">
|
||||||
|
<li><p class="first">If the table contains neither <tt class="docutils literal">assign</tt> nor <tt class="docutils literal">resize</tt>
|
||||||
|
fields, it is interpreted as an ordinary <em>1-based</em> lua
|
||||||
|
array. The container is resized to the #-size of the
|
||||||
|
table, and elements are assigned in numeric order:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
ref:resize(#table);
|
||||||
|
for i=1,#table do ref[i-1] = table[i] end
|
||||||
|
</pre>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">Otherwise, <tt class="docutils literal">resize</tt> must be <em>true</em>, <em>false</em>, or
|
||||||
|
an explicit number. If it is not false, the container
|
||||||
|
is resized. After that the usual struct-like 'pairs'
|
||||||
|
assignment is performed.</p>
|
||||||
|
<p>In case <tt class="docutils literal">resize</tt> is <em>true</em>, the size is computed
|
||||||
|
by scanning the table for the largest numeric key.</p>
|
||||||
|
</li>
|
||||||
|
</ol>
|
||||||
|
<p>This means that in order to reassign only one element of
|
||||||
|
a container using this system, it is necessary to use:</p>
|
||||||
|
<pre class="literal-block">
|
||||||
|
{ resize=false, [idx]=value }
|
||||||
|
</pre>
|
||||||
|
</li>
|
||||||
|
</ol>
|
||||||
|
<p>Since nil inside a table is indistinguishable from missing key,
|
||||||
|
it is necessary to use <tt class="docutils literal">df.NULL</tt> as a null pointer value.</p>
|
||||||
|
<p>This system is intended as a way to define a nested object
|
||||||
|
tree using pure lua data structures, and then materialize it in
|
||||||
|
C++ memory in one go. Note that if pointer auto-vivification
|
||||||
|
is used, an error in the middle of the recursive walk would
|
||||||
|
not destroy any objects allocated in this way, so the user
|
||||||
|
should be prepared to catch the error and do the necessary
|
||||||
|
cleanup.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="dfhack-utilities">
|
||||||
|
<h1><a class="toc-backref" href="#id10">DFHack utilities</a></h1>
|
||||||
|
<p>DFHack utility functions are placed in the <tt class="docutils literal">dfhack</tt> global tree.</p>
|
||||||
|
<p>Currently it defines the following features:</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.print(args...)</span></tt></p>
|
||||||
|
<p>Output tab-separated args as standard lua print would do,
|
||||||
|
but without a newline.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">print(args...)</span></tt>, <tt class="docutils literal"><span class="pre">dfhack.println(args...)</span></tt></p>
|
||||||
|
<p>A replacement of the standard library print function that
|
||||||
|
works with DFHack output infrastructure.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.printerr(args...)</span></tt></p>
|
||||||
|
<p>Same as println; intended for errors. Uses red color and logs to stderr.log.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.color([color])</span></tt></p>
|
||||||
|
<p>Sets the current output color. If color is <em>nil</em> or <em>-1</em>, resets to default.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">dfhack.is_interactive()</tt></p>
|
||||||
|
<p>Checks if the thread can access the interactive console and returns <em>true</em> or <em>false</em>.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.lineedit([prompt[,history_filename]])</span></tt></p>
|
||||||
|
<p>If the thread owns the interactive console, shows a prompt
|
||||||
|
and returns the entered string. Otherwise returns <em>nil, error</em>.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.interpreter([prompt[,env[,history_filename]]])</span></tt></p>
|
||||||
|
<p>Starts an interactive lua interpreter, using the specified prompt
|
||||||
|
string, global environment and command-line history file.</p>
|
||||||
|
<p>If the interactive console is not accessible, returns <em>nil, error</em>.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.pcall(f[,args...])</span></tt></p>
|
||||||
|
<p>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.</p>
|
||||||
|
<p>The returned error is a table with separate <tt class="docutils literal">message</tt> and
|
||||||
|
<tt class="docutils literal">stacktrace</tt> string fields; it implements <tt class="docutils literal">__tostring</tt>.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">safecall(f[,args...])</span></tt>, <tt class="docutils literal"><span class="pre">dfhack.safecall(f[,args...])</span></tt></p>
|
||||||
|
<p>Just like pcall, but also prints the error using printerr before
|
||||||
|
returning. Intended as a convenience function.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.with_suspend(f[,args...])</span></tt></p>
|
||||||
|
<p>Calls <tt class="docutils literal">f</tt> with arguments after grabbing the DF core suspend lock.
|
||||||
|
Suspending is necessary for accessing a consistent state of DF memory.</p>
|
||||||
|
<p>Returned values and errors are propagated through after releasing
|
||||||
|
the lock. It is safe to nest suspends.</p>
|
||||||
|
<p>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.</p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<div class="section" id="persistent-configuration-storage">
|
||||||
|
<h2><a class="toc-backref" href="#id11">Persistent configuration storage</a></h2>
|
||||||
|
<p>This api is intended for storing configuration options in the world itself.
|
||||||
|
It probably should be restricted to data that is world-dependent.</p>
|
||||||
|
<p>Entries are identified by a string <tt class="docutils literal">key</tt>, but it is also possible to manage
|
||||||
|
multiple entries with the same key; their identity is determined by <tt class="docutils literal">entry_id</tt>.
|
||||||
|
Every entry has a mutable string <tt class="docutils literal">value</tt>, and an array of 7 mutable <tt class="docutils literal">ints</tt>.</p>
|
||||||
|
<ul>
|
||||||
|
<li><p class="first"><tt class="docutils literal">dfhack.persistent.get(key)</tt>, <tt class="docutils literal">entry:get()</tt></p>
|
||||||
|
<p>Retrieves a persistent config record with the given string key,
|
||||||
|
or refreshes an already retrieved entry. If there are multiple
|
||||||
|
entries with the same key, it is undefined which one is retrieved
|
||||||
|
by the first version of the call.</p>
|
||||||
|
<p>Returns entry, or <em>nil</em> if not found.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal">dfhack.persistent.delete(key)</tt>, <tt class="docutils literal">entry:delete()</tt></p>
|
||||||
|
<p>Removes an existing entry. Returns <em>true</em> if succeeded.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.persistent.get_all(key[,match_prefix])</span></tt></p>
|
||||||
|
<p>Retrieves all entries with the same key, or starting with key..'/'.
|
||||||
|
Calling <tt class="docutils literal"><span class="pre">get_all('',true)</span></tt> will match all entries.</p>
|
||||||
|
<p>If none found, returns nil; otherwise returns an array of entries.</p>
|
||||||
|
</li>
|
||||||
|
<li><p class="first"><tt class="docutils literal"><span class="pre">dfhack.persistent.save({key=str1,</span> <span class="pre">...}[,new])</span></tt>, <tt class="docutils literal"><span class="pre">entry:save([new])</span></tt></p>
|
||||||
|
<p>Saves changes in an entry, or creates a new one. Passing true as
|
||||||
|
new forces creation of a new entry even if one already exists;
|
||||||
|
otherwise the existing one is simply updated.
|
||||||
|
Returns <em>entry, did_create_new</em></p>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<p>Since the data is hidden in data structures owned by the DF world,
|
||||||
|
and automatically stored in the save game, these save and retrieval
|
||||||
|
functions can just copy values in memory without doing any actual I/O.
|
||||||
|
However, currently every entry has a 180+-byte dead-weight overhead.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,3 @@
|
|||||||
|
if not(FILE) then
|
||||||
|
adv_tools.menu:display()
|
||||||
|
end
|
Loading…
Reference in New Issue