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
moMasterPlugin.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moMasterPlugin.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 
32 #include "moMasterPlugin.h"
33 #include <moPort.h>
34 #include "moArray.h"
35 moDefineDynamicArray( moMasterPluginsArray )
36 
37 
38 #include <iostream>
39 using namespace std;
40 
42 }
43 
44 
45 //===========================================
46 //
47 // moMasterPlugin
48 //
49 //===========================================
50 
51 void moMasterPlugin::Load(moText plugin_file)
52 {
53  name = plugin_file;
54  handle = moLoadPlugin(plugin_file);
55 
56  if(!handle) {
57  #ifndef MO_WIN32
58  moDebugManager::Error( "moMasterPlugin::Load > Cannot open library: " + moText(dlerror()) );
59  #else
60  CHAR szBuf[80];
61  DWORD dw = GetLastError();
62  sprintf(szBuf, "%s failed: GetLastError returned %i\n",
63  (char*)plugin_file, (int)dw);
64  moDebugManager::Error( "moMasterPlugin::Load > Cannot open library: " + moText(szBuf) );
65  #endif
66  return;
67  }
68 
69  #ifdef MO_WIN32
70  FARPROC farp;
71  farp = GetProcAddress(handle, "DestroyMasterEffectFactory");
72  CreateMasterEffectFactory = CreateMasterEffectFactoryFunction(GetProcAddress(handle, "CreateMasterEffectFactory"));
73  DestroyMasterEffectFactory = DestroyMasterEffectFactoryFunction(GetProcAddress(handle, "DestroyMasterEffectFactory"));
74  #else
75  CreateMasterEffectFactory = CreateMasterEffectFactoryFunction(dlsym(handle, "CreateMasterEffectFactory"));
76  DestroyMasterEffectFactory = DestroyMasterEffectFactoryFunction(dlsym(handle, "DestroyMasterEffectFactory"));
77  #endif
78 
79  if(this->CreateMasterEffectFactory!=NULL)
80  m_factory = this->CreateMasterEffectFactory();
81 
82 }
83 
85 {
86  this->DestroyMasterEffectFactory();
87 
88  CreateMasterEffectFactory = NULL;
89  DestroyMasterEffectFactory = NULL;
90 
91  moUnloadPlugin(handle);
92 }
93 
94 
96 
97  moMasterEffect **narray;
98 
99  if(m_factory!=NULL) {
100  moMasterEffect *pfx = m_factory->Create();
101 
102  if(pfx==NULL) return NULL;
103 
104  if(n==0) {//primera vez
105  array = new moMasterEffect* [1];
106  } else if(n>0) {//array dinamico
107  narray = new moMasterEffect* [n+1];//generamos el nuevo vacio
108  for(int i=0;i<n;i++) narray[i] = array[i];//copiamos el array
109  delete [] array;
110  array = narray;
111  }
112 
113  array[n] = pfx;
114  n++;
115  return pfx;
116  }
117  return NULL;
118 }
119 
121 
122  moMasterEffect **narray;
123  int i,j;
124 
125  if(m_factory!=NULL) {
126 
127  for(j=0;j<n;j++)
128  if(array[j]==EfectoMaestro) {
129 
130  m_factory->Destroy(EfectoMaestro);
131 
132  if(n==1) {//muere
133  delete [] array;
134  } else if(n>1) {//array dinamico
135  narray = new moMasterEffect* [n-1];//generamos el nuevo vacio
136  for(i=0;i<j;i++) narray[i] = array[i];//copiamos el array hasta el j(destruido)
137  for(i=j;i<(n-1);i++) narray[i] = array[i+1];//copiamos el array desde el j
138  delete [] array;
139  array = narray;
140  }
141  n--;
142  return true;
143  }
144  }
145  return false;
146 }
147 
148 //===========================================
149 //
150 // moMasterPluginsArray
151 //
152 //===========================================
153 /*
154 void moMasterPluginsArray::Add(moMasterPlugin* plugin)
155 {
156  if(length < max_length)
157  {
158  if(plugin!=NULL) {
159  cout << "plugin agregado\n";
160  array[length] = plugin;
161  length++;
162  } else {
163  cout << "error: plugin no fue creado!\n";
164  }
165  }
166 }
167 
168 void moMasterPluginsArray::Init(MOuint nplugins)
169 {
170  max_length = 256;
171  length = nplugins;
172  array = new moMasterPlugin*[max_length];
173  for(MOuint i = 0; i < max_length; i++) array[i] = NULL;
174 }
175 
176 void moMasterPluginsArray::Finish()
177 {
178  if(array != NULL)
179  {
180  delete[] array;
181  array = NULL;
182  }
183 
184  length = 0;
185 }
186 */
187 
188 LIBMOLDEO_API moMasterEffect* moNewMasterEffect(moText effect_name, moMasterPluginsArray &plugins)
189 {
190  // Creando el nombre complete del plugin(incluyendo ruta por defecto)
191  // a partir del name del efecto.
192  moText complete_name;
193 
194  if(!stricmp(effect_name, "nil")) return NULL;
195 
196  #if defined(_WIN32)
197  complete_name = moText(moDataManager::GetModulesDir()+ "/mastereffects/") + (moText)effect_name;
198  #ifdef _DEBUG
199  complete_name+= moText("_d");
200  #endif
201  complete_name += moText(".dll");
202  #else
203  complete_name = moText(moDataManager::GetModulesDir()+ "/mastereffects/libmoldeo_") + (moText)effect_name;
204  #ifdef _DEBUG
205  complete_name+= moText("_d");
206  #endif
207  complete_name += moPluginExtension;
208  #endif
209 
210  // Indice del plugin que se utilizara para crear a este efecto.
211  int plg_index = -1;
212 
213  // First, revisa que el plugin ya no haya sido cargado antes.
214  for(MOuint i = 0; i < plugins.Count(); i++)
215  if(!stricmp(plugins[i]->GetName(), complete_name))
216  {
217  plg_index = i;
218  break;
219  }
220 
221  if(plg_index == -1)
222  {
223  // Es la primera vez que se intenta cargar este plugin. Se lo agrega al array.
224  // Falta control de errores(que pasa si la libreria no carga, etc?) :-)
225  plg_index = plugins.Count();
226  moMasterPlugin *pplugin = new moMasterPlugin(complete_name);
227  plugins.Add( pplugin );
228  }
229 
230  // El plugin crea al efecto!
231  if(plugins[plg_index]->m_factory!=NULL)
232  return plugins[plg_index]->Create();
233  else return NULL;
234 }
235 
236 
237 LIBMOLDEO_API bool moDeleteMasterEffect(moMasterEffect *mastereffect, moMasterPluginsArray &plugins)
238 {
239  // Creando el nombre complete del plugin(incluyendo ruta por defecto)
240  // a partir del nombre del efecto.
241  moText complete_name;
242 
243  if( mastereffect->GetName() == "" ) return false;
244 
245  #if defined(_WIN32)
246  complete_name = moText(moDataManager::GetModulesDir()+ "/mastereffects/") + moText(mastereffect->GetName());
247  #ifdef _DEBUG
248  complete_name+= moText("_d");
249  #endif
250  complete_name += moText(".dll");
251  #else
252  complete_name = moText(moDataManager::GetModulesDir()+ "/mastereffects/libmoldeo_") + moText(mastereffect->GetName());
253  #ifdef _DEBUG
254  complete_name+= moText("_d");
255  #endif
256  complete_name += moPluginExtension;
257  #endif
258 
259  // Indice del plugin que se utilizara para crear a este efecto.
260  int plg_index = -1;
261 
262  // First, revisa que el plugin ya no haya sido cargado antes.
263  for(MOuint i = 0; i < plugins.Count(); i++)
264  if(!stricmp(plugins[i]->GetName(), complete_name))
265  {
266  plg_index = i;
267  break;
268  }
269 
270  if(plg_index == -1)
271  {
272  return false;
273  }
274 
275  bool res = plugins[plg_index]->Destroy(mastereffect);
276 
278  if (res && plugins[plg_index]->n == 0) {
279  plugins[plg_index]->Unload();
280  plugins.Remove(plg_index);
281 
282  }
283 
284  return res;
285 
286 }
287 
LIBMOLDEO_API moMasterEffect * moNewMasterEffect(moText effect_name, moMasterPluginsArray &plugins)
static void Error(moText p_text)
Anuncia un error.
moDefineDynamicArray(moMasterPluginsArray) using namespace std
const moText & GetName() const
bool Destroy(moMasterEffect *mastereffect)
LIBMOLDEO_API bool moDeleteMasterEffect(moMasterEffect *mastereffect, moMasterPluginsArray &plugins)
#define LIBMOLDEO_API
Definition: moTypes.h:180
clase de para manejar textos
Definition: moText.h:75
void moUnloadPlugin(MOpluginHandle &handle)
Definition: moBasePlugin.h:64
moText0 moText
Definition: moText.h:291
static const moText & GetModulesDir()
moMasterEffectFactory *(MO_PLG_ENTRY * CreateMasterEffectFactoryFunction)()
void Load(moText plugin_file)
void(MO_PLG_ENTRY * DestroyMasterEffectFactoryFunction)()
#define MOuint
Definition: moTypes.h:387
virtual ~moMasterEffectFactory()
moMasterEffect * Create()
MOpluginHandle moLoadPlugin(moText fn)
Definition: moBasePlugin.h:55