package app.utils; import app.entities.other.SteamID; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.math.BigInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; //https://github.com/SteamRun/SteamIDConverter/blob/master/SteamIDConverter.html public class SteamIDConverter { private static final String SID64_1 = "7656"; private static final long SID64_S = Long.valueOf("1197960265728"); private static final Pattern PatternSteam3ID = Pattern.compile("^\\[([Ug]):([0-9]):([0-9]+)\\]$"); private static final Pattern PatternSteamID32 = Pattern.compile("^STEAM_([0-9]):([0-9]):([0-9]+)$"); private static final Pattern PatternSteamID64 = Pattern.compile("7656([0-9]{12,14})"); private final Pattern PatternCustomUrl = Pattern.compile("steamcommunity\\.com\\/id\\/([A-Za-z0-9-_]{2,32})"); private final Pattern PatternMaybeCustomUrl = Pattern.compile("^([A-Za-z0-9-_]{2,32})$"); public static SteamID getSteamID(Object obj){ return getSteamID((String) obj); } public static SteamID getSteamID(String text){ boolean isSteamID = false; String steam3; String steam2; String steam64; String S3ID_1; long S3ID_2 = 0; long S3ID_3 = 0; long SID32_1 = 0; long SID32_2 = 0; long SID32_3 = 0; long SID64_2 = 0; Matcher result; if((result = PatternSteam3ID.matcher(text)).find()){ //Matcher result = PatternSteam3ID.matcher(text); S3ID_1 = result.group(1); S3ID_2 = Long.parseLong(result.group(2)); S3ID_3 = Long.parseLong(result.group(3)); if(Math.abs(S3ID_3 % 2) == 1){ SID32_2 = 1; } else { SID32_2 = 0; } SID32_3 = (S3ID_3 - SID32_2) / 2; SID64_2 = S3ID_3 + SID64_S; isSteamID = true; } else if ((result = PatternSteamID32.matcher(text)).find()){ //Matcher result = PatternSteamID32.matcher(text); SID32_1 = Long.parseLong(result.group(1)); SID32_2 = Long.parseLong(result.group(2)); SID32_3 = Long.parseLong(result.group(3)); S3ID_3 = SID32_3 * 2 + SID32_2; SID64_2 = S3ID_3 + SID64_S; isSteamID = true; } else if ((result = PatternSteamID64.matcher(text)).find()) { //Matcher result = PatternSteamID64.matcher(text); SID64_2 = Long.parseLong(result.group(1)); S3ID_3 = SID64_2 - SID64_S; if(Math.abs(S3ID_3 % 2) == 1){ SID32_2 = 1; } else { SID32_2 = 0; } SID32_3 = (S3ID_3 - SID32_2) / 2; isSteamID = true; } if(!isSteamID) return null; steam3 = String.format("[U:1:%s]",S3ID_3); steam2 = String.format("STEAM_0:%s:%s", SID32_2, SID32_3); steam64 = String.format("%s%s", SID64_1, SID64_2); return new SteamID(steam3, steam2, steam64, S3ID_3); } }