AVSValue LoadPlugin(AVSValue args, void* user_data, IScriptEnvironment* env){
extern const AVS_Linkage* const AVS_linkage; // In interface.cpp
bool quiet = (user_data != 0);
args = args[0];
const char* result=0;
for (int i=0; i<args.ArraySize(); ++i) {
HMODULE plugin;
const char* plugin_name = args[i].AsString();
if (MyLoadLibrary(plugin_name, &plugin, quiet, env)) {
typedef const char* (__stdcall *AvisynthPluginInit3Func)(IScriptEnvironment* env, const AVS_Linkage* const vectors);
AvisynthPluginInit3Func AvisynthPluginInit3 = (AvisynthPluginInit3Func)GetProcAddress(plugin, "AvisynthPluginInit3");
if (!AvisynthPluginInit3) {
AvisynthPluginInit3 = (AvisynthPluginInit3Func)GetProcAddress(plugin, "_AvisynthPluginInit3@8");
if (!AvisynthPluginInit3) { // Try for 2.5 version
typedef const char* (__stdcall *AvisynthPluginInit2Func)(IScriptEnvironment* env);
AvisynthPluginInit2Func AvisynthPluginInit2 = (AvisynthPluginInit2Func)GetProcAddress(plugin, "AvisynthPluginInit2");
if (!AvisynthPluginInit2) {
AvisynthPluginInit2 = (AvisynthPluginInit2Func)GetProcAddress(plugin, "_AvisynthPluginInit2@4");
if (!AvisynthPluginInit2) { // Older version
FreeLibrary(plugin);
if (quiet) {
// remove the last
handle from the list
HMODULE* loaded_plugins = (HMODULE*)env->GetVar("$Plugins$").AsString();
int j=0;
while (loaded_plugins[j+1]) j++;
loaded_plugins[j] = 0;
} else {
env->ThrowError("Plugin %s is not an AviSynth 2.6 or 2.5 plugin.",plugin_name);
}
} else {
result = AvisynthPluginInit2(env);
}
} else {
result = AvisynthPluginInit2(env);
}
} else {
result = AvisynthPluginInit3(env, AVS_linkage);
}
} else {
result = AvisynthPluginInit3(env, AVS_linkage);
}
}
}
if (loadplugin_prefix) free((void*)loadplugin_prefix); // Tritical May 2005
loadplugin_prefix = 0;
return result ? AVSValue(env->SaveString(result)) : AVSValue();
}