#pragma semicolon 1
#pragma newdecls required
#include <sdktools_sound>
#include <sdktools_stringtables>
ArrayList
hSoundList;
bool
bHooked;
public Plugin myinfo =
{
name = "[Any] Welcome Sound/Музыка при входе",
version = "1.0.2_20.08.2025 (rewritten by Grey83)",
description = "Музыка при входе",
author = "Nek.'a 2x2 | ggwp.site ",
url = "https://ggwp.site/"
};
public void OnPluginStart()
{
hSoundList = new ArrayList(ByteCountToCells(PLATFORM_MAX_PATH));
}
public void OnMapStart()
{
if(hSoundList.Length) hSoundList.Clear();
char sPath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, sPath, sizeof(sPath), "configs/welcome_sound.ini");
File hFile = OpenFile(sPath, "r");
if(!hFile)
{
ThrowError("Файл [%s] не существует!", sPath);
}
int pos, len;
char line[PLATFORM_MAX_PATH];
// Упрощено условие цикла
while (ReadFileLine(hFile, line, sizeof(line)))
{
if ((pos = StrContains(line, "//")) != -1) line[pos] = 0;
if ((pos = StrContains(line, "#")) != -1) line[pos] = 0;
if ((pos = StrContains(line, ";")) != -1) line[pos] = 0;
// Переписана и исправлена проверка расширения файла
// Получаем длину строки после TrimString
len = TrimString(line);
// Проверяем длину и расширение файла
if(len < 5 ||
(StrContains(line, ".mp3", false) != (len - 4) &&
StrContains(line, ".wav", false) != (len - 4))
|| !PrecacheSound(line, false))
continue;
hSoundList.PushString(line);
Format(line, sizeof(line), "sound/%s", line);
AddFileToDownloadsTable(line);
}
delete hFile;
// Переписана логика управления хуком
// Всегда отписываемся, если подписаны, на случай изменения конфига
if (bHooked)
{
UnhookEvent("player_activate", Event_Activate, EventHookMode_Pre);
bHooked = false;
}
// Подписываемся, если есть звуки
if (hSoundList.Length > 0)
{
HookEvent("player_activate", Event_Activate, EventHookMode_Pre);
bHooked = true;
}
}
public void Event_Activate(Event event, const char[] name, bool dontBroadcast)
{
int client = GetClientOfUserId(event.GetInt("userid"));
if(!client || !IsClientInGame(client) || IsFakeClient(client))
return;
char sSound[PLATFORM_MAX_PATH];
hSoundList.GetString(GetRandomInt(0, hSoundList.Length - 1), sSound, sizeof(sSound));
EmitSoundToClient(client, sSound);
}