From bdfaea2d46eb85ca69e59b9d050807cd95ce0993 Mon Sep 17 00:00:00 2001 From: Koncord Date: Tue, 2 Aug 2016 17:32:10 +0800 Subject: [PATCH] Implement 'setenv' for windows Paths converter Move 'split' from main to Utils --- apps/openmw-mp/Script/Script.cpp | 4 +-- apps/openmw-mp/Utils.cpp | 53 ++++++++++++++++++++++++++++++++ apps/openmw-mp/Utils.hpp | 10 +++++- apps/openmw-mp/main.cpp | 33 +++----------------- 4 files changed, 68 insertions(+), 32 deletions(-) diff --git a/apps/openmw-mp/Script/Script.cpp b/apps/openmw-mp/Script/Script.cpp index a9240d31b..7392e753e 100644 --- a/apps/openmw-mp/Script/Script.cpp +++ b/apps/openmw-mp/Script/Script.cpp @@ -80,7 +80,7 @@ void Script::LoadScripts(char *scripts, const char *base) while (token) { char path[4096]; - snprintf(path, sizeof(path), "%s/%s/%s", base, "scripts", token); + snprintf(path, sizeof(path), Utils::convertPath("%s/%s/%s").c_str(), base, "scripts", token); Script::scripts.emplace_back(new Script(path)); token = strtok(nullptr, ","); } @@ -104,6 +104,6 @@ void Script::UnloadScripts() void Script::LoadScript(const char *script, const char *base) { char path[4096]; - snprintf(path, sizeof(path), "%s/%s/%s", base, "scripts", script); + snprintf(path, sizeof(path), Utils::convertPath("%s/%s/%s").c_str(), base, "scripts", script); Script::scripts.emplace_back(new Script(path)); } \ No newline at end of file diff --git a/apps/openmw-mp/Utils.cpp b/apps/openmw-mp/Utils.cpp index 25bf5bd53..d698de3f7 100644 --- a/apps/openmw-mp/Utils.cpp +++ b/apps/openmw-mp/Utils.cpp @@ -10,6 +10,59 @@ using namespace std; +#ifdef _WIN32 +int setenv(const char *name, const char *value, int overwrite) +{ + std::unique_ptr tmp(new char[strlen(name) + strlen(value) + 1]); + sprintf(tmp.get(), "%s=%s", name, value); + + printf("%s\n",tmp.get()); + + return putenv((const char*)tmp.get()); +} +#endif + + +std::string Utils::convertPath(std::string str) +{ +#if defined(_WIN32) +#define _SEP_ '\\' +#elif defined(__APPLE__) +#define _SEP_ ':' +#endif + +#if defined(_WIN32) || defined(__APPLE__) + for(auto &ch : str) + if(ch == '/') + ch = _SEP_; +#endif //defined(_WIN32) || defined(__APPLE__) + return str; + +#undef _SEP_ +} + +const vector Utils::split(const string &str, int delimiter) +{ + string buffer; + vector result; + + for (auto symb:str) + if (symb != delimiter) + buffer += symb; + else if (!buffer.empty()) + { + result.push_back(move(buffer)); + buffer.clear(); + } + if (!buffer.empty()) + result.push_back(move(buffer)); + + return result; +} + + + +#undef _SEP_ void Utils::timestamp() { time_t ltime; diff --git a/apps/openmw-mp/Utils.hpp b/apps/openmw-mp/Utils.hpp index 59ba04ee2..7364cc6e8 100644 --- a/apps/openmw-mp/Utils.hpp +++ b/apps/openmw-mp/Utils.hpp @@ -7,7 +7,7 @@ #include #include - +#include #if (!defined(DEBUG_PRINTF) && defined(DEBUG)) #define DEBUG_PRINTF printf @@ -15,8 +15,16 @@ #define DEBUG_PRINTF(...) #endif +#ifdef _WIN32 +int setenv(const char *name, const char *value, int overwrite); +#endif + namespace Utils { + + const std::vector split(const std::string &str, int delimiter); + + std::string convertPath(std::string str); template constexpr unsigned int hash(const char(&str)[N], size_t I = N) { diff --git a/apps/openmw-mp/main.cpp b/apps/openmw-mp/main.cpp index 8be5ba8a0..ccf314f2b 100644 --- a/apps/openmw-mp/main.cpp +++ b/apps/openmw-mp/main.cpp @@ -63,44 +63,19 @@ std::string loadSettings (Settings::Manager & settings) return settingspath; } -const vector split(const string &str, int delimiter) -{ - string buffer; - vector result; - - for (auto symb:str) - if (symb != delimiter) - buffer += symb; - else if (!buffer.empty()) - { - result.push_back(move(buffer)); - buffer.clear(); - } - if (!buffer.empty()) - result.push_back(move(buffer)); - - return result; -} - - - int main(int argc, char *argv[]) { Settings::Manager mgr; loadSettings(mgr); - //string plugin_home = "/home/koncord/ClionProjects/tes3mp-server/files"; - int players = mgr.getInt("players", "General"); int port = mgr.getInt("port", "General"); - std::string plugin_home = mgr.getString("home", "Plugins"); - string moddir = plugin_home + "/files"; + string plugin_home = mgr.getString("home", "Plugins"); + string moddir = Utils::convertPath(plugin_home + "/files"); - vector plugins (split(mgr.getString("plugins", "Plugins"), ',')); - - cout << plugins[0] << endl; + vector plugins (Utils::split(mgr.getString("plugins", "Plugins"), ',')); printVersion("0.0.1b", 1); @@ -108,7 +83,7 @@ int main(int argc, char *argv[]) setenv("AMXFILE", moddir.c_str(), 1); setenv("MOD_DIR", moddir.c_str(), 1); // hack for lua - setenv("LUA_PATH", (plugin_home + "/scripts/?.lua" + ";" + plugin_home + "/scripts/?.t").c_str(), 1); + setenv("LUA_PATH", Utils::convertPath(plugin_home + "/scripts/?.lua" + ";" + plugin_home + "/scripts/?.t").c_str(), 1); for(auto plugin : plugins) Script::LoadScript(plugin.c_str(), plugin_home.c_str());