forked from karv/Art-of-Meow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IdentificableManager.cs
72 lines (64 loc) · 1.68 KB
/
IdentificableManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.IO;
using System.Linq;
using Newtonsoft.Json;
namespace AoM
{
/// <summary>
/// Represents a common mplementation of the object collection of a kind
/// </summary>
public abstract class IdentificableManager<T>
where T : IIdentificable
{
/// <summary>
/// The collection of items
/// </summary>
protected readonly T [] Collection;
/// <summary>
/// Creates and returns an specified item.
/// </summary>
/// <param name="name">Base name of the item</param>
protected S Get<S> (string name)
where S : T
{
// REMARK: OfType is used to remove redundances as much as possible
return Collection.OfType<S> ().First (z => z.Name == name);
}
/// <summary>
/// Gets the item with a specified name
/// </summary>
protected T Get (string name)
{
return Collection.First (z => z.Name == name);
}
/// <summary>
/// </summary>
/// <param name="Collection">The collection of items</param>
protected IdentificableManager (T [] Collection)
{
this.Collection = Collection;
}
/// <summary>
/// Constructs a new database from a json file
/// </summary>
/// <returns>The file.</returns>
/// <param name="fileName">File name.</param>
/// <param name = "settings">Json conversion settings</param>
public static S FromFile<S> (string fileName, JsonSerializerSettings settings)
where S : IdentificableManager<T>
{
string jsonStr;
try
{
var file = File.OpenText (fileName);
jsonStr = file.ReadToEnd ();
file.Close ();
}
catch (IOException ex)
{
throw new IOException ("Cannot read json file: " + fileName, ex);
}
var ret = JsonConvert.DeserializeObject<S> (jsonStr, settings);
return ret;
}
}
}