mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-11-09 17:58:20 +00:00
216 lines
4.9 KiB
C++
216 lines
4.9 KiB
C++
#include "NejlikaData.h"
|
|
|
|
#include "Game.h"
|
|
#include "dConfig.h"
|
|
#include "json.hpp"
|
|
#include "Logger.h"
|
|
#include "Lookup.h"
|
|
|
|
#include <fstream>
|
|
|
|
using namespace nejlika;
|
|
|
|
namespace
|
|
{
|
|
|
|
std::unordered_map<nejlika::ModifierNameType, std::vector<nejlika::ModifierNameTemplate>> modifierNameTemplates;
|
|
|
|
std::unordered_map<LWOOBJID, nejlika::AdditionalItemData> additionalItemData;
|
|
|
|
std::unordered_map<LWOOBJID, nejlika::AdditionalEntityData> additionalEntityData;
|
|
|
|
std::unordered_map<LOT, nejlika::EntityTemplate> entityTemplates;
|
|
|
|
std::unordered_map<LOT, nejlika::UpgradeTemplate> upgradeTemplates;
|
|
|
|
nejlika::Lookup lookup;
|
|
|
|
}
|
|
|
|
const nejlika::Lookup& nejlika::NejlikaData::GetLookup()
|
|
{
|
|
return lookup;
|
|
}
|
|
|
|
const std::unordered_map<ModifierNameType,std::vector<ModifierNameTemplate>>& nejlika::NejlikaData::GetModifierNameTemplates()
|
|
{
|
|
return modifierNameTemplates;
|
|
}
|
|
|
|
const std::vector<nejlika::ModifierNameTemplate>& nejlika::NejlikaData::GetModifierNameTemplates(ModifierNameType type)
|
|
{
|
|
const auto it = modifierNameTemplates.find(type);
|
|
|
|
if (it != modifierNameTemplates.end()) {
|
|
return it->second;
|
|
}
|
|
|
|
static const std::vector<nejlika::ModifierNameTemplate> empty;
|
|
return empty;
|
|
}
|
|
|
|
const std::optional<AdditionalItemData*> nejlika::NejlikaData::GetAdditionalItemData(LWOOBJID id) {
|
|
const auto& it = additionalItemData.find(id);
|
|
|
|
if (it != additionalItemData.end()) {
|
|
return std::optional<AdditionalItemData*>(&it->second);
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
const std::optional<AdditionalEntityData*> nejlika::NejlikaData::GetAdditionalEntityData(LWOOBJID id) {
|
|
const auto& it = additionalEntityData.find(id);
|
|
|
|
if (it != additionalEntityData.end()) {
|
|
return std::optional<AdditionalEntityData*>(&it->second);
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
const std::optional<EntityTemplate*> nejlika::NejlikaData::GetEntityTemplate(LOT lot) {
|
|
const auto& it = entityTemplates.find(lot);
|
|
|
|
if (it != entityTemplates.end()) {
|
|
return std::optional<EntityTemplate*>(&it->second);
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
const std::optional<UpgradeTemplate*> nejlika::NejlikaData::GetUpgradeTemplate(LOT lot) {
|
|
const auto& it = upgradeTemplates.find(lot);
|
|
|
|
if (it != upgradeTemplates.end()) {
|
|
return std::optional<UpgradeTemplate*>(&it->second);
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
void nejlika::NejlikaData::SetAdditionalItemData(LWOOBJID id, AdditionalItemData data) {
|
|
additionalItemData[id] = data;
|
|
}
|
|
|
|
void nejlika::NejlikaData::SetAdditionalEntityData(LWOOBJID id, AdditionalEntityData data) {
|
|
additionalEntityData[id] = data;
|
|
}
|
|
|
|
void nejlika::NejlikaData::UnsetAdditionalItemData(LWOOBJID id) {
|
|
additionalItemData.erase(id);
|
|
}
|
|
|
|
void nejlika::NejlikaData::UnsetAdditionalEntityData(LWOOBJID id) {
|
|
additionalEntityData.erase(id);
|
|
}
|
|
|
|
void nejlika::NejlikaData::LoadNejlikaData()
|
|
{
|
|
const auto& lookupFile = Game::config->GetValue("lookup");
|
|
|
|
if (!lookupFile.empty())
|
|
{
|
|
lookup = Lookup(lookupFile);
|
|
}
|
|
|
|
modifierNameTemplates.clear();
|
|
|
|
// Load data from json file
|
|
const auto& directory_name = Game::config->GetValue("nejlika");
|
|
|
|
if (directory_name.empty())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Loop through all files in the directory
|
|
for (const auto& entry : std::filesystem::directory_iterator(directory_name))
|
|
{
|
|
if (!entry.is_regular_file())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// It has to end on .mod.json
|
|
const auto& path = entry.path().string();
|
|
|
|
if (path.size() < 9 || path.substr(path.size() - 9) != ".mod.json")
|
|
{
|
|
continue;
|
|
}
|
|
|
|
LoadNejlikaDataFile(entry.path().string());
|
|
}
|
|
}
|
|
|
|
void nejlika::NejlikaData::LoadNejlikaDataFile(const std::string& path) {
|
|
std::ifstream file(path);
|
|
|
|
if (!file.is_open())
|
|
{
|
|
LOG("Failed to open nejlika data file: %s", path.c_str());
|
|
return;
|
|
}
|
|
|
|
nlohmann::json json;
|
|
|
|
try
|
|
{
|
|
json = nlohmann::json::parse(file);
|
|
}
|
|
catch (const nlohmann::json::exception& e)
|
|
{
|
|
LOG("Failed to parse nejlika data file: %s", e.what());
|
|
return;
|
|
}
|
|
|
|
if (json.contains("modifier-templates"))
|
|
{
|
|
const auto& modifierTemplates = json["modifier-templates"];
|
|
|
|
for (const auto& value : modifierTemplates)
|
|
{
|
|
auto modifierTemplate = ModifierNameTemplate(value);
|
|
|
|
if (modifierTemplate.GetModifiers().empty())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
modifierNameTemplates[modifierTemplate.GetType()].push_back(modifierTemplate);
|
|
}
|
|
}
|
|
|
|
LOG("Loaded %d modifier templates", modifierNameTemplates.size());
|
|
|
|
if (json.contains("entity-templates"))
|
|
{
|
|
const auto& entityTemplatesArray = json["entity-templates"];
|
|
|
|
for (const auto& value : entityTemplatesArray)
|
|
{
|
|
auto entityTemplate = EntityTemplate(value);
|
|
|
|
entityTemplates[entityTemplate.GetLOT()] = entityTemplate;
|
|
}
|
|
}
|
|
|
|
LOG("Loaded %d entity templates", entityTemplates.size());
|
|
|
|
if (json.contains("upgrade-templates"))
|
|
{
|
|
const auto& upgradeTemplatesArray = json["upgrade-templates"];
|
|
|
|
for (const auto& value : upgradeTemplatesArray)
|
|
{
|
|
auto upgradeTemplate = UpgradeTemplate(value);
|
|
|
|
upgradeTemplates[upgradeTemplate.GetLot()] = upgradeTemplate;
|
|
}
|
|
}
|
|
|
|
LOG("Loaded %d upgrade templates", upgradeTemplates.size());
|
|
}
|
|
|