Merged with default protocol5

Sat, 09 Jan 2016 17:41:21 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 09 Jan 2016 17:41:21 +0200
branch
protocol5
changeset 106
7b156b764d11
parent 104
a76af67a3a4b (diff)
parent 105
b4466472aecd (current diff)
child 108
5900be70c619

Merged with default

sources/coloredline.cpp file | annotate | diff | comparison | revisions
sources/coloredline.h file | annotate | diff | comparison | revisions
sources/interface.cpp file | annotate | diff | comparison | revisions
sources/interface.h file | annotate | diff | comparison | revisions
sources/mystring.cpp file | annotate | diff | comparison | revisions
sources/mystring.h file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.h file | annotate | diff | comparison | revisions
--- a/sources/coloredline.cpp	Sat Jan 09 17:20:25 2016 +0200
+++ b/sources/coloredline.cpp	Sat Jan 09 17:41:21 2016 +0200
@@ -135,6 +135,14 @@
 
 // -------------------------------------------------------------------------------------------------
 //
+void ColoredLine::add_string (const String& text)
+{
+	for (char a : text)
+		add_char (a);
+}
+
+// -------------------------------------------------------------------------------------------------
+//
 void ColoredLine::set_color (Color a, bool on)
 {
 	assert (a < 8);
--- a/sources/coloredline.h	Sat Jan 09 17:20:25 2016 +0200
+++ b/sources/coloredline.h	Sat Jan 09 17:41:21 2016 +0200
@@ -69,6 +69,7 @@
 	const Vector<int>& data() const { return m_data; }
 	int length() const { return m_length; }
 	void add_char (char ch);
+	void add_string (const String& msg);
 	void finalize();
 	int rows (int cols) const;
 
--- a/sources/interface.cpp	Sat Jan 09 17:20:25 2016 +0200
+++ b/sources/interface.cpp	Sat Jan 09 17:41:21 2016 +0200
@@ -635,10 +635,7 @@
 	if (CurrentInputState == INPUTSTATE_CONFIRM_DISCONNECTION)
 	{
 		if (ch == 'y' or ch == 'Y')
-		{
-			Session.disconnect();
-			DisconnectConfirmFunction();
-		}
+			disconnected();
 		else if (ch == 'n' or ch == 'N')
 			set_input_state (INPUTSTATE_NORMAL);
 
@@ -1035,6 +1032,14 @@
 
 // -------------------------------------------------------------------------------------------------
 //
+void Interface::disconnected()
+{
+	Session.disconnect();
+	set_input_state (INPUTSTATE_NORMAL);
+}
+
+// -------------------------------------------------------------------------------------------------
+//
 void Interface::handle_command(const String& input)
 {
 	if (input[0] != '/')
@@ -1076,6 +1081,13 @@
 		endwin();
 		throw Exitception();
 	}
+	else if (command == "watch")
+	{
+		if (not args.is_empty())
+			Session.request_watch(args);
+		else
+			print_error("No CVars to watch.\n");
+	}
 	else
 		print_error("Unknown command %s\n", command.chars());
 }
--- a/sources/interface.h	Sat Jan 09 17:20:25 2016 +0200
+++ b/sources/interface.h	Sat Jan 09 17:41:21 2016 +0200
@@ -59,6 +59,7 @@
 	void need_refresh();
 	void tab_complete (const String& part, String complete);
 	RCONSession* get_session() { return &Session; }
+	void disconnected();
 	void handle_command(const String& input);
 
 	void vprint (const char* fmtstr, va_list args);
@@ -110,4 +111,4 @@
 	int find_next_word();
 };
 
-END_ZFC_NAMESPACE
\ No newline at end of file
+END_ZFC_NAMESPACE
--- a/sources/mystring.cpp	Sat Jan 09 17:20:25 2016 +0200
+++ b/sources/mystring.cpp	Sat Jan 09 17:41:21 2016 +0200
@@ -315,7 +315,7 @@
 
 // -------------------------------------------------------------------------------------------------
 //
-bool String::starts_with (const String& other)
+bool String::starts_with (const String& other) const
 {
 	if (length() < other.length())
 		return false;
@@ -509,4 +509,13 @@
 		m_string = m_string.substr (a, b - a + 1);
 }
 
+// -------------------------------------------------------------------------------------------------
+//
+String String::normalized (int (*filter)(int)) const
+{
+	String result = *this;
+	result.normalize(filter);
+	return result;
+}
+
 END_ZFC_NAMESPACE
--- a/sources/mystring.h	Sat Jan 09 17:20:25 2016 +0200
+++ b/sources/mystring.h	Sat Jan 09 17:41:21 2016 +0200
@@ -100,6 +100,7 @@
 	void insert (int pos, const char*c) { m_string.insert (pos, c); }
 	void modify_index (int &a) { if (a < 0) { a = length() - a; } }
 	void normalize (int (*filter)(int) = &isspace);
+	String normalized (int (*filter)(int) = &isspace) const;
 	void prepend (String a) { m_string = (a + m_string).std_string(); }
 	void remove (int pos, int len) { m_string.replace (pos, len, ""); }
 	void remove_at (int pos) { m_string.erase (m_string.begin() + pos); }
@@ -110,8 +111,9 @@
 	void shrink_to_fit() { m_string.shrink_to_fit(); }
 	void __cdecl sprintf (const char* fmtstr, ...);
 	void vsprintf (const char* fmtstr, va_list args);
-	bool starts_with (const String &other);
-	String strip (const List<char>& unwanted);
+	bool starts_with (const String &other) const;
+	String strip (char unwanted) { return strip ({unwanted}); }
+	String strip (const List<char> &unwanted);
 	void trim (int n);
 
 	static String from_number (short int a);
--- a/sources/network/rconsession.cpp	Sat Jan 09 17:20:25 2016 +0200
+++ b/sources/network/rconsession.cpp	Sat Jan 09 17:41:21 2016 +0200
@@ -184,6 +184,9 @@
 				}
 
 				m_interface->print ("End of previous messages.\n");
+
+				// Watch sv_hostname so that we can update the titlebar when it changes.
+				request_watch("sv_hostname");
 				break;
 
 			case SVRC_UPDATE:
@@ -222,6 +225,40 @@
 					}
 				}
 				break;
+
+			case SVRC_WATCHINGCVAR:
+				m_interface->print ("You are now watching %s\n", packet.read_string().chars());
+				m_interface->print ("Its value is: %s\n", packet.read_string().chars());
+				break;
+
+			case SVRC_ALREADYWATCHINGCVAR:
+				m_interface->print ("You are already watching %s\n", packet.read_string().chars());
+				break;
+
+			case SVRC_WATCHCVARNOTFOUND:
+				m_interface->print ("CVar %s not found\n", packet.read_string().chars());
+				break;
+
+			case SVRC_CVARCHANGED:
+				{
+					String name = packet.read_string();
+					String value = packet.read_string();
+					m_interface->print ("The value of CVar %s", name.chars());
+					m_interface->print (" is now %s\n", value.chars());
+
+					// If sv_hostname changes, update the titlebar
+					if (name == "sv_hostname")
+					{
+						m_hostname = value;
+						m_interface->set_title(m_hostname);
+					}
+				}
+				break;
+
+			case SVRC_YOUREDISCONNECTED:
+				m_interface->print ("You have been disconnected: %s", packet.read_string().chars());
+				m_interface->disconnected();
+				break;
 			}
 		}
 	}
@@ -328,8 +365,18 @@
 		return false;
 
 	Bytestream packet;
-	packet.write_byte (CLRC_COMMAND);
-	packet.write_string (message);
+
+	// Let's hardcode a /watch for CVar watching testing purposes
+	if (message.starts_with ("/watch "))
+	{
+		request_watch(message.mid(String("/watch ").length(), -1).split(','));
+	}
+	else
+	{
+		packet.write_byte (CLRC_COMMAND);
+		packet.write_string (message);
+	}
+
 	send (packet);
 	bump_last_ping();
 	return true;
@@ -389,4 +436,27 @@
 	m_interface = iface;
 }
 
+// -------------------------------------------------------------------------------------------------
+//
+void RCONSession::request_watch (const String& cvar)
+{
+	StringList cvars;
+	cvars.append(cvar);
+	request_watch(cvars);
+}
+
+// -------------------------------------------------------------------------------------------------
+//
+void RCONSession::request_watch (const StringList& cvars)
+{
+	Bytestream packet;
+	packet.write_byte(CLRC_WATCHCVAR);
+
+	for (int i = 0; i < cvars.size(); ++i)
+		packet.write_string(cvars[i].normalized());
+
+	packet.write_string("");
+	send(packet);
+}
+
 END_ZFC_NAMESPACE
--- a/sources/network/rconsession.h	Sat Jan 09 17:20:25 2016 +0200
+++ b/sources/network/rconsession.h	Sat Jan 09 17:41:21 2016 +0200
@@ -38,7 +38,7 @@
 //
 enum
 {
-	RCON_PROTOCOL_VERSION = 4
+	RCON_PROTOCOL_VERSION = 5
 };
 
 // -------------------------------------------------------------------------------------------------
@@ -54,6 +54,11 @@
 	SVRC_UPDATE,
 	SVRC_TABCOMPLETE,
 	SVRC_TOOMANYTABCOMPLETES,
+	SVRC_WATCHINGCVAR,
+	SVRC_ALREADYWATCHINGCVAR,
+	SVRC_WATCHCVARNOTFOUND,
+	SVRC_CVARCHANGED,
+	SVRC_YOUREDISCONNECTED,
 };
 
 // -------------------------------------------------------------------------------------------------
@@ -66,6 +71,7 @@
 	CLRC_PONG,
 	CLRC_DISCONNECT,
 	CLRC_TABCOMPLETE,
+	CLRC_WATCHCVAR,
 };
 
 // -------------------------------------------------------------------------------------------------
@@ -114,6 +120,8 @@
 	bool is_active() const;
 	void request_tab_complete (const String& part);
 	void set_interface (class Interface* iface);
+	void request_watch (const String& cvar);
+	void request_watch (const StringList& cvars);
 
 private:
 	RCONSessionState m_state;
@@ -130,4 +138,4 @@
 	class Interface* m_interface;
 };
 
-END_ZFC_NAMESPACE
\ No newline at end of file
+END_ZFC_NAMESPACE

mercurial