#include <windows.h>

#ifdef _DEBUG
#define LEAK_CHECK //メモリリーク検出（しない場合はコメントアウト）
#endif
#ifdef LEAK_CHECK
#include <crtdbg.h>
#endif

#include "kbmio_module.h"
#include "kbmio_decoder.h"

#ifdef _DEBUG
static const DWORD kbmio_Version = 0x7FFFFFFF; //デバッグ時は優先度最大
#define KBMIO_VERSION_STR L"0x7FFFFFFF(Debug)"
#else
static const DWORD kbmio_Version = 14;
#define KBMIO_VERSION_STR L"0.14"
#endif

static const wchar_t kbmio_Description[] = L"MIO Decoder v" KBMIO_VERSION_STR " based on ERISA-Library v1.07e";
static const wchar_t kbmio_Copyright[] = L"kbmio.kpi Copyright (C) 2000-2019 Kobarin\n"
                                         L"ERISA-Library Copyright (C) 2000-2004  Leshade Entis, Entis-soft.";
// {53294516-2E8E-4BE4-A47E-897079DDBB2A}
static const GUID kbmio_GUID = 
{ 0x53294516, 0x2e8e, 0x4be4, { 0xa4, 0x7e, 0x89, 0x70, 0x79, 0xdd, 0xbb, 0x2a } };

static const DWORD kbmio_MultipleInstance = KPI_MULTINST_INFINITE;//同時に何個でも作成可能
                                            //KPI_MULTINST_ZERO;  //常に別プロセス起動
                                            //KPI_MULTINST_ONE;   //2個目以降は別プロセス起動
                                            //KPI_MULTINST_UNIQUE;//プロセスをまたいでも１つまで
                                            //KPI_MULTINST_ZERO + N;//(N+1)個目以降は別プロセス起動
                                            //↑どれか１つを選択
static const wchar_t kbmio_SupportExts[] = L".mio";
static const wchar_t kbmio_MultiSongExts[] = L"";
static const DWORD kbmio_SupportTagInfo = //KPI_DECODER_MODULEINFO::TAG_TITLE | //未対応ならコメントアウト
                                          //KPI_DECODER_MODULEINFO::TAG_EXTRA | //未対応ならコメントアウト
                                          0;
static const DWORD kbmio_SupportConfig = 0;
static void* kbmio_Reserved1 = NULL;
static void* kbmio_Reserved2 = NULL;
//////////////////////////////////////////////////////////////////////////////
KbMioDecoderModule::KbMioDecoderModule(void)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbmio_Version,
        kbmio_MultipleInstance,
        kbmio_GUID,
        kbmio_Description,
        kbmio_Copyright,
        kbmio_SupportExts,
        kbmio_MultiSongExts,
        kbmio_Reserved1,
        kbmio_Reserved2,
        kbmio_SupportTagInfo,
        kbmio_SupportConfig,
        0, 0, 0, 0                      //reserved
     }
{
    eriInitializeLibrary();
}
//////////////////////////////////////////////////////////////////////////////
KbMioDecoderModule::~KbMioDecoderModule(void)
{
    eriCloseLibrary();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbMioDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbMioDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
    IKpiFile     *pFile,   //音楽データ
    IKpiFolder   *pFolder, //音楽データがあるフォルダ
    IKpiDecoder **ppDecoder)
{
    KbMioDecoder *pDecoder = new KbMioDecoder;
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
/////////////////////////////////////////////////////////////////////////////
HRESULT WINAPI kpi_CreateInstance(REFIID riid, void **ppvObject, IKpiUnknown *pUnknown)
{
    *ppvObject = NULL;
    if(!IsEqualIID(riid, IID_IKpiDecoderModule)){
        return E_NOINTERFACE;
    }
    IKpiDecoderModule *pDecoderModule = new KbMioDecoderModule();
    *ppvObject = pDecoderModule;
    return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
//DllMain
/////////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call) {
    case DLL_PROCESS_ATTACH:
    {
#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_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
