daily pastebin goal
58%
SHARE
TWEET

Chatter Config

a guest Dec 28th, 2015 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. -- This file contains the LoadConfig function.
  3.  
  4.  
  5.  
  6.  
  7. -- The default config.
  8. local m_ConfigDefaults =
  9. Prefix         = "@7[{WORLD}]@f<{PLAYERNAME}> {MESSAGE}",
  10. UsePlayerColor = true,
  11. UseRankColor   = false,
  12. ChatPerWorld   = true,
  13. ColorSymbol    = "@",
  14.  
  15. PersonalPrefixes =
  16. {
  17.     -- You can add a player by using ["PlayerName"] = "Custom Prefix",
  18.     -- ["STR_Warrior"] = "[{RANK}] <{PLAYERNAME}> {MESSAGE}",
  19. },
  20.  
  21. RankPrefixes =
  22. {
  23.     -- You can add a specific prefix for a certain rank by using ["Rank"] = Custom Prefix
  24.     ["Admin"] = "@c[Admin]@f {MESSAGE}",
  25.     ["Crew"] = "@c[Crew]@f {MESSAGE}",
  26. },
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. -- Writes the default config to the given path
  34. function WriteDefaultConfigToPath(a_Path)
  35.     local File = io.open(a_Path, "w")
  36.     File:write(m_ConfigDefaults)
  37.     File:close()
  38. end
  39.  
  40.  
  41.  
  42.  
  43.  
  44. -- Loads the default config
  45. function GetDefaultConfigLoader()
  46.     return loadstring("return {" .. m_ConfigDefaults .. "}")
  47. end
  48.  
  49.  
  50.  
  51.  
  52.  
  53. function LoadConfig(a_Path)
  54.     assert(type(a_Path) == 'string')
  55.    
  56.     -- Check if the config file exists
  57.     if (not cFile:IsFile(a_Path)) then
  58.         LOGWARNING("[Chatter] Config file does not exist. Using defaults.")
  59.         WriteDefaultConfigToPath(a_Path)
  60.     end
  61.    
  62.     -- Creates a config loader
  63.     local CfgContent = cFile:ReadWholeFile(a_Path)
  64.     local CfgLoader, Err = loadstring("return {" .. CfgContent .. "}")
  65.     if (not CfgLoader) then
  66.         LOGWARNING("[Chatter] Error while loading config file. \"" .. Err .. "\". Using defaults.")
  67.         CfgLoader = GetDefaultConfigLoader()
  68.     end
  69.    
  70.     local Succes, Result = pcall(CfgLoader)
  71.     if (not Succes) then
  72.         LOGWARNING("[Chatter] Error while loading config file. \"" .. Result .. "\". Using defaults.")
  73.         Result = GetDefaultConfigLoader()()
  74.     end
  75.    
  76.     -- Make sure that PersonalPrefixes and RankPrefixes are a valid table
  77.     Result.PersonalPrefixes = (type(Result.PersonalPrefixes) == 'table' and Result.PersonalPrefixes) or {}
  78.     Result.RankPrefixes = (type(Result.RankPrefixes) == 'table' and Result.RankPrefixes) or {}
  79.    
  80.     -- Changes all the color codes to working color codes.
  81.     local function FinalizePrefix(a_PrefixString)
  82.         for Idx = 1, a_PrefixString:len() do
  83.             if (
  84.                 (a_PrefixString:sub(Idx, Idx) == Result.ColorSymbol) and
  85.                 (IsColor(a_PrefixString:sub(Idx + 1, Idx + 1)))
  86.             ) then
  87.         a_PrefixString = a_PrefixString:gsub(Result.ColorSymbol .. a_PrefixString:sub(Idx + 1, Idx + 1), cChatColor.Color .. a_PrefixString:sub(Idx + 1, Idx + 1))
  88.             end
  89.         end
  90.         return a_PrefixString
  91.     end
  92.    
  93.     -- Finalize the default prefix
  94.     Result.Prefix = FinalizePrefix(Result.Prefix)
  95.    
  96.     -- Finalize the personal prefixes
  97.     for PlayerName, Prefix in pairs(Result.PersonalPrefixes) do
  98.         Result.PersonalPrefixes[PlayerName] = FinalizePrefix(Prefix)
  99.     end
  100.    
  101.     -- Finalize the rank prefixes.
  102.     for Rank, Prefix in pairs(Result.RankPrefixes) do
  103.         Result.RankPrefixes[Rank] = FinalizePrefix(Prefix)
  104.     end
  105.    
  106.     g_Config = Result
  107. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top