#include <windows.h>
#include <stdio.h>
#include "kbs98_module.h"
#include "kbs98_decoder.h"
#include "kbs98_config.h"

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

static const wchar_t kbs98_Description[] = L"S98 Decoder v" KBS98_VERSION_STR " based on m_s98.kpi v1.0r10(061019)";
static const wchar_t kbs98_Copyright[] =   L"kbs98.kpi Copyright(C) 2016-2022 Kobarin\n"
                                           L"m_s98.kpi Copyright(C) 200?-2006 Mamiya, RuRuRu\n"
                                           L"FM Sound Generator rel.008  Copyright(C) 1998-2003 cisc\n"
                                           L"emu2413 Copyright(C)2001-2022 by Mitsutaka Okazaki";

// {CDD2C15B-71BC-45D1-90E9-076763ADC693}
static const GUID kbs98_GUID = 
{ 0xcdd2c15b, 0x71bc, 0x45d1, { 0x90, 0xe9, 0x7, 0x67, 0x63, 0xad, 0xc6, 0x93 } };

static const DWORD kbs98_MultipleInstance = KPI_MULTINST_ONE;
static const wchar_t kbs98_SupportExts[] = L".s98/.vgm/.vgz/.cym/.mym/.x1f";
static const wchar_t kbs98_SupportExtsS98[] = L".s98/.cym/.mym/.x1f";
static const wchar_t kbs98_SupportExtsVGM[] = L".vgm/.vgz/.cym/.mym/.x1f";
static const wchar_t kbs98_SupportExtsCYM_MYM_X1F[] = L".cym/.mym/.x1f";

static const wchar_t kbs98_MultiSongExts[] = L"";
static const DWORD kbs98_SupportTagInfo = 0;//KPI_DECODER_MODULEINFO::TAG_EXTRA;
static const DWORD kbs98_SupportConfig = 1;
static void* kbs98_Reserved1 = NULL;
static void* kbs98_Reserved2 = NULL;

HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbS98DecoderModule::KbS98DecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbs98_Version,
        kbs98_MultipleInstance,
        kbs98_GUID,
        kbs98_Description,
        kbs98_Copyright,
        kbs98_SupportExts,
        kbs98_MultiSongExts,
        kbs98_Reserved1,
        kbs98_Reserved2,
        kbs98_SupportTagInfo,
        kbs98_SupportConfig,
        0, 0, 0, 0                   //reserved
     }
{
    pConfig->AddRef();
    m_pConfig = pConfig;
}
//////////////////////////////////////////////////////////////////////////////
KbS98DecoderModule::~KbS98DecoderModule(void)
{
    m_pConfig->Release();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbS98DecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    kbs98_Settings settings;
    kbs98_GetSettings(m_pConfig, &settings);
    if(settings.bEnableS98 && settings.bEnableVGM){
        m_ModuleInfo.cszSupportExts = kbs98_SupportExts;
    }
    else if (settings.bEnableS98) {
        m_ModuleInfo.cszSupportExts = kbs98_SupportExtsS98;
    }
    else if (settings.bEnableVGM) {
        m_ModuleInfo.cszSupportExts = kbs98_SupportExtsVGM;
    }
    else{
        m_ModuleInfo.cszSupportExts = kbs98_SupportExtsCYM_MYM_X1F;
    }
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbS98DecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                      IKpiFile     *pFile,   //音楽データ
                                      IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                      IKpiDecoder **ppDecoder)
{
    KbS98Decoder *pDecoder = new KbS98Decoder(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 KbS98DecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return kbs98_EnumConfig(pEnumerator);
}
/////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbS98DecoderModule::ApplyConfig(const wchar_t *cszSection,//セクション名
                             const wchar_t *cszKey,    //キー名
                             INT64  nValue, //変更後の値(BOOL/INT の場合)
                             double dValue, //変更後の値(FLOAT の場合)
                             const wchar_t *cszValue)////変更後の値(文字列)
{
    return kbs98_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, &kbs98_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbS98DecoderModule(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;
}
