auto_steam_update.sp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #pragma semicolon 1
  2. #include <sourcemod>
  3. #include <sdktools>
  4. #include <steamtools>
  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 "1.9.1"
  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. updaterCvar = CreateConVar("auto_steam_update_auto_update", "1", "Enables automatic plugin updating (has no effect if Updater is not installed)");
  57. sv_password = FindConVar("sv_password");
  58. RegAdminCmd("sm_postponeupdate", Command_PostponeUpdate, ADMFLAG_RCON, "Postpone a pending server restart for a Steam update");
  59. RegAdminCmd("sm_updatetimer", Command_ForceRestart, ADMFLAG_RCON, "Force the server update timer to start immediately");
  60. hudText = CreateHudSynchronizer();
  61. if(hudText == INVALID_HANDLE) {
  62. LogMessage("HUD text is not supported on this mod. The persistant timer will not display.");
  63. } else {
  64. LogMessage("HUD text is supported on this mod. The persistant timer will display.");
  65. }
  66. decl String:folder[16];
  67. GetGameFolderName(folder, sizeof(folder));
  68. if(StrEqual(folder, "tf", false)) {
  69. isTF = true;
  70. }
  71. }
  72. public OnMapStart() {
  73. if(isTF) {
  74. PrecacheSound(ALERT_SOUND); // this sound is in TF2 only
  75. }
  76. }
  77. public OnClientPostAdminCheck(client) {
  78. if(CheckCommandAccess(client, "BypassAutoSteamUpdateDisallow", ADMFLAG_GENERIC, true)) {
  79. return;
  80. }
  81. if(disallowPlayers) {
  82. decl String:kickMessage[255];
  83. GetConVarString(kickMessageCvar, kickMessage, sizeof(kickMessage));
  84. KickClient(client, kickMessage);
  85. }
  86. }
  87. public Action:Steam_RestartRequested() {
  88. startTimer();
  89. return Plugin_Continue;
  90. }
  91. public Action:Command_ForceRestart(client, args) {
  92. suspendPlugin = false;
  93. LogAction(client, -1, "%L manually triggered an update timer", client);
  94. startTimer(true);
  95. return Plugin_Handled;
  96. }
  97. startTimer(bool:forced = false) {
  98. if(suspendPlugin) {
  99. return;
  100. }
  101. if(!IsServerPopulated()) { // If there's no clients in the server, go ahead and restart it
  102. LogMessage("Received a master server restart request, and there are no players in the server. Restarting to update.");
  103. ServerCommand("_restart");
  104. return;
  105. }
  106. new lock = GetConVarInt(lockCvar);
  107. if(lock == 1) {
  108. decl String:password[255];
  109. GetConVarString(passwordCvar, password, sizeof(password));
  110. GetConVarString(sv_password, originalPassword, sizeof(originalPassword));
  111. SetConVarString(sv_password, password);
  112. }
  113. if(lock == 2) {
  114. disallowPlayers = true;
  115. }
  116. if(!forced) {
  117. LogMessage("Received a master server restart request, beginning restart timer.");
  118. }
  119. timeRemaining = GetConVarInt(timerCvar) * 60;
  120. timeRemaining++;
  121. restartTimer = CreateTimer(1.0, DoTimer, INVALID_HANDLE, TIMER_REPEAT);
  122. suspendPlugin = true;
  123. return;
  124. }
  125. public Action:DoTimer(Handle:timer) {
  126. timeRemaining--;
  127. if(timeRemaining <= -1) {
  128. LogMessage("Restarting server for Steam update.");
  129. for(new i = 1; i <= MaxClients; i++) {
  130. if (!IsClientAuthorized(i) || !IsClientInGame(i) || IsFakeClient(i)) {
  131. continue;
  132. }
  133. new String:kickMessage[255];
  134. GetConVarString(shutdownMessageCvar, kickMessage, sizeof(kickMessage));
  135. KickClient(i, kickMessage);
  136. }
  137. ServerCommand("_restart");
  138. return Plugin_Stop;
  139. }
  140. if(timeRemaining / 60 <= GetConVarInt(messageTimeCvar)) {
  141. if(hudText != INVALID_HANDLE) {
  142. for(new i = 1; i <= MaxClients; i++) {
  143. if(!IsClientConnected(i) || !IsClientInGame(i) || IsFakeClient(i)) {
  144. continue;
  145. }
  146. SetHudTextParams(GetConVarFloat(hudXCvar), GetConVarFloat(hudYCvar), 1.0, GetConVarInt(hudRCvar), GetConVarInt(hudGCvar), GetConVarInt(hudBCvar), 255);
  147. ShowSyncHudText(i, hudText, "Update: %i:%02i", timeRemaining / 60, timeRemaining % 60);
  148. }
  149. }
  150. if(timeRemaining > 60 && timeRemaining % 60 == 0) {
  151. PrintHintTextToAll("A game update has been released.\nThis server will shut down to update in %i minutes.", timeRemaining / 60);
  152. PrintToServer("[SM] A game update has been released. This server will shut down to update in %i minutes.", timeRemaining / 60);
  153. if(isTF) {
  154. EmitSoundToAll(ALERT_SOUND);
  155. }
  156. }
  157. if(timeRemaining == 60) {
  158. PrintHintTextToAll("A game update has been released.\nThis server will shut down to update in 1 minute.");
  159. PrintToServer("[SM] A game update has been released. This server will shut down to update in 1 minute.");
  160. if(isTF) {
  161. EmitSoundToAll(ALERT_SOUND);
  162. }
  163. }
  164. }
  165. if(timeRemaining <= 60 && hudText == INVALID_HANDLE) {
  166. PrintCenterTextAll("Update: %i:%02i", timeRemaining / 60, timeRemaining % 60);
  167. }
  168. return Plugin_Continue;
  169. }
  170. public Action:Command_PostponeUpdate(client, args) {
  171. if(restartTimer == INVALID_HANDLE) {
  172. ReplyToCommand(client, "[SM] There is no update timer currently running.");
  173. return Plugin_Handled;
  174. }
  175. CloseHandle(restartTimer);
  176. restartTimer = INVALID_HANDLE;
  177. LogAction(client, -1, "%L aborted the update timer.", client);
  178. new Float:delay = GetConVarInt(delayCvar) * 60.0;
  179. CreateTimer(delay, ReenablePlugin);
  180. ReplyToCommand(client, "[SM] The update timer has been cancelled for %i minutes.", GetConVarInt(delayCvar));
  181. PrintHintTextToAll("The update timer has been cancelled for %i minutes.", GetConVarInt(delayCvar));
  182. disallowPlayers = false;
  183. if(GetConVarInt(lockCvar) == 1) {
  184. SetConVarString(sv_password, originalPassword);
  185. }
  186. return Plugin_Handled;
  187. }
  188. public Action:ReenablePlugin(Handle:timer) {
  189. suspendPlugin = false;
  190. return Plugin_Stop;
  191. }
  192. IsServerPopulated() {
  193. for(new i = 1; i <= MaxClients; i++) {
  194. if(IsClientConnected(i) && !IsFakeClient(i)) {
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. /////////////////////////////////
  201. public OnAllPluginsLoaded() {
  202. new Handle:convar;
  203. if(LibraryExists("updater")) {
  204. Updater_AddPlugin(UPDATE_URL);
  205. new String:newVersion[10];
  206. Format(newVersion, sizeof(newVersion), "%sA", PLUGIN_VERSION);
  207. convar = CreateConVar("auto_steam_update_version", newVersion, "Automatic Steam Update Version", FCVAR_DONTRECORD|FCVAR_NOTIFY|FCVAR_CHEAT);
  208. } else {
  209. convar = CreateConVar("auto_steam_update_version", PLUGIN_VERSION, "Automatic Steam Update Version", FCVAR_DONTRECORD|FCVAR_NOTIFY|FCVAR_CHEAT);
  210. }
  211. HookConVarChange(convar, Callback_VersionConVarChanged);
  212. }
  213. public Callback_VersionConVarChanged(Handle:convar, const String:oldValue[], const String:newValue[]) {
  214. ResetConVar(convar);
  215. }
  216. public Action:Updater_OnPluginDownloading() {
  217. if(!GetConVarBool(updaterCvar)) {
  218. return Plugin_Handled;
  219. }
  220. return Plugin_Continue;
  221. }
  222. public OnLibraryAdded(const String:name[]) {
  223. if(StrEqual(name, "updater")) {
  224. Updater_AddPlugin(UPDATE_URL);
  225. }
  226. }
  227. public Updater_OnPluginUpdated() {
  228. ReloadPlugin();
  229. }