#include <windows.h>
#include <stdio.h>
#include "kbqcp_module.h"
#include "kbqcp_decoder.h"

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

static const wchar_t kbqcp_Description[] = L"Qualcomm PureVoice Decoder v" KBQCP_VERSION_STR " based on PureVoice SDK 3.5";
static const wchar_t kbqcp_Copyright[] =   L"kbqcp.kpi Copyright (C) 2003-2019 Kobarin\n"
                                           L"qscl.dll Copyright (C) 2003-2008 QUALCOMM Inc";

// {C2B4F34D-3F52-467C-BD74-5EC798B6A340}
static const GUID kbqcp_GUID = 
{ 0xc2b4f34d, 0x3f52, 0x467c, { 0xbd, 0x74, 0x5e, 0xc7, 0x98, 0xb6, 0xa3, 0x40 } };

static const DWORD kbqcp_MultipleInstance = KPI_MULTINST_INFINITE;   //同時に何個でも作成可能
                                            //KPI_MULTINST_ZERO;     //常に別プロセス起動
                                            //KPI_MULTINST_ONE;      //2個目以降は別プロセス起動
                                            //KPI_MULTINST_UNIQUE;   //プロセスをまたいでも１つまで
                                            //KPI_MULTINST_ZERO + N; //(N+1)個目以降は別プロセス起動
                                            //↑どれか1つを選択
static const wchar_t kbqcp_SupportExts[] = L".qcp";
static const wchar_t kbqcp_MultiSongExts[] = L"";//SupportExts に含まないものを指定しても無視される
static const DWORD kbqcp_SupportTagInfo = KPI_DECODER_MODULEINFO::TAG_TITLE | //未対応ならコメントアウト
                                          //KPI_DECODER_MODULEINFO::TAG_EXTRA | //未対応ならコメントアウト
                                          0;
static const DWORD kbqcp_SupportConfig = 0;
static void* kbqcp_Reserved1 = NULL;
static void* kbqcp_Reserved2 = NULL;

HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbQcpDecoderModule::KbQcpDecoderModule(void) //IKpiConfig 未対応なら KbQcpDecoderModle(void)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbqcp_Version,
        kbqcp_MultipleInstance,
        kbqcp_GUID,
        kbqcp_Description,
        kbqcp_Copyright,
        kbqcp_SupportExts,
        kbqcp_MultiSongExts,
        kbqcp_Reserved1,
        kbqcp_Reserved2,
        kbqcp_SupportTagInfo,
        kbqcp_SupportConfig,
        0, 0, 0, 0                   //reserved
     }
{
}
//////////////////////////////////////////////////////////////////////////////
KbQcpDecoderModule::~KbQcpDecoderModule(void)
{
    KbQcpDecoder::Uninitialize();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbQcpDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbQcpDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                      IKpiFile     *pFile,   //音楽データ
                                      IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                      IKpiDecoder **ppDecoder)
{
    KbQcpDecoder *pDecoder = new KbQcpDecoder;
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
/////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbQcpDecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return FALSE;
}
/////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbQcpDecoderModule::ApplyConfig(const wchar_t *cszSection,//セクション名
                             const wchar_t *cszKey,    //キー名
                             INT64  nValue, //変更後の値(BOOL/INT の場合)
                             double dValue, //変更後の値(FLOAT の場合)
                             const wchar_t *cszValue)////変更後の値(文字列)
{
    return KPI_CFGRET_OK;
}
/////////////////////////////////////////////////////////////////////////////
HRESULT WINAPI kpi_CreateInstance(REFIID riid, void **ppvObject, IKpiUnknown *pUnknown)
{
    *ppvObject = NULL;
    if(!IsEqualIID(riid, IID_IKpiDecoderModule)){
        return E_NOINTERFACE;
    }
    IKpiDecoderModule *pDecoderModule = new KbQcpDecoderModule;
    *ppvObject = pDecoderModule;
    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;
}
