CapsAdmin

Untitled

Jan 3rd, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. local function printf(s, ...) (CLIENT and epoe.Print or print)(s:format(...)) end
  2.  
  3. local replacers =
  4. {
  5.     include =
  6.     {
  7.         pre = function(line, i, script)
  8.  
  9.         end,
  10.  
  11.         replace = function(str, data)
  12.  
  13.         end,
  14.     }
  15.  
  16.     define =
  17.     {
  18.         pre = function(line, i, script)
  19.             local key, value = line:match("(.-)%s(.+)")
  20.             if not key then
  21.                 key, value = line:match("(.-)%s(.+)")
  22.             end
  23.  
  24.             if not key then return false, "key" end
  25.             if not value then return false, "value" end
  26.  
  27.             local func, args = key:match("(.+)%((.+)%)")
  28.  
  29.             if func then
  30.                 args = args:Split(",")
  31.             else
  32.                 func = key
  33.                 args = nil
  34.             end
  35.  
  36.             if not func then return false, "function" end
  37.             return {func = func, args = args, value = value, i = i}
  38.         end,
  39.         replace = function(str, data)
  40.             local newstr = ""
  41.             for i, line in ipairs(str:Split("\n")) do
  42.                 if i > data.i then
  43.                     if line:find(data.func) then
  44.                         if not data.args then
  45.                             line = line:gsub(data.func, data.value)
  46.                             printf("replaced line %i (%q): %s", i, line, data.value)
  47.                         else
  48.                             local args = line:match(data.func .. "%((.-)%)")
  49.                             if not args then return false, "arguments" end
  50.  
  51.                             args = args:Split(",")
  52.                             --PrintTable(args)
  53.  
  54.                             for key, arg in pairs(args) do
  55.                                 if data.args[key] then
  56.                                     data.value = data.value:gsub(data.args[key], arg)
  57.                                 end
  58.                             end
  59.                             line = line:gsub(data.func .. ".-%)", data.value)
  60.                             printf("replaced line %i: %s", i, line)
  61.                         end
  62.                     end
  63.                 end
  64.                 newstr = newstr .. line .. "\n"
  65.             end
  66.  
  67.             --print(newstr)
  68.  
  69.             return true, newstr
  70.         end
  71.     },
  72. }
  73.  
  74. function eek_process(str)
  75.     local oldstr = str
  76.     local OK, newstr =
  77.     pcall(function()
  78.         local funcs = {}
  79.  
  80.         for i, line in pairs(str:Split("\n")) do
  81.             if line:sub(0, 3) == "--#" then
  82.  
  83.                 line = line:sub(4)
  84.  
  85.                 local func_name = line:match("(%a-)%s+")
  86.  
  87.                 local replacer = replacers[func_name]
  88.  
  89.                 if replacer and replacer.pre then
  90.                     funcs[func_name] = funcs[func_name] or {}
  91.                     line = line:sub(#func_name+2)
  92.  
  93.                     local data, error
  94.                     if line then
  95.                         data, error = replacer.pre(line, i, str)
  96.                     else
  97.                         error = "content"
  98.                     end
  99.  
  100.                     if data then
  101.                         table.insert(funcs[func_name], data)
  102.                     else
  103.                         debug.Trace()
  104.                         printf("%s expected for %s at line %i in %s! ( skipping )", error, func_name, i, line)
  105.                     end
  106.                 end
  107.             end
  108.         end
  109.  
  110.         for func_name, tbl in pairs(funcs) do
  111.             if replacers[func_name].replace then
  112.                 for i, data in pairs(tbl) do
  113.                     local ok, msg = replacers[func_name].replace(str, data)
  114.                     if ok then
  115.                         str = msg
  116.                     else
  117.                         printf("%s expected for %s at line %i in %s! ( skipping )", msg, func_name, data.i, util.CRC(str))
  118.                     end
  119.                 end
  120.             end
  121.         end
  122.  
  123.         return str
  124.     end)
  125.  
  126.     if not OK then print(newstr) return oldstr end
  127.  
  128.     return newstr
  129. end
  130.  
  131. hook.Add("LuaDevPreProcess", 1, function(script, info)
  132.     return eek_process(script)
  133. end)
Advertisement
Add Comment
Please, Sign In to add comment