Nelarius / imnodes

A small, dependency-free node editor for dear imgui
MIT License
2.04k stars 249 forks source link

emscripten + Saving editor state #177

Open pavanakumar opened 1 year ago

pavanakumar commented 1 year ago

Imnodes saves the editor state to the ini file in lines 3232-3246 of file imnodes.cpp,

void SaveEditorStateToIniFile(const ImNodesEditorContext* const editor, const char* const file_name)
{
    size_t      data_size = 0u;
    const char* data = SaveEditorStateToIniString(editor, &data_size);
    FILE*       file = ImFileOpen(file_name, "wt");
    if (!file)
    {
        return;
    }

    fwrite(data, sizeof(char), data_size, file);
    fclose(file);
}

Just by protecting this using the __EMSCRIPTEN__ macro helps,

void SaveEditorStateToIniFile(const ImNodesEditorContext* const editor, const char* const file_name)
{
#ifndef __EMSCRIPTEN__
    size_t      data_size = 0u;
    const char* data = SaveEditorStateToIniString(editor, &data_size);
    FILE*       file = ImFileOpen(file_name, "wt");
    if (!file)
    {
        return;
    }

    fwrite(data, sizeof(char), data_size, file);
    fclose(file);
#endif
}

The browser does not allow access to the filesystem unless you are running this via Node. Just wanted to put this issue here if someone faced similar issue and wants a work around.