Ver código fonte

Refactor filename extension handling in DynLibManager

The code that adds the correct extension for a dynamic library is
unsightly, broken (it tries to substr() a string without checking its
length; also, it doesn't build on Not-Windows) and duplicated.

This commit refactors this bit to address those issues.

Theoretical behavior modification: now, only DynLibManager tries to add
an extension to form a valid filename; DynLib assumes the given string
is a path.  In practice, only DynLibManager uses DynLib, so we're good,
I guess.
Marc Legendre 9 anos atrás
pai
commit
25f10b3653

+ 4 - 1
Source/BansheeUtility/Include/BsDynLib.h

@@ -39,6 +39,9 @@ namespace BansheeEngine
 	class BS_UTILITY_EXPORT DynLib
     {
     public:
+		/** System-specific file extension for a dynamic library (e.g. "dll"). */
+		static const String extension;
+
 		/** Constructs the dynamic library object and loads the library with the specified name. */
 		DynLib(const String& name);
         ~DynLib();
@@ -72,4 +75,4 @@ namespace BansheeEngine
     };
 
 	/** @} */
-}
+}

+ 12 - 16
Source/BansheeUtility/Source/BsDynLib.cpp

@@ -18,6 +18,17 @@
 
 namespace BansheeEngine 
 {
+
+#if BS_PLATFORM == BS_PLATFORM_LINUX
+	const String DynLib::extension = "so";
+#elif BS_PLATFORM == BS_PLATFORM_OSX
+	const String DynLib::extension = "dylib";
+#elif BS_PLATFORM == BS_PLATFORM_WIN32
+	const String DynLib::extension = "dll";
+#else
+#  error Unhandled platform
+#endif
+
     DynLib::DynLib(const String& name)
     {
         mName = name;
@@ -35,22 +46,7 @@ namespace BansheeEngine
 		if(m_hInst)
 			return;
 
-		String name = mName;
-#if BS_PLATFORM == BS_PLATFORM_LINUX
-        // dlopen() does not add .so to the filename, like windows does for .dll
-        if (name.substr(name.length() - 3, 3) != ".so")
-           name += ".so";
-#elif BS_PLATFORM == BS_PLATFORM_OSX
-        // dlopen() does not add .dylib to the filename, like windows does for .dll
-        if (name.substr(name.length() - 6, 6) != ".dylib")
-			name += ".dylib";
-#elif BS_PLATFORM == BS_PLATFORM_WIN32
-		// Although LoadLibraryEx will add .dll itself when you only specify the library name,
-		// if you include a relative path then it does not. So, add it to be sure.
-		if (name.substr(name.length() - 4, 4) != ".dll")
-			name += ".dll";
-#endif
-        m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD(name.c_str());
+        m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD(mName.c_str());
 
         if(!m_hInst)
 		{

+ 6 - 12
Source/BansheeUtility/Source/BsDynLibManager.cpp

@@ -11,19 +11,13 @@ namespace BansheeEngine
 
     DynLib* DynLibManager::load(const String& name)
     {
+		// Add the extension (.dll, .so, ...) if necessary.
 		String filename = name;
-#if BS_PLATFORM == BS_PLATFORM_LINUX
-		if (name.substr(name.length() - 3, 3) != ".so")
-			name += ".so";
-#elif BS_PLATFORM == BS_PLATFORM_OSX
-		if (name.substr(name.length() - 6, 6) != ".dylib")
-			name += ".dylib";
-#elif BS_PLATFORM == BS_PLATFORM_WIN32
-		// Although LoadLibraryEx will add .dll itself when you only specify the library name,
-		// if you include a relative path then it does not. So, add it to be sure.
-		if (filename.substr(filename.length() - 4, 4) != ".dll")
-			filename += ".dll";
-#endif
+		const int length = filename.length();
+		const String extension = "." + DynLib::extension;
+		const int ext_length = extension.length();
+		if (length <= ext_length || filename.substr(length - ext_length) != extension)
+			filename += extension;
 
 		auto iterFind = mLoadedLibraries.find(filename);
 		if (iterFind != mLoadedLibraries.end())