Many fixes to DFHack tools and examples

develop
Petr Mrázek 2010-03-26 13:01:46 +01:00
parent 02b7d92135
commit 4d939363b5
24 changed files with 431 additions and 241 deletions

@ -1,5 +1,5 @@
// Attach test // Attach test
// attachtest - 100x attach/detach, 100x reads, 100x writes // attachtest - 1000x suspend/resume
#include <iostream> #include <iostream>
#include <climits> #include <climits>
@ -21,9 +21,12 @@ int main (void)
DF.Attach(); DF.Attach();
DF.Detach(); DF.Detach();
} }
catch (DFHack::Error::NoProcess& e) catch (exception& e)
{ {
cerr << e.what() << endl; cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
/* /*

@ -8,18 +8,10 @@
#include <vector> #include <vector>
using namespace std; using namespace std;
#include <DFError.h>
#include <DFTypes.h> #include <DFTypes.h>
#include <DFHackAPI.h> #include <DFHackAPI.h>
#include <DFMemInfo.h> #include <DFMemInfo.h>
/*
oh. fsck it. I'll do the hexdump now and add the things I found in a research/ folder
groups of four bytes, 4 per line
1 space between bytes
2 between groups
offset from the object start on the left
and length set from command line
default 256
*/
/* /*
address = absolute address of dump start address = absolute address of dump start
@ -131,9 +123,16 @@ int main (int argc,const char* argv[])
vector<DFHack::t_matgloss> creaturestypes; vector<DFHack::t_matgloss> creaturestypes;
DFHack::API DF ("Memory.xml"); DFHack::API DF ("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DFHack::memory_info * mem = DF.getMemoryInfo(); DFHack::memory_info * mem = DF.getMemoryInfo();
@ -173,9 +172,9 @@ int main (int argc,const char* argv[])
} }
DF.Detach(); DF.Detach();
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl; cout << "Done. Press any key to continue" << endl;
cin.ignore(); cin.ignore();
#endif #endif
return 0; return 0;
} }

@ -1,6 +1,7 @@
// Catsplosion // Catsplosion
// By Zhentar // By Zhentar
// This work of evil makes every cat, male or female, grown or kitten, pregnant and due within 2 in-game hours... // This work of evil makes every cat, male or female, grown or kitten, pregnant
// and due within 2 in-game hours...
#include <iostream> #include <iostream>
#include <climits> #include <climits>
@ -9,6 +10,7 @@
#include <stdlib.h> // for rand() #include <stdlib.h> // for rand()
using namespace std; using namespace std;
#include <DFError.h>
#include <DFTypes.h> #include <DFTypes.h>
#include <DFHackAPI.h> #include <DFHackAPI.h>
#include <DFMemInfo.h> #include <DFMemInfo.h>
@ -33,13 +35,19 @@ int fertilizeCat(DFHack::API & DF, const DFHack::t_creature & creature)
int main (void) int main (void)
{ {
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
proc = DF.getProcess(); proc = DF.getProcess();
mem = DF.getMemoryInfo(); mem = DF.getMemoryInfo();
creature_pregnancy_offset = mem->getOffset("creature_pregnancy"); creature_pregnancy_offset = mem->getOffset("creature_pregnancy");
@ -47,6 +55,9 @@ int main (void)
if(!DF.ReadCreatureMatgloss(creaturestypes)) if(!DF.ReadCreatureMatgloss(creaturestypes))
{ {
cerr << "Can't get the creature types." << endl; cerr << "Can't get the creature types." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
@ -54,6 +65,9 @@ int main (void)
if(!DF.InitReadCreatures(numCreatures)) if(!DF.InitReadCreatures(numCreatures))
{ {
cerr << "Can't get creatures" << endl; cerr << "Can't get creatures" << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }

@ -6,6 +6,7 @@
#include <vector> #include <vector>
using namespace std; using namespace std;
#include <DFError.h>
#include <DFTypes.h> #include <DFTypes.h>
#include <DFHackAPI.h> #include <DFHackAPI.h>
#include <DFMemInfo.h> #include <DFMemInfo.h>
@ -301,9 +302,16 @@ void printCreature(DFHack::API & DF, const DFHack::t_creature & creature)
int main (void) int main (void)
{ {
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }

@ -8,6 +8,7 @@
#include <vector> #include <vector>
using namespace std; using namespace std;
#include <DFError.h>
#include <DFTypes.h> #include <DFTypes.h>
#include <DFHackAPI.h> #include <DFHackAPI.h>
#include <DFMemInfo.h> #include <DFMemInfo.h>
@ -190,14 +191,20 @@ void printItem(DFHack::t_item item, const string & typeString,const matGlosses &
int main () int main ()
{ {
DFHack::API DF("Memory.xml");
DFHack::API DF ("Memory.xml"); try
{
if(!DF.Attach()) DF.Attach();
}
catch (exception& e)
{ {
cerr << "DF not found" << endl; cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DFHack::memory_info * mem = DF.getMemoryInfo(); DFHack::memory_info * mem = DF.getMemoryInfo();
DF.InitViewAndCursor(); DF.InitViewAndCursor();
matGlosses mat; matGlosses mat;
@ -280,11 +287,10 @@ int main ()
} }
DF.FinishReadItems(); DF.FinishReadItems();
} }
DF.FinishReadBuildings();
DF.Detach(); DF.Detach();
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl; cout << "Done. Press any key to continue" << endl;
cin.ignore(); cin.ignore();
#endif #endif
return 0; return 0;
} }

@ -50,9 +50,16 @@ int main (int numargs, char** args)
DFHack::mapblock40d Block; DFHack::mapblock40d Block;
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }

@ -45,12 +45,20 @@ uint32_t endian_swap(uint32_t x)
int main (void) int main (void)
{ {
DFHack::API DF ("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
vector< vector<string> > englishWords; vector< vector<string> > englishWords;
vector< vector<string> > foreignWords; vector< vector<string> > foreignWords;
if(!DF.InitReadNameTables(englishWords,foreignWords)) if(!DF.InitReadNameTables(englishWords,foreignWords))

@ -15,9 +15,16 @@ int main (void)
{ {
vector<DFHack::t_matgloss> creaturestypes; vector<DFHack::t_matgloss> creaturestypes;
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }

@ -11,11 +11,19 @@ using namespace std;
int main (void) int main (void)
{ {
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
vector <DFHack::t_matgloss> Woods; vector <DFHack::t_matgloss> Woods;
DF.ReadWoodMatgloss(Woods); DF.ReadWoodMatgloss(Woods);

@ -13,12 +13,19 @@ using namespace std;
int main (void) int main (void)
{ {
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
} }
else catch (exception& e)
{ {
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
if (DF.InitViewAndCursor()) if (DF.InitViewAndCursor())
{ {
int32_t x,y,z; int32_t x,y,z;
@ -47,7 +54,7 @@ int main (void)
{ {
cerr << "Can't detach from DF" << endl; cerr << "Can't detach from DF" << endl;
} }
}
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl; cout << "Done. Press any key to continue" << endl;
cin.ignore(); cin.ignore();

@ -24,10 +24,12 @@ void print_bits ( T val, std::ostream& out )
val >>= 1; val >>= 1;
} }
} }
vector< vector<string> > englishWords; vector< vector<string> > englishWords;
vector< vector<string> > foreignWords; vector< vector<string> > foreignWords;
uint32_t numCreatures; uint32_t numCreatures;
vector<DFHack::t_matgloss> creaturestypes; vector<DFHack::t_matgloss> creaturestypes;
void printDwarves(DFHack::API & DF) void printDwarves(DFHack::API & DF)
{ {
int dwarfCounter = 0; int dwarfCounter = 0;
@ -311,12 +313,18 @@ bool setCursorToCreature(DFHack::API &DF)
int main (void) int main (void)
{ {
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if (!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DF.Suspend();
DFHack::memory_info * mem = DF.getMemoryInfo(); DFHack::memory_info * mem = DF.getMemoryInfo();

@ -31,12 +31,20 @@ void printSettlement(DFHack::API & DF, const DFHack::t_settlement & settlement,
int main (int argc,const char* argv[]) int main (int argc,const char* argv[])
{ {
DFHack::API DF ("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DFHack::t_settlement current; DFHack::t_settlement current;
uint32_t numSettlements; uint32_t numSettlements;
if(!DF.InitReadSettlements(numSettlements)) if(!DF.InitReadSettlements(numSettlements))
@ -44,6 +52,7 @@ int main (int argc,const char* argv[])
cerr << "Could not read Settlements" << endl; cerr << "Could not read Settlements" << endl;
return 1; return 1;
} }
vector< vector<string> > englishWords; vector< vector<string> > englishWords;
vector< vector<string> > foreignWords; vector< vector<string> > foreignWords;
if(!DF.InitReadNameTables(englishWords,foreignWords)) if(!DF.InitReadNameTables(englishWords,foreignWords))
@ -51,6 +60,7 @@ int main (int argc,const char* argv[])
cerr << "Can't get name tables" << endl; cerr << "Can't get name tables" << endl;
return 1; return 1;
} }
cout << "Settlements\n"; cout << "Settlements\n";
/*for(uint32_t i =0;i<numSettlements;i++){ /*for(uint32_t i =0;i<numSettlements;i++){
DFHack::t_settlement temp; DFHack::t_settlement temp;
@ -58,6 +68,7 @@ int main (int argc,const char* argv[])
printSettlement(DF,temp,englishWords,foreignWords); printSettlement(DF,temp,englishWords,foreignWords);
}*/ }*/
// MSVC claims this is causing the heap to be corrupted, I think it is because the currentSettlement vector only has 1 item in it // MSVC claims this is causing the heap to be corrupted, I think it is because the currentSettlement vector only has 1 item in it
cout << "Current Settlement\n"; cout << "Current Settlement\n";
if(DF.ReadCurrentSettlement(current)) if(DF.ReadCurrentSettlement(current))
printSettlement(DF,current,englishWords,foreignWords); printSettlement(DF,current,englishWords,foreignWords);
@ -65,6 +76,7 @@ int main (int argc,const char* argv[])
DF.FinishReadNameTables(); DF.FinishReadNameTables();
DF.FinishReadSettlements(); DF.FinishReadSettlements();
DF.Detach(); DF.Detach();
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl; cout << "Done. Press any key to continue" << endl;
cin.ignore(); cin.ignore();

@ -15,11 +15,19 @@ int main (void)
{ {
string blah; string blah;
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
cout << "Attached, DF should be suspended now" << endl; cout << "Attached, DF should be suspended now" << endl;
getline(cin, blah); getline(cin, blah);

@ -22,12 +22,19 @@ int main (int numargs, const char ** args)
input >> std::hex >> addr; input >> std::hex >> addr;
} }
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
} }
else catch (exception& e)
{ {
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
DFHack::Process* p = DF.getProcess(); DFHack::Process* p = DF.getProcess();
DFHack::memory_info* mem = DF.getMemoryInfo(); DFHack::memory_info* mem = DF.getMemoryInfo();
const vector<string> * names = mem->getClassIDMapping(); const vector<string> * names = mem->getClassIDMapping();
@ -46,7 +53,7 @@ int main (int numargs, const char ** args)
cout << "cap 0x" << hex << p->readDWord(addr+0xC) << endl; cout << "cap 0x" << hex << p->readDWord(addr+0xC) << endl;
#endif #endif
*/ */
}
#ifndef LINUX_BUILD #ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl; cout << "Done. Press any key to continue" << endl;
cin.ignore(); cin.ignore();

@ -308,14 +308,18 @@ main(int argc, char *argv[])
vector<t_vein> veinVector; vector<t_vein> veinVector;
vector<t_frozenliquidvein> IceVeinVector; vector<t_frozenliquidvein> IceVeinVector;
// init the API
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
try
{
DF.Attach();
pDF = &DF; pDF = &DF;
// attach }
if(!DF.Attach()) catch (exception& e)
{ {
error = "Can't find DF."; cerr << e.what() << endl;
pDF = 0; #ifndef LINUX_BUILD
cin.ignore();
#endif
finish(0); finish(0);
} }

@ -268,7 +268,9 @@ bool API::InitMap()
uint32_t x_array_loc = g_pProcess->readDWord (map_offset); uint32_t x_array_loc = g_pProcess->readDWord (map_offset);
if (!x_array_loc) if (!x_array_loc)
{ {
throw Error::NoMapLoaded(); return false;
// FIXME: only throw this due to programmer error, in the other map functions
//throw Error::NoMapLoaded();
} }
// get the size // get the size

@ -15,13 +15,30 @@ int main (void)
uint32_t bytes_read = 0; uint32_t bytes_read = 0;
DFHack::occupancies40d occupancies; DFHack::occupancies40d occupancies;
DFHack::API DF ("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DF.InitMap();
// init the map
if(!DF.InitMap())
{
cerr << "Can't init map." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
DF.getSize(x_max,y_max,z_max); DF.getSize(x_max,y_max,z_max);
// walk the map // walk the map

@ -41,15 +41,20 @@ int main ()
int items; int items;
int found = 0, converted = 0; int found = 0, converted = 0;
DFHack::API DF ("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DF.Suspend();
// Find out which material is bauxite // Find out which material is bauxite
if(!DF.ReadStoneMatgloss(stoneMat)) if(!DF.ReadStoneMatgloss(stoneMat))
{ {

@ -299,7 +299,7 @@ int main (int argc, char** argv)
string s_targets; string s_targets;
string s_origin; string s_origin;
bool verbose; bool verbose;
int max; int max = 10;
argstream as(argc,argv); argstream as(argc,argv);
as >>option('v',"verbose",verbose,"Active verbose mode") as >>option('v',"verbose",verbose,"Active verbose mode")
@ -327,11 +327,21 @@ int main (int argc, char** argv)
else else
{ {
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(DF.Attach()) try
{ {
DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
if (DF.InitMap()) if (DF.InitMap())
{ {
int count = dig(DF, targets, 10, origin[0],origin[1],origin[2], verbose); int count = dig(DF, targets, max, origin[0],origin[1],origin[2], verbose);
cout << count << " targets designated" << endl; cout << count << " targets designated" << endl;
if (!DF.Detach()) if (!DF.Detach())
@ -344,14 +354,9 @@ int main (int argc, char** argv)
cerr << "Unable to init map" << endl; cerr << "Unable to init map" << endl;
} }
} }
else #ifndef LINUX_BUILD
{
cerr << "Unable to attach to DF process" << endl;
}
}
#ifndef LINUX_BUILD
cout << "Done. Press any key to continue" << endl; cout << "Done. Press any key to continue" << endl;
cin.ignore(); cin.ignore();
#endif #endif
return 0; return 0;
} }

@ -100,9 +100,16 @@ int main (void)
{ {
string select; string select;
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DFHack::Process * p = DF.getProcess(); DFHack::Process * p = DF.getProcess();

@ -194,9 +194,16 @@ int main ()
<< "Like set on fire all MICROCLINE item_stone..." << endl << "Like set on fire all MICROCLINE item_stone..." << endl
<< "Some unusual combinations might be untested and cause the program to crash..."<< endl << "Some unusual combinations might be untested and cause the program to crash..."<< endl
<< "so, watch your step and backup your fort" << endl; << "so, watch your step and backup your fort" << endl;
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DFHack::memory_info *mem = DF.getMemoryInfo(); DFHack::memory_info *mem = DF.getMemoryInfo();

@ -15,12 +15,19 @@ int main (void)
DFHack::designations40d designations; DFHack::designations40d designations;
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
try
if(!DF.Attach()) {
DF.Attach();
}
catch (exception& e)
{ {
cerr << "DF not found" << endl; cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DF.InitMap(); DF.InitMap();
if (DF.InitViewAndCursor()) if (DF.InitViewAndCursor())

@ -54,18 +54,29 @@ int main (int argc, const char* argv[])
vector<DFHack::t_matgloss> stonetypes; vector<DFHack::t_matgloss> stonetypes;
vector< vector <uint16_t> > layerassign; vector< vector <uint16_t> > layerassign;
// init the API
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
try
// attach {
if(!DF.Attach()) DF.Attach();
}
catch (exception& e)
{ {
cerr << "DF not found" << endl; cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
// init the map // init the map
DF.InitMap(); if(!DF.InitMap())
{
cerr << "Can't init map." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
DF.getSize(x_max,y_max,z_max); DF.getSize(x_max,y_max,z_max);
// get stone matgloss mapping // get stone matgloss mapping
@ -73,6 +84,9 @@ int main (int argc, const char* argv[])
{ {
//DF.DestroyMap(); //DF.DestroyMap();
cerr << "Can't get the materials." << endl; cerr << "Can't get the materials." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
@ -80,6 +94,9 @@ int main (int argc, const char* argv[])
if(!DF.ReadGeology( layerassign )) if(!DF.ReadGeology( layerassign ))
{ {
cerr << "Can't get region geology." << endl; cerr << "Can't get region geology." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }

@ -14,12 +14,29 @@ int main (void)
DFHack::designations40d designations; DFHack::designations40d designations;
DFHack::API DF("Memory.xml"); DFHack::API DF("Memory.xml");
if(!DF.Attach()) try
{ {
cerr << "DF not found" << endl; DF.Attach();
}
catch (exception& e)
{
cerr << e.what() << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1; return 1;
} }
DF.InitMap();
// init the map
if(!DF.InitMap())
{
cerr << "Can't init map." << endl;
#ifndef LINUX_BUILD
cin.ignore();
#endif
return 1;
}
DF.getSize(x_max,y_max,z_max); DF.getSize(x_max,y_max,z_max);
// walk the map // walk the map