40
40
41
41
#include < mutex>
42
42
43
+ #ifndef _WIN32
44
+ #include < dlfcn.h>
45
+ #endif
46
+
47
+ #ifdef _WIN32
48
+ #define AVISYNTH_SO " avisynth.dll"
49
+ #else
50
+ #define AVISYNTH_SO " libavisynth.so"
51
+ #endif
52
+
43
53
// Allocate storage for and initialise static members
44
54
namespace {
45
55
int avs_refcount = 0 ;
56
+ #ifdef _WIN32
46
57
HINSTANCE hLib = nullptr ;
58
+ #else
59
+ void * hLib = nullptr ;
60
+ #endif
47
61
IScriptEnvironment *env = nullptr ;
48
62
std::mutex AviSynthMutex;
49
63
}
@@ -54,14 +68,26 @@ typedef IScriptEnvironment* __stdcall FUNC(int);
54
68
55
69
AviSynthWrapper::AviSynthWrapper () {
56
70
if (!avs_refcount++) {
57
- hLib = LoadLibrary (L" avisynth.dll" );
71
+ #ifdef _WIN32
72
+ #define CONCATENATE (x, y ) x ## y
73
+ #define _Lstr (x ) CONCATENATE(L, x)
74
+ hLib = LoadLibraryW (_Lstr (AVISYNTH_SO));
75
+ #undef _Lstr
76
+ #undef CONCATENATE
77
+ #else
78
+ hLib = dlopen (AVISYNTH_SO, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
79
+ #endif
58
80
59
81
if (!hLib)
60
- throw AvisynthError (" Could not load avisynth.dll " );
82
+ throw AvisynthError (" Could not load " AVISYNTH_SO );
61
83
62
- FUNC *CreateScriptEnv = (FUNC*)GetProcAddress (hLib, " CreateScriptEnvironment" );
84
+ #ifdef _WIN32
85
+ FUNC* CreateScriptEnv = (FUNC*)GetProcAddress (hLib, " CreateScriptEnvironment" );
86
+ #else
87
+ FUNC* CreateScriptEnv = (FUNC*)dlsym (hLib, " CreateScriptEnvironment" );
88
+ #endif
63
89
if (!CreateScriptEnv)
64
- throw AvisynthError (" Failed to get address of CreateScriptEnv from avisynth.dll " );
90
+ throw AvisynthError (" Failed to get address of CreateScriptEnv from " AVISYNTH_SO );
65
91
66
92
env = CreateScriptEnv (AVISYNTH_INTERFACE_VERSION);
67
93
@@ -80,8 +106,12 @@ AviSynthWrapper::AviSynthWrapper() {
80
106
AviSynthWrapper::~AviSynthWrapper () {
81
107
if (!--avs_refcount) {
82
108
delete env;
83
- AVS_linkage = nullptr ;
109
+ # ifdef _WIN32
84
110
FreeLibrary (hLib);
111
+ #else
112
+ dlclose (hLib);
113
+ #endif
114
+ AVS_linkage = nullptr ;
85
115
}
86
116
}
87
117
0 commit comments