///////////////////////////////////////////////////////////////////////////////
/// @file deResource.cpp
///
/// @brief Destiny3D Resource Manager DLL Control
///
/// @author Hootie
///
/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
/// contents of this file is subject to the Destiny3D Member License which
/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
///
/// This file is distributed "AS IS" without warranty of any kind.  Novus
/// Delta, LLC. does not guarantee the fitness of the contents of this file
/// for any particular purpose.
///
/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
///
/// <hr>
///                                 Change History
/// <hr>
///
/// @date Feb 2002
/// @author Hootie
/// @remarks Level 2 Singleton DLL Wrap
///
///////////////////////////////////////////////////////////////////////////////


//=================================================================================
// Includes
//=================================================================================

#include <windows.h> // for DllMain
#include "deResource.hpp"
#include "deResourceManager.hpp"


//=================================================================================
// Local Function Prototypes
//=================================================================================

static deBoolean CreateResourceManager(void);
static deBoolean DestroyResourceManager(void);



//=================================================================================
// Global DLL Memory
//=================================================================================

static deResourceManager* GlobalResourceManager = NULL;
int deResourceProcessCount = 0;



//=================================================================================
// DllMain()
//
// Dll startup code.
//
// This is the entry point for other processes using the resource manager.  This
// function gets called whenever another program does a LoadLibrary() on this
// DLL.  This keeps a reference counter (deResourceProcessCount) of the number of
// processes currently connected to this instance of the DLL.  When this DLL is 
// first loaded, it creates the resource manager object Once the reference count
// goes to zero, the resource manager object is destroyed. 
//=================================================================================

#ifndef DESTINY3D_STATIC_LINK
BOOL WINAPI IdeResource_DllMain(HINSTANCE /*hinstDLL*/, DWORD fdwReason, LPVOID /*lpvReserved*/)
{
	if (fdwReason == DLL_PROCESS_ATTACH)
	{
		deResourceProcessCount++;

		if (deResourceProcessCount == 1)
		{
			if (!CreateResourceManager())
			{
				// error - Create failed
				return FALSE;
			}
		}
	} 


	if (fdwReason == DLL_PROCESS_DETACH)
	{
		deResourceProcessCount--;

		if (deResourceProcessCount == 0)
		{
			if (!DestroyResourceManager())
			{
				// error - Destroy failed
				return FALSE;
			}
		}
	}

	return TRUE; 
}
#endif



//=================================================================================
// CreateResourceManager()
//
// Initialize the resource manager
//=================================================================================

static deBoolean CreateResourceManager(void)
{

	if (GlobalResourceManager)
	{
		// Already exists
		return deTRUE;
	}

	GlobalResourceManager = new deResourceManager;

/*
	if (!GlobalResourceManager)
	{
		// error - Resource Manager create failed
		return deFALSE;
	}

	if (!GlobalResourceManager->GetCreationResult())
	{
		// error - Memory Manager create failed
		delete GlobalResourceManager;
		return deFALSE;
	}
*/
   return deTRUE;
}



//=================================================================================
// DestroyResourceManager()
//
// Destroy the resource manager object.
//=================================================================================

static deBoolean DestroyResourceManager(void)
{
	if (GlobalResourceManager)
		delete GlobalResourceManager;

	return deTRUE;
}



//=================================================================================
// IdeResourceManager_GetManager()
//
// This is the only exported function call in the DLL.  It is the only access 
// point for the outside world.  A call to this function will return an interface
// pointer good for an IdeResourceManager interface.  If new interfaces are 
// created, leave this alone and create another export function to feed the new
// interface objects.  That will maintain backward compatibility. 
//=================================================================================

IdeResourceManager *IdeResourceManager_GetManager()
{
	if (!GlobalResourceManager)
	{
		if (!CreateResourceManager())
			return NULL;
	}
	return GlobalResourceManager;
}


void IdeResourceManager_ShutDown()
{
	delete GlobalResourceManager;
	GlobalResourceManager = NULL;
}
