Rewrote IniFile::ReadFile to use a switch and cache the section node. Rewrote StringToTapNoteScore to use a map.

This commit is contained in:
Kyzentun
2015-03-08 22:38:00 -06:00
parent ff520479ca
commit 3c9452d894
5 changed files with 77 additions and 52 deletions
+24 -20
View File
@@ -201,30 +201,34 @@ static const char *TapNoteScoreNames[] = {
"W1", "W1",
"CheckpointHit", "CheckpointHit",
}; };
struct tns_conversion_helper
{
std::map<RString, TapNoteScore> conversion_map;
tns_conversion_helper()
{
FOREACH_ENUM(TapNoteScore, tns)
{
conversion_map[TapNoteScoreNames[tns]]= tns;
}
// for backward compatibility
conversion_map["Boo"]= TNS_W5;
conversion_map["Good"]= TNS_W4;
conversion_map["Great"]= TNS_W3;
conversion_map["Perfect"]= TNS_W2;
conversion_map["Marvelous"]= TNS_W1;
}
};
tns_conversion_helper tns_converter;
XToString( TapNoteScore ); XToString( TapNoteScore );
LuaXType( TapNoteScore ); LuaXType( TapNoteScore );
TapNoteScore StringToTapNoteScore( const RString &s ) TapNoteScore StringToTapNoteScore( const RString &s )
{ {
// new style std::map<RString, TapNoteScore>::iterator tns=
if ( s == "None" ) return TNS_None; tns_converter.conversion_map.find(s);
else if( s == "HitMine" ) return TNS_HitMine; if(tns != tns_converter.conversion_map.end())
else if( s == "AvoidMine" ) return TNS_AvoidMine; {
else if( s == "CheckpointHit" ) return TNS_CheckpointHit; return tns->second;
else if( s == "CheckpointMiss" )return TNS_CheckpointMiss; }
else if( s == "Miss" ) return TNS_Miss;
else if( s == "W5" ) return TNS_W5;
else if( s == "W4" ) return TNS_W4;
else if( s == "W3" ) return TNS_W3;
else if( s == "W2" ) return TNS_W2;
else if( s == "W1" ) return TNS_W1;
// for backward compatibility
else if( s == "Boo" ) return TNS_W5;
else if( s == "Good" ) return TNS_W4;
else if( s == "Great" ) return TNS_W3;
else if( s == "Perfect" ) return TNS_W2;
else if( s == "Marvelous" ) return TNS_W1;
return TapNoteScore_Invalid; return TapNoteScore_Invalid;
} }
// This is necessary because the StringToX macro wasn't used, and Preference // This is necessary because the StringToX macro wasn't used, and Preference
+29 -14
View File
@@ -34,6 +34,8 @@ bool IniFile::ReadFile( const RString &sPath )
bool IniFile::ReadFile( RageFileBasic &f ) bool IniFile::ReadFile( RageFileBasic &f )
{ {
RString keyname; RString keyname;
// keychild is used to cache the node that values are being added to. -Kyz
XNode* keychild= NULL;
while( 1 ) while( 1 )
{ {
RString line; RString line;
@@ -61,24 +63,34 @@ bool IniFile::ReadFile( RageFileBasic &f )
} }
if( line.size() == 0 ) if( line.empty() )
continue; continue;
if( line[0] == ';' ) switch(line[0])
{
case ';':
case '#':
continue; // comment continue; // comment
if( line[0] == '#' ) case '/':
continue; // comment case '-':
if( line.size() > 1 && line[0] == '/' && line[1] == '/' ) if(line.size() > 1 && line[0] == line[1])
continue; // comment { continue; } // comment (Lua or C++ style)
if( line.size() > 1 && line[0] == '-' && line[1] == '-' ) goto keyvalue;
continue; // comment (Lua style) case '[':
if(line[line.size()-1] == ']')
if( line[0] == '[' && line[line.size()-1] == ']' )
{ {
// New section. // New section.
keyname = line.substr(1, line.size()-2); keyname = line.substr(1, line.size()-2);
} keychild= GetChild(keyname);
else if(keychild == NULL)
{ {
keychild= AppendChild(keyname);
}
break;
}
default:
keyvalue:
if(keychild == NULL)
{ break; }
// New value. // New value.
size_t iEqualIndex = line.find("="); size_t iEqualIndex = line.find("=");
if( iEqualIndex != string::npos ) if( iEqualIndex != string::npos )
@@ -86,10 +98,13 @@ bool IniFile::ReadFile( RageFileBasic &f )
RString valuename = line.Left((int) iEqualIndex); RString valuename = line.Left((int) iEqualIndex);
RString value = line.Right(line.size()-valuename.size()-1); RString value = line.Right(line.size()-valuename.size()-1);
Trim(valuename); Trim(valuename);
if( keyname.size() && valuename.size() ) if(!valuename.empty())
SetValue( keyname, valuename, value ); {
SetKeyValue(keychild, valuename, value);
} }
} }
break;
}
} }
} }
+5
View File
@@ -44,6 +44,11 @@ public:
pNode = AppendChild( sKey ); pNode = AppendChild( sKey );
pNode->AppendAttr<T>( sValueName, value ); pNode->AppendAttr<T>( sValueName, value );
} }
template <typename T>
void SetKeyValue(XNode* keynode, const RString &sValueName, const T &value)
{
keynode->AppendAttr<T>(sValueName, value);
}
bool DeleteKey( const RString &keyname ); bool DeleteKey( const RString &keyname );
bool DeleteValue( const RString &keyname, const RString &valuename ); bool DeleteValue( const RString &keyname, const RString &valuename );
+6 -5
View File
@@ -463,16 +463,17 @@ void PrefsManager::ReadGamePrefsFromIni( const RString &sIni )
FOREACH_CONST_Child( &ini, section ) FOREACH_CONST_Child( &ini, section )
{ {
if( !BeginsWith(section->GetName(), GAME_SECTION_PREFIX) ) RString section_name= section->GetName();
if( !BeginsWith(section_name, GAME_SECTION_PREFIX) )
continue; continue;
RString sGame = section->GetName().Right( section->GetName().length() - GAME_SECTION_PREFIX.length() ); RString sGame = section_name.Right( section_name.length() - GAME_SECTION_PREFIX.length() );
GamePrefs &gp = m_mapGameNameToGamePrefs[ sGame ]; GamePrefs &gp = m_mapGameNameToGamePrefs[ sGame ];
// todo: read more prefs here? -aj // todo: read more prefs here? -aj
ini.GetValue( section->GetName(), "Announcer", gp.m_sAnnouncer ); ini.GetValue(section_name, "Announcer", gp.m_sAnnouncer);
ini.GetValue( section->GetName(), "Theme", gp.m_sTheme ); ini.GetValue(section_name, "Theme", gp.m_sTheme);
ini.GetValue( section->GetName(), "DefaultModifiers", gp.m_sDefaultModifiers ); ini.GetValue(section_name, "DefaultModifiers", gp.m_sDefaultModifiers);
} }
} }
+1 -1
View File
@@ -53,7 +53,7 @@ extern const RageTimer RageZeroTimer;
// For profiling how long some chunk of code takes. -Kyz // For profiling how long some chunk of code takes. -Kyz
#define START_TIME(name) float name##_start_time= RageTimer::GetTimeSinceStartFast(); #define START_TIME(name) float name##_start_time= RageTimer::GetTimeSinceStartFast();
#define END_TIME(name) float name##_end_time= RageTimer::GetTimeSinceStartFast(); LOG->Warn(#name " time: %f", name##_end_time - name##_start_time); #define END_TIME(name) float name##_end_time= RageTimer::GetTimeSinceStartFast(); LOG->Warn(#name " time: %f to %f = %f", name##_start_time, name##_end_time, name##_end_time - name##_start_time);
#endif #endif