CapsAdmin

Untitled

Aug 27th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1. materials = materials or {} local self = materials
  2.  
  3.  
  4. materials.Replaced = {}
  5.  
  6. function materials.ReplaceTexture(path, to)
  7.     check(path, "string")
  8.     check(to, "string", "ITexture", "Material")
  9.    
  10.     path = path:lower()
  11.    
  12.     local mat = Material(path)
  13.    
  14.     if not mat:IsError() then
  15.        
  16.         local typ = type(to)
  17.         local tex
  18.        
  19.         if typ == "string" then
  20.             tex = Material(to):GetMaterialTexture("$basetexture")
  21.         elseif typ == "ITexture" then
  22.             tex = to
  23.         elseif typ == "Material" then
  24.             tex = to:GetMaterialTexture("$basetexture")
  25.         else return false end
  26.            
  27.         self.Replaced[path] = self.Replaced[path] or {}
  28.        
  29.         self.Replaced[path].OldTexture = self.Replaced[path].OldTexture or mat:GetMaterialTexture("$basetexture")
  30.         self.Replaced[path].NewTexture = tex
  31.        
  32.         mat:SetMaterialTexture("$basetexture",tex)
  33.        
  34.         return true
  35.     end
  36.    
  37.     return false
  38. end
  39.  
  40.  
  41. function materials.SetColor(path, color)
  42.     check(path, "string")
  43.     check(color, "Vector")
  44.    
  45.     path = path:lower()
  46.    
  47.     local mat = Material(path)
  48.    
  49.     if not mat:IsError() then
  50.         self.Replaced[path] = self.Replaced[path] or {}
  51.         self.Replaced[path].OldColor = self.Replaced[path].OldColor or mat:GetMaterialVector("$color")
  52.         self.Replaced[path].NewColor = color
  53.        
  54.         mat:SetMaterialVector("$color", color)
  55.        
  56.         return true
  57.     end
  58.    
  59.     return false
  60. end
  61.  
  62.  
  63. function materials.RestoreAll()
  64.     for name, tbl in pairs(self.Replaced) do
  65.         if
  66.             !pcall(function()
  67.                 if tbl.OldTexture then
  68.                     materials.ReplaceTexture(name, tbl.OldTexture)
  69.                 end
  70.                
  71.                 if tbl.OldColor then
  72.                     materials.SetColor(name, tbl.OldColor)
  73.                 end
  74.             end)
  75.         then
  76.             print("Failed to restore: " .. tostring(name))
  77.         end
  78.     end
  79. end
  80. hook.Add('ShutDown','MatRestorer',materials.RestoreAll)
Advertisement
Add Comment
Please, Sign In to add comment