Skip to content

Commit

Permalink
Deduplicate common json schema type definitions. Fix pitch definition…
Browse files Browse the repository at this point in the history
… in warpball template schema
  • Loading branch information
FreeSlave committed Sep 23, 2024
1 parent aafd340 commit cb3cde1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 101 deletions.
27 changes: 12 additions & 15 deletions cl_dll/hud_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,9 @@ using namespace rapidjson;
const char hudInventorySchema[] = R"(
{
"type": "object",
"definitions": {
"alpha": {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"color": {
"type": ["string", "null"],
"pattern": "^([0-9]{1,3}[ ]+[0-9]{1,3}[ ]+[0-9]{1,3})|((#|0x)[0-9a-fA-F]{6})$"
}
},
"properties": {
"default_sprite_alpha": "#/definitions/alpha",
"text_alpha": "#/definitions/alpha",
"default_sprite_alpha": "definitions.json#/alpha",
"text_alpha": "definitions.json#/alpha",
"items": {
"additionalProperties": {
"type": "object",
Expand All @@ -35,10 +24,10 @@ const char hudInventorySchema[] = R"(
"type": ["string", "null"]
},
"color": {
"$ref": "#/definitions/color"
"$ref": "definitions.json#/color"
},
"alpha": {
"$ref": "#/definitions/alpha"
"$ref": "definitions.json#/alpha"
},
"position": {
"type": "string",
Expand Down Expand Up @@ -102,6 +91,14 @@ bool InventoryHudSpec::ReadFromFile(const char *fileName)
}
}
auto colorIt = value.FindMember("color");

Color color;
if (UpdatePropertyFromJson(color, value, "color"))
{
item.packedColor = PackRGB(color.r, color.g, color.b);
item.colorDefined = true;
}

if (colorIt != value.MemberEnd())
{
if (colorIt->value.IsString())
Expand Down
30 changes: 2 additions & 28 deletions dlls/soundscripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@ using namespace rapidjson;
const char* soundScriptsSchema = R"(
{
"definitions": {
"range": {
"type": ["string", "object", "number"],
"minimum": 0.0,
"maximum": 1.0,
"pattern": "[0-9]+(\\.[0-9]+)?(,[0-9]+(\\.[0-9]+)?)?",
"properties": {
"min": {
"type": "number"
},
"max": {
"type": "number"
}
}
},
"range_int": {
"type": ["string", "object", "integer"],
"pattern": "[0-9]+(,[0-9]+)?",
"properties": {
"min": {
"type": "integer"
},
"max": {
"type": "integer"
}
}
},
"soundscript": {
"type": "object",
"properties": {
Expand All @@ -55,15 +29,15 @@ const char* soundScriptsSchema = R"(
"pattern": "^auto|weapon|voice|item|body|static$"
},
"volume": {
"$ref": "#/definitions/range",
"$ref": "definitions.json#/range",
},
"attenuation": {
"type": ["number", "string"],
"minimum": 0,
"pattern": "^norm|idle|static|none$"
},
"pitch": {
"$ref": "#/definitions/range_int"
"$ref": "definitions.json#/range_int"
}
}
}
Expand Down
62 changes: 6 additions & 56 deletions dlls/warpball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,63 +68,13 @@ const char warpballCatalogSchema[] = R"(
"maximum": 1.0
},
"pitch": {
"$ref": "#/definitions/range"
"$ref": "definitions.json#/range_int"
},
"attenuation": {
"type": "number",
"minimum": 0
}
}
},
"alpha": {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"color": {
"type": ["string", "array"],
"pattern": "^([0-9]{1,3}[ ]+[0-9]{1,3}[ ]+[0-9]{1,3})|((#|0x)[0-9a-fA-F]{6})$",
"items": {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"minItems": 3,
"maxItems": 3
},
"range": {
"type": ["string", "object", "number", "array"],
"pattern": "[0-9]+(\\.[0-9]+)?(,[0-9]+(\\.[0-9]+)?)?",
"properties": {
"min": {
"type": "number"
},
"max": {
"type": "number"
}
},
"items": {
"type": "number"
},
"minItems": 2,
"maxItems": 2
},
"range_int": {
"type": ["string", "object", "integer", "array"],
"pattern": "[0-9]+(,[0-9]+)?",
"properties": {
"min": {
"type": "integer"
},
"max": {
"type": "integer"
}
},
"items": {
"type": "integer",
},
"minItems": 2,
"maxItems": 2
}
},
"properties": {
Expand Down Expand Up @@ -165,10 +115,10 @@ const char warpballCatalogSchema[] = R"(
"$ref": "#/definitions/sprite_name"
},
"color": {
"$ref": "#/definitions/color"
"$ref": "definitions.json#/color"
},
"alpha": {
"$ref": "#/definitions/alpha"
"$ref": "definitions.json#/alpha"
},
"width": {
"type": "integer",
Expand All @@ -178,7 +128,7 @@ const char warpballCatalogSchema[] = R"(
"type": "integer"
},
"life": {
"$ref": "#/definitions/range"
"$ref": "definitions.json#/range"
}
}
},
Expand All @@ -187,13 +137,13 @@ const char warpballCatalogSchema[] = R"(
"minumum": 1
},
"beam_count": {
"$ref": "#/definitions/range_int"
"$ref": "definitions.json#/range_int"
},
"light": {
"type": ["object", "null"],
"properties": {
"color": {
"$ref": "#/definitions/color"
"$ref": "definitions.json#/color"
},
"radius": {
"type": "integer"
Expand Down
80 changes: 78 additions & 2 deletions game_shared/json_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,60 @@

using namespace rapidjson;

constexpr const char definitions[] = R"(
{
"alpha": {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"color": {
"type": ["string", "array", "null"],
"pattern": "^([0-9]{1,3}[ ]+[0-9]{1,3}[ ]+[0-9]{1,3})|((#|0x)[0-9a-fA-F]{6})$",
"items": {
"type": "integer",
"minimum": 0,
"maximum": 255
},
"minItems": 3,
"maxItems": 3
},
"range": {
"type": ["string", "object", "number", "array"],
"pattern": "[0-9]+(\\.[0-9]+)?(,[0-9]+(\\.[0-9]+)?)?",
"properties": {
"min": {
"type": "number"
},
"max": {
"type": "number"
}
},
"items": {
"type": "number"
},
"minItems": 2,
"maxItems": 2
},
"range_int": {
"type": ["string", "object", "integer", "array"],
"pattern": "[0-9]+(,[0-9]+)?",
"properties": {
"min": {
"type": "integer"
},
"max": {
"type": "integer"
}
},
"items": {
"type": "integer",
},
"minItems": 2,
"maxItems": 2
}
})";

static void CalculateLineAndColumnFromOffset(const char* pMemFile, size_t offset, size_t& line, size_t& column)
{
const char* cur = pMemFile;
Expand Down Expand Up @@ -52,19 +106,41 @@ static void ReportParseErrors(const char* fileName, ParseResult& parseResult, co
JSON_ERROR("%s: JSON parse error: %s (Line %lu, column %lu)\n", fileName, GetParseError_En(parseResult.Code()), errorLine, errorColumn);
}

class DefinitionsProvider : public IRemoteSchemaDocumentProvider
{
public:
DefinitionsProvider(const SchemaDocument* schema): _schema(schema) {}
const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) {
return _schema;
}
private:
const SchemaDocument* _schema;
};

bool ReadJsonDocumentWithSchema(Document &document, const char *pMemFile, int fileSize, const char *schemaText, const char* fileName)
{
if (!fileName)
fileName = "";

Document definitionsSchemaDocument;
definitionsSchemaDocument.Parse<kParseTrailingCommasFlag | kParseCommentsFlag>(definitions);
ParseResult parseResult = definitionsSchemaDocument;
if (!parseResult) {
ReportParseErrors(fileName, parseResult, pMemFile);
return false;
}
SchemaDocument definitionsSchema(definitionsSchemaDocument);

Document schemaDocument;
schemaDocument.Parse<kParseTrailingCommasFlag | kParseCommentsFlag>(schemaText);
ParseResult parseResult = schemaDocument;
parseResult = schemaDocument;
if (!parseResult) {
ReportParseErrors(fileName, parseResult, pMemFile);
return false;
}
SchemaDocument schema(schemaDocument);

DefinitionsProvider provider(&definitionsSchema);
SchemaDocument schema(schemaDocument, 0, 0, &provider);

document.Parse<kParseTrailingCommasFlag | kParseCommentsFlag>(pMemFile, fileSize);
parseResult = document;
Expand Down

0 comments on commit cb3cde1

Please sign in to comment.