www.pudn.com > uoth_src.zip > RuneLibrary.cpp


//----------------------------------------------------------------------------- 
//  
// @doc 
// 
// @module	RuneLibrary.cpp - RuneLibrary information class | 
// 
// This module contains the RuneLibrary information class. 
// 
// Copyright (c) 2002 - Descartes Systems Sciences, Inc. 
// 
// 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. Neither the name of Descartes Systems Sciences, Inc nor the names of  
//    its contributors may be used to endorse or promote products derived  
//    from this software without specific prior written permission. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  
// "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 COPYRIGHT  
// OWNER OR CONTRIBUTORS 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. 
// 
// @end 
// 
// $History: Cnf.cpp $ 
//       
//----------------------------------------------------------------------------- 
 
#include "stdafx.h" 
#include "RuneLibrary.h" 
#include "DataParser.h" 
#include "uoth.h" 
 
// 
// Debug NEW 
// 
 
#if defined (_DEBUG) 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
// 
// Externals 
// 
 
extern TCHAR g_szSettingsFile []; 
extern HWND g_hWndMain; 
 
// 
// Class globals 
// 
 
CAtlArray  CRuneLibrary::gm_vRuneLibraries; 
 
// 
// Locals 
// 
 
static int s_nCurrent = -1; 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  constructor. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CRuneLibrary::CRuneLibrary () 
{ 
	Init (true); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc  destructor. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
CRuneLibrary::~CRuneLibrary () 
{ 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Initialize the object 
// 
// @parm bool | fConstruct | If true, we are being called from the 
//		the constructor 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::Init (bool fConstruct) 
{ 
	m_fPerm = false; 
	m_strName .Empty (); 
	m_vRuneBooks .RemoveAll (); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Begin the processing of a RuneLibrary 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char ** | papszAttrs | Attributes 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::OnStart (CDataParser *pParser, const XML_Char **papszAttrs) 
{ 
 
	// 
	// No attributes are allowed 
	// 
 
	if (papszAttrs [0] != NULL) 
	{ 
		pParser ->SetError (IDS_ERR_XML_NO_ATTRS); 
		return; 
	} 
 
	// 
	// Initialize the RuneLibrary 
	// 
 
	Init (false); 
 
	// 
	// Move to the new state 
	// 
 
	pParser ->SetState (CDataParser::InRuneLibrary); 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Begin the processing of an XML element 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char * | pszName | Name of the element 
// 
// @parm const XML_Char ** | papszAttrs | Attributes 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::OnStartElement (CDataParser *pParser,  
	const XML_Char *pszName, const XML_Char **papszAttrs) 
{ 
	// 
	// Get the element 
	// 
 
	_Element nElement = GetElement (pszName); 
	if (nElement == Unknown) 
	{ 
		pParser ->SetError (IDS_ERR_XML_UNEXPECTED_ELEMENT); 
		return; 
	} 
 
	// 
	// No attributes are allowed 
	// 
 
	if (papszAttrs [0] != NULL) 
	{ 
		pParser ->SetError (IDS_ERR_XML_NO_ATTRS); 
		return; 
	} 
 
	// 
	// If this is a rune book, change state 
	// 
 
	if (nElement == RuneBook) 
	{ 
		pParser ->m_sRuneBook .OnStart (pParser, papszAttrs); 
	} 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc End an XML element 
// 
// @parm CDataParser * | pParser | Current parser 
// 
// @parm const XML_Char * | pszName | Name of the element 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::OnEndElement (CDataParser *pParser, const XML_Char *pszName) 
{ 
 
	// 
	// Get the element 
	// 
 
	_Element nElement = GetElement (pszName); 
	if (nElement == Unknown) 
	{ 
		pParser ->SetError (IDS_ERR_XML_UNEXPECTED_ELEMENT); 
		return; 
	} 
 
	// 
	// Switch based on the element 
	// 
 
	switch (nElement) 
	{ 
		 
		// 
		// If we are ending the RuneLibrary 
		// 
 
		case RuneLibrary: 
			pParser ->SetState (CDataParser::InData); 
			gm_vRuneLibraries .Add (*this); 
			break; 
 
		// 
		// If this is the name 
		// 
 
		case Name: 
			m_strName = pParser ->GetText (false); 
			break; 
	} 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Get the element number 
// 
// @parm const XML_Char * | pszName | Element name 
// 
// @rdesc Element id. 
// 
//----------------------------------------------------------------------------- 
 
CRuneLibrary::_Element CRuneLibrary::GetElement (const XML_Char *pszName) 
{ 
	if (stricmp (pszName, "RuneLibrary") == 0) 
		return RuneLibrary; 
	else if (stricmp (pszName, "Name") == 0) 
		return Name; 
	else if (stricmp (pszName, "RuneBook") == 0) 
		return RuneBook; 
	else 
		return Unknown; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Return the current rune library 
// 
// @rdesc Pointer to the current rune library or NULL if there is none 
// 
//----------------------------------------------------------------------------- 
 
CRuneLibrary *CRuneLibrary::GetCurrent () 
{ 
	if (s_nCurrent >= 0) 
		return &gm_vRuneLibraries [s_nCurrent]; 
	else 
		return NULL; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Set the current rune library  
// 
// @parm CRuneLibrary * | pRuneLibrary | Pointer to the character 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::SetCurrent (CRuneLibrary *pRuneLibrary) 
{ 
 
	// 
	// Set the rune library 
	// 
 
	s_nCurrent = pRuneLibrary - &gm_vRuneLibraries [0]; 
	 
	// 
	// Reset the has maps 
	// 
 
	ApplyFilter (); 
 
	// 
	// Save the setting 
	// 
 
	::WritePrivateProfileString (PROFILE_SETTINGS, PROFILE_RUNELIBRARY,  
		pRuneLibrary ? (LPCTSTR) pRuneLibrary ->m_strName : _T ( ""),  
		g_szSettingsFile); 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Get the default rune library from the profile 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::GetDefaultCurrent () 
{ 
 
	// 
	// Get the current rune library 
	// 
 
	TCHAR szText [256]; 
	::GetPrivateProfileString (PROFILE_SETTINGS, PROFILE_RUNELIBRARY, 
		_T ( ""), szText, _countof (szText), g_szSettingsFile); 
	szText [_countof (szText) - 1] = 0; 
 
	// 
	// Locate the rune library 
	// 
 
	CRuneLibrary *pRuneLibrary = Find (szText); 
 
	// 
	// Based on the results of the find, select the default 
	// 
 
	if (pRuneLibrary == NULL) 
	{ 
		if (gm_vRuneLibraries .GetCount () > 0) 
			s_nCurrent = 0; 
		else 
			s_nCurrent = -1; 
	} 
	else 
		s_nCurrent = pRuneLibrary - &gm_vRuneLibraries [0]; 
	ApplyFilter (); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Locate a rune library 
// 
// @parm LPCTSTR | pszName | Name of the rune library 
// 
// @rdesc Pointer to the rune library or NULL if not found 
// 
//----------------------------------------------------------------------------- 
 
CRuneLibrary *CRuneLibrary::Find (LPCTSTR pszName) 
{ 
	for (int irl = 0; irl < gm_vRuneLibraries .GetCount (); ++irl) 
	{ 
		if (_tcsicmp (gm_vRuneLibraries [irl] .m_strName, pszName) == 0) 
			return &gm_vRuneLibraries [irl]; 
	} 
	return NULL; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Add a rune library 
// 
// @parm LPCTSTR | pszName | Name of the rune library 
// 
// @rdesc Pointer to the rune library  
// 
//----------------------------------------------------------------------------- 
 
CRuneLibrary *CRuneLibrary::Add (LPCTSTR pszName) 
{ 
 
	// 
	// Try to find the rune library 
	// 
 
	CRuneLibrary *pRuneLibrary = Find (pszName); 
	if (pRuneLibrary != NULL)  
		return pRuneLibrary; 
 
	// 
	// Create a new rune library 
	// 
 
	CRuneLibrary c; 
	c .m_strName = pszName; 
	gm_vRuneLibraries .Add (c); 
	return &gm_vRuneLibraries [gm_vRuneLibraries .GetCount () - 1]; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Delete a rune library 
// 
// @parm CRuneLibrary * | pRuneLibrary | Library to delete.  This address 
//		must be within the array of rune libraries. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::Delete (CRuneLibrary *pRuneLibrary) 
{ 
 
	// 
	// Adjust the current index 
	// 
 
	int nDelete = pRuneLibrary - &gm_vRuneLibraries [0]; 
	if (s_nCurrent < nDelete) 
		; 
	else if (s_nCurrent == nDelete) 
		s_nCurrent = -1; 
	else  
		s_nCurrent--; 
 
	// 
	// Delete the rune library 
	// 
 
	gm_vRuneLibraries .RemoveAt (nDelete); 
 
	// 
	// Update the current 
	// 
 
	SetCurrent (GetCurrent ()); 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Write to a stream 
// 
// @parm FILE * | fp | Output file 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::Write (FILE *fp) 
{ 
	fprintf (fp, "\t\n"); 
	fprintf (fp, "\t\t%s\n", (LPCTSTR) m_strName); 
	for (int irb = 0; irb < m_vRuneBooks .GetCount (); ++irb) 
		m_vRuneBooks [irb] .Write (fp); 
	fprintf (fp, "\t\n"); 
	return; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Locate a rune book 
// 
// @parm LPCTSTR | pszName | Name of the rune book 
// 
// @rdesc Pointer to the rune book or NULL if not found 
// 
//----------------------------------------------------------------------------- 
 
CRuneBook *CRuneLibrary::FindBook (LPCTSTR pszName) 
{ 
	for (int irb = 0; irb < m_vRuneBooks .GetCount (); ++irb) 
	{ 
		if (_tcsicmp (m_vRuneBooks [irb] .m_strName, pszName) == 0) 
			return &m_vRuneBooks [irb]; 
	} 
	return NULL; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Add a rune book 
// 
// @parm LPCTSTR | pszName | Name of the rune book 
// 
// @rdesc Pointer to the rune book  
// 
//----------------------------------------------------------------------------- 
 
CRuneBook *CRuneLibrary::AddBook (LPCTSTR pszName) 
{ 
 
	// 
	// Try to find the rune book 
	// 
 
	CRuneBook *pRuneBook = FindBook (pszName); 
	if (pRuneBook != NULL)  
		return pRuneBook; 
 
	// 
	// Create a new rune book 
	// 
 
	CRuneBook c; 
	c .m_strName = pszName; 
	m_vRuneBooks .Add (c); 
	pRuneBook = &m_vRuneBooks [m_vRuneBooks .GetCount () - 1]; 
	return pRuneBook; 
} 
 
//----------------------------------------------------------------------------- 
// 
// @mfunc Delete a rune book 
// 
// @parm CRuneBook * | pRuneBook | Book to delete.  This address 
//		must be within the array of rune books. 
// 
// @rdesc None. 
// 
//----------------------------------------------------------------------------- 
 
void CRuneLibrary::DeleteBook (CRuneBook *pRuneBook) 
{ 
 
	// 
	// Delete the rune book 
	// 
 
	m_vRuneBooks .RemoveAt (pRuneBook - &m_vRuneBooks [0]); 
}