ihhub / fheroes2

fheroes2 is a recreation of Heroes of Might and Magic II game engine.
https://ihhub.github.io/fheroes2/
GNU General Public License v2.0
2.67k stars 373 forks source link

[Discussion topic] Add JSON third party library as a replacement for XML code #3477

Open ihhub opened 3 years ago

ihhub commented 3 years ago

This issue is made as an exception. The rest of discussions and ideas must go to Discussion section.

The current source code for possible modding features are done with XML help. This code is totally outdated and not in use anymore. The first step for code for proper modding support is to remove XML code usage everywhere. Secondly, we shouldn't use XML for modding as this is typeless string base format which is hard to control and even edit. The proposal is to add a third party independent library which could be compiled as a part of fheroes2 project. This library ideally should be C++ library (with C++11 support). The best candidate for more stable format is JSON which is type-dependent, easy to edit and it's widely supported everywhere. Before even implementing any code related to reading something from JSON file we should re-developed the way where and how we store all information we're going to modify.

One of candidates for the third-party library is RapidJSON, another one is JSON.

ihhub commented 3 years ago

Hi @oleg-derevenetz and @idshibanov , I would like to hear your opinion about this idea.

oleg-derevenetz commented 3 years ago

Hi @ihhub

One of candidates for the third-party library is RapidJSON, another one is JSON.

If there is no need to constantly parse large volumes of JSON data (I suppose that this will be done only once - at startup - anyway) I'd vote for nlohmann/JSON. It's high customizable and well-supported.

Before even implementing any code related to reading something from JSON file we should re-developed the way where and how we store all information we're going to modify.

I think we can do this piece-by-piece: for example, migrate heroes data to JSON first, then spells, then artifacts, etc, etc. I think it's easier to start from heroes, because they don't have "specific" per-hero logic AFAIK (as opposed to spells or artifacts).

idshibanov commented 3 years ago

I'd vote for nlohmann/JSON.

I was going to suggest the same library. It is efficient and easier to use than RapidJSON.

I think we can do this piece-by-piece: for example, migrate heroes data to JSON first, then spells, then artifacts, etc, etc.

Keep in mind that heroes have secondary skills that could be under a separate json file.

Let's define the relationships and go from simple to complex (a skill or spell). Artifacts will be later since they can add a spell, modify a skill or relate to a monster. Some things shouldn't have it's own JSON entry - probably resources (what is gold, ore, etc), hero primary skills (attack, defense, etc) and maybe faction (knight, warlock, etc).

Also later on we have to add more flags/fields to struct so JSON data will replace huge switch/cases in files like monster.cpp (GetRNDSize, isFlying, FromDwelling) or mp2.cpp.

ihhub commented 1 year ago

Hi @oleg-derevenetz , I think it is a time we bring JSON support to the project. I can see that adding information about every object on the Adventure Map programmatically might be very troublesome to do and to maintain in the future as the game currently has around 900 objects. I am thinking that we create a structure in JSON format to be loaded into the game and this file can be populated by even non developers. Once the file is completed we can put it inside resurrection.h2d file. What do you think?

oleg-derevenetz commented 1 year ago

@ihhub yes, why not. IIRC there originally was Qt-based editor at the time, I believe its data files (xml?) can also be used to populate this json (with additional verification maybe?).

ihhub commented 1 year ago

Hi @oleg-derevenetz , yes, we can use json as a main basis for the map format and compress it with zlib. This is actually done by HoMM4 (XML format) as far as I remember.

FyiurAmron commented 4 months ago

Just out of curiosity, why JSON and not e.g. YAML? I'm not a big YAML fan myself and usually prefer JSON for data transmission, but for config and user-editable stuff, having the input defined as YAML makes it an order of magnitude simpler to edit and maintain by humans, while also benefitting from being an industry standard (>20 years old), having support virtually in every language and being parsed essentially to the same data structures as JSON (you can easily get your hands on YAML-to-JSON converter, since YAML is basically JSON superset and you can still parse most JSONs with a YAML parser properly). There's https://github.com/jbeder/yaml-cpp etc. , so no additional coding over JSON-based data is needed TBH.

Like I said, I'm not a big fan of YAML itself, but for modding and game design, it's virtually perfect, since it simply integrates in a very easy way and is much more readable and writeable than JSON.

PS I'm not even proposing any other JSON-based extensions like JSON5 etc. mainly because the support does vary very wildly between platforms.