Skip to content

Commit

Permalink
Enable include useplugins.h and scanplugins.h in a cpp file.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhcad committed Feb 20, 2012
1 parent 15cc3ca commit 150ec52
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
6 changes: 5 additions & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ LDFLAGS += -nologo $(VCLIBS)
ifdef APPTYPE # application
ifdef IS_CONSOLE
LDFLAGS += -subsystem:console
endif
else
ifdef IS_AFXDLL # MFC app
LDFLAGS += -entry:"wWinMainCRTStartup"
endif
endif # IS_CONSOLE
else # not app
ifndef IS_LIB # dll
LDFLAGS += -subsystem:windows -dll
endif
Expand Down
28 changes: 15 additions & 13 deletions interface/core/nonplugin/scanplugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@
#endif

#ifndef PLUGINS_MAXCOUNT
#define PLUGINS_MAXCOUNT 20
#define PLUGINS_MAXCOUNT 40
#endif

namespace x3 {

static HMODULE s_modules[PLUGINS_MAXCOUNT] = { NULL };
static int s_nplugin = 0;
static int s_nmods = 0;

static bool loadfilter(const char* filename, const char* ext)
{
if (_stricmp(ext, ".pln") == 0
&& GetModuleHandleA(PathFindFileNameA(filename)) == NULL)
{
s_modules[s_nplugin] = x3LoadLibrary(filename);
if (s_modules[s_nplugin])
s_nplugin++;
s_modules[s_nmods] = x3LoadLibrary(filename);
if (s_modules[s_nmods])
s_nmods++;
}
return s_nplugin < PLUGINS_MAXCOUNT;
return s_nmods < PLUGINS_MAXCOUNT;
}

int loadPlugins(const char* folder = "plugins")
int loadScanPlugins(const char* folder = "plugins")
{
char path[MAX_PATH];

Expand All @@ -47,18 +47,20 @@ int loadPlugins(const char* folder = "plugins")
}

x3::scanfiles(loadfilter, path, true);
return s_nplugin;
return s_nmods;
}

void unloadPlugins()
void unloadScanPlugins()
{
while (s_nplugin > 0)
while (s_nmods > 0)
{
x3FreeLibrary(s_modules[--s_nplugin]);
x3FreeLibrary(s_modules[--s_nmods]);
}
}

#ifndef X3_EXCLUDE_CREATEOBJECT
#if !defined(X3_EXCLUDE_CREATEOBJECT) && !defined(CREATEOBJECTIMPL)
#define CREATEOBJECTIMPL

class IObject;
bool createObject(const char* clsid, long iid, IObject** p)
{
Expand All @@ -67,7 +69,7 @@ bool createObject(const char* clsid, long iid, IObject** p)
return f && f(clsid, iid, p);
}
HMODULE getManagerModule() { return s_modules[0]; }
#endif
#endif // CREATEOBJECTIMPL

} // x3
#endif
24 changes: 17 additions & 7 deletions interface/core/nonplugin/useplugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace x3 {

static LoadModuleHelper* s_plugins[10] = { NULL };
static LoadModuleHelper* s_plugins[20] = { NULL };
static int s_nplugin = 0;

#if !defined(X3_EXCLUDE_CREATEOBJECT) && !defined(CREATEOBJECTIMPL)
Expand All @@ -43,14 +43,24 @@ bool createObject(const char* clsid, long iid, IObject** p)
}
#endif // CREATEOBJECTIMPL

void loadPlugins(const char* const* plugins, const char* folder = PLUGIN_PATH)
int loadPlugins(const char* const* plugins, const char* folder = PLUGIN_PATH)
{
HMODULE basemod = GetModuleHandleA(SELF_MODULE_NAME);
for (int i = 0; s_nplugin < 10 - 1 && plugins[i]; ++i, ++s_nplugin)

for (int i = 0; s_nplugin < 20 - 1 && plugins[i]; ++i)
{
s_plugins[s_nplugin] = new LoadModuleHelper();
s_plugins[s_nplugin]->load(plugins[i], basemod, folder);
LoadModuleHelper* p = new LoadModuleHelper();
if (p->load(plugins[i], basemod, folder))
{
s_plugins[s_nplugin++] = p;
}
else
{
delete p;
}
}

return s_nplugin;
}

void unloadPlugins()
Expand All @@ -64,8 +74,8 @@ void unloadPlugins()
}

/** \code
* static const char* plugins[] = { "x3manager.pln", "myplugin.pln", NULL };
* static x3::AutoLoadPlugins autoload(plugins);
* const char* plugins[] = { "x3manager.pln", "myplugin.pln", NULL };
* x3::AutoLoadPlugins autoload(plugins, "plugins");
* \endcode
*/
struct AutoLoadPlugins
Expand Down
4 changes: 2 additions & 2 deletions source/example/consoledemo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ int main()

int main()
{
x3::loadPlugins();
x3::loadScanPlugins();

int ret = test();

x3::unloadPlugins();
x3::unloadScanPlugins();

return ret;
}
Expand Down

0 comments on commit 150ec52

Please sign in to comment.