/
Mod.cs
177 lines (154 loc) · 5.32 KB
/
Mod.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//-----------------------------------------------------------------------
// <copyright file="Mod.cs" company="Mike Caron">
// using System.Standard.Disclaimer;
// </copyright>
//-----------------------------------------------------------------------
namespace MCModManager
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
using Dapper;
/// <summary>
/// Represents a modification to Minecraft
/// </summary>
public class Mod
{
/// <summary>
/// The namespace the Manifests must live under
/// </summary>
internal static readonly XNamespace Namespace = XNamespace.Get("http://mike-caron.com/mod-manager/manifest");
/// <summary>
/// Cached list of ModVersions
/// </summary>
private IList<ModVersion> versions;
/// <summary>
/// Initializes a new instance of the Mod class
/// </summary>
public Mod()
{
}
/// <summary>
/// Gets the unique ID of this particular Mod
/// </summary>
public ID Id { get; private set; }
/// <summary>
/// Gets the name of the Mod
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Gets the URL where the Manifest lives
/// </summary>
public string Url { get; private set; }
/// <summary>
/// Gets a list of all versions this Mod has
/// </summary>
public IEnumerable<ModVersion> Versions
{
get
{
if (this.versions == null)
{
this.versions = ModVersion.GetVersions(this.Id).ToList();
}
return this.versions;
}
}
/// <summary>
/// Gets a value indicating whether this Mod is actually Minecraft.Jar
/// </summary>
public bool IsMinecraftJar
{
get
{
return this.Id == "base:minecraft";
}
}
/// <summary>
/// Returns a list of all Mods in the database
/// </summary>
/// <returns>a list of all Mods in the database</returns>
public static IEnumerable<Mod> LoadMods()
{
using (var dbConn = Database.GetConnection())
{
return dbConn.Query("SELECT Id, Name, Url FROM mod")
.Select(m => new Mod
{
Id = m.id,
Name = m.name,
Url = m.url,
});
}
}
/// <summary>
/// Loads a Mod from a Manifest
/// </summary>
/// <param name="uri">the URI where the Manifest lives</param>
/// <returns>a Mod</returns>
public static Mod LoadFromUrl(string uri)
{
Mod ret = new Mod();
XDocument manifest = XDocument.Load(uri);
if (manifest.Root.Name != Namespace.GetName("manifest"))
{
throw new Exception("That's not a McModManager manifest");
}
ret.Name = manifest.Root.Element(Namespace.GetName("name")).Value;
ret.Id = manifest.Root.Element(Namespace.GetName("id"));
if (ret.Id.Version != null)
{
throw new Exception("Manifest IDs cannot include versions");
}
if (manifest.Root.Element(Namespace.GetName("url")) != null)
{
ret.Url = manifest.Root.Element(Namespace.GetName("url")).Value;
}
using (var dbConn = Database.GetConnection())
using (var tx = dbConn.BeginTransaction())
{
ret.Save(dbConn, tx);
foreach (var ver in manifest.Root.Element(Namespace.GetName("versions")).Elements(Namespace.GetName("version")))
{
var v = ModVersion.LoadVersion(ret, ver, dbConn, tx);
v.Save(dbConn, tx);
}
tx.Commit();
}
return ret;
}
/// <summary>
/// Saves this Mod to the database
/// </summary>
public void Save()
{
using (var dbConn = Database.GetConnection())
using (var tx = dbConn.BeginTransaction())
{
this.Save(dbConn, tx);
tx.Commit();
}
}
/// <summary>
/// Saves this Mod to the database
/// </summary>
/// <param name="dbConn">The database connection</param>
/// <param name="tx">The transaction scope</param>
public void Save(IDbConnection dbConn, IDbTransaction tx = null)
{
dbConn.Execute("REPLACE INTO mod (id, name, url) VALUES (@Id, @Name, @Url)", new { Id = (string)this.Id, this.Name, this.Url }, tx);
}
/// <summary>
/// Returns a string representing this Mod
/// </summary>
/// <returns>a string representing this Mod</returns>
public override string ToString()
{
return string.Format("{0} {1}", this.Name, this.Versions.Last().Ver);
}
}
}