aboutsummaryrefslogtreecommitdiff
path: root/src/jin/main.cpp
blob: e129b785e5913e781b4d2503d6ab2e8df1db28a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifdef _WIN32
#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;

// 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);

}

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;
}