1
0
Fork 0

Fix #10073: Stop truncating output of list_ai and friends commands

pull/10076/head
Charles Pigott 2022-10-10 12:19:58 +01:00
parent 24f3022ee0
commit 9059215b3b
8 changed files with 38 additions and 42 deletions

View File

@ -139,9 +139,9 @@ public:
static int GetStartNextTime(); static int GetStartNextTime();
/** Wrapper function for AIScanner::GetAIConsoleList */ /** Wrapper function for AIScanner::GetAIConsoleList */
static char *GetConsoleList(char *p, const char *last, bool newest_only = false); static std::string GetConsoleList(bool newest_only = false);
/** Wrapper function for AIScanner::GetAIConsoleLibraryList */ /** Wrapper function for AIScanner::GetAIConsoleLibraryList */
static char *GetConsoleLibraryList(char *p, const char *last); static std::string GetConsoleLibraryList();
/** Wrapper function for AIScanner::GetAIInfoList */ /** Wrapper function for AIScanner::GetAIInfoList */
static const ScriptInfoList *GetInfoList(); static const ScriptInfoList *GetInfoList();
/** Wrapper function for AIScanner::GetUniqueAIInfoList */ /** Wrapper function for AIScanner::GetUniqueAIInfoList */

View File

@ -315,14 +315,14 @@
return DAYS_IN_YEAR; return DAYS_IN_YEAR;
} }
/* static */ char *AI::GetConsoleList(char *p, const char *last, bool newest_only) /* static */ std::string AI::GetConsoleList(bool newest_only)
{ {
return AI::scanner_info->GetConsoleList(p, last, newest_only); return AI::scanner_info->GetConsoleList(newest_only);
} }
/* static */ char *AI::GetConsoleLibraryList(char *p, const char *last) /* static */ std::string AI::GetConsoleLibraryList()
{ {
return AI::scanner_library->GetConsoleList(p, last, true); return AI::scanner_library->GetConsoleList(true);
} }
/* static */ const ScriptInfoList *AI::GetInfoList() /* static */ const ScriptInfoList *AI::GetInfoList()

View File

@ -45,6 +45,8 @@
#include "company_cmd.h" #include "company_cmd.h"
#include "misc_cmd.h" #include "misc_cmd.h"
#include <sstream>
#include "safeguards.h" #include "safeguards.h"
/* scriptfile handling */ /* scriptfile handling */
@ -1160,19 +1162,14 @@ DEF_CONSOLE_CMD(ConReload)
/** /**
* Print a text buffer line by line to the console. Lines are separated by '\n'. * Print a text buffer line by line to the console. Lines are separated by '\n'.
* @param buf The buffer to print. * @param full_string The multi-line string to print.
* @note All newlines are replace by '\0' characters.
*/ */
static void PrintLineByLine(char *buf) static void PrintLineByLine(const std::string &full_string)
{ {
char *p = buf; std::istringstream in(full_string);
/* Print output line by line */ std::string line;
for (char *p2 = buf; *p2 != '\0'; p2++) { while (std::getline(in, line)) {
if (*p2 == '\n') { IConsolePrint(CC_DEFAULT, line.c_str());
*p2 = '\0';
IConsolePrint(CC_DEFAULT, p);
p = p2 + 1;
}
} }
} }
@ -1182,10 +1179,9 @@ DEF_CONSOLE_CMD(ConListAILibs)
IConsolePrint(CC_HELP, "List installed AI libraries. Usage: 'list_ai_libs'."); IConsolePrint(CC_HELP, "List installed AI libraries. Usage: 'list_ai_libs'.");
return true; return true;
} }
char buf[4096];
AI::GetConsoleLibraryList(buf, lastof(buf));
PrintLineByLine(buf); const std::string output_str = AI::GetConsoleLibraryList();
PrintLineByLine(output_str);
return true; return true;
} }
@ -1196,10 +1192,9 @@ DEF_CONSOLE_CMD(ConListAI)
IConsolePrint(CC_HELP, "List installed AIs. Usage: 'list_ai'."); IConsolePrint(CC_HELP, "List installed AIs. Usage: 'list_ai'.");
return true; return true;
} }
char buf[4096];
AI::GetConsoleList(buf, lastof(buf));
PrintLineByLine(buf); const std::string output_str = AI::GetConsoleList();
PrintLineByLine(output_str);
return true; return true;
} }
@ -1210,10 +1205,9 @@ DEF_CONSOLE_CMD(ConListGameLibs)
IConsolePrint(CC_HELP, "List installed Game Script libraries. Usage: 'list_game_libs'."); IConsolePrint(CC_HELP, "List installed Game Script libraries. Usage: 'list_game_libs'.");
return true; return true;
} }
char buf[4096];
Game::GetConsoleLibraryList(buf, lastof(buf));
PrintLineByLine(buf); const std::string output_str = Game::GetConsoleLibraryList();
PrintLineByLine(output_str);
return true; return true;
} }
@ -1224,10 +1218,9 @@ DEF_CONSOLE_CMD(ConListGame)
IConsolePrint(CC_HELP, "List installed Game Scripts. Usage: 'list_game'."); IConsolePrint(CC_HELP, "List installed Game Scripts. Usage: 'list_game'.");
return true; return true;
} }
char buf[4096];
Game::GetConsoleList(buf, lastof(buf));
PrintLineByLine(buf); const std::string output_str = Game::GetConsoleList();
PrintLineByLine(output_str);
return true; return true;
} }

View File

@ -93,9 +93,9 @@ public:
static void Load(int version); static void Load(int version);
/** Wrapper function for GameScanner::GetConsoleList */ /** Wrapper function for GameScanner::GetConsoleList */
static char *GetConsoleList(char *p, const char *last, bool newest_only = false); static std::string GetConsoleList(bool newest_only = false);
/** Wrapper function for GameScanner::GetConsoleLibraryList */ /** Wrapper function for GameScanner::GetConsoleLibraryList */
static char *GetConsoleLibraryList(char *p, const char *last); static std::string GetConsoleLibraryList();
/** Wrapper function for GameScanner::GetInfoList */ /** Wrapper function for GameScanner::GetInfoList */
static const ScriptInfoList *GetInfoList(); static const ScriptInfoList *GetInfoList();
/** Wrapper function for GameScanner::GetUniqueInfoList */ /** Wrapper function for GameScanner::GetUniqueInfoList */

View File

@ -225,14 +225,14 @@
} }
} }
/* static */ char *Game::GetConsoleList(char *p, const char *last, bool newest_only) /* static */ std::string Game::GetConsoleList(bool newest_only)
{ {
return Game::scanner_info->GetConsoleList(p, last, newest_only); return Game::scanner_info->GetConsoleList(newest_only);
} }
/* static */ char *Game::GetConsoleLibraryList(char *p, const char *last) /* static */ std::string Game::GetConsoleLibraryList()
{ {
return Game::scanner_library->GetConsoleList(p, last, true); return Game::scanner_library->GetConsoleList(true);
} }
/* static */ const ScriptInfoList *Game::GetInfoList() /* static */ const ScriptInfoList *Game::GetInfoList()

View File

@ -227,12 +227,14 @@ static void ShowHelp()
/* We need to initialize the AI, so it finds the AIs */ /* We need to initialize the AI, so it finds the AIs */
AI::Initialize(); AI::Initialize();
p = AI::GetConsoleList(p, lastof(buf), true); const std::string ai_list = AI::GetConsoleList(true);
p = strecpy(p, ai_list.c_str(), lastof(buf));
AI::Uninitialize(true); AI::Uninitialize(true);
/* We need to initialize the GameScript, so it finds the GSs */ /* We need to initialize the GameScript, so it finds the GSs */
Game::Initialize(); Game::Initialize();
p = Game::GetConsoleList(p, lastof(buf), true); const std::string game_list = Game::GetConsoleList(true);
p = strecpy(p, game_list.c_str(), lastof(buf));
Game::Uninitialize(true); Game::Uninitialize(true);
/* ShowInfo put output to stderr, but version information should go /* ShowInfo put output to stderr, but version information should go

View File

@ -145,15 +145,16 @@ void ScriptScanner::RegisterScript(ScriptInfo *info)
} }
} }
char *ScriptScanner::GetConsoleList(char *p, const char *last, bool newest_only) const std::string ScriptScanner::GetConsoleList(bool newest_only) const
{ {
p += seprintf(p, last, "List of %s:\n", this->GetScannerName()); std::string p;
p += fmt::format("List of {}:\n", this->GetScannerName());
const ScriptInfoList &list = newest_only ? this->info_single_list : this->info_list; const ScriptInfoList &list = newest_only ? this->info_single_list : this->info_list;
for (const auto &item : list) { for (const auto &item : list) {
ScriptInfo *i = item.second; ScriptInfo *i = item.second;
p += seprintf(p, last, "%10s (v%d): %s\n", i->GetName(), i->GetVersion(), i->GetDescription()); p += fmt::format("{:>10} (v{:d}): {}\n", i->GetName(), i->GetVersion(), i->GetDescription());
} }
p += seprintf(p, last, "\n"); p += "\n";
return p; return p;
} }

View File

@ -57,7 +57,7 @@ public:
/** /**
* Get the list of registered scripts to print on the console. * Get the list of registered scripts to print on the console.
*/ */
char *GetConsoleList(char *p, const char *last, bool newest_only) const; std::string GetConsoleList(bool newest_only) const;
/** /**
* Check whether we have a script with the exact characteristics as ci. * Check whether we have a script with the exact characteristics as ci.