source: golgotha/src/i4/dll/dll_man.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.5 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "dll/dll_man.hh"
10#include "file/file.hh"
11#include "memory/malloc.hh"
12#include "error/alert.hh"
13#include <stdio.h>
14
15i4_dll_manager_class i4_dll_man;
16
17#ifdef __linux
18char *i4_dll_dir="linux_dlls";
19#elif _WINDOWS
20char *i4_dll_dir="win32_dlls";
21#endif
22
23void i4_dll_manager_class::init()
24{
25  i4_directory_struct ds;
26  if (i4_get_directory(i4_const_str(i4_dll_dir), ds))
27  {
28
29    i4_const_str dll_extension(i4_const_str("dll"));
30    i4_const_str debug_substr(i4_const_str("_debug"));
31
32    for (int f=0; f<ds.tfiles; f++)
33    {
34      i4_filename_struct fn;
35      i4_split_path(*ds.files[f], fn);
36
37      int is_debug=(strstr(fn.filename, "_debug")==0) ? 0 : 1;
38
39      if (fn.extension[0] && strcmp(fn.extension, "dll")==0 &&
40#ifdef DEBUG
41          is_debug
42#else
43          !is_debug
44#endif
45          )
46      {
47        i4_str *fullname = i4_const_str("%s/%S").sprintf(512, i4_dll_dir, ds.files[f]);
48        load(*fullname);
49        delete fullname;
50      }
51    }
52  }
53}
54
55void i4_dll_manager_class::uninit()
56{
57  while (dll_list.begin()!=dll_list.end())
58  {
59    dll_node *i=&*dll_list.begin();
60
61    i4_init_class *old_first_init=i4_init_class::first_init;
62    i4_init_class::first_init=i->init_list;
63
64    for (i4_init_class *f=i->init_list; f; f=f->next_init)
65      f->uninit();
66
67    dll_list.erase();
68    delete i;
69
70    i4_init_class::first_init=old_first_init;
71  }
72
73  dll_list.destroy_all();
74}
75
76i4_bool i4_dll_manager_class::load(const i4_const_str &filename, i4_bool prepend_dll_dir)
77{
78  i4_const_str s=filename;
79  char tmp1[512],tmp2[512];
80  if (prepend_dll_dir)
81  {
82    sprintf(tmp2, "%s/%s",i4_dll_dir,i4_os_string(filename, tmp1, 512));
83    return load(i4_const_str(tmp2));
84  }
85
86  i4_str *full=i4_full_path(filename);
87
88  i4_alert(i4_const_str("Loading DLL [%S]"),512, full);
89
90  if (is_loaded(*full)) 
91    unload(*full);
92
93  // add this to the dll list
94  dll_node *node=new dll_node;
95  node->name=full;
96
97  i4_init_class *old_first_init=i4_init_class::first_init;
98  i4_init_class::first_init=0;
99
100  node->dll=i4_open_dll(*full);
101
102  node->init_list=i4_init_class::first_init;
103  i4_init_class::first_init=old_first_init;
104
105  if (!node->dll)
106  {
107    delete node;
108    return i4_F;
109  }
110
111  for (i4_init_class *f=node->init_list; f; f=f->next_init)
112    f->init();
113
114  dll_list.insert(*node);
115  return i4_T;
116}
117
118i4_bool i4_dll_manager_class::is_loaded(const i4_const_str &filename)
119{
120  for (i4_isl_list<dll_node>::iterator i=dll_list.begin(); i!=dll_list.end(); ++i)
121    if (*i->name == filename)
122      return i4_T;
123  return i4_F;
124}
125
126
127i4_bool i4_dll_manager_class::unload(const i4_const_str &filename)
128{
129  for (i4_isl_list<dll_node>::iterator i=dll_list.begin(); i!=dll_list.end(); ++i)
130    if (*i->name == filename)
131    {
132      for (i4_init_class *f=i->init_list; f; f=f->next_init)
133        f->uninit();
134      i->init_list=0;
135
136      delete dll_list.find_and_unlink(&*i);
137      return i4_T;
138    }
139
140  return i4_F;
141}
142
143i4_dll_manager_class::dll_node::~dll_node()
144{
145  if (name) delete name;
146  if (dll) delete dll;
147}
Note: See TracBrowser for help on using the repository browser.