Advertisement
Leystryku

Old gmod AntiCrash script from 2012 or something

Jun 26th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. -- AntiCrash coded by Flapadar
  2. -- Fixed by Leystryku
  3.  
  4. AntiCrash = {}
  5.  
  6. CreateConVar("anticrash_enabled", "1", FCVAR_ARCHIVE)
  7.  
  8. if ( not GetConVar("anticrash_enabled"):GetBool() ) then
  9.  
  10.     return
  11.  
  12. end
  13.  
  14. if(CLIENT) then
  15.     timer.Simple(3, function()
  16.         RunConsoleCommand("cl_timeout", "90")
  17.     end)
  18.  
  19.     hook.Add("InitPostEntity","timeout", function()
  20.         LocalPlayer():ConCommand("cl_timeout 90")
  21.     end)
  22.    
  23. end
  24.  
  25.  
  26. if SERVER then
  27.  
  28.     util.AddNetworkString("AntiCrash.Pong")
  29.     AddCSLuaFile("anticrash.lua")
  30.  
  31.     function AntiCrash.Ping( ply, cmd, args )
  32.    
  33.         if ( not ply.LastPing or ply.LastPing + 5 < CurTime() ) then
  34.        
  35.             ply.LastPing = CurTime()
  36.            
  37.             net.Start("AntiCrash.Pong")
  38.             net.Send( ply )
  39.  
  40.             --MsgN("Ping !")
  41.         end
  42.        
  43.     end
  44.    
  45.     concommand.Add("_anticrash_ping", AntiCrash.Ping)
  46.  
  47.     return
  48. end
  49.  
  50. timer.Simple(1, function()
  51.     if(IsValid(LocalPlayer())) then
  52.         LocalPlayer():ConCommand("cl_timeout 70")
  53.     end
  54. end)
  55.  
  56.  
  57. AntiCrash.LastMoveTime = CurTime() + 10
  58. AntiCrash.ShouldRetry = true
  59. AntiCrash.Crashed = false
  60. AntiCrash.Spawned = false
  61. AntiCrash.Pending = false
  62. AntiCrash.SpawnTime = 0
  63.  
  64.  
  65.  
  66. function AntiCrash.IsCrashed()
  67.  
  68.     if ( not AntiCrash.Spawned or not LocalPlayer or AntiCrash.Crashed ) then return end
  69.        
  70.     if ( AntiCrash.SpawnTime > CurTime() ) then return end
  71.  
  72.     if ( AntiCrash.LastMoveTime > CurTime() ) then return end
  73.  
  74.     if ( not IsValid(LocalPlayer()) ) then return end
  75.  
  76.     if ( not LocalPlayer():IsFrozen() and not LocalPlayer():InVehicle() ) then
  77.  
  78.         return true
  79.  
  80.     end
  81.  
  82. end
  83.  
  84. function AntiCrash.Pong( um )
  85.  
  86.     AntiCrash.LastMoveTime = CurTime() + 10
  87.     MsgN("[AntiCrash] Connection regained - received pong")
  88.  
  89. end
  90.  
  91. function AntiCrash.Move()
  92.  
  93.     AntiCrash.LastMoveTime = CurTime() + 1
  94.    
  95. end
  96.  
  97. function AntiCrash.InitPostEntity()
  98.  
  99.     AntiCrash.Spawned = true
  100.     AntiCrash.SpawnTime = CurTime() + 5
  101.  
  102. end
  103.  
  104. function AntiCrash.ServerCrash()
  105.  
  106.     local menucrashtime = CurTime()
  107.     local retrytime = menucrashtime + 55
  108.    
  109.     for k , v  in ipairs(player.GetAll()) do
  110.         v.CrashedPing = v:Ping()
  111.     end
  112.  
  113.     local dframe = vgui.Create("DFrame")
  114.     dframe:SetSize(200 , 150)
  115.     dframe:SetTitle("Anti-Crash")
  116.     dframe:Center()
  117.     dframe:MakePopup()
  118.  
  119.     function dframe:Close(...)
  120.         AntiCrash.ShouldRetry = false
  121.         return DFrame.Close(self , ...)
  122.     end
  123.  
  124.     local dlabel = vgui.Create("DLabel")
  125.     dlabel:SetParent(dframe)
  126.     dlabel:SetPos(27 , 30)
  127.     dlabel:SetSize(195 , 25)
  128.     dlabel:SetText(string.format("Autoreconnect in %d seconds!" , retrytime - CurTime()))
  129.  
  130.     function dlabel:Paint( ... )
  131.    
  132.         self:SetText(string.format("Autoreconnect in %d seconds!" , retrytime - CurTime()))
  133.  
  134.     end
  135.  
  136.     local dbutton = vgui.Create("DButton")
  137.     dbutton:SetParent(dframe)
  138.     dbutton:SetPos(5 , 50)
  139.     dbutton:SetSize(190 , 20)
  140.     dbutton:SetText("Reconnect now")
  141.     dbutton.DoClick = function()
  142.         RunConsoleCommand("retry")
  143.     end
  144.  
  145.     local dlabel = vgui.Create("DLabel")
  146.     dlabel:SetParent(dframe)
  147.     dlabel:SetPos(22 , 65)
  148.     dlabel:SetSize(210 , 60)
  149.     dlabel:SetText("Ignore if you're in a map change.\nPlease wait until the timer hits 0.\nReconnecting early might be problematic!\n")
  150.  
  151.     local dbutton = vgui.Create("DButton")
  152.     dbutton:SetParent(dframe)
  153.     dbutton:SetPos(5 , 120)
  154.     dbutton:SetSize(190 , 22)
  155.     dbutton:SetText("Cancel")
  156.     dbutton.DoClick = function()
  157.         AntiCrash.ShouldRetry = false
  158.         dframe:SetVisible(false)
  159.     end
  160.  
  161.  
  162.     hook.Add("Think" , "Crashed" , function()
  163.         for k , v in ipairs(player.GetAll()) do
  164.             if v.CrashedPing != v:Ping() then
  165.                 MsgN("[AntiCrash] Connection regained - ping changed.")
  166.                 hook.Remove("Think" , "Crashed")
  167.                 AntiCrash.Crashed = false
  168.                 AntiCrash.LastMoveTime = CurTime() + 5
  169.             end
  170.         end
  171.        
  172.         /*
  173.         local moving = false
  174.        
  175.         for k , v in ipairs(ents.GetAll()) do
  176.             if v:GetVelocity():Length() > 5 then
  177.                 -- Well, not everything's stopped moving.
  178.                 -- 5 incase some props stuck in another prop and is spazzing or something
  179.                 -- It should stop moving, but i'm not entirely sure
  180.                
  181.                 moving = true
  182.             end
  183.         end
  184.        
  185.         if moving then
  186.             hook.Remove("Think" , "Crashed")
  187.             MsgN("[AntiCrash] Connection regained - movement detected")
  188.             AntiCrash.Crashed = false
  189.             AntiCrash.LastMoveTime = CurTime() + 5
  190.         end
  191.        
  192.         */
  193.        
  194.         if AntiCrash.Crashed and (retrytime - CurTime() - 0.5) < 0 and AntiCrash.LastMoveTime + 5 < CurTime() then
  195.             if AntiCrash.ShouldRetry then
  196.                 RunConsoleCommand("retry")
  197.             end
  198.         elseif AntiCrash.LastMoveTime > CurTime() then
  199.             hook.Remove("Think" , "Crashed")
  200.             AntiCrash.Crashed = false
  201.             if dframe and dframe:IsValid() then
  202.                 dframe:Remove()
  203.             end
  204.         end
  205.     end )
  206.    
  207.    
  208. end
  209.  
  210. function AntiCrash.Think()
  211.  
  212.     if not AntiCrash.Crashed and AntiCrash.IsCrashed() then
  213.  
  214.         RunConsoleCommand("_anticrash_ping")
  215.        
  216.         if AntiCrash.LastMoveTime < CurTime() then
  217.            
  218.             if(not AntiCrash.ShowPanelIn) then
  219.                 AntiCrash.ShowPanelIn = CurTime() + 4
  220.                 print("Showing Panel in 4 seconds, abort if lag done")
  221.             elseif( AntiCrash.ShowPanelIn and AntiCrash.ShowPanelIn > CurTime() ) then
  222.                 MsgN("[AntiCrash] Connection down - Did not receive pong")
  223.                 AntiCrash.Crashed = true
  224.                 AntiCrash.ShouldRetry = true -- This is a seperate crash from the previous, the user might want to reconnect this time.
  225.                 AntiCrash.ShowPanelIn = nil
  226.                
  227.                 AntiCrash.ServerCrash()
  228.                 hook.Call( "ServerCrash" , nil ) -- Incase anyone else wants to hook into server crashes.
  229.                
  230.             end
  231.         else
  232.             AntiCrash.Crashed = false
  233.             AntiCrash.ShowPanelIn = nil
  234.         end
  235.  
  236.         MsgN("[AntiCrash] Connection lost - sending ping")
  237.     end
  238.    
  239. end
  240.  
  241. hook.Add("InitPostEntity" , "AntiCrash.InitPostEntity", AntiCrash.InitPostEntity)
  242. hook.Add("Move" , "AntiCrash.Move", AntiCrash.Move)
  243. hook.Add("Think" , "AntiCrash.Think", AntiCrash.Think)
  244.  
  245. net.Receive("AntiCrash.Pong", AntiCrash.Pong)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement