2026-02-02 09:24:57 -08:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07: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-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07: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-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "ProfileViewer.h"
|
2009-11-06 10:35:32 -08:00
|
|
|
|
2025-08-02 07:05:58 -07:00
|
|
|
#include "lib/code_annotation.h"
|
2021-05-30 10:35:26 -07:00
|
|
|
#include "graphics/Canvas2D.h"
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "graphics/Color.h"
|
2013-10-18 08:53:07 -07:00
|
|
|
#include "graphics/FontMetrics.h"
|
2012-02-12 05:20:49 -08:00
|
|
|
#include "graphics/TextRenderer.h"
|
2019-08-03 20:52:00 -07:00
|
|
|
#include "lib/external_libraries/libsdl.h"
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "lib/os_path.h"
|
|
|
|
|
#include "lib/path.h"
|
|
|
|
|
#include "maths/Rect.h"
|
2021-05-30 10:35:26 -07:00
|
|
|
#include "maths/Size2D.h"
|
|
|
|
|
#include "maths/Vector2D.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "ps/CLogger.h"
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "ps/CStrIntern.h"
|
2009-11-06 10:35:32 -08:00
|
|
|
#include "ps/Hotkey.h"
|
|
|
|
|
#include "ps/Profile.h"
|
2020-01-02 10:07:40 -08:00
|
|
|
#include "ps/Pyrogenesis.h"
|
2025-04-17 13:10:38 -07:00
|
|
|
#include "ps/VideoMode.h"
|
2021-05-13 10:23:52 -07:00
|
|
|
#include "scriptinterface/Object.h"
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
#include "scriptinterface/Request.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2025-07-23 10:19:42 -07:00
|
|
|
#include <SDL_events.h>
|
|
|
|
|
#include <SDL_keycode.h>
|
2019-08-03 20:52:00 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <ctime>
|
2022-02-25 00:14:11 -08:00
|
|
|
#include <fstream>
|
2025-07-23 10:19:42 -07:00
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <js/TypeDecls.h>
|
|
|
|
|
#include <js/Value.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
namespace Script { class Interface; }
|
2019-08-03 20:52:00 -07:00
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
struct CProfileViewerInternals
|
|
|
|
|
{
|
2015-07-28 18:07:23 -07:00
|
|
|
NONCOPYABLE(CProfileViewerInternals); // because of the ofstream
|
|
|
|
|
public:
|
2006-04-23 16:14:18 -07:00
|
|
|
CProfileViewerInternals() {}
|
|
|
|
|
|
|
|
|
|
/// Whether the profiling display is currently visible
|
|
|
|
|
bool profileVisible;
|
|
|
|
|
|
|
|
|
|
/// List of root tables
|
|
|
|
|
std::vector<AbstractProfileTable*> rootTables;
|
|
|
|
|
|
|
|
|
|
/// Path from a root table (path[0]) to the currently visible table (path[size-1])
|
|
|
|
|
std::vector<AbstractProfileTable*> path;
|
|
|
|
|
|
|
|
|
|
/// Helper functions
|
|
|
|
|
void TableIsDeleted(AbstractProfileTable* table);
|
|
|
|
|
void NavigateTree(int id);
|
|
|
|
|
|
2025-04-17 13:10:38 -07:00
|
|
|
void SaveToFile();
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
/// File for saved profile output (reset when the game is restarted)
|
|
|
|
|
std::ofstream outputStream;
|
2025-04-17 13:10:38 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Input: Filter and handle any input events that the profile display is
|
|
|
|
|
* interested in.
|
|
|
|
|
*
|
|
|
|
|
* In particular, this function handles enable/disable of the profile
|
|
|
|
|
* display as well as navigating the information tree.
|
|
|
|
|
*/
|
|
|
|
|
struct InputHandler
|
|
|
|
|
{
|
|
|
|
|
CProfileViewerInternals& profileViewer;
|
|
|
|
|
Input::Reaction operator()(const SDL_Event& ev);
|
|
|
|
|
};
|
|
|
|
|
Input::Handler<InputHandler> inputHandler{g_VideoMode.m_InputManager, Input::Slot::PROFILE_VIEWER,
|
|
|
|
|
{*this}};
|
2006-04-23 16:14:18 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// AbstractProfileTable implementation
|
|
|
|
|
|
|
|
|
|
AbstractProfileTable::~AbstractProfileTable()
|
|
|
|
|
{
|
|
|
|
|
if (CProfileViewer::IsInitialised())
|
|
|
|
|
{
|
|
|
|
|
g_ProfileViewer.m->TableIsDeleted(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// CProfileViewer implementation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AbstractProfileTable got deleted, make sure we have no dangling pointers
|
|
|
|
|
void CProfileViewerInternals::TableIsDeleted(AbstractProfileTable* table)
|
|
|
|
|
{
|
|
|
|
|
for(int idx = (int)rootTables.size()-1; idx >= 0; --idx)
|
|
|
|
|
{
|
|
|
|
|
if (rootTables[idx] == table)
|
|
|
|
|
rootTables.erase(rootTables.begin() + idx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(size_t idx = 0; idx < path.size(); ++idx)
|
|
|
|
|
{
|
|
|
|
|
if (path[idx] != table)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
path.erase(path.begin() + idx, path.end());
|
|
|
|
|
if (path.size() == 0)
|
|
|
|
|
profileVisible = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Move into child tables or return to parent tables based on the given number
|
|
|
|
|
void CProfileViewerInternals::NavigateTree(int id)
|
|
|
|
|
{
|
|
|
|
|
if (id == 0)
|
|
|
|
|
{
|
|
|
|
|
if (path.size() > 1)
|
|
|
|
|
path.pop_back();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AbstractProfileTable* table = path[path.size() - 1];
|
|
|
|
|
size_t numrows = table->GetNumberRows();
|
|
|
|
|
|
|
|
|
|
for(size_t row = 0; row < numrows; ++row)
|
|
|
|
|
{
|
|
|
|
|
AbstractProfileTable* child = table->GetChild(row);
|
|
|
|
|
|
|
|
|
|
if (!child)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
--id;
|
|
|
|
|
if (id == 0)
|
|
|
|
|
{
|
|
|
|
|
path.push_back(child);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Construction/Destruction
|
|
|
|
|
CProfileViewer::CProfileViewer()
|
|
|
|
|
{
|
|
|
|
|
m = new CProfileViewerInternals;
|
|
|
|
|
m->profileVisible = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CProfileViewer::~CProfileViewer()
|
|
|
|
|
{
|
|
|
|
|
delete m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Render
|
2022-02-25 00:14:11 -08:00
|
|
|
void CProfileViewer::RenderProfile(CCanvas2D& canvas)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
if (!m->profileVisible)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-05-30 10:35:26 -07:00
|
|
|
if (m->path.empty())
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
m->profileVisible = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-16 13:38:34 -07:00
|
|
|
PROFILE3("profile viewer");
|
2011-11-03 18:35:50 -07:00
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
AbstractProfileTable* table = m->path[m->path.size() - 1];
|
|
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
size_t numrows = table->GetNumberRows();
|
|
|
|
|
|
2013-10-18 09:15:42 -07:00
|
|
|
CStrIntern font_name("mono-stroke-10");
|
2013-10-18 08:53:07 -07:00
|
|
|
CFontMetrics font(font_name);
|
2025-05-28 20:37:37 -07:00
|
|
|
int height = font.GetHeight();
|
2010-05-30 06:42:56 -07:00
|
|
|
|
2021-05-30 10:35:26 -07:00
|
|
|
// Render background.
|
|
|
|
|
float estimateWidth = 50.0f;
|
|
|
|
|
for (const ProfileColumn& column : columns)
|
|
|
|
|
estimateWidth += static_cast<float>(column.width);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2021-05-30 10:35:26 -07:00
|
|
|
float estimateHeight = 3 + static_cast<float>(numrows);
|
2006-04-23 16:14:18 -07:00
|
|
|
if (m->path.size() > 1)
|
2021-05-30 10:35:26 -07:00
|
|
|
estimateHeight += 2;
|
2025-05-28 20:37:37 -07:00
|
|
|
estimateHeight *= height;
|
2021-05-30 10:35:26 -07:00
|
|
|
|
|
|
|
|
canvas.DrawRect(CRect(CSize2D(estimateWidth, estimateHeight)), CColor(0.0f, 0.0f, 0.0f, 0.5f));
|
|
|
|
|
|
|
|
|
|
// Draw row backgrounds.
|
2012-02-12 05:20:49 -08:00
|
|
|
for (size_t row = 0; row < numrows; ++row)
|
|
|
|
|
{
|
2021-05-30 10:35:26 -07:00
|
|
|
canvas.DrawRect(
|
2025-05-28 20:37:37 -07:00
|
|
|
CRect(CVector2D(0.0f, height * (2.0f + row) + 2.0f), CSize2D(estimateWidth, height)),
|
2021-05-30 10:35:26 -07:00
|
|
|
row % 2 ? CColor(1.0f, 1.0f, 1.0f, 0.1f): CColor(0.0f, 0.0f, 0.0f, 0.1f));
|
2012-02-12 05:20:49 -08:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 10:35:26 -07:00
|
|
|
// Print table and column titles.
|
2021-05-30 11:28:06 -07:00
|
|
|
CTextRenderer textRenderer;
|
2025-07-02 06:43:04 -07:00
|
|
|
textRenderer.SetCurrentFont(font_name, CStrIntern{});
|
2021-06-02 14:21:28 -07:00
|
|
|
textRenderer.SetCurrentColor(CColor(1.0f, 1.0f, 1.0f, 1.0f));
|
2025-05-28 20:37:37 -07:00
|
|
|
textRenderer.PrintfAt(2.0f, height, L"%hs", table->GetTitle().c_str());
|
|
|
|
|
textRenderer.Translate(22.0f, height*2.0f);
|
2012-02-12 05:20:49 -08:00
|
|
|
|
|
|
|
|
float colX = 0.0f;
|
|
|
|
|
for (size_t col = 0; col < columns.size(); ++col)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2012-02-25 09:14:47 -08:00
|
|
|
CStrW text = columns[col].title.FromUTF8();
|
2025-05-16 16:02:40 -07:00
|
|
|
float w, h;
|
2012-02-25 09:14:47 -08:00
|
|
|
font.CalculateStringSize(text.c_str(), w, h);
|
2012-02-12 05:20:49 -08:00
|
|
|
|
|
|
|
|
float x = colX;
|
2011-03-02 17:09:19 -08:00
|
|
|
if (col > 0) // right-align all but the first column
|
2012-02-12 05:20:49 -08:00
|
|
|
x += columns[col].width - w;
|
2012-02-25 09:14:47 -08:00
|
|
|
textRenderer.Put(x, 0.0f, text.c_str());
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2012-02-12 05:20:49 -08:00
|
|
|
colX += columns[col].width;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
2012-02-12 05:20:49 -08:00
|
|
|
|
2025-05-28 20:37:37 -07:00
|
|
|
textRenderer.Translate(0.0f, height);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// Print rows
|
|
|
|
|
int currentExpandId = 1;
|
|
|
|
|
|
2012-02-12 05:20:49 -08:00
|
|
|
for (size_t row = 0; row < numrows; ++row)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
if (table->IsHighlightRow(row))
|
2021-06-02 14:21:28 -07:00
|
|
|
textRenderer.SetCurrentColor(CColor(1.0f, 0.5f, 0.5f, 1.0f));
|
2006-04-23 16:14:18 -07:00
|
|
|
else
|
2021-06-02 14:21:28 -07:00
|
|
|
textRenderer.SetCurrentColor(CColor(1.0f, 1.0f, 1.0f, 1.0f));
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
if (table->GetChild(row))
|
|
|
|
|
{
|
2012-02-12 05:20:49 -08:00
|
|
|
textRenderer.PrintfAt(-15.0f, 0.0f, L"%d", currentExpandId);
|
2006-04-23 16:14:18 -07:00
|
|
|
currentExpandId++;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 14:28:50 -08:00
|
|
|
float rowColX = 0.0f;
|
2012-02-12 05:20:49 -08:00
|
|
|
for (size_t col = 0; col < columns.size(); ++col)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2025-12-28 08:36:36 -08:00
|
|
|
const CStrW text{table->GetCellText(row, col).FromUTF8()};
|
|
|
|
|
if (!text.empty())
|
|
|
|
|
{
|
|
|
|
|
float w, h;
|
|
|
|
|
font.CalculateStringSize(text.c_str(), w, h);
|
2012-02-12 05:20:49 -08:00
|
|
|
|
2025-12-28 08:36:36 -08:00
|
|
|
float x = rowColX;
|
|
|
|
|
if (col > 0) // right-align all but the first column
|
|
|
|
|
x += columns[col].width - w;
|
|
|
|
|
textRenderer.Put(x, 0.0f, text.c_str());
|
|
|
|
|
}
|
2020-11-26 14:28:50 -08:00
|
|
|
rowColX += columns[col].width;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-28 20:37:37 -07:00
|
|
|
textRenderer.Translate(0.0f, height);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
2012-02-12 05:20:49 -08:00
|
|
|
|
2021-06-02 14:21:28 -07:00
|
|
|
textRenderer.SetCurrentColor(CColor(1.0f, 1.0f, 1.0f, 1.0f));
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
if (m->path.size() > 1)
|
|
|
|
|
{
|
2025-05-28 20:37:37 -07:00
|
|
|
textRenderer.Translate(0.0f, height);
|
2012-02-25 09:14:47 -08:00
|
|
|
textRenderer.Put(-15.0f, 0.0f, L"0");
|
|
|
|
|
textRenderer.Put(0.0f, 0.0f, L"back to parent");
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 12:10:10 -07:00
|
|
|
canvas.DrawText(textRenderer);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Handle input
|
2025-04-17 13:10:38 -07:00
|
|
|
Input::Reaction CProfileViewerInternals::InputHandler::operator()(const SDL_Event& ev)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2026-05-18 12:11:00 -07:00
|
|
|
switch(ev.type)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
if (!profileViewer.profileVisible)
|
2006-04-23 16:14:18 -07:00
|
|
|
break;
|
|
|
|
|
|
2026-05-18 12:11:00 -07:00
|
|
|
int k = ev.key.keysym.sym;
|
2012-02-06 14:47:35 -08:00
|
|
|
if (k >= SDLK_0 && k <= SDLK_9)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
profileViewer.NavigateTree(k - SDLK_0);
|
2025-07-14 11:14:31 -07:00
|
|
|
return Input::Reaction::HANDLED;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-05-26 14:47:03 -07:00
|
|
|
case SDL_HOTKEYPRESS:
|
2025-04-17 13:10:38 -07:00
|
|
|
std::string_view hotkey = static_cast<const char*>(ev.user.data1);
|
2010-10-22 19:37:00 -07:00
|
|
|
|
2025-04-17 13:10:38 -07:00
|
|
|
if (hotkey == "profile.toggle")
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
if (!profileViewer.profileVisible)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
if (profileViewer.rootTables.size())
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
profileViewer.profileVisible = true;
|
|
|
|
|
profileViewer.path.push_back(profileViewer.rootTables[0]);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
size_t i;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2025-04-17 13:10:38 -07:00
|
|
|
for(i = 0; i < profileViewer.rootTables.size(); ++i)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
if (profileViewer.rootTables[i] == profileViewer.path[0])
|
2006-04-23 16:14:18 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
|
2025-04-17 13:10:38 -07:00
|
|
|
profileViewer.path.clear();
|
|
|
|
|
if (i < profileViewer.rootTables.size())
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
profileViewer.path.push_back(profileViewer.rootTables[i]);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
profileViewer.profileVisible = false;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-14 11:14:31 -07:00
|
|
|
return Input::Reaction::HANDLED;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
2010-10-22 19:37:00 -07:00
|
|
|
else if( hotkey == "profile.save" )
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
profileViewer.SaveToFile();
|
2025-07-14 11:14:31 -07:00
|
|
|
return Input::Reaction::HANDLED;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-07-14 11:14:31 -07:00
|
|
|
return Input::Reaction::PASS;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a table to the list of roots
|
2010-08-02 12:23:58 -07:00
|
|
|
void CProfileViewer::AddRootTable(AbstractProfileTable* table, bool front)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2010-08-02 12:23:58 -07:00
|
|
|
if (front)
|
|
|
|
|
m->rootTables.insert(m->rootTables.begin(), table);
|
|
|
|
|
else
|
|
|
|
|
m->rootTables.push_back(table);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
2020-12-31 06:27:02 -08:00
|
|
|
class WriteTable
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2020-12-31 06:27:02 -08:00
|
|
|
public:
|
|
|
|
|
WriteTable(std::ofstream& outputStream) : m_OutputStream(outputStream) {}
|
|
|
|
|
WriteTable(const WriteTable& writeTable) = default;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
void operator() (AbstractProfileTable* table)
|
|
|
|
|
{
|
|
|
|
|
std::vector<CStr> data; // 2d array of (rows+head)*columns elements
|
|
|
|
|
|
2010-10-31 15:00:28 -07:00
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
// Add column headers to 'data'
|
2010-10-31 15:00:28 -07:00
|
|
|
for (std::vector<ProfileColumn>::const_iterator col_it = columns.begin();
|
|
|
|
|
col_it != columns.end(); ++col_it)
|
2006-04-23 16:14:18 -07:00
|
|
|
data.push_back(col_it->title);
|
|
|
|
|
|
|
|
|
|
// Recursively add all profile data to 'data'
|
|
|
|
|
WriteRows(1, table, data);
|
|
|
|
|
|
|
|
|
|
// Calculate the width of each column ( = the maximum width of
|
|
|
|
|
// any value in that column)
|
|
|
|
|
std::vector<size_t> columnWidths;
|
2010-10-31 15:00:28 -07:00
|
|
|
size_t cols = columns.size();
|
2006-04-23 16:14:18 -07:00
|
|
|
for (size_t c = 0; c < cols; ++c)
|
|
|
|
|
{
|
|
|
|
|
size_t max = 0;
|
|
|
|
|
for (size_t i = c; i < data.size(); i += cols)
|
|
|
|
|
max = std::max(max, data[i].length());
|
|
|
|
|
columnWidths.push_back(max);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Output data as a formatted table:
|
|
|
|
|
|
2020-12-31 06:27:02 -08:00
|
|
|
m_OutputStream << "\n\n" << table->GetTitle() << "\n";
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2013-01-21 12:33:25 -08:00
|
|
|
if (cols == 0) // avoid divide-by-zero
|
|
|
|
|
return;
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
for (size_t r = 0; r < data.size()/cols; ++r)
|
|
|
|
|
{
|
|
|
|
|
for (size_t c = 0; c < cols; ++c)
|
2020-12-31 06:27:02 -08:00
|
|
|
m_OutputStream << (c ? " | " : "\n")
|
2006-04-23 16:14:18 -07:00
|
|
|
<< data[r*cols + c].Pad(PS_TRIM_RIGHT, columnWidths[c]);
|
|
|
|
|
|
|
|
|
|
// Add dividers under some rows. (Currently only the first, since
|
|
|
|
|
// that contains the column headers.)
|
|
|
|
|
if (r == 0)
|
|
|
|
|
for (size_t c = 0; c < cols; ++c)
|
2020-12-31 06:27:02 -08:00
|
|
|
m_OutputStream << (c ? "-|-" : "\n")
|
2006-04-23 16:14:18 -07:00
|
|
|
<< CStr::Repeat("-", columnWidths[c]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteRows(int indent, AbstractProfileTable* table, std::vector<CStr>& data)
|
|
|
|
|
{
|
2010-10-31 15:00:28 -07:00
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
for (size_t r = 0; r < table->GetNumberRows(); ++r)
|
|
|
|
|
{
|
|
|
|
|
// Do pretty tree-structure indenting
|
|
|
|
|
CStr indentation = CStr::Repeat("| ", indent-1);
|
|
|
|
|
if (r+1 == table->GetNumberRows())
|
|
|
|
|
indentation += "'-";
|
|
|
|
|
else
|
|
|
|
|
indentation += "|-";
|
|
|
|
|
|
2010-10-31 15:00:28 -07:00
|
|
|
for (size_t c = 0; c < columns.size(); ++c)
|
2006-04-23 16:14:18 -07:00
|
|
|
if (c == 0)
|
|
|
|
|
data.push_back(indentation + table->GetCellText(r, c));
|
|
|
|
|
else
|
|
|
|
|
data.push_back(table->GetCellText(r, c));
|
|
|
|
|
|
|
|
|
|
if (table->GetChild(r))
|
|
|
|
|
WriteRows(indent+1, table->GetChild(r), data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-12-31 06:27:02 -08:00
|
|
|
std::ofstream& m_OutputStream;
|
2006-04-23 16:14:18 -07:00
|
|
|
const WriteTable& operator=(const WriteTable&);
|
|
|
|
|
};
|
|
|
|
|
|
2011-02-18 19:14:37 -08:00
|
|
|
struct DumpTable
|
|
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
const Script::Interface& m_ScriptInterface;
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::PersistentRooted<JS::Value> m_Root;
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
DumpTable(const Script::Interface& scriptInterface, JS::HandleValue root) :
|
2021-05-14 03:18:03 -07:00
|
|
|
m_ScriptInterface(scriptInterface)
|
2015-01-24 06:46:52 -08:00
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(scriptInterface);
|
2021-05-14 03:18:03 -07:00
|
|
|
m_Root.init(rq.cx, root);
|
2015-01-24 06:46:52 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 05:02:58 -08:00
|
|
|
// std::for_each requires a move constructor and the use of JS::PersistentRooted<T> apparently breaks a requirement for an
|
2015-01-24 06:46:52 -08:00
|
|
|
// automatic move constructor
|
|
|
|
|
DumpTable(DumpTable && original) :
|
2021-05-14 03:18:03 -07:00
|
|
|
m_ScriptInterface(original.m_ScriptInterface)
|
2011-02-18 19:14:37 -08:00
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(m_ScriptInterface);
|
2021-05-14 03:18:03 -07:00
|
|
|
m_Root.init(rq.cx, original.m_Root.get());
|
2011-02-18 19:14:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void operator() (AbstractProfileTable* table)
|
|
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(m_ScriptInterface);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue t(rq.cx);
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::CreateObject(
|
2020-11-13 05:18:22 -08:00
|
|
|
rq,
|
2019-09-07 09:51:44 -07:00
|
|
|
&t,
|
|
|
|
|
"cols", DumpCols(table),
|
|
|
|
|
"data", DumpRows(table));
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::SetProperty(rq, m_Root, table->GetTitle().c_str(), t);
|
2011-03-02 17:09:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> DumpCols(AbstractProfileTable* table)
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::string> titles;
|
|
|
|
|
|
|
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
|
|
|
|
|
for (size_t c = 0; c < columns.size(); ++c)
|
|
|
|
|
titles.push_back(columns[c].title);
|
|
|
|
|
|
|
|
|
|
return titles;
|
2011-02-18 19:14:37 -08:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::Value DumpRows(AbstractProfileTable* table)
|
2011-02-18 19:14:37 -08:00
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request rq(m_ScriptInterface);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue data(rq.cx);
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::CreateObject(rq, &data);
|
2011-02-18 19:14:37 -08:00
|
|
|
|
|
|
|
|
const std::vector<ProfileColumn>& columns = table->GetColumns();
|
|
|
|
|
|
|
|
|
|
for (size_t r = 0; r < table->GetNumberRows(); ++r)
|
|
|
|
|
{
|
2026-02-02 09:24:57 -08:00
|
|
|
JS::RootedValueVector row{rq.cx};
|
|
|
|
|
if (!row.resize(columns.size() + 1))
|
|
|
|
|
throw std::runtime_error{"Resize failed"};
|
2019-07-22 12:35:14 -07:00
|
|
|
|
2021-05-13 10:23:52 -07:00
|
|
|
Script::SetProperty(rq, data, table->GetCellText(r, 0).c_str(), row);
|
2011-02-18 19:14:37 -08:00
|
|
|
|
|
|
|
|
if (table->GetChild(r))
|
2015-01-24 06:46:52 -08:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue childRows(rq.cx, DumpRows(table->GetChild(r)));
|
2026-02-02 09:24:57 -08:00
|
|
|
row[0].set(childRows);
|
2015-01-24 06:46:52 -08:00
|
|
|
}
|
2011-03-02 17:09:19 -08:00
|
|
|
|
|
|
|
|
for (size_t c = 1; c < columns.size(); ++c)
|
2026-02-02 09:24:57 -08:00
|
|
|
{
|
|
|
|
|
row[c].set(JS::StringValue(
|
|
|
|
|
JS_NewStringCopyZ(rq.cx, table->GetCellText(r, c).c_str())));
|
|
|
|
|
}
|
2011-02-18 19:14:37 -08:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
return data;
|
2011-02-18 19:14:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const DumpTable& operator=(const DumpTable&);
|
|
|
|
|
};
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
bool SortByName(AbstractProfileTable* a, AbstractProfileTable* b)
|
|
|
|
|
{
|
|
|
|
|
return (a->GetName() < b->GetName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CProfileViewer::SaveToFile()
|
2025-04-17 13:10:38 -07:00
|
|
|
{
|
|
|
|
|
m->SaveToFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CProfileViewerInternals::SaveToFile()
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
// Open the file, if necessary. If this method is called several times,
|
|
|
|
|
// the profile results will be appended to the previous ones from the same
|
|
|
|
|
// run.
|
2025-04-17 13:10:38 -07:00
|
|
|
if (!outputStream.is_open())
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
// Open the file. (It will be closed when the CProfileViewer
|
|
|
|
|
// destructor is called.)
|
2025-04-17 13:10:38 -07:00
|
|
|
const OsPath filePath{psLogDir()/"profile.txt"};
|
|
|
|
|
outputStream.open(OsString(filePath), std::ofstream::out | std::ofstream::trunc);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2025-04-17 13:10:38 -07:00
|
|
|
if (outputStream.fail())
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Failed to open profile log file");
|
2006-04-23 16:14:18 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2011-06-17 16:40:13 -07:00
|
|
|
else
|
|
|
|
|
{
|
2025-04-17 13:10:38 -07:00
|
|
|
LOGMESSAGERENDER("Profiler snapshot saved to '%s'", filePath.string8());
|
2011-06-17 16:40:13 -07:00
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time_t t;
|
|
|
|
|
time(&t);
|
2025-04-17 13:10:38 -07:00
|
|
|
outputStream << "================================================================\n\n";
|
|
|
|
|
outputStream << "PS profiler snapshot - " << asctime(localtime(&t));
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2025-04-17 13:10:38 -07:00
|
|
|
std::vector<AbstractProfileTable*> tables = rootTables;
|
2006-09-30 14:36:19 -07:00
|
|
|
sort(tables.begin(), tables.end(), SortByName);
|
2025-04-17 13:10:38 -07:00
|
|
|
for_each(tables.begin(), tables.end(), WriteTable(outputStream));
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2025-04-17 13:10:38 -07:00
|
|
|
outputStream << "\n\n================================================================\n";
|
|
|
|
|
outputStream.flush();
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
2006-09-28 13:41:12 -07:00
|
|
|
|
|
|
|
|
void CProfileViewer::ShowTable(const CStr& table)
|
|
|
|
|
{
|
|
|
|
|
m->path.clear();
|
|
|
|
|
|
|
|
|
|
if (table.length() > 0)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < m->rootTables.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
if (m->rootTables[i]->GetName() == table)
|
|
|
|
|
{
|
|
|
|
|
m->path.push_back(m->rootTables[i]);
|
|
|
|
|
m->profileVisible = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No matching table found, so don't display anything
|
|
|
|
|
m->profileVisible = false;
|
|
|
|
|
}
|