
#pragma once
#include "System.h"

// ゲーム状態
GAMESTATE GameState;

HMODULE hCSDLL;
SIMULATION_FUNC Simulation = nullptr;
CREATESHOT_FUNC CreateShot = nullptr;
CREATEHITSHOT_FUNC CreateHitShot = nullptr;
GETSCORE_FUNC GetScore = nullptr;

bool LoadFunction()
{
	hCSDLL = LoadLibrary("CurlingSimulator.dll");

	if (hCSDLL != nullptr){
		Simulation = (SIMULATION_FUNC)GetProcAddress(hCSDLL, "Simulation");
		CreateShot = (CREATESHOT_FUNC)GetProcAddress(hCSDLL, "CreateShot");
		CreateHitShot = (CREATEHITSHOT_FUNC)GetProcAddress(hCSDLL, "CreateHitShot");
		GetScore = (GETSCORE_FUNC)GetProcAddress(hCSDLL, "GetScore");
	}
	else{
		MessageBox(nullptr, "CurlingSimulator.dll の読み込みに失敗しました", "エラー", MB_OK);
		return false;
	}

	return true;
}

// メッセージの送信
void Send(char *Message)
{
	DWORD NumberOfBytesWritten;
	WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), Message, strlen(Message), &NumberOfBytesWritten, NULL);
}

// メッセージの受信
void Recv(char *Message, size_t Size)
{
	DWORD NumberOfBytesRead;
	ReadFile(GetStdHandle(STD_INPUT_HANDLE), Message, Size, &NumberOfBytesRead, NULL);
}

// メッセージ受信用スレッド
DWORD WINAPI ReceiveCUIThread(LPVOID pParam)
{
	DWORD	bRet = TRUE;
	char	Message[1024];

	while (1){
		memset(Message, 0x00, sizeof(Message));
		Recv(Message, sizeof(Message));
		DoCommand(Message);
	}

	return bRet;
}

void main()
{
	char	Message[1024];
	DWORD	dwThreadId;
	DWORD	NumberOfBytesWritten;

	// 関数の読み込み
	if (LoadFunction() != TRUE){
		return;
	}

	// スレッドの作成
	CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReceiveCUIThread, NULL, 0, &dwThreadId);

	// コマンド受信
	while (1){
		memset(Message, 0x00, sizeof(Message));
		Recv(Message, sizeof(Message));
		DoCommand(Message);
	}

	return;
}