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

📄 versionthread.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
字号:
/************************************************************************************
	Copyright (c) 2000 Aaron O'Neil
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

		1) Redistributions of source code must retain the above copyright notice, 
				this list of conditions and the following disclaimer.
		2) Redistributions in binary form must reproduce the above copyright notice, 
				this list of conditions and the following disclaimer in the documentation
				and/or other materials provided with the distribution.
		3) Redistributions in binary form must reproduce the above copyright notice on
				program startup. Additional credits for program modification are acceptable
				but original copyright and credits must be visible at startup.
		4) You may charge a reasonable copying fee for any distribution of Mud Master. 
				You may charge any fee you choose for support of Mud Master. You may not 
				charge a fee for Mud Master itself.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************************/

#include "StdAfx.h"
#include "Extern.h"
#include "VersionThread.h"
#include "LogLine.h"

void SplitVersion(CString &strData, CString &strVersion, UINT &nWeight)
{
	int nIndex;

	nIndex = strData.Find(',');
	if (nIndex == -1)
	{
		strVersion = strData;
		nWeight = 0;
		return;
	}

	strVersion = strData.Left(nIndex);
	nWeight = atoi(strData.Right(strData.GetLength()-nIndex-1));
}

UINT WeightVersion(int nVerMajor, int nVerMinor, int nVerBuild, int nVerBeta)
{
	UINT nResult;
	nResult =  nVerBeta;
	nResult += nVerBuild * 100;
	nResult += nVerMinor * 10000;
	nResult += nVerMajor * 1000000;
	return(nResult);
}

// Lines in this file have a key, followed by a colon, followed by the data.
// V:1						Version of this file.
// r:URL						Address of release version.  These need to be before the R and B.
// b:URL						Address of beta version.
// R:2.6.0					Most current released version.
// B:2.6.0 (Beta 30)		Most current beta version.
// M:text					Message to users. Each line must have a M: in front of it. The whole
//								message is concatenated. All M: appear as the same message.
// * is used as an end of file marker.  Anything after the * is ignored.
void ProcessVersionFile(CHttpFile *pHttpFile, VERSIONTHREADINFO *pInfo, CLogLine &log)
{
	CString strSep;
	CString strBetaAddress;
	CString strReleaseAddress;
	CString strVersion;
	CString strLine;
	CString strData;
	CString strMessage;
	UINT nVersion;
	UINT nCurVersion;

	if (!pHttpFile)
		return;

	pInfo->pszTextToPrint[0] = '\x0';

	nCurVersion = WeightVersion(_config.nVersionMajor,_config.nVersionMinor,
		_config.nVersionBuild,_config.nVersionBeta);

	strSep = "\n======================================================================\n";
	if (pInfo->bLogIt)
		log.LogIt("Calling pHttpFile->ReadString.");
	while(pHttpFile->ReadString(strLine))
	{
		if (pInfo->bLogIt)
			log.LogIt("ReadString succeeded.");
		if (strLine.IsEmpty())
			continue;
		if (strLine == '*')
			break;
		if (strLine.GetLength() < 3 || strLine.GetAt(1) != ':')
			continue;
		strData = strLine.Right(strLine.GetLength()-2);

		switch(strLine.GetAt(0))
		{
			case 'V' :	// Not doing anything with this yet. Version of the version file.
				break;

			case 'r' :	// Address of release version.
				if (pInfo->bLogIt)
					log.LogIt("Received release version address.");
				strReleaseAddress = strData;
				break;

			case 'b' :	// Address of beta version.
				if (pInfo->bLogIt)
					log.LogIt("Received beta version address.");
				strBetaAddress = strData;
				break;

			case 'R' :
				if (pInfo->bLogIt)
					log.LogIt("Received current release version information.");
				SplitVersion(strData,strVersion,nVersion);
				if (nVersion > nCurVersion)
				{
					strcat(pInfo->pszTextToPrint,ANSI_F_BOLDWHITE);
					strcat(pInfo->pszTextToPrint,strSep);
					strcat(pInfo->pszTextToPrint,ANSI_F_BOLDCYAN);
					strcat(pInfo->pszTextToPrint,"There is a new Release version available for download - ");
					strcat(pInfo->pszTextToPrint,strVersion);
					strcat(pInfo->pszTextToPrint,"\n\nYou can download it at:\n");
					strcat(pInfo->pszTextToPrint,strReleaseAddress);
					strcat(pInfo->pszTextToPrint,ANSI_F_BOLDWHITE);
					strcat(pInfo->pszTextToPrint,strSep);
				}
				break;

			case 'B' :
				if (pInfo->bLogIt)
					log.LogIt("Received current beta version information.");
				SplitVersion(strData,strVersion,nVersion);
				if (nVersion > nCurVersion)
				{
					strcat(pInfo->pszTextToPrint,ANSI_F_BOLDWHITE);
					strcat(pInfo->pszTextToPrint,strSep);
					strcat(pInfo->pszTextToPrint,ANSI_F_BOLDCYAN);
					strcat(pInfo->pszTextToPrint,"There is a new Beta version available for download - ");
					strcat(pInfo->pszTextToPrint,strVersion);
					strcat(pInfo->pszTextToPrint,"\n\nYou can download it at:\n");
					strcat(pInfo->pszTextToPrint,strBetaAddress);
					strcat(pInfo->pszTextToPrint,ANSI_F_BOLDWHITE);
					strcat(pInfo->pszTextToPrint,strSep);
				}
				else
					if (nVersion < nCurVersion)
					{
						strcat(pInfo->pszTextToPrint,ANSI_F_BOLDWHITE);
						strcat(pInfo->pszTextToPrint,strSep);
						strcat(pInfo->pszTextToPrint,ANSI_F_BOLDCYAN);
						strcat(pInfo->pszTextToPrint,"Congratulations! Your version is newer than the one available for download!");
						strcat(pInfo->pszTextToPrint,ANSI_F_BOLDWHITE);
						strcat(pInfo->pszTextToPrint,strSep);
					}
				break;

			case 'M' :
				if (pInfo->bLogIt)
					log.LogIt("Received Mud Master message line.");
				if (!strMessage.IsEmpty())
					strMessage += "\n";
				strMessage += strData;
				break;
		}
		if (pInfo->bLogIt)
			log.LogIt("Calling pHttpFile->ReadString.");
	} 

	if (!strMessage.IsEmpty())
	{
		strcat(pInfo->pszTextToPrint,ANSI_F_BOLDWHITE);
		strcat(pInfo->pszTextToPrint,"\n=== Mud Master Message ===\n");
		strcat(pInfo->pszTextToPrint,ANSI_F_BOLDCYAN);
		strcat(pInfo->pszTextToPrint,strMessage);
		strcat(pInfo->pszTextToPrint,ANSI_F_BOLDWHITE);
		strcat(pInfo->pszTextToPrint,"\n==========================\n");
	}

	if (pInfo->bLogIt)
		log.LogIt("Done processing version information.");
}


UINT VersionThreadProc(LPVOID pVoid)
{
	VERSIONTHREADINFO *pThreadInfo;
	CHttpConnection *pHttpServer = NULL;
	CHttpFile *pHttpFile = NULL;
	DWORD dwServiceType;
	CString strServerName;
	CString strObject;
	INTERNET_PORT nPort;
	CLogLine log;

	pThreadInfo = (VERSIONTHREADINFO*)pVoid;

	pThreadInfo->bLogIt = log.SetFile(VERSIONLOG);
	if (pThreadInfo->bLogIt)
		log.LogIt("Version thread started.");

	try
	{
		if (pThreadInfo->bLogIt)
			log.LogIt("Creating CInternetSession object.");
		CInternetSession isVersion("MM");

		if (pThreadInfo->bLogIt)
			log.LogIt("Calling AfxParseURL.");
		if (!AfxParseURL("http://www.mud-master.com/Version.txt", 
			dwServiceType, strServerName, strObject, nPort))
		{
			if (pThreadInfo->bLogIt)
				log.LogIt("AfxParseURL failed.");
			pThreadInfo->nThreadStatus = THREAD_VERSION_QUIT;
			AfxEndThread(1);
		}
		if (pThreadInfo->bLogIt)
			log.LogIt("Calling GetHttpConnection.");
		pHttpServer = isVersion.GetHttpConnection(strServerName,nPort);
		if (!pHttpServer)
		{
			if (pThreadInfo->bLogIt)
				log.LogIt("GetHttpConnection failed.");
			isVersion.Close();
			pThreadInfo->nThreadStatus = THREAD_VERSION_QUIT;
			AfxEndThread(1);
		}
		if (pThreadInfo->bLogIt)
			log.LogIt("Calling OpenRequest.");
		pHttpFile = pHttpServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
			strObject,NULL,1,NULL,NULL,
			INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);
		if (!pHttpFile)
		{
			if (pThreadInfo->bLogIt)
				log.LogIt("OpenRequest failed.");
			pHttpServer->Close();
			delete pHttpServer;
			isVersion.Close();
			pThreadInfo->nThreadStatus = THREAD_VERSION_QUIT;
			AfxEndThread(1);
		}
		if (pThreadInfo->bLogIt)
			log.LogIt("Calling AddRequestHeaders.");
		if (!pHttpFile->AddRequestHeaders("Accept: text/*\r\n"))
		{
			if (pThreadInfo->bLogIt)
				log.LogIt("AddRequestHeaders failed.");
			pHttpServer->Close();
			delete pHttpServer;
			pHttpFile->Close();
			delete pHttpFile;
			isVersion.Close();
			pThreadInfo->nThreadStatus = THREAD_VERSION_QUIT;
			AfxEndThread(1);
		}
		if (pThreadInfo->bLogIt)
			log.LogIt("Calling SetReadBufferSize.");
		pHttpFile->SetReadBufferSize(VERSION_BUFFER);
		if (pThreadInfo->bLogIt)
			log.LogIt("Calling SendRequest.");		
		pHttpFile->SendRequest();
		if (pThreadInfo->bLogIt)
			log.LogIt("Processing version file.");
		ProcessVersionFile(pHttpFile,pThreadInfo,log);
		if (pThreadInfo->bLogIt)
			log.LogIt("Closing HTTP file.");
		if (pHttpFile)
			pHttpFile->Close();
		delete pHttpFile;
		if (pThreadInfo->bLogIt)
			log.LogIt("Closing server connection.");
		if (pHttpServer)
			pHttpServer->Close();
		delete pHttpServer;
		isVersion.Close();
	}
	catch(CInternetException *pEx)
	{
		if (pThreadInfo->bLogIt)
		{
			CString strTemp;
			strTemp.Format("CInternetException thrown. m_dwContext = %d, m_dwError = %d",
				pEx->m_dwContext, pEx->m_dwError);
			log.LogIt(strTemp);
		}
		if (pHttpFile)
			pHttpFile->Close();
		delete pHttpFile;
		if (pHttpServer)
			pHttpServer->Close();
		delete pHttpServer;
		pEx->Delete();
		pThreadInfo->nThreadStatus = THREAD_VERSION_QUIT;
		AfxEndThread(1);
	}

	pThreadInfo->nThreadStatus = THREAD_VERSION_DONE;
	AfxEndThread(0);
	return(0);
}

⌨️ 快捷键说明

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