2021-05-01 16:04:53 +02:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2010-01-09 20:20:14 +01:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-01-09 20:20:14 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-01-09 20:20:14 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-09 20:20:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "DebugSerializer.h"
|
|
|
|
|
2021-05-01 16:04:53 +02:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2021-05-13 19:23:52 +02:00
|
|
|
#include "scriptinterface/Object.h"
|
2021-05-14 12:18:03 +02:00
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
|
|
|
#include "scriptinterface/JSON.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-10 12:31:31 +01:00
|
|
|
#include "lib/secure_crt.h"
|
2010-07-31 23:22:39 +02:00
|
|
|
#include "lib/utf8.h"
|
2010-08-04 23:15:41 +02:00
|
|
|
#include "ps/CStr.h"
|
2010-01-10 12:31:31 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
#include <sstream>
|
2010-07-04 19:03:45 +02:00
|
|
|
#include <iomanip>
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The output format here is intended to be compatible with YAML,
|
|
|
|
* so it is human readable and usable in diff and can also be parsed with
|
|
|
|
* external tools.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// MSVC and GCC give slightly different serializations of floats
|
|
|
|
// (e.g. "1e+010" vs "1e+10"). To make the debug serialization easily comparable
|
|
|
|
// across platforms, we want to convert to a canonical form.
|
|
|
|
// TODO: we just do e+0xx now; ought to handle varying precisions and inf and nan etc too
|
|
|
|
template<typename T>
|
2011-08-16 13:18:32 +02:00
|
|
|
std::string canonfloat(T value, int prec)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
std::stringstream str;
|
2010-07-04 19:03:45 +02:00
|
|
|
str << std::setprecision(prec) << value;
|
2010-01-09 20:20:14 +01:00
|
|
|
std::string r = str.str();
|
|
|
|
size_t e = r.find('e');
|
|
|
|
if (e == r.npos) // no "e"
|
|
|
|
return r;
|
|
|
|
if (e == r.length() - 5 && r[e + 2] == '0') // e.g. "1e+010"
|
|
|
|
r.erase(e + 2, 1);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
CDebugSerializer::CDebugSerializer(const ScriptInterface& scriptInterface, std::ostream& stream, bool includeDebugInfo) :
|
2011-10-24 16:31:05 +02:00
|
|
|
m_ScriptInterface(scriptInterface), m_Stream(stream), m_IsDebug(includeDebugInfo), m_Indent(0)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDebugSerializer::Indent(int spaces)
|
|
|
|
{
|
|
|
|
m_Indent += spaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDebugSerializer::Dedent(int spaces)
|
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(spaces <= m_Indent);
|
2010-01-09 20:20:14 +01:00
|
|
|
m_Indent -= spaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INDENT std::string(m_Indent, ' ')
|
|
|
|
|
|
|
|
void CDebugSerializer::Comment(const std::string& comment)
|
|
|
|
{
|
|
|
|
m_Stream << INDENT << "# " << comment << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDebugSerializer::TextLine(const std::string& text)
|
|
|
|
{
|
|
|
|
m_Stream << INDENT << text << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDebugSerializer::PutNumber(const char* name, uint8_t value)
|
|
|
|
{
|
|
|
|
m_Stream << INDENT << name << ": " << (int)value << "\n";
|
|
|
|
}
|
|
|
|
|
2010-10-23 21:59:40 +02:00
|
|
|
void CDebugSerializer::PutNumber(const char* name, int8_t value)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-10-23 21:59:40 +02:00
|
|
|
m_Stream << INDENT << name << ": " << (int)value << "\n";
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 17:06:28 +01:00
|
|
|
void CDebugSerializer::PutNumber(const char* name, uint16_t value)
|
|
|
|
{
|
|
|
|
m_Stream << INDENT << name << ": " << value << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDebugSerializer::PutNumber(const char* name, int16_t value)
|
|
|
|
{
|
|
|
|
m_Stream << INDENT << name << ": " << value << "\n";
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
void CDebugSerializer::PutNumber(const char* name, uint32_t value)
|
|
|
|
{
|
|
|
|
m_Stream << INDENT << name << ": " << value << "\n";
|
|
|
|
}
|
|
|
|
|
2010-10-23 21:59:40 +02:00
|
|
|
void CDebugSerializer::PutNumber(const char* name, int32_t value)
|
|
|
|
{
|
|
|
|
m_Stream << INDENT << name << ": " << value << "\n";
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
void CDebugSerializer::PutNumber(const char* name, float value)
|
|
|
|
{
|
2010-07-04 19:03:45 +02:00
|
|
|
m_Stream << INDENT << name << ": " << canonfloat(value, 8) << "\n";
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDebugSerializer::PutNumber(const char* name, double value)
|
|
|
|
{
|
2010-07-04 19:03:45 +02:00
|
|
|
m_Stream << INDENT << name << ": " << canonfloat(value, 17) << "\n";
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-02 22:32:37 +02:00
|
|
|
void CDebugSerializer::PutNumber(const char* name, fixed value)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-08-04 23:15:41 +02:00
|
|
|
m_Stream << INDENT << name << ": " << value.ToString() << "\n";
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDebugSerializer::PutBool(const char* name, bool value)
|
|
|
|
{
|
|
|
|
m_Stream << INDENT << name << ": " << (value ? "true" : "false") << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDebugSerializer::PutString(const char* name, const std::string& value)
|
|
|
|
{
|
|
|
|
std::string escaped;
|
|
|
|
escaped.reserve(value.size());
|
|
|
|
for (size_t i = 0; i < value.size(); ++i)
|
|
|
|
if (value[i] == '"')
|
|
|
|
escaped += "\\\"";
|
|
|
|
else if (value[i] == '\\')
|
|
|
|
escaped += "\\\\";
|
|
|
|
else if (value[i] == '\n')
|
|
|
|
escaped += "\\n";
|
|
|
|
else
|
|
|
|
escaped += value[i];
|
|
|
|
|
|
|
|
m_Stream << INDENT << name << ": " << "\"" << escaped << "\"\n";
|
|
|
|
}
|
|
|
|
|
2014-08-03 19:29:49 +02:00
|
|
|
void CDebugSerializer::PutScriptVal(const char* name, JS::MutableHandleValue value)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2020-12-27 18:18:13 +01:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2020-12-27 18:18:13 +01:00
|
|
|
JS::RootedValue serialize(rq.cx);
|
2021-05-13 19:23:52 +02:00
|
|
|
if (Script::GetProperty(rq, value, "Serialize", &serialize) && !serialize.isNullOrUndefined())
|
2020-12-27 18:18:13 +01:00
|
|
|
{
|
2021-01-16 11:33:00 +01:00
|
|
|
// If the value has a Serialize property, pretty-parse that instead.
|
|
|
|
// (this gives more accurate OOS reports).
|
2021-05-01 16:04:53 +02:00
|
|
|
ScriptFunction::Call(rq, value, "Serialize", &serialize);
|
2021-05-14 12:18:03 +02:00
|
|
|
std::string serialized_source = Script::ToString(rq, &serialize, true);
|
2021-01-16 11:33:00 +01:00
|
|
|
m_Stream << INDENT << name << ": " << serialized_source << "\n";
|
2020-12-27 18:18:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-14 12:18:03 +02:00
|
|
|
std::string source = Script::ToString(rq, value, true);
|
2020-12-27 18:18:13 +01:00
|
|
|
m_Stream << INDENT << name << ": " << source << "\n";
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-25 21:01:30 +02:00
|
|
|
void CDebugSerializer::PutRaw(const char* name, const u8* data, size_t len)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
m_Stream << INDENT << name << ": (" << len << " bytes)";
|
|
|
|
|
|
|
|
char buf[4];
|
|
|
|
for (size_t i = 0; i < len; ++i)
|
|
|
|
{
|
2012-01-01 17:43:10 +01:00
|
|
|
sprintf_s(buf, ARRAY_SIZE(buf), " %02x", (unsigned int)data[i]);
|
2010-01-09 20:20:14 +01:00
|
|
|
m_Stream << buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Stream << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CDebugSerializer::IsDebug() const
|
|
|
|
{
|
2011-10-24 16:31:05 +02:00
|
|
|
return m_IsDebug;
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
2011-01-12 13:29:00 +01:00
|
|
|
|
|
|
|
std::ostream& CDebugSerializer::GetStream()
|
|
|
|
{
|
|
|
|
return m_Stream;
|
|
|
|
}
|