#include <windows.h>

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

#include "kbvorbis_module.h"
#include "kbvorbis_decoder.h"

#ifdef _DEBUG
static const DWORD kbvorbis_Version = 0x7FFFFFFF; //デバッグ時は優先度最大
#define KBVORBIS_VERSION_STR L"0x7FFFFFFF(Debug)"
#else
static const DWORD kbvorbis_Version = 135;
#define KBVORBIS_VERSION_STR L"1.35"
#endif

static const wchar_t kbvorbis_Description[] = L"Ogg Vorbis Decoder v" KBVORBIS_VERSION_STR " based on libvorbis 1.3.7/libogg 1.3.5";
static const wchar_t kbvorbis_Copyright[] = L"kbvobis.kpi Copyright (C) 2000-2021 Kobarin\n"
                                            L"libvorbis Copyright (C) 2000-2020 Monty, Xiph.Org\n"
                                            L"libogg Copyright (C) 2000-2021 Xiph.Org";
// {55D6C73F-43F2-40A2-A472-8932C07EB1B7}
static const GUID kbvorbis_GUID = 
{ 0x55d6c73f, 0x43f2, 0x40a2, { 0xa4, 0x72, 0x89, 0x32, 0xc0, 0x7e, 0xb1, 0xb7 } };

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