// Rp3DLoader.h : Declaration of the CRp3DLoader
// CRp3DLoader represents the plugin. We implement here the
// IRsUnknownPackage interface that is asked during the plugin loading.
// The interface then enable communication with system and all other
// packages.

#pragma once

#define LOADER3D_PACKAGE_VERSION1			1	// package version
#define LOADER3D_PACKAGE_VERSION			LOADER3D_PACKAGE_VERSION1  

[
	object,
	helpstring("IRp3DLoader Interface"),
	uuid("42E4CCB9-80D8-4a8d-B352-D5C4857F4950"),
	pointer_default(unique)
]
__interface IRp3DLoader : public IUnknown
{
public:
};



[
	ATTRIBUTE_COCLASS,
	threading(both),
	vi_progid("Rs3DLoader.Rp3DLoader"),
	progid("Rs3DLoader.Rp3DLoader.1"),
	version(1.0),
	uuid("626B33C9-973B-42c1-96E3-9999E0A50426"),
	helpstring("Rp3DLoader Class")
]
class ATL_NO_VTABLE CRp3DLoader: 
	public IRp3DLoader,
    public IRsUnknownPackage,
    public IRsModulePackage,
	public IRsPackageDesc
{
public:
    CRp3DLoader()
    {
    }

    HRESULT FinalConstruct()
    {
        return S_OK;
    }

    void FinalRelease() 
    {
    }

    BEGIN_COM_MAP(CRp3DLoader)
        COM_INTERFACE_ENTRY(IRp3DLoader)
        COM_INTERFACE_ENTRY2(IRsObject, IRsUnknownPackage)
        COM_INTERFACE_ENTRY(IRsUnknownPackage)
        COM_INTERFACE_ENTRY(IRsModulePackage)
    END_COM_MAP()

    DECLARE_CLASSFACTORY_SINGLETON(CRp3DLoader)
    DECLARE_NOT_AGGREGATABLE(CRp3DLoader)
    DECLARE_PROTECT_FINAL_CONSTRUCT()

    // IRsUnknownPackage
    STDMETHOD(Initialize)(IRsUnknownPackage *pParentPackage);           // Called to initialize this package
    STDMETHOD(UnInitialize)();                                          // This method releases m_pKernelPackage.
    STDMETHOD(OnSysMessage)(MSGID msgId, IRsUnknownMsg* pMsg);          // package messages handler
    STDMETHOD(GetNodeFactory)(IRsNodeFactory** ppNodeFactory);          // returns NodeFactory interface provided by package
    STDMETHOD(GetCmdFactory)(IRsCmdFactory** ppCmdFactory);             // returns Command Factory interface provided by package
    STDMETHOD(GetDataObjFactory)(IRsDataObjFactory** ppDataObjFactory); // returns Data Object Factory interface provided by package
    STDMETHOD(GetModuleInstance)(RtInstance* pInstance);                // returns Package module instance
    STDMETHOD(GetResourceInstance)(RtInstance* pInstance);              // returns Package resource instance

    // IRsObject Methods
    RS_DECLARE_IRS_OBJECT(CRp3DLoader, L"3DLoader Package");

    // package version
    RS_DECLARE_PACKAGE_VERSION(PRODUCT_VERSION, LOADER3D_PACKAGE_VERSION);
	RS_PACKAGE_DESCRIPTION(L"3d library sdk example");

public:
    // Help functions
	static HRESULT GetKernelPackage(IRpKernel** ppPackage); // Returns kernel interface
    static HRESULT CreateDataObj(const IID* piid, IRsDataObject** ppDataObj);
    static HRESULT CreateCmdObj(const IID *piid, IRsNode **ppCmd);
    static HRESULT CreateNode(const IID *piid, IRsNode **ppNode);
    static HRESULT ExecuteCommand(IRsNode *pCmdNode);   // Call to synchronous execution of commands
	static HRESULT SendCommand(IRsNode *pCmdNode);      // Send command to commands' queue
    static HRESULT Create3DLoaderFnSet(IRf3DLoader **ppFnSet);

    // Log support
    RS_LOG_ACCESS(m_spKernelPackage)
    #define RS_LOG(lev, hr, text) CRp3DLoader::MyLogEntry(lev, hr, text, __FILE__, __LINE__);   


private:
    // Pointer to kernel Package.
    static CComPtr<IRpKernel>			m_spKernelPackage;
    // Pointer to sys factory for creating rs objects
    static CComPtr<IRsSysFactory>		m_spSysFactory;
    // Pointer to sys core for synchronous command processing
    static CComPtr<IRsSysCore>			m_spSysCore;

    // Pointer to 3D loader Package.
    static CComPtr<IRp3DLoader> m_spSamplePackage;

    // node factory
    CComPtr<IRsNodeFactory>				m_spNodeFactory;
    // command factory
    CComPtr<IRsCmdFactory>				m_spCmdFactory;
    // command factory
    CComPtr<IRsDataObjFactory>			m_spDataObjFactory;
};


