From: MandhelingFreak Date: Mon, 10 Mar 2014 19:43:01 +0000 (+0900) Subject: 航路保存機能試作。 X-Git-Tag: release/ver1.3.1~4 X-Git-Url: http://git.osdn.net/view?p=gvonavish%2FGVONavish.git;a=commitdiff_plain;h=d26cb3a47bdd65aa62b5c516f081664532a3fe91 航路保存機能試作。 --- diff --git a/GVONavish/GVONavish/GVONavish.cpp b/GVONavish/GVONavish/GVONavish.cpp index 78cdc6d..ddcc008 100644 --- a/GVONavish/GVONavish/GVONavish.cpp +++ b/GVONavish/GVONavish/GVONavish.cpp @@ -87,6 +87,7 @@ static GVOConfig s_config( k_configFileName ); static GVOGameProcess s_gvoGameProcess; static GVORenderer s_renderer; static GVOWorldMap s_worldMap; +const std::wstring && k_routeListFilePath = g_makeFullPath(L"RouteList.dat"); static std::unique_ptr s_shipRouteList; static POINT s_latestSurveyCoord; static GVOVector s_latestShipVector; @@ -149,6 +150,7 @@ int APIENTRY _tWinMain( _In_ HINSTANCE hInstance, // ƒƒCƒ“ ƒƒbƒZ[ƒW ƒ‹[ƒv: const LRESULT retVal = s_mainLoop(); + s_shipRouteList->saveToFile( k_routeListFilePath ); s_gvoGameProcess.teardown(); s_config.save(); @@ -216,6 +218,7 @@ static BOOL InitInstance( HINSTANCE hInstance, int nCmdShow ) s_renderer.setup( &s_config, g_hdcMain, &s_worldMap ); s_shipRouteList.reset( new GVOShipRouteList() ); + s_shipRouteList->loadFromFile( k_routeListFilePath ); s_pollingInterval = s_config.m_pollingInterval; s_gvoGameProcess.setup( s_config ); diff --git a/GVONavish/GVONavish/GVONormalizedPoint.h b/GVONavish/GVONavish/GVONormalizedPoint.h index 6e68ce0..18af47a 100644 --- a/GVONavish/GVONavish/GVONormalizedPoint.h +++ b/GVONavish/GVONavish/GVONormalizedPoint.h @@ -31,4 +31,8 @@ public: { return m_y; } + }; + +// ƒtƒ@ƒCƒ‹“üo—Í—p‚ɃAƒ‰ƒCƒƒ“ƒgŠm”F +static_assert(sizeof(GVONormalizedPoint) == (sizeof(float)*2), "bat size."); diff --git a/GVONavish/GVONavish/GVOShipRoute.h b/GVONavish/GVONavish/GVOShipRoute.h index 3470c6c..954091e 100644 --- a/GVONavish/GVONavish/GVOShipRoute.h +++ b/GVONavish/GVONavish/GVOShipRoute.h @@ -1,11 +1,12 @@ #pragma once #include +#include #include "GVONormalizedPoint.h" //!@brief q˜H class GVOShipRoute { public: - typedef std::deque Line; + typedef std::vector Line; typedef std::deque Lines; private: Lines m_lines; @@ -14,6 +15,9 @@ private: bool m_hilight = false; bool m_fixed = false; //!<@brief q˜HŒÅ’èƒtƒ‰ƒO + std::time_t m_earliestDateTime = std::time_t(); + std::time_t m_latestDateTime = std::time_t(); + public: GVOShipRoute() = default; ~GVOShipRoute() = default; @@ -78,6 +82,39 @@ public: { return m_length; } + + std::time_t earliestDateTime() const + { + return m_earliestDateTime; + } + + // ‚¿‚å‚Á‚Ɖ˜‚¢‚©‚ȁ[ + void setEarliestDateTime( const std::time_t dateTime ) + { + m_earliestDateTime = dateTime; + } + + // ‚¿‚å‚Á‚Ɖ˜‚¢‚©‚ȁ[ + std::time_t latestDateTime() const + { + return m_latestDateTime; + } + + // ‚¿‚å‚Á‚Ɖ˜‚¢‚©‚ȁ[ + void setLatestDateTime( const std::time_t dateTime ) + { + m_latestDateTime = dateTime; + } + + // ‚¿‚å‚Á‚Ɖ˜‚¢‚©‚ȁ[ + void addLine( Line & line ) + { + m_lines.push_back( line ); + } + void addLine( Line && line ) + { + m_lines.push_back( line ); + } private: }; diff --git a/GVONavish/GVONavish/GVOShipRouteList.cpp b/GVONavish/GVONavish/GVOShipRouteList.cpp index b343545..01e9789 100644 --- a/GVONavish/GVONavish/GVOShipRouteList.cpp +++ b/GVONavish/GVONavish/GVOShipRouteList.cpp @@ -2,6 +2,103 @@ #include "GVOShipRouteList.h" +namespace { + struct FileHeaderV1 { + enum { + k_FileVersion = 1, + }; + const uint32_t version = k_FileVersion; + uint32_t favoritsCount = 0; + }; + + typedef FileHeaderV1 FileHeader; +} + + +bool GVOShipRouteList::saveToFile( const std::wstring & filePath ) +{ + std::unique_ptr file(::_wfopen(filePath.c_str(), L"wb"), &::fclose); + if ( !file ) { + return false; + } + + FileHeader fileHeader; + + ::fseek( file.get(), sizeof(FileHeader), SEEK_SET ); + + for ( const auto & shipRoute : m_shipRouteList ) { + if ( shipRoute->isFavorite() ) { + ++fileHeader.favoritsCount; + + std::time_t dateTime = std::time_t(); + dateTime = shipRoute->earliestDateTime(); + ::fwrite( &dateTime, sizeof(dateTime), 1, file.get() ); + dateTime = shipRoute->latestDateTime(); + ::fwrite( &dateTime, sizeof(dateTime), 1, file.get() ); + + const size_t lineCount = shipRoute->getLines().size(); + ::fwrite( &lineCount, sizeof(lineCount), 1, file.get() ); + + for ( const auto & line : shipRoute->getLines() ) { + const size_t count = line.size(); + ::fwrite( &count, sizeof(count), 1, file.get() ); + if ( !line.empty() ) { + ::fwrite( &line[0], sizeof(line[0]), line.size(), file.get() ); + } + } + } + } + + ::fseek( file.get(), 0, SEEK_SET ); + ::fwrite( &fileHeader, sizeof(fileHeader), 1, file.get() ); + return true; +} + + +bool GVOShipRouteList::loadFromFile( const std::wstring & filePath ) +{ + std::unique_ptr file( ::_wfopen( filePath.c_str(), L"rb" ), &::fclose ); + if ( !file ) { + return false; + } + + FileHeader fileHeader; + ::fread( &fileHeader, sizeof( fileHeader ), 1, file.get() ); + + if ( fileHeader.version != FileHeader::k_FileVersion ) { + return false; + } + + m_shipRouteList.clear(); + + for ( uint32_t i = 0; i < fileHeader.favoritsCount; ++i ) { + GVOShipRoutePtr shipRoute( new GVOShipRoute() ); + shipRoute->setFavorite( true ); + + std::time_t dateTime = std::time_t(); + ::fread( &dateTime, sizeof(dateTime), 1, file.get() ); + shipRoute->setEarliestDateTime( dateTime ); + ::fread( &dateTime, sizeof(dateTime), 1, file.get() ); + shipRoute->setLatestDateTime( dateTime ); + + size_t lineCount = 0; + ::fread( &lineCount, sizeof(lineCount), 1, file.get() ); + + for ( size_t k = 0; k < lineCount; ++k ) { + size_t pointCount = 0; + ::fread( &pointCount, sizeof(pointCount), 1, file.get() ); + if ( 0 < pointCount ) { + GVOShipRoute::Line tmp( pointCount ); + ::fread( &tmp[0], sizeof(tmp[0]), tmp.size(), file.get() ); + shipRoute->addLine( std::move( tmp ) ); + } + } + + m_shipRouteList.push_back( std::move( shipRoute ) ); + } + + return true; +} void GVOShipRouteList::closeRoute() diff --git a/GVONavish/GVONavish/GVOShipRouteList.h b/GVONavish/GVONavish/GVOShipRouteList.h index 436277d..68dbd0b 100644 --- a/GVONavish/GVONavish/GVOShipRouteList.h +++ b/GVONavish/GVONavish/GVOShipRouteList.h @@ -11,6 +11,8 @@ class IGVOShipRouteListObserver; class GVOShipRouteList { private: typedef std::list RouteList; + +private: RouteList m_shipRouteList; IGVOShipRouteListObserver * m_observer = nullptr; size_t m_maxRouteCountWithoutFavorits = 30; //!<@brief ‚¨‹C‚É“ü‚è‚ðœŠO‚µ‚½q˜H•Û‘¶” @@ -19,6 +21,13 @@ public: GVOShipRouteList() = default; ~GVOShipRouteList() = default; + //!@note ‚ ‚ñ‚Ü‚ès‹V‚ª—Ç‚¢Ž–‚¶‚á‚È‚¢‚¯‚ê‚ǁB + bool saveToFile( const std::wstring & filePath ); + + //!@note ‚ ‚ñ‚Ü‚ès‹V‚ª—Ç‚¢Ž–‚¶‚á‚È‚¢‚¯‚ê‚ǁB + bool loadFromFile( const std::wstring & filePath ); + + void setObserver( IGVOShipRouteListObserver * observer ) { m_observer = observer; @@ -55,8 +64,6 @@ public: return reverseIndex; } - //void removeShipRouteAtReverseIndex( int reverseIndex ); - void removeShipRoute( GVOShipRoutePtr shipRoute ); void clearAllItems();