auto_steam_update.sp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #pragma semicolon 1
  2. #include <sourcemod>
  3. #include <sdktools>
  4. #include <steampawn>
  5. #undef REQUIRE_PLUGIN
  6. #tryinclude <updater>
  7. #define UPDATE_URL "http://hg.doctormckay.com/public-plugins/raw/default/automatic_steam_update.txt"
  8. #define PLUGIN_VERSION "2.0.0"
  9. #define ALERT_SOUND "ui/system_message_alert.wav"
  10. new Handle:delayCvar;
  11. new Handle:timerCvar;
  12. new Handle:messageTimeCvar;
  13. new Handle:lockCvar;
  14. new Handle:passwordCvar;
  15. new Handle:kickMessageCvar;
  16. new Handle:shutdownMessageCvar;
  17. new Handle:hudXCvar;
  18. new Handle:hudYCvar;
  19. new Handle:hudRCvar;
  20. new Handle:hudGCvar;
  21. new Handle:hudBCvar;
  22. new Handle:updaterCvar;
  23. new Handle:restartTimer;
  24. new bool:suspendPlugin = false;
  25. new timeRemaining = 0;
  26. new bool:disallowPlayers = false;
  27. new String:originalPassword[255];
  28. new bool:isTF = false;
  29. new Handle:hudText;
  30. new Handle:sv_password;
  31. public Plugin:myinfo = {
  32. name = "[ANY] Automatic Steam Update",
  33. author = "Dr. McKay",
  34. description = "Automatically restarts the server to update via Steam",
  35. version = PLUGIN_VERSION,
  36. url = "http://www.doctormckay.com"
  37. };
  38. public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max) {
  39. MarkNativeAsOptional("Updater_AddPlugin");
  40. return APLRes_Success;
  41. }
  42. public OnPluginStart() {
  43. AutoExecConfig(true, "plugin.autosteamupdate");
  44. delayCvar = CreateConVar("auto_steam_update_delay", "5", "How long in minutes the server should wait before starting another countdown after being postponed.");
  45. timerCvar = CreateConVar("auto_steam_update_timer", "5", "How long in minutes the server should count down before restarting.");
  46. messageTimeCvar = CreateConVar("auto_steam_update_message_display_time", "5", "At how much time in minutes left on the timer should the timer be displayed?");
  47. lockCvar = CreateConVar("auto_steam_update_lock", "0", "0 - don't lock the server / 1 - set sv_password to auto_steam_update_password during timer / 2 - don't set a password, but kick everyone who tries to connect during the timer");
  48. passwordCvar = CreateConVar("auto_steam_update_password", "", "The password to set sv_password to if auto_steam_update_lock = 1", FCVAR_PROTECTED);
  49. kickMessageCvar = CreateConVar("auto_steam_update_kickmessage", "The server will shut down soon to acquire Steam updates, so no new connections are allowed", "The message to display to kicked clients if auto_steam_update_lock = 2");
  50. shutdownMessageCvar = CreateConVar("auto_steam_update_shutdown_message", "Server shutting down for Steam update", "The message displayed to clients when the server restarts");
  51. hudXCvar = CreateConVar("auto_steam_update_hud_text_x_pos", "0.01", "X-position for HUD timer (only on supported games) -1 = center", _, true, -1.0, true, 1.0);
  52. hudYCvar = CreateConVar("auto_steam_update_hud_text_y_pos", "0.01", "Y-position for HUD timer (only on supported games) -1 = center", _, true, -1.0, true, 1.0);
  53. hudRCvar = CreateConVar("auto_steam_update_hud_text_red", "0", "Amount of red for the HUD timer (only on supported games)", _, true, 0.0, true, 255.0);
  54. hudGCvar = CreateConVar("auto_steam_update_hud_text_green", "255", "Amount of red for the HUD timer (only on supported games)", _, true, 0.0, true, 255.0);
  55. hudBCvar = CreateConVar("auto_steam_update_hud_text_blue", "0", "Amount of red for the HUD timer (only on supported games)", _, true, 0.0, true, 255.0);
  56. #if defined _updater_included
  57. updaterCvar = CreateConVar("auto_steam_update_auto_update", "1", "Enables automatic plugin updating (has no effect if Updater is not installed)");
  58. #endif
  59. sv_password = FindConVar("sv_password");
  60. RegAdminCmd("sm_postponeupdate", Command_PostponeUpdate, ADMFLAG_RCON, "Postpone a pending server restart for a Steam update");
  61. RegAdminCmd("sm_updatetimer", Command_ForceRestart, ADMFLAG_RCON, "Force the server update timer to start immediately");
  62. hudText = CreateHudSynchronizer();
  63. if(hudText == INVALID_HANDLE) {
  64. LogMessage("HUD text is not supported on this mod. The persistant timer will not display.");
  65. } else {
  66. LogMessage("HUD text is supported on this mod. The persistant timer will display.");
  67. }
  68. decl String:folder[16];
  69. GetGameFolderName(folder, sizeof(folder));
  70. if(StrEqual(folder, "tf", false)) {
  71. isTF = true;
  72. }
  73. }
  74. public OnMapStart() {
  75. if(isTF) {
  76. PrecacheSound(ALERT_SOUND); // this sound is in TF2 only
  77. }
  78. }
  79. public OnClientPostAdminCheck(client) {
  80. if(CheckCommandAccess(client, "BypassAutoSteamUpdateDisallow", ADMFLAG_GENERIC, true)) {
  81. return;
  82. }
  83. if(disallowPlayers) {
  84. decl String:kickMessage[255];
  85. GetConVarString(kickMessageCvar, kickMessage, sizeof(kickMessage));
  86. KickClient(client, kickMessage);
  87. }
  88. }
  89. public void SteamPawn_OnRestartRequested() {
  90. startTimer();
  91. }
  92. public Action:Command_ForceRestart(client, args) {
  93. suspendPlugin = false;
  94. LogAction(client, -1, "%L manually triggered an update timer", client);
  95. startTimer(true);
  96. return Plugin_Handled;
  97. }
  98. startTimer(bool:forced = false) {
  99. if(suspendPlugin) {
  100. return;
  101. }
  102. if(!IsServerPopulated()) { // If there's no clients in the server, go ahead and restart it
  103. LogMessage("Received a master server restart request, and there are no players in the server. Restarting to update.");
  104. ServerCommand("_restart");
  105. return;
  106. }
  107. new lock = GetConVarInt(lockCvar);
  108. if(lock == 1) {
  109. decl String:password[255];
  110. GetConVarString(passwordCvar, password, sizeof(password));
  111. GetConVarString(sv_password, originalPassword, sizeof(originalPassword));
  112. SetConVarString(sv_password, password);
  113. }
  114. if(lock == 2) {
  115. disallowPlayers = true;
  116. }
  117. if(!forced) {
  118. LogMessage("Received a master server restart request, beginning restart timer.");
  119. }
  120. timeRemaining = GetConVarInt(timerCvar) * 60;
  121. timeRemaining++;
  122. restartTimer = CreateTimer(1.0, DoTimer, INVALID_HANDLE, TIMER_REPEAT);
  123. suspendPlugin = true;
  124. return;
  125. }
  126. public Action:DoTimer(Handle:timer) {
  127. timeRemaining--;
  128. if(timeRemaining <= -1) {
  129. LogMessage("Restarting server for Steam update.");
  130. for(new i = 1; i <= MaxClients; i++) {
  131. if (!IsClientAuthorized(i) || !IsClientInGame(i) || IsFakeClient(i)) {
  132. continue;
  133. }
  134. new String:kickMessage[255];
  135. GetConVarString(shutdownMessageCvar, kickMessage, sizeof(kickMessage));
  136. KickClient(i, kickMessage);
  137. }
  138. ServerCommand("_restart");
  139. return Plugin_Stop;
  140. }
  141. if(timeRemaining / 60 <= GetConVarInt(messageTimeCvar)) {
  142. if(hudText != INVALID_HANDLE) {
  143. for(new i = 1; i <= MaxClients; i++) {
  144. if(!IsClientConnected(i) || !IsClientInGame(i) || IsFakeClient(i)) {
  145. continue;
  146. }
  147. SetHudTextParams(GetConVarFloat(hudXCvar), GetConVarFloat(hudYCvar), 1.0, GetConVarInt(hudRCvar), GetConVarInt(hudGCvar), GetConVarInt(hudBCvar), 255);
  148. ShowSyncHudText(i, hudText, "Update: %i:%02i", timeRemaining / 60, timeRemaining % 60);
  149. }
  150. }
  151. if(timeRemaining > 60 && timeRemaining % 60 == 0) {
  152. PrintHintTextToAll("A game update has been released.\nThis server will shut down to update in %i minutes.", timeRemaining / 60);
  153. PrintToServer("[SM] A game update has been released. This server will shut down to update in %i minutes.", timeRemaining / 60);
  154. if(isTF) {
  155. EmitSoundToAll(ALERT_SOUND);
  156. }
  157. }
  158. if(timeRemaining == 60) {
  159. PrintHintTextToAll("A game update has been released.\nThis server will shut down to update in 1 minute.");
  160. PrintToServer("[SM] A game update has been released. This server will shut down to update in 1 minute.");
  161. if(isTF) {
  162. EmitSoundToAll(ALERT_SOUND);
  163. }
  164. }
  165. }
  166. if(timeRemaining <= 60 && hudText == INVALID_HANDLE) {
  167. PrintCenterTextAll("Update: %i:%02i", timeRemaining / 60, timeRemaining % 60);
  168. }
  169. return Plugin_Continue;
  170. }
  171. public Action:Command_PostponeUpdate(client, args) {
  172. if(restartTimer == INVALID_HANDLE) {
  173. ReplyToCommand(client, "[SM] There is no update timer currently running.");
  174. return Plugin_Handled;
  175. }
  176. CloseHandle(restartTimer);
  177. restartTimer = INVALID_HANDLE;
  178. LogAction(client, -1, "%L aborted the update timer.", client);
  179. new Float:delay = GetConVarInt(delayCvar) * 60.0;
  180. CreateTimer(delay, ReenablePlugin);
  181. ReplyToCommand(client, "[SM] The update timer has been cancelled for %i minutes.", GetConVarInt(delayCvar));
  182. PrintHintTextToAll("The update timer has been cancelled for %i minutes.", GetConVarInt(delayCvar));
  183. disallowPlayers = false;
  184. if(GetConVarInt(lockCvar) == 1) {
  185. SetConVarString(sv_password, originalPassword);
  186. }
  187. return Plugin_Handled;
  188. }
  189. public Action:ReenablePlugin(Handle:timer) {
  190. suspendPlugin = false;
  191. return Plugin_Stop;
  192. }
  193. IsServerPopulated() {
  194. for(new i = 1; i <= MaxClients; i++) {
  195. if(IsClientConnected(i) && !IsFakeClient(i)) {
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. /////////////////////////////////
  202. public OnAllPluginsLoaded() {
  203. new Handle:convar;
  204. #if defined _updater_included
  205. if(LibraryExists("updater")) {
  206. Updater_AddPlugin(UPDATE_URL);
  207. new String:newVersion[10];
  208. Format(newVersion, sizeof(newVersion), "%sA", PLUGIN_VERSION);
  209. convar = CreateConVar("auto_steam_update_version", newVersion, "Automatic Steam Update Version", FCVAR_DONTRECORD|FCVAR_NOTIFY|FCVAR_CHEAT);
  210. } else {
  211. convar = CreateConVar("auto_steam_update_version", PLUGIN_VERSION, "Automatic Steam Update Version", FCVAR_DONTRECORD|FCVAR_NOTIFY|FCVAR_CHEAT);
  212. }
  213. #else
  214. convar = CreateConVar("auto_steam_update_version", PLUGIN_VERSION, "Automatic Steam Update Version", FCVAR_DONTRECORD|FCVAR_NOTIFY|FCVAR_CHEAT);
  215. #endif
  216. HookConVarChange(convar, Callback_VersionConVarChanged);
  217. }
  218. public Callback_VersionConVarChanged(Handle:convar, const String:oldValue[], const String:newValue[]) {
  219. ResetConVar(convar);
  220. }
  221. #if defined _updater_included
  222. public Action:Updater_OnPluginDownloading() {
  223. if(!GetConVarBool(updaterCvar)) {
  224. return Plugin_Handled;
  225. }
  226. return Plugin_Continue;
  227. }
  228. public OnLibraryAdded(const String:name[]) {
  229. if(StrEqual(name, "updater")) {
  230. Updater_AddPlugin(UPDATE_URL);
  231. }
  232. }
  233. public Updater_OnPluginUpdated() {
  234. ReloadPlugin();
  235. }
  236. #endif