Browse Source

added schema validation for hashmap files

master
cinnaboot 8 years ago
parent
commit
35411dcc34
  1. 11
      TODO.md
  2. 2
      data/hashmap_scene_hexmap.json
  3. 39
      data/hexmap_schema.json
  4. 5
      src/hexgame.cpp
  5. 39
      src/scene_loader.cpp
  6. 2
      src/scene_loader.h

11
TODO.md

@ -1,13 +1,12 @@
## TODO:
- better (flat) level mesh for testing grid position
- reduce verbosity of assimp loading
- make hashmap grid type
- in-game editor for adding removing hexes to line up with terrain/indoor meshes
- need to update json schema with "oneOf" to validate different grid types
- find a better alternative to g_data_dir in scene_loader.cpp
- make and validate json schema for grid hash_map
- position entities on grid hexes
- pathfinding
- assimp animation
- map generation
- gooey
- add object inspector window
- modify position, scaling, rotation
@ -34,9 +33,6 @@
- maybe can cause loading mesh to fail when more than one mesh per file
- figure out bug in fragment shader when removing commented line
- move input handling out to new file, controller?
- map generation
- pathfinding
- assimp animation
- replace aixlog with custom logging iostream?
- think about combining mesh data and render_group data to save some memory
- actually don't need to save the vertex/normal buffers after passing to opengl
@ -49,6 +45,7 @@
- add initial opengl constant for INT_MAX, and checks in render_group functions
- try out YAML instead of JSON (make sure can still use schema validation)
- there's a bug when changing hex_grid.hex_radius from a 2 digit number to a 1 digit number in test_scene.json
- also noticing more bugs with rapidjson schema validation, "required" fields don't seem to always cause validation to fail
- move from COLLADA output to glTF (GL Transmission Format) https://www.khronos.org/gltf/
- add config.js/yaml for application config (remove some defines everywhere)
- maybe try deferred rendering at some point to use a ton of lights

2
data/hashmap_scene_hexmap.json

File diff suppressed because one or more lines are too long

39
data/hexmap_schema.json

@ -0,0 +1,39 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"vector3": {
"type": "object",
"properties": {
"x": { "type": "number" },
"y": { "type": "number" },
"z": { "type": "number" }
},
"required": ["x", "y", "z"]
}
},
"type": "object",
"properties": {
"hex_size": {"type": "number"},
"layout_mode": {"type": "string"},
"world_position": {"$ref": "#/definitions/vector3"},
"world_normal": {"$ref": "#/definitions/vector3"},
"hexes": {
"type": "array",
"items": {
"XPos": {"type": "number"},
"YPos": {"type": "number"},
"q": {"type": "integer"},
"r": {"type": "integer"},
"s": {"type": "integer"},
"vertices": {
"type": "array",
"items": {"$ref": "#definitions/vector3"}
}
},
"required": ["XPos", "YPos", "q", "r", "s", "vertices"]
}
},
"required": ["hex_size", "layout_mode", "world_position", "world_normal", "hexes"]
}

5
src/hexgame.cpp

@ -9,6 +9,7 @@
#define DATA_DIR "../data"
#define DEFAULT_SCENE_FILE "hashmap_scene.json"
#define SCENE_SCHEMA_FILE "scene_schema.json"
#define HEXMAP_SCHEMA_FILE "hexmap_schema.json"
#define MAX_ENTITIES 1000
#include <vector>
@ -63,7 +64,7 @@ loadSceneFromJson(game_state* s, render_state* rs)
if (slCreateHexRenderGroups(gs->grid, rs)) {
if (gs->grid.gridT == HASH_MAP) {
if (!slParseGridHashMap(gs->grid, DATA_DIR))
if (!slParseGridHashMap(gs->grid, DATA_DIR, HEXMAP_SCHEMA_FILE))
ret = false;
}
@ -96,7 +97,7 @@ init()
// which contains the hashtable.
g_game_state->grid.hex_map = std::unordered_map<Hex, hex_info, hex_hashfunc>();
// NOTE: testing hex_add
g_game_state->grid.draw_mode = ADD_HEXES;
g_game_state->grid.draw_mode = NONE;
// TODO: maybe add this as an option to scene json?
g_game_state->grid.normal = v3f(0.f, 0.f, 1.f);

39
src/scene_loader.cpp

@ -30,8 +30,8 @@ bool loadTextFile(char*& output, const char* data_dir, const char* file_name);
bool parseFile(rapidjson::Document* doc, const char* file_contents, const char* file_name);
glm::vec3 parseVec3(const rapidjson::Value& node);
glm::vec4 parseVec4(const rapidjson::Value& node);
bool validateScene(rapidjson::Document* schema_doc, rapidjson::Document* scene_doc,
const char* scene_file);
bool validateJSON(rapidjson::Document* schema_doc, rapidjson::Document* doc,
const char* json_file);
void addV3f(rapidjson::Writer<rapidjson::StringBuffer>& writer, float x, float y, float z);
@ -58,7 +58,7 @@ slLoadFile(const char* data_dir, const char* scene_file, const char* schema_file
utilSafeFree(scene_contents);
utilSafeFree(schema_contents);
if (!retDoc || !retSchema || !validateScene(&schema_doc, sd->doc, scene_file)) {
if (!retDoc || !retSchema || !validateJSON(&schema_doc, sd->doc, scene_file)) {
slFreeSceneDoc(sd);
return nullptr;
}
@ -163,11 +163,11 @@ slParseHexGrid(slSceneDoc* sd, hexgrid& hg, util_image& palette_image)
}
bool
slParseGridHashMap(hexgrid& hg, const char* data_dir)
slParseGridHashMap(hexgrid& hg, const char* data_dir, const char* hexmap_schema_file)
{
LOG(DEBUG) << "loading map_file: " << hg.map_file << "\n";
char* contents;
char* contents;
if (!loadTextFile(contents, data_dir, hg.map_file))
return false;
@ -178,11 +178,19 @@ slParseGridHashMap(hexgrid& hg, const char* data_dir)
return true;
}
// TODO: still need a schema for the hashmap
rapidjson::Document doc;
bool ret = false;
rapidjson::Document doc, schema_doc;
bool retDoc, retSchema = false;
char* schema_contents;
if (parseFile(&doc, contents, hg.map_file)) {
retDoc = parseFile(&doc, contents, hg.map_file);
if (loadTextFile(schema_contents, data_dir, hexmap_schema_file))
retSchema = parseFile(&schema_doc, schema_contents, hexmap_schema_file);
utilSafeFree(contents);
utilSafeFree(schema_contents);
if (retDoc && retSchema && validateJSON(&schema_doc, &doc, hg.map_file)) {
hg.hex_size = doc["hex_size"].GetInt();
std::string orientation_str = doc["layout_mode"].GetString();
hg.layout_mode = (orientation_str == "layout_flat") ? LAYOUT_FLAT : LAYOUT_POINTY;
@ -214,11 +222,10 @@ slParseGridHashMap(hexgrid& hg, const char* data_dir)
hg.hex_map.insert({hxi.hex, hxi});
}
ret = true;
return true;
}
utilSafeFree(contents);
return ret;
return false;
}
bool
@ -362,13 +369,13 @@ parseFile(rapidjson::Document* doc, const char* file_contents, const char* file_
}
bool
validateScene(rapidjson::Document* schema_doc, rapidjson::Document* scene_doc, const char* scene_file)
validateJSON(rapidjson::Document* schema_doc, rapidjson::Document* doc, const char* json_file)
{
rapidjson::SchemaDocument schema(*schema_doc);
rapidjson::SchemaValidator validator(schema);
if (!scene_doc->Accept(validator)) {
LOG(ERROR) << "Error validating scene file: " << scene_file << "\n";
if (!doc->Accept(validator)) {
LOG(ERROR) << "Error validating scene file: " << json_file << "\n";
rapidjson::StringBuffer sb;
validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
LOG(ERROR) << "Invalid schema: " << sb.GetString() << "\n";
@ -379,7 +386,7 @@ validateScene(rapidjson::Document* schema_doc, rapidjson::Document* scene_doc, c
return false;
}
LOG(INFO) << "scene file: " << scene_file << " passed schema validation\n";
LOG(INFO) << "JSON file: " << json_file << " passed schema validation\n";
return true;
}

2
src/scene_loader.h

@ -27,7 +27,7 @@ void slParseCamera(slSceneDoc* sd, camera& cam);
void slParseHexGrid(slSceneDoc* sd, hexgrid& hg, util_image& palette_image);
bool slParseGridHashMap(hexgrid& hg, const char* data_dir);
bool slParseGridHashMap(hexgrid& hg, const char* data_dir, const char* hexmap_schema_file);
bool slCreateHexRenderGroups(hexgrid& hg, render_state* rs);

Loading…
Cancel
Save