diff --git a/server/TracyUserData.cpp b/server/TracyUserData.cpp index 1c62d488..4db79bea 100644 --- a/server/TracyUserData.cpp +++ b/server/TracyUserData.cpp @@ -11,9 +11,11 @@ namespace tracy constexpr auto FileDescription = "description"; constexpr auto FileTimeline = "timeline"; constexpr auto FileOptions = "options"; +constexpr auto FileAnnotations = "annotations"; enum : uint32_t { VersionTimeline = 0 }; enum : uint32_t { VersionOptions = 2 }; +enum : uint32_t { VersionAnnotations = 0 }; UserData::UserData() : m_preserveState( false ) @@ -143,6 +145,74 @@ void UserData::StateShouldBePreserved() m_preserveState = true; } +void UserData::LoadAnnotations( std::vector>& data ) +{ + assert( Valid() ); + FILE* f = OpenFile( FileAnnotations, false ); + if( f ) + { + uint32_t ver; + fread( &ver, 1, sizeof( ver ), f ); + if( ver == VersionAnnotations ) + { + uint32_t sz; + fread( &sz, 1, sizeof( sz ), f ); + for( uint32_t i=0; i(); + + uint32_t tsz; + fread( &tsz, 1, sizeof( tsz ), f ); + if( tsz != 0 ) + { + char buf[1024]; + assert( tsz < 1024 ); + fread( buf, 1, tsz, f ); + ann->text.assign( buf, tsz ); + } + fread( &ann->start, 1, sizeof( ann->start ), f ); + fread( &ann->end, 1, sizeof( ann->end ), f ); + fread( &ann->color, 1, sizeof( ann->color ), f ); + + data.emplace_back( std::move( ann ) ); + } + } + fclose( f ); + } +} + +void UserData::SaveAnnotations( const std::vector>& data ) +{ + if( !m_preserveState ) return; + if( data.empty() ) + { + Remove( FileAnnotations ); + return; + } + assert( Valid() ); + FILE* f = OpenFile( FileAnnotations, true ); + if( f ) + { + uint32_t ver = VersionAnnotations; + fwrite( &ver, 1, sizeof( ver ), f ); + uint32_t sz = uint32_t( data.size() ); + fwrite( &sz, 1, sizeof( sz ), f ); + for( auto& ann : data ) + { + sz = uint32_t( ann->text.size() ); + fwrite( &sz, 1, sizeof( sz ), f ); + if( sz != 0 ) + { + fwrite( ann->text.c_str(), 1, sz, f ); + } + fwrite( &ann->start, 1, sizeof( ann->start ), f ); + fwrite( &ann->end, 1, sizeof( ann->end ), f ); + fwrite( &ann->color, 1, sizeof( ann->color ), f ); + } + fclose( f ); + } +} + FILE* UserData::OpenFile( const char* filename, bool write ) { const auto path = GetSavePath( m_program.c_str(), m_time, filename, write ); diff --git a/server/TracyUserData.hpp b/server/TracyUserData.hpp index f361325b..b1d04c3a 100644 --- a/server/TracyUserData.hpp +++ b/server/TracyUserData.hpp @@ -1,13 +1,16 @@ #ifndef __TRACYUSERDATA_HPP__ #define __TRACYUSERDATA_HPP__ +#include #include #include #include +#include namespace tracy { +struct Annotation; struct ViewData; class UserData @@ -26,6 +29,9 @@ public: void SaveState( const ViewData& data ); void StateShouldBePreserved(); + void LoadAnnotations( std::vector>& data ); + void SaveAnnotations( const std::vector>& data ); + private: FILE* OpenFile( const char* filename, bool write ); void Remove( const char* filename ); diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 4b6e5483..7f33c47f 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -151,12 +151,14 @@ View::View( FileRead& f, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetViewToLastFrames(); m_userData.StateShouldBePreserved(); m_userData.LoadState( m_vd ); + m_userData.LoadAnnotations( m_annotations ); } View::~View() { m_worker.Shutdown(); m_userData.SaveState( m_vd ); + m_userData.SaveAnnotations( m_annotations ); if( m_compare.loadThread.joinable() ) m_compare.loadThread.join(); if( m_saveThread.joinable() ) m_saveThread.join();