[API] Add a mechanism for Dear ImGui assertion callbacks (#100)
* Syncing wip to github * Assertion callback works * Restore example that does not crash * Add comment in build script. Merge with upstream. * Fix assert always triggering * Resolve jvm env at runtime to prevent looking up bad stack address * Fixed EXCEPTION_ACCESS_VIOLATION within native code after callback was complete. * Fix linting errors * Change ImGui to Dear ImGui Co-authored-by: SpaiR <klimbetoo@gmail.com> * Move jvm native layer to its own header/source Co-authored-by: SpaiR <klimbetoo@gmail.com>
This commit is contained in:
1 parent
adf9d5fe4e
commit
74203bc333
15 files changed
+286
-24
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package imgui;
|
||||
|
||||
import imgui.assertion.ImAssertCallback;
|
||||
import imgui.flag.ImGuiCond;
|
||||
import imgui.flag.ImGuiDragDropFlags;
|
||||
import imgui.flag.ImGuiInputTextFlags;
|
||||
@@ -76,6 +77,14 @@ public class ImGui {
|
||||
ImFontAtlas.nInit();
|
||||
ImGuiPlatformIO.init();
|
||||
nInitInputTextData();
|
||||
setAssertCallback(new ImAssertCallback() {
|
||||
@Override
|
||||
public void imAssertCallback(String assertion, int line, String file) {
|
||||
System.err.println("Dear ImGui Assertion Failed: " + assertion);
|
||||
System.err.println("Assertion Located At: " + file + ":" + line);
|
||||
Thread.dumpStack();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static String resolveFullLibName() {
|
||||
@@ -131,7 +140,9 @@ public class ImGui {
|
||||
*/
|
||||
|
||||
private static native void nInitJni(); /*
|
||||
Jni::InitJvm(env);
|
||||
Jni::InitCommon(env);
|
||||
Jni::InitAssertion(env);
|
||||
Jni::InitCallbacks(env);
|
||||
Jni::InitBindingStruct(env);
|
||||
*/
|
||||
@@ -190,6 +201,17 @@ public class ImGui {
|
||||
ImGui::SetCurrentContext((ImGuiContext*) ptr);
|
||||
*/
|
||||
|
||||
/**
|
||||
* Set a custom assertion callback for ImGui assertions.
|
||||
* Take note: Any custom assertion callback SHOULD NOT throw any exception.
|
||||
* After any callback the application will be terminated, any attempt to bypass this behavior
|
||||
* will result in a EXCEPTION_ACCESS_VIOLATION from within the native layer.
|
||||
* @param callback The custom ImGui assertion callback
|
||||
*/
|
||||
public static native void setAssertCallback(ImAssertCallback callback); /*
|
||||
Jni::SetAssertionCallback(callback);
|
||||
*/
|
||||
|
||||
// Main
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
|
||||
package imgui.assertion;
|
||||
|
||||
/**
|
||||
* Callback for native IM_ASSERT calls.
|
||||
*/
|
||||
public abstract class ImAssertCallback {
|
||||
/**
|
||||
* Called from native code to bring the callback into java side.
|
||||
* Will force the application to terminate with exit code 1 after forwarding the callback, this is required, otherwise
|
||||
* if execution is returned to the native layer, a EXCEPTION_ACCESS_VIOLATION will be thrown.
|
||||
* If the callback throws an exception, it will be caught and printed along with a warning to prevent execution
|
||||
* from returning to the native layer.
|
||||
*
|
||||
* @param assertion The assertion string
|
||||
* @param line The line number of the assertion in the source file
|
||||
* @param file The source file where the assertion occurred
|
||||
*/
|
||||
public void imAssert(final String assertion, final int line, final String file) {
|
||||
try {
|
||||
imAssertCallback(assertion, line, file);
|
||||
} catch (Exception ex) {
|
||||
System.err.println("WARNING: Exception thrown in Dear ImGui Assertion Callback!");
|
||||
System.err.println("Dear ImGui Assertion Failed: " + assertion);
|
||||
System.err.println("Assertion Located At: " + file + ":" + line);
|
||||
ex.printStackTrace();
|
||||
}
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* The assertion callback from ImGui.
|
||||
* Do not throw an exception within this callback, to prevent
|
||||
* execution from returned to the native layer and cause a EXCEPTION_ACCESS_VIOLATION,
|
||||
* the callback will catch any exceptions and print a warning.
|
||||
* <p>
|
||||
* There is no way to catch the assertion and continue execution.
|
||||
* You may however call <code>System.exit(code)</code> with your own exit code.
|
||||
*
|
||||
* @param assertion The assertion string
|
||||
* @param line The line number of the assertion in the source file
|
||||
* @param file The source file where the assertion occurred
|
||||
*/
|
||||
public abstract void imAssertCallback(String assertion, int line, String file);
|
||||
}
|
||||
Reference in new issue
Block a user