Skip to content

Commit

Permalink
Fixed a bug where the SK item display feature did not work
Browse files Browse the repository at this point in the history
Fixed a bug where installing the maphack in multiple clients at once would sometimes cause a crash
  • Loading branch information
slayergod13 committed Aug 20, 2015
1 parent 526e83f commit 8a4bcc8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
12 changes: 10 additions & 2 deletions BH/MPQReader.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "MPQReader.h"
#include "BH.h"


std::map<std::string, MPQData*> MpqDataMap;

#define SFILE_INVALID_SIZE 0xFFFFFFFF
Expand Down Expand Up @@ -80,7 +79,6 @@ MPQData::MPQData(MPQFile *file) : error(ERROR_SUCCESS) {
}
MPQData::~MPQData() {}


// To handle servers with customized mpq files, try to read Patch_D2.mpq using Stormlib
// (http://www.zezula.net/en/mpq/stormlib.html). We load the StormLib dll with LoadLibrary
// to avoid imposing any run- or compile-time dependencies on the user. If we can't load
Expand All @@ -100,20 +98,27 @@ bool ReadMPQFiles(std::string fileName) {
SFileReadFile = (MPQReadFile)GetProcAddress(dllHandle, "SFileReadFile");
SFileCloseFile = (MPQCloseFile)GetProcAddress(dllHandle, "SFileCloseFile");

HANDLE pMutex = CreateMutex(NULL, true, "Global\BH_PATCH_D2_MPQ_MUTEX");
WaitForSingleObject(
pMutex, // handle to mutex
INFINITE); // no time-out interval

if (SFileOpenArchive && SFileCloseArchive && SFileOpenFileEx && SFileCloseFile && SFileGetFileSize && SFileReadFile) {
// Copy the MPQ file to avoid sharing access violations
std::string copyFileName(fileName);
size_t start_pos = copyFileName.find("Patch_D2.mpq");
if (start_pos != std::string::npos) {
copyFileName.replace(start_pos, 12, "Patch_D2.copy.mpq");
}

std::ifstream src(fileName.c_str(), std::ios::binary);
std::ofstream dst(copyFileName.c_str(), std::ios::binary);
dst << src.rdbuf();
dst.close();
src.close();

MPQArchive archive(copyFileName.c_str());

const int NUM_MPQS = 13;
std::string mpqFiles[NUM_MPQS] = {
"UniqueItems",
Expand Down Expand Up @@ -144,6 +149,9 @@ bool ReadMPQFiles(std::string fileName) {
}
}
FreeLibrary(dllHandle);

ReleaseMutex(pMutex);
CloseHandle(pMutex);
}
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions BH/Modules/Item/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ void Item::OnLoad() {
Toggles["Allow Unknown Items"] = BH::config->ReadToggle("Allow Unknown Items", "None", false);

//InitializeMPQData();
if (Toggles["Advanced Item Display"].state) {
InitializeItemRules();
}

showPlayer = BH::config->ReadKey("Show Players Gear", "VK_0");

Expand Down Expand Up @@ -80,6 +77,10 @@ void Item::OnUnload() {
void Item::OnLoop() {
if (!D2CLIENT_GetUIState(0x01))
viewingUnit = NULL;

if (Toggles["Advanced Item Display"].state) {
ItemDisplay::InitializeItemRules();
}

if (viewingUnit && viewingUnit->dwUnitId) {
if (!viewingUnit->pInventory){
Expand Down
54 changes: 32 additions & 22 deletions BH/Modules/Item/ItemDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,30 +225,40 @@ bool IntegerCompare(unsigned int Lvalue, BYTE operation, unsigned int Rvalue) {
}
}

void InitializeItemRules() {
vector<pair<string, string>> rules = BH::config->ReadMapList("ItemDisplay");
for (unsigned int i = 0; i < rules.size(); i++) {
string buf;
stringstream ss(rules[i].first);
vector<string> tokens;
while (ss >> buf) {
tokens.push_back(buf);
namespace ItemDisplay {
bool item_display_initialized = false;
void InitializeItemRules() {
if (item_display_initialized) return;
if (!IsInitialized()){
return;
}

LastConditionType = CT_None;
Rule *r = new Rule();
vector<Condition*> RawConditions;
for (vector<string>::iterator tok = tokens.begin(); tok < tokens.end(); tok++) {
Condition::BuildConditions(RawConditions, (*tok));
}
Condition::ProcessConditions(RawConditions, r->conditions);
BuildAction(&(rules[i].second), &(r->action));

RuleList.push_back(r);
if (r->action.colorOnMap.length() > 0) {
MapRuleList.push_back(r);
} else if (r->action.name.length() == 0) {
IgnoreRuleList.push_back(r);
item_display_initialized = true;
vector<pair<string, string>> rules = BH::config->ReadMapList("ItemDisplay");
for (unsigned int i = 0; i < rules.size(); i++) {
string buf;
stringstream ss(rules[i].first);
vector<string> tokens;
while (ss >> buf) {
tokens.push_back(buf);
}

LastConditionType = CT_None;
Rule *r = new Rule();
vector<Condition*> RawConditions;
for (vector<string>::iterator tok = tokens.begin(); tok < tokens.end(); tok++) {
Condition::BuildConditions(RawConditions, (*tok));
}
Condition::ProcessConditions(RawConditions, r->conditions);
BuildAction(&(rules[i].second), &(r->action));

RuleList.push_back(r);
if (r->action.colorOnMap.length() > 0) {
MapRuleList.push_back(r);
}
else if (r->action.name.length() == 0) {
IgnoreRuleList.push_back(r);
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion BH/Modules/Item/ItemDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ extern vector<Rule*> RuleList;
extern vector<Rule*> MapRuleList;
extern vector<Rule*> IgnoreRuleList;

void InitializeItemRules();
namespace ItemDisplay {
void InitializeItemRules();
}
StatProperties *GetStatProperties(unsigned int stat);
void BuildAction(string *str, Action *act);
void HandleUnknownItemCode(char *code, char *tag);
Expand Down
19 changes: 5 additions & 14 deletions Packaging/BH.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Token DB: tokens.db
//Item Display Configuration
Advanced Item Display: True, None

//Ignore 'junk' (Miscellaneous potions, quivers, etc.), potion, scroll renaming & gold piles below 2500.
//Ignore 'junk' (Miscellaneous potions, etc.), potion, scroll renaming & gold piles below 2500.
ItemDisplay[GOLD<2500]:
ItemDisplay[tsc]: %GREEN%+ %WHITE%TP
ItemDisplay[isc]: %GREEN%+ %WHITE%ID
Expand All @@ -154,9 +154,6 @@ ItemDisplay[mp4]: %BLUE%+ %WHITE%M4
ItemDisplay[mp5]: %BLUE%+ %WHITE%M5
ItemDisplay[rvs]: %PURPLE%+ %WHITE%R1
ItemDisplay[rvl]: %PURPLE%+ %WHITE%R2
ItemDisplay[aqv]:
ItemDisplay[cqv]:
ItemDisplay[key]:

//Ignore ALMOST ALL inferior, white and magic Normal and Exceptional items
//Exceptions:
Expand Down Expand Up @@ -214,8 +211,8 @@ ItemDisplay[SET amu]: %PURPLE%0 %GREEN%%NAME%%CONTINUE%
//Runewords
ItemDisplay[RW]: %NAME%%MAP%%CONTINUE%

//Ebug Bases >750 Defense
ItemDisplay[EQ2 !SUP ETH NMAG ELT SOCK=0 DEF>750]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
//Ebug Bases >650 Defense
ItemDisplay[EQ2 !SUP ETH NMAG ELT SOCK=0 DEF>650]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%

//Elite Eth Infinity/Insight Bases
ItemDisplay[WP8 ELT !7vo !7o7 ETH NMAG !SUP SOCK=0]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
Expand All @@ -236,12 +233,6 @@ ItemDisplay[utp !ETH NMAG (ED>9 SOCK=3 OR ED>14 !SOCK=1 !SOCK=2)]: %RED%0 %WHITE
//15ed Monarchs
ItemDisplay[uit !ETH NMAG ED>14 !SOCK=1 !SOCK=2 !SOCK=3]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%

//Memory & Leaf Bases
ItemDisplay[WP11 NMAG !SUP SOCK=0 SK52>2]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
ItemDisplay[WP11 !sst !lst !8ss !8ls !6ss !6ls NMAG !SUP SOCK=0 SK58>2]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
ItemDisplay[WP11 NMAG SOCK=2 SK52>2]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
ItemDisplay[WP11 NMAG SOCK=4 SK58>2]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%

//Chaos Bases
ItemDisplay[!ETH NMAG (9tw OR 7tw) (SK273>0 AND SK271>2) (SOCK=0 OR SOCK=3)]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
ItemDisplay[!ETH NMAG (9tw OR 7tw OR 9lw OR 7lw) (SK267>0 AND SK278>2) (SOCK=0 OR SOCK=3)]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
Expand All @@ -258,7 +249,7 @@ ItemDisplay[(7wa OR 7gd) ETH NMAG ED>9 (SOCK=0 OR SOCK=6)]: %RED%0 %WHITE%%NAME%
ItemDisplay[(7wa OR 7fb) ETH NMAG ED>9 SOCK=5]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%

//FOH Sticks
ItemDisplay[(wsp OR 9ws OR 7ws) (SK121>2 AND SK123>2) (SOCK=0 OR SOCK=5)]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
ItemDisplay[NMAG (wsp OR 9ws OR 7ws) (SK121>2 AND SK123>2) (SOCK=0 OR SOCK=5)]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%

//Diadems
ItemDisplay[ci3 !SUP !ETH NMAG SOCK=0]: %RED%0 %WHITE%%NAME%%MAP%%CONTINUE%
Expand Down Expand Up @@ -351,6 +342,6 @@ Mustache[stats]: {{#defense}}\n\n >{{defense}} defense{{/defense}}{{#stats}}\
Mustache[header-unique]: {{#quality=Unique}}**{{^name}}{{type}}{{/name}}{{name}}** (L{{iLevel}}){{#sockets}}[{{sockets}}]{{/sockets}}{{/quality}}
Mustache[header-magic]: {{#quality$Magic|Rare}}**{{^name}}{{type}}{{/name}}{{name}}** (L{{iLevel}}){{#sockets}}[{{sockets}}]{{/sockets}}{{/quality}}
Mustache[header-else]: {{#quality^Unique|Magic|Rare}}{{^isRuneword}}{{^name}}{{type}}{{/name}}{{name}}{{/isRuneword}}{{#isRuneword}}**{{runeword}}** {{type}}{{/isRuneword}} (L{{iLevel}}){{#sockets}}[{{sockets}}]{{/sockets}}{{/quality}}
Mustache[header]: {{>header-unique}}{{>header-magic}}{{>header-else}}{{#count}} **x{{count}}**{{/count}}
Mustache[header]: {{#isEthereal}}Eth {{/isEthereal}}{{>header-unique}}{{>header-magic}}{{>header-else}}{{#count}} **x{{count}}**{{/count}}
Mustache[item]: {{>header}}{{>stats}}{{^isRuneword}}{{#socketed}}\n\n * {{>>item}}{{/socketed}}{{/isRuneword}}\n
Mustache[stash]: {{#this}}* {{>item}}\n\n{{/this}}
4 changes: 2 additions & 2 deletions Packaging/UI.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Settings]
Minimized=1
X=0
Y=0
X=89
Y=179

0 comments on commit 8a4bcc8

Please sign in to comment.