-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
142 lines (122 loc) · 3.78 KB
/
Copy pathcore.cpp
File metadata and controls
142 lines (122 loc) · 3.78 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "stdafx.h"
#include "core.h"
#include "env.h"
#include "state.h"
using namespace std;
using namespace std::filesystem;
const char MetaTableName[] = "CoreMeta";
const char ModuleName[] = "core";
inline static CState* get_state(lua_State* L) noexcept
{
void** meta = static_cast<void**>(luaL_checkudata(L, 1, MetaTableName));
CState* self = static_cast<CState*>(*meta);
return self;
}
static int l_core_get_env(lua_State* L) noexcept
{
CState* state = get_state(L);
const char* n = luaL_checkstring(L, 2);
wstring wname = ansi_to_wstring(std::string{n});
const auto it = state->env_map_.find(wname);
if (it != state->env_map_.end())
{
const std::wstring& wval = it->second;
std::string val = wstring_to_ansi(wval);
lua_pushlstring(L, val.data(), val.size());
}
else
{
lua_pushnil(L); // not found
}
return 1;
}
static int l_core_set_env(lua_State* L) noexcept
{
CState* state = get_state(L);
const char* n = luaL_checkstring(L, 2); // first argument
const char* v = luaL_checkstring(L, 3); // second argument
wstring wname = ansi_to_wstring(std::string{n});
wstring wval = ansi_to_wstring(std::string{v});
state->env_map_[wname] = wval;
return 0;
}
static int l_core_output(lua_State* L) noexcept
{
const char* n = luaL_checkstring(L, 2);
OutputDebugStringA(n);
return 0;
}
static int l_core_set_icon_on(lua_State* L) noexcept
{
CState* state = get_state(L);
const char* s = luaL_checkstring(L, 2);
state->SetOnIconPath(s);
return 0;
}
static int l_core_set_icon_off(lua_State* L) noexcept
{
CState* state = get_state(L);
const char* s = luaL_checkstring(L, 2);
state->SetOffIconPath(s);
return 0;
}
static int l_core_set_app_path(lua_State* L) noexcept
{
CState* state = get_state(L);
const char* s = luaL_checkstring(L, 2);
state->SetAppPath(s);
return 0;
}
static int l_core_set_app_args(lua_State* L) noexcept
{
CState* state = get_state(L);
const char* s = luaL_checkstring(L, 2);
state->SetAppArgs(ansi_to_wstring(std::string{s}));
return 0;
}
static int l_core_set_work_dir(lua_State* L) noexcept
{
CState* state = get_state(L);
const char* s = luaL_checkstring(L, 2);
state->SetAppWorkDir(s);
return 0;
}
static int l_core_set_app_hide(lua_State* L) noexcept
{
CState* state = get_state(L);
luaL_checktype(L, 2, LUA_TBOOLEAN);
int val = lua_toboolean(L, 2);
state->SetAppHide(val != 0);
return 0;
}
static int lua_open_core(lua_State* L) noexcept
{
// 创建 metatable
luaL_newmetatable(L, MetaTableName);
static const luaL_Reg methods[] = {{"getenv", l_core_get_env},
{"setenv", l_core_set_env},
{"seticonon", l_core_set_icon_on},
{"seticonoff", l_core_set_icon_off},
{"setapppath", l_core_set_app_path},
{"setappargs", l_core_set_app_args},
{"setappworkdir", l_core_set_work_dir},
{"setapphide", l_core_set_app_hide},
{"output", l_core_output},
{NULL, NULL}};
luaL_setfuncs(L, methods, 0);
// mt.__index = mt
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
// 创建 userdata(单例)
void** ud = (void**)lua_newuserdatauv(L, sizeof(void*), 1);
*ud = kStatePtr;
// 绑定 metatable
luaL_getmetatable(L, MetaTableName);
lua_setmetatable(L, -2);
// 返回 userdata 作为 module
return 1;
}
void core_load_libs(lua_State* L) noexcept
{
luaL_requiref(L, ModuleName, lua_open_core, 1);
}