finished read through of SlippiReplayComm.cpp

This commit is contained in:
Nikhil Narayana 2023-08-20 17:12:35 -07:00
commit 1b26177cc4
No known key found for this signature in database
GPG key ID: 1B34839FA8D6245E
2 changed files with 72 additions and 71 deletions

View file

@ -39,7 +39,7 @@ SlippiReplayComm::SlippiReplayComm()
{
INFO_LOG_FMT(EXPANSIONINTERFACE, "SlippiReplayComm: Using playback config path: {}",
SConfig::GetSlippiConfig().slippi_input);
config_file_path = SConfig::GetSlippiConfig().slippi_input;
m_config_file_path = SConfig::GetSlippiConfig().slippi_input;
}
SlippiReplayComm::~SlippiReplayComm()
@ -48,62 +48,63 @@ SlippiReplayComm::~SlippiReplayComm()
SlippiReplayComm::CommSettings SlippiReplayComm::getSettings()
{
return comm_file_settings;
return m_comm_file_settings;
}
std::string SlippiReplayComm::getReplayPath()
{
std::string replayFilePath = comm_file_settings.replay_path;
if (comm_file_settings.mode == "queue")
std::string replay_file_path = m_comm_file_settings.replay_path;
if (m_comm_file_settings.mode == "queue")
{
// If we are in queue mode, let's grab the replay from the queue instead
replayFilePath = comm_file_settings.queue.empty() ? "" : comm_file_settings.queue.front().path;
replay_file_path =
m_comm_file_settings.queue.empty() ? "" : m_comm_file_settings.queue.front().path;
}
return replayFilePath;
return replay_file_path;
}
bool SlippiReplayComm::isNewReplay()
{
loadFile();
std::string replayFilePath = getReplayPath();
std::string replay_file_path = getReplayPath();
bool hasPathChanged = replayFilePath != previous_replay_loaded;
bool isReplay = !!replayFilePath.length();
bool has_path_changed = replay_file_path != m_previous_replay_loaded;
bool is_replay = !!replay_file_path.length();
// The previous check is mostly good enough but it does not
// work if someone tries to load the same replay twice in a row
// the command_id was added to deal with this
bool hasCommandChanged = comm_file_settings.command_id != previous_command_id;
bool has_command_changed = m_comm_file_settings.command_id != m_previous_command_id;
// This checks if the queue index has changed, this is to fix the
// issue where the same replay showing up twice in a row in a
// queue would never cause this function to return true
bool hasQueueIdxChanged = false;
if (comm_file_settings.mode == "queue" && !comm_file_settings.queue.empty())
bool has_queue_idx_changed = false;
if (m_comm_file_settings.mode == "queue" && !m_comm_file_settings.queue.empty())
{
hasQueueIdxChanged = comm_file_settings.queue.front().index != previous_idx;
has_queue_idx_changed = m_comm_file_settings.queue.front().index != m_previous_idx;
}
bool isNewReplay = hasPathChanged || hasCommandChanged || hasQueueIdxChanged;
bool is_new_replay = has_path_changed || has_command_changed || has_queue_idx_changed;
return isReplay && isNewReplay;
return is_replay && is_new_replay;
}
void SlippiReplayComm::nextReplay()
{
if (comm_file_settings.queue.empty())
if (m_comm_file_settings.queue.empty())
return;
// Increment queue position
comm_file_settings.queue.pop();
m_comm_file_settings.queue.pop();
}
std::unique_ptr<Slippi::SlippiGame> SlippiReplayComm::loadGame()
{
auto replayFilePath = getReplayPath();
INFO_LOG_FMT(EXPANSIONINTERFACE, "Attempting to load replay file {}", replayFilePath.c_str());
auto result = Slippi::SlippiGame::FromFile(replayFilePath);
auto replay_file_path = getReplayPath();
INFO_LOG_FMT(EXPANSIONINTERFACE, "Attempting to load replay file {}", replay_file_path.c_str());
auto result = Slippi::SlippiGame::FromFile(replay_file_path);
if (result)
{
// If we successfully loaded a SlippiGame, indicate as such so
@ -111,27 +112,27 @@ std::unique_ptr<Slippi::SlippiGame> SlippiReplayComm::loadGame()
// file did not exist yet, result will be falsy, which will keep
// the replay considered new so that the file will attempt to be
// loaded again
previous_replay_loaded = replayFilePath;
previous_command_id = comm_file_settings.command_id;
if (comm_file_settings.mode == "queue" && !comm_file_settings.queue.empty())
m_previous_replay_loaded = replay_file_path;
m_previous_command_id = m_comm_file_settings.command_id;
if (m_comm_file_settings.mode == "queue" && !m_comm_file_settings.queue.empty())
{
previous_idx = comm_file_settings.queue.front().index;
m_previous_idx = m_comm_file_settings.queue.front().index;
}
WatchSettings ws;
ws.path = replayFilePath;
ws.start_frame = comm_file_settings.start_frame;
ws.end_frame = comm_file_settings.end_frame;
if (comm_file_settings.mode == "queue")
ws.path = replay_file_path;
ws.start_frame = m_comm_file_settings.start_frame;
ws.end_frame = m_comm_file_settings.end_frame;
if (m_comm_file_settings.mode == "queue")
{
ws = comm_file_settings.queue.front();
ws = m_comm_file_settings.queue.front();
}
if (comm_file_settings.output_overlay_files)
if (m_comm_file_settings.output_overlay_files)
{
std::string dirpath = File::GetExeDirectory();
File::WriteStringToFile(dirpath + DIR_SEP + "Slippi/out-station.txt", ws.game_station);
File::WriteStringToFile(dirpath + DIR_SEP + "Slippi/out-time.txt", ws.game_start_at);
std::string dir_path = File::GetExeDirectory();
File::WriteStringToFile(dir_path + DIR_SEP + "Slippi/out-station.txt", ws.game_station);
File::WriteStringToFile(dir_path + DIR_SEP + "Slippi/out-time.txt", ws.game_start_at);
}
current = ws;
@ -145,8 +146,8 @@ void SlippiReplayComm::loadFile()
// TODO: Consider even only checking file mod time every 250 ms or something? Not sure
// TODO: what the perf impact is atm
u64 modTime = File::GetFileModTime(config_file_path);
if (modTime != 0 && modTime == config_last_load_mod_time)
u64 mod_time = File::GetFileModTime(m_config_file_path);
if (mod_time != 0 && mod_time == m_config_last_load_mod_time)
{
// TODO: Maybe be smarter than just using mod time? Look for other things that would
// TODO: indicate that file has changed and needs to be reloaded?
@ -154,27 +155,27 @@ void SlippiReplayComm::loadFile()
}
WARN_LOG_FMT(EXPANSIONINTERFACE, "File change detected in comm file: {}",
config_file_path.c_str());
config_last_load_mod_time = modTime;
m_config_file_path.c_str());
m_config_last_load_mod_time = mod_time;
// TODO: Maybe load file in a more intelligent way to save
// TODO: file operations
std::string commFileContents;
File::ReadFileToString(config_file_path, commFileContents);
std::string comm_file_contents;
File::ReadFileToString(m_config_file_path, comm_file_contents);
auto res = json::parse(commFileContents, nullptr, false);
auto res = json::parse(comm_file_contents, nullptr, false);
if (res.is_discarded() || !res.is_object())
{
// Happens if there is a parse error, I think?
comm_file_settings.mode = "normal";
comm_file_settings.replay_path = "";
comm_file_settings.start_frame = Slippi::GAME_FIRST_FRAME;
comm_file_settings.end_frame = INT_MAX;
comm_file_settings.command_id = "";
comm_file_settings.output_overlay_files = false;
comm_file_settings.is_real_time_mode = false;
comm_file_settings.should_resync = true;
comm_file_settings.rollback_display_method = "off";
m_comm_file_settings.mode = "normal";
m_comm_file_settings.replay_path = "";
m_comm_file_settings.start_frame = Slippi::GAME_FIRST_FRAME;
m_comm_file_settings.end_frame = INT_MAX;
m_comm_file_settings.command_id = "";
m_comm_file_settings.output_overlay_files = false;
m_comm_file_settings.is_real_time_mode = false;
m_comm_file_settings.should_resync = true;
m_comm_file_settings.rollback_display_method = "off";
if (res.is_string())
{
@ -182,7 +183,7 @@ void SlippiReplayComm::loadFile()
// This is really only here because when developing it might be easier
// to just throw in a string instead of an object
comm_file_settings.replay_path = res.get<std::string>();
m_comm_file_settings.replay_path = res.get<std::string>();
}
else
{
@ -190,29 +191,29 @@ void SlippiReplayComm::loadFile()
// Reset in the case of read error. this fixes a race condition where file mod time changes
// but the file is not readable yet?
config_last_load_mod_time = 0;
m_config_last_load_mod_time = 0;
}
return;
}
// TODO: Support file with only path string
comm_file_settings.mode = res.value("mode", "normal");
comm_file_settings.replay_path = res.value("replay", "");
comm_file_settings.start_frame = res.value("startFrame", Slippi::GAME_FIRST_FRAME);
comm_file_settings.end_frame = res.value("endFrame", INT_MAX);
comm_file_settings.command_id = res.value("commandId", "");
comm_file_settings.output_overlay_files = res.value("outputOverlayFiles", false);
comm_file_settings.is_real_time_mode = res.value("isRealTimeMode", false);
comm_file_settings.should_resync = res.value("shouldResync", true);
comm_file_settings.rollback_display_method = res.value("rollbackDisplayMethod", "off");
m_comm_file_settings.mode = res.value("mode", "normal");
m_comm_file_settings.replay_path = res.value("replay", "");
m_comm_file_settings.start_frame = res.value("startFrame", Slippi::GAME_FIRST_FRAME);
m_comm_file_settings.end_frame = res.value("endFrame", INT_MAX);
m_comm_file_settings.command_id = res.value("commandId", "");
m_comm_file_settings.output_overlay_files = res.value("outputOverlayFiles", false);
m_comm_file_settings.is_real_time_mode = res.value("isRealTimeMode", false);
m_comm_file_settings.should_resync = res.value("shouldResync", true);
m_comm_file_settings.rollback_display_method = res.value("rollbackDisplayMethod", "off");
if (comm_file_settings.mode == "queue")
if (m_comm_file_settings.mode == "queue")
{
auto queue = res["queue"];
if (queue.is_array())
{
std::queue<WatchSettings>().swap(comm_file_settings.queue);
std::queue<WatchSettings>().swap(m_comm_file_settings.queue);
int index = 0;
for (json::iterator it = queue.begin(); it != queue.end(); ++it)
{
@ -225,9 +226,9 @@ void SlippiReplayComm::loadFile()
w.game_station = el.value("gameStation", "");
w.index = index++;
comm_file_settings.queue.push(w);
m_comm_file_settings.queue.push(w);
};
queue_was_empty = false;
m_queue_was_empty = false;
}
}
}

View file

@ -50,15 +50,15 @@ private:
void loadFile();
std::string getReplayPath();
std::string config_file_path;
std::string previous_replay_loaded;
std::string previous_command_id;
int previous_idx;
std::string m_config_file_path;
std::string m_previous_replay_loaded;
std::string m_previous_command_id;
int m_previous_idx;
u64 config_last_load_mod_time;
u64 m_config_last_load_mod_time;
// Queue stuff
bool queue_was_empty = true;
bool m_queue_was_empty = true;
CommSettings comm_file_settings;
CommSettings m_comm_file_settings;
};