Ejemplo n.º 1
0
SaveGame::SaveGame(const Server* ptServer, const Client* ptClient, const Player* ptPlayer) {
	if(ptServer == nullptr) {
		// Multiplayer game saved onto local disk
		return; // TODO
	}
	// Save all files as "test.sav" for now.
	File* ptFile = trap->OpenFile("save/test.sav", "wb");
	if(ptFile == nullptr) {
		R_Error("Couldn't open save file \"save/test.sav\"\n");
		return;
	}

	// FIXME: Need to write more code involving towns...
	string sTown = "Survivor's Camp";
	cJSONStream* ptStream = cJSON_Stream_New(MAX_SAVE_DEPTH, true, MAX_SAVE_SIZE, MAX_SAVE_SIZE);

	{
		// --- Save Start ---
		cJSON_Stream_BeginObject(ptStream, "SAVE");

		{
			// --- Header Data ---
			cJSON_Stream_BeginObject(ptStream, "HEADER");

			cJSON_Stream_WriteString(ptStream, "savename", "Test Save");
			cJSON_Stream_WriteString(ptStream, "spawntown", sTown.c_str());

			// --- Header Data ---
			cJSON_Stream_EndBlock(ptStream);
		}

		{
			// --- Quest Data ---
			cJSON_Stream_BeginObject(ptStream, "QUEST");

			for(auto it = ptServer->ptQuestManager->GetStateStartIterator();
				it != ptServer->ptQuestManager->GetStateEndIterator();
				++it) {
				if(it->second == CQS_QUEST_COMPLETE) { // Mark the quest as being complete in a previous game
					cJSON_Stream_WriteInteger(ptStream, it->first.c_str(), CQS_QUEST_COMPLETEPREVIOUS);
				} else {
					cJSON_Stream_WriteInteger(ptStream, it->first.c_str(), it->second);
				}
			}

			// --- Quest Data ---
			cJSON_Stream_EndBlock(ptStream);
		}

		// --- Save Start ---
		cJSON_Stream_EndBlock(ptStream);
	}

	const char* sFileContents = cJSON_Stream_Finalize(ptStream);
	trap->WriteFile(ptFile, sFileContents);
	trap->CloseFile(ptFile);
}
Ejemplo n.º 2
0
void JKG_Bans_SaveBans()
{
	cJSONStream *stream;
	const char *buffer;
	fileHandle_t f;
	banentry_t *entry;
	unsigned int curr = time(NULL);
	
	stream = cJSON_Stream_New(8, 1, 0, 0);
	cJSON_Stream_BeginObject(stream, NULL); // Root object
	cJSON_Stream_WriteInteger(stream, "nextid", nextBanId);
	cJSON_Stream_BeginArray(stream, "bans"); // Start bans array
	for(entry = bans; entry; entry = entry->next) {
		if (entry->expireTime && curr >= entry->expireTime) {
			continue;	// Don't save expired bans
		}
		cJSON_Stream_BeginObject(stream, NULL); // Ban entry
		cJSON_Stream_WriteInteger(stream, "id", entry->id );
		cJSON_Stream_WriteInteger(stream, "mask", entry->mask );
		cJSON_Stream_WriteInteger(stream, "expire", entry->expireTime);
		cJSON_Stream_WriteString(stream, "reason", entry->banreason);
		
		cJSON_Stream_BeginArray(stream, "ip"); // IP
		cJSON_Stream_WriteInteger(stream, NULL, entry->ip[0]);
		cJSON_Stream_WriteInteger(stream, NULL, entry->ip[1]);
		cJSON_Stream_WriteInteger(stream, NULL, entry->ip[2]);
		cJSON_Stream_WriteInteger(stream, NULL, entry->ip[3]);
		cJSON_Stream_EndBlock(stream);

		cJSON_Stream_EndBlock(stream); // Ban entry
	}
	cJSON_Stream_EndBlock(stream);	// Bans array
	cJSON_Stream_EndBlock(stream);	// Root object
	buffer = cJSON_Stream_Finalize(stream);

	trap_FS_FOpenFile(g_banfile.string, &f, FS_WRITE);
	trap_FS_Write(buffer, strlen(buffer), f);
	trap_FS_FCloseFile(f);

	free((void *)buffer);
}
Ejemplo n.º 3
0
static int GLua_JSON_RegisterStream( lua_State *L )
{
	fJSON.streamHandle = cJSON_Stream_New( luaL_checkinteger( L, 1 ), lua_toboolean( L, 2 ), 0, 0 );
	return 0;
}