libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moPlugin.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moPlugin.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andrés Colubri
29 
30 *******************************************************************************/
31 #include <string>
32 #include "moPlugin.h"
33 #include <moPort.h>
34 
35 #include "moArray.h"
36 moDefineDynamicArray( moPluginDefinitions )
37 moDefineDynamicArray( moPluginsArray )
38 
39 
41 }
42 //===========================================
43 //
44 // moPlugin
45 //
46 //===========================================
47 
48 void moPlugin::Load(moText plugin_file)
49 {
50  name = plugin_file;
51  handle = moLoadPlugin(plugin_file);
52 
53  if(!handle) {
54  #ifndef MO_WIN32
55  moDebugManager::Error( "moPlugin::Load > Cannot open library: " + moText(dlerror()) );
56  #else
57  CHAR szBuf[80];
58  DWORD dw = GetLastError();
59  sprintf(szBuf, "%s failed: GetLastError returned %i\n",
60  (char*)plugin_file, (int)dw);
61  moDebugManager::Error( "moPlugin::Load > Cannot open library: " + moText(szBuf) );
62  #endif
63  return;
64  }
65 
66  #ifdef MO_WIN32
67  FARPROC farp;
68  farp = GetProcAddress(handle, "DestroyEffectFactory");
69  CreateEffectFactory = CreateEffectFactoryFunction(GetProcAddress(handle, "CreateEffectFactory"));
70  DestroyEffectFactory = DestroyEffectFactoryFunction(GetProcAddress(handle, "DestroyEffectFactory"));
71  #else
72  CreateEffectFactory = CreateEffectFactoryFunction(dlsym(handle, "CreateEffectFactory"));
73  DestroyEffectFactory = DestroyEffectFactoryFunction(dlsym(handle, "DestroyEffectFactory"));
74  #endif
75 
76  if(this->CreateEffectFactory!=NULL)
78 
79 }
80 
82 {
84 
85  CreateEffectFactory = NULL;
86  DestroyEffectFactory = NULL;
87 
88  moUnloadPlugin(handle);
89 }
90 
91 
93 
94  moEffect **narray;
95 
96  if(m_factory!=NULL) {
97  moEffect *pfx = m_factory->Create();
98 
99  if(pfx==NULL) return NULL;
100 
101  if(n==0) {//primera vez
102  array = new moEffect* [1];
103  } else if(n>0) {//array dinamico
104  narray = new moEffect* [n+1];//generamos el nuevo vacio
105  for(int i=0;i<n;i++) narray[i] = array[i];//copiamos el array
106  delete [] array;
107  array = narray;
108  }
109 
110  array[n] = pfx;
111  n++;
112  return pfx;
113  }
114  return NULL;
115 }
116 
118 
119  moEffect **narray;
120  int i,j;
121 
122  if(m_factory!=NULL) {
123 
124  for(j=0;j<n;j++)
125  if(array[j]==Efecto) {
126 
127  m_factory->Destroy(Efecto);
128 
129  if(n==1) {//muere
130  delete [] array;
131  } else if(n>1) {//array dinamico
132  narray = new moEffect* [n-1];//generamos el nuevo vacio
133  for(i=0;i<j;i++) narray[i] = array[i];//copiamos el array hasta el j(destruido)
134  for(i=j;i<(n-1);i++) narray[i] = array[i+1];//copiamos el array desde el j
135  delete [] array;
136  array = narray;
137  }
138  n--;
139  return true;
140  }
141  }
142  return false;
143 }
144 
145 
146 LIBMOLDEO_API moEffect* moNewEffect(moText effect_name, moPluginsArray &plugins)
147 {
148  // Creando el nombre complete del plugin(incluyendo ruta por defecto)
149  // a partir del nombre del efecto.
150  moText complete_name;
151 
152  if(!stricmp(effect_name, "nil")) return NULL;
153 
154  #if defined(_WIN32)
155  complete_name = moText(moDataManager::GetModulesDir()+ "/effects/") + (moText)effect_name;
156  #ifdef _DEBUG
157  complete_name+= moText("_d");
158  #endif
159  complete_name += moText(".dll");
160  #else
161  complete_name = moText(moDataManager::GetModulesDir()+ "/effects/libmoldeo_") + (moText)effect_name;
162  #ifdef _DEBUG
163  complete_name+= moText("_d");
164  #endif
165  complete_name += moPluginExtension;
166  #endif
167 
168  // Indice del plugin que se utilizara para crear a este efecto.
169  int plg_index = -1;
170 
171  // First, revisa que el plugin ya no haya sido cargado antes.
172  for(MOuint i = 0; i < plugins.Count(); i++)
173  if(!stricmp(plugins[i]->GetName(), complete_name))
174  {
175  plg_index = i;
176  break;
177  }
178 
179  if(plg_index == -1)
180  {
181  // Es la primera vez que se intenta cargar este plugin. Se lo agrega al array.
182  // Falta control de errores(que pasa si la libreria no carga, etc?) :-)
183  plg_index = plugins.Count();
184  moPlugin* pPlugin = new moPlugin(complete_name);
185  plugins.Add( pPlugin );
186  }
187 
188  // El plugin crea al efecto!
189  if(plugins[plg_index]->m_factory!=NULL)
190  return plugins[plg_index]->Create();
191  else return NULL;
192 }
193 
194 
195 LIBMOLDEO_API bool moDeleteEffect(moEffect *effect, moPluginsArray &plugins)
196 {
197  // Creando el nombre complete del plugin(incluyendo ruta por defecto)
198  // a partir del nombre del efecto.
199  moText complete_name;
200 
201  if(!stricmp(effect->GetName(), "")) return false;
202 
203  #if defined(_WIN32)
204  complete_name = moText(moDataManager::GetModulesDir()+ "/effects/") + moText(effect->GetName());
205  #ifdef _DEBUG
206  complete_name+= moText("_d");
207  #endif
208  complete_name += moText(".dll");
209  #else
210  complete_name = moText(moDataManager::GetModulesDir()+ "/effects/libmoldeo_") + moText(effect->GetName());
211  #ifdef _DEBUG
212  complete_name+= moText("_d");
213  #endif
214  complete_name += moPluginExtension;
215  #endif
216 
217  // Indice del plugin que se utilizara para crear a este efecto.
218  int plg_index = -1;
219 
220  // First, revisa que el plugin ya no haya sido cargado antes.
221  for(MOuint i = 0; i < plugins.Count(); i++)
222  if(!stricmp(plugins[i]->GetName(), complete_name))
223  {
224  plg_index = i;
225  break;
226  }
227 
228  if(plg_index == -1)
229  {
230  return false;
231  }
232 
233  bool res = plugins[plg_index]->Destroy(effect);
234 
236  if (res && plugins[plg_index]->n == 0) {
237  plugins[plg_index]->Unload();
238  plugins.Remove(plg_index);
239 
240  }
241 
242  return res;
243 }
244 
245 
Plugin de efecto.
Definition: moPlugin.h:61
LIBMOLDEO_API bool moDeleteEffect(moEffect *effect, moPluginsArray &plugins)
Definition: moPlugin.cpp:195
moEffectFactory *(MO_PLG_ENTRY * CreateEffectFactoryFunction)()
Definition: moPlugin.h:52
static void Error(moText p_text)
Anuncia un error.
moEffectFactory * m_factory
Definition: moPlugin.h:70
virtual void Destroy(moEffect *fx)=0
const moText & GetName() const
LIBMOLDEO_API moEffect * moNewEffect(moText effect_name, moPluginsArray &plugins)
Definition: moPlugin.cpp:146
moEffect ** array
Definition: moPlugin.h:64
int n
Definition: moPlugin.h:65
#define LIBMOLDEO_API
Definition: moTypes.h:180
virtual moEffect * Create(void)=0
bool Destroy(moEffect *effect)
Definition: moPlugin.cpp:117
clase de para manejar textos
Definition: moText.h:75
void(MO_PLG_ENTRY * DestroyEffectFactoryFunction)()
Definition: moPlugin.h:53
moDefineDynamicArray(moPluginDefinitions) moDefineDynamicArray(moPluginsArray) moEffectFactory
Definition: moPlugin.cpp:36
DestroyEffectFactoryFunction DestroyEffectFactory
Definition: moPlugin.h:68
void moUnloadPlugin(MOpluginHandle &handle)
Definition: moBasePlugin.h:64
moText0 moText
Definition: moText.h:291
static const moText & GetModulesDir()
Fábrica de efectos, clase abstracta pura a implementar para un plugin derivado de moEffect.
Definition: moPlugin.h:43
void Load(moText plugin_file)
Definition: moPlugin.cpp:48
CreateEffectFactoryFunction CreateEffectFactory
Definition: moPlugin.h:67
moEffect * Create()
Definition: moPlugin.cpp:92
#define MOuint
Definition: moTypes.h:387
clase base para objetos dibujables
Definition: moEffect.h:82
void Unload()
Definition: moPlugin.cpp:81
MOpluginHandle moLoadPlugin(moText fn)
Definition: moBasePlugin.h:55