Skip to content

Commit

Permalink
Make autolag more eager to increase lag
Browse files Browse the repository at this point in the history
Tweak command messages
Switch from rich edit to standard edit control to enable context menu
  • Loading branch information
CoderTimZ committed Oct 19, 2018
1 parent 0c2aeae commit f1e18d9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 100 deletions.
54 changes: 21 additions & 33 deletions NetplayInputPlugin/client.cpp
Expand Up @@ -33,14 +33,14 @@ client::client(shared_ptr<io_service> io_s, shared_ptr<client_dialog> my_dialog)
frame = 0;
golf = false;

my_dialog->status("Available Commands:\n"
"/name <name> ........... Set your name\n"
"/join <address> ........ Join a game\n"
"/host [port] ........... Host a game\n"
"/start ................. Start the game\n"
"/autolag ............... Toggle automatic lag on and off\n"
"/lag <lag> ............. Set the netplay input lag\n"
"/golf .................. Toggle golf mode on and off");
my_dialog->status("Available Commands:\r\n\r\n"
"/name <name> ........... Set your name\r\n"
"/host [port] ........... Host a private server\r\n"
"/join <address> ........ Join a game\r\n"
"/start ................. Start the game\r\n"
"/autolag ............... Toggle automatic lag on and off\r\n"
"/lag <lag> ............. Set the netplay input lag\r\n"
"/golf .................. Toggle golf mode on and off\r\n");
}

client::~client() {
Expand Down Expand Up @@ -95,32 +95,17 @@ void client::ping_public_server_list() {
ip::tcp::endpoint endpoint = *iterator;
c->socket.async_connect(endpoint, [=](error_code error) {
if (error) return set_ping(-2);

c->socket.set_option(ip::tcp::no_delay(true), error);
if (error) return set_ping(-2);

c->read([=](packet& p) {
switch (p.read<uint8_t>()) {
case VERSION: {
auto protocol_version = p.read<uint32_t>();
if (protocol_version != PROTOCOL_VERSION) return set_ping(-3);
c->send(packet() << PING << timestamp());
c->read([=](packet& p) {
switch (p.read<uint8_t>()) {
case PONG:
set_ping(timestamp() - p.read<double>());
break;

default:
set_ping(-3);
}
});
break;
}

default:
set_ping(-3);
}
if (p.read<uint8_t>() != VERSION) return set_ping(-3);
auto protocol_version = p.read<uint32_t>();
if (protocol_version != PROTOCOL_VERSION) return set_ping(-3);
c->send(packet() << PING << timestamp());
c->read([=](packet& p) {
if (p.read<uint8_t>() != PONG) return set_ping(-3);
set_ping(timestamp() - p.read<double>());
});
});
});
});
Expand Down Expand Up @@ -426,14 +411,17 @@ void client::process_packet() {
if (protocol_version != PROTOCOL_VERSION) {
close();
start_game();
my_dialog->error("Server protocol version does not match client protocol version");
my_dialog->error("Server protocol version does not match client protocol version. Visit www.play64.com to get the latest version of the plugin.");
}
break;
}

case PATH: {
path = p.read();
my_dialog->status("Address: " + host + (port == 6400 ? "" : ":" + to_string(port)) + (path == "/" ? "" : path));
my_dialog->status(
"Command for others to join this game:\r\n\r\n"
"/join " + (host == "127.0.0.1" ? "<Your IP Address>" : host) + (port == 6400 ? "" : ":" + to_string(port)) + (path == "/" ? "" : path) + "\r\n"
);
break;
}

Expand Down
81 changes: 18 additions & 63 deletions NetplayInputPlugin/client_dialog.cpp
Expand Up @@ -9,7 +9,7 @@
using namespace std;

client_dialog::client_dialog(HMODULE hmod, HWND main_window)
: hmod(hmod), main_window(main_window), h_rich(LoadLibrary(L"Riched20.dll")), hwndDlg(NULL), thread([=] { gui_thread(); }) {
: hmod(hmod), main_window(main_window), hwndDlg(NULL), thread([=] { gui_thread(); }) {
initialized.get_future().get();
}

Expand All @@ -26,10 +26,6 @@ client_dialog::~client_dialog() {
} else {
thread.detach();
}

if (h_rich) {
FreeLibrary(h_rich);
}
}

void client_dialog::set_message_handler(function<void(string)> message_handler) {
Expand Down Expand Up @@ -63,19 +59,13 @@ void client_dialog::status(const string& text) {
if (destroyed) return;

PostMessage(hwndDlg, WM_TASK, (WPARAM) new function<void(void)>([=] {
HWND output_box = GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT);
HWND output_box = GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT);
SendMessage(output_box, WM_SETREDRAW, FALSE, NULL);

bool at_bottom = scroll_at_bottom();

append_timestamp();

CHARFORMAT2 format;
format.cbSize = sizeof(format);
format.dwMask = CFM_COLOR | CFM_BOLD;
format.crTextColor = RGB(0, 0, 255);
format.dwEffects = CFE_BOLD;
SendMessage(output_box, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
insert_text(text);

if (at_bottom) {
Expand All @@ -94,19 +84,13 @@ void client_dialog::error(const string& text) {
if (destroyed) return;

PostMessage(hwndDlg, WM_TASK, (WPARAM) new function<void(void)>([=] {
HWND output_box = GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT);
HWND output_box = GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT);
SendMessage(output_box, WM_SETREDRAW, FALSE, NULL);

bool at_bottom = scroll_at_bottom();

append_timestamp();

CHARFORMAT2 format;
format.cbSize = sizeof(format);
format.dwMask = CFM_COLOR | CFM_BOLD;
format.crTextColor = RGB(255, 0, 0);
format.dwEffects = CFE_BOLD;
SendMessage(output_box, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
insert_text(text);

if (at_bottom) {
Expand All @@ -125,27 +109,14 @@ void client_dialog::message(const string& name, const string& message) {
if (destroyed) return;

PostMessage(hwndDlg, WM_TASK, (WPARAM) new function<void(void)>([=] {
HWND output_box = GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT);
HWND output_box = GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT);
SendMessage(output_box, WM_SETREDRAW, FALSE, NULL);

bool at_bottom = scroll_at_bottom();

append_timestamp();

CHARFORMAT2 format;
format.cbSize = sizeof(format);
format.dwMask = CFM_COLOR | CFM_BOLD;
format.crTextColor = RGB(0, 0, 0);
format.dwEffects = CFE_BOLD;
SendMessage(output_box, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
insert_text(name + ":");

format.cbSize = sizeof(format);
format.dwMask = CFM_COLOR | CFM_BOLD;
format.crTextColor = RGB(0, 0, 0);
format.dwEffects = 0;
SendMessage(output_box, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
insert_text(" " + message);
insert_text(name + ": " + message);

if (at_bottom) {
scroll_to_bottom();
Expand Down Expand Up @@ -269,19 +240,6 @@ void client_dialog::gui_thread() {
0
);

RECT statRect;
POINT tl, br;
GetWindowRect(GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT), &statRect);
tl.x = statRect.left;
tl.y = statRect.top;
br.x = statRect.right;
br.y = statRect.bottom;
ScreenToClient(hwndDlg, &tl);
ScreenToClient(hwndDlg, &br);

SetWindowPos(GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT), NULL, tl.x + 1, tl.y + 1, br.x - tl.x - 2, br.y - tl.y - 2, 0);
CreateWindow(WC_STATIC, nullptr, SS_BLACKFRAME | WS_CHILD | WS_VISIBLE, tl.x, tl.y, br.x - tl.x, br.y - tl.y, hwndDlg, (HMENU)IDC_STATIC, hmod, 0);

initialized.set_value(true);

MSG message;
Expand All @@ -297,26 +255,24 @@ bool client_dialog::scroll_at_bottom() {
SCROLLINFO psi;
psi.cbSize = sizeof(psi);
psi.fMask = SIF_ALL;
if (GetScrollInfo(GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT), SB_VERT, &psi) && psi.nMax - (int)psi.nPage > psi.nTrackPos) {
if (GetScrollInfo(GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT), SB_VERT, &psi) && psi.nMax - (int)psi.nPage > psi.nTrackPos) {
return false;
}

return true;
}

void client_dialog::scroll_to_bottom() {
SendMessage(GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT), WM_VSCROLL, MAKELONG(SB_BOTTOM, 0), 0);
SendMessage(GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT), WM_VSCROLL, MAKELONG(SB_BOTTOM, 0), 0);
}

void client_dialog::select_end() {
CHARRANGE cr;
cr.cpMin = -1;
cr.cpMax = -1;
SendMessage(GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT), EM_EXSETSEL, 0, (LPARAM) &cr);
int outLength = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT));
SendMessage(GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT), EM_SETSEL, outLength, outLength);
}

void client_dialog::insert_text(const string& text) {
SendMessage(GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT), EM_REPLACESEL, 0, (LPARAM)utf8_to_wstring(text).c_str());
SendMessage(GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT), EM_REPLACESEL, 0, (LPARAM)utf8_to_wstring(text).c_str());
}

void client_dialog::append_timestamp() {
Expand All @@ -330,17 +286,10 @@ void client_dialog::append_timestamp() {

select_end();

CHARFORMAT2 format;
format.cbSize = sizeof(format);
format.dwMask = CFM_COLOR | CFM_BOLD;
format.crTextColor = RGB(128, 128, 128);
format.dwEffects = 0;
SendMessage(GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT), EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);

if (SendMessage(GetDlgItem(hwndDlg, IDC_OUTPUT_RICHEDIT), WM_GETTEXTLENGTH, 0, 0) == 0) {
if (GetWindowTextLength(GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT)) == 0) {
insert_text("(" + string(buffer) + ") ");
} else {
insert_text("\n(" + string(buffer) + ") ");
insert_text("\r\n(" + string(buffer) + ") ");
}
}

Expand Down Expand Up @@ -424,6 +373,12 @@ INT_PTR CALLBACK client_dialog::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wPara
}
break;

case WM_CTLCOLORSTATIC:
if ((HWND)lParam == GetDlgItem(hwndDlg, IDC_OUTPUT_EDIT)) {
return (LRESULT)((HBRUSH)GetStockObject(WHITE_BRUSH));
}
break;

case WM_TASK:
auto task = (function <void(void)>*) wParam;
(*task)();
Expand Down
1 change: 0 additions & 1 deletion NetplayInputPlugin/client_dialog.h
Expand Up @@ -26,7 +26,6 @@ class client_dialog {
HWND main_window;
std::function<void(std::string)> message_handler;
std::function<void(void)> close_handler;
HMODULE h_rich;
HWND hwndDlg;
std::thread thread;
std::promise<bool> initialized;
Expand Down
2 changes: 1 addition & 1 deletion NetplayInputPlugin/resource.h
Expand Up @@ -12,6 +12,6 @@
#define IDC_CONFIG_BUTTON 1005
#define IDC_COMBO_PLUGINS 1010
#define IDC_USER_LIST 1020
#define IDC_OUTPUT_RICHEDIT 1021
#define IDC_OUTPUT_EDIT 1021
#define IDC_INPUT_EDIT 1022
#define IDC_SERVER_LIST 1023
2 changes: 1 addition & 1 deletion NetplayInputPlugin/resource.rc
Expand Up @@ -19,7 +19,7 @@ EXSTYLE WS_EX_WINDOWEDGE | WS_EX_APPWINDOW
CAPTION APP_NAME_AND_VERSION
FONT 10, "Consolas"
{
CONTROL "", IDC_OUTPUT_RICHEDIT, RICHEDIT_CLASS, WS_VSCROLL | NOT WS_TABSTOP | ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY | NOT ES_SUNKEN, 2, 2, 314, 182
EDITTEXT IDC_OUTPUT_EDIT, 2, 2, 314, 182, WS_VSCROLL | NOT WS_TABSTOP | ES_AUTOVSCROLL | ES_MULTILINE | ES_READONLY
EDITTEXT IDC_INPUT_EDIT, 2, 186, 476, 12, WS_GROUP | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL | ES_WANTRETURN
LISTBOX IDC_USER_LIST, 318, 2, 160, 90, WS_HSCROLL | WS_VSCROLL | LBS_NOINTEGRALHEIGHT | LBS_HASSTRINGS | LBS_NOTIFY | LBS_NOSEL | NOT WS_TABSTOP
LISTBOX IDC_SERVER_LIST, 318, 94, 160, 90, WS_HSCROLL | WS_VSCROLL | LBS_NOINTEGRALHEIGHT | LBS_HASSTRINGS | LBS_NOTIFY | NOT WS_TABSTOP
Expand Down
2 changes: 1 addition & 1 deletion NetplayInputPlugin/room.cpp
Expand Up @@ -111,7 +111,7 @@ void room::auto_adjust_lag() {

double latency = get_total_latency();

int ideal_lag = min((int)ceil(latency * fps - 0.25), 255);
int ideal_lag = min((int)ceil(latency * fps - 0.1), 255);
if (ideal_lag < lag) {
send_lag(-1, lag - 1);
} else if (ideal_lag > lag) {
Expand Down

0 comments on commit f1e18d9

Please sign in to comment.