feat: add base item

This commit is contained in:
k0t9i
2023-01-24 02:49:35 +04:00
parent 6ca8752108
commit 43f38c7a57
20 changed files with 461 additions and 33 deletions

View File

@@ -6,11 +6,13 @@
#include "Factories/NPCFactory.h"
#include "Factories/PlayerFactory.h"
#include "Factories/SkillFactory.h"
#include "Factories/ItemFactory.h"
#include "Repositories/DropRepository.h"
#include "Repositories/HeroRepository.h"
#include "Repositories/NPCRepository.h"
#include "Repositories/PlayerRepository.h"
#include "Repositories/SkillRepository.h"
#include "Repositories/ItemRepository.h"
#include "GameStructs/NetworkHandlerWrapper.h"
#include "GameStructs/GameEngineWrapper.h"
#include "GameStructs/L2GameDataWrapper.h"
@@ -88,6 +90,17 @@ namespace Interlude
);
return result;
}
ItemRepository& GetItemRepository() const override
{
static auto factory = ItemFactory(GetL2GameData(), GetFName());
static EntityHandler handler;
static auto result = ItemRepository(
GetNetworkHandler(),
factory,
handler
);
return result;
}
NetworkHandlerWrapper& GetNetworkHandler() const override
{
static NetworkHandlerWrapper result;

View File

@@ -0,0 +1,53 @@
#pragma once
#include <memory>
#include <map>
#include <chrono>
#include "../GameStructs/L2GameDataWrapper.h"
#include "../GameStructs/FName.h"
#include "../../../Common/Common.h"
#include "Domain/Entities/BaseItem.h"
#include "../../../DTO/ItemData.h"
using namespace L2Bot::Domain;
namespace Interlude
{
class ItemFactory
{
public:
ItemFactory(const L2GameDataWrapper& l2GameData, const FName& fName) :
m_L2GameData(l2GameData),
m_FName(fName)
{
}
ItemFactory() = delete;
virtual ~ItemFactory() = default;
std::unique_ptr<Entities::BaseItem> Create(const ItemData& itemInfo) const
{
const auto data = m_L2GameData.GetItemData(itemInfo.itemId);
const auto nameEntry = data ? m_FName.GetEntry(data->nameIndex) : nullptr;
const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr;
const auto description = data && data->description ? data->description : L"";
return std::make_unique<Entities::BaseItem>(
itemInfo.itemId,
itemInfo.amount,
itemInfo.isEquipped > 0,
itemInfo.enchantLevel,
itemInfo.mana,
nameEntry ? ConvertFromWideChar(nameEntry->value) : "",
iconEntry ? ConvertFromWideChar(iconEntry->value) : "",
ConvertFromWideChar(description),
data ? data->weight : 0
);
}
private:
const L2GameDataWrapper& m_L2GameData;
const FName& m_FName;
};
}

View File

@@ -30,8 +30,8 @@ namespace Interlude
const auto cost = data ? data->mpCost : 0;
const auto range = data ? data->range : 0;
const auto name = data ? data->name : L"";
const auto description = data ? data->description : L"";
const auto name = data && data->name ? data->name : L"";
const auto description = data && data->description ? data->description : L"";
const auto iconEntry = data ? m_FName.GetEntry(data->iconNameIndex) : nullptr;
return std::make_unique<Entities::Skill>(

View File

@@ -7,6 +7,10 @@
#include "../../../Events/SkillCancelledEvent.h"
#include "../../../Events/AbnormalEffectChangedEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Events/ItemCreatedEvent.h"
#include "../../../Events/ItemUpdatedEvent.h"
#include "../../../Events/ItemDeletedEvent.h"
#include "../../../DTO/ItemData.h"
namespace Interlude
{
@@ -18,6 +22,8 @@ namespace Interlude
int(__thiscall* GameEngineWrapper::__OnReceiveMagicSkillUse)(GameEngine*, User*, User*, L2ParamStack&) = 0;
void(__thiscall* GameEngineWrapper::__OnReceiveMagicSkillCanceled)(GameEngine*, User*) = 0;
void(__thiscall* GameEngineWrapper::__AddAbnormalStatus)(GameEngine*, L2ParamStack&) = 0;
void(__thiscall* GameEngineWrapper::__AddInventoryItem)(GameEngine*, ItemInfo&) = 0;
void(__thiscall* GameEngineWrapper::__OnReceiveUpdateItemList)(GameEngine*, UpdateItemListActionType, ItemInfo&) = 0;
void GameEngineWrapper::Init(HMODULE hModule)
@@ -38,6 +44,12 @@ namespace Interlude
(FARPROC&)__AddAbnormalStatus = (FARPROC)splice(
GetProcAddress(hModule, "?AddAbnormalStatus@UGameEngine@@UAEXAAVL2ParamStack@@@Z"), __AddAbnormalStatus_hook
);
(FARPROC&)__AddInventoryItem = (FARPROC)splice(
GetProcAddress(hModule, "?AddInventoryItem@UGameEngine@@UAEXAAUItemInfo@@@Z"), __AddInventoryItem_hook
);
(FARPROC&)__OnReceiveUpdateItemList = (FARPROC)splice(
GetProcAddress(hModule, "?OnReceiveUpdateItemList@UGameEngine@@UAEXHAAUItemInfo@@@Z"), __OnReceiveUpdateItemList_hook
);
}
void GameEngineWrapper::Restore()
@@ -47,6 +59,8 @@ namespace Interlude
restore((void*&)__OnReceiveMagicSkillUse);
restore((void*&)__OnReceiveMagicSkillCanceled);
restore((void*&)__AddAbnormalStatus);
restore((void*&)__AddInventoryItem);
restore((void*&)__OnReceiveUpdateItemList);
}
void __fastcall GameEngineWrapper::__Init_hook(GameEngine* This, uint32_t /*edx*/, float_t unk)
@@ -85,4 +99,48 @@ namespace Interlude
EventDispatcher::GetInstance().Dispatch(AbnormalEffectChangedEvent{ stack.GetBufferAsVector<int32_t>(3) });
(*__AddAbnormalStatus)(This, stack);
}
void __fastcall GameEngineWrapper::__AddInventoryItem_hook(GameEngine* This, int, ItemInfo& itemInfo)
{
EventDispatcher::GetInstance().Dispatch(
ItemCreatedEvent
{
ItemData
{
itemInfo.itemId,
itemInfo.amount,
itemInfo.isEquipped,
itemInfo.enchantLevel,
itemInfo.mana,
}
}
);
(*__AddInventoryItem)(This, itemInfo);
}
void __fastcall GameEngineWrapper::__OnReceiveUpdateItemList_hook(GameEngine* This, int, UpdateItemListActionType actionType, ItemInfo& itemInfo)
{
const ItemData itemData
{
itemInfo.itemId,
itemInfo.amount,
itemInfo.isEquipped,
itemInfo.enchantLevel,
itemInfo.mana,
};
switch (actionType)
{
case UpdateItemListActionType::created:
EventDispatcher::GetInstance().Dispatch(ItemCreatedEvent{ itemData });
break;
case UpdateItemListActionType::updated:
EventDispatcher::GetInstance().Dispatch(ItemUpdatedEvent{ itemData });
break;
case UpdateItemListActionType::deleted:
EventDispatcher::GetInstance().Dispatch(ItemDeletedEvent{ itemInfo.itemId });
break;
}
(*__OnReceiveUpdateItemList)(This, actionType, itemInfo);
}
}

View File

@@ -24,12 +24,17 @@ namespace Interlude
static int(__thiscall* __OnReceiveMagicSkillUse)(GameEngine*, User*, User*, L2ParamStack&);
static void(__thiscall* __OnReceiveMagicSkillCanceled)(GameEngine*, User*);
static void(__thiscall* __AddAbnormalStatus)(GameEngine*, L2ParamStack&);
static void(__thiscall* __AddInventoryItem)(GameEngine*, ItemInfo&);
static void(__thiscall* __OnReceiveUpdateItemList)(GameEngine*, UpdateItemListActionType, ItemInfo&);
static void __fastcall __Init_hook(GameEngine* This, uint32_t /*edx*/, float_t unk);
static void __fastcall __OnSkillListPacket_hook(GameEngine* This, uint32_t /*edx*/, L2ParamStack& stack);
static int __fastcall __OnReceiveMagicSkillUse_hook(GameEngine* This, uint32_t /*edx*/, User* u1, User* u2, L2ParamStack& stack);
static void __fastcall __OnReceiveMagicSkillCanceled_hook(GameEngine* This, uint32_t /*edx*/, User* user);
static void __fastcall __AddAbnormalStatus_hook(GameEngine* This, uint32_t /*edx*/, L2ParamStack& stack);
static void __fastcall __AddInventoryItem_hook(GameEngine* This, int /*edx*/, ItemInfo& itemInfo);
static void __fastcall __OnReceiveUpdateItemList_hook(GameEngine* This, int /*edx*/, UpdateItemListActionType actionType, ItemInfo& itemInfo);
private:
static void* originalInitAddress;
static GameEngine* _target;

View File

@@ -255,4 +255,11 @@ namespace Interlude
int32_t iconNameIndex; //0x0048
char pad_004C[52]; //0x004C
}; //Size: 0x0080
enum class UpdateItemListActionType : uint32_t
{
created = 1,
updated,
deleted
};
};

View File

@@ -0,0 +1,134 @@
#pragma once
#include <map>
#include <chrono>
#include <shared_mutex>
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/ItemFactory.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Services/EntityHandler.h"
#include "../../../Events/ItemCreatedEvent.h"
#include "../../../Events/ItemUpdatedEvent.h"
#include "../../../Events/ItemDeletedEvent.h"
#include "../../../Events/EventDispatcher.h"
using namespace L2Bot::Domain;
namespace Interlude
{
class ItemRepository : public Repositories::EntityRepositoryInterface
{
public:
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
{
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
std::map<uint32_t, Entities::BaseItem*> itemPtrs;
for (const auto& kvp : m_Items)
{
itemPtrs[kvp.first] = kvp.second.get();
}
const auto objects = m_EntityHandler.GetEntities<Entities::BaseItem*>(itemPtrs, [this](Entities::BaseItem* item) {
return std::make_unique<Entities::BaseItem>(item);
});
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
for (const auto kvp : objects)
{
result.push_back(kvp.second);
}
return result;
}
ItemRepository(const NetworkHandlerWrapper& networkHandler, const ItemFactory& factory, EntityHandler& handler) :
m_NetworkHandler(networkHandler),
m_Factory(factory),
m_EntityHandler(handler)
{
EventDispatcher::GetInstance().Subscribe(ItemCreatedEvent::name, [this](const Event& evt) {
OnItemCreated(evt);
});
EventDispatcher::GetInstance().Subscribe(ItemUpdatedEvent::name, [this](const Event& evt) {
OnItemUpdated(evt);
});
EventDispatcher::GetInstance().Subscribe(ItemDeletedEvent::name, [this](const Event& evt) {
OnItemDeleted(evt);
});
}
void OnItemCreated(const Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == ItemCreatedEvent::name)
{
const auto casted = static_cast<const ItemCreatedEvent&>(evt);
const auto& data = casted.GetItemData();
auto item = m_Factory.Create(data);
if (m_Items.find(data.itemId) == m_Items.end())
{
m_Items.emplace(data.itemId, std::move(item));
}
else
{
// When equip/unequip accessories
m_Items[data.itemId]->Update(item.get());
}
}
}
void OnItemUpdated(const Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == ItemUpdatedEvent::name)
{
const auto casted = static_cast<const ItemUpdatedEvent&>(evt);
const auto& data = casted.GetItemData();
//todo exception?
if (m_Items.find(data.itemId) == m_Items.end())
{
return;
}
auto item = m_Factory.Create(data);
m_Items[data.itemId]->Update(item.get());
}
}
void OnItemDeleted(const Event& evt)
{
//fixme may be a race condition
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
if (evt.GetName() == ItemDeletedEvent::name)
{
const auto casted = static_cast<const ItemDeletedEvent&>(evt);
m_Items.erase(casted.GetItemId());
}
}
ItemRepository() = delete;
virtual ~ItemRepository()
{
Reset();
}
void Reset() override
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
m_Items.clear();
}
private:
const ItemFactory& m_Factory;
std::map<uint32_t, std::unique_ptr<Entities::BaseItem>> m_Items;
uint32_t m_UsedSkillId = 0;
const NetworkHandlerWrapper& m_NetworkHandler;
std::shared_timed_mutex m_Mutex;
EntityHandler& m_EntityHandler;
};
}

View File

@@ -21,6 +21,7 @@ public:
virtual Repositories::EntityRepositoryInterface& GetNPCRepository() const = 0;
virtual Repositories::EntityRepositoryInterface& GetPlayerRepository() const = 0;
virtual Repositories::EntityRepositoryInterface& GetSkillRepository() const = 0;
virtual Repositories::EntityRepositoryInterface& GetItemRepository() const = 0;
virtual NetworkHandlerInterface& GetNetworkHandler() const = 0;
virtual GameEngineInterface& GetGameEngine() const = 0;
virtual L2GameDataInterface& GetL2GameData() const = 0;