Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Proof-of-concept Implementation for new save format (#5771) #5772

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ include(CheckCXXCompilerFlag)
#SET(CMAKE_SHARED_LIBRARY_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CXX_FLAGS} -m32")

find_package(PkgConfig)
find_package(SQLite3)
if (NOT DYNAMIC_LINKING)
if(NOT MSVC)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.dll.a")
Expand Down
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
${CMAKE_SOURCE_DIR}/pch/main-pch.hpp)
endif ()

target_link_libraries(cataclysm-bn-tiles-common PUBLIC SQLite::SQLite3)

Check failure on line 72 in src/CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Build

Target "cataclysm-bn-tiles-common" links to:

if (CMAKE_USE_PTHREADS_INIT)
target_compile_options(cataclysm-bn-tiles-common PUBLIC "-pthread")
endif ()
Expand Down Expand Up @@ -181,6 +183,8 @@
target_include_directories(cataclysm-bn-common PUBLIC ${CURSES_INCLUDE_DIR})
target_link_libraries(cataclysm-bn-common PUBLIC ${CURSES_LIBRARIES})

target_link_libraries(cataclysm-bn-tiles-common PUBLIC SQLite::SQLite3)

if (CMAKE_USE_PTHREADS_INIT)
target_compile_options(cataclysm-bn-common PUBLIC "-pthread")
endif ()
Expand Down
17 changes: 12 additions & 5 deletions src/diary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,13 @@ void diary::export_to_md( bool last_export )

bool diary::store()
{
if( !g->get_world_db() ) {
return false;
}

std::string name = base64_encode( get_avatar().get_save_id() + "_diary" );
std::string path = g->get_world_base_save_path() + "/" + name + ".json";
const bool is_writen = write_to_file( path, [&]( std::ostream & fout ) {
const bool is_writen = g->get_world_db()->write_to_file( name + ".json", [&](
std::ostream & fout ) {
serialize( fout );
}, _( "diary data" ) );
return is_writen;
Expand Down Expand Up @@ -826,10 +830,13 @@ void diary::serialize( JsonOut &jsout )

void diary::load()
{
if( !g->get_world_db() ) {
return;
}

std::string name = base64_encode( get_avatar().get_save_id() + "_diary" );
std::string path = g->get_world_base_save_path() + "/" + name + ".json";
if( file_exist( path ) ) {
read_from_file( path, [&]( std::istream & fin ) {
if( g->get_world_db()->file_exist( name + ".json" ) ) {
g->get_world_db()->read_from_file( name + ".json", [&]( std::istream & fin ) {
deserialize( fin );
} );
}
Expand Down
5 changes: 5 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12141,6 +12141,11 @@ std::string game::get_world_base_save_path() const
return world_generator->active_world->folder_path();
}

shared_ptr_fast<worlddb> game::get_world_db()
{
return world_generator->active_world_db;
}

void game::shift_destination_preview( point delta )
{
for( tripoint &p : destination_preview ) {
Expand Down
6 changes: 6 additions & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "pimpl.h"
#include "point.h"
#include "type_id.h"
#include "worlddb.h"

class Character;
class Creature_tracker;
Expand Down Expand Up @@ -168,6 +169,11 @@ class game
*/
std::string get_world_base_save_path() const;

/**
* @return The current world database, or nullptr if no world is loaded.
*/
shared_ptr_fast<worlddb> get_world_db();

/**
* @brief Should be invoked whenever options change.
*/
Expand Down
18 changes: 9 additions & 9 deletions src/mapbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static std::string find_quad_path( const std::string &dirname, const tripoint &o
static std::string find_dirname( const tripoint &om_addr )
{
const tripoint segment_addr = omt_to_seg_copy( om_addr );
return string_format( "%s/maps/%d.%d.%d", g->get_world_base_save_path(), segment_addr.x,
return string_format( "maps/%d.%d.%d", segment_addr.x,
segment_addr.y, segment_addr.z );
}

Expand Down Expand Up @@ -97,7 +97,7 @@ submap *mapbuffer::lookup_submap( const tripoint &p )

void mapbuffer::save( bool delete_after_save )
{
assure_dir_exist( g->get_world_base_save_path() + "/maps" );
// assure_dir_exist( g->get_world_base_save_path() + "/maps" );

int num_saved_submaps = 0;
int num_total_submaps = submaps.size();
Expand Down Expand Up @@ -144,7 +144,7 @@ void mapbuffer::save( bool delete_after_save )
// delete_on_save deletes everything, otherwise delete submaps
// outside the current map.
const bool zlev_del = !map_has_zlevels && om_addr.z != g->get_levz();
save_quad( dirname, quad_path, om_addr, submaps_to_delete,
save_quad( quad_path, om_addr, submaps_to_delete,
delete_after_save || zlev_del ||
om_addr.x < map_origin.x || om_addr.y < map_origin.y ||
om_addr.x > map_origin.x + HALF_MAPSIZE ||
Expand All @@ -158,7 +158,7 @@ void mapbuffer::save( bool delete_after_save )
get_distribution_grid_tracker().on_saved();
}

void mapbuffer::save_quad( const std::string &dirname, const std::string &filename,
void mapbuffer::save_quad( const std::string &filename,
const tripoint &om_addr, std::list<tripoint> &submaps_to_delete,
bool delete_after_save )
{
Expand Down Expand Up @@ -199,8 +199,7 @@ void mapbuffer::save_quad( const std::string &dirname, const std::string &filena
}

// Don't create the directory if it would be empty
assure_dir_exist( dirname );
write_to_file( filename, [&]( std::ostream & fout ) {
g->get_world_db()->write_to_file( filename, [&]( std::ostream & fout ) {
JsonOut jsout( fout );
jsout.start_array();
for( auto &submap_addr : submap_addrs ) {
Expand Down Expand Up @@ -247,20 +246,21 @@ submap *mapbuffer::unserialize_submaps( const tripoint &p )
const std::string dirname = find_dirname( om_addr );
std::string quad_path = find_quad_path( dirname, om_addr );

if( !file_exist( quad_path ) ) {
if( !g->get_world_db()->file_exist( quad_path ) ) {
// Fix for old saves where the path was generated using std::stringstream, which
// did format the number using the current locale. That formatting may insert
// thousands separators, so the resulting path is "map/1,234.7.8.map" instead
// of "map/1234.7.8.map".
std::ostringstream buffer;
buffer << dirname << "/" << om_addr.x << "." << om_addr.y << "." << om_addr.z << ".map";
if( file_exist( buffer.str() ) ) {
if( g->get_world_db()->file_exist( buffer.str() ) ) {
quad_path = buffer.str();
}
}

using namespace std::placeholders;
if( !read_from_file_optional_json( quad_path, std::bind( &mapbuffer::deserialize, this, _1 ) ) ) {
if( !g->get_world_db()->read_from_file_optional_json( quad_path, std::bind( &mapbuffer::deserialize,
this, _1 ) ) ) {
// If it doesn't exist, trigger generating it.
return nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mapbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class mapbuffer
void remove_submap( tripoint addr );
submap *unserialize_submaps( const tripoint &p );
void deserialize( JsonIn &jsin );
void save_quad( const std::string &dirname, const std::string &filename,
void save_quad( const std::string &filename,
const tripoint &om_addr, std::list<tripoint> &submaps_to_delete,
bool delete_after_save );
submap_map_t submaps;
Expand Down
5 changes: 3 additions & 2 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6086,7 +6086,7 @@ void overmap::open( overmap_special_batch &enabled_specials )
overmap::unserialize( fin, terfilename );
};

if( read_from_file_optional( terfilename, ter_reader ) ) {
if( g->get_world_db()->read_from_file_optional( terfilename, ter_reader ) ) {
const std::string plrfilename = overmapbuffer::player_filename( loc );
const auto plr_reader = [&]( std::istream & fin ) {
overmap::unserialize_view( fin, plrfilename );
Expand Down Expand Up @@ -6115,7 +6115,8 @@ void overmap::save() const
serialize_view( stream );
} );

write_to_file( overmapbuffer::terrain_filename( loc ), [&]( std::ostream & stream ) {
g->get_world_db()->write_to_file( overmapbuffer::terrain_filename( loc ), [&](
std::ostream & stream ) {
serialize( stream );
} );
}
Expand Down
4 changes: 2 additions & 2 deletions src/overmapbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ omt_route_params::~omt_route_params() = default;

std::string overmapbuffer::terrain_filename( const point_abs_om &p )
{
return string_format( "%s/o.%d.%d", g->get_world_base_save_path(), p.x(), p.y() );
return string_format( "o.%d.%d", p.x(), p.y() );
}

std::string overmapbuffer::player_filename( const point_abs_om &p )
Expand Down Expand Up @@ -267,7 +267,7 @@ overmap *overmapbuffer::get_existing( const point_abs_om &p )
// checked in a previous call of this function).
return nullptr;
}
if( file_exist( terrain_filename( p ) ) ) {
if( g->get_world_db() && g->get_world_db()->file_exist( terrain_filename( p ) ) ) {
// File exists, load it normally (the get function
// indirectly call overmap::open to do so).
return &get( p );
Expand Down
Loading
Loading