~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to glchess/src/ai-profile.vala

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
public class AIProfile
 
2
{
 
3
    public string name;
 
4
    public string protocol;
 
5
    public string binary;
 
6
    public string path;
 
7
    public string args = "";
 
8
    public string[] easy_options;
 
9
    public string[] normal_options;
 
10
    public string[] hard_options;
 
11
}
 
12
 
 
13
public List<AIProfile> load_ai_profiles (string filename)
 
14
{
 
15
   var profiles = new List<AIProfile> ();
 
16
 
 
17
   var file = new KeyFile ();
 
18
   try
 
19
   {
 
20
       file.load_from_file (filename, KeyFileFlags.NONE);
 
21
   }
 
22
   catch (KeyFileError e)
 
23
   {
 
24
       warning ("Failed to load AI profiles: %s", e.message);
 
25
       return profiles;   
 
26
   }
 
27
   catch (FileError e)
 
28
   {
 
29
       warning ("Failed to load AI profiles: %s", e.message);
 
30
       return profiles;
 
31
   }
 
32
 
 
33
   foreach (string name in file.get_groups ())
 
34
   {
 
35
       debug ("Loading AI profile %s", name);
 
36
       var profile = new AIProfile ();
 
37
       try
 
38
       {
 
39
           profile.name = name;
 
40
           profile.protocol = file.get_value (name, "protocol");
 
41
           profile.binary = file.get_value (name, "binary");
 
42
           if (file.has_key (name, "args"))
 
43
               profile.args = file.get_value (name, "args");
 
44
           profile.easy_options = load_options (file, name, "easy");
 
45
           profile.normal_options = load_options (file, name, "normal");
 
46
           profile.hard_options = load_options (file, name, "hard");
 
47
       }
 
48
       catch (KeyFileError e)
 
49
       {
 
50
           continue;
 
51
       }
 
52
 
 
53
       var path = Environment.find_program_in_path (profile.binary);
 
54
       if (path != null)
 
55
       {
 
56
           profile.path = path;
 
57
           profiles.append (profile);
 
58
       }
 
59
   }
 
60
 
 
61
   return profiles;
 
62
}
 
63
 
 
64
private string[] load_options (KeyFile file, string name, string difficulty) throws KeyFileError
 
65
{
 
66
    int count = 0;
 
67
    while (file.has_key (name, "option-%s-%d".printf (difficulty, count)))
 
68
        count++;
 
69
 
 
70
    string[] options = new string[count];
 
71
    for (var i = 0; i < count; i++)
 
72
        options[i] = file.get_value (name, "option-%s-%d".printf (difficulty, i));
 
73
 
 
74
    return options;
 
75
}