Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dschu012 committed Apr 12, 2021
1 parent f91b2f4 commit 9ca79d4
Show file tree
Hide file tree
Showing 13 changed files with 567 additions and 272 deletions.
8 changes: 8 additions & 0 deletions Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ void ShowAction::SetResult(ItemActionResult* action, Unit* pItem) {
action->bHide = false;
}

void SetStyleAction::SetResult(ItemActionResult* action, Unit* pItem) {
if (STYLES.contains(m_Value)) {
for (auto act : STYLES[m_Value]) {
act->SetResult(action, pItem);
}
}
}

void SetNameAction::SetResult(ItemActionResult* action, Unit* pItem) {
std::map<std::wstring, TokenReplaceFunction> TOKEN_REPLACEMENT_FUNCTIONS = {
{ L"{Name}", &GetItemName },
Expand Down
6 changes: 6 additions & 0 deletions Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class PaletteIndexAction : public Action {
virtual void SetResult(ItemActionResult* action, Unit* pItem) = 0;
};

class SetStyleAction : public Action {
public:
SetStyleAction(std::wstring value) : Action(value) { }
void SetResult(ItemActionResult* action, Unit* pItem);
};

class SetNameAction : public ColorTextAction {
public:
SetNameAction(std::wstring value) : ColorTextAction(value) { }
Expand Down
112 changes: 81 additions & 31 deletions Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

namespace fs = std::filesystem;

#define CONDITION(NAME) validConditions[L#NAME " "] = &newInstance<NAME##Condition>
#define ACTION(NAME) validActions[L#NAME " "] = &newInstance<NAME##Action>
#define CONDITION(NAME) m_ValidConditions[L#NAME " "] = &newInstance<NAME##Condition>
#define ACTION(NAME) m_ValidActions[L#NAME " "] = &newInstance<NAME##Action>

Config::Config(std::wstring path) : m_Path(path) {
SetupValidConditions();
SetupValidActions();
}

void Config::SetupValidConditions(ConditionMap& validConditions) {
void Config::SetupValidConditions() {
CONDITION(Type);
CONDITION(Code);
CONDITION(Class); //Helm, Sword, etc... (ItemTypes.txt)
Expand Down Expand Up @@ -52,7 +54,8 @@ void Config::SetupValidConditions(ConditionMap& validConditions) {
CONDITION(Height);
}

void Config::SetupValidActions(ActionMap& validActions) {
void Config::SetupValidActions() {
ACTION(SetStyle);
ACTION(SetName);
ACTION(SetDescription);
ACTION(SetBackgroundColor);
Expand All @@ -63,11 +66,59 @@ void Config::SetupValidActions(ActionMap& validActions) {
ACTION(MinimapIcon); //todo.
}

void Config::ParseRule(Rule* rule, std::vector<Rule*> &rules, ConditionMap validConditions, ActionMap validActions, std::vector<Line>& lines) {
if (!rule) {
return;
void Config::HandleToken(ConfigToken token, std::wstring* line, uint32_t lineNum, std::vector<Rule*>& rules, std::map<std::wstring, std::vector<Action*>>& styles, std::vector<Line>& lines) {
switch (token) {
case ConfigToken::STYLE: {
ParseStyle(line, styles, lines);
break;
}
case ConfigToken::SHOW: {
Rule* rule = new Rule(lineNum);
rule->AddAction(new ShowAction());
ParseRule(rule, rules, lines);
break;
}
case ConfigToken::HIDE: {
Rule* rule = new Rule(lineNum);
rule->AddAction(new HideAction());
ParseRule(rule, rules, lines);
break;
}
default:
break;
}
lines.clear();
}

void Config::ParseStyle(std::wstring* line, std::map<std::wstring, std::vector<Action*>>& styles, std::vector<Line>& lines) {
std::wstring style = std::wstring(trim(line->substr(6)));
for (auto line : lines) {
std::wstring s = line.sText;
//ignore comments or blank lines
if (s.find(COMMENT) != std::string::npos) {
s = s.erase(s.find(COMMENT));
}
s = trim(s);
if (s.length() == 0) {
continue;
}
std::wstring conditionName = L"";
std::wstring value = L"";
for (auto validAction : m_ValidActions) {
int l = validAction.first.length();
if (s.compare(0, l, validAction.first) == 0) {
conditionName = s.substr(0, l);
value = s.substr(l);
break;
}
}
if (!conditionName.empty() && !value.empty()) {
styles[style].push_back(m_ValidActions[conditionName](value));
}
}
}

void Config::ParseRule(Rule* rule, std::vector<Rule*> &rules, std::vector<Line>& lines) {
AndCondition* andCondition = new AndCondition();
for (auto line : lines) {
std::wstring s = line.sText;
Expand All @@ -85,7 +136,7 @@ void Config::ParseRule(Rule* rule, std::vector<Rule*> &rules, ConditionMap valid
}
std::wstring conditionName = L"";
std::wstring value = L"";
for (auto validCondition : validConditions) {
for (auto validCondition : m_ValidConditions) {
int l = validCondition.first.length();
if (s.compare(0, l, validCondition.first) == 0) {
conditionName = s.substr(0, l);
Expand All @@ -94,10 +145,10 @@ void Config::ParseRule(Rule* rule, std::vector<Rule*> &rules, ConditionMap valid
}
}
if (!conditionName.empty() && !value.empty()) {
andCondition->AddCondition(validConditions[conditionName](value));
andCondition->AddCondition(m_ValidConditions[conditionName](value));
}
else {
for (auto validAction : validActions) {
for (auto validAction : m_ValidActions) {
int l = validAction.first.length();
if (s.compare(0, l, validAction.first) == 0) {
conditionName = s.substr(0, l);
Expand All @@ -106,7 +157,7 @@ void Config::ParseRule(Rule* rule, std::vector<Rule*> &rules, ConditionMap valid
}
}
if (!conditionName.empty() && !value.empty()) {
rule->AddAction(validActions[conditionName](value));
rule->AddAction(m_ValidActions[conditionName](value));
}
}
}
Expand All @@ -115,22 +166,19 @@ void Config::ParseRule(Rule* rule, std::vector<Rule*> &rules, ConditionMap valid
lines.clear();
}

std::vector<Rule*> Config::ParseFilter() {
void Config::ParseFilter(std::vector<Rule*>& rules, std::map<std::wstring, std::vector<Action*>>& styles) {
if (!fs::exists(m_Path)) {
MessageBoxA(nullptr, "No item.filter found.", "Error", MB_OK | MB_ICONSTOP);
exit(0);
}

ConditionMap conditionMap;
ActionMap actionMap;
SetupValidConditions(conditionMap);
SetupValidActions(actionMap);

std::vector<Rule*> rules;
std::wifstream f(m_Path);
std::wstring s;
uint32_t tokenLineNum = 0;
uint32_t lineNum = 0;
ConfigToken token = ConfigToken::NONE;
Rule* r = NULL;
std::wstring* line = NULL;
std::vector<Line> lines;

while (std::getline(f, s)) {
Expand All @@ -143,23 +191,25 @@ std::vector<Rule*> Config::ParseFilter() {
if (s.length() == 0) {
continue;
}
if (s.compare(0, 4, L"Show") == 0) {
ParseRule(r, rules, conditionMap, actionMap, lines);
r = new Rule(lineNum, s);
r->AddAction(new ShowAction());
if (s.compare(0, 5, L"Style") == 0) {
tokenLineNum = lineNum;
HandleToken(token, line, tokenLineNum, rules, styles, lines);
token = ConfigToken::STYLE;
line = new std::wstring(s);
}
else if (s.compare(0, 4, L"Hide") == 0) {
ParseRule(r, rules, conditionMap, actionMap, lines);
r = new Rule(lineNum, s);
r->AddAction(new HideAction());
else if (s.compare(0, 4, L"Show") == 0) {
tokenLineNum = lineNum;
HandleToken(token, line, tokenLineNum, rules, styles, lines);
token = ConfigToken::SHOW;
}
else if (r) {
lines.push_back({ lineNum, s });
else if (s.compare(0, 4, L"Hide") == 0) {
tokenLineNum = lineNum;
HandleToken(token, line, tokenLineNum, rules, styles, lines);
token = ConfigToken::HIDE;
}
else {
DEBUG_LOG(L"Invalid Line: {}, expected...\n", lineNum);
lines.push_back({ lineNum, s });
}
}
ParseRule(r, rules, conditionMap, actionMap, lines);
return rules;
HandleToken(token, line, tokenLineNum, rules, styles, lines);
}
20 changes: 15 additions & 5 deletions Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ struct Line {
std::wstring sText;
};

enum class ConfigToken : uint8_t {
NONE,
STYLE,
SHOW,
HIDE
};

class Config {
private:
std::wstring m_Path;

void SetupValidConditions(ConditionMap& validConditions);
void SetupValidActions(ActionMap& validActions);
void ParseRule(Rule* rule, std::vector<Rule*>& rules, ConditionMap validConditions, ActionMap validActions, std::vector<Line>& lines);
ConditionMap m_ValidConditions;
ActionMap m_ValidActions;

void SetupValidConditions();
void SetupValidActions();
void HandleToken(ConfigToken token, std::wstring* line, uint32_t lineNum, std::vector<Rule*>& rules, std::map<std::wstring, std::vector<Action*>>& styles, std::vector<Line>& lines);
void ParseStyle(std::wstring* line, std::map<std::wstring, std::vector<Action*>>& styles, std::vector<Line>& lines);
void ParseRule(Rule* rule, std::vector<Rule*>& rules, std::vector<Line>& lines);
public:
Config(std::wstring path);
std::vector<Rule*> ParseFilter();
void ParseFilter(std::vector<Rule*>& rules, std::map<std::wstring, std::vector<Action*>>& styles);
};

2 changes: 2 additions & 0 deletions Globals.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "Globals.h"
#include <fmt/format.h>
#include <fmt/core.h>
#include "Action.h"

GameVersion GAME_VERSION = GameVersion::NONE;

bool TXT_DATA_LOADED = false;

std::map<std::wstring, std::vector<Action*>> STYLES;
std::map<std::wstring, uint32_t> ITEM_NAME_LOOKUP_TABLE;
std::map<std::wstring, uint32_t> ITEM_CODE_LOOKUP_TABLE;
std::map<std::wstring, uint32_t> RUNE_NAME_LOOKUP_TABLE;
Expand Down
3 changes: 3 additions & 0 deletions Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#include <Windows.h>
#include <map>
#include <string>
#include "Action.h"
#include "D2Constants.h"

extern GameVersion GAME_VERSION;

extern bool TXT_DATA_LOADED;
extern std::map<std::wstring, std::vector<Action*>> STYLES;

extern std::map<std::wstring, uint16_t> ITEM_TYPE_LOOKUP_TABLE;
extern std::map<std::wstring, uint32_t> ITEM_NAME_LOOKUP_TABLE;
extern std::map<std::wstring, uint32_t> ITEM_CODE_LOOKUP_TABLE;
Expand Down
6 changes: 5 additions & 1 deletion ItemFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ bool ItemFilter::IS_DEBUG_MODE = false;
Config* ItemFilter::Configuration = NULL;

ItemFilter::ItemFilter() {

LoadFilter();


DEBUG_LOG(L"D2COMMON_UNITS_FreeUnit: {}\n", fmt::ptr(&D2COMMON_UNITS_FreeUnit));
//alot of 114d functions need diff stubs or they changed from __stdcall to __fastcall
if (GetGameVersion() == GameVersion::V114d) {
Expand Down Expand Up @@ -109,7 +111,9 @@ ItemFilter::ItemFilter() {

void ItemFilter::LoadFilter() {
Configuration = new Config(L"./item.filter");
RULES = Configuration->ParseFilter();
RULES.empty();
STYLES.empty();
Configuration->ParseFilter(RULES, STYLES);
ITEM_ACTIONS.empty();

DEBUG_LOG(L"Filter loaded\n");
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ d2lootfilter is a plugin that can be used with [PlugY](http://plugy.free.fr/en/i
* [Conditions](#Conditions)
* [Stats](#Stats)
* [Actions](#Actions)
* [Styles](#Styles)
* [Credits](#Credits)

**This has not been heavily tested, crashes may happen, and items may be lost**
Expand Down Expand Up @@ -131,6 +132,7 @@ e.x. `Max(Stat(39), Stat(43), Stat(41), Stat(45)) > 0` can be used to filter the

| Name | Valid Values |
|-|-|
| SetStyle `<Value>` | Sets the styling for an item. A style is a group of actions that will be applied. See [Styles](#Styles). |
| SetName `<Value>` | Sets the name for an item. Special token `{Name}` is the base name for the item. When using continue it will append from the previous condition block. |
| SetDescription `<Value>` | Sets the description for an item. Special token `{Description}` is the base name for the item. When using continue it will append from the previous condition block. |
| SetBackgroundColor `<Value>` | Sets the background color of the item when on the ground. Pallette index color or White, Red, Green, Blue, Gold, Gray, Black, Tan, Orange, Yellow, Purple, Dark Green |
Expand All @@ -139,6 +141,30 @@ e.x. `Max(Stat(39), Stat(43), Stat(41), Stat(45)) > 0` can be used to filter the
| ChatNotify `<Boolean>` | Notify when the item drops in chat. True or False |
| MinimapIcon `<Value>` | Sets the color of the item on your minimap when on the ground. Value is a pallette index color. |

### Styles

Lets you apply a group of actions to an item. i.e.

```
Style Tier 1 Items
SetName {Purple}T1 {Name}
ChatNotify True
MinimapIcon Purple
SetInventoryColor Purple
Show
Type Diadem
Rarity Unique
SetStyle Tier 1 Items
Show
Type Unearthed Wand
Rarity Unique
SetStyle Tier 1 Items
```

will apply all of the `Tier 1 Items` styles to the items.

### Credits

Special thanks to everyone that has shared their work at [Phrozen-Keep](https://d2mods.info/forum/viewforum.php?f=8) [(Discord)](https://discord.gg/NvfftHY).
Expand Down
3 changes: 1 addition & 2 deletions Rule.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "Rule.h"


Rule::Rule(uint32_t lineNum, std::wstring text) : m_LineNum(lineNum),
m_Text(text) {
Rule::Rule(uint32_t lineNum) : m_LineNum(lineNum) {
}

bool Rule::Evaluate(Unit* pItem) {
Expand Down
3 changes: 1 addition & 2 deletions Rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ class Rule {
private:
uint32_t m_LineNum;
bool m_IsContinue = false;
std::wstring m_Text;
std::vector<Condition*> m_Conditions;
std::vector<Action*> m_Actions;
public:
Rule(uint32_t lineNum, std::wstring text);
Rule(uint32_t lineNum);
bool Evaluate(Unit* pItem);
void EvaluateActionResult(ItemActionResult* action, Unit* pItem);
void SetIsContinue(bool isContinue) { m_IsContinue = isContinue; };
Expand Down
4 changes: 2 additions & 2 deletions Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include <fmt/format.h>
#include <fmt/core.h>
#ifdef _DEBUG
#define DEBUG_LOG(fmt, ...) fmt::print(fmt, __VA_ARGS___);
#define DEBUG_LOG(f, ...) fmt::print(f, __VA_ARGS__);
#else
#define DEBUG_LOG(fmt, ...) ;
#define DEBUG_LOG(f, ...) ;
#endif


Expand Down
4 changes: 2 additions & 2 deletions d2lootfilter.vcxproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommand>$(D2Path)\Game.exe</LocalDebuggerCommand>
<LocalDebuggerCommandArguments>-w -3dfx</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>-w</LocalDebuggerCommandArguments>
<LocalDebuggerWorkingDirectory>$(D2Path)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerCommand>$(D2Path)\Game.exe</LocalDebuggerCommand>
<LocalDebuggerCommandArguments>-w -3dfx</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>-w</LocalDebuggerCommandArguments>
<LocalDebuggerWorkingDirectory>$(D2Path)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
Expand Down
Loading

0 comments on commit 9ca79d4

Please sign in to comment.