/***********************************************************************
*Copyright (c)2012 , 天际边工作室
*All rights reserved.
**
文件名称：	Main.cpp
*摘要：		程序入口，主程序
**
*修改历史：
*	版本    日期     作者     改动内容和原因
*   ----------------------------------------------------
*	1.0    2012-10-5  wstjb     基本的功能完成
***********************************************************************/

#define WINDOW_CLASS_NAME "SkySimulater"
#define SCREEN_WIDTH 400
#define SCREEN_HEIGHT 300
#define DEBUG_CLIENTBLOCK  new( _CLIENT_BLOCK, __FILE__, __LINE__)
#define _CRTDBG_MAP_ALLOC
#define new DEBUG_CLIENTBLOCK

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#include "GamEng.h"
#include <fstream>
#include <time.h>
#include <Shlwapi.h>
#include "resource.h"  
#include "Dialog.h" 
#include "SkinH.h" 
#pragma comment(lib,"SkinH.lib") 

#pragma comment(lib,"shlwapi.lib")
HINSTANCE hDLLDrv;
HINSTANCE hDLLDrvSRP;
HWND hDlgModeless;
RECT rect;
HWND hwnd;
HDC hdc;
HDC hdcMem;
HBITMAP hbmp;
HBITMAP hbmp2;
HDC hdcTmp;
HINSTANCE hInst;
unsigned int active;//本窗口是否为活动
PAINTSTRUCT ps;
BITMAP bmp;
Game game;//=NULL;
char texta[255][20];
int flag=0;
//SIZE size;
int version_now=0;
int color=0;
int menushow1=1;
int menumove1=1;
int menuspeed1=1;
//按键定义
int KeyReturn=13;
int KeyEsc=27;
int KeyHome=36;
int KeySpace=32;
int KeyLeft0=37;
int KeyUp0=38;
int KeyRight0=39;
int KeyDown0=40;
int KeyR = 82;
int KeyN = 78;//20161221
int KeyPageUp=33;
int KeyPageDown=34;
int KeyInsert=45;
int KeyDelete=46;
int KeyEnd=35;
int widthx;//20170602改全局
int heighty;//20170602改全局

byte libByte[2097152];
byte resByte[2731][16384];//存储lib资源
int resByteLen[2731];
int resPos[2731][6];//存储lib地址和编号 第六位为长度
int resCount;
char g_gameName[9+1];
int g_debugFlag = 0;
int g_dgdelayFlag = 0;
int useEngStr = 0;//20170130使用gam文件包含的菜单显示名
byte engByte[294912];//20170130eng
int noReadScript = 0;//20170131用于不启用读档脚本
int allowSave = 0;//20170319允许即时存档
int disableAnima = 0 ;//20170820关闭灵力秒杀

int allowDebug = 0;
int pluginSum=0;
int allowThird = 0;
int GMCUsed=0;
int SRPUsed=0;
int dOpenFlag = 0;//直接打开
char absolutepath[MAX_PATH];
int runDebug = 0;//20171003 20210816改含义为0-开启随机战斗 1-关闭随机战斗
int mNCDebug = 0;//20210816地图无碰撞开关 0-关闭 1-开启
int fgtDebug = 0;//20210816战斗秒杀开关 0-关闭 1-开启

LRESULT CALLBACK WinSkySideProc(	//声明窗口函数
								HWND hwnd,      // handle to window
								UINT uMsg,      // message identifier
								WPARAM wParam,  // first message parameter
								LPARAM lParam   // second message parameter
								);

/***********************************************************************
* 说明:     windows主函数
* 输入参数: 略
* 返回值  : 略
* 修改历史:
*               姓名            日期             说明
*             ------          ----------      -------------
*             wstjb          2012-10-5       完成基本功能
***********************************************************************/
int WINAPI WinMain(
	HINSTANCE hInstance,      // handle to current instance
	HINSTANCE hPrevInstance,  // handle to previous instance
	LPSTR lpCmdLine,          // command line
	int nCmdShow              // show state
	)
{
	WNDCLASSEX wndcs;
	MSG msg;
	//HDC hdc;
	hInst = hInstance;
	char version[100];
	char workpath[MAX_PATH];   
    GetModuleFileName(NULL,(LPSTR)workpath,sizeof(workpath));   
	char *p = strrchr(workpath, '\\');  
	*p=0x00;  
	//char absolutepath[MAX_PATH];
	sprintf_s(absolutepath,"%s\\%s",workpath,STARTINIPATH);
	char versionpath[MAX_PATH];
	sprintf_s(versionpath,"%s\\%s",workpath,VERSIONINIPATH);
	widthx = GetPrivateProfileInt("Startup", "Width", 800, absolutepath );
	heighty = GetPrivateProfileInt("Startup", "Height", 600, absolutepath );
	version_now=GetPrivateProfileInt("Startup", "Version", 130818100, versionpath );//130325021;
	color=GetPrivateProfileInt("Startup", "Color", 0, absolutepath);
	menushow1=GetPrivateProfileInt("Startup", "Menushow", 0, absolutepath);
	menumove1=GetPrivateProfileInt("Startup", "Menumove", 0, absolutepath );
	menuspeed1=GetPrivateProfileInt("Startup", "Menuspeed", 0, absolutepath );

	KeyReturn=GetPrivateProfileInt("Key", "Return", 13, absolutepath );
	KeyEsc=GetPrivateProfileInt("Key", "Esc", 27, absolutepath);
	KeySpace=GetPrivateProfileInt("Key", "Space" ,32, absolutepath );
	KeyHome=GetPrivateProfileInt("Key", "Home", 36, absolutepath );
	KeyUp0=GetPrivateProfileInt("Key", "Up", 38, absolutepath );
	KeyDown0=GetPrivateProfileInt("Key", "Down", 40, absolutepath );
	KeyLeft0=GetPrivateProfileInt("Key", "Left", 37, absolutepath );
	KeyRight0=GetPrivateProfileInt("Key", "Right", 39, absolutepath );
	KeyR = GetPrivateProfileInt("Key", "Repeat", 82, absolutepath);
	KeyN = GetPrivateProfileInt("Key", "AutoWalk", 78,absolutepath );
	KeyPageUp = GetPrivateProfileInt("Key", "PageUp", 33, absolutepath );
	KeyPageDown = GetPrivateProfileInt("Key", "PageDown", 34, absolutepath );
	KeyInsert = GetPrivateProfileInt("Key", "KeyInsert", 45,absolutepath );
	KeyDelete = GetPrivateProfileInt("Key", "KeyDelete", 46, absolutepath );
	KeyHome = GetPrivateProfileInt("Key", "KeyHome", 36,absolutepath );
	KeyEnd = GetPrivateProfileInt("Key", "KeyEnd", 35, absolutepath );

	allowDebug = GetPrivateProfileInt("Debug", "DebugEnable", 0, absolutepath );
	runDebug = GetPrivateProfileInt("Debug", "RunDebug", 0, absolutepath );//20171003
	mNCDebug = GetPrivateProfileInt("Debug", "MNCDebug", 0, absolutepath );//20210816
	fgtDebug = GetPrivateProfileInt("Debug", "FightDebug", 0, absolutepath );//20210816
	pluginSum = GetPrivateProfileInt("Plugin", "PluginSum", 0, absolutepath );
	GMCUsed = GetPrivateProfileInt("Plugin", "GMCUsed", 0, absolutepath );
	SRPUsed = GetPrivateProfileInt("Plugin", "SRPUsed", 0, absolutepath );
	allowThird = GetPrivateProfileInt("Plugin", "AllowThird", 0, absolutepath );

	//20181031add
    noReadScript = GetPrivateProfileInt("MenuConfig", "CloseStartScript", 0, absolutepath );
    allowSave = GetPrivateProfileInt("MenuConfig", "AllowInstantSave", 0, absolutepath );
    disableAnima = GetPrivateProfileInt("MenuConfig", "CloseAnimaSuddenKill", 0, absolutepath );

	wndcs.cbSize =sizeof(WNDCLASSEX); 
	wndcs.cbClsExtra =0;
	wndcs.cbWndExtra =0;
	wndcs.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
	wndcs.hCursor =LoadCursor(NULL,IDC_ARROW);
	wndcs.hIcon =LoadIcon(hInstance, "about");
	wndcs.hInstance =hInstance;
	wndcs.lpfnWndProc =WinSkySideProc;
	wndcs.lpszClassName =WINDOW_CLASS_NAME;
	wndcs.lpszMenuName =MAKEINTRESOURCE(IDR_MENU1);
	wndcs.style = CS_DBLCLKS|CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
	wndcs.hIconSm = LoadIcon(hInstance, "about");
	//hinstance_app=hinstance;

	RegisterClassEx(&wndcs);

	//20161118
	LPWSTR *szArgList;
	int argCount;

	sprintf_s(version,"天际边伏魔引擎 ver%d.%02d",version_now%1000/100,version_now - version_now/100*100);
	g_debugFlag = 0;

	//szArgList 载入的调试的gam文件名
	szArgList = CommandLineToArgvW((LPCWSTR)GetCommandLineW(), &argCount);
	if (lpCmdLine[0] == '\0')
	{
		//sprintf_s(version,"SkyRPGSim Debuger");
		//g_debugFlag = 1;

		//sprintf_s(version,"SkyRPGSim ver%d.%02d",version_now%1000/100,version_now - version_now/100*100);
		//g_debugFlag = 0;
	}
	else if(allowDebug == 1)
	{
		sprintf_s(version,"SkyRPGSim Debuger");
		g_debugFlag = 1;
	}
	else 
	{
		dOpenFlag = 1;

	}

	LocalFree(szArgList);

	hwnd=CreateWindowEx(NULL,
		WINDOW_CLASS_NAME,
		version,
		WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU| WS_VISIBLE,
		CW_USEDEFAULT,CW_USEDEFAULT,
		widthx+10,heighty+20,
		NULL,
		NULL,
		hInstance,
		NULL);



	ShowWindow (hwnd, nCmdShow) ;//显示Windows窗口
	UpdateWindow (hwnd) ;//修改Windows窗口

	if(allowDebug==1)
	{
		HMENU hMenu = GetMenu(hwnd);

		HMENU hSubMenu = GetSubMenu(hMenu,1);
		EnableMenuItem( hSubMenu,4,MF_BYPOSITION |MF_ENABLED );
		DeleteObject(&hSubMenu);
		DeleteObject(&hMenu);
	}
	if(GMCUsed==1)
	{
		HMENU hMenu = GetMenu(hwnd);
		HMENU hSubMenu = GetSubMenu(hMenu,2);
		HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
		EnableMenuItem( hSubMenuA,0,MF_BYPOSITION |MF_ENABLED );
		DeleteObject(&hSubMenuA);
		DeleteObject(&hSubMenu);
		DeleteObject(&hMenu);
	}

	/*	hdcPaint=GetDC(hwnd);
	hdc =CreateCompatibleDC(hdcPaint);
	hbmp=CreateCompatibleBitmap(hdcPaint,size.cx,size.cy);
	SelectObject(hdc,hbmp);*/
	hdc = GetDC(hwnd );

	SetMapMode(hdc, MM_ISOTROPIC);    
	SetWindowExtEx(hdc, 160, 96, NULL); 
	SetViewportExtEx(hdc, widthx, heighty, NULL);

	hdcMem  = CreateCompatibleDC(hdc);
	hdcTmp  = CreateCompatibleDC(hdc);

	if(GMCUsed==1)
	{
		SetBkMode(hdc, TRANSPARENT);
		SetBkMode(hdcMem, TRANSPARENT);
		SetBkMode(hdcTmp, TRANSPARENT);
	}
	if(SRPUsed==1)
	{
		MessageBox(hwnd,"SRP插件已经启用，请在对话框界面按E修改对话","提示",MB_OK);  
	}

	//hbmp=CreateCompatibleBitmap(hdc,800,600);
	hbmp=(HBITMAP)LoadImage(NULL, "Res\\logo.sbp", IMAGE_BITMAP,0,0, LR_LOADFROMFILE);
	//hbmp2=CreateCompatibleBitmap(hdc,800,600);
	hbmp2=(HBITMAP)LoadImage(NULL, "Res\\logo.sbp", IMAGE_BITMAP,0,0, LR_LOADFROMFILE);
	SelectObject(hdcTmp,hbmp);
	SelectObject(hdcMem,hbmp2);
	DeleteObject(hbmp);
	DeleteObject(hbmp2);
	//20160816优化随机数种子
	struct timeval tv; 
	gettimeofday(&tv, NULL);
	srand(tv.tv_sec + tv.tv_usec);

	if(1==GetPrivateProfileInt("Startup", "Screen", 0, absolutepath ))
	{
		HWND    hDesk;     
		RECT    rc;     
		hDesk   =    GetDesktopWindow();     
		GetWindowRect(    hDesk,   &rc    );     
		SetWindowLong(    hwnd,    GWL_STYLE,    WS_BORDER    );     
		SetWindowPos( hwnd,    HWND_TOP,0,0, rc.right, rc.bottom,  SWP_SHOWWINDOW); 
		widthx=GetSystemMetrics(SM_CXSCREEN);
		heighty=GetSystemMetrics(SM_CYSCREEN);
		SetViewportExtEx(hdc, widthx, heighty, NULL);
	}

	SetUnhandledExceptionFilter(TopLevelExceptionFilter); 
	//game=new Game();
	game.Game_Init();
	game.Game_Main();

	if(dOpenFlag == 1)
	{
		game.libOperator.Init();
		LPTSTR pszExtension;
		delchar(lpCmdLine, '\"');
		pszExtension = PathFindExtension(lpCmdLine);
		pszExtension = _strlwr(pszExtension);
		if(strcmp(pszExtension,".gam")==0)
		{
			game.libOperator.libLen = readFromFile(libByte,lpCmdLine,1);//gam
			int rt = CheckGamCorrect(lpCmdLine);//检测gam文件
			if(rt == -1)
			{
				MessageBox(hwnd,"请不要载入6980gam文件！","提示",MB_OK );
				return 0;
			}
			if(rt == -2)
			{
				MessageBox(hwnd,"文件格式不正确！","警告",MB_OK );
				return 0;
			}
			if(rt == -3)
			{
				MessageBox(hwnd,"本模拟平台仅支持RPG游戏！","警告",MB_OK );
				return 0;
			}
			useEngStr = 1;//20170130使用gam文件包含的菜单显示名
			readFromFile(engByte,lpCmdLine,2);//eng

		}
		else
		{
			game.libOperator.libLen = readFromFile(libByte,lpCmdLine,0);//lib
			useEngStr = 0;
		}
		getEngStr();
		game.libOperator.GetData();

		memcpy(game.save_start.gameName,libByte+3,8);
		del_sp(game.save_start.gameName);
		memset(g_gameName,0,sizeof(g_gameName));
		memcpy(g_gameName,game.save_start.gameName,8);

		
		//20160730新增初始化
		game.ng = 0;
		game.scriptEng.keyflag = 0;
		KillTimer (hwnd, 1) ;

		ShowCG(1,MOVIE_LOGO,0,0,8);
		ShowCG(1,MOVIE_GAME_NAME,0,0,8);
	}
	if(g_debugFlag == 1)
	{
		//MessageBox(hwnd,(char *)lpCmdLine,"提示",MB_OK );
		game.libOperator.Init();
		game.libOperator.libLen = readFromFile(libByte,(char *)lpCmdLine,0);//lib
		getEngStr();//20210630
		game.libOperator.GetData();

		memcpy(game.save_start.gameName,libByte+3,8);
		del_sp(game.save_start.gameName);
		memset(g_gameName,0,sizeof(g_gameName));
		memcpy(g_gameName,game.save_start.gameName,8);


		wlogfile(1, "winmain" ,"初始化debug",0); 
		//20160730新增初始化
		game.ng = 0;
		game.scriptEng.keyflag = 0;
		KillTimer (hwnd, 1) ;

		ShowCG(1,MOVIE_LOGO,0,0,8);
		ShowCG(1,MOVIE_GAME_NAME,0,0,8);

	}

	/*while(1)

	{
	if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
	{
	if(!GetMessage(&msg,NULL,0,0))
	return msg.wParam;
	TranslateMessage(&msg);
	DispatchMessage(&msg);
	}
	else if(active)
	{
	game.Game_Main();
	}
	else
	WaitMessage();
	}*/

	while (GetMessage (&msg, NULL, 0, 0))
	{
		if(hDlgModeless==0||!IsDialogMessage(hDlgModeless,&msg))
		{
			TranslateMessage (&msg) ;
			DispatchMessage (&msg) ;
			Sleep(5);

		}
		else if(IsDialogMessage(hDlgModeless,&msg))
		{
			if (((&msg)->message == WM_KEYDOWN) && ((&msg)->wParam == VK_RETURN))
			{
				TranslateMessage (&msg) ;
				DispatchMessage (&msg) ;
			}
			if (((&msg)->message == WM_KEYDOWN) && ((&msg)->wParam == VK_TAB))
			{
				TranslateMessage (&msg) ;
				DispatchMessage (&msg) ;
			}
			if (((&msg)->message == WM_KEYDOWN) && ((&msg)->wParam == VK_ESCAPE))
			{
				TranslateMessage (&msg) ;
				DispatchMessage (&msg) ;
			}
		}
	}


	/*while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg) ;
		DispatchMessage (&msg) ;
		Sleep(5);
	}*/

	//game.Game_Shutdown();

	return (msg.wParam);
}


/***********************************************************************
* 说明:     窗口函数
* 输入参数: 略
* 返回值  : 略
* 修改历史:
*               姓名            日期             说明
*             ------          ----------      -------------
*             wstjb          2012-10-5       完成基本功能
***********************************************************************/
LRESULT CALLBACK WinSkySideProc(
	HWND hwnd,      // handle to window
	UINT uMsg,      // message identifier
	WPARAM wParam,  // first message parameter
	LPARAM lParam   // second message parameter
	)
{
	SetUnhandledExceptionFilter(TopLevelExceptionFilter); 
	switch (uMsg)
	{
	case WM_CREATE:
		{RECT rc, rc1, rctomove;
		int width = GetSystemMetrics(SM_CXSCREEN);
		int height = GetSystemMetrics(SM_CYSCREEN);
		rc.left = 0;
		rc.top = 0;
		rc.right = width+10;
		rc.bottom = height+50;
		GetClientRect(hwnd, &rc1);
		rc1.right +=10;
		rc1.bottom +=50;
		rctomove.left = (rc.right - rc.left) / 2 - (rc1.right - rc1.left) / 2;
		rctomove.right =  widthx + 10;
		rctomove.top = (rc.bottom - rc.top) / 2 - (rc1.bottom - rc1.top) / 2;
		rctomove.bottom = heighty + 30;
		//设置窗体位置
		SetWindowPos(hwnd,HWND_TOP,rctomove.left, rctomove.top, rctomove.right, rctomove.bottom,SWP_SHOWWINDOW);
		/*int scrWidth,scrHeight;
		RECT rect;
		//获得屏幕尺寸
		scrWidth=GetSystemMetrics(SM_CXSCREEN);
		scrHeight=GetSystemMetrics(SM_CYSCREEN);
		//获取窗体尺寸
		GetWindowRect(hwnd,&rect);
		rect.left=(scrWidth-rect.right)/2;
		rect.top=(scrHeight-rect.bottom)/2;
		//设置窗体位置
		SetWindowPos(hwnd,HWND_TOP,rect.left,rect.top,GetPrivateProfileInt("Startup", "Width", 800, absolutepath ),GetPrivateProfileInt("Startup", "Height", 600, absolutepath ),SWP_SHOWWINDOW);
		break;*/
		//菜单初始化20171003
		if(allowThird == 1)
		{
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,2);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_ENABLETHIRD,MF_CHECKED  );
				DelThirdPartyPluginEnableChoose(hSubMenuA,0);
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
		}

		//20181103
		HMENU hMenu = GetMenu(hwnd);
		HMENU hSubMenu = GetSubMenu(hMenu,1);
		HMENU hSubMenuA = GetSubMenu(hSubMenu,3);
		CheckMenuItem ( hSubMenuA,IDM_ANIMA0,MF_UNCHECKED  );
		CheckMenuItem ( hSubMenuA,IDM_ANIMA1,MF_UNCHECKED  );
		CheckMenuItem ( hSubMenuA,IDM_ANIMA2,MF_UNCHECKED  );
		
		if(disableAnima == 0)CheckMenuItem ( hSubMenuA,IDM_ANIMA0,MF_CHECKED  );
		if(disableAnima == 1)CheckMenuItem ( hSubMenuA,IDM_ANIMA1,MF_CHECKED  );
		if(disableAnima == 2)CheckMenuItem ( hSubMenuA,IDM_ANIMA2,MF_CHECKED  );

		CheckMenuItem ( hSubMenu,IDM_NOREADSCRIPT,MF_UNCHECKED  );
		if(noReadScript == 1)CheckMenuItem ( hSubMenu,IDM_NOREADSCRIPT,MF_CHECKED  );

		CheckMenuItem ( hSubMenu,IDM_ALLOWSAVE,MF_UNCHECKED  );
		if(allowSave == 1)CheckMenuItem ( hSubMenu,IDM_ALLOWSAVE,MF_CHECKED  );

		DeleteObject(&hSubMenuA);
		DeleteObject(&hSubMenu);
		DeleteObject(&hMenu);
		
	}
		break;
	case WM_ACTIVATEAPP:
		active=wParam;
		SetFocus(hwnd);
		break;
		//case WM_LBUTTONDOWN:
		//	ShowPic(hdc, hdcMem,200,hbmp,bmp,"Res\\2037.sbp");
		//	ShowPic(hdc, hdcMem,200,hbmp,bmp,"Res\\a.sbp");
		//	TextOut(hdc,700 ,100,"dsdsd",strlen("dsdsd"));
		//	Delay(5000);
		//return 0;
	case	WM_DESTROY:
		//ReleaseObjects();
		PostQuitMessage (0) ;
		break;
	case 	WM_PAINT:
		//HDC hdcPaint;
		//hdcPaint=GetDC(hwnd);
		//hdcPaint = BeginPaint(hwnd, &ps); 
		/*hdc =CreateCompatibleDC(hdcPaint);
		hbmp=CreateCompatibleBitmap(hdcPaint,size.cx,size.cy);
		SelectObject(hdc,hbmp);
		//hdc = GetDC(hwnd );
		hdcMem  = CreateCompatibleDC(hdcPaint);
		SelectObject(hdcMem,hbmp);*/
		if(flag==0)
		{
			HDC hdcPaint;
			hdcPaint = BeginPaint(hwnd, &ps); 
			BitBlt(hdcPaint,0,0,800,600,hdcTmp,0,0,SRCCOPY);
			EndPaint(hwnd, &ps); 
		}
		else
		{
			hdc = BeginPaint(hwnd, &ps); 
			BitBlt(hdc,0,0,800,600,hdcTmp,0,0,SRCCOPY);
			EndPaint(hwnd, &ps); 
			//BitBlt(hdc,0,0,800,600,hdcTmp,0,0,SRCCOPY);
		}
		//EndPaint(hwnd, &ps); 
		break;   

	case WM_SIZE:
		//size.cx  = LOWORD(lParam);
		//size.cy  = HIWORD(lParam);

		return 0;

		/*case WM_CLOSE:
		if(IDYES==MessageBox(hwnd,"是否退出?","提示",MB_YESNO))
		{
		PostQuitMessage(0);
		//DestroyWindow(hwnd);
		}
		break;*/
	case WM_COPYDATA://20161118
		if(g_debugFlag == 1)//调试状态
		{
			COPYDATASTRUCT *pCopyData = (COPYDATASTRUCT*)lParam;  
			char szBuffer[300];  

			memset(szBuffer, 0, sizeof(szBuffer));  
			sprintf(szBuffer, "%s", (char*)pCopyData->lpData);  

			//数据分析,进行断点中断处理
			int ret = game.gameMap.scriptEng.DgDelMsg((int)pCopyData->dwData,szBuffer);
			if(ret == 12)
			{
				g_dgdelayFlag = 1;
			}
			else if(ret == 13)//步进
			{
				g_dgdelayFlag = 2;
			}
		}
		break;   
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{	
		case IDM_EXIT:
			PostQuitMessage(0);
			break;
		case IDM_OPEN:
			if(g_debugFlag == 1)break;
			OPENFILENAME ofn;
			static char szFile[256];
			static char szFileTitle[256];
			memset(&ofn,0,sizeof(ofn));
			ofn.lStructSize=sizeof(ofn);
			ofn.hwndOwner=hwnd;
			ofn.lpstrFilter="lib文件(*.lib),gam文件(*.gam)\0*.lib;*.gam;\0\0";
			ofn.nFilterIndex=1;
			ofn.lpstrFile=szFile;
			ofn.nMaxFile=sizeof(szFile);
			ofn.lpstrFileTitle=szFileTitle;
			ofn.nMaxFileTitle=sizeof(szFileTitle);
			ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_EXPLORER;
			//当按下确定按钮
			if(GetOpenFileName(&ofn))
			{
				game.libOperator.Init();
				LPTSTR pszExtension;
				pszExtension = PathFindExtension(ofn.lpstrFile);
				pszExtension = _strlwr(pszExtension);
				if(strcmp(pszExtension,".gam")==0)
				{
					game.libOperator.libLen = readFromFile(libByte,ofn.lpstrFile,1);//gam
					int rt = CheckGamCorrect(ofn.lpstrFile);//检测gam文件
					if(rt == -1)
					{
						MessageBox(hwnd,"请不要载入6980gam文件！","提示",MB_OK );
						break;
					}
					if(rt == -2)
					{
						MessageBox(hwnd,"文件格式不正确！","警告",MB_OK );
						break;
					}
					if(rt == -3)
					{
						MessageBox(hwnd,"本模拟平台仅支持RPG游戏！","警告",MB_OK );
						break;
					}
					useEngStr = 1;//20170130使用gam文件包含的菜单显示名
					readFromFile(engByte,ofn.lpstrFile,2);//eng

				}
				else
				{
					game.libOperator.libLen = readFromFile(libByte,ofn.lpstrFile,0);//lib
					useEngStr = 0;
				}
				getEngStr();
				game.libOperator.GetData();

				memcpy(game.save_start.gameName,libByte+3,8);
				del_sp(game.save_start.gameName);
				memset(g_gameName,0,sizeof(g_gameName));
				memcpy(g_gameName,game.save_start.gameName,8);

				if(SRPUsed == 1)
				{
					char workpath[MAX_PATH];   
					GetModuleFileName(NULL,(LPSTR)workpath,sizeof(workpath));   
					char *p = strrchr(workpath, '\\');  
					*p=0x00;  
					char tPath[MAX_PATH];
					sprintf(tPath,"%s\\Res\\SRP.dll",workpath);
					hDLLDrvSRP = LoadLibrary(tPath);
					if(hDLLDrvSRP==0)
					{
						MessageBox(hwnd,"启用SRP插件功能失败，请下载SRP扩展库到Res文件夹下","提示",MB_OK);  
						ShellExecute(hwnd,"open","http://sim.skysidestudio.com/help_1.html",NULL,NULL,SW_SHOW );
						break;
					}
					if(strlen(g_gameName)==0)
					{
						MessageBox(hwnd,"启用SRP插件功能失败，请先加载LIB文件","提示",MB_OK);  
						break;
					}
					InitSRP(hDLLDrvSRP,g_gameName);
				
					MessageBox(hwnd,"启用SRP插件功能成功，请在对话框界面按E修改对话","提示",MB_OK);  
					HMENU hMenu = GetMenu(hwnd);
					HMENU hSubMenu = GetSubMenu(hMenu,2);
					HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
					CheckMenuItem ( hSubMenuA,IDM_GMC,MF_CHECKED  );
					DeleteObject(&hSubMenuA);
					DeleteObject(&hSubMenu);
					DeleteObject(&hMenu);
				}
				wlogfile(1, "winmain" ,"初始化",0); 
				//20160730新增初始化
				game.ng = 0;
				game.scriptEng.keyflag = 0;
				KillTimer (hwnd, 1) ;

				ShowCG(1,MOVIE_LOGO,0,0,8);
				ShowCG(1,MOVIE_GAME_NAME,0,0,8);


			}

			else
			{
			}
			break;
		case IDM_ABOUT:
			DialogBox(hInst,LPCTSTR(IDD_ABOUT),hwnd,(DLGPROC)AboutProc);
			/*char word[200];
			sprintf_s(word,"天际边伏魔引擎（原天际边步步高A系列RPG游戏windows模拟平台(SkyRPGSim)） ver%d.%02d\n天际边工作室 荣誉出品",version_now%1000/100,version_now - version_now/100*100);

			MessageBox(hwnd,word,"关于",MB_OK);  */
			break;
		case IDM_KEY:
			MessageBox(hwnd,"方向键移动\nPageUp/PageDown翻页\n回车确认Esc返回\nN自动行走R重复上回合\nInsert/Delete/Home/End功能键","按键说明",MB_OK);  
			break;
		case IDM_NEW:
			ShellExecute(hwnd,"open","notepad.exe","天际边伏魔引擎(原SkyRPGSim)版本更新记录.txt","",SW_SHOW );
			break;
		case IDM_INI:
			{
				DialogBox(hInst,LPCTSTR(IDD_WINDOW),hwnd,(DLGPROC)WindowCfgProc);
				//ShellExecute(hwnd,"open","SkyRPGSim窗口配置程序.exe","","",SW_SHOW );
				//int widthx = GetPrivateProfileInt("Startup", "Width", 800, absolutepath );
				//int heighty = GetPrivateProfileInt("Startup", "Height", 600, absolutepath );
				//SetWindowPos( hwnd,    hwnd,0,0, widthx, heighty,  SWP_SHOWWINDOW); 
				//SetViewportExtEx(hdc, widthx, heighty, NULL);
				//MoveWindow(hwnd,200,300,widthx,heighty,true);

				break;
			}
		case IDM_KEYINI:
			hDlgModeless = CreateDialog(hInst,LPCTSTR(IDD_KEYINI),hwnd,(DLGPROC)KeyCfgProc);
			ShowWindow (hDlgModeless, SW_SHOW) ;
			break;
		case IDM_RANDFIGHT:
			game.gameMap.scriptEng.fightEnable = 0;
			break;
		case IDM_SAVECHANGE:
			//game.gameMap.LoadInit();
			//game.gameMap.scriptEng.save.ChangeSave();
			break;
		case IDM_NOREADSCRIPT:
			{
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,1);
				if(noReadScript == 0)
				{
					if(IDYES==MessageBox(hwnd,"是否关闭读档脚本功能?\n读档脚本功能用于在读档时启用计时器，\n检测游戏角色属性等操作，\n部分游戏在开启即时存档时存档后\n在读档时需要关闭这一功能","提示",MB_YESNO))
					{
						noReadScript = 1;
						CheckMenuItem ( hSubMenu,IDM_NOREADSCRIPT,MF_CHECKED  );
						WritePrivateProfileString( "MenuConfig", "CloseStartScript","1", absolutepath );
					}
				}
				else
				{
					noReadScript = 0;
					CheckMenuItem ( hSubMenu,IDM_NOREADSCRIPT,MF_UNCHECKED  );
					WritePrivateProfileString( "MenuConfig", "CloseStartScript","0", absolutepath );
				}
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				break;
			}
		case IDM_ALLOWSAVE:
			{

				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,1);
				if(allowSave == 0)
				{
					if(IDYES==MessageBox(hwnd,"是否开启即时存档功能?\n部分游戏在开启即时存档时需要关闭读档脚本，\n否则可能存在无法正常读档的情况","提示",MB_YESNO))
					{
						allowSave = 1;
						CheckMenuItem ( hSubMenu,IDM_ALLOWSAVE,MF_CHECKED  );
						WritePrivateProfileString( "MenuConfig", "AllowInstantSave","1", absolutepath );
					}
				}
				else
				{
					allowSave = 0;
					CheckMenuItem ( hSubMenu,IDM_ALLOWSAVE,MF_UNCHECKED  );
					WritePrivateProfileString( "MenuConfig", "AllowInstantSave","0", absolutepath );
				}
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				break;
			}
		case IDM_ANIMA0://灵力秒杀
			{
				disableAnima = 0;
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,1);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,3);
				CheckMenuItem ( hSubMenuA,IDM_ANIMA0,MF_CHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_ANIMA1,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_ANIMA2,MF_UNCHECKED  );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				WritePrivateProfileString( "MenuConfig", "CloseAnimaSuddenKill","0", absolutepath );
				MessageBox(hwnd,"设置成功","提示",MB_OK); 
				break;
			}
		case IDM_ANIMA1://灵力秒杀
			{
				disableAnima = 1;
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,1);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,3);
				CheckMenuItem ( hSubMenuA,IDM_ANIMA0,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_ANIMA1,MF_CHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_ANIMA2,MF_UNCHECKED  );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				WritePrivateProfileString( "MenuConfig", "CloseAnimaSuddenKill","1", absolutepath );
				MessageBox(hwnd,"设置成功","提示",MB_OK); 
				break;
			}
		case IDM_ANIMA2://灵力秒杀
			{
				disableAnima = 2;
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,1);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,3);
				CheckMenuItem ( hSubMenuA,IDM_ANIMA0,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_ANIMA1,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_ANIMA2,MF_CHECKED  );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				WritePrivateProfileString( "MenuConfig", "CloseAnimaSuddenKill","2", absolutepath );
				MessageBox(hwnd,"设置成功","提示",MB_OK); 
				break;
			}
		case IDM_DGIFTONOFF://20210816 调试-随机战斗开关
			if(runDebug == 0)
			{
				runDebug = 1;
			}
			else
			{
				runDebug = 0;
			}
			break;
		case IDM_DGMNCONOFF://20210816 调试-地图无碰撞开关
			if(mNCDebug == 0)
			{
				mNCDebug = 1;
			}
			else
			{
				mNCDebug = 0;
			}
			break;
		case IDM_DGFGTONOFF://20210816 调试-战斗秒杀开关
			if(fgtDebug == 0)
			{
				fgtDebug = 1;
			}
			else
			{
				fgtDebug = 0;
			}
			break;
		case IDM_DUMP:


			break;
		case IDM_STYLE0:
			//SkinH_Detach();
			//SkinH_SetAero(1);
			break;
		case IDM_STYLE1:
			{
				SkinH_AttachEx(("Res\\skin1.she"), NULL);
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,3);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_STYLE1,MF_CHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE2,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE3,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE4,MF_UNCHECKED  );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				break;
			}
		case IDM_STYLE2:
			{
				SkinH_AttachEx(("Res\\skin2.she"), NULL);
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,3);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_STYLE1,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE2,MF_CHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE3,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE4,MF_UNCHECKED  );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				break;
			}
		case IDM_STYLE3:
			{
				SkinH_AttachEx(("Res\\skin3.she"), NULL);
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,3);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_STYLE1,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE2,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE3,MF_CHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE4,MF_UNCHECKED  );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				break;
			}
		case IDM_STYLE4:
			{
				SkinH_AttachEx(("Res\\skin4.she"), NULL);
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,3);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_STYLE1,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE2,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE3,MF_UNCHECKED  );
				CheckMenuItem ( hSubMenuA,IDM_STYLE4,MF_CHECKED  );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
				break;
			}
		case IDM_ENABLETHIRD:
			if(allowThird == 0)
			{
				allowThird = 1;
				MessageBox(hwnd,"启用第三方插件功能成功","提示",MB_OK);  
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,2);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_ENABLETHIRD,MF_CHECKED  );
				WritePrivateProfileString("Plugin", "AllowThird", "1", absolutepath );
				DelThirdPartyPluginEnableChoose(hSubMenuA,0);
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
			}
			else
			{
				allowThird = 0;
				if(hDLLDrv)FreeLibrary(hDLLDrv);
				if(hDLLDrvSRP)FreeLibrary(hDLLDrvSRP);
				MessageBox(hwnd,"关闭第三方插件功能成功","提示",MB_OK);  
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,2);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_ENABLETHIRD,MF_UNCHECKED  );
				WritePrivateProfileString("Plugin", "AllowThird", "0", absolutepath );
				GMCUsed = 0;
				DelThirdPartyPluginEnableChoose(hSubMenuA,1);
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
			}
			break;
		case IDM_MAP:
			//MessageBox(hwnd,"敬请期待","",MB_OK);  
			DialogBox(hInst,LPCTSTR(IDD_MAP),hwnd,(DLGPROC)MapProc);
			break;
		case IDM_STRATEGY:
			ShellExecute(hwnd,"open","http://sim.skysidestudio.com/help_1.html",NULL,NULL,SW_SHOW );
			
			break;
		case IDM_PICREPLACE:
			MessageBox(hwnd,"敬请期待","",MB_OK);  
			break;
		case IDM_MSG:
			if(SRPUsed == 0)
			{
				char workpath[MAX_PATH];   
				GetModuleFileName(NULL,(LPSTR)workpath,sizeof(workpath));   
				char *p = strrchr(workpath, '\\');  
				*p=0x00;  
				char tPath[MAX_PATH];
				sprintf(tPath,"%s\\Res\\SRP.dll",workpath);
				hDLLDrvSRP = LoadLibrary(tPath);
				if(hDLLDrvSRP==0)
				{
					MessageBox(hwnd,"启用SRP插件功能失败，请下载SRP扩展库到Res文件夹下","提示",MB_OK);  
					ShellExecute(hwnd,"open","http://sim.skysidestudio.com/help_1.html",NULL,NULL,SW_SHOW );
					break;
				}
				if(strlen(g_gameName)==0)
				{
					MessageBox(hwnd,"启用SRP插件功能失败，请先加载LIB文件","提示",MB_OK);  
					break;
				}
				InitSRP(hDLLDrvSRP,g_gameName);
				SRPUsed = 1;
				
				MessageBox(hwnd,"启用SRP插件功能成功，请在对话框界面按E修改对话","提示",MB_OK);  
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,2);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_GMC,MF_CHECKED  );
				WritePrivateProfileString( "Plugin","SRPUsed", "1", absolutepath );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
			}
			else
			{
				SRPUsed = 0;
				
				if(hDLLDrvSRP)FreeLibrary(hDLLDrvSRP);
				MessageBox(hwnd,"停用SRP插件功能成功","提示",MB_OK);  
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,2);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_GMC,MF_UNCHECKED  );
				WritePrivateProfileString( "Plugin","SRPUsed", "0", absolutepath );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
			}
			break;
		case IDM_GMC:
			if(GMCUsed == 0)
			{
				char workpath[MAX_PATH];   
				GetModuleFileName(NULL,(LPSTR)workpath,sizeof(workpath));   
				char *p = strrchr(workpath, '\\');  
				*p=0x00;  
				char tPath[MAX_PATH];
				sprintf(tPath,"%s\\Res\\GMC.dll",workpath);
				hDLLDrv = LoadLibrary(tPath);
				if(hDLLDrv==0)
				{
					MessageBox(hwnd,"启用GMC插件功能失败，请下载GMC扩展库到Res文件夹下","提示",MB_OK);  
					ShellExecute(hwnd,"open","http://sim.skysidestudio.com/help_1.html",NULL,NULL,SW_SHOW );
					break;
				}
				InitGMC(hDLLDrv);
				GMCUsed = 1;
				
				MessageBox(hwnd,"启用GMC插件功能成功","提示",MB_OK);  
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,2);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_GMC,MF_CHECKED  );
				WritePrivateProfileString( "Plugin", "GMCUsed","1", absolutepath );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
			}
			else
			{
				GMCUsed = 0;
				
				if(hDLLDrv)FreeLibrary(hDLLDrv);
				MessageBox(hwnd,"停用GMC插件功能成功","提示",MB_OK);  
				HMENU hMenu = GetMenu(hwnd);
				HMENU hSubMenu = GetSubMenu(hMenu,2);
				HMENU hSubMenuA = GetSubMenu(hSubMenu,1);
				CheckMenuItem ( hSubMenuA,IDM_GMC,MF_UNCHECKED  );
				WritePrivateProfileString( "Plugin","GMCUsed", "0", absolutepath );
				DeleteObject(&hSubMenuA);
				DeleteObject(&hSubMenu);
				DeleteObject(&hMenu);
			}
			break;
		}
	case WM_TIMER:
		{
			if(game.ng == 1)
			{
				if(game.gameMap.scriptEng.DelTimerEvent()==GAMEOVER_FLAG)
				{
					game.ng=0;
					game.ChoiceShow();
				}
			}
		}
	case WM_KEYDOWN:
		{			
			if(SRPUsed == 1 && wParam == 69 )//20171003 SRP修改对话
			{
				SRPShowSayBox(hDLLDrvSRP,hwnd);
				
			}
			else
			{
				game.KeyDown(wParam);
			}
			_CrtDumpMemoryLeaks();
		}

	default:
		return DefWindowProc (hwnd, uMsg, 
			wParam, lParam) ;
	}
	return 0;
}
