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

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

#include "kbssf_module.h"
#include "kbssf_decoder.h"
#include "kbssf_config.h"
#include "aossf/src/pversion.h"

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

// {568007E8-A34D-429A-8DC7-CA846C80E8EF}
static const GUID kbssf_GUID =
{ 0x568007e8, 0xa34d, 0x429a, { 0x8d, 0xc7, 0xca, 0x84, 0x6c, 0x80, 0xe8, 0xef } };


static const wchar_t kbssf_Description[] = L"Sega Saturn Sound Format(SSF) Decoder v" KBSSF_VERSION_STR " based on aossf v" XSFDRIVER_VERSIONS " / Audio Overload SDK v" AOSDK_VERSIONS;
static const wchar_t kbssf_Copyright[] = L"kbssf.kpi Copyright (C) 2019 Kobarin\n"
                                          "aossf Copyright (C) Unknown author\n"
                                          "Audio Overload SDK " AOSDK_COPYRIGHT
                                          "\n"
                                          L"zlib (C) 1995-2017 Jean-loup Gailly and Mark Adler";

//////////////////////////////////////////////////////////////////////////////
static const DWORD kbssf_MultipleInstance = KPI_MULTINST_ONE;
static const wchar_t kbssf_SupportExts[] = L".ssf/.minissf";
static const wchar_t kbssf_MultiSongExts[] = L"";
static const DWORD kbssf_SupportTagInfo = //KPI_DECODER_MODULEINFO::TAG_TITLE |
                                          //KPI_DECODER_MODULEINFO::TAG_EXTRA |
                                           0;
static const DWORD kbssf_SupportConfig = 1;
static void* kbssf_Reserved1 = NULL;
static void* kbssf_Reserved2 = NULL;
HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbSsfDecoderModule::KbSsfDecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbssf_Version,                 //dwPluginVersion
        kbssf_MultipleInstance,
        kbssf_GUID,
        kbssf_Description,
        kbssf_Copyright,
        kbssf_SupportExts,
        kbssf_MultiSongExts,
        kbssf_Reserved1,
        kbssf_Reserved2,
        kbssf_SupportTagInfo,
        kbssf_SupportConfig,
        0, 0, 0, 0                      //reserved
     }
{
    m_pConfig = pConfig;
    KbSsfDecoder::Init(pConfig);
    pConfig->AddRef();
}
//////////////////////////////////////////////////////////////////////////////
KbSsfDecoderModule::~KbSsfDecoderModule(void)
{
    KbSsfDecoder::Deinit();
    m_pConfig->Release();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbSsfDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbSsfDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                         IKpiFile     *pFile,   //音楽データ
                                         IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                         IKpiDecoder **ppDecoder)
{
    KbSsfDecoder *pDecoder = new KbSsfDecoder(m_pConfig);
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
//////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbSsfDecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return kbssf_EnumConfig(pEnumerator);
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbSsfDecoderModule::ApplyConfig(const wchar_t *cszSection,
                                             const wchar_t *cszKey,
                                             INT64 nValue,
                                             double dValue,
                                             const wchar_t *cszValue)
{
    return kbssf_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, &kbssf_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbSsfDecoderModule(pConfig);
    *ppvObject = pDecoderModule;
    pConfig->Release();//KbSsfDecoderModule 内で 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;
}
/////////////////////////////////////////////////////////////////////////////
