Fixes to Windows utf-8 support + one more Minecraft newline fix

This commit is contained in:
Tiiffi
2024-12-06 16:24:04 +02:00
parent cc77044df1
commit 2d29741691

View File

@ -140,9 +140,7 @@ void sighandler(int sig)
putchar('\n');
global_connection_alive = 0;
#ifndef _WIN32
exit(EXIT_SUCCESS);
#endif
}
#define MAX_WAIT_TIME 600
@ -239,6 +237,9 @@ int main(int argc, char *argv[])
old_output_codepage = GetConsoleOutputCP();
old_input_codepage = GetConsoleCP();
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
// Set the file translation mode to UTF16
_setmode(_fileno(stdin), _O_U16TEXT);
#endif
@ -502,20 +503,32 @@ void print_color(int color)
void packet_print(rc_packet *packet)
{
uint8_t *data = packet->data;
int i;
if (flag_raw_output == 1) {
fputs((char *) data, stdout);
return;
}
int i;
// Newline fix for Minecraft
if (global_valve_protocol == false) {
const char test[] = "Unknown or incomplete command, see below for error";
size_t test_size = sizeof test - 1;
if (strncmp((char *) data, test, test_size) == 0) {
fwrite(data, test_size, 1, stdout);
putchar('\n');
data = &data[test_size];
}
}
int default_color = 0;
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO console_info;
if (GetConsoleScreenBufferInfo(console_handle, &console_info) != 0) {
default_color = console_info.wAttributes + 0x30;
} else default_color = 0x37;
}
else default_color = 0x37;
#endif
bool slash = false;
@ -536,6 +549,7 @@ void packet_print(rc_packet *packet)
continue;
}
// Add missing slashes
if (colors_detected == false && global_minecraft_newline_fix && data[i] == '/') {
slash ? putchar('\n') : (slash = true);
}
@ -587,7 +601,7 @@ bool rcon_auth(int sock, char *passwd)
return 0; // send failed
}
receive:
receive:
packet = net_recv_packet(sock);
if (packet == NULL)
return 0;
@ -739,12 +753,16 @@ int run_terminal_mode(int sock)
#endif
char tmp[1];
#ifdef _WIN32
// TODO: More Windows side testing!
int result = recv(sock, tmp, sizeof(tmp), MSG_PEEK | 0);
#else
int result = recv(sock, tmp, sizeof(tmp), MSG_PEEK | MSG_DONTWAIT);
#endif
if (result == 0) {
// Connection closed
break;
break; // Connection closed
}
// TODO: Check for errors too!
// TODO: Check for return values and errors!
}
}
@ -762,6 +780,9 @@ char *utf8_getline(char *buf, int size, FILE *stream)
return NULL;
}
wint_t ch;
while ((ch = getwchar()) != L'\n' && ch != WEOF);
// Calculates UTF-8 buffer size
int required_size = WideCharToMultiByte(CP_UTF8, 0, in, -1, NULL, 0, NULL, NULL);
if (size < required_size) {
@ -781,7 +802,11 @@ char *utf8_getline(char *buf, int size, FILE *stream)
// gets line from stdin and deals with rubbish left in the input buffer
int get_line(char *buffer, int bsize)
{
#ifdef _WIN32
char *ret = utf8_getline(buffer, bsize, stdin);
#else
char *ret = fgets(buffer, bsize, stdin);
#endif
if (ret == NULL) {
if (ferror(stdin)) {
log_error("Error %d: %s\n", errno, strerror(errno));
@ -793,14 +818,15 @@ int get_line(char *buffer, int bsize)
// remove unwanted characters from the buffer
buffer[strcspn(buffer, "\r\n")] = '\0';
int len = strlen(buffer);
// clean input buffer if needed
#ifndef _WIN32
if (len == bsize - 1) {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
#endif
return len;
}