From d1b27418a6a39aa15d51465e61975c5230dcc9d6 Mon Sep 17 00:00:00 2001
From: Alexander Gavrilov
Date: Thu, 5 Apr 2012 11:32:23 +0400
Subject: [PATCH 01/10] Add a World::GetPersistentData version that auto-adds
if not found.
---
library/include/DataFuncs.h | 4 ++++
library/include/DataIdentity.h | 4 +++-
library/include/modules/World.h | 8 ++++++++
library/modules/World.cpp | 15 +++++++++++++++
4 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/library/include/DataFuncs.h b/library/include/DataFuncs.h
index 822dcd45d..109b2c176 100644
--- a/library/include/DataFuncs.h
+++ b/library/include/DataFuncs.h
@@ -32,6 +32,10 @@ distribution.
#include "DataIdentity.h"
#include "LuaWrapper.h"
+#ifndef BUILD_DFHACK_LIB
+#error Due to export issues this header is internal to the main library.
+#endif
+
namespace df {
// A very simple and stupid implementation of some stuff from boost
template struct is_same_type { static const bool value = false; };
diff --git a/library/include/DataIdentity.h b/library/include/DataIdentity.h
index c83b2092a..bab87e1bb 100644
--- a/library/include/DataIdentity.h
+++ b/library/include/DataIdentity.h
@@ -160,6 +160,8 @@ namespace DFHack
};
}
+// Due to export issues, this stuff can only work in the main dll
+#ifdef BUILD_DFHACK_LIB
namespace df
{
using DFHack::function_identity_base;
@@ -575,4 +577,4 @@ namespace df
return &identity;
}
}
-
+#endif
diff --git a/library/include/modules/World.h b/library/include/modules/World.h
index 9ed6a3ed9..e7b17ce41 100644
--- a/library/include/modules/World.h
+++ b/library/include/modules/World.h
@@ -139,8 +139,16 @@ namespace DFHack
PersistentDataItem AddPersistentData(const std::string &key);
PersistentDataItem GetPersistentData(const std::string &key);
PersistentDataItem GetPersistentData(int entry_id);
+ // Calls GetPersistentData(key); if not found, adds and sets added to true.
+ // The result can still be not isValid() e.g. if the world is not loaded.
+ PersistentDataItem GetPersistentData(const std::string &key, bool *added);
+ // Lists all items with the given key.
+ // If prefix is true, search for keys starting with key+"/".
+ // GetPersistentData(&vec,"",true) returns all items.
+ // Items have alphabetic order by key; same key ordering is undefined.
void GetPersistentData(std::vector *vec,
const std::string &key, bool prefix = false);
+ // Deletes the item; returns true if success.
bool DeletePersistentData(const PersistentDataItem &item);
void ClearPersistentCache();
diff --git a/library/modules/World.cpp b/library/modules/World.cpp
index d570abaec..ca5d0aee0 100644
--- a/library/modules/World.cpp
+++ b/library/modules/World.cpp
@@ -300,6 +300,21 @@ PersistentDataItem World::GetPersistentData(int entry_id)
return PersistentDataItem();
}
+PersistentDataItem World::GetPersistentData(const std::string &key, bool *added)
+{
+ *added = false;
+
+ PersistentDataItem rv = GetPersistentData(key);
+
+ if (!rv.isValid())
+ {
+ *added = true;
+ rv = AddPersistentData(key);
+ }
+
+ return rv;
+}
+
void World::GetPersistentData(std::vector *vec, const std::string &key, bool prefix)
{
if (!BuildPersistentCache())
From 3afed43cdb435c788964d983a358d97ee291c2fc Mon Sep 17 00:00:00 2001
From: Alexander Gavrilov
Date: Thu, 5 Apr 2012 11:59:39 +0400
Subject: [PATCH 02/10] Experimental: try wrapping a dfhack api function.
---
library/LuaTools.cpp | 6 ++++++
library/LuaTypes.cpp | 11 ++++++++---
library/LuaWrapper.cpp | 5 ++++-
library/include/DataFuncs.h | 22 ++++++++++++++++++++--
library/include/LuaWrapper.h | 5 +++++
5 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp
index 5a50d0dd2..16c60457a 100644
--- a/library/LuaTools.cpp
+++ b/library/LuaTools.cpp
@@ -35,14 +35,18 @@ distribution.
// must be last due to MS stupidity
#include "DataDefs.h"
#include "DataIdentity.h"
+#include "DataFuncs.h"
#include "modules/World.h"
+#include "modules/Gui.h"
#include "LuaWrapper.h"
#include "LuaTools.h"
#include "MiscUtils.h"
+#include "df/job.h"
+
#include
#include
#include
@@ -868,6 +872,8 @@ lua_State *DFHack::Lua::Open(color_ostream &out, lua_State *state)
OpenPersistent(state);
+ LuaWrapper::AddMethodWrapper(state, 0, -1, "getSelectedJob", df::wrap_function(&Gui::getSelectedJob));
+
lua_setglobal(state, "dfhack");
// load dfhack.lua
diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp
index 6d5961f8d..2cc929ee9 100644
--- a/library/LuaTypes.cpp
+++ b/library/LuaTypes.cpp
@@ -1029,11 +1029,16 @@ static int meta_call_function(lua_State *state)
/**
* Create a closure invoking the given function, and add it to the field table.
*/
-static void AddMethodWrapper(lua_State *state, int meta_idx, int field_idx,
- const char *name, function_identity_base *fun)
+void LuaWrapper::AddMethodWrapper(lua_State *state, int meta_idx, int field_idx,
+ const char *name, function_identity_base *fun)
{
+ field_idx = lua_absindex(state, field_idx);
+
lua_rawgetp(state, LUA_REGISTRYINDEX, &DFHACK_TYPETABLE_TOKEN);
- lua_pushvalue(state, meta_idx);
+ if (meta_idx)
+ lua_pushvalue(state, meta_idx);
+ else
+ lua_pushlightuserdata(state, NULL); // can't be a metatable
lua_pushfstring(state, "%s()", name);
lua_pushlightuserdata(state, fun);
lua_pushcclosure(state, meta_call_function, 4);
diff --git a/library/LuaWrapper.cpp b/library/LuaWrapper.cpp
index 818e6b9e1..2f3958efe 100644
--- a/library/LuaWrapper.cpp
+++ b/library/LuaWrapper.cpp
@@ -53,7 +53,10 @@ static luaL_Reg no_functions[] = { { NULL, NULL } };
*/
void LuaWrapper::field_error(lua_State *state, int index, const char *err, const char *mode)
{
- lua_getfield(state, UPVAL_METATABLE, "__metatable");
+ if (lua_islightuserdata(state, UPVAL_METATABLE))
+ lua_pushstring(state, "(global)");
+ else
+ lua_getfield(state, UPVAL_METATABLE, "__metatable");
const char *cname = lua_tostring(state, -1);
const char *fname = index ? lua_tostring(state, index) : "*";
luaL_error(state, "Cannot %s field %s.%s: %s.",
diff --git a/library/include/DataFuncs.h b/library/include/DataFuncs.h
index 109b2c176..3529d8059 100644
--- a/library/include/DataFuncs.h
+++ b/library/include/DataFuncs.h
@@ -67,10 +67,15 @@ namespace df {
CT *self = (CT*)DFHack::LuaWrapper::get_object_addr(state, base++, UPVAL_METHOD_NAME, "invoke");
#define LOAD_ARG(type) \
type v##type; df::identity_traits::get()->lua_write(state, UPVAL_METHOD_NAME, &v##type, base++);
+#define OSTREAM_ARG DFHack::color_ostream&
+#define LOAD_OSTREAM(name) \
+ DFHack::color_ostream_proxy name(DFHack::Core::getInstance().getConsole());
-#define INSTANTIATE_WRAPPERS(Count, FArgs, Args, Loads) \
+#define INSTANTIATE_RETURN_TYPE(FArgs) \
template struct return_type
+
+
+
Thin wrappers around C++ functions, similar to the ones for virtual methods.
+
+
+
+dfhack.gui.getSelectedWorkshopJob(silent)
+When a job is selected in 'q' mode, returns the job, else
+prints error unless silent and returns nil.
+
+dfhack.gui.getSelectedJob(silent)
+Returns the job selected in a workshop or unit/jobs screen.
+
+dfhack.gui.getSelectedUnit(silent)
+Returns the unit selected via 'v', 'k', unit/jobs, or
+a full-screen item view of a cage or suchlike.
+
+dfhack.gui.getSelectedItem(silent)
+Returns the item selected via 'v' ->inventory, 'k', 't', or
+a full-screen item view of a container. Note that in the
+last case, the highlighted contained item is returned, not
+the container itself.
+
+dfhack.gui.showAnnouncement(text,color,is_bright)
+Adds a regular announcement with given text, color, and brightness.
+The is_bright boolean actually seems to invert the brightness.
+
+dfhack.gui.showPopupAnnouncement(text,color,is_bright)
+Pops up a titan-style modal announcement window.
+
+
+
+
+
+
+dfhack.job.cloneJobStruct(job)
+Creates a deep copy of the given job.
+
+dfhack.job.printJobDetails(job)
+Prints info about the job.
+
+dfhack.job.getJobHolder(job)
+Returns the building holding the job.
+
+dfhack.job.is_equal(job1,job2)
+Compares important fields in the job and nested item structures.
+
+dfhack.job.is_item_equal(job_item1,job_item2)
+Compares important fields in the job item structures.
+
+
+
+