Skip to content

Commit 6ac614c

Browse files
author
Jon Daniel
committed
switch to C++17 and use timestamp for saves
1 parent 3413763 commit 6ac614c

11 files changed

Lines changed: 64 additions & 207 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.1)
22

33
project(Chasm-Reverse)
44

5-
set(CMAKE_CXX_STANDARD 11)
5+
set(CMAKE_CXX_STANDARD 17)
66

77
if(MSVC)
88
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_NO_WARNINGS /MP")

src/client/client.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <chrono>
12
#include "../assert.hpp"
23
#include "../game_constants.hpp"
34
#include "../i_drawers_factory.hpp"
@@ -138,8 +139,11 @@ void Client::Save( SaveLoadBuffer& buffer, SaveComment& out_save_comment )
138139
for( const bool& wall_visibility : dynamic_walls_visibility )
139140
save_stream.WriteBool( wall_visibility );
140141

142+
143+
std::time_t timestamp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
144+
141145
// Write comment
142-
std::snprintf( out_save_comment.data(), sizeof(SaveComment), "Level%2d health %03d", current_map_data_->number, player_state_.health );
146+
std::snprintf( out_save_comment.data(), sizeof(SaveComment), "%s", ctime( &timestamp ) + 4);
143147
}
144148

145149
void Client::Load( const SaveLoadBuffer& buffer, unsigned int& buffer_pos )

src/common/files.cpp

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,47 +45,15 @@ const char* ExtractExtension( const char* const file_path )
4545

4646
} // namespace ChasmReverse
4747

48-
bool exists( const std::string& file_path )
48+
std::filesystem::path remove_extension( const std::filesystem::path& path )
4949
{
50-
struct stat sb;
51-
if(stat(file_path.c_str(), &sb) == 0)
52-
return true;
53-
return false;
54-
}
55-
56-
bool is_directory( const std::string& file_path )
57-
{
58-
struct stat sb;
59-
if((stat(file_path.c_str(), &sb) == 0) && ((sb.st_mode & S_IFMT) == S_IFDIR))
60-
return true;
61-
return false;
62-
}
63-
64-
bool is_regular_file( const std::string& file_path )
65-
{
66-
struct stat sb;
67-
if((stat(file_path.c_str(), &sb) == 0) && ((sb.st_mode & S_IFMT) == S_IFREG))
68-
return true;
69-
return false;
70-
}
7150

72-
bool is_symlink( const std::string& file_path )
73-
{
74-
struct stat sb;
75-
if((stat(file_path.c_str(), &sb) == 0) && ((sb.st_mode & S_IFMT) == S_IFLNK))
76-
return true;
77-
return false;
78-
}
51+
if( path == "." || path == ".." ) return path;
7952

80-
bool real_path( std::string& path )
81-
{
82-
char* dst = realpath(path.c_str(), NULL);
83-
if(dst == nullptr)
84-
return false;
53+
std::filesystem::path dst = path.stem();
8554

86-
path = dst;
87-
free(dst);
55+
while(!dst.extension().empty()) dst = dst.stem();
8856

89-
return true;
57+
return path.parent_path() / dst;
9058
}
9159

src/common/files.hpp

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <cstdio>
33
#include <cstring>
44
#include <string>
5+
#include <filesystem>
6+
57
// Include OS-dependend stuff for "mkdir".
68
#ifdef _WIN32
79
#include <direct.h>
@@ -17,75 +19,6 @@ void FileRead( std::FILE* const file, void* buffer, const unsigned int size );
1719
void FileWrite( std::FILE* const file, const void* buffer, const unsigned int size );
1820
const char* ExtractExtension( const char* const file_path );
1921

20-
2122
} // namespace ChasmReverse
2223

23-
template<class T>
24-
T dir_name( T const& path, T const& delims = "/\\" )
25-
{
26-
return path.substr(0, path.find_last_of(delims) + 1);
27-
}
28-
29-
template<class T>
30-
T base_name( T const& path, T const& delims = "/\\" )
31-
{
32-
return path.substr(path.find_last_of(delims) + 1);
33-
}
34-
35-
template<class T>
36-
T remove_extension( T const& filename )
37-
{
38-
typename T::size_type const p(base_name<std::string>(filename).find_last_of('.'));
39-
return ((p > 0 && p != T::npos) ? dir_name<std::string>(filename) + "/" + base_name<std::string>(filename).substr(0, p) : filename);
40-
}
41-
42-
template<class T>
43-
T extract_extension( T const& filename )
44-
{
45-
typename T::size_type const p(base_name<std::string>(filename).find_last_of('.'));
46-
return ((p > 0 && p != T::npos) ? dir_name<std::string>(filename) + "/" + base_name<std::string>(filename).substr(p, T::npos) : filename);
47-
}
48-
49-
bool exists( const std::string& file_path );
50-
bool is_directory( const std::string& file_path );
51-
bool is_regular_file( const std::string& file_path );
52-
bool is_symlink( const std::string& file_path );
53-
bool real_path( std::string& path );
54-
55-
template<class T>
56-
bool do_mkdir( T const& path )
57-
{
58-
struct stat sb;
59-
if(::stat(path.c_str(), &sb) != 0)
60-
{
61-
#ifdef _WIN32
62-
if((_mkdir(path.c_str()) != 0) && errno != EEXIST)
63-
#else
64-
if((mkdir(path.c_str(), 0777) != 0) && errno != EEXIST)
65-
#endif
66-
return false;
67-
}
68-
else if(!S_ISDIR(sb.st_mode))
69-
{
70-
errno = ENOTDIR;
71-
return false;
72-
}
73-
return true;
74-
}
75-
76-
template<class T>
77-
bool create_directories( T& path, T const& delims = "/\\" )
78-
{
79-
T build;
80-
for(typename T::size_type pos = 0; (pos = path.find('/')) != T::npos;)
81-
{
82-
build += path.substr(0, pos + 1);
83-
do_mkdir<T>(build);
84-
path.erase(0, pos + 1);
85-
}
86-
if (!path.empty()) {
87-
build += path;
88-
do_mkdir<T>(build);
89-
}
90-
return true;
91-
}
24+
std::filesystem::path remove_extension( const std::filesystem::path& path );

src/host.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <cstring>
2-
31
#include <framebuffer.hpp>
42
#include <glsl_program.hpp>
53
#include <ogl_state_manager.hpp>
@@ -402,28 +400,20 @@ void Host::GetSavesNames( SavesNames& out_saves_names )
402400
{
403401
SaveComment& out_save_comment= out_saves_names[slot];
404402

405-
char file_name[32];
406-
GetSaveFileNameForSlot( slot, file_name, sizeof(file_name) );
407-
408-
if( LoadSaveComment( file_name, out_save_comment ) )
409-
{ /* all ok */ }
410-
else
403+
std::filesystem::path file_name = GetSaveFileNameForSlot( slot );
404+
if( !LoadSaveComment( file_name, out_save_comment ) )
411405
out_save_comment[0]= '\0';
412406
}
413407
}
414408

415-
void Host::SaveGame( const unsigned int slot_number )
409+
void Host::SaveGame( const uint8_t slot_number )
416410
{
417-
char file_name[64];
418-
GetSaveFileNameForSlot( slot_number, file_name, sizeof(file_name) );
419-
DoSave( file_name );
411+
DoSave( GetSaveFileNameForSlot( slot_number ) );
420412
}
421413

422-
void Host::LoadGame( const unsigned int slot_number )
414+
void Host::LoadGame( const uint8_t slot_number )
423415
{
424-
char file_name[64];
425-
GetSaveFileNameForSlot( slot_number, file_name, sizeof(file_name) );
426-
DoLoad( file_name );
416+
DoLoad( GetSaveFileNameForSlot( slot_number ) );
427417
}
428418

429419
void Host::VidRestart()
@@ -610,7 +600,7 @@ void Host::DoRunLevel( const unsigned int map_number, const DifficultyType diffi
610600
is_single_player_= true;
611601
}
612602

613-
void Host::DoSave( const char* const save_file_name )
603+
void Host::DoSave( const std::filesystem::path& save_file_name )
614604
{
615605
if( !( local_server_ != nullptr && client_ != nullptr && is_single_player_ ) )
616606
{
@@ -632,7 +622,7 @@ void Host::DoSave( const char* const save_file_name )
632622
Log::User( "Game save failed." );
633623
}
634624

635-
void Host::DoLoad( const char* const save_file_name )
625+
void Host::DoLoad( const std::filesystem::path& save_file_name )
636626
{
637627
SaveLoadBuffer save_buffer;
638628
unsigned int save_buffer_pos= 0u;

src/host.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class Host final : public HostCommands
5050

5151
virtual void GetSavesNames( SavesNames& out_saves_names ) override;
5252
virtual bool SaveAvailable() const override;
53-
virtual void SaveGame( unsigned int slot_number ) override;
54-
virtual void LoadGame( unsigned int slot_number ) override;
53+
virtual void SaveGame( uint8_t slot_number ) override;
54+
virtual void LoadGame( uint8_t slot_number ) override;
5555

5656
virtual void VidRestart() override;
5757

@@ -70,8 +70,8 @@ class Host final : public HostCommands
7070
void DoVidRestart();
7171

7272
void DoRunLevel( unsigned int map_number, DifficultyType difficulty );
73-
void DoSave( const char* save_file_name );
74-
void DoLoad( const char* save_file_name );
73+
void DoSave( const std::filesystem::path& save_file_name );
74+
void DoLoad( const std::filesystem::path& save_file_name );
7575

7676
void DrawLoadingFrame( float progress, const char* caption );
7777

src/host_commands.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class HostCommands
3838
virtual void GetSavesNames( SavesNames& out_saves_names )= 0;
3939

4040
virtual bool SaveAvailable() const = 0;
41-
virtual void SaveGame( unsigned int slot_number )= 0;
42-
virtual void LoadGame( unsigned int slot_number )= 0;
41+
virtual void SaveGame( uint8_t slot_number )= 0;
42+
virtual void LoadGame( uint8_t slot_number )= 0;
4343

4444
virtual void VidRestart()= 0;
4545
};

src/save_load.cpp

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ using namespace ChasmReverse;
99

1010
#include "save_load.hpp"
1111

12-
#define SAVES_DIR "saves"
1312

1413
namespace PanzerChasm
1514
{
@@ -54,14 +53,14 @@ SaveHeader::HashType SaveHeader::CalculateHash( const unsigned char* data, unsig
5453
}
5554

5655
bool SaveData(
57-
const char* file_name,
56+
const std::filesystem::path& file_name,
5857
const SaveComment& save_comment,
5958
const SaveLoadBuffer& data )
6059
{
61-
FILE* f= std::fopen( file_name, "wb" );
60+
FILE* f= std::fopen( file_name.native().c_str(), "wb" );
6261
if( f == nullptr )
6362
{
64-
Log::Warning( "Can not write save \"", file_name, "\"" );
63+
Log::Warning( "Can not write save \"", file_name.native(), "\"" );
6564
return false;
6665
}
6766

@@ -81,13 +80,13 @@ bool SaveData(
8180

8281
// Returns true, if all ok
8382
bool LoadData(
84-
const char* file_name,
83+
const std::filesystem::path& file_name,
8584
SaveLoadBuffer& out_data )
8685
{
87-
FILE* const f= std::fopen( file_name, "rb" );
86+
FILE* const f= std::fopen( file_name.native().c_str(), "rb" );
8887
if( f == nullptr )
8988
{
90-
Log::Warning( "Can not read save \"", file_name, "\"." );
89+
Log::Warning( "Can not read save \"", file_name.native(), "\"." );
9190
return false;
9291
}
9392

@@ -146,10 +145,10 @@ bool LoadData(
146145
}
147146

148147
bool LoadSaveComment(
149-
const char* file_name,
148+
const std::filesystem::path& file_name,
150149
SaveComment& out_save_comment )
151150
{
152-
FILE* const f= std::fopen( file_name, "rb" );
151+
FILE* const f= std::fopen( file_name.native().c_str(), "rb" );
153152
if( f == nullptr )
154153
{
155154
return false;
@@ -172,57 +171,16 @@ bool LoadSaveComment(
172171
return true;
173172
}
174173

175-
void GetSaveFileNameForSlot(
176-
const unsigned int slot_number,
177-
char* const out_file_name,
178-
const unsigned int out_file_name_max_length )
174+
std::filesystem::path GetSaveFileNameForSlot(const uint8_t slot_number)
179175
{
180-
std::snprintf( out_file_name, out_file_name_max_length, SAVES_DIR"/save_%02d.pcs", slot_number );
181-
}
182-
183-
std::string GetScreenshotFileNameForDir(const std::string& dir)
184-
{
185-
static uint8_t slot_number = 0;
186-
char* str = nullptr;
187-
188-
// TODO: use list directory to determine slot_number
189-
ssize_t len = asprintf( &str, "%s/cshot_%02d.tga", dir.empty() ? SAVES_DIR : dir.c_str(), slot_number );
190-
if(len < 14)
191-
{
192-
free(str);
193-
Log::Warning( "Couldn't allocate memory for screenshot name: ", errno, " - ", strerror(errno));
194-
return std::string();
195-
}
196-
197-
slot_number = slot_number < 99 ? slot_number + 1 : 0;
198-
std::string dst(str, len);
199-
200-
free(str);
201-
return dst;
202-
}
203-
204-
std::string CreateScreenshotsDir(const std::string& file)
205-
{
206-
std::string dst_dir = dir_name<std::string>(file);
207-
std::string dst_file = base_name<std::string>(file);
208-
std::string cwd = get_current_dir_name();
209-
210-
if(dst_dir.empty())
211-
dst_dir = file[0] == '/' ? "/" : std::string(get_current_dir_name()) + "/" + SAVES_DIR;
212-
213-
if((!exists( dst_dir ) && !create_directories<std::string>( dst_dir )) || !real_path( dst_dir ))
214-
{
215-
Log::Warning("Couldn't create screenshot directory: ", dst_dir, " - ", strerror(errno));
216-
dst_dir.clear();
217-
}
218-
219-
return dst_dir;
176+
static char tmp[12] = "save_";
177+
std::snprintf( tmp, 12, "save_%02hhu.pcs", slot_number );
178+
return std::filesystem::absolute(SAVES_DIR / tmp);
220179
}
221180

222181
void CreateSlotSavesDir()
223182
{
224-
std::string dir(SAVES_DIR);
225-
if(!create_directories<std::string>( dir ))
183+
if(!std::filesystem::exists(SAVES_DIR) && !std::filesystem::create_directories( SAVES_DIR ))
226184
Log::Warning("Couldn't create saves directory: ", strerror(errno));
227185
}
228186

0 commit comments

Comments
 (0)