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 =
20 {
21 ushort.MaxValue,
22 ushort.MaxValue,
23 ushort.MaxValue,
24 ushort.MaxValue,
25 };
26 int[] original =
27 {
28 version.Major,
29 version.Minor,
30 version.Build,
31 version.Revision,
32 };
33 int[] versions =
34 {
35 version.Major,
36 version.Minor,
37 version.Build,
38 version.Revision,
39 };
40 bool[] changeReset = new bool[4];
41
42 for (int i = 4 - 1; i >= 0; i--)
43 {
44 string current = subVersions[i];
45
46 if (current.StartsWith("("))
47 {
48 int j = 0;
49
50 for (; j < current.Length; j++)
51 {
52 if (current[j] == ')')
53 {
54 break;
55 }
56 }
57
58 if (j == current.Length)
59 {
60 continue; //Broken. No number left. better ignore
61 }
62
63 string max = current.Substring(1, j - 1);
64
65 if (max == "~")
66 {
67 changeReset[i] = true;
68 }
69 else if (int.TryParse(max, out int newMax))
70 {
71 if (i == 0)
72 {
73 continue; //Can not wrap the last digit
74 }
75
76 wrapValues[i] = newMax;
77 }
78
79 current = current.Remove(0, j + 1);
80 }
81
82 if (i != 0) //Check if we wrapped
83 {
84 if (versions[i] >= wrapValues[i])
85 {
86 versions[i] = 0;
87 versions[i - 1]++;
88 }
89 }
90
91 switch (current)
92 {
93 case "+":
94 versions[i]++;
95
96 break;
97 case "-" when versions[i] != 0:
98 versions[i]--;
99
100 break;
101 default:
102 {
103 if (current.ToLower(CultureInfo.InvariantCulture) == "x")
104 {
105 //Do nothing, X stands for leave the value as is, except the next lower version part wrapped around.
106 }
107 else if (current.StartsWith("{") && current.EndsWith("}"))
108 {
109 string format = current.Remove(current.Length - 1, 1).Remove(0, 1);
110
111 string value = DateTime.Now.ToString(format);
112
113 if (long.TryParse(value, out long newValue))
114 {
115 versions[i] = (int)(newValue % ushort.MaxValue);
116 }
117 }
118 else if (int.TryParse(current, out int v))
119 {
120 versions[i] = v;
121 }
122
123 break;
124 }
125 }
126 }
127
128 ApplyChangeReset(changeReset, original, versions);
129
130 return new Version(
131 versions[0],
132 versions[1] < 0 ? 0 : versions[1],
133 versions[2] < 0 ? 0 : versions[2],
134 versions[3] < 0 ? 0 : versions[3]
135 );
136 }
137
138
145 private static void ApplyChangeReset(IReadOnlyList<bool> changeReset, IReadOnlyList<int> original, IList<int> versions)
146 {
147 for (int j = 0; j < changeReset.Count; j++)
148 {
149 if (!changeReset[j] || versions[j] == original[j])
150 {
151 continue;
152 }
153
154 for (int i = j + 1; i < versions.Count; i++)
155 {
156 if (!changeReset[i])
157 {
158 versions[i] = 0;
159 }
160 }
161 }
162 }
163}
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