Skip to content

Commit

Permalink
[2.8.6] Demo Recording/Playback (#337)
Browse files Browse the repository at this point in the history
* initial commit with horrible code dupcliation haha lol
Set up elapsedtimer to generate wait# stuff between packets recorded

* implement groundwork for internal demo server

* add core playback functionality

* make it work kinda by including SC packet in demo

* Add a file dialog for loading a demo file instead of a hardcoded path

* Change /play to > in OOC to begin playback or skip to next element
Pop up file dialog box *before* establishing the connection, not after.
TODO:
* Fix having to load the same file *twice* to be able to connect to the demo server for some reason
* Fix the segfault caused by calling the playback() function when there's no remaining data, it's almost like demo_data.isEmpty() is ignored for whatever reason???

* Clear demo data when loading a demo file to prevent stacking demos

* Properly disconnect the client when sending the request for file browser fails to obtain a good demo file

* Fix append_to_file newlining even if file didn't exist prior to calling this function

* Add a very scuffed exception to not log or demo record anything that happens in the Demo playback local server

* Reduce invalid file spam by checking for non-logging server better
Use file_exists() to be more consistent in append_to_file

* Fix the client crashing when receiving loading packets etc. at runtime such as SC, SM, CI etc. (TODO: parity???)
Remove useless debug stuff

* Preserve newlines for demo packets such as "CT", "MS" etc.

* Implement /max_wait, /min_wait for adjusting the maximum and minimum wait in milliseconds between wait packets
Add /pause or | shorthand to pause playback
Re-add /play and keep > as a shorthand
Remove clientside restrictions from sending empty OOC messages and sending OOC messages without a name - these should be serverside.

* Empty music list
Default the character to Spectator char no matter the selection in CSS

* Allow -1 character ID or character ID that does not fit into the local Character Select Screen list to still be parsed corectly, using the character folder in the MS packet as reference.
Allow servers with no selectable characters to still be properly loaded
Bypass the Character Select Screen when joining a server with no character select screen and automatically become a spectator

* Properly handle demo files without SC packet to dictate which chars exist
Add a /load command letting you load a demo file without rejoining the demo server (the CS packet will not be properly handled but I cannot think of a single tangible problem this causes lol)
Make sure all DEMO CT messages are colored properly

* Fix logs bleeding into each other if you disabled logging or you joined a demo server after leaving another

* Prevent logging even if log_filename is defined because a user might disable auto logging when running the game

* Fix custom shownames not appearing in the IC logs

* Set up new logic for max_wait, meaning that dead air being skipped is prioritized with MS (IC chat) packets being used as the anchor.

* Better logic for min_wait to only affect important packets (IC Chat)

* Fix encoding not being performed on packets that are saved to the .demo file, resulting in characters having something like "#1" in their message breaking that specific message

* Fix a weird setup in courtroom.cpp that happened out of the merge and bugged the code
Fix aopacket being busted up as well

* add missing feature flags to the demoserver

* use random port

* move writing to the demo file to a function

* only listen on localhost

Co-authored-by: oldmud0 <[email protected]>

* remove copypasta

* add a help to the demo server

* fix empty demo disconnecting the server

* tell the user how to begin

Co-authored-by: scatterflower <[email protected]>
Co-authored-by: stonedDiscord <[email protected]>
Co-authored-by: stonedDiscord <[email protected]>
Co-authored-by: oldmud0 <[email protected]>
  • Loading branch information
5 people authored Jan 13, 2021
1 parent dda459c commit 2f84055
Show file tree
Hide file tree
Showing 10 changed files with 447 additions and 27 deletions.
9 changes: 9 additions & 0 deletions include/aoapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "aopacket.h"
#include "datatypes.h"
#include "demoserver.h"
#include "discord_rich_presence.h"

#include "bass.h"
Expand All @@ -27,6 +28,8 @@
#include <QStringList>
#include <QTextStream>

#include <QElapsedTimer>

class NetworkManager;
class Lobby;
class Courtroom;
Expand Down Expand Up @@ -261,6 +264,9 @@ class AOApplication : public QApplication {
// directory if it doesn't exist.
bool append_to_file(QString p_text, QString p_file, bool make_dir = false);

// Append to the currently open demo file if there is one
void append_to_demofile(QString packet_string);

// Appends the argument string to serverlist.txt
void write_to_serverlist_txt(QString p_line);

Expand Down Expand Up @@ -458,6 +464,9 @@ class AOApplication : public QApplication {
void *user);
static void doBASSreset();

QElapsedTimer demo_timer;
DemoServer* demo_server = nullptr;

private:
const int RELEASE = 2;
const int MAJOR_VERSION = 8;
Expand Down
2 changes: 1 addition & 1 deletion include/aopacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AOPacket {

QString get_header() { return m_header; }
QStringList &get_contents() { return m_contents; }
QString to_string();
QString to_string(bool encoded = false);

void net_encode();
void net_decode();
Expand Down
55 changes: 55 additions & 0 deletions include/demoserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef DEMOSERVER_H
#define DEMOSERVER_H

#include "aopacket.h"

#include <QDebug>
#include <QObject>
#include <QQueue>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
#include <QFileDialog>

class DemoServer : public QObject
{
Q_OBJECT
public:
explicit DemoServer(QObject *parent = nullptr);

bool server_started = false;
int port = 27088;
int max_wait = -1;
int min_wait = -1;

private:
void handle_packet(AOPacket packet);
void load_demo(QString filename);

QTcpServer* tcp_server;
QTcpSocket* client_sock = nullptr;
bool client_connected = false;
bool partial_packet = false;
QString temp_packet = "";
QQueue<QString> demo_data;
QString sc_packet;
int num_chars = 0;
QString p_path;
QTimer *timer;
int elapsed_time = 0;

private slots:
void accept_connection();
void destroy_connection();
void recv_data();
void client_disconnect();
void playback();

public slots:
void start_server();

signals:

};

#endif // DEMOSERVER_H
4 changes: 4 additions & 0 deletions src/aoapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ void AOApplication::construct_lobby()
if (is_discord_enabled())
discord->state_lobby();

if (demo_server)
demo_server->deleteLater();
demo_server = new DemoServer();

w_lobby->show();
}

Expand Down
10 changes: 8 additions & 2 deletions src/aopacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ AOPacket::AOPacket(QString p_packet_string)
m_contents = packet_contents.mid(1, packet_contents.size()-2); // trims %
}

QString AOPacket::to_string()
QString AOPacket::to_string(bool encoded)
{
return m_header + "#" + m_contents.join("#") + "#%";
QStringList contents = m_contents;
if (encoded)
contents.replaceInStrings("#", "<num>")
.replaceInStrings("%", "<percent>")
.replaceInStrings("$", "<dollar>")
.replaceInStrings("&", "<and>");
return m_header + "#" + contents.join("#") + "#%";
}

void AOPacket::net_encode()
Expand Down
26 changes: 13 additions & 13 deletions src/courtroom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,20 @@ void Courtroom::done_received()
objection_player->set_volume(0);
blip_player->set_volume(0);

set_char_select_page();
if (char_list.size() > 0)
{
set_char_select_page();
set_char_select();
}
else
{
update_character(m_cid);
enter_courtroom();
}

set_mute_list();
set_pair_list();

set_char_select();

show();

ui_spectator->show();
Expand Down Expand Up @@ -1279,8 +1286,6 @@ void Courtroom::set_pos_dropdown(QStringList pos_dropdowns)
ui_pos_dropdown->addItems(pos_dropdown_list);
// Unblock the signals so the element can be used for setting pos again
ui_pos_dropdown->blockSignals(false);

qDebug() << pos_dropdown_list;
}

void Courtroom::update_character(int p_cid)
Expand Down Expand Up @@ -1323,7 +1328,6 @@ void Courtroom::update_character(int p_cid)
set_sfx_dropdown();
set_effects_dropdown();

qDebug() << "update_character called";
if (newchar) // Avoid infinite loop of death and suffering
set_iniswap_dropdown();

Expand Down Expand Up @@ -2519,7 +2523,7 @@ void Courtroom::play_char_sfx(QString sfx_name)
void Courtroom::initialize_chatbox()
{
int f_charid = m_chatmessage[CHAR_ID].toInt();
if (f_charid >= 0 &&
if (f_charid >= 0 && f_charid < char_list.size() &&
(m_chatmessage[SHOWNAME].isEmpty() || !ui_showname_enable->isChecked())) {
QString real_name = char_list.at(f_charid).name;

Expand Down Expand Up @@ -2967,7 +2971,7 @@ void Courtroom::log_ic_text(QString p_name, QString p_showname,
{
chatlogpiece log_entry(p_name, p_showname, p_message, p_action, p_color);
ic_chatlog_history.append(log_entry);
if (ao_app->get_auto_logging_enabled())
if (ao_app->get_auto_logging_enabled() && !ao_app->log_filename.isEmpty())
ao_app->append_to_file(log_entry.get_full(), ao_app->log_filename, true);

while (ic_chatlog_history.size() > log_maximum_blocks &&
Expand Down Expand Up @@ -3132,7 +3136,7 @@ void Courtroom::play_preanim(bool immediate)
else
anim_state = 1;
preanim_done();
qDebug() << "could not find " + anim_to_find;
qDebug() << "W: could not find " + anim_to_find;
return;
}

Expand Down Expand Up @@ -3794,9 +3798,6 @@ void Courtroom::on_ooc_return_pressed()
{
QString ooc_message = ui_ooc_chat_message->text();

if (ooc_message == "" || ui_ooc_chat_name->text() == "")
return;

if (ooc_message.startsWith("/pos")) {
if (ooc_message == "/pos jud") {
toggle_judge_buttons(true);
Expand Down Expand Up @@ -4749,7 +4750,6 @@ void Courtroom::on_area_list_double_clicked(QTreeWidgetItem *p_item, int column)
QStringList packet_contents;
packet_contents.append(p_area);
packet_contents.append(QString::number(m_cid));
qDebug() << packet_contents;
ao_app->send_server_packet(new AOPacket("MC", packet_contents), false);
}

Expand Down
Loading

0 comments on commit 2f84055

Please sign in to comment.