BadScript 2
Loading...
Searching...
No Matches
BadVersionExtensions.cs
Go to the documentation of this file.
1using System.Globalization;
2
4
8public static class BadVersionExtensions
9{
16 public static Version ChangeVersion(this Version version, string changeStr)
17 {
18 string[] subVersions = changeStr.Split('.');
19 int[] wrapValues = { ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue };
20 int[] original = { version.Major, version.Minor, version.Build, version.Revision };
21 int[] versions = { version.Major, version.Minor, version.Build, version.Revision };
22 bool[] changeReset = new bool[4];
23
24 for (int i = 4 - 1; i >= 0; i--)
25 {
26 string current = subVersions[i];
27
28 if (current.StartsWith("("))
29 {
30 int j = 0;
31
32 for (; j < current.Length; j++)
33 {
34 if (current[j] == ')')
35 {
36 break;
37 }
38 }
39
40 if (j == current.Length)
41 {
42 continue; //Broken. No number left. better ignore
43 }
44
45 string max = current.Substring(1, j - 1);
46
47 if (max == "~")
48 {
49 changeReset[i] = true;
50 }
51 else if (int.TryParse(max, out int newMax))
52 {
53 if (i == 0)
54 {
55 continue; //Can not wrap the last digit
56 }
57
58 wrapValues[i] = newMax;
59 }
60
61 current = current.Remove(0, j + 1);
62 }
63
64 if (i != 0) //Check if we wrapped
65 {
66 if (versions[i] >= wrapValues[i])
67 {
68 versions[i] = 0;
69 versions[i - 1]++;
70 }
71 }
72
73 switch (current)
74 {
75 case "+":
76 versions[i]++;
77
78 break;
79 case "-" when versions[i] != 0:
80 versions[i]--;
81
82 break;
83 default:
84 {
85 if (current.ToLower(CultureInfo.InvariantCulture) == "x")
86 {
87 //Do nothing, X stands for leave the value as is, except the next lower version part wrapped around.
88 }
89 else if (current.StartsWith("{") && current.EndsWith("}"))
90 {
91 string format = current.Remove(current.Length - 1, 1)
92 .Remove(0, 1);
93
94 string value = DateTime.Now.ToString(format);
95
96 if (long.TryParse(value, out long newValue))
97 {
98 versions[i] = (int)(newValue % ushort.MaxValue);
99 }
100 }
101 else if (int.TryParse(current, out int v))
102 {
103 versions[i] = v;
104 }
105
106 break;
107 }
108 }
109 }
110
111 ApplyChangeReset(changeReset, original, versions);
112
113 return new Version(versions[0],
114 versions[1] < 0 ? 0 : versions[1],
115 versions[2] < 0 ? 0 : versions[2],
116 versions[3] < 0 ? 0 : versions[3]
117 );
118 }
119
120
127 private static void ApplyChangeReset(IReadOnlyList<bool> changeReset,
128 IReadOnlyList<int> original,
129 IList<int> versions)
130 {
131 for (int j = 0; j < changeReset.Count; j++)
132 {
133 if (!changeReset[j] || versions[j] == original[j])
134 {
135 continue;
136 }
137
138 for (int i = j + 1; i < versions.Count; i++)
139 {
140 if (!changeReset[i])
141 {
142 versions[i] = 0;
143 }
144 }
145 }
146 }
147}
static void ApplyChangeReset(IReadOnlyList< bool > changeReset, IReadOnlyList< int > original, IList< int > versions)
Applies the Change Reset to the Version Array.
static Version ChangeVersion(this Version version, string changeStr)
Changes the Version Object by the given Change String.
Contains Versioning Extensions and APIs for the BadScript2 Runtime.
Definition BadVersion.cs:11