Codechange: use string/fmt instead of printf for squirrel's PRINTFUNC

This commit is contained in:
Rubidium
2023-04-19 07:04:43 +02:00
committed by rubidium42
parent 075671bcfc
commit 8b93e45e22
7 changed files with 47 additions and 67 deletions

View File

@@ -44,10 +44,10 @@ ScriptStorage::~ScriptStorage()
* @param error_msg Is this an error message?
* @param message The actual message text.
*/
static void PrintFunc(bool error_msg, const SQChar *message)
static void PrintFunc(bool error_msg, const std::string &message)
{
/* Convert to OpenTTD internal capable string */
ScriptController::Print(error_msg, message);
ScriptController::Print(error_msg, message.c_str());
}
ScriptInstance::ScriptInstance(const char *APIName) :

View File

@@ -208,36 +208,27 @@ size_t Squirrel::GetAllocatedMemory() const noexcept
void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
{
SQChar buf[1024];
seprintf(buf, lastof(buf), "Error %s:" OTTD_PRINTF64 "/" OTTD_PRINTF64 ": %s", source, line, column, desc);
std::string msg = fmt::format("Error {}:{}/{}: {}", source, line, column, desc);
/* Check if we have a custom print function */
Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
engine->crashed = true;
SQPrintFunc *func = engine->print_func;
if (func == nullptr) {
Debug(misc, 0, "[Squirrel] Compile error: {}", buf);
Debug(misc, 0, "[Squirrel] Compile error: {}", msg);
} else {
(*func)(true, buf);
(*func)(true, msg);
}
}
void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s)
{
va_list arglist;
SQChar buf[1024];
va_start(arglist, s);
vseprintf(buf, lastof(buf), s, arglist);
va_end(arglist);
/* Check if we have a custom print function */
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
if (func == nullptr) {
fprintf(stderr, "%s", buf);
fprintf(stderr, "%s", s.c_str());
} else {
(*func)(true, buf);
(*func)(true, s);
}
}
@@ -248,14 +239,13 @@ void Squirrel::RunError(HSQUIRRELVM vm, const SQChar *error)
sq_setprintfunc(vm, &Squirrel::ErrorPrintFunc);
/* Check if we have a custom print function */
SQChar buf[1024];
seprintf(buf, lastof(buf), "Your script made an error: %s\n", error);
std::string msg = fmt::format("Your script made an error: {}\n", error);
Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
SQPrintFunc *func = engine->print_func;
if (func == nullptr) {
fprintf(stderr, "%s", buf);
fprintf(stderr, "%s", msg.c_str());
} else {
(*func)(true, buf);
(*func)(true, msg);
}
/* Print below the error the stack, so the users knows what is happening */
@@ -279,22 +269,14 @@ SQInteger Squirrel::_RunError(HSQUIRRELVM vm)
return 0;
}
void Squirrel::PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
void Squirrel::PrintFunc(HSQUIRRELVM vm, const std::string &s)
{
va_list arglist;
SQChar buf[1024];
va_start(arglist, s);
vseprintf(buf, lastof(buf) - 2, s, arglist);
va_end(arglist);
strecat(buf, "\n", lastof(buf));
/* Check if we have a custom print function */
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
if (func == nullptr) {
printf("%s", buf);
printf("%s", s.c_str());
} else {
(*func)(false, buf);
(*func)(false, s);
}
}

View File

@@ -24,7 +24,7 @@ class Squirrel {
friend class ScriptAllocatorScope;
private:
typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
typedef void (SQPrintFunc)(bool error_msg, const std::string &message);
HSQUIRRELVM vm; ///< The VirtualMachine instance for squirrel
void *global_pointer; ///< Can be set by who ever initializes Squirrel
@@ -63,12 +63,12 @@ protected:
/**
* If a user runs 'print' inside a script, this function gets the params.
*/
static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...) WARN_FORMAT(2, 3);
static void PrintFunc(HSQUIRRELVM vm, const std::string &s);
/**
* If an error has to be print, this function is called.
*/
static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...) WARN_FORMAT(2, 3);
static void ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s);
public:
Squirrel(const char *APIName);