Skip to content

Commit

Permalink
修复SteamAPP获取 & 增加解析SteamUID功能
Browse files Browse the repository at this point in the history
  • Loading branch information
EvolvedGhost committed Jan 18, 2023
1 parent cc6944a commit 5360f9b
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 8 deletions.
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ plugins {
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion

id("net.mamoe.mirai-console") version "2.12.0"
id("net.mamoe.mirai-console") version "2.13.3"
}

group = "com.evolvedghost.mirai.steamhelper"
version = "1.0.10"
version = "1.0.11"

repositories {
maven("https://maven.aliyun.com/repository/public")
Expand All @@ -23,8 +23,8 @@ repositories {
}

dependencies {
implementation("org.jsoup:jsoup:1.15.2")
implementation("com.google.code.gson:gson:2.9.0")
implementation("org.jsoup:jsoup:1.15.3")
implementation("com.google.code.gson:gson:2.10.1")
implementation("org.quartz-scheduler:quartz:2.3.2") {
exclude(group = "org.slf4j")
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/Steamhelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
object Steamhelper : KotlinPlugin(JvmPluginDescription(
id = "com.evolvedghost.mirai.steamhelper.steamhelper",
name = "SteamHelper",
version = "1.0.10",
version = "1.0.11",
) {
author("EvolvedGhost")
}) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/SteamhelperCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,10 @@ object SteamhelperCommand : CompositeCommand(
sendMessage("你没有推送的权限")
}
}

@SubCommand("uid")
@Description("解析各种格式的SteamUID")
suspend fun CommandSender.uid(SteamUID: String) {
sendMessage(getSteamUID(SteamUID))
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/SteamhelperConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,21 @@ object SteamhelperPluginSetting : ReadOnlyPluginConfig("Steamhelper") {
)
val messageSearch: String by value("<nm>(<id>)\n<ds>")

@ValueDescription(
"""
SteamUID解析列表:
换行请使用\n,其他特殊字符同理,每项都会默认带换行
<fid>=好友码
<64id>=SteamID64
<32id>=SteamID32
<3id>=SteamID3
<iid>=邀请码
<csid>=CSGO码
<5mid>=FiveM码
"""
)
val messageSteamUID: String by value("好友码:<fid>\nSteam64:<64id>\nSteam32:<32id>\nSteam3:<3id>\nCSGO码:<csid>\nFiveM码:<5mid>\n个人主页:https://s.team/p/<iid>")

@ValueDescription(
"""
Epic当前限免信息:
Expand Down
38 changes: 37 additions & 1 deletion src/main/kotlin/messager/SteamMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.evolvedghost.mirai.steamhelper.messager.handler.setPush
import com.evolvedghost.mirai.steamhelper.messager.handler.setSearch
import com.evolvedghost.mirai.steamhelper.utils.*
import com.evolvedghost.mirai.steamhelper.worker.SteamApp
import com.evolvedghost.mirai.steamhelper.worker.SteamUserID
import kotlinx.coroutines.delay
import kotlinx.coroutines.sync.withLock
import net.mamoe.mirai.Bot
Expand Down Expand Up @@ -146,9 +147,11 @@ suspend fun getCompare(AppNameOrAppid: Array<out String>): String {
1 -> {
//成功
}

0 -> {
return "未找到相应的App"
}

else -> {
pluginWarn("SteamHelper在搜索的时候得到了一个错误:", search.exception)
return "搜索发生错误,如长期出现此信息请检查日志和机器人网络状况"
Expand Down Expand Up @@ -264,9 +267,11 @@ fun getSubscribe(app: SteamApp, flag: Int): String {
0 -> {
"涨价"
}

2 -> {
"降价"
}

else -> {
return "内部错误"
}
Expand Down Expand Up @@ -328,10 +333,12 @@ suspend fun getSubscribe(flag: Boolean, cs: CommandSender, AppNameOrAppid: Array
//成功
appid = search.appid!!.toInt()
}

0 -> {
cs.sendMessage("未找到相应的App")
return
}

else -> {
pluginWarn("SteamHelper在搜索的时候得到了一个错误:", search.exception)
cs.sendMessage("搜索发生错误,如长期出现此信息请检查日志和机器人网络状况")
Expand Down Expand Up @@ -362,7 +369,8 @@ suspend fun getSubscribe(flag: Boolean, cs: CommandSender, AppNameOrAppid: Array
// 对subscribeMap中进行添加
try {
if (!SteamhelperPluginData.subscribeMap.contains(appid)) {
SteamhelperPluginData.subscribeMap[appid] = mutableMapOf(botID to mutableMapOf(contactID to 0))
SteamhelperPluginData.subscribeMap[appid] =
mutableMapOf(botID to mutableMapOf(contactID to 0))
} else if (!SteamhelperPluginData.subscribeMap[appid]!!.contains(botID)) {
SteamhelperPluginData.subscribeMap[appid]!![botID] = mutableMapOf(contactID to 0)
} else if (!SteamhelperPluginData.subscribeMap[appid]!![botID]!!.contains(contactID)) {
Expand Down Expand Up @@ -448,9 +456,11 @@ fun getSearch(AppNameOrAppid: Array<out String>): String {
1 -> {
//成功
}

0 -> {
return "未找到相应的App"
}

else -> {
pluginWarn("SteamHelper在搜索的时候得到了一个错误:", search.exception)
return "搜索发生错误,如长期出现此信息请检查日志和机器人网络状况"
Expand All @@ -468,8 +478,34 @@ fun getSearch(AppNameOrAppid: Array<out String>): String {
arrayOf(search.appid, search.appName, search.appDescription)
)
}

0 -> "搜索无结果"
-1 -> "搜索失败,如长期出现此信息请检查日志和机器人网络状况"
else -> "内部错误"
}
}

/** SteamUID替换关键词 */
val keywordsSteamUID = arrayOf("<fid>", "<64id>", "<32id>", "<3id>", "<iid>", "<csid>", "<5mid>")

/** 解析SteamUID */
fun getSteamUID(SteamUID: String): String {
val uid = SteamUserID()
return if (uid.setSteamIdAuto(SteamUID)) {
replace(
SteamhelperPluginSetting.messageSteamUID,
keywordsSteamUID,
arrayOf(
uid.getSteamAccountId().toString(),
uid.getSteam64Id().toString(),
uid.getSteam32Id(),
uid.getSteam3Id(),
uid.getSteamInviteId(),
uid.getSteamCSGOId(),
uid.getSteamFiveMId()
)
)
} else {
"无法解析该ID"
}
}
11 changes: 9 additions & 2 deletions src/main/kotlin/worker/SteamApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,18 @@ class SteamApp() {
val url =
"https://store.steampowered.com/search/?term=" + URLEncoder.encode(keyword, "utf-8") + "&cc=$area"
val get = SSLHelper().getDocument(url)
localAppid =
get.getElementById("search_resultsRows")?.getElementsByTag("a")?.first()?.attr("data-ds-appid")
localAppid = get.getElementById("search_results")?.getElementsByTag("a")?.first()
?.attr("data-ds-appid")
if (!localAppid.isNullOrEmpty()) {
appid = localAppid
return 1
} else {
localAppid =
get.getElementById("search_resultsRows")?.getElementsByTag("a")?.first()?.attr("data-ds-appid")
if (!localAppid.isNullOrEmpty()) {
appid = localAppid
return 1
}
}
} catch (e: Exception) {
pluginExceptionHandler("Steam应用搜索", e)
Expand Down
173 changes: 173 additions & 0 deletions src/main/kotlin/worker/SteamUserID.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.evolvedghost.mirai.steamhelper.worker

import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.security.MessageDigest

class SteamUserID {
private var uidFlagA: Int = -1
private var uidFlagB: Int = -1
var isInit = false
fun getSteamAccountId(): Long {
return (uidFlagB * 2 + uidFlagA).toLong()
}

fun getSteam3Id(): String {
return "[U:1:" + getSteamAccountId() + "]"
}

fun getSteam32Id(): String {
return "STEAM_1:$uidFlagA:$uidFlagB"
}

fun getSteam64Id(): Long {
return 76561197960265728 + (uidFlagB * 2) + uidFlagA
}

fun getSteamFiveMId(): String {
return "steam:" + java.lang.Long.toHexString(getSteam64Id())
}

fun getSteamInviteId(): String {
val hex = java.lang.Long.toHexString(getSteamAccountId())
val result = StringBuilder()
val dictionary = mapOf(
'0' to 'b',
'1' to 'c',
'2' to 'd',
'3' to 'f',
'4' to 'g',
'5' to 'h',
'6' to 'j',
'7' to 'k',
'8' to 'm',
'9' to 'n',
'a' to 'p',
'b' to 'q',
'c' to 'r',
'd' to 't',
'e' to 'v',
'f' to 'w',
)
for (i in 0..6) {
if(i == 3){
result.append('-')
}
val nextChar = dictionary[hex[i]] ?: return ""
result.append(nextChar)
}
return result.toString()
}

// Modify From https://github.com/emily33901/go-csfriendcode
fun getSteamCSGOId(): String {
val id = getSteamAccountId()
val arr = ByteBuffer.allocate(java.lang.Long.BYTES).putLong(id or 0x4353474F00000000).array()
arr.reverse()
val hash =
ByteBuffer.wrap(MessageDigest.getInstance("MD5").digest(arr)).order(ByteOrder.LITTLE_ENDIAN).long.toUInt()
var id64 = getSteam64Id()
var r: ULong = 0u
for (i in 0..7) {
val idNibble = (id64 and 0xF).toByte()
id64 = id64 shr 4
val hashNibble = (hash shr i) and 1u
val a = (r shl 4).toUInt() or idNibble.toUInt()
r = ((r shr 28).toUInt().toULong() shl 32) or a.toULong()
r = ((r shr 31).toUInt().toULong() shl 32) or ((a shl 1) or hashNibble).toULong()
}
val inputBytes = ByteBuffer.allocate(java.lang.Long.BYTES).putLong(r.toLong()).array()
inputBytes.reverse()
var input = ByteBuffer.wrap(inputBytes).long.toULong()
val result = StringBuilder()
val alnum = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
for (i in 0..12) {
if (i == 4 || i == 9) {
result.append('-')
}
result.append(alnum[(input and 31u).toInt()])
input = input shr 5
}
var code = result.toString()
if (code.startsWith("AAAA-")) {
code = code.substring(5)
}
return code
}

fun setSteamAccountId(id: Long): Boolean {
var tempId = id;
uidFlagA = (tempId % 2).toInt()
tempId -= uidFlagA
uidFlagB = (tempId / 2).toInt()
return true
}

fun setSteamAccountId(id: String): Boolean {
return setSteamAccountId(id.toLong())
}

fun setSteam3Id(id: String): Boolean {
val value = Regex("(?<=U:1:).*?(?=]|$)").find(id, 0)?.value
if (value != null) {
setSteamAccountId(value)
} else {
return false
}
return true
}

fun setSteam32Id(id: String): Boolean {
val stringFlagA = Regex("(?<=STEAM_0:|STEAM_1:).*?(?=:)").find(id, 0)?.value
if (stringFlagA != null) {
val tempFlagA = stringFlagA.toInt()
if (tempFlagA == 0 || tempFlagA == 1) {
uidFlagA = tempFlagA
} else {
return false
}
val stringFlagB = Regex("(?<=STEAM_0:$uidFlagA:|STEAM_1:$uidFlagA:).*?(?=$)").find(id, 0)?.value
if (stringFlagB != null) {
uidFlagB = stringFlagB.toInt()
} else {
return false
}
} else {
return false
}
return true
}

fun setSteam64Id(id: String): Boolean {
return setSteam64Id(id.toLong())
}

fun setSteam64Id(id: Long): Boolean {
uidFlagA = (id % 2).toInt()
uidFlagB = ((id - uidFlagA - 76561197960265728) / 2).toInt()
return true
}

fun setSteamFiveMId(id: String): Boolean {
setSteam64Id(java.lang.Long.parseLong(id.replace("steam:", ""), 16))
return true
}

fun setSteamIdAuto(id: String): Boolean {
return try {
if (id.contains("U:1")) {
setSteam3Id(id)
} else if (id.contains("STEAM_")) {
setSteam32Id(id)
} else if (id.contains("steam:")) {
setSteamFiveMId(id)
} else if (id.length == 17) {
setSteam64Id(id)
} else {
setSteamAccountId(id)
}
} catch (e: Exception) {
false
}
}
}

0 comments on commit 5360f9b

Please sign in to comment.