Metin2 [C++] Blend Item Renewal [JSON]

Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Blaze

Elite
Elite
VIP
Joined
Jan 28, 2019
Messages
507
Credits
635
So how do you set up this "system"?
  1. Make sure that the compiler you are using supports at least C++11.
  2. JSON ayrıştırıcısını (parser) descent.
  3. Replace old blend_item.h and blend_item.cpp files with new ones, that's all.
  4. If you want the "hot-reload" (instantly update) function of the JSON file, you can add the cmd_gm.cpp part (recommended).
Example blend.json content
Code:
Expand Collapse Copy
{
    "50821": {
        "type": "CRITICAL_PCT",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50822": {
        "type": "PENETRATE_PCT",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50823": {
        "type": "ATTACK_SPEED",
        "value": [ 2, 3, 4, 5, 8 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50824": {
        "type": "RESIST_MAGIC",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50825": {
        "type": "ATT_BONUS",
        "value": [ 30, 50, 70, 90, 120 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50826": {
        "type": "DEF_BONUS",
        "value": [ 40, 70, 100, 150, 200 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "51002": {
        "type": "ENERGY",
        "value": [ 1, 3, 5, 7, 10 ],
        "duration": 1800 // It is possible to use only one duration (and value).
    }
}

blend_item.cpp:
Expand Collapse Copy
#include "stdafx.h"
#include "constants.h"
#include "blend_item.h"
#include "locale_service.h"

#include <fstream>

nlohmann::json json_blend;

bool Blend_Item_init()
{
    try
    {
        json_blend = nlohmann::json::parse(std::ifstream(LocaleService_GetBasePath() + "/blend.json"), nullptr, true, true);
        return true;
    }
    catch (const nlohmann::json::parse_error& message)
    {
        sys_err("%s", message.what());
        return false;
    }
}

bool Blend_Item_find(const uint32_t item)
{
    return json_blend.contains(std::to_string(item));
}

int32_t Blend_Item_verify(const nlohmann::json& item, const bool can_string)
{
    if (item.is_number())
        return item.get<int32_t>();

    return can_string ? FN_get_apply_type(item.get<std::string>().c_str()) : item.at(number(0, item.size() - 1)).get<int32_t>();
}

void Blend_Item_set_value(const LPITEM item)
{
    if (!item)
        return;

    try
    {
        const auto& find = json_blend.find(std::to_string(item->GetVnum())).value();
        item->SetSocket(0, Blend_Item_verify(find.at("type"), true));
        item->SetSocket(1, Blend_Item_verify(find.at("value"), false));
        item->SetSocket(2, Blend_Item_verify(find.at("duration"), false));
    }
    catch (const nlohmann::json::exception& message)
    {
        sys_err("%s", message.what());
    }
}

blend_item.h:
Expand Collapse Copy
#pragma once

#include "item.h"

#include <cstdint>
#include <nlohmann/json.hpp>

bool Blend_Item_init();
bool Blend_Item_find(uint32_t item);
int32_t Blend_Item_verify(const nlohmann::json& item, bool can_string);
void Blend_Item_set_value(LPITEM item);

Cmd_gm.cpp for reload:
Expand Collapse Copy
// Add:
#include "blend_item.h"

// Find:
            case 'c':    // cube

                Cube_init ();
                break;

// Paste (under):
            case 'b':
                {
                    if (Blend_Item_init())
                        ch->ChatPacket(CHAT_TYPE_INFO, "Blend items reloaded.");
                    else
                        ch->ChatPacket(CHAT_TYPE_INFO, "Something went wrong while loading blend items, check syserr.txt!");
                }
                break;

 
So how do you set up this "system"?
  1. Make sure that the compiler you are using supports at least C++11.
  2. JSON ayrıştırıcısını (parser) descent.
  3. Replace old blend_item.h and blend_item.cpp files with new ones, that's all.
  4. If you want the "hot-reload" (instantly update) function of the JSON file, you can add the cmd_gm.cpp part (recommended).
Example blend.json content
Code:
Expand Collapse Copy
{
    "50821": {
        "type": "CRITICAL_PCT",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50822": {
        "type": "PENETRATE_PCT",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50823": {
        "type": "ATTACK_SPEED",
        "value": [ 2, 3, 4, 5, 8 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50824": {
        "type": "RESIST_MAGIC",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50825": {
        "type": "ATT_BONUS",
        "value": [ 30, 50, 70, 90, 120 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50826": {
        "type": "DEF_BONUS",
        "value": [ 40, 70, 100, 150, 200 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "51002": {
        "type": "ENERGY",
        "value": [ 1, 3, 5, 7, 10 ],
        "duration": 1800 // It is possible to use only one duration (and value).
    }
}

blend_item.cpp:
Expand Collapse Copy
#include "stdafx.h"
#include "constants.h"
#include "blend_item.h"
#include "locale_service.h"

#include <fstream>

nlohmann::json json_blend;

bool Blend_Item_init()
{
    try
    {
        json_blend = nlohmann::json::parse(std::ifstream(LocaleService_GetBasePath() + "/blend.json"), nullptr, true, true);
        return true;
    }
    catch (const nlohmann::json::parse_error& message)
    {
        sys_err("%s", message.what());
        return false;
    }
}

bool Blend_Item_find(const uint32_t item)
{
    return json_blend.contains(std::to_string(item));
}

int32_t Blend_Item_verify(const nlohmann::json& item, const bool can_string)
{
    if (item.is_number())
        return item.get<int32_t>();

    return can_string ? FN_get_apply_type(item.get<std::string>().c_str()) : item.at(number(0, item.size() - 1)).get<int32_t>();
}

void Blend_Item_set_value(const LPITEM item)
{
    if (!item)
        return;

    try
    {
        const auto& find = json_blend.find(std::to_string(item->GetVnum())).value();
        item->SetSocket(0, Blend_Item_verify(find.at("type"), true));
        item->SetSocket(1, Blend_Item_verify(find.at("value"), false));
        item->SetSocket(2, Blend_Item_verify(find.at("duration"), false));
    }
    catch (const nlohmann::json::exception& message)
    {
        sys_err("%s", message.what());
    }
}

blend_item.h:
Expand Collapse Copy
#pragma once

#include "item.h"

#include <cstdint>
#include <nlohmann/json.hpp>

bool Blend_Item_init();
bool Blend_Item_find(uint32_t item);
int32_t Blend_Item_verify(const nlohmann::json& item, bool can_string);
void Blend_Item_set_value(LPITEM item);

Cmd_gm.cpp for reload:
Expand Collapse Copy
// Add:
#include "blend_item.h"

// Find:
            case 'c':    // cube

                Cube_init ();
                break;

// Paste (under):
            case 'b':
                {
                    if (Blend_Item_init())
                        ch->ChatPacket(CHAT_TYPE_INFO, "Blend items reloaded.");
                    else
                        ch->ChatPacket(CHAT_TYPE_INFO, "Something went wrong while loading blend items, check syserr.txt!");
                }
                break;

*** Hidden text: cannot be quoted. ***
can you contact me on discord?
"darkside01"
 
So how do you set up this "system"?
  1. Make sure that the compiler you are using supports at least C++11.
  2. JSON ayrıştırıcısını (parser) descent.
  3. Replace old blend_item.h and blend_item.cpp files with new ones, that's all.
  4. If you want the "hot-reload" (instantly update) function of the JSON file, you can add the cmd_gm.cpp part (recommended).
Example blend.json content
Code:
Expand Collapse Copy
{
    "50821": {
        "type": "CRITICAL_PCT",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50822": {
        "type": "PENETRATE_PCT",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50823": {
        "type": "ATTACK_SPEED",
        "value": [ 2, 3, 4, 5, 8 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50824": {
        "type": "RESIST_MAGIC",
        "value": [ 8, 10, 12, 15, 20 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50825": {
        "type": "ATT_BONUS",
        "value": [ 30, 50, 70, 90, 120 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "50826": {
        "type": "DEF_BONUS",
        "value": [ 40, 70, 100, 150, 200 ],
        "duration": [ 60, 120, 180, 300, 600 ]
    },

    "51002": {
        "type": "ENERGY",
        "value": [ 1, 3, 5, 7, 10 ],
        "duration": 1800 // It is possible to use only one duration (and value).
    }
}

blend_item.cpp:
Expand Collapse Copy
#include "stdafx.h"
#include "constants.h"
#include "blend_item.h"
#include "locale_service.h"

#include <fstream>

nlohmann::json json_blend;

bool Blend_Item_init()
{
    try
    {
        json_blend = nlohmann::json::parse(std::ifstream(LocaleService_GetBasePath() + "/blend.json"), nullptr, true, true);
        return true;
    }
    catch (const nlohmann::json::parse_error& message)
    {
        sys_err("%s", message.what());
        return false;
    }
}

bool Blend_Item_find(const uint32_t item)
{
    return json_blend.contains(std::to_string(item));
}

int32_t Blend_Item_verify(const nlohmann::json& item, const bool can_string)
{
    if (item.is_number())
        return item.get<int32_t>();

    return can_string ? FN_get_apply_type(item.get<std::string>().c_str()) : item.at(number(0, item.size() - 1)).get<int32_t>();
}

void Blend_Item_set_value(const LPITEM item)
{
    if (!item)
        return;

    try
    {
        const auto& find = json_blend.find(std::to_string(item->GetVnum())).value();
        item->SetSocket(0, Blend_Item_verify(find.at("type"), true));
        item->SetSocket(1, Blend_Item_verify(find.at("value"), false));
        item->SetSocket(2, Blend_Item_verify(find.at("duration"), false));
    }
    catch (const nlohmann::json::exception& message)
    {
        sys_err("%s", message.what());
    }
}

blend_item.h:
Expand Collapse Copy
#pragma once

#include "item.h"

#include <cstdint>
#include <nlohmann/json.hpp>

bool Blend_Item_init();
bool Blend_Item_find(uint32_t item);
int32_t Blend_Item_verify(const nlohmann::json& item, bool can_string);
void Blend_Item_set_value(LPITEM item);

Cmd_gm.cpp for reload:
Expand Collapse Copy
// Add:
#include "blend_item.h"

// Find:
            case 'c':    // cube

                Cube_init ();
                break;

// Paste (under):
            case 'b':
                {
                    if (Blend_Item_init())
                        ch->ChatPacket(CHAT_TYPE_INFO, "Blend items reloaded.");
                    else
                        ch->ChatPacket(CHAT_TYPE_INFO, "Something went wrong while loading blend items, check syserr.txt!");
                }
                break;

*** Hidden text: cannot be quoted. ***
t4rfews
 
Back
Top