bool updateRegistry(Json::Value &jsonRoot, Json::Value &outputRoot)
{
	bool success = true;
	for (Json::ValueIterator iter = jsonRoot.begin(); iter != jsonRoot.end(); iter++) {
		Json::Value solutionsRoot = jsonRoot.get(iter.memberName(), Json::Value());
		Json::Value solutionsOutput;
		for(Json::ValueIterator itr = solutionsRoot["settings"].begin() ; itr != solutionsRoot["settings"].end() ; itr++ )
		{
			bool missingValue = false;
			Json::Value currentOption;
			const char * path = solutionsRoot["options"]["path"].asCString();
			const char * valueName = itr.memberName();
			long valueToSet = solutionsRoot["settings"][valueName].asUInt();
			
		    wstring updatedPath = toWideChar(path);
			wstring updatedValueName = toWideChar(valueName);
			long value = -1;
			try
			{
				value = getDwordValue(hKeyFromString(solutionsRoot["options"]["hKey"].asCString()), updatedPath.c_str(), updatedValueName.c_str());
			} catch (int exCode)
			{
				currentOption["statusCode"] = exCode;
				currentOption["oldValue"] = Json::Value();
				currentOption["newValue"] = Json::Value();
				success = false;
				missingValue = true;
			}

			bool valueSet = setDwordValue(hKeyFromString(solutionsRoot["options"]["hKey"].asCString()), updatedPath.c_str(), updatedValueName.c_str(), valueToSet);
			if (missingValue)
			{
				currentOption["oldValue"] = Json::Value();
			} else {
				currentOption["oldValue"] = value;
			}
			try
			{
				currentOption["newValue"] = getDwordValue(hKeyFromString(solutionsRoot["options"]["hKey"].asCString()), updatedPath.c_str(), updatedValueName.c_str());
			} catch (int exceptionCode) {
				// TODO Handle gracefully.
			}
			if (!valueSet)
			{
				success = false;
				currentOption["newValue"] = value;
			}
		
			solutionsOutput[valueName] = currentOption;
		}
		outputRoot[iter.memberName()]["results"] = solutionsOutput;
	}
	return success;
}
Пример #2
0
void JsonArchive::DumpJsonTree(Json::Value* root, int depth)
{
	if (root == NULL)
	{
		root = &m_Root;
	}

	depth += 1;
	printf( " {type=[%d], size=%d}", root->type(), root->size() );

	if( root->size() > 0 )
	{
		printf("\n");
		for( Json::ValueIterator itr = root->begin() ; itr != root->end() ; itr++ )
		{
			// Print depth.
			for( int tab = 0 ; tab < depth; tab++)
			{
			   printf("-");
			}
			printf(" subvalue(%s) -", itr.memberName());
			DumpJsonTree( &(*itr), depth);
		}
	}
	else
	{
		printf(" ");
		PrintJsonValue(root);
		printf( "\n" );
	}
}
Пример #3
0
void generateLevel(const std::string &level) {
	cleanLevel();

	Json::Value root;   // will contains the root value after parsing.
	curLevel = new Level(level, &root);
	if(!loadJSONLevel(level, root)) {
		printf("Error parsing JSON file for level %s!\n", level.c_str());
		generateLevel("worldmap");
		return; // TODO: Load from a default?
	}
	Json::Value tileset = root["tileset"];
	Json::Value customEntities = root["entities"];
	
	char line[201] = "";
	std::string filename = "levels/"+level+".lvl";
	if(DEBUG) printf("Reading file %s\n", filename.c_str());
	
	FILE *fp = fopen(filename.c_str(), "r");
	if(!fp) {
		printf("ERROR: Level %s not found!\n", filename.c_str());
		generateLevel("worldmap");
		return;
	}
	
	char blockC[] = "-";
	int x=0, y=0, biggestX=0, biggestY=0;
	Entity *ent;
	for(y=0; fgets(line, sizeof(line), fp); y++) {
		if(DEBUG) printf("Read line: %s", line);
		for(x=0; line[x]; x++) {
			if(line[x] <= 32) continue;
			blockC[0] = line[x];
			if(!!tileset[blockC]) {
				if(DEBUG) printf("Spawning block(%d,%d) type(%d,%c)\n", x, y, line[x], line[x]);
				
				ent = constructEntity(tileset[blockC], x, y);
				posLookup[x][y] = ent; // TODO: Remove from list, ensure consistency across block movements
				setEntityProperties(ent, tileset[blockC]);

				if(x > biggestX) biggestX = x;
				if(y > biggestY) biggestY = y;
			} else {
				printf("Unknown Block: '%c' (%d)\n", line[x], line[x]);
			}
		}
		memset(line, '\0', sizeof(line));
	}
	curLevel->w = max((biggestX+1)*BLOCK_SIZE, WIDTH);
	curLevel->h = max((biggestY+1)*BLOCK_SIZE, HEIGHT);

	char sPos[20];
	for(Json::ValueIterator iter = customEntities.begin(); iter != customEntities.end(); iter++ ) {
		strncpy(sPos, iter.memberName(), sizeof(sPos)-1);
		x = atoi(strtok(sPos+1, ","));
		y = atoi(strtok(NULL, ")"));
		if((ent = posLookup[x][y]) == NULL) {
			printf("Level setup error: Entity(%d,%d) not found!\n", x, y);
			continue;
		}
		if(DEBUG) printf("Applying Entity(%d,%d) specifics...\n", x, y);
		setEntityProperties(ent, *iter);
	}

	compileBackground(renderer);
}
Пример #4
0
void Socket::socketThread() {
    Json::Reader reader;
    
	bool running = true;
    int addrinfo_status, connect_status, socket_status = 0;
    
	while ( running ) {
        
		
        
		if ( !socketfd || socket_status ) {
			struct addrinfo host_info;
			struct addrinfo *host_info_list;
            
			// Close the existing socket, if it has been left open.
			if ( socketfd ) {
				close( socketfd );
				socketfd = NULL;
			}
            
			// Establish connection
			memset( &host_info, 0, sizeof host_info );
            
			host_info.ai_family = AF_UNSPEC;
			host_info.ai_socktype = SOCK_STREAM;
            
			addrinfo_status = getaddrinfo( hostname, port, &host_info, &host_info_list );
            
			if ( addrinfo_status ) {
				std::cout << "couldn't get address" << std::endl ;
				sleep(1);
				continue;
			}
            
			socketfd = socket( host_info_list->ai_family, host_info_list->ai_socktype, host_info_list->ai_protocol );
            
			if ( socketfd == -1 ) {
				socket_status = -1;
				std::cout << "socket error" << std::endl ;
				sleep(1);
				continue;
			}
            
			connect_status = connect( socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen );
            
			if ( connect_status ) {
				socket_status = -1;
				std::cout << "connect error" << std::endl ;
				sleep(1);
				continue;
			}
            
			socket_status = 0;
		}
        
		int max_message_size = 1000000;
        
		ssize_t bytes_recieved;
        
		struct hss_header header;
		string buffer;
        
		bytes_recieved = recv( socketfd, &header, sizeof header, 0 );
		
		//std::cout << "received " << bytes_recieved << std::endl;
        
		if ( bytes_recieved == sizeof header ) {
            
			// Parse the header ( what there is of it )
			header.string_bytes = ntohl( header.string_bytes );
            
			if ( header.string_bytes > max_message_size ) {
				// Bad header, f**k it.
				socket_status = -3;
				std::cout << "bad header, disconnecting";
				continue;
			}
            
			buffer.resize( header.string_bytes );
            
			size_t b = recv( socketfd, (void * ) buffer.c_str(), header.string_bytes, MSG_WAITALL );
            
			if ( b != header.string_bytes ) {
                
			}
            
            Json::Value msg;
            
			//std::cout << buffer << std::endl;
            reader.parse( buffer, msg );
            
            Json::Value set = msg["set"];
            if ( set.isObject() ) {
                
                Json::Value set = msg["set"];
                for ( Json::ValueIterator i = set.begin(); i != set.end(); i ++ ) {
                    string k = (string) i.memberName();
                    horten->set( set[k], k, flags );
                }
            }

		} else if (bytes_recieved == 0) {
			std::cout << "host shut down." << std::endl ;
			socket_status = -3;
			sleep(1);
		} else if (bytes_recieved == -1 ) {
			std::cout << "receive error! " << errno << std::endl ;
			socket_status = -4;
			sleep(1);
		}
	}
}