finish up plugin architecture
[csRadar.git] / csrPlatform.h
diff --git a/csrPlatform.h b/csrPlatform.h
new file mode 100644 (file)
index 0000000..46f6513
--- /dev/null
@@ -0,0 +1,66 @@
+// Copyright (C) 2021 Harry Godden (hgn)
+
+// Cross platform (windows/unix) dynamic linking wrapper
+//=======================================================================================================================
+
+#ifdef CSR_EXECUTABLE
+
+#if defined(_WIN32) || defined(__CYGWIN__)
+       #define CSR_WINDOWS
+       #include <libloaderapi.h>
+   typedef HWMODULE csr_so;
+#else
+   #define CSR_UNIX
+   #include <dlfcn.h>
+       typedef void *csr_so;
+#endif
+
+csr_so csr_libopen( const char *name )
+{
+       char path[ 512 ];
+       strcpy( path, "ext/" );
+       strcat( path, name );
+       
+       #ifdef CSR_UNIX
+               strcat( path, ".so" );
+       #else
+               strcat( path, ".dll" );
+       #endif
+       
+       log_info( "Loading dynamic library (%s)\n", path );
+
+       #ifdef CSR_UNIX
+               return dlopen( path, RTLD_NOW );
+       #else
+               return LoadLibrary( path );
+       #endif
+}
+
+void *csr_get_proc( csr_so so, const char *name )
+{
+       #ifdef CSR_UNIX
+               return dlsym( so, name );
+       #else
+               return (void *)GetProcAddress( so, name );
+       #endif
+}
+
+void csr_libclose( csr_so so )
+{
+       #ifdef CSR_UNIX
+               dlclose( so );
+       #else
+               FreeLibrary( so );
+       #endif
+}
+
+void csr_liberr(void)
+{
+       #ifdef CSR_UNIX
+       log_error( "Could not load that DLL (%s)\n", dlerror() );
+       #else
+       log_error( "Could not load that DLL (Windows unkown)\n" );
+       #endif
+}
+
+#endif