2019-07-26 21:05:24 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "TracyStorage.hpp"
|
|
|
|
#include "TracyUserData.hpp"
|
2019-08-28 17:45:22 +00:00
|
|
|
#include "TracyViewData.hpp"
|
2019-07-26 21:05:24 +00:00
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
constexpr auto FileDescription = "description";
|
2019-08-28 17:45:22 +00:00
|
|
|
constexpr auto FileTimeline = "timeline";
|
|
|
|
|
|
|
|
enum : uint32_t { VersionTimeline = 0 };
|
2019-07-26 21:05:24 +00:00
|
|
|
|
|
|
|
UserData::UserData()
|
2019-08-28 17:37:01 +00:00
|
|
|
: m_preserveState( false )
|
2019-07-26 21:05:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UserData::UserData( const char* program, uint64_t time )
|
|
|
|
: m_program( program )
|
|
|
|
, m_time( time )
|
|
|
|
{
|
2019-08-28 17:28:31 +00:00
|
|
|
FILE* f = OpenFile( FileDescription, false );
|
|
|
|
if( f )
|
2019-07-26 21:05:24 +00:00
|
|
|
{
|
2019-08-28 17:28:31 +00:00
|
|
|
fseek( f, 0, SEEK_END );
|
|
|
|
const auto sz = ftell( f );
|
|
|
|
fseek( f, 0, SEEK_SET );
|
|
|
|
auto buf = std::make_unique<char[]>( sz );
|
|
|
|
fread( buf.get(), 1, sz, f );
|
|
|
|
fclose( f );
|
|
|
|
m_description.assign( buf.get(), buf.get() + sz );
|
2019-07-26 21:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserData::Init( const char* program, uint64_t time )
|
|
|
|
{
|
|
|
|
assert( !Valid() );
|
|
|
|
m_program = program;
|
|
|
|
m_time = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UserData::SetDescription( const char* description )
|
|
|
|
{
|
|
|
|
assert( Valid() );
|
|
|
|
|
|
|
|
m_description = description;
|
|
|
|
const auto sz = m_description.size();
|
|
|
|
|
2019-08-28 17:28:31 +00:00
|
|
|
FILE* f = OpenFile( FileDescription, true );
|
2019-07-26 21:05:24 +00:00
|
|
|
if( !f ) return false;
|
|
|
|
|
|
|
|
fwrite( description, 1, sz, f );
|
|
|
|
fclose( f );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-28 17:45:22 +00:00
|
|
|
void UserData::LoadState( ViewData& data )
|
|
|
|
{
|
|
|
|
assert( Valid() );
|
|
|
|
FILE* f = OpenFile( FileTimeline, false );
|
|
|
|
if( !f ) return;
|
|
|
|
uint32_t ver;
|
|
|
|
fread( &ver, 1, sizeof( ver ), f );
|
|
|
|
if( ver == VersionTimeline )
|
|
|
|
{
|
|
|
|
fread( &data.zvStart, 1, sizeof( data.zvStart ), f );
|
|
|
|
fread( &data.zvEnd, 1, sizeof( data.zvEnd ), f );
|
2019-08-28 17:49:27 +00:00
|
|
|
fread( &data.zvHeight, 1, sizeof( data.zvHeight ), f );
|
|
|
|
fread( &data.zvScroll, 1, sizeof( data.zvScroll ), f );
|
2019-08-28 17:52:36 +00:00
|
|
|
fread( &data.frameScale, 1, sizeof( data.frameScale ), f );
|
|
|
|
fread( &data.frameStart, 1, sizeof( data.frameStart ), f );
|
2019-08-28 17:45:22 +00:00
|
|
|
}
|
|
|
|
fclose( f );
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserData::SaveState( const ViewData& data )
|
|
|
|
{
|
|
|
|
if( !m_preserveState ) return;
|
|
|
|
assert( Valid() );
|
|
|
|
FILE* f = OpenFile( FileTimeline, true );
|
|
|
|
if( !f ) return;
|
|
|
|
uint32_t ver = VersionTimeline;
|
|
|
|
fwrite( &ver, 1, sizeof( ver ), f );
|
|
|
|
fwrite( &data.zvStart, 1, sizeof( data.zvStart ), f );
|
|
|
|
fwrite( &data.zvEnd, 1, sizeof( data.zvEnd ), f );
|
2019-08-28 17:49:27 +00:00
|
|
|
fwrite( &data.zvHeight, 1, sizeof( data.zvHeight ), f );
|
|
|
|
fwrite( &data.zvScroll, 1, sizeof( data.zvScroll ), f );
|
2019-08-28 17:52:36 +00:00
|
|
|
fwrite( &data.frameScale, 1, sizeof( data.frameScale ), f );
|
|
|
|
fwrite( &data.frameStart, 1, sizeof( data.frameStart ), f );
|
2019-08-28 17:45:22 +00:00
|
|
|
fclose( f );
|
|
|
|
}
|
|
|
|
|
2019-08-28 17:37:01 +00:00
|
|
|
void UserData::StateShouldBePreserved()
|
|
|
|
{
|
|
|
|
m_preserveState = true;
|
|
|
|
}
|
|
|
|
|
2019-08-28 17:28:31 +00:00
|
|
|
FILE* UserData::OpenFile( const char* filename, bool write )
|
|
|
|
{
|
|
|
|
const auto path = GetSavePath( m_program.c_str(), m_time, filename, write );
|
|
|
|
if( !path ) return nullptr;
|
|
|
|
FILE* f = fopen( path, write ? "wb" : "rb" );
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2019-07-26 21:05:24 +00:00
|
|
|
}
|