Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking change: Steam dropped public api support #15

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 97 additions & 112 deletions addons/sourcemod/scripting/FamilyShareManager.sp
Original file line number Diff line number Diff line change
@@ -1,53 +1,60 @@
#pragma newdecls required

#include <utilshelper>
#include <ripext>
#include <multicolors>
#tryinclude <SteamWorks>
#include <SteamWorks>

#undef REQUIRE_PLUGIN
#tryinclude <materialadmin>
#tryinclude <sourcebanspp>
#define REQUIRE_PLUGIN

#define TAG "{green}[Family Share Manager]"

Handle g_hCvar_Reject = INVALID_HANDLE;
Handle g_hCvar_RejectDuration = INVALID_HANDLE;
Handle g_hCvar_RejectMessage = INVALID_HANDLE;
Handle g_hCvar_Whitelist = INVALID_HANDLE;
Handle g_hCvar_IgnoreAdmins = INVALID_HANDLE;
ConVar g_hCvar_Reject;
ConVar g_hCvar_RejectDuration;
ConVar g_hCvar_RejectMessage;
ConVar g_hCvar_Whitelist;
ConVar g_hCvar_IgnoreAdmins;

Handle g_hWhitelistTrie = INVALID_HANDLE;
Handle g_hCvar_Method = INVALID_HANDLE;

char g_sWhitelist[PLATFORM_MAX_PATH];

bool g_bParsed = false;
bool g_bIgnoreAdmins = false;
bool g_bSourceBans = false;
bool g_bMaterialAdmin = false;

int g_iAppID = -1;
int g_iReject;
int g_iRejectDuration;

bool g_bLateLoad = false;
// Added this here so it compiles via GitHub Actions without the SourceBans/MaterialAdmin includes.
#if !defined _sourcebanspp_included
Rushaway marked this conversation as resolved.
Show resolved Hide resolved
native void SBPP_BanPlayer(int iAdmin, int iTarget, int iTime, const char[] sReason);
#endif
#if !defined _materialadmin_included
native bool MABanPlayer(int iClient, int iTarget, int iType, int iTime, char[] sReason);
#define MA_BAN_STEAM 1
#endif

public Plugin myinfo =
{
name = "Family Share Manager",
author = "Sidezz (+bonbon, 11530, maxime1907, .Rushaway)",
description = "Whitelist or ban family shared accounts",
version = "1.7.3",
version = "1.8.0",
url = ""
}

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
g_bLateLoad = late;
return APLRes_Success;
}

public void OnPluginStart()
{
// Get one here
// https://steamcommunity.com/dev
g_hCvar_Reject = CreateConVar("sm_familyshare_reject", "1", "2 = ban, 1 = kick, 0 = ignore", FCVAR_NOTIFY);
g_hCvar_RejectDuration = CreateConVar("sm_familyshare_reject_duration", "10", "How much time is the player banned", FCVAR_NOTIFY);
g_hCvar_RejectMessage = CreateConVar("sm_familyshare_reject_message", "Family sharing is disabled on this server.", "Message to display in sourcebans/on ban/on kick", FCVAR_NOTIFY);
g_hCvar_IgnoreAdmins = CreateConVar("sm_familyshare_ignoreadmins", "1", "Ignore admins using family shared accounts", FCVAR_NOTIFY, true, 0.0, true, 1.0);
g_hCvar_Whitelist = CreateConVar("sm_familyshare_whitelist", "familyshare_whitelist.cfg", "File to use for whitelist configuration");
g_hCvar_Method = CreateConVar("sm_familyshare_method", "0", "Method to detect family sharing [0 = Steam API, 1 = SteamWorks extension]");

g_iAppID = GetAppID();
if (g_iAppID <= -1)
Expand All @@ -68,19 +75,47 @@ public void OnPluginStart()

parseList();

HookConVarChange(g_hCvar_Reject, OnConVarChanged);
HookConVarChange(g_hCvar_RejectDuration, OnConVarChanged);
HookConVarChange(g_hCvar_IgnoreAdmins, OnConVarChanged);

RegAdminCmd("sm_reloadlist", command_reloadWhiteList, ADMFLAG_ROOT, "Reload the whitelist");
RegAdminCmd("sm_addtolist", command_addToList, ADMFLAG_ROOT, "Add a player to the whitelist");
RegAdminCmd("sm_removefromlist", command_removeFromList, ADMFLAG_ROOT, "Remove a player from the whitelist");
RegAdminCmd("sm_displaylist", command_displayList, ADMFLAG_ROOT, "View current whitelist");
}

if (g_bLateLoad)
{
for (int i = 1; i < MaxClients; i++)
{
if (IsClientInGame(i) && IsClientAuthorized(i))
OnClientPostAdminCheck(i);
}
}
public void OnLibraryAdded(const char []name)
{
if( strcmp(name, "sourcebans++") == 0 )
g_bSourceBans = true;
else if( strcmp(name, "materialadmin") == 0 )
g_bMaterialAdmin = true;
}

public void OnLibraryRemoved(const char []name)
{
if( strcmp(name, "sourcebans++") == 0 )
g_bSourceBans = false;
else if( strcmp(name, "materialadmin") == 0 )
g_bMaterialAdmin = false;
}

public void OnConfigsExecuted()
{
g_iReject = GetConVarInt(g_hCvar_Reject);
g_iRejectDuration = GetConVarInt(g_hCvar_RejectDuration);
g_bIgnoreAdmins = GetConVarBool(g_hCvar_IgnoreAdmins);
}

void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
if(convar==g_hCvar_Reject)
g_iReject = GetConVarInt(convar);
else if(convar==g_hCvar_RejectDuration)
g_iRejectDuration = GetConVarInt(convar);
else if(convar==g_hCvar_IgnoreAdmins)
g_bIgnoreAdmins = GetConVarBool(convar);
}

public Action command_removeFromList(int client, int args)
Expand Down Expand Up @@ -255,7 +290,7 @@ public int playerMenuHandle(Menu playerMenu, MenuAction action, int client, int
}

char steamid[32];
GetClientAuthId(target, AuthId_Steam2, steamid, sizeof(steamid));
GetClientAuthId(target, AuthId_Steam2, steamid, sizeof(steamid), false);

StripQuotes(steamid);
TrimString(steamid);
Expand Down Expand Up @@ -353,7 +388,7 @@ stock bool CheckWhiteList(int client)
if (g_bParsed)
{
char auth[2][64];
GetClientAuthId(client, AuthId_Steam2, auth[0], sizeof(auth[]));
GetClientAuthId(client, AuthId_Steam2, auth[0], sizeof(auth[]), false);
whiteListed = GetTrieString(g_hWhitelistTrie, auth[0], auth[1], sizeof(auth[]));
if(whiteListed)
{
Expand All @@ -362,78 +397,14 @@ stock bool CheckWhiteList(int client)
}
}

if (CheckCommandAccess(client, "sm_admin", ADMFLAG_GENERIC) && GetConVarInt(g_hCvar_IgnoreAdmins) > 0)
if (CheckCommandAccess(client, "sm_admin", ADMFLAG_GENERIC) && g_bIgnoreAdmins)
{
return true;
}

return false;
}

public void OnClientPostAdminCheck(int client)
{
if (CheckWhiteList(client))
return;

if (GetConVarInt(g_hCvar_Method) == 0 && !IsFakeClient(client))
checkFamilySharing(client);
}

stock void checkFamilySharing(int client)
{
char sSteam64ID[32];
GetClientAuthId(client, AuthId_SteamID64, sSteam64ID, sizeof(sSteam64ID));

char sSteamAPIEndpoint[255];
GetSteamAPIEndpoint(sSteamAPIEndpoint, sizeof(sSteamAPIEndpoint));

char sSteamAPIKey[64];
GetSteamAPIKey(sSteamAPIKey, sizeof(sSteamAPIKey));

char sRequest[256];
FormatEx(sRequest, sizeof(sRequest), "http://%s/IPlayerService/IsPlayingSharedGame/v0001/?key=%s&steamid=%s&appid_playing=%d&format=json", sSteamAPIEndpoint, sSteamAPIKey, sSteam64ID, g_iAppID);

HTTPRequest request = new HTTPRequest(sRequest);

request.Get(OnFamilyShareReceived, client);
}

stock void OnFamilyShareReceived(HTTPResponse response, any client)
{
if (response.Status != HTTPStatus_OK)
return;

// Indicate that the response contains a JSON object
JSONObject responseData = view_as<JSONObject>(response.Data);

if (!responseData.HasKey("lender_steamid"))
return;

int lenderSteamid = responseData.GetInt("lender_steamid");

char rejectMessage[255];
GetConVarString(g_hCvar_RejectMessage, rejectMessage, sizeof(rejectMessage));

if (lenderSteamid == 0)
return;

int iReject = GetConVarInt(g_hCvar_Reject);

switch (iReject)
{
case (2):
{
LogMessage("Banning %L for %d minutes (Family share)", client, GetConVarInt(g_hCvar_RejectDuration));
ServerCommand("sm_ban #%i %d \"%s\"", GetClientUserId(client), GetConVarInt(g_hCvar_RejectDuration), rejectMessage);
}
case (1):
{
LogMessage("Kicking %L (Family share)", client);
ServerCommand("sm_kick #%i \"%s\"", GetClientUserId(client), rejectMessage);
}
}
}

// Credit to Dr. McKay
// https://forums.alliedmods.net/showthread.php?t=233257
stock int GetAppID() {
Expand All @@ -455,7 +426,6 @@ stock int GetAppID() {
return -1;
}

#if defined _SteamWorks_Included
stock int GetClientOfAuthId(int authid)
{
for(int i = 1; i <= MaxClients; i++)
Expand All @@ -469,7 +439,7 @@ stock int GetClientOfAuthId(int authid)
//Split 1: [U:
//Split 2: 1:
//Split 3: 12345]

int auth = StringToInt(split[2]);
if(auth == authid) return i;
}
Expand All @@ -480,33 +450,48 @@ stock int GetClientOfAuthId(int authid)

public void SteamWorks_OnValidateClient(int ownerauthid, int authid)
{
if (GetConVarInt(g_hCvar_Method) != 1)
if (g_iReject < 1)
return;

int client = GetClientOfAuthId(authid);

if (client < 1 || client > MaxClients)
return;

if (IsFakeClient(client) || IsClientSourceTV(client))
return;

if (CheckWhiteList(client))
return;

if(ownerauthid != authid)
if (ownerauthid != authid)
{
LogMessage("Kicking %L (Family share)", client);
char rejectMessage[255]; GetConVarString(g_hCvar_RejectMessage, rejectMessage, sizeof(rejectMessage));
KickClient(client, rejectMessage);
ApplyPunishement(client);
}
}

/*
//Now using SteamWorks:
EUserHasLicenseForAppResult result = SteamWorks_HasLicenseForApp(client, g_hCvar_AppId.IntValue);

//Debug text: PrintToServer("Client %N License Value: %i", client, view_as<int>(result));
stock void ApplyPunishement(int client)
{
char rejectMessage[255];
GetConVarString(g_hCvar_RejectMessage, rejectMessage, sizeof(rejectMessage));

//No License, kick em:
if(result > k_EUserHasLicenseResultHasLicense)
switch (g_iReject)
{
char rejectMessage[255]; GetConVarString(g_hCvar_RejectMessage, rejectMessage, sizeof(rejectMessage));
KickClient(client, rejectMessage);
case (2):
{
LogAction(-1, -1, "Banning %L for %d minutes (Family share)", client, g_iRejectDuration);

if (g_bSourceBans && GetFeatureStatus(FeatureType_Native, "SBPP_BanPlayer") == FeatureStatus_Available)
SBPP_BanPlayer(0, client, g_iRejectDuration, rejectMessage);
else if (g_bMaterialAdmin && GetFeatureStatus(FeatureType_Native, "MABanPlayer") == FeatureStatus_Available)
MABanPlayer(0, client, MA_BAN_STEAM, g_iRejectDuration, rejectMessage);
else
BanClient(client, g_iRejectDuration, BANFLAG_AUTO, rejectMessage);
}
case (1):
{
LogAction(-1, -1, "Kicking %L (Family share)", client);
KickClient(client, rejectMessage);
}
}
*/
}
#endif
14 changes: 7 additions & 7 deletions sourceknight.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ project:
- source: /Pawn/include
dest: /addons/sourcemod/scripting/include

- name: utilshelper
type: git
repo: https://github.com/srcdslab/sm-plugin-UtilsHelper
unpack:
- source: /addons/sourcemod/scripting/include
dest: /addons/sourcemod/scripting/include

- name: multicolors
type: git
repo: https://github.com/srcdslab/sm-plugin-MultiColors
unpack:
- source: /addons
dest: /addons

- name: sourcebans-pp
type: git
repo: https://github.com/srcdslab/sourcebans-pp
unpack:
- source: /game/addons
dest: /addons

root: /
output: /addons/sourcemod/plugins
targets:
Expand Down
Loading