This repository has been archived by the owner on Sep 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
AddonDll.h
487 lines (438 loc) · 14.2 KB
/
AddonDll.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#pragma once
/*
* Copyright (C) 2005-2009 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "Addon.h"
#include "DllAddon.h"
#include "AddonManager.h"
#include "AddonStatusHandler.h"
#include "settings/GUIDialogSettings.h"
#include "utils/URIUtils.h"
#include "filesystem/File.h"
#include "filesystem/SpecialProtocol.h"
#include "filesystem/Directory.h"
#include "utils/log.h"
using namespace XFILE;
namespace ADDON
{
template<class TheDll, typename TheStruct, typename TheProps>
class CAddonDll : public CAddon
{
public:
CAddonDll(const AddonProps &props);
CAddonDll(const cp_extension_t *ext);
virtual ~CAddonDll();
AddonPtr Clone() const;
virtual ADDON_STATUS GetStatus();
// addon settings
virtual void SaveSettings();
virtual CStdString GetSetting(const CStdString& key);
bool Create();
virtual void Stop();
void Destroy();
protected:
void HandleException(std::exception &e, const char* context);
bool Initialized() { return m_initialized; }
virtual void BuildLibName(const cp_extension_t *ext = NULL) {}
virtual bool LoadSettings();
TheStruct* m_pStruct;
TheProps* m_pInfo;
private:
TheDll* m_pDll;
bool m_initialized;
bool LoadDll();
bool m_needsavedsettings;
virtual ADDON_STATUS TransferSettings();
TiXmlElement MakeSetting(DllSetting& setting) const;
static void AddOnStatusCallback(void *userData, const ADDON_STATUS status, const char* msg);
static bool AddOnGetSetting(void *userData, const char *settingName, void *settingValue);
static void AddOnOpenSettings(const char *url, bool bReload);
static void AddOnOpenOwnSettings(void *userData, bool bReload);
static const char* AddOnGetAddonDirectory(void *userData);
static const char* AddOnGetUserDirectory(void *userData);
};
template<class TheDll, typename TheStruct, typename TheProps>
CAddonDll<TheDll, TheStruct, TheProps>::CAddonDll(const cp_extension_t *ext)
: CAddon(ext)
{
// if library attribute isn't present, look for a system-dependent one
if (ext && m_strLibName.IsEmpty())
{
#if defined(_LINUX) && !defined(TARGET_DARWIN)
m_strLibName = CAddonMgr::Get().GetExtValue(ext->configuration, "@library_linux");
#elif defined(_WIN32) && defined(HAS_SDL_OPENGL)
m_strLibName = CAddonMgr::Get().GetExtValue(ext->configuration, "@library_wingl");
#elif defined(_WIN32) && defined(HAS_DX)
m_strLibName = CAddonMgr::Get().GetExtValue(ext->configuration, "@library_windx");
#elif defined(TARGET_DARWIN)
m_strLibName = CAddonMgr::Get().GetExtValue(ext->configuration, "@library_osx");
#endif
}
m_pStruct = NULL;
m_initialized = false;
m_pDll = NULL;
m_pInfo = NULL;
m_needsavedsettings = false;
}
template<class TheDll, typename TheStruct, typename TheProps>
CAddonDll<TheDll, TheStruct, TheProps>::CAddonDll(const AddonProps &props)
: CAddon(props)
{
m_pStruct = NULL;
m_initialized = false;
m_pDll = NULL;
m_pInfo = NULL;
m_needsavedsettings = false;
}
template<class TheDll, typename TheStruct, typename TheProps>
CAddonDll<TheDll, TheStruct, TheProps>::~CAddonDll()
{
if (m_initialized)
Destroy();
}
template<class TheDll, typename TheStruct, typename TheProps>
AddonPtr CAddonDll<TheDll, TheStruct, TheProps>::Clone() const
{
return AddonPtr(new CAddonDll<TheDll, TheStruct, TheProps>(*this));
}
template<class TheDll, typename TheStruct, typename TheProps>
bool CAddonDll<TheDll, TheStruct, TheProps>::LoadDll()
{
CStdString strFileName;
if (!Parent())
{
strFileName = LibPath();
}
else
{ //FIXME hack to load same Dll twice
CStdString extension = URIUtils::GetExtension(m_strLibName);
strFileName = "special://temp/" + m_strLibName;
URIUtils::RemoveExtension(strFileName);
strFileName += "-" + ID() + extension;
if (!CFile::Exists(strFileName))
CFile::Cache(LibPath(), strFileName);
CLog::Log(LOGNOTICE, "ADDON: Loaded virtual child addon %s", strFileName.c_str());
}
/* Check if lib being loaded exists, else check in XBMC binary location */
if (!CFile::Exists(strFileName))
{
CStdString temp = CSpecialProtocol::TranslatePath("special://xbmc/");
CStdString tempbin = CSpecialProtocol::TranslatePath("special://xbmcbin/");
strFileName.erase(0, temp.size());
strFileName = tempbin + strFileName;
if (!CFile::Exists(strFileName))
{
CLog::Log(LOGERROR, "ADDON: Could not locate %s", m_strLibName.c_str());
return false;
}
}
/* Load the Dll */
m_pDll = new TheDll;
m_pDll->SetFile(strFileName);
m_pDll->EnableDelayedUnload(false);
if (!m_pDll->Load())
{
delete m_pDll;
m_pDll = NULL;
new CAddonStatusHandler(ID(), ADDON_STATUS_UNKNOWN, "Can't load Dll", false);
return false;
}
m_pStruct = (TheStruct*)malloc(sizeof(TheStruct));
ZeroMemory(m_pStruct, sizeof(TheStruct));
m_pDll->GetAddon(m_pStruct);
return (m_pStruct != NULL);
}
template<class TheDll, typename TheStruct, typename TheProps>
bool CAddonDll<TheDll, TheStruct, TheProps>::Create()
{
CLog::Log(LOGDEBUG, "ADDON: Dll Initializing - %s", Name().c_str());
m_initialized = false;
if (!LoadDll())
return false;
try
{
ADDON_STATUS status = m_pDll->Create(NULL, m_pInfo);
if (status == ADDON_STATUS_OK)
m_initialized = true;
else if ((status == ADDON_STATUS_NEED_SETTINGS) || (status == ADDON_STATUS_NEED_SAVEDSETTINGS))
{
m_needsavedsettings = (status == ADDON_STATUS_NEED_SAVEDSETTINGS);
if (TransferSettings() == ADDON_STATUS_OK)
m_initialized = true;
else
new CAddonStatusHandler(ID(), status, "", false);
}
else
{ // Addon failed initialization
CLog::Log(LOGERROR, "ADDON: Dll %s - Client returned bad status (%i) from Create and is not usable", Name().c_str(), status);
new CAddonStatusHandler(ID(), status, "", false);
}
}
catch (std::exception &e)
{
HandleException(e, "m_pDll->Create");
}
return m_initialized;
}
template<class TheDll, typename TheStruct, typename TheProps>
void CAddonDll<TheDll, TheStruct, TheProps>::Stop()
{
/* Inform dll to stop all activities */
try
{
if (m_needsavedsettings) // If the addon supports it we save some settings to settings.xml before stop
{
char str_id[64] = "";
char str_value[1024];
CAddon::LoadUserSettings();
for (unsigned int i=0; (strcmp(str_id,"###End") != 0); i++)
{
strcpy(str_id, "###GetSavedSettings");
sprintf (str_value, "%i", i);
m_pDll->SetSetting((const char*)&str_id, (void*)&str_value);
if (strcmp(str_id,"###End") != 0) UpdateSetting(str_id, str_value);
}
CAddon::SaveSettings();
}
if (m_pDll)
{
m_pDll->Stop();
CLog::Log(LOGINFO, "ADDON: Dll Stopped - %s", Name().c_str());
}
}
catch (std::exception &e)
{
HandleException(e, "m_pDll->Stop");
}
}
template<class TheDll, typename TheStruct, typename TheProps>
void CAddonDll<TheDll, TheStruct, TheProps>::Destroy()
{
/* Unload library file */
try
{
if (m_pDll)
{
m_pDll->Destroy();
m_pDll->Unload();
}
}
catch (std::exception &e)
{
HandleException(e, "m_pDll->Unload");
}
free(m_pStruct);
m_pStruct = NULL;
if (m_pDll)
{
delete m_pDll;
m_pDll = NULL;
CLog::Log(LOGINFO, "ADDON: Dll Destroyed - %s", Name().c_str());
}
m_initialized = false;
}
template<class TheDll, typename TheStruct, typename TheProps>
ADDON_STATUS CAddonDll<TheDll, TheStruct, TheProps>::GetStatus()
{
try
{
return m_pDll->GetStatus();
}
catch (std::exception &e)
{
HandleException(e, "m_pDll->GetStatus()");
}
return ADDON_STATUS_UNKNOWN;
}
template<class TheDll, typename TheStruct, typename TheProps>
bool CAddonDll<TheDll, TheStruct, TheProps>::LoadSettings()
{
if (m_settingsLoaded)
return true;
if (!LoadDll())
return false;
ADDON_StructSetting** sSet;
std::vector<DllSetting> vSet;
unsigned entries = 0;
try
{
entries = m_pDll->GetSettings(&sSet);
DllUtils::StructToVec(entries, &sSet, &vSet);
m_pDll->FreeSettings();
}
catch (std::exception &e)
{
HandleException(e, "m_pDll->GetSettings()");
return false;
}
if (vSet.size())
{
// regenerate XML doc
m_addonXmlDoc.Clear();
TiXmlElement node("settings");
m_addonXmlDoc.InsertEndChild(node);
for (unsigned i=0; i < entries; i++)
{
DllSetting& setting = vSet[i];
m_addonXmlDoc.RootElement()->InsertEndChild(MakeSetting(setting));
}
CAddon::SettingsFromXML(m_addonXmlDoc, true);
}
else
return CAddon::LoadSettings();
m_settingsLoaded = true;
CAddon::LoadUserSettings();
return true;
}
template<class TheDll, typename TheStruct, typename TheProps>
TiXmlElement CAddonDll<TheDll, TheStruct, TheProps>::MakeSetting(DllSetting& setting) const
{
TiXmlElement node("setting");
switch (setting.type)
{
case DllSetting::CHECK:
{
node.SetAttribute("id", setting.id);
node.SetAttribute("type", "bool");
node.SetAttribute("label", setting.label);
break;
}
case DllSetting::SPIN:
{
node.SetAttribute("id", setting.id);
node.SetAttribute("type", "enum");
node.SetAttribute("label", setting.label);
CStdString values;
for (unsigned int i = 0; i < setting.entry.size(); i++)
{
values.append(setting.entry[i]);
values.append("|");
}
node.SetAttribute("values", values.c_str());
break;
}
default:
break;
}
return node;
}
template<class TheDll, typename TheStruct, typename TheProps>
void CAddonDll<TheDll, TheStruct, TheProps>::SaveSettings()
{
// must save first, as TransferSettings() reloads saved settings!
CAddon::SaveSettings();
if (m_initialized)
TransferSettings();
}
template<class TheDll, typename TheStruct, typename TheProps>
CStdString CAddonDll<TheDll, TheStruct, TheProps>::GetSetting(const CStdString& key)
{
return CAddon::GetSetting(key);
}
template<class TheDll, typename TheStruct, typename TheProps>
ADDON_STATUS CAddonDll<TheDll, TheStruct, TheProps>::TransferSettings()
{
bool restart = false;
ADDON_STATUS reportStatus = ADDON_STATUS_OK;
CLog::Log(LOGDEBUG, "Calling TransferSettings for: %s", Name().c_str());
LoadSettings();
const TiXmlElement *category = m_addonXmlDoc.RootElement() ? m_addonXmlDoc.RootElement()->FirstChildElement("category") : NULL;
if (!category)
category = m_addonXmlDoc.RootElement(); // no categories
while (category)
{
const TiXmlElement *setting = category->FirstChildElement("setting");
while (setting)
{
ADDON_STATUS status = ADDON_STATUS_OK;
const char *id = setting->Attribute("id");
const char *type = setting->Attribute("type");
const char *option = setting->Attribute("option");
if (type)
{
if (strcmpi(type,"sep") == 0 || strcmpi(type,"lsep") == 0)
{
/* Don't propagate separators */
}
else if (strcmpi(type, "text") == 0 || strcmpi(type, "ipaddress") == 0 ||
strcmpi(type, "video") == 0 || strcmpi(type, "audio") == 0 ||
strcmpi(type, "image") == 0 || strcmpi(type, "folder") == 0 ||
strcmpi(type, "executable") == 0 || strcmpi(type, "file") == 0 ||
strcmpi(type, "action") == 0 || strcmpi(type, "date") == 0 ||
strcmpi(type, "time") == 0 || strcmpi(type, "select") == 0 ||
strcmpi(type, "addon") == 0 || strcmpi(type, "labelenum") == 0 ||
strcmpi(type, "fileenum") == 0 )
{
status = m_pDll->SetSetting(id, (const char*) GetSetting(id).c_str());
}
else if ((strcmpi(type, "enum") == 0 || strcmpi(type,"integer") == 0))
{
int tmp = atoi(GetSetting(id));
status = m_pDll->SetSetting(id, (int*) &tmp);
}
else if (strcmpi(type, "bool") == 0)
{
bool tmp = (GetSetting(id) == "true") ? true : false;
status = m_pDll->SetSetting(id, (bool*) &tmp);
}
else if (strcmpi(type, "rangeofnum") == 0 || strcmpi(type, "slider") == 0 ||
strcmpi(type, "number") == 0)
{
float tmpf = (float)atof(GetSetting(id));
int tmpi;
if (option && strcmpi(option,"int") == 0)
{
tmpi = (int)floor(tmpf);
status = m_pDll->SetSetting(id, (int*) &tmpi);
}
else
{
status = m_pDll->SetSetting(id, (float*) &tmpf);
}
}
else
{
/* Log unknowns as an error, but go ahead and transfer the string */
CLog::Log(LOGERROR, "Unknown setting type '%s' for %s", type, Name().c_str());
status = m_pDll->SetSetting(id, (const char*) GetSetting(id).c_str());
}
if (status == ADDON_STATUS_NEED_RESTART)
restart = true;
else if (status != ADDON_STATUS_OK)
reportStatus = status;
}
setting = setting->NextSiblingElement("setting");
}
category = category->NextSiblingElement("category");
}
if (restart || reportStatus != ADDON_STATUS_OK)
{
new CAddonStatusHandler(ID(), restart ? ADDON_STATUS_NEED_RESTART : reportStatus, "", true);
}
return ADDON_STATUS_OK;
}
template<class TheDll, typename TheStruct, typename TheProps>
void CAddonDll<TheDll, TheStruct, TheProps>::HandleException(std::exception &e, const char* context)
{
m_initialized = false;
m_pDll->Unload();
CLog::Log(LOGERROR, "ADDON: Dll %s, throws an exception '%s' during %s. Contact developer '%s' with bug reports", Name().c_str(), e.what(), context, Author().c_str());
}
}; /* namespace ADDON */