Skip to content

Commit

Permalink
Backport compatible changes from 109d branch
Browse files Browse the repository at this point in the history
  • Loading branch information
swine-flu committed Jun 8, 2024
1 parent 16a6b16 commit 2960007
Show file tree
Hide file tree
Showing 20 changed files with 1,110 additions and 1,018 deletions.
108 changes: 54 additions & 54 deletions Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "D2Ptrs.h"
#include "ItemFilter.h"

typedef std::wstring(*TokenReplaceFunction)(ActionResult* action, Unit* pItem);
typedef std::wstring(*TokenReplaceFunction)(ActionResult& action, Unit* pItem);

#define COLOR(STR, IDX) { L"{"#STR"}", ##IDX## }
static std::unordered_map<std::wstring, std::wstring> COLOR_TO_STRING = {
static const utility::string_umap<std::wstring, std::wstring> COLOR_TO_STRING = {
COLOR(White, TEXT_WHITE),
COLOR(Red, TEXT_RED),
COLOR(Green, TEXT_GREEN),
Expand All @@ -32,7 +32,7 @@ static std::unordered_map<std::wstring, std::wstring> COLOR_TO_STRING = {
#undef COLOR

#define COLOR(STR, IDX) { L#STR, ##IDX## }, { L"\""#STR"\"", ##IDX## }
static std::unordered_map<std::wstring, uint8_t> COLOR_TO_PALETTE_IDX = {
static const utility::string_umap<std::wstring, uint8_t> COLOR_TO_PALETTE_IDX = {
COLOR(White, 0x20),
COLOR(White, 0x20),
COLOR(Red, 0x0A),
Expand All @@ -55,23 +55,23 @@ static std::unordered_map<std::wstring, uint8_t> COLOR_TO_PALETTE_IDX = {
};
#undef COLOR

static std::wstring GetItemName(ActionResult* action, Unit* pItem) {
if (action->bItemNameSet) {
return action->wsItemName;
static std::wstring GetItemName(ActionResult& action, Unit* pItem) {
if (action.bItemNameSet) {
return action.wsItemName;
} else {
return L"{Name}";
}
}

static std::wstring GetItemDesc(ActionResult* action, Unit* pItem) {
if (action->bItemDescSet) {
return action->wsItemDesc;
static std::wstring GetItemDesc(ActionResult& action, Unit* pItem) {
if (action.bItemDescSet) {
return action.wsItemDesc;
} else {
return L"{Description}";
}
}

static std::wstring GetPotionNumber(ActionResult* action, Unit* pItem) {
static std::wstring GetPotionNumber(ActionResult& action, Unit* pItem) {
if(D2COMMON_ITEMS_CheckItemTypeId(pItem, ItemType::HEALING_POTION)
|| D2COMMON_ITEMS_CheckItemTypeId(pItem, ItemType::MANA_POTION)) {
return GetItemCode(pItem).substr(2);
Expand All @@ -86,18 +86,18 @@ static std::wstring GetPotionNumber(ActionResult* action, Unit* pItem) {
return L"";
}

static std::wstring GetRuneNumber(ActionResult* action, Unit* pItem) {
static std::wstring GetRuneNumber(ActionResult& action, Unit* pItem) {
if (D2COMMON_ITEMS_CheckItemTypeId(pItem, ItemType::RUNE)) {
return std::to_wstring(_wtoi(GetItemCode(pItem).substr(1).c_str()));
return std::to_wstring(std::stoi(GetItemCode(pItem).substr(1)));
}
return L"";
}

static std::wstring Newline(ActionResult* action, Unit* pItem) {
static std::wstring Newline(ActionResult& action, Unit* pItem) {
return L"\n";
}

static std::wstring GetItemPrice(ActionResult* action, Unit* pItem) {
static std::wstring GetItemPrice(ActionResult& action, Unit* pItem) {
int nPrice = 0;
Unit* pPlayer = D2CLIENT_GetPlayerUnit();
if (pItem != NULL && pPlayer != NULL) {
Expand All @@ -106,49 +106,49 @@ static std::wstring GetItemPrice(ActionResult* action, Unit* pItem) {
return std::to_wstring(nPrice);
}

static std::wstring GetItemSockets(ActionResult* action, Unit* pItem) {
static std::wstring GetItemSockets(ActionResult& action, Unit* pItem) {
return std::to_wstring(D2COMMON_STATLIST_GetUnitStatUnsigned(pItem, Stat::ITEM_NUMSOCKETS, 0));
}


ColorTextAction::ColorTextAction(std::wstring value, ActionType type) : Action(value, type) {
ColorTextAction::ColorTextAction(std::wstring_view value, ActionType type) : Action(value, type) {
for (auto const& color : COLOR_TO_STRING) {
replace(m_Value, color.first, color.second);
}
}

PaletteIndexAction::PaletteIndexAction(std::wstring value, ActionType type) : Action(value, type) {
if (COLOR_TO_PALETTE_IDX.contains(value)) {
m_PaletteIndex = COLOR_TO_PALETTE_IDX[value];
PaletteIndexAction::PaletteIndexAction(std::wstring_view value, ActionType type) : Action(value, type) {
if (auto search = COLOR_TO_PALETTE_IDX.find(m_Value); search != COLOR_TO_PALETTE_IDX.end()) {
m_PaletteIndex = search->second;
}
else {
m_PaletteIndex = static_cast<uint8_t>(std::stoi(value, nullptr, 16));
m_PaletteIndex = static_cast<uint8_t>(std::stoi(m_Value, nullptr, 16));
}
}


void HideAction::SetResult(ActionResult* action, Unit* pItem) {
action->bHide = true;
void HideAction::SetResult(ActionResult& action, Unit* pItem) const {
action.bHide = true;
}

void ShowAction::SetResult(ActionResult* action, Unit* pItem) {
action->bHide = false;
void ShowAction::SetResult(ActionResult& action, Unit* pItem) const {
action.bHide = false;
}

void ContinueAction::SetResult(ActionResult* action, Unit* pItem) {
action->bContinue = true;
void ContinueAction::SetResult(ActionResult& action, Unit* pItem) const {
action.bContinue = true;
}

void SetStyleAction::SetResult(ActionResult* action, Unit* pItem) {
if (GlobalStyles.contains(m_Value)) {
for (auto act : GlobalStyles[m_Value]) {
void SetStyleAction::SetResult(ActionResult& action, Unit* pItem) const {
if (auto search = GlobalStyles.find(m_Value); search != GlobalStyles.end()) {
for (const auto& act : search->second) {
act->SetResult(action, pItem);
}
}
}

void SetNameAction::SetResult(ActionResult* action, Unit* pItem) {
std::unordered_map<std::wstring, TokenReplaceFunction> TOKEN_REPLACEMENT_FUNCTIONS = {
void SetNameAction::SetResult(ActionResult& action, Unit* pItem) const {
static const utility::string_umap<std::wstring, TokenReplaceFunction> TOKEN_REPLACEMENT_FUNCTIONS = {
{ L"{Name}", &GetItemName },
{ L"{Sockets}", &GetItemSockets },
{ L"{Price}", &GetItemPrice },
Expand All @@ -161,12 +161,12 @@ void SetNameAction::SetResult(ActionResult* action, Unit* pItem) {
for (const auto& token : TOKEN_REPLACEMENT_FUNCTIONS) {
replace(result, token.first, token.second(action, pItem));
}
action->wsItemName = result;
action->bItemNameSet = true;
action.wsItemName = result;
action.bItemNameSet = true;
}

void SetDescriptionAction::SetResult(ActionResult* action, Unit* pItem) {
std::unordered_map<std::wstring, TokenReplaceFunction> TOKEN_REPLACEMENT_FUNCTIONS = {
void SetDescriptionAction::SetResult(ActionResult& action, Unit* pItem) const {
static const utility::string_umap<std::wstring, TokenReplaceFunction> TOKEN_REPLACEMENT_FUNCTIONS = {
{ L"{Description}", &GetItemDesc },
{ L"{Sockets}", &GetItemSockets },
{ L"{Price}", &GetItemPrice },
Expand All @@ -178,37 +178,37 @@ void SetDescriptionAction::SetResult(ActionResult* action, Unit* pItem) {
for (const auto& token : TOKEN_REPLACEMENT_FUNCTIONS) {
replace(result, token.first, token.second(action, pItem));
}
action->wsItemDesc = result;
action->bItemDescSet = true;
action.wsItemDesc = result;
action.bItemDescSet = true;
}

void SetBackgroundColorAction::SetResult(ActionResult* action, Unit* pItem) {
action->bBackgroundPaletteIndexSet = true;
action->nBackgroundPaletteIndex = m_PaletteIndex;
void SetBackgroundColorAction::SetResult(ActionResult& action, Unit* pItem) const {
action.bBackgroundPaletteIndexSet = true;
action.nBackgroundPaletteIndex = m_PaletteIndex;
}

void SetInventoryColorAction::SetResult(ActionResult* action, Unit* pItem) {
action->bInvBackgroundPaletteIndexSet = true;
action->nInvBackgroundPaletteIndex = m_PaletteIndex;
void SetInventoryColorAction::SetResult(ActionResult& action, Unit* pItem) const {
action.bInvBackgroundPaletteIndexSet = true;
action.nInvBackgroundPaletteIndex = m_PaletteIndex;
}

void SetBorderColorAction::SetResult(ActionResult* action, Unit* pItem) {
action->bBorderPaletteIndexSet = true;
action->nBorderPaletteIndex = m_PaletteIndex;
void SetBorderColorAction::SetResult(ActionResult& action, Unit* pItem) const {
action.bBorderPaletteIndexSet = true;
action.nBorderPaletteIndex = m_PaletteIndex;
}

ChatNotifyAction::ChatNotifyAction(std::wstring value) : Action(value, ActionType::CHAT_NOTIFY) {
m_Expression = Parser::Parse(value.c_str());
ChatNotifyAction::ChatNotifyAction(std::wstring_view value) : Action(value, ActionType::CHAT_NOTIFY) {
m_Expression = Parser::Parse(m_Value.c_str());
}

void ChatNotifyAction::SetResult(ActionResult* action, Unit* pItem) {
action->bChatAlert = m_Expression->Evaluate(pItem) != 0;
void ChatNotifyAction::SetResult(ActionResult& action, Unit* pItem) const {
action.bChatAlert = m_Expression->Evaluate(pItem) != 0;
}

void PlayAlertAction::SetResult(ActionResult* action, Unit* pItem) {
void PlayAlertAction::SetResult(ActionResult& action, Unit* pItem) const {
}

void MinimapIconAction::SetResult(ActionResult* action, Unit* pItem) {
action->bMinimapIcon = true;
action->nMinimapIconPaletteIndex = m_PaletteIndex;
void MinimapIconAction::SetResult(ActionResult& action, Unit* pItem) const {
action.bMinimapIcon = true;
action.nMinimapIconPaletteIndex = m_PaletteIndex;
}
112 changes: 80 additions & 32 deletions Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,95 +48,143 @@ class Action {
ActionType m_Type;
std::wstring m_Value;
public:
Action(std::wstring value = L"", ActionType type = ActionType::NONE) : m_Value(value), m_Type(type) {};
ActionType GetType() { return m_Type; }
virtual void SetResult(ActionResult* pResult, Unit* pItem) = 0;
Action(std::wstring_view value = {}, ActionType type = ActionType::NONE) : m_Value(value), m_Type(type) {};
virtual ~Action() = default;
ActionType GetType() const { return m_Type; }
virtual void SetResult(ActionResult& action, Unit* pItem) const = 0;
};

class ShowAction : public Action {
public:
ShowAction(std::wstring value = L"") : Action(value, ActionType::SHOW) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
ShowAction(std::wstring_view value = {}) : Action(value, ActionType::SHOW) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<ShowAction>(value); }
};

class HideAction : public Action {
public:
HideAction(std::wstring value = L"") : Action(value, ActionType::HIDE) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
HideAction(std::wstring_view value = {}) : Action(value, ActionType::HIDE) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<HideAction>(value); }
};

class ContinueAction : public Action {
public:
ContinueAction(std::wstring value = L"") : Action(value, ActionType::CONTINUE) { };
void SetResult(ActionResult* pResult, Unit* pItem) override;
ContinueAction(std::wstring_view value = {}) : Action(value, ActionType::CONTINUE) { };
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<ContinueAction>(value); }
};

class ColorTextAction : public Action {
public:
ColorTextAction(std::wstring value = L"", ActionType type = ActionType::NONE);
virtual void SetResult(ActionResult* pResult, Unit* pItem) = 0;
ColorTextAction(std::wstring_view value = {}, ActionType type = ActionType::NONE);
virtual void SetResult(ActionResult& action, Unit* pItem) const = 0;
};

class PaletteIndexAction : public Action {
protected:
uint8_t m_PaletteIndex = 0;
public:
PaletteIndexAction(std::wstring value = L"", ActionType type = ActionType::NONE);
virtual void SetResult(ActionResult* pResult, Unit* pItem) = 0;
PaletteIndexAction(std::wstring_view value = {}, ActionType type = ActionType::NONE);
virtual void SetResult(ActionResult& action, Unit* pItem) const = 0;
};

class SetStyleAction : public Action {
public:
SetStyleAction(std::wstring value = L"") : Action(value, ActionType::SET_STYLE) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
SetStyleAction(std::wstring_view value = {}) : Action(value, ActionType::SET_STYLE) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<SetStyleAction>(value); }
};

class SetNameAction : public ColorTextAction {
public:
SetNameAction(std::wstring value = L"") : ColorTextAction(value, ActionType::SET_NAME) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
SetNameAction(std::wstring_view value = {}) : ColorTextAction(value, ActionType::SET_NAME) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<SetNameAction>(value); }
};

class SetDescriptionAction : public ColorTextAction {
public:
SetDescriptionAction(std::wstring value = L"") : ColorTextAction(value, ActionType::SET_DESCRIPTION) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
SetDescriptionAction(std::wstring_view value = {}) : ColorTextAction(value, ActionType::SET_DESCRIPTION) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<SetDescriptionAction>(value); }
};

class SetBackgroundColorAction : public PaletteIndexAction {
public:
SetBackgroundColorAction(std::wstring value = L"") : PaletteIndexAction(value, ActionType::SET_BORDER_COLOR) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
SetBackgroundColorAction(std::wstring_view value = {}) : PaletteIndexAction(value, ActionType::SET_BORDER_COLOR) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<SetBackgroundColorAction>(value); }
};

class SetInventoryColorAction : public PaletteIndexAction {
public:
SetInventoryColorAction(std::wstring value = L"") : PaletteIndexAction(value, ActionType::SET_INVENTORY_COLOR) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
SetInventoryColorAction(std::wstring_view value = {}) : PaletteIndexAction(value, ActionType::SET_INVENTORY_COLOR) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<SetInventoryColorAction>(value); }
};

class SetBorderColorAction : public PaletteIndexAction {
public:
SetBorderColorAction(std::wstring value = L"") : PaletteIndexAction(value, ActionType::SET_BORDER_COLOR) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
SetBorderColorAction(std::wstring_view value = {}) : PaletteIndexAction(value, ActionType::SET_BORDER_COLOR) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<SetBorderColorAction>(value); }
};

class ChatNotifyAction : public Action {
protected:
Expression* m_Expression;
std::unique_ptr<Expression> m_Expression;
public:
ChatNotifyAction(std::wstring value = L"");
void SetResult(ActionResult* pResult, Unit* pItem) override;
ChatNotifyAction(std::wstring_view value = {});
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<ChatNotifyAction>(value); }
};

class PlayAlertAction : public Action {
public:
PlayAlertAction(std::wstring value = L"") : Action(value, ActionType::PLAY_ALERT) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
PlayAlertAction(std::wstring_view value = {}) : Action(value, ActionType::PLAY_ALERT) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<PlayAlertAction>(value); }
};

class MinimapIconAction : public PaletteIndexAction {
public:
MinimapIconAction(std::wstring value = L"") : PaletteIndexAction(value, ActionType::MINIMAP_ICON) {};
void SetResult(ActionResult* pResult, Unit* pItem) override;
MinimapIconAction(std::wstring_view value = {}) : PaletteIndexAction(value, ActionType::MINIMAP_ICON) {};
void SetResult(ActionResult& action, Unit* pItem) const override;

static std::unique_ptr<Action> MakeInstance(std::wstring_view value = {}) { return std::make_unique<MinimapIconAction>(value); }
};

class ActionFactory {
public:
static std::unique_ptr<Action> MakeInstance(std::wstring_view action, std::wstring_view value = {}) {
static const utility::string_umap_icase<std::wstring, std::unique_ptr<Action>(&)(std::wstring_view)> lookup = {
{ L"Continue", ContinueAction::MakeInstance },
{ L"SetStyle", SetStyleAction::MakeInstance },
{ L"SetName", SetNameAction::MakeInstance },
{ L"SetDescription", SetDescriptionAction::MakeInstance },
{ L"SetBackgroundColor", SetBackgroundColorAction::MakeInstance },
{ L"SetInventoryColor", SetInventoryColorAction::MakeInstance },
{ L"SetBorderColor", SetBorderColorAction::MakeInstance },
{ L"ChatNotify", ChatNotifyAction::MakeInstance },
{ L"PlayAlert", PlayAlertAction::MakeInstance },
{ L"MinimapIcon", MinimapIconAction::MakeInstance },
};

if (auto search = lookup.find(action); search != lookup.end()) {
return search->second(value);
}
return nullptr;
}
};
Loading

0 comments on commit 2960007

Please sign in to comment.