#include <windows.h>
#include <wchar.h>

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

#include "kbopus_module.h"
#include "kbopus_decoder.h"

#ifdef _DEBUG
static const DWORD kbopus_Version = 0x7FFFFFFF; //デバッグ時は優先度最大
#define KBOPUS_VERSION_STR L"0x7FFFFFFF(Debug)" 
#else
static const DWORD kbopus_Version = 15;
#define KBOPUS_VERSION_STR L"0.15"
#endif

// {181DE1FF-81BB-41FB-9B3F-0B5286203B04}
static const GUID kbopus_GUID = 
{ 0x181de1ff, 0x81bb, 0x41fb, { 0x9b, 0x3f, 0xb, 0x52, 0x86, 0x20, 0x3b, 0x4 } };

static const wchar_t kbopus_Description[] = L"Ogg Opus Decoder v" KBOPUS_VERSION_STR " based on libopus 1.5.2/opusfile 0.12/libogg 1.3.5";
static const wchar_t kbopus_Copyright[] = L"kbopus.kpi Copyright (C) 2016-2024 Kobarin\n"
                                          L"libopus Coypright (C) 2016-2024 IETF\n"
                                          L"libogg Copyright (C) 2000-2021 Xiph.Org";
//////////////////////////////////////////////////////////////////////////////
static const DWORD kbopus_MultipleInstance = KPI_MULTINST_INFINITE;
static const wchar_t kbopus_ppszSupportExts[] = L".opus";
static const wchar_t kbopus_ppszMultiSongExts[] = L"";
static const DWORD kbopus_SupportTagInfo = KPI_DECODER_MODULEINFO::TAG_TITLE |
                                           KPI_DECODER_MODULEINFO::TAG_EXTRA |
                                           0;
static const DWORD kbopus_SupportConfig = 0;
static void* kbopus_Reserved1 = NULL;
static void* kbopus_Reserved2 = NULL;
//////////////////////////////////////////////////////////////////////////////
KbOpusDecoderModule::KbOpusDecoderModule(void)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbopus_Version,                 //dwPluginVersion
        kbopus_MultipleInstance,
        kbopus_GUID,
        kbopus_Description,
        kbopus_Copyright,
        kbopus_ppszSupportExts,
        kbopus_ppszMultiSongExts,
        kbopus_Reserved1,
        kbopus_Reserved2,
        kbopus_SupportTagInfo,
        kbopus_SupportConfig,
        0, 0, 0, 0                      //reserved
     }
{
}
//////////////////////////////////////////////////////////////////////////////
KbOpusDecoderModule::~KbOpusDecoderModule(void)
{
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbOpusDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbOpusDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                         IKpiFile     *pFile,   //音楽データ
                                         IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                         IKpiDecoder **ppDecoder)
{
    KbOpusDecoder *pDecoder = new KbOpusDecoder;
    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 KbOpusDecoderModule();
    *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;
}
/////////////////////////////////////////////////////////////////////////////
