Skip to content

Commit

Permalink
Win32: Allow library.c to be compiled
Browse files Browse the repository at this point in the history
However, it is currently just a stub on Windows. All the
LoadLibrary() etc. calls elsewhere in the engine should be
replaced with Library instances.
  • Loading branch information
skyjake committed Dec 30, 2011
1 parent da282c2 commit 69289ab
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions doomsday/engine/portable/src/library.c
Expand Up @@ -27,26 +27,41 @@
#include "m_misc.h"
#include "m_args.h"

#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <dlfcn.h>
#include <string.h>
#ifdef WIN32
# include "de_platform.h"
#endif

#ifdef UNIX
# include <sys/types.h>
# include <unistd.h>
# include <dirent.h>
# include <dlfcn.h>
# include <string.h>
#endif

#define MAX_LIBRARIES 64 /// @todo Replace with a dynamic list.

#ifdef UNIX
typedef void* handle_t;
#endif
#ifdef WIN32
typedef HINSTANCE handle_t;
#endif

struct library_s {
ddstring_t* path;
handle_t handle;
boolean isGamePlugin;
};

#ifdef UNIX
static filename_t appDir; /// @todo Use ddstring_t
#endif

static ddstring_t* lastError;
static Library* loadedLibs[MAX_LIBRARIES];

#ifdef UNIX
static void getBundlePath(char* path, size_t len)
{
if(ArgCheckWith("-libdir", 1))
Expand Down Expand Up @@ -74,6 +89,7 @@ static void getBundlePath(char* path, size_t len)
#endif
#endif
}
#endif

static void addToLoaded(Library* lib)
{
Expand Down Expand Up @@ -106,7 +122,9 @@ static void removeFromLoaded(Library* lib)
void Library_Init(void)
{
lastError = Str_NewStd();
#ifdef UNIX
getcwd(appDir, sizeof(appDir));
#endif
}

void Library_Shutdown(void)
Expand All @@ -118,6 +136,7 @@ void Library_Shutdown(void)

void Library_ReleaseGames(void)
{
#ifdef UNIX
int i;

for(i = 0; i < MAX_LIBRARIES; ++i)
Expand All @@ -133,8 +152,10 @@ void Library_ReleaseGames(void)
lib->handle = 0;
}
}
#endif
}

#ifdef UNIX
static void reopenLibraryIfNeeded(Library* lib)
{
assert(lib);
Expand All @@ -147,11 +168,13 @@ static void reopenLibraryIfNeeded(Library* lib)
assert(lib->handle);
}
}
#endif

Library* Library_New(const char *fileName)
{
Library* lib = 0;
handle_t handle;
#ifdef UNIX
filename_t bundlePath; /// @todo Use ddstring_t
#ifdef MACOSX
char* ptr;
Expand Down Expand Up @@ -188,12 +211,15 @@ Library* Library_New(const char *fileName)
printf("Library_New: Error opening \"%s\" (%s).\n", bundlePath, Library_LastError());
return 0;
}
#endif

// Create the Library instance.
lib = calloc(1, sizeof(*lib));
lib->handle = handle;
lib->path = Str_NewStd();
#ifdef UNIX
Str_Set(lib->path, bundlePath);
#endif

addToLoaded(lib);

Expand All @@ -213,7 +239,9 @@ void Library_Delete(Library *lib)
assert(lib);
if(lib->handle)
{
#ifdef UNIX
dlclose(lib->handle);
#endif
}
Str_Delete(lib->path);
removeFromLoaded(lib);
Expand All @@ -222,13 +250,17 @@ void Library_Delete(Library *lib)

void* Library_Symbol(Library* lib, const char* symbolName)
{
void* ptr = 0;

assert(lib);
#ifdef UNIX
reopenLibraryIfNeeded(lib);
void* ptr = dlsym(lib->handle, symbolName);
ptr = dlsym(lib->handle, symbolName);
if(!ptr)
{
Str_Set(lastError, dlerror());
}
#endif
return ptr;
}

Expand All @@ -244,6 +276,7 @@ void Library_AddSearchDir(const char *dir)

int Library_IterateAvailableLibraries(int (*func)(const char *, void *), void *data)
{
#ifdef UNIX
struct dirent* entry = NULL;
filename_t bundlePath;
DIR* dir = NULL;
Expand Down Expand Up @@ -272,5 +305,6 @@ int Library_IterateAvailableLibraries(int (*func)(const char *, void *), void *d
if(func(entry->d_name, data)) break;
}
closedir(dir);
#endif
return 0;
}

0 comments on commit 69289ab

Please sign in to comment.