This repository was archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplatform_main.cpp
More file actions
185 lines (137 loc) · 3.79 KB
/
Copy pathplatform_main.cpp
File metadata and controls
185 lines (137 loc) · 3.79 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
180
181
182
183
184
185
// platform layer setup + game code loading/reloading
#ifdef _MSC_VER
#pragma warning(disable : 4007)
#endif
#include "../basic.h"
#include "platform_strings.h"
#include "platform_api.h"
// here we treat game_state* as void* so this doesn't have to know anything about the game
typedef void* (*startup_type)(platform_api*);
typedef bool (*main_loop_type)(void*);
typedef void (*shut_down_type)(void*);
typedef void (*on_reload_type)(platform_api*, void*);
typedef main_loop_type on_unload_type;
startup_type start_up = null;
main_loop_type main_loop = null;
shut_down_type shut_down = null;
on_reload_type on_reload = null;
on_unload_type on_unload = null;
platform_dll game_dll;
platform_file_attributes attrib;
platform_api api;
void* game_state;
string exe_folder;
string dll_path;
string temp_dll_path;
bool load_lib();
bool load_funcs();
bool try_reload();
i32 main(i32 argc, char** argv) {
api = platform_build_api();
api.your_dll = &game_dll;
string exe_path;
platform_error err = api.get_bin_path(&exe_path);
if(!err.good) {
return 1;
}
i32 idx = string_last_slash(exe_path);
exe_folder = make_substring(exe_path, 0, idx + 1, api.heap_alloc);
#ifdef _WIN32
dll_path = make_cat_string(exe_folder, string_literal("exile.dll"), api.heap_alloc);
temp_dll_path = make_cat_string(exe_folder, string_literal("exile_temp.dll"), api.heap_alloc);
#elif defined(__linux__)
dll_path = make_cat_string(exe_folder, string_literal("libexile.so"), api.heap_alloc);
temp_dll_path = make_cat_string(exe_folder, string_literal("libexile_temp.so"), api.heap_alloc);
#endif
free_string(exe_path, api.heap_free);
if(!load_lib()) {
return 1;
}
err = api.get_file_attributes(&attrib, dll_path);
if(!err.good) {
return 1;
}
if(!load_funcs()) {
return 1;
}
game_state = (*start_up)(&api);
if(game_state == null) {
return 1;
}
while((*main_loop)(game_state)) {
if(!try_reload()) {
return 1;
}
}
(*shut_down)(game_state);
api.free_library(&game_dll);
platform_shutdown();
free_string(exe_folder, api.heap_free);
free_string(dll_path, api.heap_free);
free_string(temp_dll_path, api.heap_free);
#ifdef CHECK_NO_LEAKS
if(global_num_allocs) {
api.debug_break();
} else {
api.write_stdout_str(string_literal("Zero net allocations achieved\n"));
}
#endif
return 0;
}
bool load_lib() {
// we just spinlock here waiting for the file to unlock
platform_error err;
u32 itr = 0;
do {
itr++;
err = api.copy_file(dll_path, temp_dll_path, true);
} while(err.error == PLT_SHARING_ERROR && itr < 100000);
err = api.load_library(&game_dll, temp_dll_path);
if(!err.good) {
return false;
}
err = api.get_file_attributes(&attrib, dll_path);
if(!err.good) {
return false;
}
return true;
}
bool try_reload() {
platform_file_attributes to_test;
platform_error err = api.get_file_attributes(&to_test, dll_path);
if(!err.good) {
return false;
}
if(api.test_file_written(&attrib, &to_test)) {
attrib = to_test;
(*on_unload)(game_state);
api.free_library(&game_dll);
if(!load_lib()) return false;
if(!load_funcs()) return false;
(*on_reload)(&api, game_state);
}
return true;
}
bool load_funcs() {
platform_error err = api.get_proc_address((void**)&start_up, &game_dll, string_literal("start_up"));
if(!err.good) {
return false;
}
err = api.get_proc_address((void**)&main_loop, &game_dll, string_literal("main_loop"));
if(!err.good) {
return false;
}
err = api.get_proc_address((void**)&shut_down, &game_dll, string_literal("shut_down"));
if(!err.good) {
return false;
}
err = api.get_proc_address((void**)&on_reload, &game_dll, string_literal("on_reload"));
if(!err.good) {
return false;
}
err = api.get_proc_address((void**)&on_unload, &game_dll, string_literal("on_unload"));
if(!err.good) {
return false;
}
return true;
}