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

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

#include "kbncsf_module.h"
#include "kbncsf_decoder.h"
#include "kbncsf_config.h"

#ifdef _DEBUG
static const DWORD kbncsf_Version = 0x7FFFFFFF; //デバッグ時は優先度最大
#define KBNCSF_VERSION_STR L"0x7FFFFFFF(Debug)"
#else
static const DWORD kbncsf_Version = 1;
#define KBNCSF_VERSION_STR L"0.01"
#endif

// {93432E38-F6CA-46F7-B40C-4F6F4C1F3F90}
static const GUID kbncsf_GUID =
{ 0x93432e38, 0xf6ca, 0x46f7, { 0xb4, 0xc, 0x4f, 0x6f, 0x4c, 0x1f, 0x3f, 0x90 } };

static const wchar_t kbncsf_Description[] = L"Nitro Composer Sound Format(NCSF) Decoder v" KBNCSF_VERSION_STR " based on in_ncsf v1.11.1";
static const wchar_t kbncsf_Copyright[] = L"kbncsf.kpi Copyright (C) 2019 Kobarin\n"
                                          L"in_ncsf Copyright (C) Naram Qashat (CyberBotX)\n"
                                          L"zlib (C) 1995-2017 Jean-loup Gailly and Mark Adler";

//////////////////////////////////////////////////////////////////////////////
static const DWORD kbncsf_MultipleInstance = KPI_MULTINST_INFINITE;
static const wchar_t kbncsf_SupportExts[] = L".ncsf/.minincsf";
static const wchar_t kbncsf_MultiSongExts[] = L"";
static const DWORD kbncsf_SupportTagInfo = //KPI_DECODER_MODULEINFO::TAG_TITLE |
                                          //KPI_DECODER_MODULEINFO::TAG_EXTRA |
                                           0;
static const DWORD kbncsf_SupportConfig = 1;
static void* kbncsf_Reserved1 = NULL;
static void* kbncsf_Reserved2 = NULL;
HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbNcsfDecoderModule::KbNcsfDecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbncsf_Version,                 //dwPluginVersion
        kbncsf_MultipleInstance,
        kbncsf_GUID,
        kbncsf_Description,
        kbncsf_Copyright,
        kbncsf_SupportExts,
        kbncsf_MultiSongExts,
        kbncsf_Reserved1,
        kbncsf_Reserved2,
        kbncsf_SupportTagInfo,
        kbncsf_SupportConfig,
        0, 0, 0, 0                      //reserved
     }
{
    m_pConfig = pConfig;
    KbNcsfDecoder::Init(pConfig);
    pConfig->AddRef();
}
//////////////////////////////////////////////////////////////////////////////
KbNcsfDecoderModule::~KbNcsfDecoderModule(void)
{
    KbNcsfDecoder::Deinit();
    m_pConfig->Release();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbNcsfDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbNcsfDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                         IKpiFile     *pFile,   //音楽データ
                                         IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                         IKpiDecoder **ppDecoder)
{
    KbNcsfDecoder *pDecoder = new KbNcsfDecoder(m_pConfig);
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
//////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbNcsfDecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return kbncsf_EnumConfig(pEnumerator);
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbNcsfDecoderModule::ApplyConfig(const wchar_t *cszSection,
                                             const wchar_t *cszKey,
                                             INT64 nValue,
                                             double dValue,
                                             const wchar_t *cszValue)
{
    return kbncsf_ApplyConfig(m_pConfig, cszSection, cszKey, nValue, dValue, cszValue);
}
/////////////////////////////////////////////////////////////////////////////
HRESULT WINAPI kpi_CreateInstance(REFIID riid, void **ppvObject, IKpiUnknown *pUnknown)
{
    *ppvObject = NULL;
    if(!IsEqualIID(riid, IID_IKpiDecoderModule)){
        return E_NOINTERFACE;
    }
    IKpiConfig *pConfig = NULL;
    kpi_CreateConfig(pUnknown, &kbncsf_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbNcsfDecoderModule(pConfig);
    *ppvObject = pDecoderModule;
    pConfig->Release();//KbNcsfDecoderModule 内で AddRef しているので必ず必要
    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
        g_hKpi = hModule;
        DisableThreadLibraryCalls(hModule);
        break;
    }
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
