]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu_helper.lua
filterlist api cleanup
[dragonfireclient.git] / builtin / mainmenu_helper.lua
1 --------------------------------------------------------------------------------
2 function dump(o, dumped)
3         dumped = dumped or {}
4         if type(o) == "number" then
5                 return tostring(o)
6         elseif type(o) == "string" then
7                 return string.format("%q", o)
8         elseif type(o) == "table" then
9                 if dumped[o] then
10                         return "<circular reference>"
11                 end
12                 dumped[o] = true
13                 local t = {}
14                 for k,v in pairs(o) do
15                         t[#t+1] = "" .. k .. " = " .. dump(v, dumped)
16                 end
17                 return "{" .. table.concat(t, ", ") .. "}"
18         elseif type(o) == "boolean" then
19                 return tostring(o)
20         elseif type(o) == "function" then
21                 return "<function>"
22         elseif type(o) == "userdata" then
23                 return "<userdata>"
24         elseif type(o) == "nil" then
25                 return "nil"
26         else
27                 error("cannot dump a " .. type(o))
28                 return nil
29         end
30 end
31
32 --------------------------------------------------------------------------------
33 function string:split(sep)
34         local sep, fields = sep or ",", {}
35         local pattern = string.format("([^%s]+)", sep)
36         self:gsub(pattern, function(c) fields[#fields+1] = c end)
37         return fields
38 end
39
40 --------------------------------------------------------------------------------
41 function string:trim()
42         return (self:gsub("^%s*(.-)%s*$", "%1"))
43 end
44
45 --------------------------------------------------------------------------------
46 engine.get_game = function(index)
47         local games = game.get_games()
48         
49         if index > 0 and index <= #games then
50                 return games[index]
51         end
52         
53         return nil
54 end
55
56 --------------------------------------------------------------------------------
57 function fs_escape_string(text)
58         if text ~= nil then
59                 while (text:find("\r\n") ~= nil) do
60                         local newtext = text:sub(1,text:find("\r\n")-1)
61                         newtext = newtext .. " " .. text:sub(text:find("\r\n")+3)
62                         
63                         text = newtext
64                 end
65                 
66                 while (text:find("\n") ~= nil) do
67                         local newtext = text:sub(1,text:find("\n")-1)
68                         newtext = newtext .. " " .. text:sub(text:find("\n")+1)
69                         
70                         text = newtext
71                 end
72                 
73                 while (text:find("\r") ~= nil) do
74                         local newtext = text:sub(1,text:find("\r")-1)
75                         newtext = newtext .. " " .. text:sub(text:find("\r")+1)
76                         
77                         text = newtext
78                 end
79                 
80                 text = string.gsub(text,"\\","\\\\")
81                 text = string.gsub(text,"%]","\\]")
82                 text = string.gsub(text,"%[","\\[")
83                 text = string.gsub(text,";","\\;")
84                 text = string.gsub(text,",","\\,")
85         end
86         return text
87 end
88
89 --------------------------------------------------------------------------------
90 function explode_textlist_event(text)
91         
92         local retval = {}
93         retval.typ = "INV"
94         
95         local parts = text:split(":")
96                                 
97         if #parts == 2 then
98                 retval.typ = parts[1]:trim()
99                 retval.index= tonumber(parts[2]:trim())
100                 
101                 if type(retval.index) ~= "number" then
102                         retval.typ = "INV"
103                 end
104         end
105         
106         return retval
107 end