3030#include " df/world.h"
3131#include " df/world_raws.h"
3232
33- #include " EnumToString.h"
34-
3533using namespace DFHack ;
3634using namespace df ::enums;
3735
@@ -210,9 +208,8 @@ bool ContentLoader::Load()
210208 contentLoader->obsidian = lookupMaterialIndex (INORGANIC, " OBSIDIAN" );
211209
212210 loadGraphicsFromDisk (); // these get destroyed when flushImgFiles is called.
213- ALLEGRO_PATH * p = al_create_path (" stonesense/index.txt" );
214- bool overallResult = parseContentIndexFile ( al_path_cstr (p, ALLEGRO_NATIVE_PATH_SEP) );
215- al_destroy_path (p);
211+ std::filesystem::path p{ " stonesense" };
212+ bool overallResult = parseContentIndexFile ( p / " index.txt" );
216213 translationComplete = false ;
217214
218215 return overallResult;
@@ -235,33 +232,15 @@ bool ContentLoader::reload_configs()
235232 flushImgFiles ();
236233
237234 loadGraphicsFromDisk (); // these get destroyed when flushImgFiles is called.
238- ALLEGRO_PATH * p = al_create_path (" stonesense/index.txt" );
239- bool overallResult = parseContentIndexFile ( al_path_cstr (p, ALLEGRO_NATIVE_PATH_SEP) );
240- al_destroy_path (p);
235+ std::filesystem::path p = std::filesystem::path{} / " stonesense" / " index.txt" ;
236+ bool overallResult = parseContentIndexFile ( p );
241237
242238 return overallResult;
243239}
244240
245- // takes a filename and the file referring to it, and makes a combined filename in
246- // HTML style: (ie "/something" is relative to the stonesense root, everything
247- // else is relative to the referrer)
248- // buffer must be FILENAME_BUFFERSIZE chars
249- // returns true if it all works
250-
251- std::filesystem::path getLocalFilename (const char * filename, std::filesystem::path relativeto)
241+ std::filesystem::path getLocalFilename (std::filesystem::path filename, std::filesystem::path relativeto)
252242{
253- // TODO verify that this does not leak
254- ALLEGRO_PATH* temppath;
255- if (filename[0 ] == ' /' || filename[0 ] == ' \\ ' ) {
256- temppath = al_create_path (filename);
257- al_make_path_canonical (temppath);
258- }
259- else {
260- temppath = al_create_path (relativeto.string ().c_str ());
261- al_join_paths (temppath, al_create_path (filename));
262- al_make_path_canonical (temppath);
263- }
264- return std::string{ al_path_cstr (temppath, ALLEGRO_NATIVE_PATH_SEP) };
243+ return relativeto.remove_filename () / filename;
265244}
266245
267246bool ContentLoader::parseContentIndexFile ( std::filesystem::path filepath )
@@ -313,16 +292,14 @@ bool ContentLoader::parseContentIndexFile( std::filesystem::path filepath )
313292 continue ;
314293 }
315294
316- std::filesystem::path configfilepath = getLocalFilename (line.c_str (), filepath);
317- ALLEGRO_PATH * temppath = al_create_path (configfilepath.string ().c_str ());
318- const char * extension;
319- extension = al_get_path_extension (temppath);
320- if (strcmp (extension," .xml" ) == 0 ) {
295+ std::filesystem::path configfilepath = filepath.remove_filename () / std::filesystem::path{ line }.make_preferred ();
296+ auto extension = configfilepath.extension ();
297+ if (extension == " .xml" ) {
321298 LogVerbose (" Reading xml %s...\n " , configfilepath.string ().c_str ());
322299 if (!parseContentXMLFile (configfilepath)) {
323300 LogError (" Failure in reading %s\n " ,configfilepath.string ().c_str ());
324301 }
325- } else if (strcmp ( extension, " .txt " ) == 0 ) {
302+ } else if (extension == " .txt " ) {
326303 LogVerbose (" Reading index %s...\n " , configfilepath.string ().c_str ());
327304 if (!parseContentIndexFile (configfilepath)) {
328305 LogError (" Failure in reading %s\n " ,configfilepath.string ().c_str ());
@@ -468,13 +445,19 @@ const char* getDocument(TiXmlNode* element)
468445 return parent->Value ();
469446}
470447
471- void contentError (const char * message, TiXmlNode* element)
448+ void contentError (const string& message, TiXmlNode* element)
472449{
473- LogError (" %s: %s: %s (Line %d)\n " ,getDocument (element),message,element->Value (),element->Row ());
450+ auto safeStr = [](const char * s)->const char * {return s ? s : " (unknown)" ; };
451+
452+ LogError (" %s: %s: %s (Line %d)\n " ,
453+ safeStr (getDocument (element)),
454+ message.c_str (),
455+ element ? safeStr (element->Value ()) : " (no element)" ,
456+ element ? element->Row () : -1 );
474457}
475- void contentWarning (const char * message, TiXmlNode* element)
458+ void contentWarning (const string& message, TiXmlNode* element)
476459{
477- LogVerbose (" %s: %s: %s (Line %d)\n " ,getDocument (element),message,element->Value (),element->Row ());
460+ LogVerbose (" %s: %s: %s (Line %d)\n " ,getDocument (element),message. c_str () ,element->Value (),element->Row ());
478461}
479462// converts list of characters 0-5 into bits, ignoring garbage
480463// eg "035" or "0 3 5" or "0xx3x5" are all good
@@ -697,11 +680,11 @@ uint8_t lookupMaterialBright(int matType,int matIndex)
697680 return 0 ;
698681}
699682
700- int loadConfigImgFile (const char * filename, TiXmlElement* referrer)
683+ int loadConfigImgFile (std::filesystem::path filename, TiXmlElement* referrer)
701684{
702685 std::filesystem::path documentRef = getDocument (referrer);
703686 std::filesystem::path configfilepath = getLocalFilename (filename, documentRef);
704- return loadImgFile (configfilepath. string (). c_str () );
687+ return loadImgFile (configfilepath);
705688}
706689
707690void ContentLoader::flushCreatureConfig ()
0 commit comments