2016-02-11 17:17:52 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 09.02.2016
|
|
|
|
*
|
|
|
|
* Shared system functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2017-03-12 23:24:53 +00:00
|
|
|
#if !defined(FCODER_SYSTEM_SHARED_CPP)
|
|
|
|
#define FCODER_SYSTEM_SHARED_CPP
|
|
|
|
|
2016-08-29 13:57:05 +00:00
|
|
|
//
|
2017-03-11 18:35:12 +00:00
|
|
|
// Standard implementation of file system stuff based on the file track layer.
|
2016-08-29 13:57:05 +00:00
|
|
|
//
|
|
|
|
|
2017-06-30 22:54:06 +00:00
|
|
|
internal i32
|
|
|
|
system_get_binary_path_string(String *out){
|
|
|
|
out->size = system_get_4ed_path(out->str, out->memory_size);
|
|
|
|
return(out->size);
|
|
|
|
}
|
|
|
|
|
2016-08-29 13:57:05 +00:00
|
|
|
internal void
|
|
|
|
init_shared_vars(){
|
2017-03-19 18:25:12 +00:00
|
|
|
umem scratch_size = KB(128);
|
2017-03-23 22:45:32 +00:00
|
|
|
void *scratch_memory = system_memory_allocate(scratch_size);
|
|
|
|
shared_vars.scratch = make_part(scratch_memory, (i32)scratch_size);
|
2017-03-19 18:25:12 +00:00
|
|
|
|
2017-11-17 19:22:12 +00:00
|
|
|
umem font_scratch_size = MB(4);
|
|
|
|
void *font_scratch_memory = system_memory_allocate(font_scratch_size);
|
|
|
|
shared_vars.font_scratch = make_part(font_scratch_memory, (i32)font_scratch_size);
|
|
|
|
|
2017-11-21 19:48:04 +00:00
|
|
|
umem pixel_scratch_size = MB(4);
|
|
|
|
void *pixel_scratch_memory = system_memory_allocate(pixel_scratch_size);
|
|
|
|
shared_vars.pixel_scratch = make_part(pixel_scratch_memory, (i32)pixel_scratch_size);
|
|
|
|
|
2017-03-19 18:25:12 +00:00
|
|
|
shared_vars.track_table_size = KB(16);
|
2017-03-23 22:45:32 +00:00
|
|
|
shared_vars.track_table = system_memory_allocate(shared_vars.track_table_size);
|
2016-08-29 13:57:05 +00:00
|
|
|
|
2017-03-19 18:25:12 +00:00
|
|
|
shared_vars.track_node_size = KB(16);
|
2017-03-23 22:45:32 +00:00
|
|
|
void *track_nodes = system_memory_allocate(shared_vars.track_node_size);
|
2016-08-29 13:57:05 +00:00
|
|
|
|
2017-03-19 18:25:12 +00:00
|
|
|
i32 track_result = init_track_system(&shared_vars.track, &shared_vars.scratch, shared_vars.track_table, shared_vars.track_table_size, track_nodes, shared_vars.track_node_size);
|
2016-08-29 13:57:05 +00:00
|
|
|
|
|
|
|
if (track_result != FileTrack_Good){
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal b32
|
|
|
|
handle_track_out_of_memory(i32 val){
|
2017-07-18 21:19:28 +00:00
|
|
|
b32 result = false;
|
2016-08-29 13:57:05 +00:00
|
|
|
|
|
|
|
switch (val){
|
|
|
|
case FileTrack_OutOfTableMemory:
|
|
|
|
{
|
|
|
|
u32 new_table_size = shared_vars.track_table_size*2;
|
2017-03-23 22:45:32 +00:00
|
|
|
void *new_table = system_memory_allocate(new_table_size);
|
2017-03-19 18:25:12 +00:00
|
|
|
move_track_system(&shared_vars.track, &shared_vars.scratch, new_table, new_table_size);
|
2017-03-23 22:45:32 +00:00
|
|
|
system_memory_free(shared_vars.track_table, shared_vars.track_table_size);
|
2016-08-29 13:57:05 +00:00
|
|
|
shared_vars.track_table_size = new_table_size;
|
|
|
|
shared_vars.track_table = new_table;
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case FileTrack_OutOfListenerMemory:
|
|
|
|
{
|
|
|
|
shared_vars.track_node_size *= 2;
|
2017-03-23 22:45:32 +00:00
|
|
|
void *node_expansion = system_memory_allocate(shared_vars.track_node_size);
|
2017-03-19 18:25:12 +00:00
|
|
|
expand_track_system_listeners(&shared_vars.track, &shared_vars.scratch, node_expansion, shared_vars.track_node_size);
|
2016-08-29 13:57:05 +00:00
|
|
|
}break;
|
|
|
|
|
2017-07-18 21:19:28 +00:00
|
|
|
default: result = true; break;
|
2016-08-29 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_Add_Listener_Sig(system_add_listener){
|
2017-03-23 22:45:32 +00:00
|
|
|
b32 result = false;
|
2016-08-29 13:57:05 +00:00
|
|
|
for (;;){
|
2017-03-23 22:45:32 +00:00
|
|
|
i32 track_result = add_listener(&shared_vars.track, &shared_vars.scratch, (u8*)filename);
|
2016-08-29 13:57:05 +00:00
|
|
|
if (handle_track_out_of_memory(track_result)){
|
|
|
|
if (track_result == FileTrack_Good){
|
2017-03-23 22:45:32 +00:00
|
|
|
result = true;
|
2016-08-29 13:57:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_Remove_Listener_Sig(system_remove_listener){
|
2017-03-23 22:45:32 +00:00
|
|
|
b32 result = false;
|
|
|
|
i32 track_result = remove_listener(&shared_vars.track, &shared_vars.scratch, (u8*)filename);
|
2016-08-29 13:57:05 +00:00
|
|
|
if (track_result == FileTrack_Good){
|
2017-03-23 22:45:32 +00:00
|
|
|
result = true;
|
2016-08-29 13:57:05 +00:00
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_Get_File_Change_Sig(system_get_file_change){
|
2017-03-19 18:25:12 +00:00
|
|
|
b32 result = false;
|
2016-08-29 13:57:05 +00:00
|
|
|
|
2017-03-23 19:14:39 +00:00
|
|
|
i32 size = 0;
|
2017-03-23 22:45:32 +00:00
|
|
|
i32 get_result = get_change_event(&shared_vars.track, &shared_vars.scratch, (u8*)buffer, max, &size);
|
2016-08-29 13:57:05 +00:00
|
|
|
|
|
|
|
*required_size = size;
|
2017-03-19 18:25:12 +00:00
|
|
|
*mem_too_small = false;
|
2016-08-29 13:57:05 +00:00
|
|
|
if (get_result == FileTrack_Good){
|
2017-03-19 18:25:12 +00:00
|
|
|
result = true;
|
2016-08-29 13:57:05 +00:00
|
|
|
}
|
|
|
|
else if (get_result == FileTrack_MemoryTooSmall){
|
2017-03-19 18:25:12 +00:00
|
|
|
*mem_too_small = true;
|
|
|
|
result = true;
|
2016-08-29 13:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2016-08-29 02:14:02 +00:00
|
|
|
//
|
|
|
|
// General shared pieces
|
|
|
|
//
|
|
|
|
|
2016-05-28 00:46:22 +00:00
|
|
|
internal File_Data
|
|
|
|
sysshared_load_file(char *filename){
|
2018-11-20 08:18:54 +00:00
|
|
|
File_Data result = {};
|
2016-05-28 00:46:22 +00:00
|
|
|
|
2018-11-20 08:18:54 +00:00
|
|
|
Plat_Handle handle = {};
|
2016-08-29 02:14:02 +00:00
|
|
|
if (system_load_handle(filename, &handle)){
|
|
|
|
u32 size = system_load_size(handle);
|
2016-05-28 00:46:22 +00:00
|
|
|
|
2016-08-29 02:14:02 +00:00
|
|
|
result.got_file = 1;
|
|
|
|
if (size > 0){
|
|
|
|
result.size = size;
|
2017-03-23 22:45:32 +00:00
|
|
|
result.data = (char*)system_memory_allocate(size+1);
|
2016-08-29 02:14:02 +00:00
|
|
|
|
|
|
|
if (!result.data){
|
2017-01-14 03:01:35 +00:00
|
|
|
result = null_file_data;
|
2016-05-28 00:46:22 +00:00
|
|
|
}
|
2016-08-29 02:14:02 +00:00
|
|
|
else{
|
|
|
|
if (!system_load_file(handle, result.data, size)){
|
2017-03-23 22:45:32 +00:00
|
|
|
system_memory_free(result.data, size+1);
|
2017-01-14 03:01:35 +00:00
|
|
|
result = null_file_data;
|
2016-08-29 02:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-28 00:46:22 +00:00
|
|
|
}
|
2016-08-29 02:14:02 +00:00
|
|
|
|
|
|
|
system_load_close(handle);
|
2016-05-28 00:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2016-02-11 17:17:52 +00:00
|
|
|
internal void
|
2016-02-20 18:23:46 +00:00
|
|
|
sysshared_filter_real_files(char **files, i32 *file_count){
|
2017-03-23 22:45:32 +00:00
|
|
|
i32 end = *file_count;
|
|
|
|
i32 i = 0, j = 0;
|
|
|
|
for (; i < end; ++i){
|
|
|
|
if (system_file_can_be_made((u8*)files[i])){
|
2016-02-11 17:17:52 +00:00
|
|
|
files[j] = files[i];
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*file_count = j;
|
|
|
|
}
|
|
|
|
|
2017-11-17 19:22:12 +00:00
|
|
|
// HACK(allen): Get rid of this now!?
|
2016-05-27 17:11:38 +00:00
|
|
|
internal Partition
|
|
|
|
sysshared_scratch_partition(i32 size){
|
2017-03-23 22:45:32 +00:00
|
|
|
void *data = system_memory_allocate((umem)size);
|
2016-05-27 17:11:38 +00:00
|
|
|
Partition part = make_part(data, size);
|
|
|
|
return(part);
|
2016-02-20 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
2017-03-23 19:14:39 +00:00
|
|
|
sysshared_partition_grow(Partition *part, i32 new_size){
|
2017-11-17 19:22:12 +00:00
|
|
|
Assert(part->pos == 0);
|
|
|
|
|
2017-03-23 19:14:39 +00:00
|
|
|
void *data = 0;
|
2016-05-27 17:11:38 +00:00
|
|
|
if (new_size > part->max){
|
2017-03-23 22:45:32 +00:00
|
|
|
data = system_memory_allocate((umem)new_size);
|
2016-05-27 17:11:38 +00:00
|
|
|
memcpy(data, part->base, part->pos);
|
2017-03-23 22:45:32 +00:00
|
|
|
system_memory_free(part->base, part->max);
|
2016-05-27 17:11:38 +00:00
|
|
|
part->base = (char*)data;
|
2017-03-23 22:45:32 +00:00
|
|
|
part->max = new_size;
|
2016-05-27 17:11:38 +00:00
|
|
|
}
|
2016-02-20 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 01:11:48 +00:00
|
|
|
internal void*
|
|
|
|
sysshared_push_block(Partition *part, i32 size){
|
2018-11-20 08:18:54 +00:00
|
|
|
void *result = push_array(part, i8, size);
|
2017-11-17 19:22:12 +00:00
|
|
|
if (result == 0){
|
2017-11-13 22:12:04 +00:00
|
|
|
sysshared_partition_grow(part, size + part->max);
|
2018-11-20 08:18:54 +00:00
|
|
|
result = push_array(part, i8, size);
|
2016-05-28 01:11:48 +00:00
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:11:38 +00:00
|
|
|
internal b32
|
|
|
|
sysshared_to_binary_path(String *out_filename, char *filename){
|
|
|
|
b32 translate_success = 0;
|
|
|
|
i32 max = out_filename->memory_size;
|
2017-07-18 21:19:28 +00:00
|
|
|
i32 size = out_filename->size = system_get_4ed_path(out_filename->str, out_filename->memory_size);
|
2016-05-27 17:11:38 +00:00
|
|
|
if (size > 0 && size < max-1){
|
|
|
|
out_filename->size = size;
|
2016-08-28 04:31:06 +00:00
|
|
|
if (append_sc(out_filename, filename) && terminate_with_null(out_filename)){
|
2016-05-27 17:11:38 +00:00
|
|
|
translate_success = 1;
|
|
|
|
}
|
2016-02-20 18:23:46 +00:00
|
|
|
}
|
2017-03-10 20:44:42 +00:00
|
|
|
return(translate_success);
|
2016-02-20 18:23:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 23:24:53 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-11 17:17:52 +00:00
|
|
|
// BOTTOM
|
|
|
|
|