PGE API 0.4
PR00F's Game Engine full documentation
Loading...
Searching...
No Matches
PGEcfgProfiles Class Reference

PR00F's Game Engine profile config handler class. More...

Detailed Description

PR00F's Game Engine profile config handler class.

Definition at line 25 of file PGEcfgProfiles.h.

#include <PGEcfgProfiles.h>

+ Inheritance diagram for PGEcfgProfiles:

Public Member Functions

 PGEcfgProfiles ()
 
virtual ~PGEcfgProfiles ()
 
CConsole & getConsole () const
 Returns access to console preset with logger module name as this class.
 
bool reinitialize (const char *gameTitle)
 Resets initial state.
 
void shutdown ()
 Resets everything.
 
std::string getLangFileName () const
 Returns the file name of the selected language file.
 
int readLanguageData (std::string **&langTable) const
 Reads the language file into the given table.
 
bool areProfilesInMyDocs () const
 Returns whether player profiles are stored in user documents folder.
 
std::string getPathToProfiles () const
 Returns the path to the profiles.
 
int getProfilesCount () const
 Returns the number of found profiles.
 
const std::string ** getProfilesList () const
 Returns the found profile names.
 
const std::string ** getProfilePlayersList () const
 Returns the found player names in profiles.
 
int addProfile (const char *sUser, const char *sNick)
 Creates a new profile.
 
int deleteProfile (int nIndex)
 Deletes the specified profile.
 
void ProcessCommandLine (const char *szCmdLine)
 Application can pass command line to be parsed for extra one-time configuration.
 
const std::map< std::string, PGEcfgVariable > & getCommandLineVars () const
 
int getProfileIndex () const
 Gets index of active profile.
 
const std::string getProfileName () const
 Gets name of active profile.
 
bool setProfile (int nIndex)
 Sets active profile by index.
 
bool setProfile (const std::string &sProfileName)
 Sets active profileby name.
 
void DeleteVar (const char *varName)
 Deletes a cvar by the given name.
 
bool readConfiguration ()
 Loads configuration for current profile from file.
 
bool writeConfiguration ()
 Saves configuration for current profile to file.
 
- Public Member Functions inherited from PGEcfgFile
 PGEcfgFile (bool bRequireAllAcceptedVarsDefineRequirement, bool bCaseSensitiveVars)
 
virtual ~PGEcfgFile ()
 
CConsole & getConsole () const
 Returns access to console preset with logger module name as this class.
 
std::map< std::string, PGEcfgVariable > & getVars ()
 
const std::map< std::string, PGEcfgVariable > & getVars () const
 
bool load (const char *fname)
 Loads variables from the given config file.
 
bool save (const char *fname="") const
 Saves variables to the given config file.
 
const std::string & getFilename () const
 
const std::string & getPathToFile () const
 
bool getAllAcceptedVarsDefineRequirement () const
 
bool getCaseSensitiveVars () const
 
const std::set< std::string > & getAcceptedVars () const
 
bool setAcceptedVars (const std::set< std::string > &newAcceptedVars)
 
std::vector< std::string > & getTemplate ()
 Returns the lines of the template generated from the loaded config file.
 
const std::vector< std::string > & getTemplate () const
 Returns the lines of the template generated from the loaded config file.
 
 PGEcfgFile (const PGEcfgFile &other)
 
PGEcfgFileoperator= (const PGEcfgFile &other)
 

Static Public Member Functions

static const char * getLoggerModuleName ()
 Returns the logger module name of this class.
 
static std::string getMyDocsFolder ()
 Returns the path to user documents.
 
- Static Public Member Functions inherited from PGEcfgFile
static const char * getLoggerModuleName ()
 Returns the logger module name of this class.
 

Protected Member Functions

virtual bool validateOnLoad (std::ifstream &f) const override
 Validate the file being processed by load().
 
virtual bool validateOnSave (std::ofstream &f) const override
 Validate the file being processed by save().
 
std::map< std::string, PGEcfgVariable > & getCommandLineVars ()
 
- Protected Member Functions inherited from PGEcfgFile

Private Member Functions

 PGEcfgProfiles (const PGEcfgProfiles &)
 
PGEcfgProfilesoperator= (const PGEcfgProfiles &)
 
bool getPlayerNameFromFile (const char *cFilename, std::string &sPlayerName)
 Gets player name from given cfg file.
 
void LoadProfilesList ()
 Fills up sFoundProfiles and sFoundProfilePlayerNames.
 
void ClearVars ()
 Removes all cvars.
 

Static Private Member Functions

static bool findMyDocsFolder ()
 Stores user docs folder path in sPathToMyDocs.
 

Private Attributes

std::string sLangFileName
 Filename of language file.
 
bool bMainCFGinMyDocs
 Do we store cfg file in user documents folder?
 
std::string sPathToProfiles
 Search path for profiles based on bMainCFGinMyDocs and pPathToMyDocs.
 
int nProfilesCount
 Number of found profiles.
 
std::string ** sFoundProfiles
 Directory names of found profiles (just add ".cfg" for profile file).
 
std::string ** sFoundProfilePlayerNames
 Found profile names (player names).
 
int nActiveProfile
 Index of active profile.
 
std::string m_sActiveProfile
 Name of active profile.
 
PGEcfgVariable cvarWrong
 This is returned when trying to access an invalid cvar.
 
std::map< std::string, PGEcfgVariablem_commandLineVars
 Any added variables parsed by ProcessCommandLine().
 

Static Private Attributes

static std::string sPathToMyDocs
 Path to user documents.
 

Additional Inherited Members

- Static Protected Member Functions inherited from PGEcfgFile
static bool lineIsValueAssignment (const std::string &sTrimmedLine, bool bCaseSensitiveVars, std::string &sVar, std::string &sValue, bool &bParseError)
 
- Protected Attributes inherited from PGEcfgFile
std::set< std::string > m_acceptedVars
 
std::map< std::string, PGEcfgVariablem_vars
 

Constructor & Destructor Documentation

◆ PGEcfgProfiles() [1/2]

PGEcfgProfiles::PGEcfgProfiles ( )
explicit

Definition at line 22 of file PGEcfgProfiles.cpp.

◆ ~PGEcfgProfiles()

PGEcfgProfiles::~PGEcfgProfiles ( )
virtual

Definition at line 33 of file PGEcfgProfiles.cpp.

◆ PGEcfgProfiles() [2/2]

PGEcfgProfiles::PGEcfgProfiles ( const PGEcfgProfiles & other)
private

Definition at line 752 of file PGEcfgProfiles.cpp.

Member Function Documentation

◆ addProfile()

int PGEcfgProfiles::addProfile ( const char * sUser,
const char * sNick )

Creates a new profile.

This creates a profile directory and a profile file in it immediately. Only the mandatory CVAR containing the nickname of the profile is saved to the file, no other CVARs. You have to explicitly call writeConfiguration() to save all variables based on the config template.

Creating a new profile won't change the current profile. You have to manually switch to it instead. The current profile index may change but it will still index the original active profile.

Parameters
sUserThe username for the profile, which will be used primarily for the filename. Thus some characters are restricted (?, *, etc ...). Cannot be empty.
sNickThe nickname / player name for the profile, which can hold any character. This is what is shown in a game, for example in frag table. Cannot be empty.
Returns
Non-negative index of the new profile on success. -1 if a profile already exists with the given user name. -2 if a profile can't be created for some other reason. -3 getPathToProfiles() is empty, maybe forgot to call reinitialize() ?

Definition at line 310 of file PGEcfgProfiles.cpp.

◆ areProfilesInMyDocs()

bool PGEcfgProfiles::areProfilesInMyDocs ( ) const

Returns whether player profiles are stored in user documents folder.

Example: if true, MyDocs/<GAMENAME>/profiles, otherwise program folder/gamedata/profiles.

Definition at line 246 of file PGEcfgProfiles.cpp.

◆ ClearVars()

void PGEcfgProfiles::ClearVars ( )
private

Removes all cvars.

Removes all cvariables.

Definition at line 972 of file PGEcfgProfiles.cpp.

◆ deleteProfile()

int PGEcfgProfiles::deleteProfile ( int nIndex)

Deletes the specified profile.

This deletes the profile directory and the profile file in it immediately. If the given index is the current profile, the current profile index will be -1 after deletion. Otherwise, the current profile and any loaded cvars won't be changed. The current profile index may change.

Parameters
nIndexThe index of the profile to be deleted. Only non-negative values are accepted.
Returns
0 on success. -1 if invalid index was given. -2 if the profile exists but can't be deleted for some reason. -3 profile file deleted but its directory could not be deleted.

Definition at line 405 of file PGEcfgProfiles.cpp.

◆ DeleteVar()

void PGEcfgProfiles::DeleteVar ( const char * varName)

Deletes a cvar by the given name.

You cannot delete the cvar holding the name of the player because it would invalidate the profile. If you delete a cvar from the current profile, it will be deleted only on memory-level. To save changes made to the current profile, use writeConfiguration().

Definition at line 640 of file PGEcfgProfiles.cpp.

◆ findMyDocsFolder()

bool PGEcfgProfiles::findMyDocsFolder ( )
staticprivate

Stores user docs folder path in sPathToMyDocs.

Returns
True if successful, false on failure.

Definition at line 769 of file PGEcfgProfiles.cpp.

◆ getCommandLineVars() [1/2]

std::map< std::string, PGEcfgVariable > & PGEcfgProfiles::getCommandLineVars ( )
protected

Definition at line 740 of file PGEcfgProfiles.cpp.

◆ getCommandLineVars() [2/2]

const std::map< std::string, PGEcfgVariable > & PGEcfgProfiles::getCommandLineVars ( ) const

Definition at line 496 of file PGEcfgProfiles.cpp.

◆ getConsole()

CConsole & PGEcfgProfiles::getConsole ( ) const

Returns access to console preset with logger module name as this class.

Intentionally not virtual, so the getConsole() in derived class should hide this instead of overriding.

Returns
Console instance used by this class.

Definition at line 46 of file PGEcfgProfiles.cpp.

◆ getLangFileName()

string PGEcfgProfiles::getLangFileName ( ) const

Returns the file name of the selected language file.

Definition at line 165 of file PGEcfgProfiles.cpp.

◆ getLoggerModuleName()

const char * PGEcfgProfiles::getLoggerModuleName ( )
static

Returns the logger module name of this class.

Intentionally not virtual, so derived class should hide this instead of overriding. Not even private, so user can also access this from outside, for any reason like controlling log filtering per logger module name.

Returns
The logger module name of this class.

Definition at line 59 of file PGEcfgProfiles.cpp.

◆ getMyDocsFolder()

string PGEcfgProfiles::getMyDocsFolder ( )
static

Returns the path to user documents.

Returns
The absolute path to user documents directory. There is always a delimiter character at the end.

Definition at line 69 of file PGEcfgProfiles.cpp.

◆ getPathToProfiles()

string PGEcfgProfiles::getPathToProfiles ( ) const

Returns the path to the profiles.

Returns
The absolute path to game profiles directory. There is always a delimiter character at the end.

Definition at line 256 of file PGEcfgProfiles.cpp.

◆ getPlayerNameFromFile()

bool PGEcfgProfiles::getPlayerNameFromFile ( const char * cFilename,
std::string & sPlayerName )
private

Gets player name from given cfg file.

Definition at line 798 of file PGEcfgProfiles.cpp.

◆ getProfileIndex()

int PGEcfgProfiles::getProfileIndex ( ) const

Gets index of active profile.

Definition at line 505 of file PGEcfgProfiles.cpp.

◆ getProfileName()

const std::string PGEcfgProfiles::getProfileName ( ) const

Gets name of active profile.

Definition at line 513 of file PGEcfgProfiles.cpp.

◆ getProfilePlayersList()

const string ** PGEcfgProfiles::getProfilePlayersList ( ) const

Returns the found player names in profiles.

Definition at line 283 of file PGEcfgProfiles.cpp.

◆ getProfilesCount()

int PGEcfgProfiles::getProfilesCount ( ) const

Returns the number of found profiles.

Definition at line 265 of file PGEcfgProfiles.cpp.

◆ getProfilesList()

const string ** PGEcfgProfiles::getProfilesList ( ) const

Returns the found profile names.

Definition at line 274 of file PGEcfgProfiles.cpp.

◆ LoadProfilesList()

void PGEcfgProfiles::LoadProfilesList ( )
private

Fills up sFoundProfiles and sFoundProfilePlayerNames.

Counts the found profiles (directories with cfg file inside them with the same name), stores it in nProfilesCount. If the current profile index is valid, it may change during this function but the current profile and loaded cvars remain unchanged.

Definition at line 840 of file PGEcfgProfiles.cpp.

◆ operator=()

PGEcfgProfiles & PGEcfgProfiles::operator= ( const PGEcfgProfiles & )
private

Definition at line 759 of file PGEcfgProfiles.cpp.

◆ ProcessCommandLine()

void PGEcfgProfiles::ProcessCommandLine ( const char * szCmdLine)

Application can pass command line to be parsed for extra one-time configuration.

Definition at line 452 of file PGEcfgProfiles.cpp.

◆ readConfiguration()

bool PGEcfgProfiles::readConfiguration ( )

Loads configuration for current profile from file.

Note that config variables parsed earlier by ProcessCommandLine() will overwrite the variables read from profile config file.

Returns
True on success, false on failure.

Definition at line 658 of file PGEcfgProfiles.cpp.

◆ readLanguageData()

int PGEcfgProfiles::readLanguageData ( std::string **& langTable) const

Reads the language file into the given table.

The given pointer will be valid only if the returned number is positive;

Returns
The number of read language lines.

Definition at line 177 of file PGEcfgProfiles.cpp.

◆ reinitialize()

bool PGEcfgProfiles::reinitialize ( const char * gameTitle)

Resets initial state.

Unloads active profile and resets active profile index to -1 (no active profile selected), and re-reads profiles, language data, etc.

Returns
True on success, false otherwise.

Definition at line 81 of file PGEcfgProfiles.cpp.

◆ setProfile() [1/2]

bool PGEcfgProfiles::setProfile ( const std::string & sProfileName)

Sets active profileby name.

Sets active profile by name.

This also implies reading the configuration data for the given profile. The active profile index will be -1 and active profile name will be empty if the given name is valid but an error occured while reading config data. Note that config variables parsed earlier by ProcessCommandLine() will overwrite the variables read from profile config file.

Parameters
sProfileNameA valid profile name. Specifying an empty string will not load anything but erase all previously loaded config variables.
Returns
True if setting active profile was successful, false otherwise.

Definition at line 586 of file PGEcfgProfiles.cpp.

◆ setProfile() [2/2]

bool PGEcfgProfiles::setProfile ( int nIndex)

Sets active profile by index.

This also implies reading the configuration data for the given profile. The active profile index will be -1 and active profile name will be empty if the given index is valid but an error occured while reading config data. Note that config variables parsed earlier by ProcessCommandLine() will overwrite the variables read from profile config file.

Parameters
nIndexA valid profile index in [0..getProfilesCount()-1] range. Specifying -1 will not load anything but erase all previously loaded config variables. Specifying index less than -1 will log error but leave the configuration unchanged.
Returns
True if setting active profile was successful, false otherwise.

Definition at line 531 of file PGEcfgProfiles.cpp.

◆ shutdown()

void PGEcfgProfiles::shutdown ( )

Resets everything.

Definition at line 137 of file PGEcfgProfiles.cpp.

◆ validateOnLoad()

bool PGEcfgProfiles::validateOnLoad ( std::ifstream & ) const
overrideprotectedvirtual

Validate the file being processed by load().

Derived class can override this to provide a validating function, e.g. special way to decide if the file being loaded actually has valid format. This is invoked by load() before trying to parse the file. The stream must be already opened prior to calling this function. This function must not open/close the given file stream. Upon success, load() will continue reading the file from the point until this function has read the file.

Returns
False to indicate that load() should not process the file and fail. True if load() should continue processing the file.

Reimplemented from PGEcfgFile.

Definition at line 717 of file PGEcfgProfiles.cpp.

◆ validateOnSave()

bool PGEcfgProfiles::validateOnSave ( std::ofstream & ) const
overrideprotectedvirtual

Validate the file being processed by save().

Derived class can override this to provide a validating function which makes the file compatible with the optionally overridden validateOnLoad() function. For example, if validateOnLoad() checks for a special header, then validateOnSave() is responsible for writing that special header into the file. This is invoked by save() when trying to write the file. The stream must be already opened prior to calling this function. This function must not open/close the given file stream. Upon success, save() will continue writing the file from the point this function has written the file.

Returns
False to indicate that save() should not continue writing the file and fail. True if save() should continue writing the file.

Reimplemented from PGEcfgFile.

Definition at line 734 of file PGEcfgProfiles.cpp.

◆ writeConfiguration()

bool PGEcfgProfiles::writeConfiguration ( )

Saves configuration for current profile to file.

PGE never invokes this function, so it won't overwrite any file automatically. Application has to call this function whenever it wants to save to file.

Returns
True on success, false on failure.

Definition at line 693 of file PGEcfgProfiles.cpp.

Member Data Documentation

◆ bMainCFGinMyDocs

bool PGEcfgProfiles::bMainCFGinMyDocs
private

Do we store cfg file in user documents folder?

Definition at line 86 of file PGEcfgProfiles.h.

◆ cvarWrong

PGEcfgVariable PGEcfgProfiles::cvarWrong
private

This is returned when trying to access an invalid cvar.

Definition at line 93 of file PGEcfgProfiles.h.

◆ m_commandLineVars

std::map<std::string, PGEcfgVariable> PGEcfgProfiles::m_commandLineVars
private

Any added variables parsed by ProcessCommandLine().

Definition at line 95 of file PGEcfgProfiles.h.

◆ m_sActiveProfile

std::string PGEcfgProfiles::m_sActiveProfile
private

Name of active profile.

Definition at line 92 of file PGEcfgProfiles.h.

◆ nActiveProfile

int PGEcfgProfiles::nActiveProfile
private

Index of active profile.

Definition at line 91 of file PGEcfgProfiles.h.

◆ nProfilesCount

int PGEcfgProfiles::nProfilesCount
private

Number of found profiles.

Definition at line 88 of file PGEcfgProfiles.h.

◆ sFoundProfilePlayerNames

std::string** PGEcfgProfiles::sFoundProfilePlayerNames
private

Found profile names (player names).

Definition at line 90 of file PGEcfgProfiles.h.

◆ sFoundProfiles

std::string** PGEcfgProfiles::sFoundProfiles
private

Directory names of found profiles (just add ".cfg" for profile file).

Definition at line 89 of file PGEcfgProfiles.h.

◆ sLangFileName

std::string PGEcfgProfiles::sLangFileName
private

Filename of language file.

Definition at line 85 of file PGEcfgProfiles.h.

◆ sPathToMyDocs

std::string PGEcfgProfiles::sPathToMyDocs
staticprivate

Path to user documents.

Definition at line 83 of file PGEcfgProfiles.h.

◆ sPathToProfiles

std::string PGEcfgProfiles::sPathToProfiles
private

Search path for profiles based on bMainCFGinMyDocs and pPathToMyDocs.

Definition at line 87 of file PGEcfgProfiles.h.


The documentation for this class was generated from the following files: