bool JSON_ParseFile(char *filename, const unordered_map<const char*, jsonParseFunc>& parsers, void* output) { if(!filename || !filename[0]) { R_Message(PRIORITY_WARNING, "JSON_ParseFile: bad filename sent\n"); return false; } File* file = trap->OpenFile(filename, "rb"); if(!file) { R_Message(PRIORITY_WARNING, "JSON_ParseFile: could not open file %s\n", filename); return false; } size_t numChars = trap->GetFileSize(file); char* s = (char*)trap->Zone_Alloc(numChars * sizeof(char), "files"); trap->ReadPlaintext(file, numChars, s); trap->CloseFile(file); char error[1024] = {0}; cJSON* root = cJSON_ParsePooled(s, error, sizeof(error)); if(error[0] || !root) { R_Message(PRIORITY_ERROR, "ERROR: %s: %s\n", filename, error); trap->Zone_FastFree(s, "files"); return false; } for(auto it = cJSON_GetFirstItem(root); it; it = cJSON_GetNextItem(it)) { JSON_ParseFieldSet(root, parsers, output); } trap->Zone_FastFree(s, "files"); return true; }
/* ============================ JKG_LoadAmmo Loads an individual ammo (.ammo) file. Called on both the client and the server. ============================ */ static qboolean JKG_LoadAmmo(const char* fileName, const char* dir) { int fileLen; fileHandle_t f; char fileBuffer[AMMO_FILEBUFFER]; char error[AMMO_ERRBUFFER]; fileLen = trap->FS_Open(va("%s%s", dir, fileName), &f, FS_READ); if (!f || fileLen == -1) { Com_Printf(S_COLOR_RED "Could not read %s\n", fileName); return qfalse; } if (fileLen == 0) { trap->FS_Close(f); Com_Printf(S_COLOR_RED "%s is blank\n", fileName); return qfalse; } if ((fileLen + 1) >= AMMO_FILEBUFFER) { trap->FS_Close(f); Com_Printf(S_COLOR_RED "%s is too big - max file size %d bytes (%.2f kb)\n", fileName, AMMO_FILEBUFFER, AMMO_FILEBUFFER / 1024.0f); return qfalse; } trap->FS_Read(&fileBuffer, fileLen, f); fileBuffer[fileLen] = '\0'; trap->FS_Close(f); cJSON* json = cJSON_ParsePooled(fileBuffer, error, AMMO_ERRBUFFER); if (!json) { Com_Printf(S_COLOR_RED "%s: %s\n", fileName, error); return qfalse; } // Read each node in the json file from top level as an ammo_t cJSON* child; for (child = cJSON_GetFirstItem(json); child; child = cJSON_GetNextItem(child)) { if (numAmmoLoaded >= MAX_AMMO_TYPES) { Com_Printf("MAX_AMMO_TYPES exceeded\n"); break; } if (JKG_ParseSingleAmmo(child)) { numAmmoLoaded++; } } cJSON_Delete(json); return qtrue; }
static int GLua_JSON_GetNextObject( lua_State *L ) { // Check and make sure we're using a valid index if( !FJSON_ValidateNode( L, 1 ) ) { lua_pushnil(L); return 1; } // Get the next object now? int index = luaL_checkint( L, 1 ); cJSON *node = cJSON_GetNextItem( fJSON.jsonBinding.at( index ) ); if( !node ) { lua_pushnil( L ); return 1; } FJSON_PassLastElement( L, node ); return 1; }
static void ParseAmmoFile ( const char *fileText ) { int i = 0; cJSON *json = NULL; char jsonError[MAX_STRING_CHARS] = { 0 }; json = cJSON_ParsePooled (fileText, jsonError, sizeof (jsonError)); if ( json == NULL ) { Com_Printf (S_COLOR_RED "Error: %s\n", jsonError); } else { ammo_t *ammo = &ammoTable[0]; cJSON *jsonNode; cJSON *field; const char *string = NULL; for ( jsonNode = cJSON_GetFirstItem (json); jsonNode; jsonNode = cJSON_GetNextItem (jsonNode), ammo++, numAmmoLoaded++, i++ ) { field = cJSON_GetObjectItem (jsonNode, "name"); string = cJSON_ToString (field); if(string && string[0]) Q_strncpyz (ammo->name, string, sizeof (ammo->name)); field = cJSON_GetObjectItem (jsonNode, "ammoMax"); ammo->ammoMax = cJSON_ToNumber(field); ammo->ammoIndex = i; #ifdef _CGAME { cJSON *visual = cJSON_GetObjectItem (jsonNode, "visual"); if ( visual ) { cJSON *child = NULL; field = cJSON_GetObjectItem (visual, "model"); string = cJSON_ToString (field); if ( string && string[0] ) { ammo->model = trap->R_RegisterModel (string); } field = cJSON_GetObjectItem (visual, "fx"); string = cJSON_ToString (field); if ( string && string[0] ) { ammo->fx = trap->FX_RegisterEffect (string); } field = cJSON_GetObjectItem (visual, "deathfx"); string = cJSON_ToString (field); if ( string && string[0] ) { ammo->deathFx = trap->FX_RegisterEffect (string); } child = cJSON_GetObjectItem (visual, "miss"); if ( child ) { field = cJSON_GetObjectItem (child, "impactfx"); string = cJSON_ToString (field); if ( string && string[0] ) { ammo->missFx = trap->FX_RegisterEffect (string); } } child = cJSON_GetObjectItem (visual, "hit"); if ( child ) { field = cJSON_GetObjectItem (child, "impactfx"); string = cJSON_ToString (field); if ( string && string[0] ) { ammo->hitFx = trap->FX_RegisterEffect (string); } } } } #endif } } cJSON_Delete (json); Com_Printf ("Successfully loaded %d ammo types.\n", numAmmoLoaded); }