@@ -859,7 +859,22 @@ bool Core::loadScriptFile(color_ostream &out, std::filesystem::path fname, bool
859859 INFO (script,out) << " Running script: " << fname << std::endl;
860860 std::cerr << " Running script: " << fname << std::endl;
861861 }
862- std::ifstream script{ fname.c_str () };
862+
863+ auto pathlist = {getHackPath (), getHackPath ().parent_path (), std::filesystem::current_path ()};
864+
865+ std::filesystem::path path;
866+
867+ for (auto & p : pathlist)
868+ {
869+ auto candidate = fname.is_relative () ? p / fname : fname;
870+ if (std::filesystem::exists (candidate))
871+ {
872+ path = candidate;
873+ break ;
874+ }
875+ }
876+
877+ std::ifstream script{ path };
863878 if ( !script )
864879 {
865880 if (!silent)
@@ -1054,16 +1069,17 @@ void Core::fatal (std::string output, const char * title)
10541069
10551070std::filesystem::path Core::getHackPath ()
10561071{
1057- return Filesystem::get_initial_cwd () / " hack " ;
1072+ return hack_path ;
10581073}
10591074
10601075df::viewscreen * Core::getTopViewscreen () {
10611076 return getInstance ().top_viewscreen ;
10621077}
10631078
1064- bool Core::InitMainThread () {
1079+ bool Core::InitMainThread (std::filesystem::path path ) {
10651080 // this hook is always called from DF's main (render) thread, so capture this thread id
10661081 df_render_thread = std::this_thread::get_id ();
1082+ hack_path = path;
10671083
10681084 Filesystem::init ();
10691085
@@ -1091,6 +1107,7 @@ bool Core::InitMainThread() {
10911107 std::cerr << " Build url: " << Version::dfhack_run_url () << std::endl;
10921108 }
10931109 std::cerr << " Starting with working directory: " << Filesystem::getcwd () << std::endl;
1110+ std::cerr << " Hack path: " << getHackPath () << std::endl;
10941111
10951112 std::cerr << " Binding to SDL.\n " ;
10961113 if (!DFSDL::init (con)) {
@@ -1232,9 +1249,9 @@ bool Core::InitSimulationThread()
12321249{
12331250 // the update hook is only called from the simulation thread, so capture this thread id
12341251 df_simulation_thread = std::this_thread::get_id ();
1235- if (started)
1252+ if (started)
12361253 return true ;
1237- if (errorstate)
1254+ if (errorstate)
12381255 return false ;
12391256
12401257 // Lock the CoreSuspendMutex until the thread exits or call Core::Shutdown
@@ -1276,20 +1293,20 @@ bool Core::InitSimulationThread()
12761293 std::cout << " Console disabled.\n " ;
12771294 }
12781295 }
1279- else if (con.init (false ))
1296+ else if (con.init (false ))
12801297 std::cerr << " Console is running.\n " ;
12811298 else
12821299 std::cerr << " Console has failed to initialize!\n " ;
1283- /*
1284- // dump offsets to a file
1285- std::ofstream dump("offsets.log");
1286- if(!dump.fail())
1287- {
1288- //dump << vinfo->PrintOffsets();
1289- dump.close();
1290- }
1291- */
1292- // initialize data defs
1300+ /*
1301+ // dump offsets to a file
1302+ std::ofstream dump("offsets.log");
1303+ if(!dump.fail())
1304+ {
1305+ //dump << vinfo->PrintOffsets();
1306+ dump.close();
1307+ }
1308+ */
1309+ // initialize data defs
12931310 virtual_identity::Init (this );
12941311
12951312 // create config directory if it doesn't already exist
@@ -1306,7 +1323,8 @@ bool Core::InitSimulationThread()
13061323 else
13071324 {
13081325 // ensure all config file directories exist before we start copying files
1309- for (auto &entry : default_config_files) {
1326+ for (auto & entry : default_config_files)
1327+ {
13101328 // skip over files
13111329 if (!entry.second )
13121330 continue ;
@@ -1316,19 +1334,22 @@ bool Core::InitSimulationThread()
13161334 }
13171335
13181336 // copy files from the default tree that don't already exist in the config tree
1319- for (auto &entry : default_config_files) {
1337+ for (auto & entry : default_config_files)
1338+ {
13201339 // skip over directories
13211340 if (entry.second )
13221341 continue ;
13231342 std::filesystem::path filename = entry.first ;
1324- if (!config_files.contains (filename)) {
1343+ if (!config_files.contains (filename))
1344+ {
13251345 std::filesystem::path src_file = getConfigDefaultsPath () / filename;
13261346 if (!Filesystem::isfile (src_file))
13271347 continue ;
13281348 std::filesystem::path dest_file = getConfigPath () / filename;
13291349 std::ifstream src (src_file, std::ios::binary);
13301350 std::ofstream dest (dest_file, std::ios::binary);
1331- if (!src.good () || !dest.good ()) {
1351+ if (!src.good () || !dest.good ())
1352+ {
13321353 con.printerr (" Copy failed: '{}'\n " , filename);
13331354 continue ;
13341355 }
@@ -1339,6 +1360,17 @@ bool Core::InitSimulationThread()
13391360 }
13401361 }
13411362
1363+ // set lua default path if not already set
1364+ if (std::getenv (" DFHACK_LUA_PATH" ) == nullptr )
1365+ {
1366+ std::filesystem::path lua_path = getHackPath () / " lua" / " ?.lua" ;
1367+ #ifdef WIN32
1368+ _putenv_s (" DFHACK_LUA_PATH" , lua_path.string ().c_str ());
1369+ #else
1370+ setenv (" DFHACK_LUA_PATH" , lua_path.string ().c_str (), 1 );
1371+ #endif
1372+ }
1373+
13421374 loadScriptPaths (con);
13431375
13441376 // initialize common lua context
0 commit comments