///////////////////////////////////////////////////////////////////////////////
/// @file deResourceManager.hpp
///
/// @brief Global resource handler
///
/// @author Assassin
///
/// 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 Aug 2001
/// @author Assassin
/// @remarks Creation
///
/// @date Feb 2002
/// @author Hootie
/// @remarks Level 2 Singleton DLL Wrap
///
///////////////////////////////////////////////////////////////////////////////

#ifndef DERESOURCEMANAGER_HPP
#define DERESOURCEMANAGER_HPP


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

#include "deResource.hpp"
#include "deHash.hpp"
#include "deThread.hpp"



//=================================================================================
// deResourceManager Class
//
// Resource Manager
//=================================================================================

class deResourceManager : public IdeResourceManager
{
public:
   deResourceManager(void);
   ~deResourceManager(void);

   long GetUniqueInterfaceID() { ++m_UniqueTypes; return m_UniqueTypes; }
   deBoolean RegisterResourceType(const char *extension, deResourceMakerCB maker);
   IdeResourceBase* GetResource(const char *filename, IdeFileSystem *filesystem, deBoolean DoLoad);
   IdeResourceBase* GetResourceThreaded(const char *filename, IdeFileSystem *filesystem, deBoolean DoLoad);
   IdeResourceBase* GetResourceFromFile(IdeFile* file, const char* filename, deBoolean record);
   IdeResourceBase* GetResourceFromFileThreaded(IdeFile* file, const char* filename, deBoolean record);
   deBoolean RemoveResource(IdeResourceBase * rsc);
   deBoolean RemoveAllResources(void);

private:
   deBoolean AddResource(char *extension, IdeResourceBase *ClassPtr);
   void ParseExtension(char *filename, char buffer[]);
   IdeFile* LoadFile(const char * filename, IdeFileSystem* BaseFS, deBoolean Read, deBoolean Write);

   struct deResource
   {
      IdeResourceBase *ClassPtr;
      char extension[24];
   };

   deTHashString <deResourceMakerCB>  m_RegisteredTypes;
   deTHashString <deTHashString <deResource>*>  m_Resources;
   long m_UniqueTypes;

   // threaded load-handler
   struct LoadingResource_t
   {
	   deBoolean ready;
	   deBoolean failed;
	   IdeResourceBase* rsc;
   };
   struct LoadingResourceParams_t
   {
	   deResourceManager* manager;
	   deResourceMakerCB maker;
	   IdeFile* loadfile;
	   char filename[256];
   };
   IdeThreadLock* m_GlobalLockObj;
   IdeThreadLock* m_LoadLockObj;
   deTHashString <LoadingResource_t> m_LoadingResources;
   static u32 ThreadedLoadHandler(void* param);
   void ChangeThreadedResourceStatus(LoadingResourceParams_t* params, LoadingResource_t* rsc);
};


#endif  // End DERESOURCEMANAGER_HPP


