Skip to content

Commit d9aa225

Browse files
authored
Fix update system recognizing each version as dev version (#432)
* fix: update system recognizing each version as dev version * feat: use ring buffer for time stats * feat: add exponential backoff to obfuscation worker
1 parent ad5306e commit d9aa225

10 files changed

Lines changed: 475 additions & 210 deletions

File tree

orebfuscator-common/src/main/java/net/imprex/orebfuscator/util/MinecraftVersion.java

Lines changed: 16 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5-
import java.util.Objects;
65
import java.util.regex.Matcher;
76
import java.util.regex.Pattern;
87

98
import org.bukkit.Bukkit;
109

11-
public final class MinecraftVersion implements Comparable<MinecraftVersion> {
10+
public final class MinecraftVersion {
1211

1312
private static final class NmsMapping {
1413

@@ -23,7 +22,7 @@ private static final class NmsMapping {
2322
MAPPINGS.add(new NmsMapping("1.20.5", "v1_20_R4"));
2423
}
2524

26-
public static String get(MinecraftVersion version) {
25+
public static String get(Version version) {
2726
for (NmsMapping mapping : MAPPINGS) {
2827
if (version.isAtOrAbove(mapping.version)) {
2928
if (mapping.version.minor() != version.minor()) {
@@ -38,19 +37,17 @@ public static String get(MinecraftVersion version) {
3837
throw new RuntimeException("Can't get nms package version for minecraft version: " + version);
3938
}
4039

41-
private final MinecraftVersion version;
40+
private final Version version;
4241
private final String nmsVersion;
4342

4443
public NmsMapping(String version, String nmsVersion) {
45-
this.version = new MinecraftVersion(version);
44+
this.version = Version.parse(version);
4645
this.nmsVersion = nmsVersion;
4746
}
4847
}
4948

50-
private static final Pattern VERSION_PATTERN = Pattern.compile("(?<major>\\d+)(?:\\.(?<minor>\\d+))(?:\\.(?<patch>\\d+))?");
5149
private static final Pattern PACKAGE_PATTERN = Pattern.compile("org\\.bukkit\\.craftbukkit\\.(v\\d+_\\d+_R\\d+)");
52-
53-
private static final MinecraftVersion CURRENT_VERSION = new MinecraftVersion(Bukkit.getBukkitVersion());
50+
private static final Version CURRENT_VERSION = Version.parse(Bukkit.getBukkitVersion());
5451

5552
private static String NMS_VERSION;
5653

@@ -69,118 +66,35 @@ public static String nmsVersion() {
6966
return NMS_VERSION;
7067
}
7168

69+
public static Version current() {
70+
return CURRENT_VERSION;
71+
}
72+
7273
public static int majorVersion() {
73-
return CURRENT_VERSION.major;
74+
return CURRENT_VERSION.major();
7475
}
7576

7677
public static int minorVersion() {
77-
return CURRENT_VERSION.minor;
78+
return CURRENT_VERSION.minor();
7879
}
7980

8081
public static int patchVersion() {
81-
return CURRENT_VERSION.patch;
82+
return CURRENT_VERSION.patch();
8283
}
8384

8485
public static boolean isAbove(String version) {
85-
return CURRENT_VERSION.isAbove(new MinecraftVersion(version));
86+
return CURRENT_VERSION.isAbove(Version.parse(version));
8687
}
8788

8889
public static boolean isAtOrAbove(String version) {
89-
return CURRENT_VERSION.isAtOrAbove(new MinecraftVersion(version));
90+
return CURRENT_VERSION.isAtOrAbove(Version.parse(version));
9091
}
9192

9293
public static boolean isAtOrBelow(String version) {
93-
return CURRENT_VERSION.isAtOrBelow(new MinecraftVersion(version));
94+
return CURRENT_VERSION.isAtOrBelow(Version.parse(version));
9495
}
9596

9697
public static boolean isBelow(String version) {
97-
return CURRENT_VERSION.isBelow(new MinecraftVersion(version));
98-
}
99-
100-
private final int major;
101-
private final int minor;
102-
private final int patch;
103-
104-
public MinecraftVersion(String version) {
105-
Matcher matcher = VERSION_PATTERN.matcher(version);
106-
107-
if (!matcher.find()) {
108-
throw new IllegalArgumentException("can't parse minecraft version: " + version);
109-
}
110-
111-
this.major = Integer.parseInt(matcher.group("major"));
112-
this.minor = Integer.parseInt(matcher.group("minor"));
113-
114-
String patch = matcher.group("patch");
115-
if (patch != null) {
116-
this.patch = Integer.parseInt(patch);
117-
} else {
118-
this.patch = 0;
119-
}
120-
}
121-
122-
public int major() {
123-
return this.major;
124-
}
125-
126-
public int minor() {
127-
return this.minor;
128-
}
129-
130-
public int patch() {
131-
return this.patch;
132-
}
133-
134-
public boolean isAbove(MinecraftVersion version) {
135-
return this.compareTo(version) > 0;
136-
}
137-
138-
public boolean isAtOrAbove(MinecraftVersion version) {
139-
return this.compareTo(version) >= 0;
140-
}
141-
142-
public boolean isAtOrBelow(MinecraftVersion version) {
143-
return this.compareTo(version) <= 0;
144-
}
145-
146-
public boolean isBelow(MinecraftVersion version) {
147-
return this.compareTo(version) < 0;
148-
}
149-
150-
@Override
151-
public int compareTo(MinecraftVersion other) {
152-
int major = Integer.compare(this.major, other.major);
153-
if (major != 0) {
154-
return major;
155-
}
156-
157-
int minor = Integer.compare(this.minor, other.minor);
158-
if (minor != 0) {
159-
return minor;
160-
}
161-
162-
return Integer.compare(this.patch, other.patch);
163-
}
164-
165-
@Override
166-
public int hashCode() {
167-
return Objects.hash(major, minor, patch);
168-
}
169-
170-
@Override
171-
public boolean equals(Object obj) {
172-
if (this == obj) {
173-
return true;
174-
}
175-
if (!(obj instanceof MinecraftVersion)) {
176-
return false;
177-
}
178-
MinecraftVersion other = (MinecraftVersion) obj;
179-
return major == other.major && minor == other.minor && patch == other.patch;
180-
}
181-
182-
@Override
183-
public String toString() {
184-
return String.format("%s.%s.%s", this.major, this.minor, this.patch);
98+
return CURRENT_VERSION.isBelow(Version.parse(version));
18599
}
186100
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package net.imprex.orebfuscator.util;
2+
3+
import java.io.IOException;
4+
import java.util.Objects;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
import com.google.gson.TypeAdapter;
9+
import com.google.gson.stream.JsonReader;
10+
import com.google.gson.stream.JsonWriter;
11+
12+
public record Version(int major, int minor, int patch) implements Comparable<Version> {
13+
14+
private static final Pattern VERSION_PATTERN = Pattern.compile("(?<major>\\d+)(?:\\.(?<minor>\\d+))?(?:\\.(?<patch>\\d+))?");
15+
16+
public static Version parse(String version) {
17+
Matcher matcher = VERSION_PATTERN.matcher(version);
18+
19+
if (!matcher.find()) {
20+
throw new IllegalArgumentException("can't parse version: " + version);
21+
}
22+
23+
int major = Integer.parseInt(matcher.group("major"));
24+
25+
String minorGroup = matcher.group("minor");
26+
int minor = minorGroup != null
27+
? Integer.parseInt(minorGroup)
28+
: 0;
29+
30+
String patchGroup = matcher.group("patch");
31+
int patch = patchGroup != null
32+
? Integer.parseInt(patchGroup)
33+
: 0;
34+
35+
return new Version(major, minor, patch);
36+
}
37+
38+
public boolean isAbove(Version version) {
39+
return this.compareTo(version) > 0;
40+
}
41+
42+
public boolean isAtOrAbove(Version version) {
43+
return this.compareTo(version) >= 0;
44+
}
45+
46+
public boolean isAtOrBelow(Version version) {
47+
return this.compareTo(version) <= 0;
48+
}
49+
50+
public boolean isBelow(Version version) {
51+
return this.compareTo(version) < 0;
52+
}
53+
54+
@Override
55+
public int compareTo(Version other) {
56+
int major = Integer.compare(this.major, other.major);
57+
if (major != 0) {
58+
return major;
59+
}
60+
61+
int minor = Integer.compare(this.minor, other.minor);
62+
if (minor != 0) {
63+
return minor;
64+
}
65+
66+
return Integer.compare(this.patch, other.patch);
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
return Objects.hash(major, minor, patch);
72+
}
73+
74+
@Override
75+
public boolean equals(Object obj) {
76+
if (this == obj) {
77+
return true;
78+
}
79+
if (!(obj instanceof Version)) {
80+
return false;
81+
}
82+
Version other = (Version) obj;
83+
return major == other.major && minor == other.minor && patch == other.patch;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
return String.format("%s.%s.%s", this.major, this.minor, this.patch);
89+
}
90+
91+
public static final class Json extends TypeAdapter<Version> {
92+
93+
@Override
94+
public void write(JsonWriter out, Version value) throws IOException {
95+
out.value(value.toString());
96+
}
97+
98+
@Override
99+
public Version read(JsonReader in) throws IOException {
100+
return Version.parse(in.nextString());
101+
}
102+
}
103+
}

orebfuscator-plugin/src/main/java/net/imprex/orebfuscator/OrebfuscatorStatistics.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ private static String formatPrecent(double percent) {
1212
return String.format("%.2f%%", percent * 100);
1313
}
1414

15+
private static String formatNanos(long time) {
16+
if (time > 1000_000L) {
17+
return String.format("%.1fms", time / 1000_000d);
18+
} else if (time > 1000L) {
19+
return String.format("%.1fµs", time / 1000d);
20+
} else {
21+
return String.format("%dns", time);
22+
}
23+
}
24+
1525
private static String formatBytes(long bytes) {
1626
if (bytes > 1073741824L) {
1727
return String.format("%.1f GiB", bytes / 1073741824d);
@@ -31,6 +41,10 @@ private static String formatBytes(long bytes) {
3141
private LongSupplier memoryCacheSize = () -> 0;
3242
private LongSupplier diskCacheQueueLength = () -> 0;
3343
private LongSupplier obfuscationQueueLength = () -> 0;
44+
private LongSupplier obfuscationWaitTime = () -> 0;
45+
private LongSupplier obfuscationProcessTime = () -> 0;
46+
private LongSupplier proximityWaitTime = () -> 0;
47+
private LongSupplier proximityProcessTime = () -> 0;
3448

3549
public void onCacheHitMemory() {
3650
this.cacheHitCountMemory.incrementAndGet();
@@ -60,6 +74,22 @@ public void setObfuscationQueueLengthSupplier(LongSupplier supplier) {
6074
this.obfuscationQueueLength = Objects.requireNonNull(supplier);
6175
}
6276

77+
public void setObfuscationWaitTime(LongSupplier supplier) {
78+
this.obfuscationWaitTime = Objects.requireNonNull(supplier);
79+
}
80+
81+
public void setObfuscationProcessTime(LongSupplier supplier) {
82+
this.obfuscationProcessTime = Objects.requireNonNull(supplier);
83+
}
84+
85+
public void setProximityWaitTime(LongSupplier supplier) {
86+
this.proximityWaitTime = Objects.requireNonNull(supplier);
87+
}
88+
89+
public void setProximityProcessTime(LongSupplier supplier) {
90+
this.proximityProcessTime = Objects.requireNonNull(supplier);
91+
}
92+
6393
@Override
6494
public String toString() {
6595
long cacheHitCountMemory = this.cacheHitCountMemory.get();
@@ -94,6 +124,34 @@ public String toString() {
94124
builder.append(" - diskCacheQueueLength: ").append(diskCacheQueueLength).append('\n');
95125
builder.append(" - obfuscationQueueLength: ").append(obfuscationQueueLength).append('\n');
96126

127+
long obfuscationWaitTime = this.obfuscationWaitTime.getAsLong();
128+
long obfuscationProcessTime = this.obfuscationProcessTime.getAsLong();
129+
long obfuscationTotalTime = obfuscationWaitTime + obfuscationProcessTime;
130+
131+
double obfuscationUtilization = 0;
132+
if (obfuscationTotalTime > 0) {
133+
obfuscationUtilization = (double) obfuscationProcessTime / obfuscationTotalTime;
134+
}
135+
136+
builder.append(" - obfuscation (wait/process/utilization): ")
137+
.append(formatNanos(obfuscationWaitTime)).append(" | ")
138+
.append(formatNanos(obfuscationProcessTime)).append(" | ")
139+
.append(formatPrecent(obfuscationUtilization)).append('\n');
140+
141+
long proximityWaitTime = this.proximityWaitTime.getAsLong();
142+
long proximityProcessTime = this.proximityProcessTime.getAsLong();
143+
long proximityTotalTime = proximityWaitTime + proximityProcessTime;
144+
145+
double proximityUtilization = 0;
146+
if (proximityTotalTime > 0) {
147+
proximityUtilization = (double) proximityProcessTime / proximityTotalTime;
148+
}
149+
150+
builder.append(" - proximity (wait/process/utilization): ")
151+
.append(formatNanos(proximityWaitTime)).append(" | ")
152+
.append(formatNanos(proximityProcessTime)).append(" | ")
153+
.append(formatPrecent(proximityUtilization)).append('\n');
154+
97155
return builder.toString();
98156
}
99157

@@ -107,6 +165,10 @@ public JsonObject toJson() {
107165
object.addProperty("memoryCacheSize", this.memoryCacheSize.getAsLong());
108166
object.addProperty("diskCacheQueueLength", this.diskCacheQueueLength.getAsLong());
109167
object.addProperty("obfuscationQueueLength", this.obfuscationQueueLength.getAsLong());
168+
object.addProperty("obfuscationWaitTime", this.obfuscationWaitTime.getAsLong());
169+
object.addProperty("obfuscationProcessTime", this.obfuscationProcessTime.getAsLong());
170+
object.addProperty("proximityWaitTime", this.proximityWaitTime.getAsLong());
171+
object.addProperty("proximityProcessTime", this.proximityProcessTime.getAsLong());
110172

111173
return object;
112174
}

0 commit comments

Comments
 (0)