Kodi Development 22.0
for Binary and Script based Add-Ons
 
Loading...
Searching...
No Matches
DllHelper.h
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#pragma once
10
11#ifdef __cplusplus
12
13#include <string>
14
15#include <dlfcn.h>
16#include <kodi/AddonBase.h>
17#include <kodi/Filesystem.h>
18
19//==============================================================================
27#define REGISTER_DLL_SYMBOL(functionPtr) \
28 kodi::tools::CDllHelper::RegisterSymbol(functionPtr, #functionPtr)
29//------------------------------------------------------------------------------
30
31namespace kodi
32{
33namespace tools
34{
35
36//==============================================================================
92class ATTR_DLL_LOCAL CDllHelper
93{
94public:
95 //============================================================================
99 CDllHelper() = default;
100 //----------------------------------------------------------------------------
101
102 //============================================================================
106 virtual ~CDllHelper()
107 {
108 if (m_dll)
109 dlclose(m_dll);
110 }
111 //----------------------------------------------------------------------------
112
113 //============================================================================
120 bool LoadDll(std::string path)
121 {
122#if defined(TARGET_ANDROID)
123 if (kodi::vfs::FileExists(path))
124 {
125 // Check already defined for "xbmcaltbinaddons", if yes no copy necassary.
126 std::string xbmcaltbinaddons =
127 kodi::vfs::TranslateSpecialProtocol("special://xbmcaltbinaddons/");
128 if (path.compare(0, xbmcaltbinaddons.length(), xbmcaltbinaddons) != 0)
129 {
130 bool doCopy = true;
131 std::string dstfile = xbmcaltbinaddons + kodi::vfs::GetFileName(path);
132
133 kodi::vfs::FileStatus dstFileStat;
134 if (kodi::vfs::StatFile(dstfile, dstFileStat))
135 {
136 kodi::vfs::FileStatus srcFileStat;
137 if (kodi::vfs::StatFile(path, srcFileStat))
138 {
139 if (dstFileStat.GetSize() == srcFileStat.GetSize() &&
140 dstFileStat.GetModificationTime() > srcFileStat.GetModificationTime())
141 doCopy = false;
142 }
143 }
144
145 if (doCopy)
146 {
147 kodi::Log(ADDON_LOG_DEBUG, "Caching '%s' to '%s'", path.c_str(), dstfile.c_str());
148 if (!kodi::vfs::CopyFile(path, dstfile))
149 {
150 kodi::Log(ADDON_LOG_ERROR, "Failed to cache '%s' to '%s'", path.c_str(),
151 dstfile.c_str());
152 return false;
153 }
154 }
155
156 path = dstfile;
157 }
158 }
159 else
160 {
161 return false;
162 }
163#endif
164
165 m_dll = dlopen(path.c_str(), RTLD_LOCAL | RTLD_LAZY);
166 if (m_dll == nullptr)
167 {
168 kodi::Log(ADDON_LOG_ERROR, "Unable to load %s", dlerror());
169 return false;
170 }
171 return true;
172 }
173 //----------------------------------------------------------------------------
174
175 //============================================================================
189 template<typename T>
190 bool RegisterSymbol(T& functionPtr, const char* strFunctionPtr)
191 {
192 functionPtr = reinterpret_cast<T>(dlsym(m_dll, strFunctionPtr));
193 if (functionPtr == nullptr)
194 {
195 kodi::Log(ADDON_LOG_ERROR, "Unable to assign function %s", dlerror());
196 return false;
197 }
198 return true;
199 }
200 //----------------------------------------------------------------------------
201
202private:
203 void* m_dll = nullptr;
204};
206//------------------------------------------------------------------------------
207
208} /* namespace tools */
209} /* namespace kodi */
210
211#endif /* __cplusplus */
Definition DllHelper.h:93
Definition Filesystem.h:96
@ ADDON_LOG_DEBUG
0 : To include debug information in the log file.
Definition addon_base.h:184
@ ADDON_LOG_ERROR
3 : To report error messages in the log file.
Definition addon_base.h:193
bool LoadDll(std::string path)
Function to load requested library.
Definition DllHelper.h:120
virtual ~CDllHelper()
Class destructor.
Definition DllHelper.h:106
bool RegisterSymbol(T &functionPtr, const char *strFunctionPtr)
Function to register requested library symbol.
Definition DllHelper.h:190
CDllHelper()=default
Class constructor.
uint64_t GetSize() const
Get total size, in bytes.
Definition Filesystem.h:150
time_t GetModificationTime() const
Get time of last modification.
Definition Filesystem.h:165
bool ATTR_DLL_LOCAL CopyFile(const std::string &filename, const std::string &destination)
Copy a file from source to destination.
Definition Filesystem.h:1074
bool ATTR_DLL_LOCAL StatFile(const std::string &filename, kodi::vfs::FileStatus &buffer)
Get file status.
Definition Filesystem.h:1000
bool ATTR_DLL_LOCAL FileExists(const std::string &filename, bool usecache=false)
Check if a file exists.
Definition Filesystem.h:945
std::string ATTR_DLL_LOCAL TranslateSpecialProtocol(const std::string &source)
Returns the translated path.
Definition Filesystem.h:1293
std::string ATTR_DLL_LOCAL GetFileName(const std::string &path)
Return the file name from given complete path string.
Definition Filesystem.h:1377
void ATTR_DLL_LOCAL Log(const ADDON_LOG loglevel, const char *format,...)
Add a message to Kodi's log.
Definition AddonBase.h:1938