Koray

c++ Json converter for server data files

11 posts in this topic

Hello, I started to convert some server data files from .txt to .json. I intend to convert them more understandable and modern with these changes, also few bugs and a memory leak in the old system has been fixed. Currently only mob_drop_info.txt file is translated, then all .txt files and proto files will be added.

 

Tutorial for mob_drop_info.txt game part:

Add to service.h:

#define ENABLE_JSON_GAME_FILES

Add to stl.h

inline std::wstring StringToWstring(std::string input)
{
    std::wstring output(input.begin(), input.end());
    return output;
}
inline std::string WstringToString(std::wstring input)
{
    std::string output(input.begin(), input.end());
    return output;
}

Search in input_db.cpp

"%s/mob_drop_item.txt", LocaleService_GetBasePath().c_str());

Change with:

#ifdef ENABLE_JSON_GAME_FILES
			"%s/mob_drop_item.json", LocaleService_GetBasePath().c_str());
#else
			"%s/mob_drop_item.txt", LocaleService_GetBasePath().c_str());
#endif

Search:

	if (!ITEM_MANAGER::instance().ReadMonsterDropItemGroup(szMOBDropItemFileName))

Change with:

#ifdef ENABLE_JSON_GAME_FILES
	if (!ITEM_MANAGER::instance().ReadMonsterDropItemGroupNew(szMOBDropItemFileName))
#else
	if (!ITEM_MANAGER::instance().ReadMonsterDropItemGroup(szMOBDropItemFileName))
#endif

Search in item_manager.h:

		bool			ReadDropItemGroup(const char * c_pszFileName);

Add it under:

#ifdef ENABLE_JSON_GAME_FILES
		bool			ReadMonsterDropItemGroupNew(const char * c_pszFileName);
#endif

Add in item_manager_read_tables.cpp

#ifdef ENABLE_JSON_GAME_FILES
#include <fstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#endif

Search:

bool ITEM_MANAGER::ReadMonsterDropItemGroup(const char * c_pszFileName)

Change like this:

https://vgy.me/FC4F2v.png

Codes:

https://gist.github.com/mq1n/f31e4f1130819c1404b1348c8b28613d

 

Converter:

https://github.com/mq1n/M2TxtToJson

 

Note:

You need c ++ 11 and boost property tree module to use this configuration.

 

  • Like 6
  • Thanks 1

Share this post


Link to post
Share on other sites

I used boost because it's already required dependecy from game server and I didn't want to add a new dependency for such a simple change., I'm sure there are more lightweight and simple alternatives here but I don't understand one thing, what's wrong with boost?

  • Like 1

Share this post


Link to post
Share on other sites
4 hours ago, Koray said:

what's wrong with boost?

Boost PropertyTree is not a json library (but it uses internally one). It drops tons of functionality from the JSON format. I also think it treats integers as strings. It's 20 times slower than the most used json libraries.

Its implementation looks very oldish. It's basically legacy code in its utmost definition.

Also, with the new c++ standards, many boost functionality can be replaced quite easily. Boost remains a bloatware for small projects, so I try to avoid it when possible.

As for other alternatives, these two are header-only (easily to implement/copy-paste on Extern/include/ or from "pkg install") and much more beautiful:

 

  • Like 6
  • Thanks 1

Share this post


Link to post
Share on other sites
à l’instant, Tallywa a dit :

Bump

Do you just really bumped a question inside a release post?

  • Haha 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.