generated from K4ryuu/Project_Template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
291 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Text.Json; | ||
|
||
internal class CFG | ||
{ | ||
public static Config config = new(); | ||
|
||
public void CheckConfig(string moduleDirectory) | ||
{ | ||
string path = Path.Join(moduleDirectory, "config.json"); | ||
|
||
if (!File.Exists(path)) | ||
{ | ||
CreateAndWriteFile(path); | ||
} | ||
|
||
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) | ||
using (StreamReader sr = new StreamReader(fs)) | ||
{ | ||
// Deserialize the JSON from the file and load the configuration. | ||
config = JsonSerializer.Deserialize<Config>(sr.ReadToEnd())!; | ||
} | ||
} | ||
|
||
private static void CreateAndWriteFile(string path) | ||
{ | ||
|
||
using (FileStream fs = File.Create(path)) | ||
{ | ||
// File is created, and fs will automatically be disposed when the using block exits. | ||
} | ||
|
||
Console.WriteLine($"File created: {File.Exists(path)}"); | ||
|
||
Config config = new Config | ||
{ | ||
SteamWebAPI = "-", | ||
}; | ||
|
||
// Serialize the config object to JSON and write it to the file. | ||
string jsonConfig = JsonSerializer.Serialize(config, new JsonSerializerOptions() | ||
{ | ||
WriteIndented = true | ||
}); | ||
File.WriteAllText(path, jsonConfig); | ||
} | ||
} | ||
|
||
internal class Config | ||
{ | ||
public string? SteamWebAPI { get; set; } | ||
public int MinimumHourNonPrime { get; set; } | ||
public int MinimumHourPrime { get; set; } | ||
public int MinimumLevelNonPrime { get; set; } | ||
public int MinimumLevelPrime { get; set; } | ||
public bool BlockPrivateProfile { get; set; } | ||
public bool BlockNonPrime { get; set; } | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
using System.Text; | ||
using CounterStrikeSharp.API.Core; | ||
using CounterStrikeSharp.API.Core.Attributes; | ||
using CounterStrikeSharp.API.Core.Attributes.Registration; | ||
using CounterStrikeSharp.API.Modules.Utils; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json.Linq; | ||
using CounterStrikeSharp.API; | ||
|
||
namespace K4ryuuSteamRestrict | ||
{ | ||
[MinimumApiVersion(16)] | ||
public class SteamRestrictPlugin : BasePlugin | ||
{ | ||
public class SteamUserInfo | ||
{ | ||
public int SteamLevel { get; set; } | ||
public int CSGOPlaytime { get; set; } | ||
public bool IsPrivate { get; set; } | ||
public bool HasPrime { get; set; } | ||
} | ||
|
||
public override string ModuleName => "Steam Restrict"; | ||
public override string ModuleVersion => "1.0.0"; | ||
public override string ModuleAuthor => "K4ryuu"; | ||
|
||
public override void Load(bool hotReload) | ||
{ | ||
new CFG().CheckConfig(ModuleDirectory); | ||
} | ||
|
||
[GameEventHandler] | ||
public HookResult OnClientConnect(EventPlayerConnectFull @event, GameEventInfo info) | ||
{ | ||
CCSPlayerController player = @event.Userid; | ||
|
||
if (player == null || !player.IsValid || player.IsBot || CFG.config.SteamWebAPI == "-") | ||
return HookResult.Continue; | ||
|
||
_ = FetchSteamUserInfo(player); | ||
|
||
|
||
return HookResult.Continue; | ||
} | ||
|
||
private async Task<SteamUserInfo> FetchSteamUserInfo(CCSPlayerController player) | ||
{ | ||
SteamUserInfo userInfo = new SteamUserInfo(); | ||
|
||
using (HttpClient httpClient = new HttpClient()) | ||
{ | ||
string steamId = player.SteamID.ToString(); | ||
string steamWebAPIKey = CFG.config.SteamWebAPI!; | ||
|
||
string gamesUrl = $"https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key={steamWebAPIKey}&steamid={steamId}&format=json"; | ||
HttpResponseMessage gamesResponse = await httpClient.GetAsync(gamesUrl); | ||
|
||
if (gamesResponse.IsSuccessStatusCode) | ||
{ | ||
string gamesJson = await gamesResponse.Content.ReadAsStringAsync(); | ||
userInfo.CSGOPlaytime = ParseCS2Playtime(gamesJson) / 60; | ||
|
||
JArray games = (JObject.Parse(gamesJson)["response"]!["games"] as JArray)!; | ||
bool hasPrime = games.Any(game => (int)game["appid"]! == 54029); | ||
userInfo.HasPrime = hasPrime; | ||
} | ||
else | ||
{ | ||
userInfo.HasPrime = false; | ||
userInfo.CSGOPlaytime = 0; | ||
} | ||
|
||
// Fetch Steam level | ||
string levelUrl = $"http://api.steampowered.com/IPlayerService/GetSteamLevel/v1/?key={steamWebAPIKey}&steamid={steamId}"; | ||
HttpResponseMessage levelResponse = await httpClient.GetAsync(levelUrl); | ||
|
||
if (levelResponse.IsSuccessStatusCode) | ||
{ | ||
string levelJson = await levelResponse.Content.ReadAsStringAsync(); | ||
userInfo.SteamLevel = ParseSteamLevel(levelJson); | ||
} | ||
else | ||
{ | ||
userInfo.SteamLevel = 0; | ||
} | ||
|
||
// Fetch other user information | ||
string userUrl = $"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={steamWebAPIKey}&steamids={steamId}"; | ||
HttpResponseMessage userResponse = await httpClient.GetAsync(userUrl); | ||
|
||
if (userResponse.IsSuccessStatusCode) | ||
{ | ||
string userJson = await userResponse.Content.ReadAsStringAsync(); | ||
ParseSteamUserInfo(userJson, userInfo); | ||
} | ||
else | ||
{ | ||
userInfo.IsPrivate = false; | ||
} | ||
|
||
if (IsRestrictionViolated(userInfo)) | ||
{ | ||
Server.ExecuteCommand($"kickid {player.UserId} \"You have been kicked for not meeting the minimum requirements.\""); | ||
} | ||
} | ||
|
||
return userInfo; | ||
} | ||
|
||
private bool IsRestrictionViolated(SteamUserInfo userInfo) | ||
{ | ||
bool isViolated = false; | ||
|
||
if (userInfo.HasPrime) | ||
{ | ||
if (CFG.config.MinimumHourPrime != -1 && userInfo.CSGOPlaytime < CFG.config.MinimumHourPrime | ||
|| CFG.config.MinimumLevelPrime != -1 && userInfo.SteamLevel < CFG.config.MinimumLevelPrime) | ||
{ | ||
isViolated = true; | ||
} | ||
} | ||
else | ||
{ | ||
if (CFG.config.MinimumHourNonPrime != -1 && userInfo.CSGOPlaytime < CFG.config.MinimumHourNonPrime | ||
|| CFG.config.MinimumLevelNonPrime != -1 && userInfo.SteamLevel < CFG.config.MinimumLevelNonPrime) | ||
{ | ||
isViolated = true; | ||
} | ||
} | ||
|
||
if (CFG.config.BlockPrivateProfile && userInfo.IsPrivate) | ||
{ | ||
isViolated = true; | ||
} | ||
|
||
return isViolated; | ||
} | ||
|
||
private int ParseCS2Playtime(string json) | ||
{ | ||
int csPlaytime = 0; | ||
|
||
try | ||
{ | ||
JObject data = JObject.Parse(json); | ||
JArray games = (data["response"]!["games"] as JArray)!; | ||
|
||
if (games != null) | ||
{ | ||
foreach (var game in games) | ||
{ | ||
int appId = (int)game["appid"]!; | ||
if (appId == 730) | ||
{ | ||
csPlaytime = (int)game["playtime_forever"]!; | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error parsing CS:GO playtime: {ex.Message}"); | ||
} | ||
|
||
return csPlaytime; | ||
} | ||
|
||
private int ParseSteamLevel(string json) | ||
{ | ||
int steamLevel = 0; | ||
|
||
try | ||
{ | ||
JObject data = JObject.Parse(json); | ||
steamLevel = (int)data["response"]!["player_level"]!; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error parsing Steam level: {ex.Message}"); | ||
} | ||
|
||
return steamLevel; | ||
} | ||
|
||
private void ParseSteamUserInfo(string json, SteamUserInfo userInfo) | ||
{ | ||
try | ||
{ | ||
JObject data = JObject.Parse(json); | ||
JArray players = (data["response"]!["players"] as JArray)!; | ||
|
||
if (players != null && players.Count > 0) | ||
{ | ||
var player = players[0]; | ||
|
||
userInfo.IsPrivate = (int)player["communityvisibilitystate"]! != 3; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error parsing Steam user info: {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="CounterStrikeSharp.API" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
</ItemGroup> | ||
</Project> |