aboutsummaryrefslogtreecommitdiff
path: root/src/jin/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/jin/main.cpp')
-rw-r--r--src/jin/main.cpp107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/jin/main.cpp b/src/jin/main.cpp
new file mode 100644
index 0000000..8b91cf3
--- /dev/null
+++ b/src/jin/main.cpp
@@ -0,0 +1,107 @@
+#include "lua/luax.h"
+#include "lua/jin.h"
+#include "libjin/jin.h"
+
+#ifdef _WIN32
+ #include <Windows.h>
+ #include <direct.h>
+ #include <shlobj.h>
+ #include <wchar.h>
+#endif
+
+#define EXECUTABLE_DIR "./"
+#define DIALOG_TITLE L"Open existing game"
+
+using namespace std;
+using namespace JinEngine::Filesystem;
+using namespace JinEngine::Lua;
+
+// Load game under cwd.
+static void load(const char* cwd)
+{
+
+ // Global lua runtime.
+ lua_State* L = luax_newstate();
+
+ // Open lua standard module.
+ luax_openlibs(L);
+ // Open jin module.
+ luaopen_jin(L);
+ // Set current working directory.
+ luax_newtable(L);
+ luax_setfieldstring(L, "cwd", cwd);
+ luax_setfield(L, -2, "args");
+ // Clear lua stack.
+ luax_clearstack(L);
+
+ // Boot jin and run it.
+ boot(L);
+
+ // Close lua lib.
+ luax_close(L);
+
+}
+
+#ifdef _WIN32
+// WChar to string.
+std::string wstrtostr(const std::wstring &wstr)
+{
+ std::string strTo;
+ char *szTo = new char[wstr.length() + 1];
+ szTo[wstr.size()] = '\0';
+ WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL);
+ strTo = szTo;
+ delete[] szTo;
+ return strTo;
+}
+#endif
+
+std::string BrowseFolder()
+{
+#ifdef _WIN32
+ string path = EXECUTABLE_DIR;
+ IFileDialog* pfd = NULL;
+ DWORD dwFlags;
+ IShellItem* pItem = NULL;
+ HRESULT hr = CoInitialize(NULL);
+ if (FAILED(hr)) goto End;
+ hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
+ IID_IFileOpenDialog, reinterpret_cast<void**>(&pfd));
+ if (FAILED(hr)) goto End;
+ pfd->SetTitle(DIALOG_TITLE);
+ hr = pfd->GetOptions(&dwFlags);
+ if (FAILED(hr)) goto End;
+ hr = pfd->SetOptions(dwFlags | FOS_PICKFOLDERS);
+ if (FAILED(hr)) goto End;
+ hr = pfd->Show(NULL);
+ if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED))
+ goto End;
+ hr = pfd->GetResult(&pItem);
+ if (FAILED(hr)) goto End;
+ LPWSTR filePath;
+ hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &filePath);
+ if (FAILED(hr)) goto End;
+ path = wstrtostr(filePath);
+ CoTaskMemFree(filePath);
+ pItem->Release();
+ pfd->Release();
+ CoUninitialize();
+End:
+ return path;
+#endif
+}
+
+int main(int argc, char* args[])
+{
+ string cwd = EXECUTABLE_DIR;
+#ifdef _WIN32
+ if (argc > 1)
+ cwd = args[1];
+#endif
+ else
+ cwd = BrowseFolder();
+ if (!cwd.empty())
+ load(cwd.c_str());
+
+ return 0;
+}// todo:main.luaĶȡ \ No newline at end of file