Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/EnemyCollection.cs @ 9971

Last change on this file since 9971 was 9971, checked in by ascheibe, 11 years ago

#2069 added a view for displaying/reloading enemies

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.Robocode {
32  [Item("EnemyCollection", "A collection of enemy robots.")]
33  [StorableClass]
34  public class EnemyCollection : CheckedItemList<StringValue> {
35    [Storable]
36    public string RobocodePath { get; set; }
37
38    [StorableConstructor]
39    private EnemyCollection(bool deserializing) : base(deserializing) { }
40    private EnemyCollection(EnemyCollection original, Cloner cloner)
41      : base(original, cloner) {
42      RobocodePath = original.RobocodePath;
43    }
44    public EnemyCollection() : base() { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new EnemyCollection(this, cloner);
48    }
49
50    public static EnemyCollection ReloadEnemies(string robocodeDir) {
51      EnemyCollection robotList = new EnemyCollection(); ;
52
53      try {
54        var robotsDir = new DirectoryInfo(Path.Combine(robocodeDir, "robots"));
55        var robotFiles = robotsDir.GetFiles("*", SearchOption.AllDirectories)
56          .Where(x => x.Extension == ".class" || x.Extension == ".jar");
57        var robotSet = new Dictionary<string, StringValue>();
58        foreach (var robot in robotFiles) {
59          string robotName = Path.Combine(robot.DirectoryName, Path.GetFileNameWithoutExtension(robot.FullName));
60          robotName = robotName.Remove(0, robotsDir.FullName.Length);
61          string[] nameParts = robotName.Split(new[] { Path.DirectorySeparatorChar },
62            StringSplitOptions.RemoveEmptyEntries);
63          robotName = string.Join(".", nameParts);
64          robotSet[robot.FullName] = new StringValue(robotName);
65        }
66        robotList.AddRange(robotSet.Values);
67
68        foreach (var robot in robotList) {
69          robotList.SetItemCheckedState(robot, false);
70        }
71      }
72      catch { }
73
74      return robotList;
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.