sharivan

crashmap_zmbr

Jan 8th, 2021
1,543
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Crashed Map Recovery (c) 2009 Jonah Hirsch
  3. *
  4. *
  5. * Loads the map the server was on before it crashed when server restarts
  6. *
  7. *  
  8. * Changelog                            
  9. * ------------ 
  10. * 1.6
  11. *  - Adicionado um timer de 30 segundos para o carregamento do mapa ao invés de faze-lo instantaneamente
  12. * 1.5
  13. *  - Messages are now logged to logs/CMR.log
  14. *  - crashmap.txt is now generated automatically
  15. * 1.4.3
  16. *  - Fixed compile warnings
  17. *  - Backs up and restores nextmap on crash + recover (test feature!)
  18. * 1.4.2
  19. *  - Autoconfig added. cfg/sourcemod/plugin.crashmap.cfg
  20. * 1.4.1
  21. *  - Added FCVAR_DONTRECORD to version cvar
  22. * 1.4
  23. *  - Added sm_crashmap_maxrestarts
  24. *  - Added support for checking if the map being changed to crashes the server
  25. * 1.3
  26. *  - Changed method of enabling/disabling recover time to improve performance
  27. *  - Added sm_crashmap_interval
  28. * 1.2
  29. *  - Added timelimit recovery
  30. *  - Added sm_crashmap_recovertime
  31. * 1.1
  32. *  - Added log message when map is recoevered on restart
  33. * 1.0                                  
  34. *  - Initial Release           
  35. *
  36. *      
  37. */
  38.  
  39. #include <sourcemod>
  40. #include <sdktools>
  41. #define PLUGIN_VERSION "1.6"
  42.  
  43. #define TIME_TO_LOAD 30.0
  44.  
  45. static String:FileLoc[128];
  46. new String:logPath[PLATFORM_MAX_PATH]
  47. new Handle:logFileHandle = INVALID_HANDLE
  48. new Handle:dataFileHandle = INVALID_HANDLE
  49. new Handle:sm_crashmap_enabled = INVALID_HANDLE
  50. new Handle:sm_crashmap_recovertime = INVALID_HANDLE
  51. new Handle:sm_crashmap_interval = INVALID_HANDLE
  52. new Handle:sm_crashmap_maxrestarts = INVALID_HANDLE
  53. new Handle:TimeleftHandle = INVALID_HANDLE
  54. new bool:Recovered = false
  55. new bool:TimelimitChanged = false
  56. new bool:Overwrite = false
  57. new newTimelimit
  58. new String:MapToLoad[256]
  59.  
  60. public Plugin:myinfo =
  61. {
  62.     name = "Crashed Map Recovery",
  63.     author = "Crazydog and modified by SHARIVAN from ΖмBя.™ Clan",
  64.     description = "Reloads map that was being played before server crash",
  65.     version = PLUGIN_VERSION,
  66.     url = "http://theelders.net and http://www.zmbrasil.com.br"
  67. }
  68.  
  69. public OnPluginStart(){
  70.     CreateConVar("sm_crashmap_version", PLUGIN_VERSION, "Crashed Map Recovery Version", FCVAR_PLUGIN | FCVAR_SPONLY | FCVAR_NOTIFY | FCVAR_DONTRECORD);
  71.     sm_crashmap_enabled = CreateConVar("sm_crashmap_enabled", "1", "Enable Crashed Map Recovery? (1=yes 0=no)", FCVAR_NOTIFY, true, 0.0, true, 1.0)
  72.     sm_crashmap_recovertime = CreateConVar("sm_crashmap_recovertime", "0", "Recover timelimit? (1=yes 0=no)", FCVAR_NOTIFY, true, 0.0, true, 1.0)
  73.     sm_crashmap_interval = CreateConVar("sm_crashmap_interval", "20", "Interval between timeleft updates (in seconds)", FCVAR_NOTIFY, true, 1.0)
  74.     sm_crashmap_maxrestarts = CreateConVar("sm_crashmap_maxrestarts", "5", "How many consecutive crashes until server loads the default map", FCVAR_NOTIFY, true, 3.0)
  75.     AutoExecConfig(true, "plugin.crashmap")
  76.     HookConVarChange(sm_crashmap_recovertime, TimerState)
  77.     HookConVarChange(sm_crashmap_interval, IntervalChange)
  78.    
  79.     if(GetConVarInt(sm_crashmap_recovertime) == 1){
  80.         TimeleftHandle = CreateTimer(GetConVarFloat(sm_crashmap_interval), SaveTimeleft, _, TIMER_REPEAT)
  81.     }
  82.     BuildPath(Path_SM, FileLoc, 128, "data/crashmap.txt")
  83.     if(!FileExists(FileLoc)){
  84.         dataFileHandle = OpenFile(FileLoc,"a");
  85.         WriteFileLine(dataFileHandle,"SavedMap");
  86.         WriteFileLine(dataFileHandle,"{");
  87.         WriteFileLine(dataFileHandle,"}");
  88.         CloseHandle(dataFileHandle);
  89.     }
  90.    
  91.     BuildPath(Path_SM, logPath, PLATFORM_MAX_PATH, "/logs/CMR.log")
  92.     if(!FileExists(logPath)){
  93.         logFileHandle = OpenFile(logPath, "a")
  94.         CloseHandle(logFileHandle)
  95.     }
  96. }
  97.  
  98.  
  99.  
  100. public OnMapStart(){
  101.     if(GetConVarInt(sm_crashmap_enabled) == 0){
  102.         return
  103.     }
  104.     if(Recovered){
  105.         new String:CurrentMap[256];
  106.         GetCurrentMap(CurrentMap, sizeof(CurrentMap))
  107.         decl Handle:SavedMap
  108.         SavedMap = CreateKeyValues("SavedMap")
  109.         FileToKeyValues(SavedMap, FileLoc)
  110.         KvJumpToKey(SavedMap, "Map", true)
  111.         KvSetString(SavedMap, "Map", CurrentMap)
  112.         KvRewind(SavedMap)
  113.         KeyValuesToFile(SavedMap, FileLoc)
  114.         CloseHandle(SavedMap)
  115.         return
  116.     }
  117.     if(!Recovered){
  118.         new String:nextmap[256], timeleft, restarts
  119.         decl Handle:SavedMap
  120.         SavedMap = CreateKeyValues("SavedMap")
  121.         FileToKeyValues(SavedMap, FileLoc)
  122.         KvJumpToKey(SavedMap, "Map", true)
  123.         KvGetString(SavedMap, "Map", MapToLoad, sizeof(MapToLoad))
  124.         restarts = KvGetNum(SavedMap, "restarts", 0)
  125.         //LogToFile(logPath, "[CMR] Server restarted, restarts is %i", restarts)
  126.         restarts++
  127.         //LogToFile(logPath, "[CMR] Restarts incremented, restarts is %i", restarts)
  128.         LogToFile(logPath, "Restarts is %i", restarts)
  129.         KvSetNum(SavedMap, "restarts", restarts)
  130.         timeleft = KvGetNum(SavedMap, "Timeleft", 30)
  131.         KvGetString(SavedMap, "Nextmap", nextmap, sizeof(nextmap))
  132.         SetNextMap(nextmap)
  133.         newTimelimit = timeleft/60
  134.         Recovered = true
  135.         if(restarts > GetConVarInt(sm_crashmap_maxrestarts)){
  136.             LogToFile(logPath, "[CMR] Error! %s is causing the server to crash. Please fix!", MapToLoad)
  137.             KvSetNum(SavedMap, "restarts", 0)
  138.             KvRewind(SavedMap)
  139.             KeyValuesToFile(SavedMap, FileLoc)
  140.             CloseHandle(SavedMap)
  141.             return
  142.         }
  143.         KvRewind(SavedMap)
  144.         KeyValuesToFile(SavedMap, FileLoc)
  145.         CloseHandle(SavedMap)
  146.         if (GetConVarInt(sm_crashmap_recovertime) == 1) {
  147.             LogToFile(logPath, "[CMR] %s loaded after server crash. Timelimit set to %i", MapToLoad, timeleft/60)
  148.         } else {
  149.             LogToFile(logPath, "[CMR] %s loaded after server crash.", MapToLoad)
  150.         }
  151.        
  152.         CreateTimer(TIME_TO_LOAD, ChangeMapTimer, _, TIMER_FLAG_NO_MAPCHANGE);
  153.        
  154.         return
  155.     }
  156. }
  157.  
  158. public Action:ChangeMapTimer(Handle:timer) {
  159.     ForceChangeLevel(MapToLoad, "Crashed Map Recovery");
  160.    
  161.     return Plugin_Stop;
  162. }
  163.  
  164. public OnMapEnd(){
  165. }
  166.  
  167. public Action:SaveTimeleft(Handle:timer){
  168.     if(Overwrite){
  169.         new timeleft
  170.         if(!GetMapTimeLeft(timeleft)){
  171.             if(!GetMapTimeLimit(timeleft)){
  172.                 timeleft = 30
  173.             }
  174.         }
  175.         new String:nextmap[256]
  176.         GetNextMap(nextmap, sizeof(nextmap))
  177.         decl Handle:SavedMap
  178.         SavedMap = CreateKeyValues("SavedMap")
  179.         FileToKeyValues(SavedMap, FileLoc)
  180.         KvJumpToKey(SavedMap, "Map", true)
  181.         KvSetNum(SavedMap, "Timeleft", timeleft)
  182.         KvSetString(SavedMap, "Nextmap", nextmap)
  183.         KvRewind(SavedMap)
  184.         KeyValuesToFile(SavedMap, FileLoc)
  185.         CloseHandle(SavedMap)
  186.     }
  187. }
  188.  
  189. public OnClientAuthorized(client){
  190.     decl Handle:SavedMap
  191.     SavedMap = CreateKeyValues("SavedMap")
  192.     FileToKeyValues(SavedMap, FileLoc)
  193.     KvJumpToKey(SavedMap, "Map", true)
  194.     KvSetNum(SavedMap, "restarts", 0)
  195.     KvRewind(SavedMap)
  196.     KeyValuesToFile(SavedMap, FileLoc)
  197.     CloseHandle(SavedMap)
  198.     if(!TimelimitChanged && GetConVarInt(sm_crashmap_recovertime) == 1){
  199.         ServerCommand("mp_timelimit %i", newTimelimit)
  200.         TimelimitChanged = true
  201.         Overwrite = true
  202.     }
  203. }
  204.  
  205. public TimerState(Handle:convar, const String:oldValue[], const String:newValue[]){
  206.     if(GetConVarInt(sm_crashmap_recovertime) < 1){
  207.         if(TimeleftHandle != INVALID_HANDLE){
  208.             KillTimer(TimeleftHandle)
  209.             TimeleftHandle = INVALID_HANDLE
  210.         }
  211.     }
  212.     if(GetConVarInt(sm_crashmap_recovertime) > 0){
  213.         new Float:newTime = GetConVarFloat(sm_crashmap_interval)
  214.         TimeleftHandle = CreateTimer(newTime, SaveTimeleft, _, TIMER_REPEAT)
  215.         Overwrite = true
  216.     }
  217. }
  218.  
  219. public IntervalChange(Handle:convar, const String:oldValue[], const String:newValue[]){
  220.     if(TimeleftHandle != INVALID_HANDLE){
  221.         new Float:newTime = StringToFloat(newValue)
  222.         KillTimer(TimeleftHandle)
  223.         TimeleftHandle = INVALID_HANDLE
  224.         TimeleftHandle = CreateTimer(newTime, SaveTimeleft, _, TIMER_REPEAT)
  225.     }
  226. }
RAW Paste Data