Skip to content

Commit

Permalink
feat: use gnu readline for console input
Browse files Browse the repository at this point in the history
Only applies to UNIX-like systems. This fixes unicode
input and provides console history.
  • Loading branch information
Xenapte committed Dec 23, 2023
1 parent fa4f72d commit 5db210b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions BallanceMMOCommon/include/role/role.hpp
Expand Up @@ -19,6 +19,8 @@
#else
#include <sys/ioctl.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include <steam/steamnetworkingsockets.h>
#include <steam/isteamnetworkingutils.h>
Expand Down Expand Up @@ -57,6 +59,8 @@ class role {
GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
std::ignore = _setmode(_fileno(stdin), _O_U16TEXT);
#else
using_history();
#endif
init_timestamp_ = SteamNetworkingUtils()->GetLocalTimestamp();
init_time_t_ = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
Expand Down
12 changes: 11 additions & 1 deletion BallanceMMOCommon/src/utility/console.cpp
@@ -1,5 +1,9 @@
#include <iostream>
#include <algorithm>
#ifndef _WIN32
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "utility/console.hpp"
#include "utility/string_utils.hpp"

Expand Down Expand Up @@ -43,7 +47,13 @@ bool console::read_input(std::string &buf) {
buf.erase(pos);
return success;
#else
return bool(std::getline(std::cin, buf));
auto input_cstr = readline("\r> ");
if (!input_cstr)
return false;
buf.assign(input_cstr);
add_history(buf.c_str());
return std::cin.good();
// return bool(std::getline(std::cin, buf));
#endif
}

Expand Down
5 changes: 5 additions & 0 deletions BallanceMMOServer/CMakeLists.txt
Expand Up @@ -66,6 +66,11 @@ target_include_directories(yaml-cpp SYSTEM INTERFACE ${_inc}) # suppress warning
set_target_properties(GameNetworkingSockets yaml-cpp PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${BMMO_LIB_DIR})
# set_target_properties(yaml-cpp PROPERTIES VERSION "" SOVERSION "")
if (NOT WIN32)
target_link_libraries(BallanceMMOServer readline)
target_link_libraries(BallanceMMOMockClient readline)
target_link_libraries(BallanceMMORecordParser readline)
endif()

if (MSVC)
set(compile_options /Wall)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -46,6 +46,7 @@ BallanceMMO is a project which brings online experiences to Ballance.
- OpenSSL 1.1.x, plus ed25519-donna and curve25519-donna. (Valve GNS has made some minor changes, so the source is included in this project.)
- libsodium
- Google protobuf 2.6.1+ (included in submodule)
- GNU Readline (for UNIX-like systems)
- Dev pack of BallanceModLoader (client-side, [*release page*](https://github.com/Gamepiaynmo/BallanceModLoader/releases)) and (optionally) [BallanceModLoaderPlus](https://github.com/doyaGu/BallanceModLoaderPlus)

## Building server
Expand All @@ -58,20 +59,22 @@ BallanceMMO is a project which brings online experiences to Ballance.
git clone https://github.com/Swung0x48/BallanceMMO.git --recursive
```

2. Install OpenSSL and protobuf
2. Install OpenSSL, protobuf, and GNU Readline

- Debian/Ubuntu

```commandline
apt install libssl-dev
apt install libprotobuf-dev protobuf-compiler
apt install libreadline-dev
```

- Arch

```commandline
pacman -S openssl
pacman -S protobuf
pacman -S readline
```

- macOS with brew
Expand All @@ -80,6 +83,7 @@ BallanceMMO is a project which brings online experiences to Ballance.
brew install openssl@1.1
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opt/openssl@1.1/lib/pkgconfig
brew install protobuf
brew install readline
```

(Haven't tried on a Linux distro with yum as package manager. Sorry Fedora/RHEL/CentOS guys...)
Expand Down

0 comments on commit 5db210b

Please sign in to comment.