⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamesetting.cpp

📁 是一个基于热血战国协议的网络游戏。现在脱机客户端先放出来给大家研究
💻 CPP
字号:

/*
* name:		GameSetting.cpp
*
* desc:		这个是读取配置文件的类
*
*/

#include "StdAfx.h"
#include "GameSetting.h"
#include "GameMir.h"

/*
=======================================================================
函数名       : Load
功能描述     : 根据服务器和名字,得到文件夹,导入游戏设置文件
参数         : void
返回值       : NULL
=======================================================================
*/
void CGameSetting::Load(std::string server, std::string character)
{
	char buf[MAX_PATH];
	ZeroMemory(buf,sizeof(buf));
	GetModuleFileName(0,buf,sizeof(buf));
	long len=strlen(buf);
	while ( len>0 )
	{
		if ( buf[len]=='\\' )
		{
			buf[len]=static_cast<char>(0);
			break;
		}
		--len;
	}

	WIN32_FIND_DATA wfd;
	m_Filename=buf;
	m_Filename+="\\Settings\\";
	if (!CreateDirectory(m_Filename.c_str(),NULL)) 
		if(GetLastError()!=ERROR_ALREADY_EXISTS)
			return;
	m_Filename+=server+"_"+character+"\\";
	if (!CreateDirectory(m_Filename.c_str(),NULL))
		if(GetLastError()!=ERROR_ALREADY_EXISTS)
			return;
	m_Filename+="main.ini";
	strcat(buf,"\\Settings\\default\\main.ini");
	CopyFile(buf,m_Filename.c_str(),TRUE);

	UINT uOpenFlag;
	uOpenFlag=CFile::typeBinary|CFile::modeRead|CFile::shareExclusive;
	HANDLE hFind=FindFirstFile(m_Filename.c_str(),&wfd);
	if ( hFind==INVALID_HANDLE_VALUE )
	{
		uOpenFlag|=CFile::modeCreate;
	}
	else
		FindClose(hFind);

	CFile setting_file(m_Filename.c_str(),uOpenFlag);
	long flen=setting_file.GetLength();
	char*pBuf=new char[flen+1];
	ZeroMemory(pBuf,flen+1);
	setting_file.Read(pBuf,flen);
	pBuf[flen]=static_cast<char>(0);

	std::wstring text;
	LPWSTR UBuf=new WCHAR[flen+1];;
	text=ToUnicode(pBuf,UBuf,flen);
	delete[]pBuf;
	delete[]UBuf;
	std::vector<std::wstring> Lines;
	const boost::wregex reSplit(L"\\s*\\n\\s*",boost::regbase::normal|boost::regbase::icase);
	const boost::wregex reSet(L"^(.+?)\\s+\"?(.*?)\"?$",boost::regbase::normal|boost::regbase::icase);
	boost::wsmatch match;
	boost::regex_split(std::back_inserter(Lines),text,reSplit);
	m_Settings.clear();
	std::pair<std::string,std::string> key_value;
	char cBuf[STRING_BUFFER_LENGTH+1];
	for(std::vector<std::wstring>::iterator pos=Lines.begin();pos!=Lines.end();pos++)
	{
		if (boost::regex_match( *pos, match, reSet))
		{
			key_value.first=ToAnsi(match.str(1).c_str(),cBuf,STRING_BUFFER_LENGTH);
			key_value.second=ToAnsi(match.str(2).c_str(),cBuf,STRING_BUFFER_LENGTH);
			m_Settings.insert(key_value);
		}
	}
}

/*
=======================================================================
函数名       : Save
功能描述     : 把配置参数写入到保存游戏设置文件
参数         : void
返回值       : NULL
=======================================================================
*/
void CGameSetting::Save( void )
{
	UINT uOpenFlag;
	uOpenFlag=CFile::modeCreate|CFile::typeBinary|CFile::modeWrite|CFile::shareExclusive;

	CFile setting_file(m_Filename.c_str(),uOpenFlag);

	char buf[1024];

	for(std::map<std::string,std::string>::iterator pos=m_Settings.begin();pos!=m_Settings.end();pos++)
	{
		int len=sprintf(buf,"%s \"%s\"\x0d\x0a",pos->first.c_str(),pos->second.c_str());
		setting_file.Write(buf,len);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -