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

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

#include "kbpsf2_module.h"
#include "kbpsf2_decoder.h"
#include "kbpsf2_config.h"
#include "viopsf2/pversion.h"

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

// {093ECB65-FE3F-490D-8B19-0B006C482C57}
static const GUID kbpsf2_GUID =
{ 0x93ecb65, 0xfe3f, 0x490d, { 0x8b, 0x19, 0xb, 0x0, 0x6c, 0x48, 0x2c, 0x57 } };

static const wchar_t kbpsf2_Description[] = L"PlayStation2 Sound Format(PSF2) Decoder v" KBPSF2_VERSION_STR " based on viopsf2 v" XSFDRIVER_VERSIONS " / Play! " CORE_VERSIONS;
static const wchar_t kbpsf2_Copyright[] = L"kbpsf2.kpi Copyright (C) 2016-2024 Kobarin\n"
                                          L"viopsf2 Copyright (C) 2015-2017 Unknown Japanese author\n"
                                          L"\n"
                                          L"Play!\n"
                                          CORE_COPYRIGHT
                                          L"\n"
                                          ADDITIONAL_LIBRARY_COPYRIGHT;
//////////////////////////////////////////////////////////////////////////////
static const DWORD kbpsf2_MultipleInstance = KPI_MULTINST_ONE;
static const wchar_t kbpsf2_SupportExts[] = L".psf2/.minipsf2";
static const wchar_t kbpsf2_MultiSongExts[] = L"";
static const DWORD kbpsf2_SupportTagInfo = //KPI_DECODER_MODULEINFO::TAG_TITLE |
                                          //KPI_DECODER_MODULEINFO::TAG_EXTRA |
                                           0;
static const DWORD kbpsf2_SupportConfig = 1;
static void* kbpsf2_Reserved1 = NULL;
static void* kbpsf2_Reserved2 = NULL;
HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbPsf2DecoderModule::KbPsf2DecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbpsf2_Version,                 //dwPluginVersion
        kbpsf2_MultipleInstance,
        kbpsf2_GUID,
        kbpsf2_Description,
        kbpsf2_Copyright,
        kbpsf2_SupportExts,
        kbpsf2_MultiSongExts,
        kbpsf2_Reserved1,
        kbpsf2_Reserved2,
        kbpsf2_SupportTagInfo,
        kbpsf2_SupportConfig,
        0, 0, 0, 0                      //reserved
     }
{
    m_pConfig = pConfig;
    pConfig->AddRef();
}
//////////////////////////////////////////////////////////////////////////////
KbPsf2DecoderModule::~KbPsf2DecoderModule(void)
{
    m_pConfig->Release();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbPsf2DecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbPsf2DecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                         IKpiFile     *pFile,   //音楽データ
                                         IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                         IKpiDecoder **ppDecoder)
{
    KbPsf2Decoder *pDecoder = new KbPsf2Decoder(m_pConfig);
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
//////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbPsf2DecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return kbpsf2_EnumConfig(pEnumerator);
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbPsf2DecoderModule::ApplyConfig(const wchar_t *cszSection,
                                             const wchar_t *cszKey,
                                             INT64 nValue,
                                             double dValue,
                                             const wchar_t *cszValue)
{
    return kbpsf2_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, &kbpsf2_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbPsf2DecoderModule(pConfig);
    *ppvObject = pDecoderModule;
    pConfig->Release();//KbPsf2DecoderModule 内で 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);
        //_CrtSetBreakAlloc(565);
        //_CrtSetBreakAlloc(971950);
#endif
        g_hKpi = hModule;
        DisableThreadLibraryCalls(hModule);
        break;
    }
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
