@@ -32,6 +32,9 @@ FILE_SERVICE::FILE_SERVICE()
3232 Max_File_Index = 0 ;
3333 for (uint32_t n = 0 ; n < _MAX_OPEN_INI_FILES; n++)
3434 OpenFiles[n] = nullptr ;
35+ if (ResourcePathsFirstScan) {
36+ ScanResourcePaths ();
37+ }
3538}
3639
3740FILE_SERVICE::~FILE_SERVICE ()
@@ -41,7 +44,7 @@ FILE_SERVICE::~FILE_SERVICE()
4144
4245std::fstream FILE_SERVICE::_CreateFile (const char *filename, std::ios::openmode mode)
4346{
44- const auto path = filename ? std::filesystem::u8path (convert_path_sep (filename)) : std::filesystem::path ();
47+ const auto path = filename ? std::filesystem::u8path (ConvertPathResource (filename)) : std::filesystem::path ();
4548 std::fstream fileS (path, mode);
4649 return fileS;
4750}
@@ -58,7 +61,7 @@ void FILE_SERVICE::_SetFilePointer(std::fstream &fileS, std::streamoff off, std:
5861
5962bool FILE_SERVICE::_DeleteFile (const char *filename)
6063{
61- std::filesystem::path path = std::filesystem::u8path (convert_path_sep (filename));
64+ std::filesystem::path path = std::filesystem::u8path (ConvertPathResource (filename));
6265 return std::filesystem::remove (path);
6366}
6467
@@ -94,7 +97,7 @@ bool FILE_SERVICE::_ReadFile(std::fstream &fileS, void *s, std::streamsize count
9497
9598bool FILE_SERVICE::_FileOrDirectoryExists (const char *p)
9699{
97- std::filesystem::path path = std::filesystem::u8path (convert_path_sep (p));
100+ std::filesystem::path path = std::filesystem::u8path (ConvertPathResource (p));
98101 auto ec = std::error_code{};
99102 bool result = std::filesystem::exists (path, ec);
100103 if (ec)
@@ -133,7 +136,7 @@ std::vector<std::filesystem::path> FILE_SERVICE::_GetFsPathsByMask(const char *s
133136 }
134137 else
135138 {
136- srcPath = std::filesystem::u8path (convert_path_sep (sourcePath));
139+ srcPath = std::filesystem::u8path (ConvertPathResource (sourcePath));
137140 }
138141
139142 std::filesystem::path curPath;
@@ -179,7 +182,7 @@ std::time_t FILE_SERVICE::_ToTimeT(std::filesystem::file_time_type tp)
179182
180183std::filesystem::file_time_type FILE_SERVICE::_GetLastWriteTime (const char *filename)
181184{
182- std::filesystem::path path = std::filesystem::u8path (convert_path_sep (filename));
185+ std::filesystem::path path = std::filesystem::u8path (ConvertPathResource (filename));
183186 return std::filesystem::last_write_time (path);
184187}
185188
@@ -203,25 +206,25 @@ std::string FILE_SERVICE::_GetExecutableDirectory()
203206
204207std::uintmax_t FILE_SERVICE::_GetFileSize (const char *filename)
205208{
206- std::filesystem::path path = std::filesystem::u8path (convert_path_sep (filename));
209+ std::filesystem::path path = std::filesystem::u8path (ConvertPathResource (filename));
207210 return std::filesystem::file_size (path);
208211}
209212
210213void FILE_SERVICE::_SetCurrentDirectory (const char *pathName)
211214{
212- std::filesystem::path path = std::filesystem::u8path (convert_path_sep (pathName));
215+ std::filesystem::path path = std::filesystem::u8path (ConvertPathResource (pathName));
213216 std::filesystem::current_path (path);
214217}
215218
216219bool FILE_SERVICE::_CreateDirectory (const char *pathName)
217220{
218- std::filesystem::path path = std::filesystem::u8path (convert_path_sep (pathName));
221+ std::filesystem::path path = std::filesystem::u8path (ConvertPathResource (pathName));
219222 return std::filesystem::create_directories (path);
220223}
221224
222225std::uintmax_t FILE_SERVICE::_RemoveDirectory (const char *pathName)
223226{
224- std::filesystem::path path = std::filesystem::u8path (convert_path_sep (pathName));
227+ std::filesystem::path path = std::filesystem::u8path (ConvertPathResource (pathName));
225228 return std::filesystem::remove_all (path);
226229}
227230
@@ -358,6 +361,108 @@ bool FILE_SERVICE::LoadFile(const char *file_name, char **ppBuffer, uint32_t *dw
358361 return true ;
359362}
360363
364+ // ------------------------------------------------------------------------------------------------
365+ // Resource paths
366+ //
367+
368+ static bool startsWith (const std::string& str, const std::string& prefix)
369+ {
370+ return str.size () >= prefix.size () && 0 == str.compare (0 , prefix.size (), prefix);
371+ }
372+
373+ static bool endsWith (const std::string& str, const std::string& suffix)
374+ {
375+ return str.size () >= suffix.size () && 0 == str.compare (str.size ()-suffix.size (), suffix.size (), suffix);
376+ }
377+
378+ void terminate_with_char (std::string& buffer, const char chr) {
379+ // Check if already has
380+ if (!buffer.empty () && buffer[buffer.length () - 1 ] != chr) {
381+ // Append to end and store
382+ buffer += chr;
383+ }
384+ }
385+
386+ std::string get_dir_iterator_path (const std::filesystem::path& path)
387+ {
388+ std::string path_str = path.string ();
389+ size_t pos = path_str.find (std::string (" ." ) + PATH_SEP);
390+ if (pos != std::string::npos && pos == 0 )
391+ {
392+ path_str.erase (0 , 2 );
393+ }
394+ return path_str;
395+ }
396+
397+ void string_replace (std::string& input, const char * find, const char * paste)
398+ {
399+ size_t pos = 0 ;
400+ while (true ) {
401+ pos = input.find (find, pos);
402+ if (pos >= input.size ())
403+ break ;
404+ input.replace (pos, strlen (find), paste);
405+ pos += strlen (paste);
406+ }
407+ }
408+
409+ std::string convert_path (const char * path)
410+ {
411+ std::string conv;
412+ size_t size = strlen (path);
413+ for (int i = 0 ; i < size; ++i)
414+ {
415+ conv.push_back (path[i] == WRONG_PATH_SEP ? PATH_SEP : path[i]);
416+ }
417+ return conv;
418+ }
419+
420+ void FILE_SERVICE::ScanResourcePaths ()
421+ {
422+ if (ResourcePathsFirstScan)
423+ {
424+ // Seems like if static code calls us we need to do this manually to avoid any bugs
425+ ResourcePaths = std::unordered_map<std::string, std::string>();
426+ }
427+ ResourcePaths.clear ();
428+ for (const auto & entry : std::filesystem::recursive_directory_iterator (" ." )) {
429+ if (entry.is_regular_file () || entry.is_directory ()) {
430+ std::string path = get_dir_iterator_path (entry.path ());
431+ std::string path_lwr = convert_path (path.c_str ());
432+ tolwr (path_lwr.data ());
433+ if (startsWith (path_lwr, " program" )
434+ || startsWith (path_lwr, " resource" )
435+ || endsWith (path_lwr, " .ini" )) {
436+ ResourcePaths[path_lwr] = path;
437+ if (entry.is_directory ()) {
438+ terminate_with_char (path_lwr, PATH_SEP);
439+ ResourcePaths[path_lwr] = path;
440+ }
441+ }
442+ }
443+ }
444+ ResourcePathsFirstScan = false ;
445+ }
446+
447+ std::string FILE_SERVICE::ConvertPathResource (const char * path)
448+ {
449+ if (ResourcePathsFirstScan)
450+ {
451+ ScanResourcePaths ();
452+ }
453+ std::string conv = convert_path (path);
454+ tolwr (conv.data ());
455+ if (startsWith (conv, " ./" ))
456+ {
457+ string_replace (conv, " ./" , " " );
458+ }
459+ std::string result = ResourcePaths[conv];
460+ if (result.empty ())
461+ return conv;
462+ else
463+ return result;
464+ }
465+
361466// =================================================================================================
362467
363468INIFILE_T::~INIFILE_T ()
0 commit comments