Skip to content

Commit

Permalink
update v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rejchev committed Nov 25, 2023
1 parent fd8d067 commit 319ebce
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 112 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.rejchev</groupId>
<artifactId>steamid</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>

<name>SteamID</name>
<description>A SteamID is an unique identifier used to identify a Steam account.</description>
Expand All @@ -32,6 +32,12 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>compile</scope>
</dependency>
</dependencies>

<properties>
Expand Down
128 changes: 38 additions & 90 deletions src/main/java/com/github/rejchev/steamid/SteamID.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
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:
*
* 31 bits represents the account number.
* Also known as constant Z
*
*/
private int z;
Integer z;

/** NOTE:
*
* 20 bits represents the instance of the account.
* It is usually set to 1 for user accounts.
*
*/
private int instance;
Integer instance;

/** NOTE:
*
Expand All @@ -36,102 +44,42 @@ public class SteamID {
* (https://steamcommunity.com/path/W : W=Z*2+V+Y)
*
*/
private SteamIDType type;
SteamIDType type;

/** NOTE:
*
* 8 bits represents the "Universe" the steam account belongs to.
* 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;
}

Expand All @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/github/rejchev/steamid/SteamIDType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
43 changes: 42 additions & 1 deletion src/main/java/com/github/rejchev/steamid/SteamIDUtils.java
Original file line number Diff line number Diff line change
@@ -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<Integer> instance,
Optional<SteamIDType> 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());
Expand Down
30 changes: 17 additions & 13 deletions src/test/java/SteamIDTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import com.github.rejchev.steamid.SteamIDUtils;
import com.github.rejchev.steamid.exceptions.SteamViewException;
import org.junit.Assert;
import org.junit.Test;
import com.github.rejchev.steamid.SteamID;
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);
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/SteamIDUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 319ebce

Please sign in to comment.