Example #1
0
std::FILE *fopen(const char *path, const char *mode) {
#ifdef _WIN32
    try {
        return _wfsopen(u8string(path).to_wide(true).c_str(), u8string(mode).to_wide(true).c_str(), _SH_DENYNO);
    } catch(unicode_conversion_error) {
        errno = EINVAL;
        return nullptr;
    }
#else
    return std::fopen(path, mode);
#endif
}
Example #2
0
std::vector<u8string> getargv() {
#if defined(_WIN32)
    int argc;
    wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
    std::vector<u8string> result;
    result.reserve(argc);
    for(int i = 0; i < argc; ++i)
        result.push_back(u8string::from_wide(wargv[i]));
    LocalFree(static_cast<void *>(wargv));
    return result;
#elif defined(__APPLE__) && defined(__MACH__)
    int argc = *_NSGetArgc();
    char **argv = *_NSGetArgv();
    std::vector<u8string> result;
    result.reserve(argc);
    for(int i = 0; i < argc; ++i)
        result.push_back(u8string(argv[i]));
    return result;
#else
    std::ifstream cmdline("/proc/self/cmdline");
    if(cmdline.is_open()) {
        std::vector<u8string> result;
        for(;;) {
            u8string argi;
            for(;;) {
                char c;
                if(cmdline.get(c))
                    if(c != '\0')
                        argi.push_back(c);
                    else
                        break;
                else if(cmdline.eof() && argi.empty())
                    return result;
                else
                    throw std::runtime_error("Unable to get commandline arguments");
            }
            result.push_back(std::move(argi));
        }
    } else
        throw std::runtime_error("Unable to get commandline arguments");
#endif
}
Example #3
0
u8string wstring_to_u8string( const wstring &s ) {
	wstring_u8string_converter converter;

	string bytes( converter.to_bytes( s ));
	return u8string( bytes.begin(), bytes.end() );
}
void ValueProcessor::dumpSimpleValue(util::NormalOStringStream &stream, ColumnType columnType, const void *data, uint32_t size, bool withType) {
	if (isArray(columnType)) {
		stream << "(support only simple type";
	} else {
		if (withType) {
			stream << "(" << getTypeName(columnType) << ")";
		}
		switch (columnType) {
		case COLUMN_TYPE_STRING :
			{
				util::NormalXArray<char> binary;
				binary.push_back(static_cast<const char *>(data) + ValueProcessor::getEncodedVarSize(size), size);
				binary.push_back('\0');
				stream << "'" << binary.data() << "'";
			}
			break;
		case COLUMN_TYPE_TIMESTAMP :
			{
				Timestamp val = *static_cast<const Timestamp *>(data);
				util::DateTime dateTime(val);
				dateTime.format(stream, false, false);
			}
			break;
		case COLUMN_TYPE_GEOMETRY :
		case COLUMN_TYPE_BLOB:
			{
				stream << "x'";
				util::NormalIStringStream iss(
						u8string(static_cast<const char8_t*>(data) + ValueProcessor::getEncodedVarSize(size), size));
				util::HexConverter::encode(stream, iss);
				stream << "'";
			}
			break;
		case COLUMN_TYPE_BOOL:
			if (*static_cast<const bool *>(data)) {
				stream << "TRUE";
			} else {
				stream << "FALSE";
			}
			break;
		case COLUMN_TYPE_BYTE: 
			{
				int16_t byteVal = 
					*static_cast<const int8_t *>(data);
				stream << byteVal;
			}
			break;
		case COLUMN_TYPE_SHORT:
			stream << *static_cast<const int16_t *>(data);
			break;
		case COLUMN_TYPE_INT:
			stream << *static_cast<const int32_t *>(data);
			break;
		case COLUMN_TYPE_LONG:
			stream << *static_cast<const int64_t *>(data);
			break;
		case COLUMN_TYPE_FLOAT:
			stream << *static_cast<const float *>(data);
			break;
		case COLUMN_TYPE_DOUBLE:
			stream << *static_cast<const double *>(data);
			break;
		case COLUMN_TYPE_NULL:
			stream << "NULL";
			break;
		default:
			stream << "(not implement)";
			break;
		}
	}
}
Example #5
0
    const Toolset& VcpkgPaths::get_toolset(const Build::PreBuildInfo& prebuildinfo) const
    {
        if (prebuildinfo.external_toolchain_file ||
            (!prebuildinfo.cmake_system_name.empty() && prebuildinfo.cmake_system_name != "WindowsStore"))
        {
            static Toolset external_toolset = []() -> Toolset {
                Toolset ret;
                ret.dumpbin = "";
                ret.supported_architectures = {
                    ToolsetArchOption{"", System::get_host_processor(), System::get_host_processor()}};
                ret.vcvarsall = "";
                ret.vcvarsall_options = {};
                ret.version = "external";
                ret.visual_studio_root_path = "";
                return ret;
            }();
            return external_toolset;
        }

#if !defined(_WIN32)
        Checks::exit_with_message(VCPKG_LINE_INFO, "Cannot build windows triplets from non-windows.");
#else
        const std::vector<Toolset>& vs_toolsets =
            this->toolsets.get_lazy([this]() { return VisualStudio::find_toolset_instances_preferred_first(*this); });

        std::vector<const Toolset*> candidates = Util::element_pointers(vs_toolsets);
        const auto tsv = prebuildinfo.platform_toolset.get();
        auto vsp = prebuildinfo.visual_studio_path.get();
        if (!vsp && !default_vs_path.empty())
        {
            vsp = &default_vs_path;
        }

        if (tsv && vsp)
        {
            Util::stable_keep_if(
                candidates, [&](const Toolset* t) { return *tsv == t->version && *vsp == t->visual_studio_root_path; });
            Checks::check_exit(VCPKG_LINE_INFO,
                               !candidates.empty(),
                               "Could not find Visual Studio instance at %s with %s toolset.",
                               vsp->u8string(),
                               *tsv);

            Checks::check_exit(VCPKG_LINE_INFO, candidates.size() == 1);
            return *candidates.back();
        }

        if (tsv)
        {
            Util::stable_keep_if(candidates, [&](const Toolset* t) { return *tsv == t->version; });
            Checks::check_exit(
                VCPKG_LINE_INFO, !candidates.empty(), "Could not find Visual Studio instance with %s toolset.", *tsv);
        }

        if (vsp)
        {
            const fs::path vs_root_path = *vsp;
            Util::stable_keep_if(candidates,
                                 [&](const Toolset* t) { return vs_root_path == t->visual_studio_root_path; });
            Checks::check_exit(VCPKG_LINE_INFO,
                               !candidates.empty(),
                               "Could not find Visual Studio instance at %s.",
                               vs_root_path.generic_string());
        }

        Checks::check_exit(VCPKG_LINE_INFO, !candidates.empty(), "No suitable Visual Studio instances were found");
        return *candidates.front();

#endif
    }
Example #6
0
void GameLoop::next_screen()
{
	if(nullptr != m_screen)
		m_screen->stop();

	if(nullptr == m_screen) {
		NetworkMode mode = the_context.configuration->network_mode;
		if(NetworkMode::SERVER == mode) {
			m_server_screen = m_screen_factory.create_server();
			m_screen = m_server_screen.get();
		}
		else {
			if(NetworkMode::LOCAL == mode) {
				m_client.reset(new LocalClient());
			}
			else {
				if(!the_context.configuration->server_url.has_value())
					throw GameException("Client mode requires server_url configuration.");

				auto net_client = std::make_unique<ENetClient>(the_context.configuration->server_url->c_str(),
															   the_context.configuration->port); // network implementation
				m_client.reset(new BasicClient(std::move(net_client)));
			}

			m_screen_factory.set_client(m_client.get());

			// If we want to play back a replay, feed it all to the client
			// and let the normal timing in the game loop take care of it.
			auto replay_path = the_context.configuration->replay_path;
			if(replay_path.has_value() &&
			   !std::filesystem::is_regular_file(replay_path.value())) {
				Log::error("Replay not found: %s", replay_path->u8string().c_str());
				replay_path.reset();
			}

			if(replay_path.has_value()) {
				std::ifstream stream{replay_path.value()};
				Journal journal = replay_read(stream);
				GameMeta meta = journal.meta();
				meta.winner = NOONE; // this is currently necessary to prevent early exit
				m_client->send_reset(meta);
				m_client->game_start();
				for(InputDiscovered id : journal.inputs()) {
					m_client->send_input(id.input);
				}
				m_game_screen = m_screen_factory.create_game();
				m_screen = m_game_screen.get();

				// We do not copy-save the same game again in replay mode.
				the_context.configuration->autorecord = false;
			}
			else {
				m_menu_screen = m_screen_factory.create_menu();
				m_screen = m_menu_screen.get();
			}
		}

		// debug
		//DrawPink pink_draw(m_sdl_factory, 255, 0, 255);
		//m_pink_screen = std::make_unique<PinkScreen>(std::move(pink_draw));
		//m_screen = m_pink_screen.get();
	} else
	if(ServerScreen* serv = dynamic_cast<ServerScreen*>(m_screen)) {
		m_server_screen.release();
		m_screen = nullptr;
	} else
	if(MenuScreen* menu = dynamic_cast<MenuScreen*>(m_screen)) {
		if(MenuScreen::Result::PLAY == menu->result()) {
			m_client->game_start(); // create game state from meta info
			m_game_screen = m_screen_factory.create_game();
			m_transition_screen = m_screen_factory.create_transition(*menu, *m_game_screen);
			m_screen = m_transition_screen.get();
		} else
		if(MenuScreen::Result::QUIT == menu->result()) {
			m_menu_screen.release();
			m_screen = nullptr;
		}
	} else
	if(GameScreen* game = dynamic_cast<GameScreen*>(m_screen)) {
		if(the_context.configuration->replay_path.has_value()) {
			// After a replay, just exit.
			// NOTE: if we did not, we would have to restore the autorecord config flag.
			m_game_screen.release();
			m_screen = nullptr;
		}
		else {
			// Go back to menu
			m_menu_screen = m_screen_factory.create_menu();
			m_transition_screen = m_screen_factory.create_transition(*game, *m_menu_screen);
			m_screen = m_transition_screen.get();
		}
	} else
	if(TransitionScreen* transition = dynamic_cast<TransitionScreen*>(m_screen)) {
		m_screen = &transition->successor();
		m_transition_screen.release();
		// BUG! We keep the predecessor screen around unnecessarily.
	} else
	if(PinkScreen* pink = dynamic_cast<PinkScreen*>(m_screen)) {
		if(m_pink_screen.get() == pink) {
			DrawPink creme_draw(250, 220, 220);
			m_creme_screen = std::make_unique<PinkScreen>(std::move(creme_draw));
			m_transition_screen = m_screen_factory.create_transition(*pink, *m_creme_screen);
			m_screen = m_transition_screen.get();
		}
		else {
			DrawPink pink_draw(255, 0, 255);
			m_pink_screen = std::make_unique<PinkScreen>(std::move(pink_draw));
			m_transition_screen = m_screen_factory.create_transition(*pink, *m_pink_screen);
			m_screen = m_transition_screen.get();
		}
	}
	else {
		assert(false); // unknown type of m_screen
	}
}
Example #7
0
    Expected<VcpkgPaths> VcpkgPaths::create(const fs::path& vcpkg_root_dir, const std::string& default_vs_path)
    {
        std::error_code ec;
        const fs::path canonical_vcpkg_root_dir = fs::stdfs::canonical(vcpkg_root_dir, ec);
        if (ec)
        {
            return ec;
        }

        VcpkgPaths paths;
        paths.root = canonical_vcpkg_root_dir;
        paths.default_vs_path = default_vs_path;

        if (paths.root.empty())
        {
            Metrics::g_metrics.lock()->track_property("error", "Invalid vcpkg root directory");
            Checks::exit_with_message(VCPKG_LINE_INFO, "Invalid vcpkg root directory: %s", paths.root.string());
        }

        paths.packages = paths.root / "packages";
        paths.buildtrees = paths.root / "buildtrees";

        const auto overriddenDownloadsPath = System::get_environment_variable("VCPKG_DOWNLOADS");
        if (auto odp = overriddenDownloadsPath.get())
        {
            auto asPath = fs::u8path(*odp);
            if (!fs::stdfs::is_directory(asPath))
            {
                Metrics::g_metrics.lock()->track_property("error", "Invalid VCPKG_DOWNLOADS override directory.");
                Checks::exit_with_message(
                    VCPKG_LINE_INFO,
                    "Invalid downloads override directory: %s; "
                    "create that directory or unset VCPKG_DOWNLOADS to use the default downloads location.",
                    asPath.u8string());
            }

            paths.downloads = fs::stdfs::canonical(std::move(asPath), ec);
            if (ec)
            {
                return ec;
            }
        }
        else
        {
            paths.downloads = paths.root / "downloads";
        }

        paths.ports = paths.root / "ports";
        paths.installed = paths.root / "installed";
        paths.triplets = paths.root / "triplets";
        paths.scripts = paths.root / "scripts";

        paths.tools = paths.downloads / "tools";
        paths.buildsystems = paths.scripts / "buildsystems";
        paths.buildsystems_msbuild_targets = paths.buildsystems / "msbuild" / "vcpkg.targets";

        paths.vcpkg_dir = paths.installed / "vcpkg";
        paths.vcpkg_dir_status_file = paths.vcpkg_dir / "status";
        paths.vcpkg_dir_info = paths.vcpkg_dir / "info";
        paths.vcpkg_dir_updates = paths.vcpkg_dir / "updates";

        paths.ports_cmake = paths.scripts / "ports.cmake";

        return paths;
    }
Example #8
0
 u8string substr_pos(std::size_t s, std::size_t e) const {
     auto starts = substr(s);
     auto ends = starts.substr(e - s);
     return u8string(starts.begin(), ends.begin());
 }
Example #9
0
 /// Safe substring against Unicode code point counts. The result
 /// is undefined if the end marker is smaller than the start marker.
 u8string substr(std::size_t s) const {
     auto pos = begin(), e = end();
     for (; s && pos != e; --s, ++pos)
         ;
     return u8string(pos, e);
 }