diff options
Diffstat (limited to 'src/jin/main.cpp')
-rw-r--r-- | src/jin/main.cpp | 78 |
1 files changed, 56 insertions, 22 deletions
diff --git a/src/jin/main.cpp b/src/jin/main.cpp index d4f08a0..e129b78 100644 --- a/src/jin/main.cpp +++ b/src/jin/main.cpp @@ -1,36 +1,27 @@ #ifdef _WIN32 - #include <SDL2/SDL_Main.h> - #include <direct.h> +#include <SDL2/SDL_Main.h> +#include <direct.h> #endif #include "lua/luax.h" #include "lua/jin.h" #include "libjin/jin.h" + +#ifdef _WIN32 +#include <shlobj.h> #include <Windows.h> +#endif + +#define EXECUTABLE_DIR "./" +using namespace std; using namespace JinEngine::Filesystem; using namespace JinEngine::Lua; -static void setParamters(lua_State* L, int argc, char* args[]) +// Load game under cwd. +static void load(const char* cwd) { - // Add args to field. - luax_newtable(L); - for (int i = 0; i < argc; ++i) - luax_setrawstring(L, -2, i + 1, args[i]); - luax_setfield(L, -2, "args"); - // Push current working directory. - // Absolute directory. - Buffer cwd = Buffer(1024); -#ifdef _WIN32 - _getcwd((char*)&cwd, cwd.size()); -#elif defined __unix__ -#elif defined __APPLE__ -#endif - luax_setfieldstring(L, "cwd", (char*)&cwd); -} -int main(int argc, char* args[]) -{ // Global lua runtime. lua_State* L = luax_newstate(); @@ -38,8 +29,10 @@ int main(int argc, char* args[]) luax_openlibs(L); // Open jin module. luaopen_jin(L); - // Set parameters. - setParamters(L, argc, args); + // Set current working directory. + luax_newtable(L); + luax_setfieldstring(L, "cwd", cwd); + luax_setfield(L, -2, "args"); // Clear lua stack. luax_clearstack(L); @@ -49,5 +42,46 @@ int main(int argc, char* args[]) // Close lua lib. luax_close(L); +} + +std::string BrowseFolder() +{ + TCHAR path[MAX_PATH]; + + BROWSEINFO bi = { 0 }; + bi.lpszTitle = ("Browse game folder..."); + bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE; + bi.lpfn = nullptr; + LPITEMIDLIST pidl = SHBrowseForFolder(&bi); + + if (pidl != 0) + { + SHGetPathFromIDList(pidl, path); + + IMalloc * imalloc = 0; + if (SUCCEEDED(SHGetMalloc(&imalloc))) + { + imalloc->Free(pidl); + imalloc->Release(); + } + + return path; + } + + return ""; +} + +int main(int argc, char* args[]) +{ + string cwd = EXECUTABLE_DIR; +#ifdef _WIN32 + if (argc > 1) + cwd = args[1]; + else + cwd = BrowseFolder(); +#endif + if (!cwd.empty()) + load(cwd.c_str()); + return 0; }
\ No newline at end of file |