2020-08-13 04:10:44 +01:00
|
|
|
#include "Common.h"
|
|
|
|
#include "Registry.h"
|
2020-11-07 01:51:08 +00:00
|
|
|
#include "block/Block.h"
|
2020-08-13 04:10:44 +01:00
|
|
|
|
|
|
|
#include "rapidjson/document.h"
|
|
|
|
#include "rapidjson/filereadstream.h"
|
|
|
|
#include "rapidjson/stringbuffer.h"
|
|
|
|
#include "rapidjson/writer.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
namespace Feather
|
|
|
|
{
|
|
|
|
IDMapper<BlockState> Registry::BLOCK_STATES;
|
2020-11-07 01:51:08 +00:00
|
|
|
RegistryMap<Block> Registry::BLOCKS;
|
2020-08-13 04:10:44 +01:00
|
|
|
|
|
|
|
void Registry::Init()
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
if (initialized) return;
|
|
|
|
|
|
|
|
Log::Info("Loading data/blocks.json...");
|
|
|
|
{
|
|
|
|
using namespace rapidjson;
|
|
|
|
|
|
|
|
std::string text;
|
|
|
|
FILE* fp = fopen("data/blocks.json", "rb");
|
|
|
|
if (!fp) {
|
|
|
|
Log::Error("Failed to read data/blocks.json");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
text.resize(ftell(fp));
|
|
|
|
rewind(fp);
|
|
|
|
fread(&text[0], 1, text.size(), fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
Document doc;
|
|
|
|
doc.Parse(text.c_str());
|
|
|
|
|
|
|
|
|
2020-11-07 01:51:08 +00:00
|
|
|
for (auto& blockData : doc.GetObject())
|
2020-08-13 04:10:44 +01:00
|
|
|
{
|
2020-11-07 01:51:08 +00:00
|
|
|
const char* blockName = blockData.name.GetString();
|
|
|
|
Block block(blockName);
|
|
|
|
|
2020-08-14 02:47:19 +01:00
|
|
|
// TODO
|
2020-11-07 01:51:08 +00:00
|
|
|
//if (blockData.value.HasMember("properties")) {}
|
2020-08-14 02:47:19 +01:00
|
|
|
|
2020-11-07 01:51:08 +00:00
|
|
|
//Log::Info("{}: {} states", blockData.name.GetString(), blockData.value["states"].Size());
|
|
|
|
for (auto& stateData : blockData.value["states"].GetArray())
|
2020-08-13 04:10:44 +01:00
|
|
|
{
|
2020-11-07 01:51:08 +00:00
|
|
|
std::string* name = new std::string(blockName);
|
2020-08-13 04:10:44 +01:00
|
|
|
BlockState state(*name);
|
2020-08-14 02:47:19 +01:00
|
|
|
|
|
|
|
if (stateData.HasMember("properties"))
|
|
|
|
{
|
|
|
|
for (auto& prop : stateData["properties"].GetObject())
|
|
|
|
{
|
|
|
|
std::string* propName = new std::string(prop.name.GetString());
|
|
|
|
std::string* propValue = new std::string(prop.value.GetString());
|
|
|
|
state.AddProperty(*propName, *propValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 04:10:44 +01:00
|
|
|
BLOCK_STATES.AddMapping(stateData["id"].GetInt(), state);
|
|
|
|
}
|
2020-08-14 02:47:19 +01:00
|
|
|
|
2020-11-07 01:51:08 +00:00
|
|
|
BLOCKS.Register(block.GetIdentifier(), std::move(block));
|
|
|
|
|
2020-08-13 04:10:44 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 01:51:08 +00:00
|
|
|
Log::Info("Loaded {} blocks and {} block states.", BLOCKS.Size(), BLOCK_STATES.Size());
|
2020-08-13 04:10:44 +01:00
|
|
|
|
|
|
|
//StringBuffer buf;
|
|
|
|
//Writer<StringBuffer> writer(buf);
|
|
|
|
//doc.Accept(writer);
|
|
|
|
|
|
|
|
// FIXME: why does this crash
|
|
|
|
//Log::Info(text);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-11-07 01:51:08 +00:00
|
|
|
}
|