Skip to content

Commit

Permalink
persis storage ok
Browse files Browse the repository at this point in the history
  • Loading branch information
bha-evs committed Jan 9, 2024
1 parent 01343cd commit 8d886f5
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 9 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ SET(CMAKE_COLOR_MAKEFILE ON)
SET(CMAKE_VERBOSE_MAKEFILE ON)
# set(CMAKE_DEBUG_POSTFIX _d)
if (EMSCRIPTEN)
#FS layout:
#FS layout (Memory)
#Root: /tmp
#Root: /home
#Root: /dev
Expand Down Expand Up @@ -116,6 +116,8 @@ if (EMSCRIPTEN)
# file: '/TstRoot/SubDir_0/Level1/' Sz 4096 Ft 2
# file: '/TstRoot/SubDir_0/' Sz 4096 Ft 2
#
#FS Layout (persisten): stored at C:\Users\bha\AppData\Local\Google\Chrome\User Data\Default\IndexedDB
#
#sometime it cannot find pthread. If i force it in C:\Program Files\CMake\share\cmake-3.28\Modules\FindThreads.cmake it works...
#macro(_threads_check_libc)
# set(CMAKE_THREAD_LIBS_INIT "")
Expand Down
166 changes: 166 additions & 0 deletions apps/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,184 @@
#include <bofstd/boffs.h>
#include <filesystem>
#include <stdio.h>

#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#endif

#if defined(__EMSCRIPTEN__)
void game_loop_callback(void)
{
int i, Len_i;
FILE *pIo_X;
char pTxt_c[0x100], pFn_c[] = "/offline/any_file.bha";

if (!EM_ASM_INT({ return Module.fs_is_ready; }))
{
printf("wait\n");
return;
}

pIo_X = fopen(pFn_c, "rb");
if (pIo_X)
{
for (i = 0; i < 10; i++)
{
if (fgets(pTxt_c, sizeof(pTxt_c), pIo_X) == nullptr)
{
break;
}
else
{
printf("[%d] '%s'\n", i, pTxt_c);
}
}
fclose(pIo_X);
}
else
{
pIo_X = fopen(pFn_c, "wb");
if (pIo_X)
{
for (i = 0; i < 10; i++)
{
Len_i = sprintf(pTxt_c, "This is line %d\n", i);
if (fwrite(pTxt_c, Len_i, 1, pIo_X) != 1)
{
break;
}
}
fclose(pIo_X);
}
}
// Don't forget to sync to make sure you store it to IndexedDB
EM_ASM(
FS.syncfs(
false, function(err) {if (err) {
console.error("Error syncing file system:", err);
return 1; // Indicate failure
} else {
console.log("File system synced successfully.");
return 0; // Indicate success
} }););

if (0) // game_is_running())
{
// game_run_frame();
}
else
{
emscripten_cancel_main_loop();
}
}

BOFERR EmscriptenCallback(void *_pArg)
{
BOFERR Rts_E = BOF_ERR_NO_ERROR;
int i, Len_i;
FILE *pIo_X;
char pTxt_c[0x100], pFn_c[] = "/offline/any_file.bha";

const std::filesystem::directory_iterator end;
for (std::filesystem::directory_iterator it("/"); it != end; ++it)
{
const std::filesystem::path &entry = it->path();
printf("root: %s\n", entry.c_str());
}

pIo_X = fopen(pFn_c, "rb");
if (pIo_X)
{
for (i = 0; i < 10; i++)
{
if (fgets(pTxt_c, sizeof(pTxt_c), pIo_X) == nullptr)
{
break;
}
else
{
printf("[%d] '%s'\n", i, pTxt_c);
}
}
fclose(pIo_X);
}
else
{
pIo_X = fopen(pFn_c, "wb");
if (pIo_X)
{
for (i = 0; i < 10; i++)
{
Len_i = sprintf(pTxt_c, "This is line %d\n", i);
if (fwrite(pTxt_c, Len_i, 1, pIo_X) != 1)
{
break;
}
}
fclose(pIo_X);
}
}
for (std::filesystem::directory_iterator it("/offline"); it != end; ++it)
{
const std::filesystem::path &entry = it->path();
printf("offline: %s\n", entry.c_str());
}
Rts_E = BOF_ERR_FINISHED;
return Rts_E;
}
#endif

int main(int argc, char *argv[])
{
BOF::BOFSTDPARAM StdParam_X;
std::string Cwd_S;

StdParam_X.AssertInRelease_B = true;
StdParam_X.AssertCallback = nullptr;
#if defined(__EMSCRIPTEN__)
StdParam_X.EmscriptenCallback = EmscriptenCallback;
StdParam_X.EmscriptenCallback = EmscriptenCallback;
StdParam_X.EmscriptenCallbackFps_U32 = 0;
StdParam_X.pEmscriptenCallbackArg = (void *)0x12345678;
StdParam_X.pPersistentRootDir_c = "/offline";
StdParam_X.ExitOnBofShutdown_B = true;
#endif
if (Bof_Initialize(StdParam_X) == BOF_ERR_NO_ERROR)
{
BOF::Bof_GetCurrentDirectory(Cwd_S);
printf("\nPwd %s\nRunning BofStd V %s on %s under %s\n", Cwd_S.c_str(), StdParam_X.Version_S.c_str(), StdParam_X.ComputerName_S.c_str(), StdParam_X.OsName_S.c_str());

#if 0
#if defined(__EMSCRIPTEN__)
// EM_ASM is a macro to call in-line JavaScript code.
EM_ASM(
Module.fs_is_ready = 0; // flag to check when data are synchronized
FS.mkdir('/offline'); // Make a directory other than '/'
FS.mount(IDBFS, {}, '/offline'); // Then mount with IDBFS type

FS.syncfs( // Then sync
true, function(err) {
Module.print("end file sync..");
assert(!err);
Module.fs_is_ready = 1;
}););
/*
0 lets the browser decide how often to call your callback, like v-sync. You can pass a positive integer to set a specific frame rate.
true stops execution at this point, your next code that runs will be the loop callback.
*/
emscripten_set_main_loop(game_loop_callback, 0, true);

#endif
#endif
const std::filesystem::directory_iterator end;
for (std::filesystem::directory_iterator it("/"); it != end; ++it)
{
const std::filesystem::path &entry = it->path();
printf("root: %s\n", entry.c_str());
}

BOF::Bof_Shutdown();
}
// emscripten_exit_with_live_runtime();
return 0;
}
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ if (EMSCRIPTEN)
target_link_libraries(bofstd
PRIVATE
"-lwebsocket.js"
"-lidbfs.js"
)
endif()
# Defines
Expand Down
24 changes: 19 additions & 5 deletions lib/include/bofstd/bofstd.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ cmake -DCMAKE_TOOLCHAIN_FILE=C:/pro/vcpkg/scripts/buildsystems/vcpkg.cmake -DBUI
#include <string.h>
#include <string>

using BofAssertCallback =
std::function<BOFERR(const std::string &_rFile_S, uint32_t _Line_U32, const std::string &_rMasg_S)>;

using BofAssertCallback = std::function<BOFERR(const std::string &_rFile_S, uint32_t _Line_U32, const std::string &_rMasg_S)>;
#if defined(__EMSCRIPTEN__)
using BofEmscriptenCallback = std::function<BOFERR(void *)>;
#endif
#include <bofstd_export.h> //Autogenerated by cmake

#define BOF onbings::bof
Expand All @@ -56,7 +57,13 @@ struct BOFSTDPARAM
// Input Param
bool AssertInRelease_B;
BofAssertCallback AssertCallback;

#if defined(__EMSCRIPTEN__)
BofEmscriptenCallback EmscriptenCallback;
void *pEmscriptenCallbackArg;
uint32_t EmscriptenCallbackFps_U32;
const char *pPersistentRootDir_c;
bool ExitOnBofShutdown_B;
#endif
// Output param
std::string OsName_S;
std::string ComputerName_S;
Expand All @@ -71,6 +78,13 @@ struct BOFSTDPARAM
{
AssertInRelease_B = false;
AssertCallback = nullptr;
#if defined(__EMSCRIPTEN__)
EmscriptenCallback = nullptr;
EmscriptenCallbackFps_U32 = 0;
pEmscriptenCallbackArg = nullptr;
pPersistentRootDir_c = nullptr;
ExitOnBofShutdown_B = false;
#endif
OsName_S = "";
ComputerName_S = "";
Version_S = "";
Expand Down Expand Up @@ -224,7 +238,7 @@ struct BOF_RECT

bool IsInside(const BOF_RECT<T> &_rBox) const
{
return ((_rBox.Left <= Left) && (_rBox.Right >= Right) && (_rBox.Top <= Top) && (_rBox.Bottom >= Bottom));
return ((_rBox.Left <= Left) && (_rBox.Right >= Right) && (_rBox.Top <= Top) && (_rBox.Bottom >= Bottom));
}

T Width() const
Expand Down
71 changes: 69 additions & 2 deletions lib/src/bofstd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ cmake -DCMAKE_TOOLCHAIN_FILE=/home/bha/pro/github/vcpkg/scripts/buildsystems/vcp
#include <bofversioninfo.h>
#include <locale.h>
#include <map>

#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#endif
// Just to check: see std::string Bof_GetVersion()
// #include <libavutil/avutil.h>
// #include <openssl/crypto.h>
Expand Down Expand Up @@ -356,6 +358,36 @@ std::string Bof_GetVersion()
return std::to_string(BOFSTD_VERSION_MAJOR) + "." + std::to_string(BOFSTD_VERSION_MINOR) + "." + std::to_string(BOFSTD_VERSION_PATCH) + "." + std::to_string(BOFSTD_VERSION_BUILD);
}

// Emscripten only accept this kind of callback
#if defined(__EMSCRIPTEN__)
static void S_BofEmscriptenCallback(void *_pArg)
{
BOFERR Sts_E;

// if (!EM_ASM_INT({ return Module.fs_is_ready; }))
// if ((GL_BofStdParam_X.pPersistentRootDir_c) && (!EM_ASM_INT({ return Module.fs_is_ready; })))
if ((GL_BofStdParam_X.pPersistentRootDir_c) &&
(!EM_ASM_INT({ return Module.fs_is_ready; })))
{
printf("wait\n");
}
else
{
if (GL_BofStdParam_X.EmscriptenCallback)
{
Sts_E = GL_BofStdParam_X.EmscriptenCallback(_pArg); // GL_BofStdParam_X.pEmscriptenCallbackArg);
// Don't forget to sync with false to make sure you store it to IndexedDB
EM_ASM(FS.syncfs(
false, function(err) {if (err) {} }););
if (Sts_E != BOF_ERR_NO_ERROR)
{
emscripten_cancel_main_loop();
}
}
}
}
#endif

BOFERR Bof_Initialize(BOFSTDPARAM &_rStdParam_X)
{
BOFERR Rts_E;
Expand Down Expand Up @@ -449,6 +481,35 @@ BOFERR Bof_Initialize(BOFSTDPARAM &_rStdParam_X)
}
*/
#endif

#if defined(__EMSCRIPTEN__)
/*
0 lets the browser decide how often to call your callback, like v-sync. You can pass a positive integer to set a specific frame rate.
true stops execution at this point, your next code that runs will be the loop callback.
*/
if (GL_BofStdParam_X.EmscriptenCallback)
{
// EM_ASM is a macro to call in-line JavaScript code.
if (GL_BofStdParam_X.pPersistentRootDir_c)
{
EM_ASM({
Module.fs_is_ready = 0; // flag to check when data are synchronized
FS.mkdir(UTF8ToString($0)); // Make a directory other than '/'
FS.mount(IDBFS, {}, UTF8ToString($0)); // Then mount with IDBFS type

FS.syncfs( // Then sync with true
true, function(err) {
Module.print("End persistent file sync..");
assert(!err);
Module.fs_is_ready = 1;
});
},
GL_BofStdParam_X.pPersistentRootDir_c);
}
emscripten_set_main_loop_arg(S_BofEmscriptenCallback, GL_BofStdParam_X.pEmscriptenCallbackArg, GL_BofStdParam_X.EmscriptenCallbackFps_U32, false);
}
#endif

_rStdParam_X.Version_S = Bof_GetVersion();
return Rts_E;
}
Expand Down Expand Up @@ -509,9 +570,15 @@ BOFERR Bof_Shutdown()
tcsetattr(STDIN_FILENO, TCSANOW, &S_SavedTermIos_X);
*/
#endif

// Give some time to thread/logger to shutdown
BOF::Bof_MsSleep(1000);

#if defined(__EMSCRIPTEN__)
if (GL_BofStdParam_X.ExitOnBofShutdown_B)
{
emscripten_exit_with_live_runtime();
}
#endif
return Rts_E;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ int main(int argc, char *argv[])
//::testing::GTEST_FLAG(filter) = "RawCircularBuffer_Test.FillWrapOverwrite";
//::testing::GTEST_FLAG(filter) = "RawCircularBuffer_Test.*:CircularBuffer_Test.*:RawCircularBufferInSlotMode_Test.*";
//::testing::GTEST_FLAG(filter) = "BofThreadPool_Test.*:BofThread_Test.*";
::testing::GTEST_FLAG(filter) = "Fs_Test.FileLayout";
//::testing::GTEST_FLAG(filter) = "Fs_Test.FileLayout";
//::testing::GTEST_FLAG(filter) = "RawCircularBufferAlwaysContiguous_Test.*:RawCircularBuffer_Test.*:RawCircularBufferInSlotMode_Test.*";
// std::string CrtDir_S;
// BOF::Bof_GetCurrentDirectory(CrtDir_S);
Expand Down

0 comments on commit 8d886f5

Please sign in to comment.