#include <windows.h>
#include <stdio.h>
#include "kbbms_module.h"
#include "kbbms_decoder.h"

#ifdef _DEBUG
#define LEAK_CHECK //メモリリーク検出（しない場合はコメントアウト）
#endif
#ifdef LEAK_CHECK
#include <crtdbg.h>
#endif
//////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
static const DWORD kbbms_Version = 0x7FFFFFFF;//デバッグ時は優先度最大
#define KBBMS_VERSION_STR L"0x7FFFFFFF(Debug)"
#else
static const DWORD kbbms_Version = 4;
#define KBBMS_VERSION_STR L"0.04"
#endif

static const wchar_t kbbms_Description[] = L"BMS Decoder v" KBBMS_VERSION_STR " based on bmse.kpi v1.0r13";
static const wchar_t kbbms_Copyright[] =   L"kbbms.kpi Copyright(C) 2016-2019 Kobarin\n"
                                           L"bmse.kpi v1.0r13 Copyright(C) 200?-2007 Mamiya";

// {FBCCDFE8-0B2F-45EB-BE94-112A23ECCBBA}
static const GUID kbbms_GUID = 
{ 0xfbccdfe8, 0xb2f, 0x45eb, { 0xbe, 0x94, 0x11, 0x2a, 0x23, 0xec, 0xcb, 0xba } };

static const DWORD kbbms_MultipleInstance = KPI_MULTINST_INFINITE;
static const wchar_t kbbms_SupportExts[] = L".bms/.bme/.bml/.ems/.pms";
static const wchar_t kbbms_MultiSongExts[] = L"";
static const DWORD kbbms_SupportTagInfo = 0;//KPI_DECODER_MODULEINFO::TAG_EXTRA;
static const DWORD kbbms_SupportConfig = 0;
static void* kbbms_Reserved1 = NULL;
static void* kbbms_Reserved2 = NULL;

IKpiUnkProvider *g_pProvider = NULL;
HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbBmsDecoderModule::KbBmsDecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbbms_Version,
        kbbms_MultipleInstance,
        kbbms_GUID,
        kbbms_Description,
        kbbms_Copyright,
        kbbms_SupportExts,
        kbbms_MultiSongExts,
        kbbms_Reserved1,
        kbbms_Reserved2,
        kbbms_SupportTagInfo,
        kbbms_SupportConfig,
        0, 0, 0, 0                   //reserved
     }
{
    //pConfig->AddRef();
    //m_pConfig = pConfig;
}
//////////////////////////////////////////////////////////////////////////////
KbBmsDecoderModule::~KbBmsDecoderModule(void)
{
    //m_pConfig->Release();
    if(g_pProvider){
        g_pProvider->Release();
        g_pProvider = NULL;
    }
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbBmsDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbBmsDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                      IKpiFile     *pFile,   //音楽データ
                                      IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                      IKpiDecoder **ppDecoder)
{
    KbBmsDecoder *pDecoder = new KbBmsDecoder;
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
/////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbBmsDecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return FALSE;
}
/////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbBmsDecoderModule::ApplyConfig(const wchar_t *cszSection,//セクション名
                             const wchar_t *cszKey,    //キー名
                             INT64  nValue, //変更後の値(BOOL/INT の場合)
                             double dValue, //変更後の値(FLOAT の場合)
                             const wchar_t *cszValue)////変更後の値(文字列)
{
    return KPI_CFGRET_OK;//kbbms_ApplyConfig(m_pConfig, cszSection, cszKey, nValue, dValue, cszValue);
}
/////////////////////////////////////////////////////////////////////////////
HRESULT WINAPI kpi_CreateInstance(REFIID riid, void **ppvObject, IKpiUnknown *pUnknown)
{
    *ppvObject = NULL;
    if(!IsEqualIID(riid, IID_IKpiDecoderModule)){
        return E_NOINTERFACE;
    }
    if(g_pProvider){
        return E_NOINTERFACE;
    }
    //IKpiConfig *pConfig;
    //kpi_CreateConfig(pUnknown, &kbbms_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbBmsDecoderModule(NULL);
    //IKpiDecoderModule *pDecoderModule = new KbBmsDecoderModule(pConfig);
    *ppvObject = pDecoderModule;
    pUnknown->QueryInterface(IID_IKpiUnkProvider, (void**)&g_pProvider);
    //pConfig->Release();
    return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:{
        g_hKpi = hModule;
#ifdef LEAK_CHECK
        //メモリリーク検出
        int tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
        //tmpDbgFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
        tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
        //tmpDbgFlag |= _CRTDBG_CHECK_ALWAYS_DF;
        _CrtSetDbgFlag(tmpDbgFlag);
#endif
        DisableThreadLibraryCalls(hModule);
        break;
    }
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
