Author: dfranke
Date: Mon Apr 6 04:05:25 2009
New Revision: 34576
URL: http://svn.gna.org/viewcvs/wesnoth?rev=34576&view=rev
Log:
Refactor the logic for invoking wesnoth helper-programs, e.g. wesnothd
Modified:
trunk/src/filesystem.cpp
trunk/src/filesystem.hpp
trunk/src/game.cpp
trunk/src/game_config.cpp
trunk/src/game_config.hpp
Modified: trunk/src/filesystem.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/filesystem.cpp?rev=34576&r1=34575&r2=34576&view=diff
==============================================================================
--- trunk/src/filesystem.cpp (original)
+++ trunk/src/filesystem.cpp Mon Apr 6 04:05:25 2009
@@ -59,6 +59,7 @@
#include "game_config.hpp"
#include "log.hpp"
#include "loadscreen.hpp"
+#include "game_preferences.hpp"
#define DBG_FS LOG_STREAM(debug, filesystem)
#define LOG_FS LOG_STREAM(info, filesystem)
@@ -1094,6 +1095,30 @@
return result;
}
+std::string get_program_invocation(const std::string& program_name) {
+#ifdef DEBUG
+ #ifdef _WIN32
+ const char *program_suffix = "-debug.exe";
+ #else
+ const char *program_suffix = "-debug";
+ #endif
+#else
+ #ifdef _WIN32
+ const char *program_suffix = ".exe";
+ #else
+ const char *program_suffix = "";
+ #endif
+#endif
+
+ const std::string real_program_name(program_name + program_suffix);
+ if(game_config::wesnoth_program_dir.empty()) return real_program_name;
+#ifdef _WIN32
+ return game_config::wesnoth_program_dir + "\\" + real_program_name;
+#else
+ return game_config::wesnoth_program_dir + "/" + real_program_name;
+#endif
+}
+
static bool is_path_sep(char c)
{
#ifdef _WIN32
Modified: trunk/src/filesystem.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/filesystem.hpp?rev=34576&r1=34575&r2=34576&view=diff
==============================================================================
--- trunk/src/filesystem.hpp (original)
+++ trunk/src/filesystem.hpp Mon Apr 6 04:05:25 2009
@@ -226,6 +226,15 @@
std::string get_wml_location(const std::string &filename,
const std::string ¤t_dir = std::string());
+/**
+ * Returns the appropriate invocation for a Wesnoth-related binary, assuming
+ * that it is located in the same directory as the running wesnoth binary.
+ * This is just a string-transformation based on argv[0], so the returned
+ * program is not guaranteed to actaully exist. '-debug' variants are handled
+ * correctly.
+ */
+std::string get_program_invocation(const std::string &program_name);
+
class scoped_istream {
std::istream *stream;
public:
Modified: trunk/src/game.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game.cpp?rev=34576&r1=34575&r2=34576&view=diff
==============================================================================
--- trunk/src/game.cpp (original)
+++ trunk/src/game.cpp Mon Apr 6 04:05:25 2009
@@ -1235,56 +1235,30 @@
void game_controller::start_wesnothd()
{
- typedef std::vector<std::string> path_store;
- // add all paths to try to list
- path_store paths_to_try;
-
- if (!preferences::get_mp_server_program_name().empty())
-
paths_to_try.push_back(preferences::get_mp_server_program_name());
-
- std::string wesnothd_quess = game_config::wesnothd_name;
-
- paths_to_try.push_back(wesnothd_quess);
-
-
- std::string needle = "wesnothd";
- size_t found = wesnothd_quess.rfind(needle);
- if (found != std::string::npos)
- {
- wesnothd_quess = wesnothd_quess.substr(0, found +
needle.size());
-#ifdef _WIN32
- wesnothd_quess += ".exe";
-#endif
- if (wesnothd_quess != game_config::wesnothd_name)
- {
- paths_to_try.push_back(wesnothd_quess);
- }
- }
+ const std::string wesnothd_program =
+ preferences::get_mp_server_program_name().empty() ?
+ get_program_invocation("wesnothd") :
preferences::get_mp_server_program_name();
std::string config = get_user_data_dir() + "/lan_server.cfg";
- if (!file_exists(config))
- {
+ if (!file_exists(config)) {
// copy file if it isn't created yet
write_file(config,
read_file(get_wml_location("lan_server.cfg")));
}
- for (path_store::iterator iname = paths_to_try.begin();
- iname != paths_to_try.end(); ++iname)
- {
+
#ifndef _WIN32
- std::string command = "\"" + *iname +"\" -c " + config + " -d
-t 2 -T 5 ";
+ std::string command = "\"" + wesnothd_program +"\" -c " + config + " -d
-t 2 -T 5 ";
#else
- // start wesnoth as background job
- std::string command = "cmd /C start \"wesnoth server\" /B \"" +
*iname + "\" -c " + config + " -t 2 -T 5 ";
-#endif
- LOG_GENERAL << "Starting wesnothd: "<< command << "\n";
- if (std::system(command.c_str()) == 0)
- {
- // Give server a moment to start up
- SDL_Delay(50);
- return;
- }
- preferences::set_mp_server_program_name("");
- }
+ // start wesnoth as background job
+ std::string command = "cmd /C start \"wesnoth server\" /B \"" +
wesnothd_program + "\" -c " + config + " -t 2 -T 5 ";
+#endif
+ LOG_GENERAL << "Starting wesnothd: "<< command << "\n";
+ if (std::system(command.c_str()) == 0) {
+ // Give server a moment to start up
+ SDL_Delay(50);
+ return;
+ }
+ preferences::set_mp_server_program_name("");
+
// Couldn't start server so throw error
WRN_GENERAL << "Failed to run server start script\n";
throw game::mp_server_error("Starting MP server failed!");
@@ -1751,6 +1725,9 @@
/** Process commandline-arguments */
static int process_command_args(int argc, char** argv) {
+ const std::string program = argv[0];
+ game_config::wesnoth_program_dir = directory_name(program);
+
//parse arguments that shouldn't require a display device
int arg;
for(arg = 1; arg != argc; ++arg) {
@@ -2195,24 +2172,6 @@
const time_t t = time(NULL);
std::cerr << "Started on " << ctime(&t) << "\n";
- /**
- * @todo We try to guess the name of the server from the name
of the
- * binary started. This is very fragile it breaks in at least
the
- * following cases.
- * - Wesnoth got renamed to something without wesnoth in it.
- * - Wesnoth got a pre/suffix but the server not.
- */
- std::string program = argv[0];
- const std::string wesnoth = "wesnoth";
- const size_t offset = program.rfind(wesnoth);
- if(offset != std::string::npos) {
- program.replace(offset, wesnoth.length(), "wesnothd");
- game_config::wesnothd_name = program;
- } else {
- std::cerr << "Game executable doesn't have the name \""
<< wesnoth
- << "\" or similar; impossible to guess the
server executable name.\n";
- }
-
const std::string exe_dir = get_exe_dir();
if(!exe_dir.empty() && file_exists(exe_dir +
"/data/_main.cfg")) {
std::cerr << "Automatically found a possible data
directory at "
Modified: trunk/src/game_config.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_config.cpp?rev=34576&r1=34575&r2=34576&view=diff
==============================================================================
--- trunk/src/game_config.cpp (original)
+++ trunk/src/game_config.cpp Mon Apr 6 04:05:25 2009
@@ -48,7 +48,7 @@
#else
const std::string revision = VERSION;
#endif
- std::string wesnothd_name;
+ std::string wesnoth_program_dir;
bool debug = false, editor = false, ignore_replay_errors = false,
mp_debug = false, exit_at_end = false, no_delay = false, small_gui = false,
disable_autosave = false;
bool use_dummylocales = false;
@@ -225,10 +225,6 @@
sinf.name = (**server)["name"];
sinf.address = (**server)["address"];
server_list.push_back(sinf);
- }
- if (!v["wesnothd_name"].empty())
- {
- game_config::wesnothd_name = v["wesnothd_name"];
}
}
Modified: trunk/src/game_config.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_config.hpp?rev=34576&r1=34575&r2=34576&view=diff
==============================================================================
--- trunk/src/game_config.hpp (original)
+++ trunk/src/game_config.hpp Mon Apr 6 04:05:25 2009
@@ -37,7 +37,7 @@
extern const std::string version;
extern const std::string revision;
- extern std::string wesnothd_name;
+ extern std::string wesnoth_program_dir;
/** Default percentage gold carried over to the next scenario. */
extern const int gold_carryover_percentage;
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits