-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChangelogVersion.cs
117 lines (108 loc) · 2.9 KB
/
ChangelogVersion.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
using System;
using System.Text.RegularExpressions;
using UnityEngine;
namespace KerbalChangelog
{
public class ChangelogVersion : IComparable
{
bool versionNull = false;
bool malformedVersionString = false;
public int major { get; private set; }
public int minor { get; private set; }
public int patch { get; private set; }
public int build { get; private set; }
public string versionName { get; private set; } = null;
bool versionNameExists
{
get
{
if (versionName == null)
return false;
return true;
}
}
public ChangelogVersion(int maj, int min, int pat, int bui)
{
major = maj;
minor = min;
patch = pat;
build = bui;
}
public ChangelogVersion(int maj, int min, int pat, int bui, string vName) : this(maj, min, pat, bui)
{
versionName = vName;
}
// May fail, needs a try/catch
public ChangelogVersion(string version, string cfgDirName)
{
if (version == "null")
{
versionNull = true;
return;
}
Regex pattern = new Regex("(\\d+\\.\\d+\\.\\d+(\\.\\d+)?)"); //matches version numbers starting at the beginning to the end of the string
Regex malformedPattern = new Regex("\\d+\\.\\d+(\\.\\d+)?(\\.\\d+)?");
if (!pattern.IsMatch(version))
{
if (!malformedPattern.IsMatch(version))
{
Debug.Log("[KCL] broken version string: " + version);
throw new ArgumentException("version is not a valid version");
}
Debug.Log("[KCL] malformed version string: " + version + " in directory " + cfgDirName);
malformedVersionString = true;
}
string[] splitVersions = version.Split('.');
major = int.Parse(splitVersions[0]);
minor = int.Parse(splitVersions[1]);
if (!malformedVersionString)
patch = int.Parse(splitVersions[2]);
else
patch = 0;
if (splitVersions.Length > 3)
build = int.Parse(splitVersions[3]);
else
build = 0;
}
public ChangelogVersion(string version, string cfgDirName, string vName) : this(version, cfgDirName)
{
versionName = vName;
}
public override string ToString()
{
if (versionNull)
return "D.N.E";
return $"{major}.{minor}.{patch}.{build}" + (versionNameExists ? " \"" + versionName + "\"" : "");
}
public string ToStringPure()
{
if (versionNull)
return "D.N.E";
return $"{major}.{minor}.{patch}.{build}";
}
//This comparator will sort objects from highest version to lowest version
public int CompareTo(object obj)
{
if (obj == null)
return 1;
if (obj is ChangelogVersion oCLV)
{
if (oCLV.major - this.major == 0)
{
if (oCLV.minor - this.minor == 0)
{
if (oCLV.patch - this.patch == 0)
{
return oCLV.build.CompareTo(this.build);
}
return oCLV.patch.CompareTo(this.patch);
}
return oCLV.minor.CompareTo(this.minor);
}
return oCLV.major.CompareTo(this.major);
}
else
throw new ArgumentException("Object is not a ChangelogVersion");
}
}
}