summaryrefslogtreecommitdiff
path: root/plugins/devel
diff options
context:
space:
mode:
authorKelly Martin2012-08-30 09:25:26 -0500
committerKelly Martin2012-08-30 09:25:26 -0500
commita8158cb19ac25ec69db4cbb09c1eaf853a559e9a (patch)
tree301c8e510efa3ccf714e0433a9e6ef4fda9b0015 /plugins/devel
parent604cf808321382286619042b1edbf81558086cb5 (diff)
parentcb125f3d89382c4c075ea69d5178868c6e325938 (diff)
downloaddfhack-a8158cb19ac25ec69db4cbb09c1eaf853a559e9a.tar.gz
dfhack-a8158cb19ac25ec69db4cbb09c1eaf853a559e9a.tar.bz2
dfhack-a8158cb19ac25ec69db4cbb09c1eaf853a559e9a.tar.xz
Merge remote-tracking branch 'q/master'
Diffstat (limited to 'plugins/devel')
-rw-r--r--plugins/devel/CMakeLists.txt4
-rw-r--r--plugins/devel/kittens.cpp6
-rw-r--r--plugins/devel/memview.cpp2
-rw-r--r--plugins/devel/ref-index.cpp149
-rw-r--r--plugins/devel/rprobe.cpp19
-rw-r--r--plugins/devel/vshook.cpp55
6 files changed, 222 insertions, 13 deletions
diff --git a/plugins/devel/CMakeLists.txt b/plugins/devel/CMakeLists.txt
index 5d1d585a..134d5cb6 100644
--- a/plugins/devel/CMakeLists.txt
+++ b/plugins/devel/CMakeLists.txt
@@ -17,3 +17,7 @@ DFHACK_PLUGIN(stockcheck stockcheck.cpp)
DFHACK_PLUGIN(stripcaged stripcaged.cpp)
DFHACK_PLUGIN(rprobe rprobe.cpp)
DFHACK_PLUGIN(nestboxes nestboxes.cpp)
+DFHACK_PLUGIN(vshook vshook.cpp)
+IF(UNIX)
+DFHACK_PLUGIN(ref-index ref-index.cpp)
+ENDIF()
diff --git a/plugins/devel/kittens.cpp b/plugins/devel/kittens.cpp
index 2e8e6eab..b610d474 100644
--- a/plugins/devel/kittens.cpp
+++ b/plugins/devel/kittens.cpp
@@ -257,7 +257,7 @@ command_result kittens (color_ostream &out, vector <string> & parameters)
};
con.cursor(false);
con.clear();
- Console::color_value color = Console::COLOR_BLUE;
+ Console::color_value color = COLOR_BLUE;
while(1)
{
if(shutdown_flag)
@@ -282,7 +282,7 @@ command_result kittens (color_ostream &out, vector <string> & parameters)
con.flush();
con.msleep(60);
((int&)color) ++;
- if(color > Console::COLOR_MAX)
- color = Console::COLOR_BLUE;
+ if(color > COLOR_MAX)
+ color = COLOR_BLUE;
}
}
diff --git a/plugins/devel/memview.cpp b/plugins/devel/memview.cpp
index 5d8d6a9b..757b475d 100644
--- a/plugins/devel/memview.cpp
+++ b/plugins/devel/memview.cpp
@@ -73,7 +73,7 @@ void outputHex(uint8_t *buf,uint8_t *lbuf,size_t len,size_t start,color_ostream
con.reset_color();
if(isAddr((uint32_t *)(buf+j+i),ranges))
- con.color(Console::COLOR_LIGHTRED); //coloring in the middle does not work
+ con.color(COLOR_LIGHTRED); //coloring in the middle does not work
//TODO make something better?
}
if(lbuf[j+i]!=buf[j+i])
diff --git a/plugins/devel/ref-index.cpp b/plugins/devel/ref-index.cpp
new file mode 100644
index 00000000..686f6918
--- /dev/null
+++ b/plugins/devel/ref-index.cpp
@@ -0,0 +1,149 @@
+#include "Core.h"
+#include <Console.h>
+#include <Export.h>
+#include <PluginManager.h>
+#include <modules/Gui.h>
+#include <modules/Screen.h>
+#include <vector>
+#include <cstdio>
+#include <stack>
+#include <string>
+#include <cmath>
+
+#include <VTableInterpose.h>
+#include "df/item.h"
+#include "df/unit.h"
+#include "df/world.h"
+#include "df/general_ref_item.h"
+#include "df/general_ref_unit.h"
+
+using std::vector;
+using std::string;
+using std::stack;
+using namespace DFHack;
+
+using df::global::gps;
+
+DFHACK_PLUGIN("ref-index");
+
+#define global_id id
+
+template<class T>
+T get_from_global_id_vector(int32_t id, const std::vector<T> &vect, int32_t *cache)
+{
+ size_t size = vect.size();
+ int32_t start=0;
+ int32_t end=(int32_t)size-1;
+
+ // Check the cached location. If it is a match, this provides O(1) lookup.
+ // Otherwise it works like one binsearch iteration.
+ if (size_t(*cache) < size)
+ {
+ T cptr = vect[*cache];
+ if (cptr->global_id == id)
+ return cptr;
+ if (cptr->global_id < id)
+ start = *cache+1;
+ else
+ end = *cache-1;
+ }
+
+ // Regular binsearch. The end check provides O(1) caching for missing item.
+ if (start <= end && vect[end]->global_id >= id)
+ {
+ do {
+ int32_t mid=(start+end)>>1;
+
+ T cptr=vect[mid];
+ if(cptr->global_id==id)
+ {
+ *cache = mid;
+ return cptr;
+ }
+ else if(cptr->global_id>id)end=mid-1;
+ else start=mid+1;
+ } while(start<=end);
+ }
+
+ *cache = end+1;
+ return NULL;
+}
+
+template<class T> T *find_object(int32_t id, int32_t *cache);
+template<> df::item *find_object<df::item>(int32_t id, int32_t *cache) {
+ return get_from_global_id_vector(id, df::global::world->items.all, cache);
+}
+template<> df::unit *find_object<df::unit>(int32_t id, int32_t *cache) {
+ return get_from_global_id_vector(id, df::global::world->units.all, cache);
+}
+
+template<class T>
+struct CachedRef {
+ int32_t id;
+ int32_t cache;
+ CachedRef(int32_t id = -1) : id(id), cache(-1) {}
+ T *target() { return find_object<T>(id, &cache); }
+};
+
+#ifdef LINUX_BUILD
+struct item_hook : df::general_ref_item {
+ typedef df::general_ref_item interpose_base;
+
+ DEFINE_VMETHOD_INTERPOSE(df::item*, getItem, ())
+ {
+ // HUGE HACK: ASSUMES THERE ARE 4 USABLE BYTES AFTER THE OBJECT
+ // This actually is true with glibc allocator due to granularity.
+ return find_object<df::item>(item_id, 1+&item_id);
+ }
+};
+
+IMPLEMENT_VMETHOD_INTERPOSE(item_hook, getItem);
+
+struct unit_hook : df::general_ref_unit {
+ typedef df::general_ref_unit interpose_base;
+
+ DEFINE_VMETHOD_INTERPOSE(df::unit*, getUnit, ())
+ {
+ // HUGE HACK: ASSUMES THERE ARE 4 USABLE BYTES AFTER THE OBJECT
+ // This actually is true with glibc allocator due to granularity.
+ return find_object<df::unit>(unit_id, 1+&unit_id);
+ }
+};
+
+IMPLEMENT_VMETHOD_INTERPOSE(unit_hook, getUnit);
+
+command_result hook_refs(color_ostream &out, vector <string> & parameters)
+{
+ auto &hook = INTERPOSE_HOOK(item_hook, getItem);
+ if (hook.is_applied())
+ {
+ hook.remove();
+ INTERPOSE_HOOK(unit_hook, getUnit).remove();
+ }
+ else
+ {
+ hook.apply();
+ INTERPOSE_HOOK(unit_hook, getUnit).apply();
+ }
+
+ if (hook.is_applied())
+ out.print("Hook is applied.\n");
+ else
+ out.print("Hook is not applied.\n");
+ return CR_OK;
+}
+#endif
+
+DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
+{
+#ifdef LINUX_BUILD
+ commands.push_back(PluginCommand("hook-refs","Inject O(1) cached lookup into general refs.",hook_refs));
+#endif
+
+ return CR_OK;
+}
+
+DFhackCExport command_result plugin_shutdown ( color_ostream &out )
+{
+ return CR_OK;
+}
diff --git a/plugins/devel/rprobe.cpp b/plugins/devel/rprobe.cpp
index 7a091a96..805489d5 100644
--- a/plugins/devel/rprobe.cpp
+++ b/plugins/devel/rprobe.cpp
@@ -27,6 +27,7 @@ using namespace std;
#include "df/world_region_details.h"
#include "df/world_geo_biome.h"
#include "df/world_geo_layer.h"
+#include "df/region_map_entry.h"
#include "df/inclusion_type.h"
#include "df/viewscreen_choose_start_sitest.h"
@@ -79,7 +80,7 @@ command_result rprobe (color_ostream &out, vector <string> & parameters)
if (parameters.size() == 2)
{
- if (parameters[0] == "wet")
+ if (parameters[0] == "rai")
set_field = 0;
else if (parameters[0] == "veg")
set_field = 1;
@@ -87,7 +88,7 @@ command_result rprobe (color_ostream &out, vector <string> & parameters)
set_field = 2;
else if (parameters[0] == "evi")
set_field = 3;
- else if (parameters[0] == "hil")
+ else if (parameters[0] == "dra")
set_field = 4;
else if (parameters[0] == "sav")
set_field = 5;
@@ -113,11 +114,11 @@ command_result rprobe (color_ostream &out, vector <string> & parameters)
{
coord2d rg = screen->biome_rgn[i];
- df::world_data::T_region_map* rd = &data->region_map[rg.x][rg.y];
+ auto rd = &data->region_map[rg.x][rg.y];
if (set && i == to_set) {
if (set_field == 0)
- rd->wetness = set_val;
+ rd->rainfall = set_val;
else if (set_field == 1)
rd->vegetation = set_val;
else if (set_field == 2)
@@ -125,11 +126,11 @@ command_result rprobe (color_ostream &out, vector <string> & parameters)
else if (set_field == 3)
rd->evilness = set_val;
else if (set_field == 4)
- rd->hilliness = set_val;
+ rd->drainage = set_val;
else if (set_field == 5)
rd->savagery = set_val;
else if (set_field == 6)
- rd->saltiness = set_val;
+ rd->salinity = set_val;
}
out << i << ": x = " << rg.x << ", y = " << rg.y;
@@ -140,13 +141,13 @@ command_result rprobe (color_ostream &out, vector <string> & parameters)
" landmass_id: " << rd->landmass_id <<
" flags: " << hex << rd->flags.as_int() << dec << endl;
out <<
- "wet: " << rd->wetness << " " <<
+ "rai: " << rd->rainfall << " " <<
"veg: " << rd->vegetation << " " <<
"tem: " << rd->temperature << " " <<
"evi: " << rd->evilness << " " <<
- "hil: " << rd->hilliness << " " <<
+ "dra: " << rd->drainage << " " <<
"sav: " << rd->savagery << " " <<
- "sal: " << rd->saltiness;
+ "sal: " << rd->salinity;
int32_t *p = (int32_t *)rd;
int c = sizeof(*rd) / sizeof(int32_t);
diff --git a/plugins/devel/vshook.cpp b/plugins/devel/vshook.cpp
new file mode 100644
index 00000000..ceec2d08
--- /dev/null
+++ b/plugins/devel/vshook.cpp
@@ -0,0 +1,55 @@
+#include "Core.h"
+#include <Console.h>
+#include <Export.h>
+#include <PluginManager.h>
+#include <modules/Gui.h>
+#include <modules/Screen.h>
+#include <vector>
+#include <cstdio>
+#include <stack>
+#include <string>
+#include <cmath>
+
+#include <VTableInterpose.h>
+#include "df/graphic.h"
+#include "df/viewscreen_titlest.h"
+
+using std::vector;
+using std::string;
+using std::stack;
+using namespace DFHack;
+
+using df::global::gps;
+
+DFHACK_PLUGIN("vshook");
+
+struct title_hook : df::viewscreen_titlest {
+ typedef df::viewscreen_titlest interpose_base;
+
+ DEFINE_VMETHOD_INTERPOSE(void, render, ())
+ {
+ INTERPOSE_NEXT(render)();
+
+ Screen::Pen pen(' ',COLOR_WHITE,COLOR_BLACK);
+ Screen::paintString(pen,0,0,"DFHack " DFHACK_VERSION);
+ }
+};
+
+IMPLEMENT_VMETHOD_INTERPOSE(title_hook, render);
+
+DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
+{
+ if (gps)
+ {
+ if (!INTERPOSE_HOOK(title_hook, render).apply())
+ out.printerr("Could not interpose viewscreen_titlest::render\n");
+ }
+
+ return CR_OK;
+}
+
+DFhackCExport command_result plugin_shutdown ( color_ostream &out )
+{
+ INTERPOSE_HOOK(title_hook, render).remove();
+ return CR_OK;
+}