Пример #1
0
ModuleReturn ModuleManager::LoadModule(const Anope::string &modname, User *u)
{
	if (modname.empty())
		return MOD_ERR_PARAMS;

	if (FindModule(modname))
		return MOD_ERR_EXISTS;

	Log(LOG_DEBUG) << "trying to load [" << modname <<  "]";

	/* Generate the filename for the temporary copy of the module */
	Anope::string pbuf = services_dir + "/modules/runtime/" + modname + ".so.XXXXXX";

	/* Don't skip return value checking! -GD */
	ModuleReturn ret = moduleCopyFile(modname, pbuf);
	if (ret != MOD_ERR_OK)
		return ret;

	dlerror();
	void *handle = dlopen(pbuf.c_str(), RTLD_LAZY);
	const char *err = dlerror();
	if (!handle && err && *err)
	{
		Log() << err;
		return MOD_ERR_NOLOAD;
	}

	dlerror();
	Module *(*func)(const Anope::string &, const Anope::string &) = function_cast<Module *(*)(const Anope::string &, const Anope::string &)>(dlsym(handle, "AnopeInit"));
	err = dlerror();
	if (!func && err && *err)
	{
		Log() << "No init function found, not an Anope module";
		dlclose(handle);
		return MOD_ERR_NOLOAD;
	}

	if (!func)
		throw CoreException("Couldn't find constructor, yet moderror wasn't set?");

	/* Create module. */
	Anope::string nick;
	if (u)
		nick = u->nick;

	Module *m;

	try
	{
		m = func(modname, nick);
	}
	catch (const ModuleException &ex)
	{
		Log() << "Error while loading " << modname << ": " << ex.GetReason();
		return MOD_ERR_EXCEPTION;
	}

	m->filename = pbuf;
	m->handle = handle;

	Version v = m->GetVersion();
	if (v.GetMajor() < Anope::VersionMajor() || (v.GetMajor() == Anope::VersionMajor() && v.GetMinor() < Anope::VersionMinor()))
	{
		Log() << "Module " << modname << " is compiled against an older version of Anope " << v.GetMajor() << "." << v.GetMinor() << ", this is " << Anope::VersionMajor() << "." << Anope::VersionMinor();
		DeleteModule(m);
		return MOD_ERR_VERSION;
	}
	else if (v.GetMajor() > Anope::VersionMajor() || (v.GetMajor() == Anope::VersionMajor() && v.GetMinor() > Anope::VersionMinor()))
	{
		Log() << "Module " << modname << " is compiled against a newer version of Anope " << v.GetMajor() << "." << v.GetMinor() << ", this is " << Anope::VersionMajor() << "." << Anope::VersionMinor();
		DeleteModule(m);
		return MOD_ERR_VERSION;
	}
	else if (v.GetBuild() < Anope::VersionBuild())
		Log() << "Module " << modname << " is compiled against an older revision of Anope " << v.GetBuild() << ", this is " << Anope::VersionBuild();
	else if (v.GetBuild() > Anope::VersionBuild())
		Log() << "Module " << modname << " is compiled against a newer revision of Anope " << v.GetBuild() << ", this is " << Anope::VersionBuild();
	else if (v.GetBuild() == Anope::VersionBuild())
		Log(LOG_DEBUG) << "Module " << modname << " compiled against current version of Anope " << v.GetBuild();

	if (m->type == PROTOCOL && ModuleManager::FindFirstOf(PROTOCOL) != m)
	{
		DeleteModule(m);
		Log() << "You cannot load two protocol modules";
		return MOD_ERR_UNKNOWN;
	}

	FOREACH_MOD(I_OnModuleLoad, OnModuleLoad(u, m));

	return MOD_ERR_OK;
}
	Bool OpenGLGraphicDeviceLinux::OpenVersion( const RenderWindow & p_RenderOutput,
												const Version & p_Version )
	{

	    // Make sure the graphic device is not already open
		if( m_Open )
		{
			std::cout << "[OpenGLGraphicDeviceLinux::Open] Already open.\n";
			return false;
		}

        // Make sure the window is open
		if( !p_RenderOutput.IsOpen( ) )
		{
			std::cout << "[OpenGLGraphicDeviceLinux::Open] The window is not open.\n";
			return false;
		}



		// Get the Linux window by casting the Window class
	/*	const WindowLinux * pWindow = reinterpret_cast<const WindowLinux *>( &p_Window );
		m_pWindowLinux = const_cast<WindowLinux *>( pWindow );
*/
        // Get the window and display devices
        ::Display * pDisplay    = p_RenderOutput.GetDisplayDevice( );
		::Window window         = p_RenderOutput.GetWindowDevice( );
		int screen              = p_RenderOutput.GetScreenDevice( );

        // Error check the window and display devices.
		if( pDisplay == NULL )
		{
            std::cout << "[OpenGLGraphicDeviceLinux::Open] The display from the renderOutput is NULL.\n";
			return false;
		}
		if( window == 0 )
		{
            std::cout << "[OpenGLGraphicDeviceLinux::Open] The window from the renderOutput is 0.\n";
			return false;
		}

		// Create the visual information
		GLint Att[] =
		{
		    GLX_RGBA, GLX_DOUBLEBUFFER,
		    GLX_RED_SIZE, 8,
		    GLX_GREEN_SIZE, 8,
		    GLX_BLUE_SIZE, 8,
            GLX_DEPTH_SIZE, 16,
            GLX_STENCIL_SIZE, 8,
            0L, 0L
        };

        int fbElements = 0;
        ::GLXFBConfig *fbc = glXChooseFBConfig( pDisplay, screen, 0, &fbElements);

		::XVisualInfo * pVisualInfo = NULL;
        if( (pVisualInfo = glXChooseVisual( pDisplay, 0, Att ) ) == NULL )
		{
		    std::cout << "[OpenGLGraphicDeviceLinux::Open] Can not choose visual information.\n";
			return false;
		}

		// Create the color map and set it
		if( !(m_Colormap = XCreateColormap( pDisplay, RootWindow( pDisplay, screen), pVisualInfo->visual, AllocNone ) ) )
		{
		    std::cout << "[OpenGLGraphicDeviceLinux::Open] Can not create the colormap.\n";
            XFree( pVisualInfo );
            return false;
		}

        // Set the new color map
        XSetWindowColormap( pDisplay, window, m_Colormap );

        // Create a temporary context.
        ::GLXContext temporaryContext = glXCreateContext( pDisplay, pVisualInfo, NULL, GL_TRUE );
        if( !temporaryContext )
        {
            std::cout << "[OpenGLGraphicDeviceLinux::Open] Can not create a regual OpenGL context.\n";
            XFree( pVisualInfo );
            return false;
        }

        // Make the temporary context to the current one.
        glXMakeCurrent( pDisplay, window, temporaryContext );

        // Clear the visual info since we are done with it.
        XFree( pVisualInfo );


        // Attributes for the OGL 3.3 context
		int attribs[ ] =
		{
			GLX_CONTEXT_MAJOR_VERSION_ARB, static_cast<int>( p_Version.GetMajor( ) ),
			GLX_CONTEXT_MINOR_VERSION_ARB, static_cast<int>( p_Version.GetMinor( ) ),
			0
		};


		// We need the proc address for the function
		// we are going to use for OGL 3.3 context creation.
		GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = 0;

		if( ( glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" )) == NULL )
		{
			std::cout << "[OpenGLGraphicDeviceLinux::Open] Can not get the function for creating the context.\n";
			return false;
		}

		// Get the old error X11 error handler and set a new one in order to track if the opengl context is valid.
		g_XLibErrorCatched = false;
        XErrorHandler oldErrorHandler = XSetErrorHandler( XLibErrorCatcher );

        // Create the context
		if( ( m_DeviceContext = glXCreateContextAttribsARB( pDisplay, *fbc, 0, true, attribs ) ) != NULL )
		{
			// Delete the old temporary context
            glXDestroyContext( pDisplay, temporaryContext );

			// Make the new OpenGL context to the current one.
			glXMakeCurrent( pDisplay, window, m_DeviceContext );
		}

		// Reset the old X11 error handler
        XSetErrorHandler( oldErrorHandler );

        // Return false if we catched any error, or else the context is fine.
		if( g_XLibErrorCatched )
		{
            return false;
		}




	/*	// Filling the pixel fromat structure.
		static PIXELFORMATDESCRIPTOR PFD = {
			sizeof(PIXELFORMATDESCRIPTOR),
			1,
			PFD_DRAW_TO_WINDOW |
			PFD_SUPPORT_OPENGL |
			PFD_DOUBLEBUFFER,
			PFD_TYPE_RGBA,
			24,
			0, 0, 0, 0, 0, 0,
			0,
			0,
			0,
			0, 0, 0, 0,
			16, //DepthBits,
			8, //StencilBits,
			0,
			PFD_MAIN_PLANE,
			0,
			0, 0, 0
		};

		// Choose and set the pixel format
		GLuint PixelFormat;

		if((PixelFormat = ChoosePixelFormat( p_RenderOutput.GetDeviceContextHandle( ), &PFD ) ) == 0)
		{
			std::cout<< "[OpenGLGraphicDeviceWin32::Open] Can not choose pixel format.\n";
			return false;
		}
		if( ( SetPixelFormat( p_RenderOutput.GetDeviceContextHandle( ), PixelFormat, &PFD ) ) == false )
		{
			std::cout<<  "[OpenGLGraphicDeviceWin32::Open] Can not set pixel format.\n";
			return false;
		}

		// Create a temporary regual context.
		// We need this context to create any other context.
		HGLRC temporaryContext = wglCreateContext( p_RenderOutput.GetDeviceContextHandle( ) );

		if( temporaryContext == NULL )
		{
			std::cout << "[OpenGLGraphicDeviceWin32::Open] Can not create a regular OpenGL context.\n";
			return false;
		}

		// Make the temporary context to the current one
		wglMakeCurrent( NULL, NULL );
		wglMakeCurrent( p_RenderOutput.GetDeviceContextHandle( ), temporaryContext );

		// Attributes for the OGL 3.3 context
		int attribs[ ] =
		{
			WGL_CONTEXT_MAJOR_VERSION_ARB, static_cast<int>( p_Version.GetMajor( ) ),
			WGL_CONTEXT_MINOR_VERSION_ARB, static_cast<int>( p_Version.GetMinor( ) ),
			0
		};

		// We need the proc address for the function
		// we are going to use for OGL 3.3 context creation.
		PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
		if((wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress( "wglCreateContextAttribsARB" )) == NULL )
		{
			std::cout << "[OpenGLGraphicDeviceWin32::Open] Can not get the function for creating the context.\n";
			return false;
		}

		// Create the context
		if( ( m_Context = wglCreateContextAttribsARB( p_RenderOutput.GetDeviceContextHandle( ), 0, attribs) ) != NULL )
		{
			// Delete the old temporary context
			wglMakeCurrent( NULL, NULL );
			wglDeleteContext( temporaryContext );

			// Make the new OpenGL context to the current one.
			wglMakeCurrent( p_RenderOutput.GetDeviceContextHandle( ), m_Context );
		}
*/
		return true;
	}