#include <windows.h>
#include <stdio.h>
#include "kbmsxplug_module.h"
#include "kbmsxplug_decoder.h"
#include "kbmsxplug_config.h"

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

static const wchar_t kbmsxplug_Description[] = L"MSXplug Decoder v" KBMSXPLUG_VERSION_STR " based on MSXplug v1.9.1";
static const wchar_t kbmsxplug_Copyright[] =   L"kbmsxplug.kpi Copyright (C) 2016-2024 Kobarin\r\n"
                                               L"MSXplug Copyright (C) 2016-2022 Mitsutaka Okazaki";

// {02111CF3-3031-4C10-B7D7-FC48A2C4D623}
static const GUID kbmsxplug_GUID = 
{ 0x2111cf3, 0x3031, 0x4c10, { 0xb7, 0xd7, 0xfc, 0x48, 0xa2, 0xc4, 0xd6, 0x23 } };

static const DWORD kbmsxplug_MultipleInstance = KPI_MULTINST_ONE;
static const wchar_t kbmsxplug_SupportExts[] = L".kss/.mbm/.mgs/.bgm/.bgr/.opx/.mpk";
static const wchar_t kbmsxplug_MultiSongExts[] = L".kss";
static const DWORD kbmsxplug_SupportTagInfo = KPI_DECODER_MODULEINFO::TAG_TITLE |
                                              KPI_DECODER_MODULEINFO::TAG_EXTRA |
                                              0;
static const DWORD kbmsxplug_SupportConfig = 1;
static void* kbmsxplug_Reserved1 = NULL;
static void* kbmsxplug_Reserved2 = NULL;

HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbMsxplugDecoderModule::KbMsxplugDecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbmsxplug_Version,
        kbmsxplug_MultipleInstance,
        kbmsxplug_GUID,
        kbmsxplug_Description,
        kbmsxplug_Copyright,
        kbmsxplug_SupportExts,
        kbmsxplug_MultiSongExts,
        kbmsxplug_Reserved1,
        kbmsxplug_Reserved2,
        kbmsxplug_SupportTagInfo,
        kbmsxplug_SupportConfig,
        0, 0, 0, 0                   //reserved
     }
{
    pConfig->AddRef();
    m_pConfig = pConfig;
}
//////////////////////////////////////////////////////////////////////////////
KbMsxplugDecoderModule::~KbMsxplugDecoderModule(void)
{
    kbmsxplug_DeleteDlg(m_pConfig);
    m_pConfig->Release();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbMsxplugDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    kbmsxplug_GetSupportExts(m_pConfig, m_szSupportExts, _countof(m_szSupportExts));
    m_ModuleInfo.cszSupportExts = m_szSupportExts;
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbMsxplugDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                      IKpiFile     *pFile,   //音楽データ
                                      IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                      IKpiDecoder **ppDecoder)
{
    KbMsxplugDecoder *pDecoder = new KbMsxplugDecoder(m_pConfig);
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder, m_pConfig);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
/////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbMsxplugDecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return kbmsxplug_EnumConfig(pEnumerator);
}
/////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbMsxplugDecoderModule::ApplyConfig(const wchar_t *cszSection,//セクション名
                             const wchar_t *cszKey,    //キー名
                             INT64  nValue, //変更後の値(BOOL/INT の場合)
                             double dValue, //変更後の値(FLOAT の場合)
                             const wchar_t *cszValue)////変更後の値(文字列)
{
    return kbmsxplug_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;
    kpi_CreateConfig(pUnknown, &kbmsxplug_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbMsxplugDecoderModule(pConfig);
    *ppvObject = pDecoderModule;
    pConfig->Release();
    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;
}
