Skip to content

Commit

Permalink
fix: unicode input with windows 7
Browse files Browse the repository at this point in the history
Unfortunately replxx doesn't seem to support
unicode i/o on Windows 7 so we're disabling
it there.
  • Loading branch information
Xenapte committed Feb 9, 2024
1 parent 50f6121 commit ae701a3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
5 changes: 3 additions & 2 deletions BallanceMMOClient/console_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void console_window::run() {
std::lock_guard lk(mutex_);
for (const auto& [text, color]: previous_msg_) bmmo::Printf(color, text.c_str());
}
while (true) {
while (!bmmo::LOWER_THAN_WIN10 || running()) {
std::string line;
/*wchar_t wc;
do {
Expand All @@ -134,7 +134,7 @@ void console_window::run() {
cleanup();
break;
};
if (!running_)
if (!running())
break;
std::string cmd = "ballancemmo";
std::vector<std::string> args;
Expand Down Expand Up @@ -197,6 +197,7 @@ bool console_window::cleanup() {
bool console_window::hide() {
if (!running_ || !owned_console_)
return false;
if (bmmo::LOWER_THAN_WIN10) return cleanup();
bmmo::console::end_input();
return true;
}
Expand Down
20 changes: 16 additions & 4 deletions BallanceMMOCommon/src/utility/console.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <iostream>
#include <algorithm>
#include <replxx.hxx>
//#ifdef _WIN32
//#include <io.h>
//#include <fcntl.h>
//#endif
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#endif
#include "entity/globals.hpp"
#include "utility/console.hpp"
#include "utility/misc.hpp"
Expand Down Expand Up @@ -112,6 +112,18 @@ const std::vector<std::string> console::get_command_hints(bool fuzzy_matching, c
};

bool console::read_input(std::string &buf) {
#ifdef _WIN32
if (LOWER_THAN_WIN10) {
std::ignore = _setmode(_fileno(stdin), _O_U16TEXT);
std::cout << "\r> " << std::flush;
std::wstring wbuf;
bool success = bool(std::getline(std::wcin, wbuf));
buf = bmmo::string_utils::ConvertWideToUtf8(wbuf);
if (auto pos = buf.rfind('\r'); pos != std::string::npos)
buf.erase(pos);
return success;
}
#endif
replxx_instance.print("\r\033[0K");
auto input_cstr = replxx_instance.input("\r> ");
if (!input_cstr)
Expand Down
40 changes: 20 additions & 20 deletions BallanceMMOCommon/src/utility/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ namespace bmmo {
if (el > text && el[-1] == '\n')
text[el - text - 1] = '\0';
}



void DebugOutput(ESteamNetworkingSocketsDebugOutputType eType, const char* pszMsg, int ansiColor) {
// SteamNetworkingMicroseconds time = SteamNetworkingUtils()->GetLocalTimestamp() - init_timestamp_;
auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
Expand All @@ -74,35 +73,36 @@ namespace bmmo {
fflush(stdout);
fflush(stderr);
if (log_file) fflush(log_file);
exit(2);
} else {
return exit(2);
}
else if (!isatty(fileno(stdout))) {
printf("[%s] %s\n", timeStr, pszMsg);
}
#ifdef _WIN32
else if (LOWER_THAN_WIN10) {
printf("\r[%s] %s\n> ", timeStr, bmmo::string_utils::utf8_to_ansi(pszMsg).c_str());
}
#endif
else {
// printf("\r%10.2f %s\n> ", time * 1e-6, pszMsg);
if (!isatty(fileno(stdout))) {
printf("\r[%s] %s\n", timeStr, pszMsg);
fflush(stdout);
return;
}
// bmmo::replxx_instance.invoke(replxx::Replxx::ACTION::CLEAR_SELF, '\0');
if (ansiColor == bmmo::ansi::Reset
// || LOWER_THAN_WIN10 // ansi sequences cannot be used on windows versions below 10
// // possible with replxx
)
if (ansiColor == bmmo::ansi::Reset)
replxx_instance.print("\r[%s] %s\n", timeStr, pszMsg);
else
replxx_instance.print("\r[%s] %s%s\033[m\n", timeStr, bmmo::ansi::get_escape_code(ansiColor).c_str(), pszMsg);
fflush(stdout);
// bmmo::replxx_instance.invoke(replxx::Replxx::ACTION::REPAINT, '\0');
replxx_instance.print("\r[%s] %s%s\033[m\n", timeStr,
bmmo::ansi::get_escape_code(ansiColor).c_str(), pszMsg);
}
fflush(stdout);
// bmmo::replxx_instance.invoke(replxx::Replxx::ACTION::REPAINT, '\0');
}

void DebugOutput(ESteamNetworkingSocketsDebugOutputType eType, const char* pszMsg) {
DebugOutput(eType, pszMsg, bmmo::ansi::Reset);
}

void close_log() {
if (log_file) {
fclose(log_file);
log_file = nullptr;
}
if (!log_file) return;
fclose(log_file);
log_file = nullptr;
}
}

0 comments on commit ae701a3

Please sign in to comment.