Beispiel #1
0
void configuration::sanity_check_values()
{
	if(conf[DTLS_BIND_PORT] == 0 || conf[DTLS_OUTGOING_PORT] == 0) {
		// use random but persistent ports for these
		std::ofstream conffile(conf[CONFIG_FILE], std::ios_base::out | std::ios_base::binary | std::ios_base::app);
		if(conf[DTLS_BIND_PORT] == 0) {
			uint16_t port = ntohs(get_random_port());
			iout() << "Assigned DTLS_BIND_PORT random port " << port;
			conffile << "\nDTLS_BIND_PORT=" << port << '\n';
			assign_value(DTLS_BIND_PORT, port);
		}
		if(conf[DTLS_OUTGOING_PORT] == 0) {
			uint16_t port = ntohs(get_random_port());
			iout() << "Assigned DTLS_OUTGOING_PORT random port " << port;
			conffile << "\nDTLS_OUTGOING_PORT=" << port << '\n';
			assign_value(DTLS_OUTGOING_PORT, port);
		}
	}
	if(conf[DTLS_BIND_PORT] == conf[DTLS_OUTGOING_PORT] || conf[DTLS_BIND6_PORT] == conf[DTLS_OUTGOING_PORT]) {
		eout() << "DTLS_BIND_PORT or DTLS_OUTGOING_PORT not initialized to separate valid ports, these should have been set to random ports during installation."
			<< " You must set DTLS_BIND_PORT and DTLS_OUTGOING_PORT to separate port numbers in the snow configuration file."
			   << " If you have more than one device try to choose different ports for each device.";
		abort();
	}
	check_port(conf[DTLS_OUTGOING_PORT], "DTLS_OUTGOING_PORT");
	check_port(conf[DTLS_BIND_PORT], "DTLS_BIND_PORT");
	check_port(conf[DTLS_BIND6_PORT], "DTLS_BIND6_PORT");
	check_port(conf[DHT_PORT], "DHT_PORT");
	check_port(conf[NAMESERV_PORT], "NAMESERV_PORT");
	check_nonzero(conf[NAMESERV_TIMEOUT_SECS], "NAMESERV_TIMEOUT_SECS");
	check_nonzero(conf[DTLS_IDLE_TIMEOUT_SECS], "DTLS_IDLE_TIMEOUT_SECS");
	check_nonzero(conf[HEARTBEAT_SECONDS], "HEARTBEAT_SECONDS");
	check_nonzero(conf[HEARTBEAT_RETRIES], "HEARTBEAT_RETRIES");
	if(conf[NAT_IP_GRACE_PERIOD_SECONDS] < 1800) {
		wout() << "NAT IP grace period of " << conf[NAT_IP_GRACE_PERIOD_SECONDS] << " from configuration file is too short, using minimum grace period of 1800 seconds";
		assign_value(NAT_IP_GRACE_PERIOD_SECONDS, 1800);
	}
	if(conf[DHT_BOOTSTRAP_TARGET]==0) {
		wout() << "DHT_BOOTSTRAP_TARGET cannot be zero, using default value";
		assign_value(DHT_BOOTSTRAP_TARGET, 6);
	}
	if(conf[DHT_MAX_PEERS] <= 3) {
		wout() << "DHT_MAX_PEERS cannot be " << conf[DHT_MAX_PEERS] << ", must be at least 4, using default value of 99";
		assign_value(DHT_MAX_PEERS, 99);
	}
	if(conf[NATPOOL_NETMASK_BITS] > 20 || conf[NATPOOL_NETMASK_BITS] < 4) {
		eout() << "NATPOOL_NETMASK_BITS must be between 4 and 20";
		abort();
	}
	uint32_t addr, netmask = ~htonl((1 << (32 - conf[NATPOOL_NETMASK_BITS])) - 1);
	if(inet_pton(AF_INET, conf[NATPOOL_NETWORK].c_str(), &addr) != 1 || (addr & ~netmask) != 0) {
		eout() << "NATPOOL_NETWORK/NATPOOL_NETMASK_BITS as " << conf[NATPOOL_NETWORK] << "/" << conf[NATPOOL_NETMASK_BITS] << " is not a valid subnet.";
		abort();
	}
	if(conf[VIRTUAL_INTERFACE_MTU] > 65535 || conf[VIRTUAL_INTERFACE_MTU] < 576) {
		eout() << "VIRTUAL_INTERFACE_MTU cannot be " << conf[VIRTUAL_INTERFACE_MTU];
		abort();
	}
}
void SpriteHandler::LoadSpritesFromList(boost::shared_ptr<SDL_Renderer> ren,
	boost::shared_ptr<std::map<std::string, boost::shared_ptr< Sprite > > > sprmap)
{
	/* open config then parse config */

	auto s = std::string();
	std::ifstream conffile("sprites.list");

	if (conffile.is_open()) // File exists
	{
		SDL_Log("sprites.list opened...");
		while (getline(conffile, s))
		{
			/* C++ does not do switch on strings, use if/else if/else if */
			if (s[0] == '#') continue; // comment

			//if (s.compare("@SPRITE") == 0)
			//{
			//	SDL_Log("Loading sprite...");
			//	Sprite* spr;
			//
			//	std::string inln;
			//	getline(conffile, inln);
			//	spr = new Sprite(inln, ren);
			//
			//	getline(conffile, spr->name);
			//
			//	getline(conffile, inln);
			//	spr->rows = atoi(inln.c_str());
			//
			//	getline(conffile, inln);
			//	spr->cols = atoi(inln.c_str());
			//
			//	spr->framewidth = spr->w / spr->cols;
			//	spr->frameheight = spr->h / spr->rows;
			//
			//	sprmap->insert(std::pair<std::string, Sprite*>(spr->name, spr));
			//}

			if (s.compare("@TILE") == 0)
			{
				SDL_Log("Loading tile...");

				auto inln = std::string();
				getline(conffile, inln);

				auto spr = boost::shared_ptr < Sprite > {boost::make_shared< Tile >(inln, ren)};

				getline(conffile, spr->name);

				getline(conffile, inln);
				spr->rows = atoi(inln.c_str());

				getline(conffile, inln);
				spr->cols = atoi(inln.c_str());

				spr->framewidth = spr->w / spr->cols;
				spr->frameheight = spr->h / spr->rows;

				sprmap->insert(std::pair<std::string,boost::shared_ptr< Sprite > >(spr->name, spr));
			}

			if (s.compare("@PLAYER") == 0)
			{
				SDL_Log("Loading player...");

				auto inln = std::string();
				getline(conffile, inln);
				auto spr = boost::shared_ptr < Sprite > { boost::make_shared< Sprite >(inln, ren)};

				getline(conffile, spr->name);

				getline(conffile, inln);
				spr->rows = atoi(inln.c_str());

				getline(conffile, inln);
				spr->cols = atoi(inln.c_str());

				spr->framewidth = spr->w / spr->cols;
				spr->frameheight = spr->h / spr->rows;

				sprmap->insert(std::pair<std::string, boost::shared_ptr< Sprite > >(spr->name, spr));
			}

			if (s.compare("@CURSOR") == 0)
			{
				SDL_Log("Loading cursor...");

				auto inln = std::string();
				getline(conffile, inln);
				auto spr = boost::shared_ptr < Sprite > { boost::make_shared< Cursor >(inln, ren)};

				getline(conffile, spr->name);

				getline(conffile, inln);
				spr->rows = 1;

				getline(conffile, inln);
				spr->cols = 1;

				spr->framewidth = spr->w / spr->cols;
				spr->frameheight = spr->h / spr->rows;

				sprmap->insert(std::pair<std::string, boost::shared_ptr< Sprite > >(spr->name, spr));;
			}

			if (s.compare("@PROJECTILE") == 0)
			{
				SDL_Log("Loading projectile...");

				auto inln = std::string();
				getline(conffile, inln);
				auto spr = boost::shared_ptr < Sprite > { boost::make_shared< Bullet >(inln, ren)};

				getline(conffile, spr->name);
				getline(conffile, inln);
				spr->rows = 1;

				getline(conffile, inln);
				spr->cols = 1;

				spr->framewidth = spr->w / spr->cols;
				spr->frameheight = spr->h / spr->rows;

				sprmap->insert(std::pair<std::string, boost::shared_ptr< Sprite > >(spr->name, spr));
			}

			if (s.compare("@ZOMBIE") == 0)
			{
				SDL_Log("Loading zombie...");

				auto inln = std::string();
				getline(conffile, inln);
				auto spr = boost::shared_ptr < Sprite > {boost::make_shared< Zombie >(inln, ren)};

				getline(conffile, spr->name);

				getline(conffile, inln);
				spr->rows = atoi(inln.c_str());;

				getline(conffile, inln);
				spr->cols = atoi(inln.c_str());;

				spr->framewidth = spr->w / spr->cols;
				spr->frameheight = spr->h / spr->rows;

				sprmap->insert(std::pair<std::string, boost::shared_ptr< Sprite > >(spr->name, spr));
			}

			if (s.compare("@ZOMBIESPAWNER") == 0)
			{
				SDL_Log("Loading zombie spawner...");

				/* name */
				auto inln = std::string();
				getline(conffile, inln);
				auto spr = boost::shared_ptr < Sprite > {boost::make_shared< Zombiespawner >(inln, ren)};

				getline(conffile, spr->name);

				/* rows */
				getline(conffile, inln);
				spr->rows = atoi(inln.c_str());

				/* columns */
				getline(conffile, inln);
				spr->cols = atoi(inln.c_str());

				spr->framewidth = spr->w / spr->cols;
				spr->frameheight = spr->h / spr->rows;

				sprmap->insert(std::pair<std::string, boost::shared_ptr< Sprite > >(spr->name, spr));
			}

			if (s.compare("@MAGAZINE") == 0)
			{
				SDL_Log("Loading magazine...");

				/* name */
				auto inln = std::string();
				getline(conffile, inln);
				auto spr = boost::shared_ptr < Sprite > {boost::make_shared< Magazine >(inln, ren)};

				getline(conffile, spr->name);

				/* rows */
				getline(conffile, inln);
				spr->rows = atoi(inln.c_str());

				/* columns */
				getline(conffile, inln);
				spr->cols = atoi(inln.c_str());

				spr->framewidth = spr->w / spr->cols;
				spr->frameheight = spr->h / spr->rows;

				sprmap->insert(std::pair<std::string, boost::shared_ptr< Sprite > >(spr->name, spr));
			}


			/* etc.  */



		}

		conffile.close();

	}
	else // File does not exist
	{
		/* PANIC */
	}
}