File: ccPluginManager.cpp

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (276 lines) | stat: -rw-r--r-- 7,678 bytes parent folder | download | duplicates (2)
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
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  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; version 2 or later of the License.      #
//#                                                                        #
//#  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.                          #
//#                                                                        #
//#          COPYRIGHT: CloudCompare project                               #
//#                                                                        #
//##########################################################################

#include "ccPluginManager.h"

//qCC_db
#include <ccExternalFactory.h>
#include <ccLog.h>

//plugins
#include "ccGLFilterPluginInterface.h"
#include "ccIOFilterPluginInterface.h"
#include "ccStdPluginInterface.h"

//Qt
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QPluginLoader>
#include <QSet>
#include <QStandardPaths>


QStringList ccPluginManager::m_PluginPaths;
ccPluginInterfaceList ccPluginManager::m_pluginList;


// Check for metadata and warn if it's not there
// This indicates that a plugin hasn't been converted to the new JSON metadata.
// It is going to be required in a future verison of CloudCompare.
static void	sWarnIfNoMetaData( QPluginLoader *loader, const QString &fileName )
{
	QJsonObject	metaObject = loader->metaData();
	
	if ( metaObject.isEmpty() || metaObject["MetaData"].toObject().isEmpty() )
	{
		ccLog::Warning( QStringLiteral( "\t%1 does not supply meta data in the Q_PLUGIN_METADATA (see one of the example plugins). This will be required in a future verison of CloudCompare." ).arg( fileName ) );				
	}
	else
	{
		QJsonObject	data = metaObject["MetaData"].toObject();
		
		// The plugin type is going to be required
		const QStringList validTypes{ "GL", "I/O", "Standard" };
		
		const QString	pluginType = data["type"].toString();
		
		if ( !validTypes.contains( pluginType ) )
		{
			ccLog::Warning( QStringLiteral( "\t%1 does not supply a valid plugin type in its info.json. It must be one of: %2" ).arg( fileName, validTypes.join( ", " ) ) );
		}
	}
}	


ccPluginManager::ccPluginManager( QObject *parent ) :
    QObject( parent )
{
}

void ccPluginManager::setPaths( const QStringList &paths )
{
	m_PluginPaths = paths;
}

QStringList ccPluginManager::pluginPaths()
{
	return m_PluginPaths;
}

void ccPluginManager::loadPlugins()
{
	m_pluginList.clear();
	
	if ( m_PluginPaths.empty() )
	{
		qWarning() << "There are no plugin paths set. Maybe missing a call to ccPluginManager::setPaths()?";
	}

	// "static" plugins
	const QObjectList	pluginInstances = QPluginLoader::staticInstances();

	for ( QObject* plugin : pluginInstances )
	{
		ccPluginInterface* ccPlugin = dynamic_cast<ccPluginInterface*>(plugin);

		if (ccPlugin == nullptr)
		{
			continue;
		}

		ccLog::Print(QStringLiteral("[Plugin] Found: %1 (STATIC)").arg(ccPlugin->getName()));
		m_pluginList.push_back(ccPlugin);
	}

	// "dynamic" plugins
	loadFromPathsAndAddToList();

	// now iterate over plugins and automatically register what we can
	for ( ccPluginInterface *plugin : m_pluginList )
	{
		if ( plugin == nullptr )
		{
			Q_ASSERT(false);
			continue;
		}

		switch ( plugin->getType() )
		{
			case CC_STD_PLUGIN:
			{
				ccStdPluginInterface* stdPlugin = static_cast<ccStdPluginInterface*>(plugin);

				//see if this plugin provides an additional factory for objects
				ccExternalFactory* factory = stdPlugin->getCustomObjectsFactory();
				if (factory)
				{
					//if it's valid, add it to the factories set
					Q_ASSERT(ccExternalFactory::Container::GetUniqueInstance());
					ccExternalFactory::Container::GetUniqueInstance()->addFactory(factory);
				}
			}
				break;

			case CC_IO_FILTER_PLUGIN: //I/O filter
			{
				ccIOFilterPluginInterface* ioPlugin = static_cast<ccIOFilterPluginInterface*>(plugin);
				
				for ( auto &filter : ioPlugin->getFilters() )
				{
					if (filter)
					{
						FileIOFilter::Register(filter);
						ccLog::Print(QString("[Plugin][%1] New file extension(s) registered: %2").arg(ioPlugin->getName(), filter->getDefaultExtension().toUpper()));
					}
				}
			}
				break;

			default:
				//nothing to do at this point
				break;
		}
	}
}

ccPluginInterfaceList &ccPluginManager::pluginList()
{
#ifdef QT_DEBUG
	if ( m_pluginList.empty() )
	{
		qWarning() << "Plugin list is empty - did you call loadPlugins()?";
	}
#endif
	
	return m_pluginList;
}

void ccPluginManager::loadFromPathsAndAddToList()
{
	const QStringList nameFilters{
#if defined(Q_OS_MAC)
		"*.dylib"
#elif defined(Q_OS_WIN)
		"*.dll"
#elif defined(Q_OS_LINUX)
		"*.so"
#else
#error Need to specify the dynamic library extension for this OS.
#endif
	};
	
	// Map the plugin file name to its loader so we can unload it if necessary.
	//	This lets us override plugins by path.
	QMap<QString, QPluginLoader *> fileNameToLoaderMap;
	
	for ( const QString &path : pluginPaths() )
	{
		ccLog::Print( tr( "[Plugin] Searching: %1" ).arg( path ) );
		
		QDir pluginsDir( path );

		pluginsDir.setNameFilters( nameFilters );

		const QStringList	fileNames = pluginsDir.entryList();

		for ( const QString &fileName : fileNames )
		{
			const QString pluginPath = pluginsDir.absoluteFilePath( fileName );
			
			QPluginLoader *loader = new QPluginLoader( pluginPath );
			
			sWarnIfNoMetaData( loader, fileName );
			
			QObject *plugin = loader->instance();

			if ( plugin == nullptr )
			{
				ccLog::Warning( tr( "\t%1 does not seem to be a valid plugin\t(%2)" ).arg( fileName, loader->errorString() ) );
				
				delete loader;
				
				continue;
			}
			
			ccPluginInterface *ccPlugin = dynamic_cast<ccPluginInterface*>(plugin);

			if ( ccPlugin == nullptr )
			{				
				ccLog::Warning( tr( "\t%1 does not seem to be a valid plugin or it is not supported by this version" ).arg( fileName ) );

				loader->unload();
				
				delete loader;
				
				continue;
			}

			if ( ccPlugin->getName().isEmpty() )
			{
				ccLog::Error( tr( "\tPlugin %1 has a blank name" ).arg( fileName ) );

				loader->unload();
				
				delete loader;

				continue;
			}
			
			QPluginLoader *previousLoader = fileNameToLoaderMap.value( fileName );
			
			// If we have already loaded a plugin with this file name, unload it and replace the interface in the plugin list
			if ( previousLoader != nullptr )
			{
				ccPluginInterface *pluginInterface = dynamic_cast<ccPluginInterface *>( previousLoader->instance() );
				
				// maintain the order of the plugin list
				const int index = m_pluginList.indexOf( pluginInterface );
				m_pluginList.replace( index, ccPlugin );
				
				previousLoader->unload();
				
				delete previousLoader;
								
				ccLog::Warning( tr( "\t%1 overridden" ).arg( fileName ) );
			}
			else
			{
				m_pluginList.push_back( ccPlugin );
			}

			fileNameToLoaderMap[fileName] = loader;
			
			ccLog::Print( tr( "\tPlugin found: %1 (%2)" ).arg( ccPlugin->getName(), fileName ) );
		}
	}
	
	for ( QPluginLoader *loader : fileNameToLoaderMap.values() )
	{
		delete loader;
	}
}