Skip to content

Commit f5cb3b0

Browse files
authored
1.0.9
1 parent 7ef952f commit f5cb3b0

File tree

8 files changed

+1178
-553
lines changed

8 files changed

+1178
-553
lines changed

CnD_Sound.cs

Lines changed: 250 additions & 549 deletions
Large diffs are not rendered by default.

Config/Configs.cs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace CnD_Sound.Config
5+
{
6+
public static class Configs
7+
{
8+
public static class Shared {
9+
public static string? CookiesFolderPath { get; set; }
10+
}
11+
12+
private static readonly string ConfigDirectoryName = "config";
13+
private static readonly string ConfigFileName = "config.json";
14+
private static string? _configFilePath;
15+
private static ConfigData? _configData;
16+
17+
private static readonly JsonSerializerOptions SerializationOptions = new()
18+
{
19+
Converters =
20+
{
21+
new JsonStringEnumConverter()
22+
},
23+
WriteIndented = true,
24+
AllowTrailingCommas = true,
25+
ReadCommentHandling = JsonCommentHandling.Skip,
26+
};
27+
28+
public static bool IsLoaded()
29+
{
30+
return _configData is not null;
31+
}
32+
33+
public static ConfigData GetConfigData()
34+
{
35+
if (_configData is null)
36+
{
37+
throw new Exception("Config not yet loaded.");
38+
}
39+
40+
return _configData;
41+
}
42+
43+
public static ConfigData Load(string modulePath)
44+
{
45+
var configFileDirectory = Path.Combine(modulePath, ConfigDirectoryName);
46+
if(!Directory.Exists(configFileDirectory))
47+
{
48+
Directory.CreateDirectory(configFileDirectory);
49+
}
50+
51+
_configFilePath = Path.Combine(configFileDirectory, ConfigFileName);
52+
if (File.Exists(_configFilePath))
53+
{
54+
_configData = JsonSerializer.Deserialize<ConfigData>(File.ReadAllText(_configFilePath), SerializationOptions);
55+
}
56+
else
57+
{
58+
_configData = new ConfigData();
59+
}
60+
61+
if (_configData is null)
62+
{
63+
throw new Exception("Failed to load configs.");
64+
}
65+
66+
SaveConfigData(_configData);
67+
68+
return _configData;
69+
}
70+
71+
private static void SaveConfigData(ConfigData configData)
72+
{
73+
if (_configFilePath is null)
74+
{
75+
throw new Exception("Config not yet loaded.");
76+
}
77+
78+
File.WriteAllText(_configFilePath, JsonSerializer.Serialize(configData, SerializationOptions));
79+
}
80+
81+
public class ConfigData
82+
{
83+
public bool DisableLoopConnections { get; set; }
84+
public bool RemoveDefaultDisconnect { get; set; }
85+
public string InGameSoundConnect { get; set; }
86+
public string InGameSoundDisconnect { get; set; }
87+
public string InGameAllowDisableCommandsOnlyForGroups { get; set; }
88+
public string InGameSoundDisableCommands { get; set; }
89+
public int RemovePlayerCookieOlderThanXDays { get; set; }
90+
public string empty { get; set; }
91+
public bool SendLogToText { get; set; }
92+
public string Log_TextConnectMessageFormat { get; set; }
93+
public string Log_TextDisconnectMessageFormat { get; set; }
94+
public int Log_AutoDeleteLogsMoreThanXdaysOld { get; set; }
95+
private int _Log_SendLogToDiscordOnMode;
96+
public int Log_SendLogToDiscordOnMode
97+
{
98+
get => _Log_SendLogToDiscordOnMode;
99+
set
100+
{
101+
_Log_SendLogToDiscordOnMode = value;
102+
if (_Log_SendLogToDiscordOnMode < 0 || _Log_SendLogToDiscordOnMode > 3)
103+
{
104+
Log_SendLogToDiscordOnMode = 0;
105+
Console.WriteLine("|||||||||||||||||||||||||||||||||||||||||||||||| I N V A L I D ||||||||||||||||||||||||||||||||||||||||||||||||");
106+
Console.WriteLine("[Vote-GoldKingZ] Log_SendLogToDiscordOnMode: is invalid, setting to default value (0) Please Choose 0 or 1 or 2 or 3.");
107+
Console.WriteLine("[Vote-GoldKingZ] Log_SendLogToDiscordOnMode (0) = Disable");
108+
Console.WriteLine("[Vote-GoldKingZ] Log_SendLogToDiscordOnMode (1) = Text Only");
109+
Console.WriteLine("[Vote-GoldKingZ] Log_SendLogToDiscordOnMode (2) = Text With + Name + Hyperlink To Steam Profile");
110+
Console.WriteLine("[Vote-GoldKingZ] Log_SendLogToDiscordOnMode (3) = Text With + Name + Hyperlink To Steam Profile + Profile Picture");
111+
Console.WriteLine("|||||||||||||||||||||||||||||||||||||||||||||||| I N V A L I D ||||||||||||||||||||||||||||||||||||||||||||||||");
112+
}
113+
}
114+
}
115+
private string? _Log_DiscordSideColor;
116+
public string Log_DiscordSideColor
117+
{
118+
get => _Log_DiscordSideColor!;
119+
set
120+
{
121+
_Log_DiscordSideColor = value;
122+
if (_Log_DiscordSideColor.StartsWith("#"))
123+
{
124+
Log_DiscordSideColor = _Log_DiscordSideColor.Substring(1);
125+
}
126+
}
127+
}
128+
public string Log_DiscordWebHookURL { get; set; }
129+
public string Log_DiscordConnectMessageFormat { get; set; }
130+
public string Log_DiscordDisconnectMessageFormat { get; set; }
131+
public string Log_DiscordUsersWithNoAvatarImage { get; set; }
132+
public string empty2 { get; set; }
133+
public string Information_For_You_Dont_Delete_it { get; set; }
134+
135+
public ConfigData()
136+
{
137+
DisableLoopConnections = true;
138+
RemoveDefaultDisconnect = true;
139+
InGameSoundConnect = "sounds/buttons/blip1.vsnd_c";
140+
InGameSoundDisconnect = "sounds/player/taunt_clap_01.vsnd_c";
141+
InGameAllowDisableCommandsOnlyForGroups = "";
142+
InGameSoundDisableCommands = "!stopsound,!stopsounds";
143+
RemovePlayerCookieOlderThanXDays = 7;
144+
empty = "-----------------------------------------------------------------------------------";
145+
SendLogToText = false;
146+
Log_TextConnectMessageFormat = "[{DATE} - {TIME}] {PLAYERNAME} Connected [{SHORTCOUNTRY} - {CITY}] [{STEAMID} - {IP}]";
147+
Log_TextDisconnectMessageFormat = "[{DATE} - {TIME}] {PLAYERNAME} Disconnected [{SHORTCOUNTRY} - {CITY}] [{STEAMID64}] [{STEAMID} - {IP}] [{REASON}]";
148+
Log_AutoDeleteLogsMoreThanXdaysOld = 7;
149+
Log_SendLogToDiscordOnMode = 0;
150+
Log_DiscordSideColor = "00FFFF";
151+
Log_DiscordWebHookURL = "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
152+
Log_DiscordConnectMessageFormat = "{PLAYERNAME} Connected [{LONGCOUNTRY} - {CITY}]";
153+
Log_DiscordDisconnectMessageFormat = "{PLAYERNAME} Disconnected [{LONGCOUNTRY} - {CITY}] [{REASON}]";
154+
Log_DiscordUsersWithNoAvatarImage = "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/b5/b5bd56c1aa4644a474a2e4972be27ef9e82e517e_full.jpg";
155+
empty2 = "-----------------------------------------------------------------------------------";
156+
Information_For_You_Dont_Delete_it = " Vist [https://github.com/oqyh/cs2-Connect-Disconnect-Sound/tree/main?tab=readme-ov-file#-configuration-] To Understand All Above";
157+
}
158+
}
159+
}
160+
}

CounterStrikeSharp.API.dll

34 KB
Binary file not shown.

Helper/CnDJson.cs

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
using Newtonsoft.Json;
2+
using CnD_Sound.Config;
3+
using System.Text;
4+
using System.Drawing;
5+
6+
namespace CnD_Sound;
7+
8+
public class CnD_SoundJson
9+
{
10+
private static readonly HttpClient _httpClient = new HttpClient();
11+
private static readonly HttpClient httpClient = new HttpClient();
12+
private class PersonData
13+
{
14+
public int Id { get; set; }
15+
public bool BoolValue { get; set; }
16+
public DateTime Date { get; set; }
17+
}
18+
public static async Task SendToDiscordWebhookNormal(string webhookUrl, string message)
19+
{
20+
try
21+
{
22+
var payload = new { content = message };
23+
var jsonPayload = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
24+
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
25+
26+
var response = await _httpClient.PostAsync(webhookUrl, content).ConfigureAwait(false);
27+
28+
29+
}
30+
catch
31+
{
32+
}
33+
}
34+
35+
public static async Task SendToDiscordWebhookNameLink(string webhookUrl, string message, string steamUserId, string STEAMNAME)
36+
{
37+
try
38+
{
39+
string profileLink = GetSteamProfileLink(steamUserId);
40+
int colorss = int.Parse(Configs.GetConfigData().Log_DiscordSideColor, System.Globalization.NumberStyles.HexNumber);
41+
Color color = Color.FromArgb(colorss >> 16, (colorss >> 8) & 0xFF, colorss & 0xFF);
42+
using (var httpClient = new HttpClient())
43+
{
44+
var embed = new
45+
{
46+
type = "rich",
47+
title = STEAMNAME,
48+
url = profileLink,
49+
description = message,
50+
color = color.ToArgb() & 0xFFFFFF
51+
};
52+
53+
var payload = new
54+
{
55+
embeds = new[] { embed }
56+
};
57+
58+
var jsonPayload = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
59+
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
60+
var response = await _httpClient.PostAsync(webhookUrl, content).ConfigureAwait(false);
61+
62+
}
63+
}
64+
catch
65+
{
66+
}
67+
}
68+
public static async Task SendToDiscordWebhookNameLinkWithPicture(string webhookUrl, string message, string steamUserId, string STEAMNAME)
69+
{
70+
try
71+
{
72+
string profileLink = GetSteamProfileLink(steamUserId);
73+
string profilePictureUrl = await GetProfilePictureAsync(steamUserId, Configs.GetConfigData().Log_DiscordUsersWithNoAvatarImage);
74+
int colorss = int.Parse(Configs.GetConfigData().Log_DiscordSideColor, System.Globalization.NumberStyles.HexNumber);
75+
Color color = Color.FromArgb(colorss >> 16, (colorss >> 8) & 0xFF, colorss & 0xFF);
76+
using (var httpClient = new HttpClient())
77+
{
78+
var embed = new
79+
{
80+
type = "rich",
81+
description = message,
82+
color = color.ToArgb() & 0xFFFFFF,
83+
author = new
84+
{
85+
name = STEAMNAME,
86+
url = profileLink,
87+
icon_url = profilePictureUrl
88+
}
89+
};
90+
91+
var payload = new
92+
{
93+
embeds = new[] { embed }
94+
};
95+
96+
var jsonPayload = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
97+
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
98+
var response = await _httpClient.PostAsync(webhookUrl, content).ConfigureAwait(false);
99+
}
100+
}
101+
catch
102+
{
103+
104+
}
105+
}
106+
public static async Task<string> GetProfilePictureAsync(string steamId64, string defaultImage)
107+
{
108+
try
109+
{
110+
string apiUrl = $"https://steamcommunity.com/profiles/{steamId64}/?xml=1";
111+
112+
HttpResponseMessage response = await httpClient.GetAsync(apiUrl);
113+
114+
if (response.IsSuccessStatusCode)
115+
{
116+
string xmlResponse = await response.Content.ReadAsStringAsync();
117+
int startIndex = xmlResponse.IndexOf("<avatarFull><![CDATA[") + "<avatarFull><![CDATA[".Length;
118+
int endIndex = xmlResponse.IndexOf("]]></avatarFull>", startIndex);
119+
120+
if (endIndex >= 0)
121+
{
122+
string profilePictureUrl = xmlResponse.Substring(startIndex, endIndex - startIndex);
123+
return profilePictureUrl;
124+
}
125+
else
126+
{
127+
return defaultImage;
128+
}
129+
}
130+
else
131+
{
132+
return null!;
133+
}
134+
}
135+
catch
136+
{
137+
return null!;
138+
}
139+
}
140+
public static string GetSteamProfileLink(string userId)
141+
{
142+
return $"https://steamcommunity.com/profiles/{userId}";
143+
}
144+
145+
public static void SaveToJsonFile(int id, bool boolValue, DateTime date)
146+
{
147+
string cookiesFilePath = Configs.Shared.CookiesFolderPath!;
148+
string Fpath = Path.Combine(cookiesFilePath,"../../plugins/CnD_Sound/Cookies/");
149+
string Fpathc = Path.Combine(cookiesFilePath,"../../plugins/CnD_Sound/Cookies/CnD_Sound_Cookies.json");
150+
try
151+
{
152+
if(!Directory.Exists(Fpath))
153+
{
154+
Directory.CreateDirectory(Fpath);
155+
}
156+
157+
if (!File.Exists(Fpathc))
158+
{
159+
File.WriteAllText(Fpathc, "[]");
160+
}
161+
162+
List<PersonData> allPersonsData;
163+
string jsonData = File.ReadAllText(Fpathc);
164+
allPersonsData = JsonConvert.DeserializeObject<List<PersonData>>(jsonData) ?? new List<PersonData>();
165+
166+
PersonData existingPerson = allPersonsData.Find(p => p.Id == id)!;
167+
168+
if (existingPerson != null)
169+
{
170+
existingPerson.BoolValue = boolValue;
171+
existingPerson.Date = date;
172+
}
173+
else
174+
{
175+
PersonData newPerson = new PersonData { Id = id, BoolValue = boolValue, Date = date };
176+
allPersonsData.Add(newPerson);
177+
}
178+
allPersonsData.RemoveAll(p => (DateTime.Now - p.Date).TotalDays > Configs.GetConfigData().RemovePlayerCookieOlderThanXDays);
179+
180+
string updatedJsonData = JsonConvert.SerializeObject(allPersonsData, Formatting.Indented);
181+
try
182+
{
183+
File.WriteAllText(Fpathc, updatedJsonData);
184+
}catch
185+
{
186+
}
187+
}catch
188+
{
189+
}
190+
}
191+
192+
public static bool RetrieveBoolValueById(int targetId)
193+
{
194+
string cookiesFilePath = Configs.Shared.CookiesFolderPath!;
195+
string Fpath = Path.Combine(cookiesFilePath,"../../plugins/CnD_Sound/Cookies/");
196+
string Fpathc = Path.Combine(cookiesFilePath,"../../plugins/CnD_Sound/Cookies/CnD_Sound_Cookies.json");
197+
try
198+
{
199+
if (File.Exists(Fpathc))
200+
{
201+
string jsonData = File.ReadAllText(Fpathc);
202+
List<PersonData> allPersonsData = JsonConvert.DeserializeObject<List<PersonData>>(jsonData) ?? new List<PersonData>();
203+
204+
PersonData targetPerson = allPersonsData.Find(p => p.Id == targetId)!;
205+
206+
if (targetPerson != null)
207+
{
208+
if (DateTime.Now - targetPerson.Date <= TimeSpan.FromDays(Configs.GetConfigData().RemovePlayerCookieOlderThanXDays))
209+
{
210+
return targetPerson.BoolValue;
211+
}
212+
else
213+
{
214+
allPersonsData.Remove(targetPerson);
215+
string updatedJsonData = JsonConvert.SerializeObject(allPersonsData, Formatting.Indented);
216+
try
217+
{
218+
File.WriteAllText(Fpathc, updatedJsonData);
219+
}catch
220+
{
221+
}
222+
}
223+
}
224+
}
225+
return false;
226+
}catch
227+
{
228+
return false;
229+
}
230+
}
231+
}

0 commit comments

Comments
 (0)