diff --git a/library/DFHackAPI.cpp b/library/DFHackAPI.cpp index 62f51368e..94162128b 100644 --- a/library/DFHackAPI.cpp +++ b/library/DFHackAPI.cpp @@ -46,6 +46,7 @@ class API::Private uint32_t window_y_offset; uint32_t window_z_offset; uint32_t cursor_xyz_offset; + uint32_t window_dims_offset; uint32_t creature_pos_offset; uint32_t creature_type_offset; @@ -72,6 +73,8 @@ class API::Private uint32_t creature_happiness_offset; uint32_t creature_traits_offset; + uint32_t item_material_offset; + uint32_t dwarf_lang_table_offset; ProcessEnumerator* pm; @@ -86,6 +89,7 @@ class API::Private bool vegetationInited; bool creaturesInited; bool cursorWindowInited; + bool viewSizeInited; bool itemsInited; bool nameTablesInited; @@ -123,6 +127,7 @@ API::API(const string path_to_xml) d->buildingsInited = false; d->vegetationInited = false; d->cursorWindowInited = false; + d->viewSizeInited = false; d->itemsInited = false; d->pm = NULL; } @@ -1154,6 +1159,7 @@ bool API::InitViewAndCursor() d->window_y_offset = d->offset_descriptor->getAddress("window_y"); d->window_z_offset = d->offset_descriptor->getAddress("window_z"); d->cursor_xyz_offset = d->offset_descriptor->getAddress("cursor_xyz"); + if(d->window_x_offset && d->window_y_offset && d->window_z_offset) { d->cursorWindowInited = true; @@ -1165,6 +1171,20 @@ bool API::InitViewAndCursor() } } +bool API::InitViewSize() +{ + d->window_dims_offset = d->offset_descriptor->getAddress("window_dims"); + if(d->window_dims_offset) + { + d->viewSizeInited = true; + return true; + } + else + { + return false; + } +} + bool API::getViewCoords (int32_t &x, int32_t &y, int32_t &z) { assert(d->cursorWindowInited); @@ -1202,6 +1222,23 @@ bool API::setCursorCoords (const int32_t &x, const int32_t &y, const int32_t &z) Mwrite(d->cursor_xyz_offset,3*sizeof(int32_t),(uint8_t *)coords); return true; } +bool API::getWindowSize(int32_t &width, int32_t &height) +{ + assert(d->viewSizeInited); + int32_t coords[2]; + Mread(d->window_dims_offset,2*sizeof(int32_t),(uint8_t *)coords); + width = coords[0]; + height = coords[1]; + return true; +} +////FIXME: I don't know what is going to happen if you try to set these to bad values, probably bad things... +//bool API::setWindowSize(const int32_t &width, const int32_t &height) +//{ +// assert(d->viewSizeInited); +// int32_t coords[2] = {width,height}; +// Mwrite(d->window_dims_offset,2*sizeof(int32_t),(uint8_t *)coords); +// return true; +//} memory_info API::getMemoryInfo() { @@ -1211,11 +1248,15 @@ Process * API::getProcess() { return d->p; } - + uint32_t API::InitReadItems() { int items = d->offset_descriptor->getAddress("items"); assert(items); + + d->item_material_offset = d->offset_descriptor->getOffset("item_materials"); + assert(d->item_material_offset); + d->p_itm = new DfVector( d->dm->readVector(items,4)); d->itemsInited = true; return d->p_itm->getSize(); @@ -1223,15 +1264,14 @@ uint32_t API::InitReadItems() bool API::ReadItem(const uint32_t &index, t_item & item) { assert(d->buildingsInited && d->itemsInited); //should change to the generic init rather than buildings - t_item_df40d item_40d; // read pointer from vector at position uint32_t temp = *(uint32_t *) d->p_itm->at(index); - + //read building from memory Mread(temp, sizeof(t_item_df40d), (uint8_t *)&item_40d); - + // transform int32_t type = -1; d->offset_descriptor->resolveClassId(temp, type); @@ -1243,7 +1283,13 @@ bool API::ReadItem(const uint32_t &index, t_item & item) item.type = type; item.ID = item_40d.ID; item.flags = item_40d.flags; - + + //TODO certain item types (creature based, threads, seeds, bags do not have the first matType byte, instead they have the material index only located at 0x68 + Mread(temp+d->item_material_offset, sizeof(t_matglossPair), (uint8_t *) &item.material); + //for(int i = 0; i < 0xCC; i++){ // used for item research + // uint8_t byte = MreadByte(temp+i); + // item.bytes.push_back(byte); + //} return true; } void API::FinishReadItems() diff --git a/library/DFHackAPI.h b/library/DFHackAPI.h index 2de87ed4e..297025f17 100644 --- a/library/DFHackAPI.h +++ b/library/DFHackAPI.h @@ -173,6 +173,11 @@ namespace DFHack bool getCursorCoords (int32_t &x, int32_t &y, int32_t &z); bool setCursorCoords (const int32_t &x, const int32_t &y, const int32_t &z); + + bool InitViewSize(); + bool getWindowSize(int32_t & width, int32_t & height); + bool setWindowSize(const int32_t & width, const int32_t & height); + /* // FIXME: add a real creature class, move these string getLastName(const uint32_t &index, bool); @@ -183,6 +188,7 @@ namespace DFHack vector getTraits(const uint32_t &index); vector getLabors(const uint32_t &index); */ + void InitReadNameTables(); void FinishReadNameTables(); diff --git a/library/DFProcess-windows.cpp b/library/DFProcess-windows.cpp index 6c90998e0..b8abb5435 100644 --- a/library/DFProcess-windows.cpp +++ b/library/DFProcess-windows.cpp @@ -69,6 +69,7 @@ Process::Process(uint32_t pid, vector & known_versions) if(EnumProcessModules(hProcess, &hmod, 1 * sizeof(HMODULE), &junk) == 0) { CloseHandle(hProcess); + return; //if enumprocessModules fails, give up } // got base ;) diff --git a/library/DFTypes.h b/library/DFTypes.h index 8fee3b18b..ccea57383 100644 --- a/library/DFTypes.h +++ b/library/DFTypes.h @@ -538,6 +538,12 @@ struct t_item uint32_t flags; uint32_t ID; uint32_t type; + t_matglossPair material; + /* + uint8_t matType; + uint8_t material; + */ + // vector bytes; used for item research // FIXME: not complete, we need building presence bitmaps for stuff like farm plots and stockpiles, orientation (N,E,S,W) and state (open/closed) }; diff --git a/library/DFVector.h b/library/DFVector.h index 3e9af32c6..784801bfa 100644 --- a/library/DFVector.h +++ b/library/DFVector.h @@ -37,6 +37,10 @@ namespace DFHack uint8_t* data; public: + DfVector() + { + data = 0; + }; DfVector(uint32_t _start, uint32_t _size, uint32_t _item_size): start(_start),size(_size),item_size(_item_size) { @@ -53,7 +57,8 @@ namespace DFHack }; ~DfVector() { - delete [] data; + if(data) + delete [] data; } // get offset of the specified index inline void* operator[] (uint32_t index) @@ -73,48 +78,5 @@ namespace DFHack return size; }; }; - - - // nice try, come again... - /* - template - class DfVector - { - private: - // starting offset - uint32_t start; - // vector size - uint32_t size; - T* data; - - public: - DfVector(uint32_t _start, uint32_t _size): - start(_start),size(_size) - { - Mread(start,size*sizeof(T),data); - }; - DfVector(const DfVector & vec) - { - start = vec.start; - size = vec.size; - data = new T[size]; - Mread(start,size*sizeof(T),data); - }; - ~DfVector() - { - delete [] data; - } - // get offset of the specified index - inline T* operator[] (uint32_t index) - { - assert(index < size); - return data + index*sizeof(T); - }; - // get vector size - inline uint32_t getSize () - { - return size; - }; - };*/ } #endif // DFVECTOR_H_INCLUDED diff --git a/output/Memory.xml b/output/Memory.xml index ecb9ef360..94d970fbb 100644 --- a/output/Memory.xml +++ b/output/Memory.xml @@ -913,6 +913,7 @@
0x00DA061C
0x00DCE6C8
0x00DCE6A4
+
0x01746E98
@@ -921,6 +922,7 @@
15BDF50
+ 0x68 diff --git a/tools/dfitemdump.cpp b/tools/dfitemdump.cpp index 2fb957bd4..c496f611b 100644 --- a/tools/dfitemdump.cpp +++ b/tools/dfitemdump.cpp @@ -11,9 +11,57 @@ using namespace std; #include #include +struct matGlosses +{ + vector plantMat; + vector woodMat; + vector stoneMat; + vector metalMat; + vector creatureMat; +}; + +void printItem(t_item item, const vector & buildingTypes,const matGlosses & mat){ + cout << dec << "Item at x:" << item.x << " y:" << item.y << " z:" << item.z << endl; + cout << "Type: " << (int) item.type << " " << buildingTypes[item.type] << " Address: " << hex << item.origin << endl; + cout << "Material: "; + + //If the item is a thread, seed, or creature based, there is no MatType, so the MatType is actually the material + //This should probably be checked in the item function + if(item.type == 113 || item.type == 117) // item_thread or item_seeds + { + cout << mat.plantMat[item.material.type].id; + } + else if(item.type == 114 || item.type == 115 || item.type == 116 || item.type==128 || item.type == 129|| item.type == 130|| item.type == 131) // item_skin_raw item_bones item_skill item_fish_raw item_pet item_skin_tanned item_shell + { + cout << mat.creatureMat[item.material.type].id; + } + else{ + switch (item.material.type) + { + case 0: + cout << mat.woodMat[item.material.index].id; + break; + case 1: + cout << mat.stoneMat[item.material.index].id; + break; + case 2: + cout << mat.metalMat[item.material.index].id; + break; + case 12: // don't ask me why this has such a large jump, maybe this is not actually the matType for plants, but they all have this set to 12 + cout << mat.plantMat[item.material.index].id; + break; + case 121: + cout << mat.creatureMat[item.material.index].id; + break; + default: + cerr << "invalid mat" << (int) item.material.type << " " << (int) item.material.index << endl; + } + } + cout << endl; +} + int main () { - DFHack::API DF ("Memory.xml"); if(!DF.Attach()) { @@ -21,7 +69,13 @@ int main () return 1; } - + matGlosses mat; + DF.ReadPlantMatgloss(mat.plantMat); + DF.ReadWoodMatgloss(mat.woodMat); + DF.ReadStoneMatgloss(mat.stoneMat); + DF.ReadMetalMatgloss(mat.metalMat); + DF.ReadCreatureMatgloss(mat.creatureMat); + vector buildingtypes; uint32_t numBuildings = DF.InitReadBuildings(buildingtypes); DF.InitViewAndCursor(); @@ -37,16 +91,73 @@ int main () { break; } + // else if(next == 'c'){ + // cerr << "clearing similarity" << endl; + // similarity.clear(); + // continue; + // } + + // else if(next == 'p'){ + // vector same(similarity[0].size(),true); + // for(int k =0; k < similarity.size(); k++){ + // for(int j =0; j < similarity.size(); j++){ + // if(k != j){ + // for(int l =0; l < similarity[k].size(); l++){ + // if(similarity[k][l] != similarity[j][l]){ + // same[l] = false; + // } + // } + // } + // } + // } + // for(int itr =0; itr < same.size(); itr++){ + // if(same[itr] == true && similarity[0][itr] != 0){ + // cout << hex << itr << " " << hex << (int)similarity[0][itr] << endl; + // } + // } + // continue; + // } int32_t x,y,z; DF.getCursorCoords(x,y,z); + vector foundItems; for(uint32_t i = 0; i < numItems; i++) { t_item temp; DF.ReadItem(i, temp); if(temp.x == x && temp.y == y && temp.z == z) { - cout << buildingtypes[temp.type] << " 0x" << hex << temp.origin << endl; + foundItems.push_back(temp); + //cout << buildingtypes[temp.type] << " 0x" << hex << temp.origin << endl; + } + } + if(foundItems.size() == 0){ + cerr << "No Items at x:" << x << " y:" << y << " z:" << z << endl; + } + else if(foundItems.size() == 1){ + printItem(foundItems[0], buildingtypes,mat); + // similarity.push_back(foundItems[0].bytes); + } + else{ + cerr << "Please Select which item you want to display\n"; + for(int j = 0; j < foundItems.size(); ++j){ + cerr << j << " " << buildingtypes[foundItems[j].type] << endl; + } + int value; + string input2; + stringstream ss; + getline(cin, input2); + ss.str(input2); + ss >> value; + while(value >= foundItems.size()){ + cerr << "invalid choice, please try again" << endl; + input2.clear(); + ss.clear(); + getline (cin, input2); + ss.str(input2); + ss >> value; } + printItem(foundItems[value], buildingtypes,mat); + // similarity.push_back(foundItems[value].bytes); } DF.FinishReadItems(); } diff --git a/tools/position.cpp b/tools/position.cpp index 850880942..be5a3e967 100644 --- a/tools/position.cpp +++ b/tools/position.cpp @@ -21,17 +21,28 @@ int main (void) if (DF.InitViewAndCursor()) { - int32_t x,y,z; - if(DF.getViewCoords(x,y,z)) + int32_t x,y,z; + if(DF.getViewCoords(x,y,z)) cout << "view coords: " << x << "/" << y << "/" << z << endl; - if(DF.getCursorCoords(x,y,z)) + if(DF.getCursorCoords(x,y,z)) cout << "cursor coords: " << x << "/" << y << "/" << z << endl; } else { cerr << "cursor and window parameters are unsupported on your version of DF" << endl; } - + + if(DF.InitViewSize()) + { + int32_t width,height; + if(DF.getWindowSize(width,height)) + cout << "window size : " << width << " " << height << endl; + } + else + { + cerr << "view size is unsupported on your version of DF" << endl; + } + if(!DF.Detach()) { cerr << "Can't detach from DF" << endl;