impl os functions to delete generated hot loading files
This commit is contained in:
@@ -370,9 +370,157 @@ fn void w32_try_reloading_library(w32_library_t *lib, app_frame_t frame) {
|
|||||||
end_of_lib_load:;
|
end_of_lib_load:;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////////////////////
|
||||||
|
// file ops
|
||||||
|
fn s8_t os_abs(ma_arena_t *arena, s8_t rel) {
|
||||||
|
const int buffer_size = 2048;
|
||||||
|
ma_temp_t scratch = ma_begin_scratch1(arena);
|
||||||
|
s16_t rel16 = s16_from_s8(scratch.arena, rel);
|
||||||
|
wchar_t *buffer = ma_push_array(scratch.arena, wchar_t, buffer_size);
|
||||||
|
DWORD written = GetFullPathNameW(rel16.str, buffer_size, buffer, 0);
|
||||||
|
assert(written != 0);
|
||||||
|
assert((i64)written < (i64)buffer_size);
|
||||||
|
s8_t result = s8_from_s16(arena, s16(buffer, written));
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn s8_t os_exe(ma_arena_t *arena) {
|
||||||
|
const int buffer_size = 2048;
|
||||||
|
ma_temp_t scratch = ma_begin_scratch1(arena);
|
||||||
|
wchar_t *buffer = ma_push_array(scratch.arena, wchar_t, buffer_size);
|
||||||
|
DWORD wsize = GetModuleFileNameW(0, buffer, buffer_size);
|
||||||
|
assert(wsize != 0);
|
||||||
|
s8_t result = s8_from_s16(arena, s16(buffer, wsize));
|
||||||
|
s8_normalize_path_unsafe(result);
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn s8_t os_exe_dir(ma_arena_t *arena) {
|
||||||
|
ma_temp_t scratch = ma_begin_scratch1(arena);
|
||||||
|
s8_t exe = os_exe(scratch.arena);
|
||||||
|
s8_t path = s8_chop_last_slash(exe);
|
||||||
|
s8_t result = s8_copy(arena, path);
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn b32 os_delete(s8_t path) {
|
||||||
|
ma_temp_t scratch = ma_begin_scratch();
|
||||||
|
s16_t path16 = s16_from_s8(scratch.arena, path);
|
||||||
|
BOOL success = DeleteFileW(path16.str);
|
||||||
|
b32 result = true;
|
||||||
|
if (success == 0) {
|
||||||
|
result = false; // path not found
|
||||||
|
}
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////
|
||||||
|
// file iter
|
||||||
|
typedef struct w32_file_iter_t w32_file_iter_t;
|
||||||
|
struct w32_file_iter_t {
|
||||||
|
HANDLE handle;
|
||||||
|
WIN32_FIND_DATAW data;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct os_file_iter_t os_file_iter_t;
|
||||||
|
struct os_file_iter_t {
|
||||||
|
s8_t abs;
|
||||||
|
s8_t rel;
|
||||||
|
s8_t name;
|
||||||
|
b8 is_directory;
|
||||||
|
b8 is_valid;
|
||||||
|
|
||||||
|
s8_t path;
|
||||||
|
ma_arena_t *arena;
|
||||||
|
|
||||||
|
union {
|
||||||
|
w32_file_iter_t *w32;
|
||||||
|
void *platform;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
fn void os_advance(os_file_iter_t *it) {
|
||||||
|
while (FindNextFileW(it->w32->handle, &it->w32->data) != 0) {
|
||||||
|
WIN32_FIND_DATAW *data = &it->w32->data;
|
||||||
|
|
||||||
|
// Skip '.' and '..'
|
||||||
|
if (data->cFileName[0] == '.' && data->cFileName[1] == '.' && data->cFileName[2] == 0) continue;
|
||||||
|
if (data->cFileName[0] == '.' && data->cFileName[1] == 0) continue;
|
||||||
|
|
||||||
|
it->is_directory = data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
||||||
|
it->name = s8_from_s16(it->arena, s16(data->cFileName, wstr_len(data->cFileName)));
|
||||||
|
|
||||||
|
const char *is_dir = it->is_directory ? "/" : "";
|
||||||
|
const char *separator = it->path.str[it->path.len - 1] == '/' ? "" : "/";
|
||||||
|
|
||||||
|
it->rel = s8_printf(it->arena, "%S%s%S%s", it->path, separator, it->name, is_dir);
|
||||||
|
it->abs = os_abs(it->arena, it->rel);
|
||||||
|
it->is_valid = true;
|
||||||
|
|
||||||
|
if (it->is_directory) {
|
||||||
|
assert(it->rel.str[it->rel.len - 1] == '/');
|
||||||
|
assert(it->abs.str[it->abs.len - 1] == '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
it->is_valid = false;
|
||||||
|
DWORD error = GetLastError();
|
||||||
|
assert(error == ERROR_NO_MORE_FILES);
|
||||||
|
FindClose(it->w32->handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn os_file_iter_t *os_iterate_files(ma_arena_t *arena, s8_t path) {
|
||||||
|
os_file_iter_t *it = ma_push_type(arena, os_file_iter_t);
|
||||||
|
it->w32 = ma_push_type(arena, w32_file_iter_t);
|
||||||
|
it->arena = arena;
|
||||||
|
it->path = path;
|
||||||
|
it->is_valid = true;
|
||||||
|
|
||||||
|
ma_temp_t scratch = ma_begin_scratch1(arena);
|
||||||
|
s8_t mod_path = s8_printf(scratch.arena, "%S\\*", path);
|
||||||
|
s16_t mod_path16 = s16_from_s8(scratch.arena, mod_path);
|
||||||
|
|
||||||
|
it->w32->handle = FindFirstFileW(mod_path16.str, &it->w32->data);
|
||||||
|
if (it->w32->handle == INVALID_HANDLE_VALUE) {
|
||||||
|
it->is_valid = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (it->is_valid) {
|
||||||
|
assert(it->w32->data.cFileName[0] == '.' && it->w32->data.cFileName[1] == 0);
|
||||||
|
os_advance(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
|
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
|
||||||
core_init();
|
core_init();
|
||||||
|
|
||||||
|
// Delete temporary dll files
|
||||||
|
{
|
||||||
|
ma_temp_t scratch = ma_begin_scratch();
|
||||||
|
s8_t dir = os_exe_dir(scratch.arena);
|
||||||
|
for (os_file_iter_t *it = os_iterate_files(scratch.arena, dir); it->is_valid; os_advance(it)) {
|
||||||
|
b32 is_dll = s8_ends_with(it->name, s8_lit(".dll"), false);
|
||||||
|
b32 is_pdb = s8_ends_with(it->name, s8_lit(".pdb"), false);
|
||||||
|
b32 is_rdi = s8_ends_with(it->name, s8_lit(".rdi"), false);
|
||||||
|
b32 is_temp = s8_find(it->name, s8_lit("_temp_"), s8_seek_none) != -1;
|
||||||
|
if (is_temp && (is_dll || is_pdb || is_rdi)) {
|
||||||
|
b32 ok = os_delete(it->abs);
|
||||||
|
assert(ok);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ma_end_scratch(scratch);
|
||||||
|
}
|
||||||
|
|
||||||
// Set DPI aware, @todo: verify / new way to do this | @todo: get dpi ratio
|
// Set DPI aware, @todo: verify / new way to do this | @todo: get dpi ratio
|
||||||
{
|
{
|
||||||
typedef enum W32_PROCESS_DPI_AWARENESS {
|
typedef enum W32_PROCESS_DPI_AWARENESS {
|
||||||
@@ -470,8 +618,15 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
|
|||||||
ShowWindow(w32_window_handle, SW_SHOW);
|
ShowWindow(w32_window_handle, SW_SHOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
w32_library_t lib = {0};
|
||||||
|
lib.dll_name = s8_lit("app.dll");
|
||||||
|
lib.update_fn_name = s8_lit("app_update");
|
||||||
|
tcx->data[tcx_slot_app] = w32_load_opengl_fn;
|
||||||
|
|
||||||
|
// Set VSync
|
||||||
if (wglSwapIntervalEXT) {
|
if (wglSwapIntervalEXT) {
|
||||||
wglSwapIntervalEXT(1); // vsync
|
wglSwapIntervalEXT(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
f64 refresh_rate = 60;
|
f64 refresh_rate = 60;
|
||||||
@@ -480,11 +635,6 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
|
|||||||
refresh_rate = (f64)devmodew.dmDisplayFrequency;
|
refresh_rate = (f64)devmodew.dmDisplayFrequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
w32_library_t lib = {0};
|
|
||||||
lib.dll_name = s8_lit("app.dll");
|
|
||||||
lib.update_fn_name = s8_lit("app_update");
|
|
||||||
tcx->data[tcx_slot_app] = w32_load_opengl_fn;
|
|
||||||
|
|
||||||
f64 time_frame_start = w32_seconds_now();
|
f64 time_frame_start = w32_seconds_now();
|
||||||
f64 time_delta = 1.0 / refresh_rate;
|
f64 time_delta = 1.0 / refresh_rate;
|
||||||
f64 time_update = 0.0;
|
f64 time_update = 0.0;
|
||||||
|
|||||||
@@ -162,6 +162,7 @@ void *sbin_read_data(stream_t *stream, i64 size);
|
|||||||
#define S8_CODE(...) s8_lit(#__VA_ARGS__)
|
#define S8_CODE(...) s8_lit(#__VA_ARGS__)
|
||||||
#define S8_FILE s8_lit(__FILE__)
|
#define S8_FILE s8_lit(__FILE__)
|
||||||
#define S8_FILE_AND_LINE s8_lit(FILE_AND_LINE)
|
#define S8_FILE_AND_LINE s8_lit(FILE_AND_LINE)
|
||||||
|
#define s16(str,len) (s16_t){str, len}
|
||||||
|
|
||||||
#define S8_FMT(ma, str, result) \
|
#define S8_FMT(ma, str, result) \
|
||||||
va_list args1; \
|
va_list args1; \
|
||||||
|
|||||||
Reference in New Issue
Block a user