#include <windows.h>
#include <stdio.h>
#include "kbgym_module.h"
#include "kbgym_decoder.h"
#include "kbgym_config.h"

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

static const wchar_t kbgym_Description[] = L"GYM Decoder v" KBGYM_VERSION_STR " based on m_gym.kpi v1.0r10";
static const wchar_t kbgym_Copyright[] =   L"kbgym.kpi Copyright(C) 2016-2022 Kobarin\n"
                                           L"m_gym.kpi Copyright(C) 200?-2004 Mamiya\n"
                                           L"FM Sound Generator rel.008 Copyright(C) 1998-2003 cisc\n"
                                           L"fmopl.c Copyright (C) 1999,2000 Tatsuyuki Satoh , MultiArcadeMachineEmurator development\n"
                                           L"zlib Copyright (C) 1995-2021 Jean-loup Gailly and Mark Adler\n"
                                           L"libbzip2 Copyright (C) 1996-2019 Julian Seward";

// {F651F1A6-D945-4AAC-ABC4-F270D08B0243}
static const GUID kbgym_GUID = 
{ 0xf651f1a6, 0xd945, 0x4aac, { 0xab, 0xc4, 0xf2, 0x70, 0xd0, 0x8b, 0x2, 0x43 } };

static const DWORD kbgym_MultipleInstance = KPI_MULTINST_INFINITE;//KPI_MULTINST_ONE;
static const wchar_t kbgym_SupportExts[] = L".gym/.ssl";
static const wchar_t kbgym_SupportExtsEmpty[] = L"";
static const wchar_t kbgym_MultiSongExts[] = L"";
static const DWORD kbgym_SupportTagInfo = 0;//KPI_DECODER_MODULEINFO::TAG_EXTRA;
static const DWORD kbgym_SupportConfig = 1;
static void* kbgym_Reserved1 = NULL;
static void* kbgym_Reserved2 = NULL;

HINSTANCE g_hKpi = NULL;
//////////////////////////////////////////////////////////////////////////////
KbGymDecoderModule::KbGymDecoderModule(IKpiConfig *pConfig)
    :m_ModuleInfo{
        sizeof(KPI_DECODER_MODULEINFO), //cb(=sizeof(KPI_DECODER_MODULEINFO))
        KPI_DECODER_MODULE_VERSION,     //dwModuleVersion(=KPI_DECODER_MODULE_VERSION)
        kbgym_Version,
        kbgym_MultipleInstance,
        kbgym_GUID,
        kbgym_Description,
        kbgym_Copyright,
        kbgym_SupportExts,
        kbgym_MultiSongExts,
        kbgym_Reserved1,
        kbgym_Reserved2,
        kbgym_SupportTagInfo,
        kbgym_SupportConfig,
        0, 0, 0, 0                   //reserved
     }
{
    kbgym_Settings settings;
    kbgym_GetSettings(pConfig, &settings);
    if(settings.bEnableGYM){
        m_ModuleInfo.cszSupportExts = kbgym_SupportExts;
    }
    else{
        m_ModuleInfo.cszSupportExts = kbgym_SupportExtsEmpty;
    }
    m_pConfig = pConfig;
    pConfig->AddRef();
}
//////////////////////////////////////////////////////////////////////////////
KbGymDecoderModule::~KbGymDecoderModule(void)
{
    m_pConfig->Release();
}
//////////////////////////////////////////////////////////////////////////////
void  WINAPI KbGymDecoderModule::GetModuleInfo(const KPI_DECODER_MODULEINFO **ppInfo)
{
    *ppInfo = &m_ModuleInfo;
}
//////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbGymDecoderModule::Open(const KPI_MEDIAINFO *cpRequest,
                                      IKpiFile     *pFile,   //音楽データ
                                      IKpiFolder   *pFolder, //音楽データがあるフォルダ
                                      IKpiDecoder **ppDecoder)
{
    KbGymDecoder *pDecoder = new KbGymDecoder;
    DWORD dwCount = pDecoder->Open(cpRequest, pFile, pFolder);
    if(dwCount == 0){
        *ppDecoder = NULL;
        delete pDecoder;
        return 0;
    }
    *ppDecoder = pDecoder;
    return dwCount;
}
/////////////////////////////////////////////////////////////////////////////
BOOL  WINAPI KbGymDecoderModule::EnumConfig(IKpiConfigEnumerator *pEnumerator)
{
    return kbgym_EnumConfig(pEnumerator);
}
/////////////////////////////////////////////////////////////////////////////
DWORD WINAPI KbGymDecoderModule::ApplyConfig(const wchar_t *cszSection,//セクション名
                             const wchar_t *cszKey,    //キー名
                             INT64  nValue, //変更後の値(BOOL/INT の場合)
                             double dValue, //変更後の値(FLOAT の場合)
                             const wchar_t *cszValue)////変更後の値(文字列)
{
    return kbgym_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, &kbgym_GUID, NULL, &pConfig);
    IKpiDecoderModule *pDecoderModule = new KbGymDecoderModule(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;
}
