From 319ebce2a311ba5ccdf00170ab1f7cea12f28684 Mon Sep 17 00:00:00 2001 From: rejchev Date: Sat, 25 Nov 2023 20:33:00 +0500 Subject: [PATCH] update v1.1.0 --- pom.xml | 8 +- .../com/github/rejchev/steamid/SteamID.java | 128 ++++++------------ .../github/rejchev/steamid/SteamIDType.java | 6 +- .../github/rejchev/steamid/SteamIDUtils.java | 43 +++++- src/test/java/SteamIDTest.java | 30 ++-- src/test/java/SteamIDUtilsTest.java | 10 +- 6 files changed, 113 insertions(+), 112 deletions(-) diff --git a/pom.xml b/pom.xml index 44bb6c3..56f63f0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.rejchev steamid - 1.0.1 + 1.1.0 SteamID A SteamID is an unique identifier used to identify a Steam account. @@ -32,6 +32,12 @@ 4.13.1 test + + org.projectlombok + lombok + 1.18.30 + compile + diff --git a/src/main/java/com/github/rejchev/steamid/SteamID.java b/src/main/java/com/github/rejchev/steamid/SteamID.java index f964e55..b7ac889 100644 --- a/src/main/java/com/github/rejchev/steamid/SteamID.java +++ b/src/main/java/com/github/rejchev/steamid/SteamID.java @@ -1,17 +1,25 @@ package com.github.rejchev.steamid; import com.github.rejchev.steamid.exceptions.SteamViewException; - -import java.util.Arrays; - -public class SteamID { +import lombok.*; +import lombok.experimental.FieldDefaults; + +import java.util.Objects; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@FieldDefaults(level = AccessLevel.PRIVATE) +public final class SteamID { /** NOTE: * * The lowest bit of Steam64 represents Y. * Y is part of the ID number for the account. Y is either 0 or 1. * */ - private byte y; + Byte y; /** NOTE: * @@ -19,7 +27,7 @@ public class SteamID { * Also known as constant Z * */ - private int z; + Integer z; /** NOTE: * @@ -27,7 +35,7 @@ public class SteamID { * It is usually set to 1 for user accounts. * */ - private int instance; + Integer instance; /** NOTE: * @@ -36,7 +44,7 @@ public class SteamID { * (https://steamcommunity.com/path/W : W=Z*2+V+Y) * */ - private SteamIDType type; + SteamIDType type; /** NOTE: * @@ -44,94 +52,34 @@ public class SteamID { * Also known as constant: X * */ - private SteamIDUniverse universe; - - - public SteamID(){} + SteamIDUniverse universe; - public SteamID(long id) throws SteamViewException { - if(id < Integer.MAX_VALUE) - throw new SteamViewException("Invalid Steam ID constructor type"); + public SteamID(long sid64) throws SteamViewException { + if(sid64 < 0) + throw new SteamViewException("Invalid SteamID64 value: " + sid64); - y = (byte) (id & 0x1); - z = ((int) id) >> 1; - instance = (int) ((id >> 32) & 0xFFFFF); - type = SteamIDType.values()[(int) ((id >> 52) & 0xF)]; - universe = SteamIDUniverse.values()[(int) (id >> 56)]; + y = (byte) (sid64 & 0x1); + z = ((int) sid64) >> 1; + instance = (int) ((sid64 >> 32) & 0xFFFFF); + type = SteamIDType.values()[(int) ((sid64 >> 52) & 0xF)]; + universe = SteamIDUniverse.values()[(int) (sid64 >> 56)]; } - public SteamID(String value) { - boolean isSteam2 = value.contains("STEAM_"); - value = value - .trim() - .replaceAll("[\\[\\]]", "") - .replaceAll("STEAM_", ""); - - String[] params = value.split(":"); - - if(isSteam2) { - universe = SteamIDUniverse.values()[Integer.parseInt(params[0])]; - y = Byte.parseByte(params[1]); - z = Integer.parseInt(params[2]); - - type = SteamIDType.INDIVIDUAL; - instance = 1; - } else { - - // https://github.com/SteamRE/open-steamworks/blob/f65c0439bf06981285da1e7639de82cd760755b7/Open%20Steamworks/CSteamID.h#L385 - type = Arrays.stream(SteamIDType.values()) - .filter(x -> x.getLetter().equals(params[0])) - .findFirst().get(); - universe = SteamIDUniverse.values()[Integer.parseInt(params[1])]; + // SteamID32 = z*2+Y + // https://developer.valvesoftware.com/wiki/SteamID + public SteamID(Long sid32, + Integer instance, + SteamIDType type, + SteamIDUniverse universe) throws SteamViewException { + if(sid32 < 0) + throw new SteamViewException("Invalid SteamId32 value: " + sid32); - // W = z*2+Y - // https://developer.valvesoftware.com/wiki/SteamID - long w = Long.parseLong(params[2]); - z = (int) w/2; - y = (byte) (w - z*2); - - instance = (params.length > 3) ? Integer.parseInt(params[3]) : 1; - } - } - - public byte getY() { - return y; - } - - public void setY(byte y) { - this.y = y; - } + y = (byte) (sid32 % 2); + z = (int) ((sid32 - y) / 2); - public int getZ() { - return z; - } - - public void setZ(int z) { - this.z = z; - } - - public int getInstance() { - return instance; - } - - public void setInstance(short instance) { this.instance = instance; - } - - public SteamIDType getType() { - return type; - } - - public void setType(SteamIDType type) { this.type = type; - } - - public SteamIDUniverse getUniverse() { - return universe; - } - - public void setUniverse(SteamIDUniverse universe) { this.universe = universe; } @@ -140,9 +88,9 @@ public boolean equals(Object obj) { if(obj instanceof SteamID) { SteamID input = (SteamID) obj; - return this.y == input.getY() && - this.z == input.getZ() && - this.instance == input.getInstance() && + return Objects.equals(this.y, input.getY()) && + Objects.equals(this.z, input.getZ()) && + Objects.equals(this.instance, input.getInstance()) && this.universe.ordinal() == input.getUniverse().ordinal() && this.type.ordinal() == input.getType().ordinal(); } diff --git a/src/main/java/com/github/rejchev/steamid/SteamIDType.java b/src/main/java/com/github/rejchev/steamid/SteamIDType.java index 2875a1e..a4800e7 100644 --- a/src/main/java/com/github/rejchev/steamid/SteamIDType.java +++ b/src/main/java/com/github/rejchev/steamid/SteamIDType.java @@ -13,13 +13,13 @@ public enum SteamIDType { P2P_SUPER_SEEDER(""), ANON_USER("a"); - private String t; + private String l; SteamIDType(String i) { - t = i; + l = i; } public String getLetter() { - return t; + return l; } } diff --git a/src/main/java/com/github/rejchev/steamid/SteamIDUtils.java b/src/main/java/com/github/rejchev/steamid/SteamIDUtils.java index 5500bed..1de5fa9 100644 --- a/src/main/java/com/github/rejchev/steamid/SteamIDUtils.java +++ b/src/main/java/com/github/rejchev/steamid/SteamIDUtils.java @@ -1,7 +1,48 @@ package com.github.rejchev.steamid; +import com.github.rejchev.steamid.exceptions.SteamViewException; + +import java.util.Arrays; +import java.util.Optional; + public class SteamIDUtils { - public SteamIDUtils() {} + + private SteamIDUtils() {} + + // parsing Steam2 & Steam3 from string + public static SteamID parse(String steamId, + Optional instance, + Optional type) throws SteamViewException { + steamId = steamId.trim(); + + boolean isSteam2 = steamId.indexOf("STEAM_") == 0; + + steamId = steamId.replaceAll("[\\[\\]]", "").replaceAll("STEAM_", ""); + + String[] params = steamId.split(":"); + + SteamID buffer; + + if(isSteam2) { + buffer = SteamID.builder() + .universe(SteamIDUniverse.values()[Integer.parseInt(params[0])]) + .y(Byte.parseByte(params[1])) + .z(Integer.parseInt(params[2])) + .type(type.orElse(SteamIDType.INDIVIDUAL)) + .instance(instance.orElse(1)) + .build(); + } else buffer = new SteamID( + Long.parseLong(params[2]), + (params.length > 3) ? Integer.parseInt(params[3]) : instance.orElse(1), + Arrays.stream(SteamIDType.values()) + .filter(x -> x.getLetter().equals(params[0])) + .findFirst().get(), + SteamIDUniverse.values()[Integer.parseInt(params[1])] + ); + + return buffer; + } + public static String viewAsSteam2(SteamID steamID) { return String.format("STEAM_%d:%d:%d", steamID.getUniverse().ordinal(), steamID.getY(), steamID.getZ()); diff --git a/src/test/java/SteamIDTest.java b/src/test/java/SteamIDTest.java index 1f7cb21..cf3033b 100644 --- a/src/test/java/SteamIDTest.java +++ b/src/test/java/SteamIDTest.java @@ -1,3 +1,4 @@ +import com.github.rejchev.steamid.SteamIDUtils; import com.github.rejchev.steamid.exceptions.SteamViewException; import org.junit.Assert; import org.junit.Test; @@ -5,29 +6,32 @@ import com.github.rejchev.steamid.SteamIDType; import com.github.rejchev.steamid.SteamIDUniverse; +import java.util.Optional; + public class SteamIDTest { @Test - public void steamIDFromSteam3() { - SteamID steamID = new SteamID("[U:1:907378852]"); + public void steamIDFromSteam3() throws SteamViewException { - SteamID expectedSteamID = new SteamID(); - expectedSteamID.setInstance((short) 1); - expectedSteamID.setType(SteamIDType.INDIVIDUAL); - expectedSteamID.setUniverse(SteamIDUniverse.PUBLIC); - expectedSteamID.setY((byte) 0); - expectedSteamID.setZ(453689426); + SteamID steamID = SteamIDUtils.parse("[U:1:907378852]", Optional.empty(), Optional.empty()); + SteamID expectedSteamID = SteamID.builder() + .instance(1) + .type(SteamIDType.INDIVIDUAL) + .universe(SteamIDUniverse.PUBLIC) + .y((byte) 0) + .z(453689426) + .build(); Assert.assertEquals(expectedSteamID, steamID); } @Test - public void steamIDFromSteam2() { - SteamID steamID = new SteamID("STEAM_1:0:453689426"); + public void steamIDFromSteam2() throws SteamViewException { + SteamID steamID = SteamIDUtils.parse("STEAM_1:0:453689426", Optional.empty(), Optional.empty()); SteamID expectedSteamID = new SteamID(); - expectedSteamID.setInstance((short) 1); + expectedSteamID.setInstance(1); expectedSteamID.setType(SteamIDType.INDIVIDUAL); expectedSteamID.setUniverse(SteamIDUniverse.PUBLIC); expectedSteamID.setY((byte) 0); @@ -43,7 +47,7 @@ public void steamIDFromSteam64() throws SteamViewException { SteamID steamID = new SteamID(76561198867644580L); SteamID expectedSteamID = new SteamID(); - expectedSteamID.setInstance((short) 1); + expectedSteamID.setInstance(1); expectedSteamID.setType(SteamIDType.INDIVIDUAL); expectedSteamID.setUniverse(SteamIDUniverse.PUBLIC); expectedSteamID.setY((byte) 0); @@ -58,7 +62,7 @@ public void steamIDViewException() throws SteamViewException { SteamID steamID; try{ - steamID = new SteamID(76561L); + steamID = new SteamID(-1); } catch(SteamViewException e) { steamID = null; } diff --git a/src/test/java/SteamIDUtilsTest.java b/src/test/java/SteamIDUtilsTest.java index ee98287..39cc727 100644 --- a/src/test/java/SteamIDUtilsTest.java +++ b/src/test/java/SteamIDUtilsTest.java @@ -4,22 +4,24 @@ import com.github.rejchev.steamid.SteamID; import com.github.rejchev.steamid.SteamIDUtils; +import java.util.Optional; + public class SteamIDUtilsTest { @Test - public void steamIDAsSteam2() { + public void steamIDAsSteam2() throws SteamViewException { String expectedSteamID2 = "STEAM_1:0:453689426"; - SteamID steamID = new SteamID(expectedSteamID2); + SteamID steamID = SteamIDUtils.parse(expectedSteamID2, Optional.empty(), Optional.empty()); Assert.assertEquals(expectedSteamID2, SteamIDUtils.viewAsSteam2(steamID)); } @Test - public void steamIDAsSteam3() { + public void steamIDAsSteam3() throws SteamViewException { String expectedSteamID3 = "[A:1:907378852:1]"; - SteamID steamID = new SteamID(expectedSteamID3); + SteamID steamID = SteamIDUtils.parse(expectedSteamID3, Optional.empty(), Optional.empty()); Assert.assertEquals(expectedSteamID3, SteamIDUtils.viewAsSteam3(steamID)); }