#include <windows.h>

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

#include "kbtta_module.h"
#include "kbtta_decoder.h"

#ifdef _DEBUG
static const DWORD kbtta_Version = 0x7FFFFFFF; //デバッグ時は優先度最大
#define KBTTA_VERSION_STR L"0x7FFFFFFF(Debug)"
#else
static const DWORD kbtta_Version = 13;
#define KBTTA_VERSION_STR L"0.13"
#endif

static const wchar_t kbtta_Description[] = L"The True Audio Decoder v" KBTTA_VERSION_STR " based on libtta-cpp 2.3";
static const wchar_t kbtta_Copyright[] =   L"kbtta.kpi Copyright (C) 2004-2019 Kobarin\n"
                                           L"libtta-cpp Copyright (C) 1999-2015 Aleksander Djuric";
// {74D73815-5372-400A-98A3-0B97DB216BE2}
static const GUID kbtta_GUID = 
{ 0x74d73815, 0x5372, 0x400a, { 0x98, 0xa3, 0xb, 0x97, 0xdb, 0x21, 0x6b, 0xe2 } };

static const DWORD kbtta_MultipleInstance = KPI_MULTINST_INFINITE;//同時に何個でも作成可能
                                            //KPI_MULTINST_ZERO;  //常に別プロセス起動
                                            //KPI_MULTINST_ONE;   //2個目以降は別プロセス起動
                                            //KPI_MULTINST_UNIQUE;//プロセスをまたいでも１つまで
                                            //KPI_MULTINST_ZERO + N;//(N+1)個目以降は別プロセス起動
                                            //↑どれか１つを選択
static const wchar_t kbtta_SupportExts[] = L".tta";
static const wchar_t kbtta_MultiSongExts[] = L"";
static const DWORD kbtta_SupportTagInfo = //KPI_DECODER_MODULEINFO::TAG_TITLE | //未対応ならコメントアウト
                                          //KPI_DECODER_MODULEINFO::TAG_EXTRA | //未対応ならコメントアウト
                                          0;
static const DWORD kbtta_SupportConfig = 0;
static void* kbtta_Reserved1 = NULL;
static void* kbtta_Reserved2 = NULL;
//////////////////////////////////////////////////////////////////////////////
KbTtaDecoderModule::KbTtaDecoderModule(void)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbtta_Version,
        kbtta_MultipleInstance,
        kbtta_GUID,
        kbtta_Description,
        kbtta_Copyright,
        kbtta_SupportExts,
        kbtta_MultiSongExts,
        kbtta_Reserved1,
        kbtta_Reserved2,
        kbtta_SupportTagInfo,
        kbtta_SupportConfig,
        0, 0, 0, 0                      //reserved
     }
{
}
//////////////////////////////////////////////////////////////////////////////
KbTtaDecoderModule::~KbTtaDecoderModule(void)
{
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbTtaDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbTtaDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
    IKpiFile     *pFile,   //音楽データ
    IKpiFolder   *pFolder, //音楽データがあるフォルダ
    IKpiDecoder **ppDecoder)
{
    KbTtaDecoder *pDecoder = new KbTtaDecoder;
    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;
    }
    KbTtaDecoderModule *pDecoderModule = new KbTtaDecoderModule();
    *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;
}
/////////////////////////////////////////////////////////////////////////////
