fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
f7324a4
settings-overhaul
1function table . merge ( a , b ) 2for k , v in pairs ( b ) do 3if not a [ k ] then a [ k ] =v end 4end 5return a 6end 7 8function table . clone ( a ) 9local ret ={} 10for k , v in pairs ( a ) do 11ret [ k ] =type ( v ) == "table" and table . clone ( v ) or v 12end 13return ret 14end 15 16local function reversedipairsiter ( t , i ) 17i =i - 1 18if i ~= 0 then 19return i , t [ i ] 20end 21end 22 23function reversedipairs ( t ) 24return reversedipairsiter , t , # t + 1 25end 26 27function table . hasValue ( tbl , val ) 28for key , value in pairs ( tbl ) do 29if ( value == val ) then return true , key end 30end 31return false 32end 33 34do 35local recursiveTableString 36local stack ={} 37local cache ={} 38 39local KEY_TYPE_DOT =1 40local KEY_TYPE_BRACKET =2 41 42local function val_to_str ( v , name ) 43if type ( v ) == "string" then 44v =string . gsub ( v , "\n" , "\\n" ) 45if string . match ( string . gsub ( v , "[^'\"]" , "" ), '^"+$' ) then 46return "'" .. v .. "'" 47end 48return '"' .. string . gsub ( v , '"' , '\\"' ) .. '"' 49elseif type ( v ) == "table" then 50return recursiveTableString ( v , name ) 51else 52return tostring ( v ) 53end 54end 55 56local function key_type ( k ) 57if type ( k ) == "string" and string . match ( k , "^[_%a][_%a%d]*$" ) then 58return KEY_TYPE_DOT 59else 60return KEY_TYPE_BRACKET 61end 62end 63 64local function key_to_str ( k , name ) 65if key_type ( k ) == KEY_TYPE_DOT then 66return k 67else 68return "[" .. val_to_str ( k , name ) .. "]" 69end 70end 71 72local function getStackName () 73local result ="" 74for pos , name in ipairs ( stack ) do 75if key_type ( name ) == KEY_TYPE_DOT and pos > 1 then 76result =result .. "." .. key_to_str ( name ) 77else 78result =result .. key_to_str ( name ) 79end 80end 81return result 82end 83 84local function getTabs () 85return string . rep ( "\t" , # stack ) 86end 87 88recursiveTableString =function ( tbl , name ) 89name =name or "self" 90 91local result ="{\n" 92 93if cache [ tbl ] then 94return cache [ tbl ] 95end 96 97table . insert ( stack , name ) 98cache [ tbl ] =getStackName () 99 100for k , v in pairs ( tbl ) do 101result =result .. getTabs () .. key_to_str ( k , k ) .. " = " .. val_to_str ( v , k ) .. "\n" 102end 103table . remove ( stack , # stack ) 104 105return result .. getTabs ( stack ) .. "}" 106end 107 108function table . tostring ( tbl ) 109stack ={} 110cache ={} 111return recursiveTableString ( tbl ) 112end 113end 114 115--[[local test = { 1161, 2, 5, 117["Kappa 1 2 3 \n lul"] = true, 118data = { 1191337.84239, 120test = { 1217, 8, "NINE" 122} 123} 124} 125 126table.insert(test.data, test) 127test.data.test[3] = test 128test[58] = test.data.test 129 130print(table.tostring(test))]] 131 132function table . copy ( object ) 133local lookup_table ={} 134local function _copy ( object ) 135if type ( object ) ~= "table" then 136return object 137elseif lookup_table [ object ] then 138return lookup_table [ object ] 139end 140local new_table ={} 141lookup_table [ object ] =new_table 142for index , value in pairs ( object ) do 143new_table [ _copy ( index )] =_copy ( value ) 144end 145return setmetatable ( new_table , getmetatable ( object )) 146end 147return _copy ( object ) 148end 149 150function table . concatNumberedList ( tbl , sep , oxford ) 151if type ( tbl ) ~= "table" then 152return tostring ( tbl ) 153end 154 155local str ="" 156local num =# tbl 157for i =1 , num do 158if i < num then 159-- If we aren't at the end of the list, use commas 160-- If we're one before the end of the list, use an oxford comma if we want it 161str =str .. i .. ". " .. tbl [ i ] .. (( oxford or i < num - 1 ) and ", " or " " ) 162elseif i > 1 then 163-- If we're the last item and we have more than 1 item already.. 164-- Seperate using our defined seperator word 165str =str .. ( sep or "and" ) .. " " .. i .. ". " .. tbl [ i ] 166else 167-- Single item.. 168str =i .. ". " .. tbl [ i ] 169end 170end 171return str 172end 173 174function table . concatList ( tbl , sep , oxford ) 175if type ( tbl ) ~= "table" then 176return tostring ( tbl ) 177end 178 179local str ="" 180local num =# tbl 181for i =1 , num do 182if i < num then 183-- If we aren't at the end of the list, use commas 184-- If we're one before the end of the list, use an oxford comma if we want it 185str =str .. tbl [ i ] .. (( oxford or i < num - 1 ) and ", " or " " ) 186elseif i > 1 then 187-- If we're the last item and we have more than 1 item already.. 188-- Seperate using our defined seperator word 189str =str .. ( sep or "and" ) .. " " .. tbl [ i ] 190else 191-- Single item.. 192-- Don't even bother concating the string 193str =tbl [ i ] 194end 195end 196return str 197end 198 199function table . max ( tbl ) 200local max_key =nil 201local max_value =- math . huge 202for key , value in pairs ( tbl ) do 203value =assert ( tonumber ( value ), "expected a table full of numbers" ) 204if value > max_value then 205max_key =key 206max_value =value 207end 208end 209return max_key , max_value 210end 211 212function table . min ( tbl ) 213local min_key =nil 214local min_value =math . huge 215for key , value in pairs ( tbl ) do 216value =assert ( tonumber ( value ), "expected a table full of numbers" ) 217if value < min_value then 218min_key =key 219min_value =value 220end 221end 222return min_key , min_value 223end 224 225function table . avg ( tbl ) 226local num =0 227local total =0 228for key , value in pairs ( tbl ) do 229value =assert ( tonumber ( value ), "expected a table full of numbers" ) 230total =total + value 231num =num + 1 232end 233return total / num 234end 235 236function table . sum ( tbl ) 237local total =0 238for key , value in pairs ( tbl ) do 239value =assert ( tonumber ( value ), "expected a table full of numbers" ) 240total =total + value 241end 242return total 243end 244 245function table . random ( tbl ) 246return tbl [ math . random ( 1 , # tbl )] 247end 248 249function table . randomkeyvalue ( tbl ) 250local keys ={} 251for k , v in pairs ( tbl ) do 252table . insert ( keys , k ) 253end 254local key =keys [ math . random ( 1 , # keys )] 255return key , tbl [ key ] 256end 257 258function table . shuffle ( tbl ) 259for i =# tbl , 2 , - 1 do 260local j =math . random ( i ) 261tbl [ i ], tbl [ j ] =tbl [ j ], tbl [ i ] 262end 263end