-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.cpp
More file actions
179 lines (148 loc) · 3.43 KB
/
Copy pathstate.cpp
File metadata and controls
179 lines (148 loc) · 3.43 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "stdafx.h"
#include "core.h"
#include "msg.h"
#include "state.h"
using namespace std;
using namespace std::filesystem;
CState* kStatePtr;
LPWSTR format_state_error(const wstring& s) noexcept
{
const rsize_t word_count = s.size() + 1;
wchar_t* p = new wchar_t[word_count];
wcscpy_s(p, word_count, s.c_str());
return p;
}
inline void DebugPrint(const std::wstring& s) noexcept
{
OutputDebugString(s.c_str());
}
inline void ShowError(HWND dlg, const wstring& wmsg) noexcept
{
LPWSTR msg = format_state_error(wmsg);
// ReSharper disable once CppAssignedValueIsNeverUsed
BOOL rc = PostMessage(dlg, UWM_UPDATEINFO, NULL, reinterpret_cast<LPARAM>(msg));
assert(rc);
}
static path get_module_file_path() noexcept
{
const unique_ptr<WCHAR[]> buf = make_unique<WCHAR[]>(MAX_PATH);
const DWORD size = GetModuleFileName(nullptr, buf.get(), MAX_PATH);
assert(size > 0);
path p{wstring{buf.get(), size}};
return p;
}
static path get_module_directory() noexcept
{
const path p = get_module_file_path();
const path dir = p.parent_path();
return dir;
}
static path get_lua_file_path() noexcept
{
path p = get_module_file_path();
p.replace_extension(L".lua");
return p;
}
static path canonicalize(const path& p) noexcept
{
if (p.is_relative())
{
const path root = get_module_directory();
path rv = absolute(root / p);
return rv;
}
return p;
}
void CState::Initialize() noexcept
{
assert(!L);
L = luaL_newstate();
luaL_openlibs(L);
core_load_libs(L);
}
bool CState::RunScript() const noexcept
{
const path script_path = get_lua_file_path();
auto script_file = script_path.u8string();
const int error = luaL_loadfile(L, (const char*)script_file.c_str()) || lua_pcall(L, 0, 0, 0);
if (error != 0)
{
wstringstream ss;
ss << L"Error running script: " << lua_tostring(L, -1) << endl;
auto wmsg = ss.str();
lua_pop(L, 1);
DebugPrint(wmsg);
ShowError(kDlg, wmsg);
return false;
}
return true;
}
void CState::Reset() noexcept
{
app_hide_ = true;
app_args_.clear();
app_path_.clear();
work_dir_.clear();
on_icon_path_.clear();
off_icon_path_.clear();
env_map_.clear();
}
CState::CState() noexcept
{
}
CState::~CState() noexcept
{
if (L)
{
lua_close(L);
L = nullptr;
}
}
const path& CState::GetAppPath() const noexcept
{
return app_path_;
}
void CState::SetAppPath(const std::filesystem::path& val) noexcept
{
app_path_ = canonicalize(val);
}
const path& CState::GetOnIconPath() const noexcept
{
return on_icon_path_;
}
void CState::SetOnIconPath(const std::filesystem::path& val) noexcept
{
on_icon_path_ = canonicalize(val);
}
const path& CState::GetOffIconPath() const noexcept
{
return off_icon_path_;
}
void CState::SetOffIconPath(const std::filesystem::path& val) noexcept
{
off_icon_path_ = canonicalize(val);
}
const path& CState::GetAppWorkDir() const noexcept
{
return work_dir_;
}
void CState::SetAppWorkDir(const std::filesystem::path& val) noexcept
{
work_dir_ = canonicalize(val);
}
bool CState::GetAppHide() const noexcept
{
return app_hide_;
}
void CState::SetAppHide(bool val) noexcept
{
app_hide_ = val;
}
const wstring& CState::GetAppArgs() const noexcept
{
return app_args_;
}
void CState::SetAppArgs(const std::wstring& val) noexcept
{
app_args_ = val;
}