|
| 1 | +if not windower.file_exists(windower.windower_path .. 'plugins\\ChatMon.dll') then |
| 2 | + return false |
| 3 | +end |
| 4 | + |
| 5 | +local function read_all_lines(file_name) |
| 6 | + local f = assert(io.open(file_name, 'r')) |
| 7 | + local text = f:read('*all') |
| 8 | + f:close() |
| 9 | + return text |
| 10 | +end |
| 11 | + |
| 12 | +local file = windower.windower_path .. 'plugins\\ChatMon.xml' |
| 13 | +local text = read_all_lines(file) |
| 14 | + |
| 15 | +if text == '' then |
| 16 | + return nil |
| 17 | +end |
| 18 | + |
| 19 | +require('strings') |
| 20 | +require('lists') |
| 21 | + |
| 22 | +local quote = function(token) |
| 23 | + return '\'' .. token .. '\'' |
| 24 | +end |
| 25 | + |
| 26 | +local function format_set(values) |
| 27 | + if values == nil or values:length() == 0 then |
| 28 | + return 'S{}' |
| 29 | + end |
| 30 | + |
| 31 | + return 'S{ ' .. values:map(quote):concat(', ') .. ' }' |
| 32 | +end |
| 33 | + |
| 34 | +local sounds = { |
| 35 | + ['tell'] = 'IncomingTell.wav', |
| 36 | + ['talk'] = 'IncomingTalk.wav', |
| 37 | + ['emote'] = 'IncomingEmote.wav', |
| 38 | + ['invite'] = 'PartyInvitation.wav', |
| 39 | + ['examine'] = 'IncomingExamine.wav', |
| 40 | +} |
| 41 | + |
| 42 | +local function format_trigger(from, not_from, match, not_match, sound) |
| 43 | + return '{ ' .. L{ |
| 44 | + 'from = ' .. format_set(from), |
| 45 | + 'notFrom = ' .. format_set(not_from), |
| 46 | + 'match = ' .. quote(match or '*'), |
| 47 | + 'notMatch = ' .. quote(not_match or ''), |
| 48 | + 'sound = ' .. quote(sound), |
| 49 | + }:concat(', ') .. ' }' |
| 50 | +end |
| 51 | + |
| 52 | +local entities = { |
| 53 | + amp = '&', |
| 54 | + lt = '<', |
| 55 | + gt = '>', |
| 56 | + apos = '\'', |
| 57 | + quot = '"', |
| 58 | +} |
| 59 | + |
| 60 | +local converted = text |
| 61 | + :gsub('.*<ChatMon>', 'return {') |
| 62 | + :gsub('(=".-")', '%1,') |
| 63 | + :gsub('<settings', 'settings = {') |
| 64 | + :gsub('<%!%-%-.-%-%->%s*', '') |
| 65 | + :gsub('<trigger', '{') |
| 66 | + :gsub('</ChatMon>', '}') |
| 67 | + :gsub('/?>', '},') |
| 68 | + :gsub('&(%w+);', entities) |
| 69 | + |
| 70 | +local chatmon_plugin_xml = loadstring(converted)() |
| 71 | + |
| 72 | +local defaults = L{ |
| 73 | + { name='tell' }, |
| 74 | + { name='emote' }, |
| 75 | + { name='invite', sound='invitation' }, |
| 76 | + { name='examine' }, |
| 77 | + { name='talk', from=L{'say', 'shout', 'party', 'linkshell', 'linkshell2'}, match='<name>' }, |
| 78 | +} |
| 79 | + |
| 80 | +local triggers = L{} |
| 81 | + |
| 82 | +for data in defaults:it() do |
| 83 | + local name = data.name:ucfirst() |
| 84 | + local comment = chatmon_plugin_xml.settings[name .. 'Sound']:lower() == 'none' |
| 85 | + triggers:append((comment and '-- ' or '') .. format_trigger(data.from, data.not_from, data.match, data.not_match, sounds[data.name])) |
| 86 | +end |
| 87 | + |
| 88 | +local function parse_from(from) |
| 89 | + local matches = L{} |
| 90 | + |
| 91 | + for match in string.gmatch((from or ''):lower(), '[^|/\\,%s]+') do |
| 92 | + matches:append(match) |
| 93 | + end |
| 94 | + |
| 95 | + return matches |
| 96 | +end |
| 97 | + |
| 98 | +for _, trigger in ipairs(chatmon_plugin_xml) do |
| 99 | + local sound = trigger.sound and (sounds[trigger.sound:lower()] or trigger.sound) or 'IncomingTalk.wav' |
| 100 | + triggers:append(format_trigger(parse_from(trigger.from), parse_from(trigger.not_from), trigger.match, trigger.not_match, sound)) |
| 101 | +end |
| 102 | + |
| 103 | +local trigger_text = 'return {\n' .. triggers:map(function(line) return ' ' .. line .. ',\n' end):concat() .. '}\n' |
| 104 | + |
| 105 | +local global = assert(io.open(windower.addon_path .. '/data/triggers/global.lua', 'w')) |
| 106 | +global:write(trigger_text) |
| 107 | +global:close() |
| 108 | + |
| 109 | +local settings = {} |
| 110 | +local truthy_set = S{'true', 't', 'yes', 'y', 'on', 'o'} |
| 111 | +settings.DisableOnFocus = not truthy_set:contains(string.lower(chatmon_plugin_xml.settings.DisableOnFocus)) |
| 112 | +settings.SoundInterval = tonumber(chatmon_plugin_xml.settings.SoundInterval) |
| 113 | + |
| 114 | +-- coroutine.schedule(function() |
| 115 | +-- windower.create_dir(windower.windower_path .. 'plugins\\deprecated') |
| 116 | +-- os.rename(windower.windower_path .. 'plugins\\ChatMon.xml', windower.windower_path .. 'plugins\\depercated\\ChatMon.xml') |
| 117 | +-- os.rename(windower.windower_path .. 'plugins\\ChatMon.dll', windower.windower_path .. 'plugins\\depercated\\ChatMon.dll') |
| 118 | +-- end, 0) |
| 119 | + |
| 120 | +return settings |
0 commit comments