Skip to content

Commit 84e5001

Browse files
author
Jon Daniel
committed
Allow load of addon_path without csm.bin
1 parent f37e615 commit 84e5001

3 files changed

Lines changed: 69 additions & 67 deletions

File tree

src/common/str.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,28 @@ std::filesystem::path remove_extension( const std::filesystem::path& path )
2121
return path.parent_path() / dst;
2222
}
2323

24+
const char* ExtractFileName( const char* const file_path )
25+
{
26+
const char* file_name_pos= file_path;
27+
28+
const char* str= file_path;
29+
while( *str != '\0' )
30+
{
31+
if( *str == '/' || *str == '\\' )
32+
file_name_pos= str + 1u;
33+
34+
str++;
35+
}
36+
37+
return file_name_pos;
38+
}
39+
40+
std::string ToUpper( const std::string& s )
41+
{
42+
std::string r= s;
43+
for( char& c : r )
44+
c= std::toupper(c);
45+
return r;
46+
}
47+
48+

src/common/str.hpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
23
#ifdef _MSC_VER
34
#define strcasecmp _stricmp
45
#define strncasecmp _strnicmp
@@ -28,16 +29,28 @@ inline char* strncasestr(const char* haystack, const char* needle, size_t n)
2829
#include <filesystem>
2930

3031
#define GetSubstring strcasestr
31-
constexpr bool StringEquals( const char* s0, const char* s1 )
32-
{
33-
return ( ::strcasecmp( s0, s1 ) == 0 );
34-
}
35-
36-
constexpr bool StringNEquals( const char* s0, const char* s1, size_t n )
32+
constexpr bool StringEquals( const char* s0, const char* s1, size_t n = 0)
3733
{
38-
return ( ::strncasecmp( s0, s1, n ) == 0 );
34+
return n == 0 ? ( ::strcasecmp( s0, s1 ) == 0 ) : ( ::strncasecmp( s0, s1, n ) == 0 );
3935
}
4036

37+
const char* ExtractFileName( const char* const file_path );
4138
const char* ExtractExtension( const char* const file_path );
4239
std::filesystem::path remove_extension( const std::filesystem::path& path );
40+
std::string ToUpper( const std::string& s );
41+
/*
42+
// Own replacement for nonstandard strncasecmp
43+
static bool StringEquals( const char* const s0, const char* const s1, const unsigned int max_length )
44+
{
45+
unsigned int i= 0;
46+
while( s0[i] != '\0' && s1[i] != '\0' && i < max_length )
47+
{
48+
if( std::tolower( s0[i] ) != std::tolower( s1[i] ) )
49+
return false;
50+
i++;
51+
}
52+
53+
return i == max_length || s0[i] == s1[i];
54+
}
55+
*/
4356

src/vfs.cpp

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,6 @@ struct FileInfoPacked
2222
};
2323
#pragma pack(pop)
2424

25-
// Own replacement for nonstandard strncasecmp
26-
static bool StringEquals( const char* const s0, const char* const s1, const unsigned int max_length )
27-
{
28-
unsigned int i= 0;
29-
while( s0[i] != '\0' && s1[i] != '\0' && i < max_length )
30-
{
31-
if( std::tolower( s0[i] ) != std::tolower( s1[i] ) )
32-
return false;
33-
i++;
34-
}
35-
36-
return i == max_length || s0[i] == s1[i];
37-
}
38-
39-
static std::string ToUpper( const std::string& s )
40-
{
41-
std::string r= s;
42-
for( char& c : r )
43-
c= std::toupper(c);
44-
return r;
45-
}
46-
47-
static const char* ExtractFileName( const char* const file_path )
48-
{
49-
const char* file_name_pos= file_path;
50-
51-
const char* str= file_path;
52-
while( *str != '\0' )
53-
{
54-
if( *str == '/' || *str == '\\' )
55-
file_name_pos= str + 1u;
56-
57-
str++;
58-
}
59-
60-
return file_name_pos;
61-
}
62-
6325
static std::string PrepareAddonPath( const char* const addon_path )
6426
{
6527
if( addon_path == nullptr )
@@ -115,36 +77,38 @@ Vfs::Vfs(
11577
: archive_file_( std::fopen( archive_file_name, "rb" ) )
11678
, addon_path_( PrepareAddonPath( addon_path ) )
11779
{
118-
if( archive_file_ == nullptr )
80+
if( archive_file_ != nullptr )
11981
{
120-
Log::FatalError( "Could not open file \"", archive_file_name, "\"" );
121-
return;
122-
}
82+
char header[4];
83+
FileRead( archive_file_, header, sizeof(header) );
84+
if( std::strncmp( header, "CSid", sizeof(header) ) != 0 )
85+
{
86+
Log::FatalError( "File \"", archive_file_name, "\" is not \"Chasm: The Rift\" archive" );
87+
return;
88+
}
12389

124-
char header[4];
125-
FileRead( archive_file_, header, sizeof(header) );
126-
if( std::strncmp( header, "CSid", sizeof(header) ) != 0 )
127-
{
128-
Log::FatalError( "File \"", archive_file_name, "\" is not \"Chasm: The Rift\" archive" );
129-
return;
130-
}
90+
unsigned short files_in_archive_count;
91+
FileRead( archive_file_, &files_in_archive_count, sizeof(files_in_archive_count) );
13192

132-
unsigned short files_in_archive_count;
133-
FileRead( archive_file_, &files_in_archive_count, sizeof(files_in_archive_count) );
93+
std::vector<FileInfoPacked> files_info_packed( files_in_archive_count );
94+
FileRead( archive_file_, files_info_packed.data(), files_info_packed.size() * sizeof(FileInfoPacked ) );
13495

135-
std::vector<FileInfoPacked> files_info_packed( files_in_archive_count );
136-
FileRead( archive_file_, files_info_packed.data(), files_info_packed.size() * sizeof(FileInfoPacked ) );
96+
virtual_files_.reserve( files_in_archive_count );
13797

138-
virtual_files_.reserve( files_in_archive_count );
98+
for( const FileInfoPacked& file_info_packed : files_info_packed )
99+
{
100+
VirtualFile file;
101+
std::memcpy( &file.size , &file_info_packed.size , sizeof(unsigned int) );
102+
std::memcpy( &file.offset, &file_info_packed.offset, sizeof(unsigned int) );
139103

140-
for( const FileInfoPacked& file_info_packed : files_info_packed )
104+
virtual_files_[ VirtualFileName( file_info_packed.name, file_info_packed.name_length ) ]= file;
105+
}
106+
} else if( addon_path_.empty() )
141107
{
142-
VirtualFile file;
143-
std::memcpy( &file.size , &file_info_packed.size , sizeof(unsigned int) );
144-
std::memcpy( &file.offset, &file_info_packed.offset, sizeof(unsigned int) );
145-
146-
virtual_files_[ VirtualFileName( file_info_packed.name, file_info_packed.name_length ) ]= file;
108+
Log::FatalError( "Could not open file \"", archive_file_name, "\"" );
147109
}
110+
111+
return;
148112
}
149113

150114
Vfs::~Vfs()

0 commit comments

Comments
 (0)