gnome-chess/src/ai-profile.vala

76 lines
2 KiB
Vala
Raw Normal View History

2011-01-13 19:00:57 +00:00
public class AIProfile
{
public string name;
public string protocol;
public string binary;
public string path;
public string args = "";
public string[] easy_options;
public string[] normal_options;
public string[] hard_options;
}
2011-01-01 01:42:50 +00:00
public List<AIProfile> load_ai_profiles (string filename)
{
2011-01-01 01:42:50 +00:00
var profiles = new List<AIProfile> ();
2011-01-01 01:42:50 +00:00
var file = new KeyFile ();
try
{
2011-01-01 01:42:50 +00:00
file.load_from_file (filename, KeyFileFlags.NONE);
}
2011-01-01 01:42:50 +00:00
catch (KeyFileError e)
{
2011-01-01 01:42:50 +00:00
warning ("Failed to load AI profiles: %s", e.message);
return profiles;
}
2011-01-01 01:42:50 +00:00
catch (FileError e)
{
2011-01-01 01:42:50 +00:00
warning ("Failed to load AI profiles: %s", e.message);
return profiles;
}
foreach (string name in file.get_groups ())
{
2011-01-01 01:42:50 +00:00
debug ("Loading AI profile %s", name);
var profile = new AIProfile ();
try
{
profile.name = name;
profile.protocol = file.get_value (name, "protocol");
profile.binary = file.get_value (name, "binary");
if (file.has_key (name, "args"))
profile.args = file.get_value (name, "args");
2011-01-09 21:36:38 +00:00
profile.easy_options = load_options (file, name, "easy");
profile.normal_options = load_options (file, name, "normal");
profile.hard_options = load_options (file, name, "hard");
}
2011-01-01 01:42:50 +00:00
catch (KeyFileError e)
{
continue;
}
2011-01-13 19:00:57 +00:00
var path = Environment.find_program_in_path (profile.binary);
if (path != null)
{
profile.path = path;
profiles.append (profile);
2011-01-13 19:00:57 +00:00
}
}
return profiles;
}
2011-01-09 21:36:38 +00:00
private string[] load_options (KeyFile file, string name, string difficulty) throws KeyFileError
{
int count = 0;
while (file.has_key (name, "option-%s-%d".printf (difficulty, count)))
count++;
string[] options = new string[count];
for (var i = 0; i < count; i++)
options[i] = file.get_value (name, "option-%s-%d".printf (difficulty, i));
return options;
}