Line data Source code
1 : #include "schema.h" 2 : #include "steam_types.h" 3 : 4 : #include <jansson.h> 5 : 6 : #define OID_SCHEMA_ROOT_UNPACK_FORMAT "{s:I, s:o}" /* format : appid = int, items = json_t* */ 7 : 8 : struct oid_schema_t 9 : { 10 : json_error_t last_syntax_err; /* latest syntax error */ 11 : 12 : json_int_t appid; /* schema Steam appid */ 13 : json_t* items; /* schema Steam Item Definitions */ 14 : }; 15 : 16 8 : oid_schema_t* oid_init_schema(void) 17 : { 18 8 : return calloc(1, sizeof(oid_schema_t)); 19 : } 20 : 21 8 : void oid_free_schema(oid_schema_t* schema) 22 : { 23 8 : if (schema) 24 : { 25 8 : free(schema); 26 : } 27 8 : } 28 : 29 : /* PARSING */ 30 2 : oid_schema_error_t oid_parse_json_root(oid_schema_t* sch, json_t* jroot) 31 : { 32 2 : if (!sch || !jroot || !json_is_object(jroot)) 33 : { 34 0 : return oid_schema_invalid_argument; 35 : } 36 : 37 : json_error_t root_parsing_err; 38 2 : json_int_t root_appid = 0; 39 2 : json_t* root_items = NULL; 40 : 41 2 : int root_parsing_ret = json_unpack_ex( 42 : jroot, 43 : &root_parsing_err, 44 : JSON_STRICT, 45 : OID_SCHEMA_ROOT_UNPACK_FORMAT, 46 : "appid", &root_appid, 47 : "items", &root_items 48 : ); 49 : 50 2 : if (root_parsing_ret != 0) 51 : { 52 1 : oid_set_json_error(sch, &root_parsing_err); 53 1 : return oid_schema_json_syntax; 54 : } 55 : 56 1 : if (!json_is_array(root_items)) 57 : { 58 0 : return oid_schema_json_format; 59 : } 60 : 61 1 : sch->appid = root_appid; 62 : 63 : // TODO load item defs root in cache 64 : 65 1 : return oid_schema_success; 66 : } 67 : 68 6 : oid_schema_error_t oid_load_itemdef_schema(oid_schema_t* sch, const char* file_path) 69 : { 70 6 : if (!sch || !file_path) 71 : { 72 2 : return oid_schema_invalid_argument; 73 : } 74 : 75 : json_error_t json_err; 76 4 : json_t* root = json_load_file(file_path, 0, &json_err); 77 4 : if(!root) 78 : { 79 2 : oid_set_json_error(sch, &json_err); 80 2 : return oid_schema_json_syntax; 81 : } 82 : 83 2 : oid_schema_error_t res = oid_parse_json_root(sch, root); 84 : 85 2 : json_decref(root); 86 2 : return res; 87 : } 88 : 89 : /* ERROR HANDLING */ 90 : 91 8 : const char* oid_schema_error_to_string(oid_schema_error_t err) 92 : { 93 8 : switch (err) 94 : { 95 1 : case oid_schema_success: return "Success"; 96 2 : case oid_schema_unknown: return "Unknown error"; 97 1 : case oid_schema_invalid_argument: return "Invalid argument"; 98 1 : case oid_schema_out_of_memory: return "Out of memory"; 99 1 : case oid_schema_json_format: return "Unexpected Steam Inventory Schema JSON"; 100 1 : case oid_schema_json_syntax: return "Jansson JSON parsing error"; 101 1 : default: return oid_schema_error_to_string(oid_schema_unknown); 102 : } 103 : } 104 : 105 6 : const json_error_t* oid_get_last_json_err(const oid_schema_t* sch) 106 : { 107 6 : if (!sch) 108 : { 109 1 : return NULL; 110 : } 111 : 112 5 : return &sch->last_syntax_err; 113 : } 114 : 115 5 : void oid_set_json_error(oid_schema_t* sch, const json_error_t* err) 116 : { 117 5 : if (!sch || !err) 118 : { 119 1 : return; 120 : } 121 : 122 4 : sch->last_syntax_err = *err; 123 : }