diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp index 76042089fff..8e70ecc9320 100644 --- a/drivers/unix/file_access_unix.cpp +++ b/drivers/unix/file_access_unix.cpp @@ -63,7 +63,7 @@ Error FileAccessUnix::_open(const String& p_path, int p_mode_flags) { fclose(f); f=NULL; - String path=fix_path(p_path); + path=fix_path(p_path); //printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage()); ERR_FAIL_COND_V(f,ERR_ALREADY_IN_USE); @@ -114,6 +114,9 @@ void FileAccessUnix::close() { return; fclose(f); f = NULL; + if (close_notification_func) { + close_notification_func(path,flags); + } if (save_path!="") { //unlink(save_path.utf8().get_data()); @@ -240,6 +243,7 @@ FileAccess * FileAccessUnix::create_libc() { return memnew( FileAccessUnix ); } +CloseNotificationFunc FileAccessUnix::close_notification_func=NULL; FileAccessUnix::FileAccessUnix() { diff --git a/drivers/unix/file_access_unix.h b/drivers/unix/file_access_unix.h index 5b0f0e7cb7d..6c41a51ec5c 100644 --- a/drivers/unix/file_access_unix.h +++ b/drivers/unix/file_access_unix.h @@ -38,6 +38,10 @@ /** @author Juan Linietsky */ + + +typedef void (*CloseNotificationFunc)(const String& p_file,int p_flags); + class FileAccessUnix : public FileAccess { FILE *f; @@ -45,10 +49,13 @@ class FileAccessUnix : public FileAccess { void check_errors() const; mutable Error last_error; String save_path; + String path; - static FileAccess* create_libc(); + static FileAccess* create_libc(); public: + static CloseNotificationFunc close_notification_func; + virtual Error _open(const String& p_path, int p_mode_flags); ///< open a file virtual void close(); ///< close a file virtual bool is_open() const; ///< true when file is open diff --git a/platform/javascript/SCsub b/platform/javascript/SCsub index ac17f68aa6f..cd96cf4f316 100644 --- a/platform/javascript/SCsub +++ b/platform/javascript/SCsub @@ -17,7 +17,7 @@ javascript_objects=[] for x in javascript_files: javascript_objects.append( env_javascript.Object( x ) ) -env.Append(LINKFLAGS=["-s","EXPORTED_FUNCTIONS=\"['_main','_audio_server_mix_function']\""]) +env.Append(LINKFLAGS=["-s","EXPORTED_FUNCTIONS=\"['_main','_audio_server_mix_function','_main_after_fs_sync']\""]) prog = None diff --git a/platform/javascript/javascript_main.cpp b/platform/javascript/javascript_main.cpp index 9aade8c4459..fb87dc848e4 100644 --- a/platform/javascript/javascript_main.cpp +++ b/platform/javascript/javascript_main.cpp @@ -31,7 +31,8 @@ #include "main/main.h" #include "io/resource_loader.h" #include "os/keyboard.h" - +#include "emscripten.h" +#include OS_JavaScript *os=NULL; @@ -198,15 +199,39 @@ static void _gfx_idle() { glutPostRedisplay(); } +int start_step=0; + static void _godot_draw(void) { - os->main_loop_iterate(); + if (start_step==1) { + start_step=2; + Main::start(); + os->main_loop_begin(); + } + + if (start_step==2) { + os->main_loop_iterate(); + } + glutSwapBuffers(); } -int main(int argc, char *argv[]) { - /* Initialize the window */ + +extern "C" { + +void main_after_fs_sync(int value) { + + start_step=1; + printf("FS SYNCHED!\n"); +} + +} + +int main(int argc, char *argv[]) { + + + /* Initialize the window */ printf("let it go!\n"); glutInit(&argc, argv); os = new OS_JavaScript(_gfx_init,NULL,NULL,NULL,NULL); @@ -220,7 +245,7 @@ int main(int argc, char *argv[]) { #endif ResourceLoader::set_abort_on_missing_resources(false); //ease up compatibility - Main::start(); + glutSpecialUpFunc(_glut_skey_up); glutSpecialFunc(_glut_skey_down); @@ -238,10 +263,32 @@ int main(int argc, char *argv[]) { // glutReshapeFunc(gears_reshape); glutDisplayFunc(_godot_draw); //glutSpecialFunc(gears_special); - os->main_loop_begin(); + + + + //mount persistent filesystem + EM_ASM( + FS.mkdir('/userfs'); + FS.mount(IDBFS, {}, '/userfs'); + + + + // sync from persisted state into memory and then + // run the 'test' function + FS.syncfs(true, function (err) { + assert(!err); + console.log("done syncinc!"); + _after_sync_cb = Module.cwrap('main_after_fs_sync', 'void',['number']); + _after_sync_cb(0); + + }); + + ); glutMainLoop(); + + return 0; } diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index e18d5d949d7..ae97bf990b9 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -37,6 +37,7 @@ #include "main/main.h" #include "core/globals.h" +#include "emscripten.h" int OS_JavaScript::get_video_driver_count() const { @@ -270,6 +271,32 @@ bool OS_JavaScript::main_loop_iterate() { if (!main_loop) return false; + + if (time_to_save_sync>=0) { + int64_t newtime = get_ticks_msec(); + int64_t elapsed = newtime - last_sync_time; + last_sync_time=newtime; + + time_to_save_sync-=elapsed; + + print_line("elapsed "+itos(elapsed)+" tts "+itos(time_to_save_sync)); + + if (time_to_save_sync<0) { + //time to sync, for real + // run 'success' + print_line("DOING SYNCH!"); + EM_ASM( + FS.syncfs(function (err) { + assert(!err); + console.log("Synched!"); + //ccall('success', 'v'); + }); + ); + } + + + } + return Main::iteration(); } @@ -562,14 +589,21 @@ String OS_JavaScript::get_locale() const { String OS_JavaScript::get_data_dir() const { - if (get_data_dir_func) - return get_data_dir_func(); - return "/"; + //if (get_data_dir_func) + // return get_data_dir_func(); + return "/userfs"; //return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir"); }; +void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) { + print_line("close "+p_file+" flags "+itos(p_flags)); + if (p_file.begins_with("/userfs") && p_flags&FileAccess::WRITE) { + static_cast(get_singleton())->last_sync_time=OS::get_singleton()->get_ticks_msec(); + static_cast(get_singleton())->time_to_save_sync=5000; //five seconds since last save + } +} OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func,GetLocaleFunc p_get_locale_func) { @@ -589,6 +623,9 @@ OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, Ope open_uri_func=p_open_uri_func; get_data_dir_func=p_get_data_dir_func; get_locale_func=p_get_locale_func; + FileAccessUnix::close_notification_func=_close_notification_funcs; + + time_to_save_sync=-1; } diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index d52c465c711..55ac7cdae42 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -65,6 +65,9 @@ private: bool use_gl2; + int64_t time_to_save_sync; + int64_t last_sync_time; + Rasterizer *rasterizer; VisualServer *visual_server; AudioServerJavascript *audio_server; @@ -84,6 +87,8 @@ private: GetDataDirFunc get_data_dir_func; GetLocaleFunc get_locale_func; + static void _close_notification_funcs(const String& p_file,int p_flags); + public: // functions used by main to initialize/deintialize the OS @@ -106,7 +111,7 @@ public: typedef int64_t ProcessID; - static OS* get_singleton(); + //static OS* get_singleton(); virtual void vprint(const char* p_format, va_list p_list, bool p_stderr=false); virtual void print(const char *p_format, ... );