Skip to content

Commit

Permalink
Disable default file handler and Add hook so external projects can ho…
Browse files Browse the repository at this point in the history
…ok their own internal file handling objects
  • Loading branch information
allengar22 authored and allengar22 committed Jun 29, 2021
1 parent 2ab7f96 commit e1cff96
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# USE_NATIVE_IMGUI_INCLUDE_PATHS is set to true by default include direct path to imgui headers
# User can set it to false to create a custom way of including imgui to their project
option(USE_NATIVE_IMGUI_INCLUDE_PATHS "Set this to false if you want to define your own target_include_directories function for imgui." true)

add_library(imgui STATIC)

target_sources(imgui
PRIVATE
imgui.h
imconfig.h
imgui_demo.cpp
imgui_draw.cpp
imgui_internal.h
imgui_widgets.cpp
imgui.cpp
imgui_tables.cpp
)

if(USE_NATIVE_IMGUI_INCLUDE_PATHS)
target_include_directories(imgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
endif()
2 changes: 1 addition & 1 deletion imconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
//#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf)
//#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself.
//#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies)
//#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function.
#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function.
//#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions().
//#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available

Expand Down
44 changes: 43 additions & 1 deletion imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,49 @@ bool ImFileClose(ImFileHandle f) { return fclose(f) == 0; }
ImU64 ImFileGetSize(ImFileHandle f) { long off = 0, sz = 0; return ((off = ftell(f)) != -1 && !fseek(f, 0, SEEK_END) && (sz = ftell(f)) != -1 && !fseek(f, off, SEEK_SET)) ? (ImU64)sz : (ImU64)-1; }
ImU64 ImFileRead(void* data, ImU64 sz, ImU64 count, ImFileHandle f) { return fread(data, (size_t)sz, (size_t)count, f); }
ImU64 ImFileWrite(const void* data, ImU64 sz, ImU64 count, ImFileHandle f) { return fwrite(data, (size_t)sz, (size_t)count, f); }
#endif // #ifndef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS
#else
//Functions to redirect to our hook object function calls if it exists
ImFileHandle ImFileOpen(const char* filename, const char* mode) {
if (auto context = ImGui::GetCurrentContext()) {
if (ImIFileHelper* fileHelperPtr = context->FileHelper) {
return fileHelperPtr->ImFileOpen(filename, mode);
}
}
return nullptr;
}
bool ImFileClose(ImFileHandle file) {
if (auto context = ImGui::GetCurrentContext()) {
if (ImIFileHelper* fileHelperPtr = context->FileHelper) {
return fileHelperPtr->ImFileClose(file);
}
}
return false;
}
ImU64 ImFileGetSize(ImFileHandle file) {
if (auto context = ImGui::GetCurrentContext()) {
if (ImIFileHelper* fileHelperPtr = context->FileHelper) {
return fileHelperPtr->ImFileGetSize(file);
}
}
return 0;
}
ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file) {
if (auto context = ImGui::GetCurrentContext()) {
if (ImIFileHelper* fileHelperPtr = context->FileHelper) {
return fileHelperPtr->ImFileRead(data, size, count, file);
}
}
return 0;
}
ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file) {
if (auto context = ImGui::GetCurrentContext()) {
if (ImIFileHelper* fileHelperPtr = context->FileHelper) {
return fileHelperPtr->ImFileWrite(data, size, count, file);
}
}
return 0;
}
#endif

// Helper: Load file content into memory
// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()
Expand Down
22 changes: 21 additions & 1 deletion imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,25 @@ IMGUI_API bool ImFileClose(ImFileHandle file);
IMGUI_API ImU64 ImFileGetSize(ImFileHandle file);
IMGUI_API ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file);
IMGUI_API ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file);
#else
#else //Bedrock File Handler Hook so we can use our own internal file handling
#define IMGUI_DISABLE_TTY_FUNCTIONS // Can't use stdout, fflush if we are not using default file functions
//We'll cast this to our own internal type as needed
typedef void* ImFileHandle;
//Helper struct for hooking file operations to external systems
struct ImIFileHelper {
virtual ~ImIFileHelper() = default;
virtual ImFileHandle ImFileOpen(const char* filename, const char* mode) = 0;
virtual bool ImFileClose(ImFileHandle file) = 0;
virtual ImU64 ImFileGetSize(ImFileHandle file) = 0;
virtual ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file) = 0;
virtual ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file) = 0;
};

IMGUI_API ImFileHandle ImFileOpen(const char* filename, const char* mode);
IMGUI_API bool ImFileClose(ImFileHandle file);
IMGUI_API ImU64 ImFileGetSize(ImFileHandle file);
IMGUI_API ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file);
IMGUI_API ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file);
#endif
IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size = NULL, int padding_bytes = 0);

Expand Down Expand Up @@ -1598,6 +1615,9 @@ struct ImGuiContext
ImChunkStream<ImGuiTableSettings> SettingsTables; // ImGuiTable .ini settings entries
ImVector<ImGuiContextHook> Hooks; // Hooks for extensions (e.g. test engine)
ImGuiID HookIdNext; // Next available HookId
#ifdef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS
ImIFileHelper* FileHelper = nullptr; // Hook object for external source to implement their own fileIO
#endif

// Capture/Logging
bool LogEnabled; // Currently capturing
Expand Down

0 comments on commit e1cff96

Please sign in to comment.