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",
"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 );
LuaXType( TapNoteScore );
TapNoteScore StringToTapNoteScore( const RString &s )
{
// new style
if ( s == "None" ) return TNS_None;
else if( s == "HitMine" ) return TNS_HitMine;
else if( s == "AvoidMine" ) return TNS_AvoidMine;
else if( s == "CheckpointHit" ) return TNS_CheckpointHit;
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;
std::map<RString, TapNoteScore>::iterator tns=
tns_converter.conversion_map.find(s);
if(tns != tns_converter.conversion_map.end())
{
return tns->second;
}
return TapNoteScore_Invalid;
}
// This is necessary because the StringToX macro wasn't used, and Preference
+41 -26
View File
@@ -34,6 +34,8 @@ bool IniFile::ReadFile( const RString &sPath )
bool IniFile::ReadFile( RageFileBasic &f )
{
RString keyname;
// keychild is used to cache the node that values are being added to. -Kyz
XNode* keychild= NULL;
while( 1 )
{
RString line;
@@ -61,34 +63,47 @@ bool IniFile::ReadFile( RageFileBasic &f )
}
if( line.size() == 0 )
if( line.empty() )
continue;
if( line[0] == ';' )
continue; // comment
if( line[0] == '#' )
continue; // comment
if( line.size() > 1 && line[0] == '/' && line[1] == '/' )
continue; // comment
if( line.size() > 1 && line[0] == '-' && line[1] == '-' )
continue; // comment (Lua style)
if( line[0] == '[' && line[line.size()-1] == ']' )
switch(line[0])
{
// New section.
keyname = line.substr(1, line.size()-2);
}
else
{
// New value.
size_t iEqualIndex = line.find("=");
if( iEqualIndex != string::npos )
{
RString valuename = line.Left( (int) iEqualIndex );
RString value = line.Right( line.size()-valuename.size()-1 );
Trim( valuename );
if( keyname.size() && valuename.size() )
SetValue( keyname, valuename, value );
}
case ';':
case '#':
continue; // comment
case '/':
case '-':
if(line.size() > 1 && line[0] == line[1])
{ continue; } // comment (Lua or C++ style)
goto keyvalue;
case '[':
if(line[line.size()-1] == ']')
{
// New section.
keyname = line.substr(1, line.size()-2);
keychild= GetChild(keyname);
if(keychild == NULL)
{
keychild= AppendChild(keyname);
}
break;
}
default:
keyvalue:
if(keychild == NULL)
{ break; }
// New value.
size_t iEqualIndex = line.find("=");
if( iEqualIndex != string::npos )
{
RString valuename = line.Left((int) iEqualIndex);
RString value = line.Right(line.size()-valuename.size()-1);
Trim(valuename);
if(!valuename.empty())
{
SetKeyValue(keychild, valuename, value);
}
}
break;
}
}
}
+5
View File
@@ -44,6 +44,11 @@ public:
pNode = AppendChild( sKey );
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 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 )
{
if( !BeginsWith(section->GetName(), GAME_SECTION_PREFIX) )
RString section_name= section->GetName();
if( !BeginsWith(section_name, GAME_SECTION_PREFIX) )
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 ];
// todo: read more prefs here? -aj
ini.GetValue( section->GetName(), "Announcer", gp.m_sAnnouncer );
ini.GetValue( section->GetName(), "Theme", gp.m_sTheme );
ini.GetValue( section->GetName(), "DefaultModifiers", gp.m_sDefaultModifiers );
ini.GetValue(section_name, "Announcer", gp.m_sAnnouncer);
ini.GetValue(section_name, "Theme", gp.m_sTheme);
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
#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