From 8020839faa99b9c474cc3fbb0b55bf1a115fa3fe Mon Sep 17 00:00:00 2001 From: Webster Sheets Date: Thu, 14 Nov 2024 13:20:53 -0500 Subject: [PATCH 1/4] Add Lua type information for Input KeyChords --- data/meta/Input.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/data/meta/Input.lua b/data/meta/Input.lua index 73896a6e9a..e0ab033fe3 100644 --- a/data/meta/Input.lua +++ b/data/meta/Input.lua @@ -17,6 +17,15 @@ Input.keys = {} --================================== +---@alias Input.KeyBinding { key: integer } | { mouse: integer } | { joystick: integer, button: integer } | { joystick: integer, hat: integer, dir: integer } + +---@class Input.KeyChord +---@field activator Input.KeyBinding +---@field modifier1 Input.KeyBinding? +---@field modifier2 Input.KeyBinding? + +--================================== + ---@class Input.ActionBinding ---@field id string ---@field type string @@ -184,16 +193,16 @@ function Input.CreateInputFrame(id, modal) end -- Register an ActionBinding from Lua with the given default key associations ---@param id string The identifier of the action binding. Must be globally unique. ---@param groupId string The page and group this action binding is organized into, in the form "page.group" ----@param b1 table? The primary keychord for this binding ----@param b2 table? The secondary keychord for this binding +---@param b1 Input.KeyChord? The primary keychord for this binding +---@param b2 Input.KeyChord? The secondary keychord for this binding ---@return Input.ActionBinding function Input.RegisterActionBinding(id, groupId, b1, b2) end -- Register an AxisBinding from Lua with the given default key associations ---@param id string The identifier of the axis binding. Must be globally unique. ---@param groupId string The page and group this axis binding is organized into, in the form "page.group" ----@param pos table? The positive-direction keychord for this axis binding ----@param neg table? The negative-direction keychord for this axis binding +---@param pos Input.KeyChord? The positive-direction keychord for this axis binding +---@param neg Input.KeyChord? The negative-direction keychord for this axis binding ---@param axis table? The joystick axis to associate with this axis binding ---@return Input.AxisBinding function Input.RegisterAxisBinding(id, groupId, pos, neg, axis) end From 35d35909f6a0a3406be5bf773a9e28973c66733e Mon Sep 17 00:00:00 2001 From: Webster Sheets Date: Thu, 14 Nov 2024 14:55:37 -0500 Subject: [PATCH 2/4] Add USE_ASAN build option, fix read-past-end in l_rand_new --- CMakeLists.txt | 10 ++++++++++ src/lua/LuaRand.cpp | 7 ++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec000976a4..3c31c609aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,16 @@ if (USE_TIME_TRACE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftime-trace") endif() +option(USE_ASAN "Use -fsanitize=asan when compiling (requires Clang)" OFF) +if (USE_ASAN) + add_compile_options( + -g + -fsanitize=address + -fno-omit-frame-pointer) + add_link_options( + -fsanitize=address) +endif() + include(CheckSymbolExists) check_symbol_exists(feclearexcept "fenv.h" HAS_FECLEAREXCEPT) check_symbol_exists(feenableexcept "fenv.h" HAS_FEENABLEEXCEPT) diff --git a/src/lua/LuaRand.cpp b/src/lua/LuaRand.cpp index 558581b076..9c40131fcb 100644 --- a/src/lua/LuaRand.cpp +++ b/src/lua/LuaRand.cpp @@ -44,12 +44,13 @@ static int l_rand_new(lua_State *l) std::unique_ptr rng(new Random()); switch (lua_type(l, 1)) { case LUA_TSTRING: { - size_t sz; - const char *str = lua_tolstring(l, 1, &sz); + std::string str = LuaPull(l, 1); + // lookup3_hashlittle2 reads 2 bytes past the end of the string + str += "\0\0"; // Note, these are inputs as well as outputs! They must be initialised. Uint32 hashes[2] = { 0u, 0u }; - lookup3_hashlittle2(str, sz, hashes + 0, hashes + 1); + lookup3_hashlittle2(str.data(), str.size() - 2, hashes + 0, hashes + 1); rng->seed(hashes, 2); break; } From c56436631a2468760ab1b510ac8b50c5e2512bc9 Mon Sep 17 00:00:00 2001 From: Webster Sheets Date: Thu, 14 Nov 2024 15:31:35 -0500 Subject: [PATCH 3/4] StringName: remove unneeded includes - Don't directly use Profiler::Clock - Don't include StringHash.h as hashing is done inside the cpp file --- src/core/StringName.cpp | 20 +++++++++++++++++--- src/core/StringName.h | 12 ++---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/core/StringName.cpp b/src/core/StringName.cpp index a269e5715b..16c9ae2524 100644 --- a/src/core/StringName.cpp +++ b/src/core/StringName.cpp @@ -3,6 +3,9 @@ #include "core/StringName.h" +#include "FNV1a.h" +#include "profiler/Profiler.h" + #include #include #include @@ -68,6 +71,15 @@ StringName::StringData *StringName::make_data(const char *c, uint32_t s, uint32_ // ============================================================================= +StringTable::StringTable(uint32_t size) : + keys(size), + dist(size), + values(size), + entries(0) +{ + m_lastReclaim = Profiler::Clock::getticks(); +} + StringTable::Data *StringTable::Find(uint32_t key) { if (!key) @@ -160,11 +172,13 @@ void StringTable::Erase(uint32_t key) void StringTable::Reclaim(bool force) { - m_reclaimClock.SoftStop(); - if (m_reclaimClock.seconds() < 15.0 && !force) + uint64_t ticks = Profiler::Clock::getticks(); + double seconds = Profiler::Clock::ms(ticks - m_lastReclaim) * 1000.0; + + if (seconds < 15.0 && !force) return; - m_reclaimClock.SoftReset(); + m_lastReclaim = ticks; for (uint32_t idx = 0; idx < keys.size(); idx++) { uint32_t probed_key = keys[idx]; diff --git a/src/core/StringName.h b/src/core/StringName.h index 2a935b3c8a..95fbe7fba4 100644 --- a/src/core/StringName.h +++ b/src/core/StringName.h @@ -3,12 +3,8 @@ #pragma once -#include "StringHash.h" -#include "profiler/Profiler.h" - #include #include -#include #include /* @@ -88,11 +84,7 @@ class StringTable { public: using Data = StringName::StringData *; - StringTable(uint32_t size) : - keys(size), - dist(size), - values(size), - entries(0) {} + StringTable(uint32_t size); static StringTable *Get(); @@ -123,7 +115,7 @@ class StringTable { std::vector dist; std::vector values; uint32_t entries; - Profiler::Clock m_reclaimClock; + uint64_t m_lastReclaim; }; inline StringName operator""_name(const char *c, size_t l) { return StringName(std::string_view(c, l)); } From e5ec9dfdea09ad158fa81b5d4320dd5c55461995 Mon Sep 17 00:00:00 2001 From: Webster Sheets Date: Thu, 14 Nov 2024 15:40:46 -0500 Subject: [PATCH 4/4] Header cleanup - Add profiler/Profiler.h to various .cpp files which didn't include it prior - Remove some unneeded includes - Ensure GalaxyGenerator.h doesn't require a prior include of Galaxy.h - Don't query for equality to Pi::player in Ship-AI.cpp (removes need for Pi.h and Player.h includes) --- src/Beam.cpp | 2 ++ src/Body.cpp | 2 ++ src/Camera.cpp | 5 ++++- src/DeathView.cpp | 2 ++ src/HudTrail.cpp | 2 ++ src/Projectile.cpp | 2 ++ src/SectorView.cpp | 7 ++++--- src/Sensors.cpp | 4 +++- src/Sfx.cpp | 6 ++++-- src/Shields.cpp | 2 ++ src/Ship-AI.cpp | 10 +++------- src/Space.cpp | 5 +++-- src/SpeedLines.cpp | 1 + src/SystemView.cpp | 1 + src/core/Property.cpp | 1 + src/core/Property.h | 2 ++ src/editor/UndoSystem.cpp | 1 + src/editor/system/GalaxyEditAPI.cpp | 2 +- src/galaxy/GalaxyGenerator.h | 1 + src/lua/Lua.cpp | 4 ++-- src/lua/LuaStarSystem.cpp | 4 ++-- src/test/TestProperty.cpp | 3 ++- src/test/TestStringName.cpp | 1 + 23 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/Beam.cpp b/src/Beam.cpp index c9577cb187..3ab956376b 100644 --- a/src/Beam.cpp +++ b/src/Beam.cpp @@ -26,6 +26,8 @@ #include "lua/LuaEvent.h" #include "lua/LuaUtils.h" +#include "profiler/Profiler.h" + namespace { static float lifetime = 0.1f; } diff --git a/src/Body.cpp b/src/Body.cpp index ab9a0d341a..4de1bf98e3 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -20,6 +20,8 @@ #include "core/Log.h" #include "lua/LuaEvent.h" +#include "profiler/Profiler.h" + Body::Body() : PropertiedObject(), m_interpPos(0.0), diff --git a/src/Camera.cpp b/src/Camera.cpp index dcbe5e1c4f..10242ee5fc 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -11,11 +11,14 @@ #include "Player.h" #include "Sfx.h" #include "Space.h" +#include "SpaceStation.h" + #include "galaxy/StarSystem.h" #include "graphics/TextureBuilder.h" #include "graphics/Types.h" #include "graphics/RenderState.h" -#include "SpaceStation.h" + +#include "profiler/Profiler.h" using namespace Graphics; diff --git a/src/DeathView.cpp b/src/DeathView.cpp index 1f3add4f17..602bb93aae 100644 --- a/src/DeathView.cpp +++ b/src/DeathView.cpp @@ -11,6 +11,8 @@ #include "graphics/Graphics.h" #include "graphics/Renderer.h" +#include "profiler/Profiler.h" + DeathView::DeathView(Game *game) : View(), m_game(game) diff --git a/src/HudTrail.cpp b/src/HudTrail.cpp index 3b0e78594a..cdf5d1dd14 100644 --- a/src/HudTrail.cpp +++ b/src/HudTrail.cpp @@ -11,6 +11,8 @@ #include "graphics/Renderer.h" #include "graphics/Types.h" +#include "profiler/Profiler.h" + const float UPDATE_INTERVAL = 0.1f; const Uint16 MAX_POINTS = 100; diff --git a/src/Projectile.cpp b/src/Projectile.cpp index fb5c5aeb1f..a9790129d9 100644 --- a/src/Projectile.cpp +++ b/src/Projectile.cpp @@ -26,6 +26,8 @@ #include "lua/LuaEvent.h" #include "lua/LuaUtils.h" +#include "profiler/Profiler.h" + std::unique_ptr Projectile::s_sideMesh; std::unique_ptr Projectile::s_glowMesh; std::unique_ptr Projectile::s_sideMat; diff --git a/src/SectorView.cpp b/src/SectorView.cpp index 74c2186030..3ed524e161 100644 --- a/src/SectorView.cpp +++ b/src/SectorView.cpp @@ -24,10 +24,11 @@ #include "lua/LuaRef.h" #include "lua/LuaTable.h" #include "matrix4x4.h" -#include -#include -#include +#include "profiler/Profiler.h" + +#include +#include #include SectorView::~SectorView() {} diff --git a/src/Sensors.cpp b/src/Sensors.cpp index f23d9647a8..8db2cc61f9 100644 --- a/src/Sensors.cpp +++ b/src/Sensors.cpp @@ -10,6 +10,8 @@ #include "Ship.h" #include "Space.h" +#include "profiler/Profiler.h" + Sensors::RadarContact::RadarContact() : body(0), trail(0), @@ -62,7 +64,7 @@ Body* Sensors::ChooseTarget(TargetingCriteria crit, const Body* oldTarget ) if(!m_owner->IsType(ObjectType::PLAYER)) return nullptr; - + const Body* currTarget = oldTarget; m_radarContacts.sort(ContactDistanceSort); diff --git a/src/Sfx.cpp b/src/Sfx.cpp index b90d05620c..9a8abc2ca6 100644 --- a/src/Sfx.cpp +++ b/src/Sfx.cpp @@ -11,7 +11,8 @@ #include "JsonUtils.h" #include "ModelBody.h" #include "Pi.h" -#include "StringF.h" +#include "matrix4x4.h" + #include "core/IniConfig.h" #include "graphics/Drawables.h" #include "graphics/Graphics.h" @@ -21,7 +22,8 @@ #include "graphics/TextureBuilder.h" #include "graphics/Types.h" #include "graphics/VertexArray.h" -#include "matrix4x4.h" + +#include "profiler/Profiler.h" using namespace Graphics; diff --git a/src/Shields.cpp b/src/Shields.cpp index 52d8f1fbe5..1ad64b50e3 100644 --- a/src/Shields.cpp +++ b/src/Shields.cpp @@ -15,6 +15,8 @@ #include "scenegraph/StaticGeometry.h" #include "scenegraph/Node.h" +#include "profiler/Profiler.h" + #include REGISTER_COMPONENT_TYPE(Shields) { diff --git a/src/Ship-AI.cpp b/src/Ship-AI.cpp index 8e26766629..cdc8838a07 100644 --- a/src/Ship-AI.cpp +++ b/src/Ship-AI.cpp @@ -3,17 +3,13 @@ #include "EnumStrings.h" #include "Frame.h" -#include "Pi.h" -#include "Planet.h" -#include "Player.h" #include "Ship.h" #include "ShipAICmd.h" #include "Space.h" #include "SpaceStation.h" -#include "lua/LuaConstants.h" #include "lua/LuaEvent.h" -#include "perlin.h" -#include "ship/Propulsion.h" + +#include "profiler/Profiler.h" // returns true if command is complete bool Ship::AITimeStep(float timeStep) @@ -24,7 +20,7 @@ bool Ship::AITimeStep(float timeStep) m_decelerating = false; if (!m_curAICmd) { - if (this == Pi::player) return true; + if (this->IsType(ObjectType::PLAYER)) return true; // just in case the AI left it on ClearThrusterState(); diff --git a/src/Space.cpp b/src/Space.cpp index d26a8bf87d..90b67b40d0 100644 --- a/src/Space.cpp +++ b/src/Space.cpp @@ -22,11 +22,12 @@ #include "collider/CollisionSpace.h" #include "core/Log.h" #include "galaxy/Galaxy.h" -#include "graphics/Graphics.h" #include "lua/LuaEvent.h" #include "lua/LuaTimer.h" + +#include "profiler/Profiler.h" + #include -#include //#define DEBUG_CACHE diff --git a/src/SpeedLines.cpp b/src/SpeedLines.cpp index a4bbeb38ff..0633419b25 100644 --- a/src/SpeedLines.cpp +++ b/src/SpeedLines.cpp @@ -10,6 +10,7 @@ #include "core/IniConfig.h" #include "graphics/RenderState.h" #include "graphics/Renderer.h" +#include "profiler/Profiler.h" // default values float SpeedLines::BOUNDS = 2000.f; diff --git a/src/SystemView.cpp b/src/SystemView.cpp index c2b3714052..aa423dccec 100644 --- a/src/SystemView.cpp +++ b/src/SystemView.cpp @@ -29,6 +29,7 @@ #include "graphics/Types.h" #include "imgui/imgui.h" +#include "profiler/Profiler.h" #include "SDL_keycode.h" using namespace Graphics; diff --git a/src/core/Property.cpp b/src/core/Property.cpp index aadcb2568e..37a18a2998 100644 --- a/src/core/Property.cpp +++ b/src/core/Property.cpp @@ -5,6 +5,7 @@ #include "Json.h" #include "JsonUtils.h" +#include "StringHash.h" PropertyMapWrapper::PropertyMapWrapper(PropertyMap *m) : m_map(m) diff --git a/src/core/Property.h b/src/core/Property.h index 7cff3e71a8..68372709fa 100644 --- a/src/core/Property.h +++ b/src/core/Property.h @@ -11,6 +11,8 @@ #include "vector2.h" #include "vector3.h" +#include "FNV1a.h" + #include #include #include diff --git a/src/editor/UndoSystem.cpp b/src/editor/UndoSystem.cpp index 517c09c31c..12c0ee8f12 100644 --- a/src/editor/UndoSystem.cpp +++ b/src/editor/UndoSystem.cpp @@ -2,6 +2,7 @@ // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt #include "UndoSystem.h" +#include "core/StringHash.h" #include "utils.h" #define XXH_INLINE_ALL diff --git a/src/editor/system/GalaxyEditAPI.cpp b/src/editor/system/GalaxyEditAPI.cpp index 72a1ae214d..12b1594be1 100644 --- a/src/editor/system/GalaxyEditAPI.cpp +++ b/src/editor/system/GalaxyEditAPI.cpp @@ -7,12 +7,12 @@ #include "SystemEditorHelpers.h" #include "core/Log.h" +#include "core/StringHash.h" #include "core/macros.h" #include "editor/UndoStepType.h" #include "editor/EditorDraw.h" #include "galaxy/Factions.h" -#include "galaxy/Galaxy.h" #include "galaxy/NameGenerator.h" #include "galaxy/Sector.h" #include "galaxy/StarSystemGenerator.h" diff --git a/src/galaxy/GalaxyGenerator.h b/src/galaxy/GalaxyGenerator.h index 8044b9ad41..f0e2e2d32c 100644 --- a/src/galaxy/GalaxyGenerator.h +++ b/src/galaxy/GalaxyGenerator.h @@ -4,6 +4,7 @@ #ifndef GALAXYGENERATOR_H #define GALAXYGENERATOR_H +#include "Galaxy.h" #include "RefCounted.h" #include "Sector.h" #include "StarSystem.h" diff --git a/src/lua/Lua.cpp b/src/lua/Lua.cpp index 92f297901c..8647337a35 100644 --- a/src/lua/Lua.cpp +++ b/src/lua/Lua.cpp @@ -5,7 +5,6 @@ #include "Pi.h" #include "LuaColor.h" -#include "LuaConsole.h" #include "LuaConstants.h" #include "LuaDev.h" #include "LuaEconomy.h" @@ -35,9 +34,10 @@ #include "SystemView.h" #include "galaxy/StarSystem.h" -#include "pigui/LuaPiGui.h" #include "scenegraph/Lua.h" +#include "profiler/Profiler.h" + namespace Lua { LuaManager *manager = 0; diff --git a/src/lua/LuaStarSystem.cpp b/src/lua/LuaStarSystem.cpp index 7e83e2e018..7c709e7ee0 100644 --- a/src/lua/LuaStarSystem.cpp +++ b/src/lua/LuaStarSystem.cpp @@ -5,12 +5,10 @@ #include "FileSystem.h" #include "Game.h" #include "GameSaveError.h" -#include "LuaConstants.h" #include "LuaObject.h" #include "LuaTable.h" #include "LuaUtils.h" #include "Pi.h" -#include "Planet.h" #include "Space.h" #include "SpaceStation.h" #include "Star.h" @@ -21,6 +19,8 @@ #include "galaxy/Sector.h" #include "galaxy/StarSystem.h" +#include "profiler/Profiler.h" + /* * Class: StarSystem * diff --git a/src/test/TestProperty.cpp b/src/test/TestProperty.cpp index 428221905e..9a6b1fc91a 100644 --- a/src/test/TestProperty.cpp +++ b/src/test/TestProperty.cpp @@ -5,10 +5,11 @@ #include "Quaternion.h" #include "core/Log.h" #include "core/Property.h" +#include "profiler/Profiler.h" #include -#include #include +#include #include "doctest.h" static constexpr uint32_t ITERATIONS = 10000; diff --git a/src/test/TestStringName.cpp b/src/test/TestStringName.cpp index b82c12b0cb..478624d53f 100644 --- a/src/test/TestStringName.cpp +++ b/src/test/TestStringName.cpp @@ -3,6 +3,7 @@ #include "core/FNV1a.h" #include "core/Log.h" +#include "core/StringHash.h" #include "core/StringName.h" #include "profiler/Profiler.h"