ChatSaver/ChatSaver.lua

155 lines
4.1 KiB
Lua
Raw Normal View History

ChatSaver = LibStub('AceAddon-3.0'):NewAddon('ChatSaver','AceConsole-3.0','AceHook-3.0','AceEvent-3.0');
2011-05-25 05:05:57 -04:00
local core = ChatSaver;
function core:OnInitialize()
if(ChatSaverDB == nil) then
core.firstrun = true;
ChatSaverDB = {};
else
core.firstrun = false;
end
2011-05-25 05:05:57 -04:00
end
function core:OnEnable()
2011-05-29 06:36:07 -04:00
self:Hook('ToggleChatChannel','ToggleChatChannel',true);
self:RegisterEvent('CHAT_MSG_CHANNEL_NOTICE','ProcessChannelChange');
if(core.firstrun) then
--self:RegisterEvent('CHAT_MSG_CHANNEL_NOTICE','SetupChatSaver');
end
end
--[[
-- CHANNEL FUNCTIONS
-- Because GetChannelName() and GetChannelDisplayInfo() are less
-- than good.
]] --
function core:GetChannelInfo(id)
local channelNumber,channelName = GetChannelName(id);
local channelTable = core:GetChannelTable();
if(channelName == nil) then
id = channelTable[id];
if(id ~= nil) then
channelNumber,channelName = GetChannelName(id);
end
end
return channelNumber,channelName,core:GetChannelCategory(channelNumber);
end
function core:GetChannelTable()
local channelList = { GetChannelList() };
local channelTable = {};
for i = 1,#channelList,2 do
channelTable[channelList[i]] = channelList[i + 1];
channelTable[channelList[i + 1]] = channelList[i];
2011-08-09 05:30:28 -04:00
if type(channelList[i + 1]) == 'string' then
channelTable[channelList[i + 1]:lower()] = channelList[i];
end
end
return channelTable;
end
function core:GetChannelCategory(number)
for i = 1,GetNumDisplayChannels(),1 do
2011-06-03 08:49:37 -04:00
local _,_,_,channelNumber,_,_,category = GetChannelDisplayInfo(i);
if(channelNumber == number) then
return category;
end
end
end
--[[ EVENT FUNCTIONS ]] --
function ProcessChannelChange(_,message,_,_,_,_,_,_,index,name)
if message == 'YOU_JOINED' then
local number,_,category = core:GetChannelInfo(name)
if category == 'CHANNEL_CATEGORY_CUSTOM' then
ChatSaverDB[name] = {};
ChatSaverDB[name]['frames'] = {};
ChatSaverDB[name]['index'] = number;
ChatSaverDB[name]['frames'][DEFAULT_CHAT_FRAME:GetID()] = true;
end
elseif message == 'YOU_LEFT' then
ChatSaverDB[name] = nil
end
end
function core:RejoinChannels(...)
local currentChannels = {};
2011-05-29 06:36:07 -04:00
for i = 1,select('#',GetChannelList()) do
currentChannels[select(i,GetChannelList())] = true
end
2011-05-25 05:36:29 -04:00
local sortedChannels = {};
for channel,_ in pairs(ChatSaverDB) do
table.insert(sortedChannels,channel);
end
table.sort(sortedChannels,function(a,b) return ChatSaverDB[a].index < ChatSaverDB[b].index end);
for _,channel in pairs(sortedChannels) do
2011-08-09 05:30:28 -04:00
if currentChannels[channel] == nil then
2011-05-29 06:36:07 -04:00
JoinPermanentChannel(channel); -- does not place in chat frame properly
for index,_ in pairs(ChatSaverDB[channel].frames) do
ChatFrame_AddChannel(_G['ChatFrame'..index],channel);
end
2011-05-25 05:36:29 -04:00
end
end
self:UnregisterEvent('CHANNEL_UI_UPDATE');
end
2011-05-29 06:36:07 -04:00
function core:SetupChatSaver(...)
for frame = 1,NUM_CHAT_WINDOWS do
local chatWindowChannels = { GetChatWindowChannels(frame) };
2011-05-29 06:36:07 -04:00
for i = 1,#chatWindowChannels,2 do
local number,name,category = core:GetChannelInfo(chatWindowChannels[i]);
2011-08-09 05:30:28 -04:00
if category == 'CHANNEL_CATEGORY_CUSTOM' then
if ChatSaverDB[name] == nil then
ChatSaverDB[name] = {};
ChatSaverDB[name]['frames'] = {};
ChatSaverDB[name]['index'] = number;
end
ChatSaverDB[name]['frames'][frame] = true;
end
end
end
self:UnregisterEvent('CHAT_MSG_CHANNEL_NOTICE');
2011-05-25 05:05:57 -04:00
end
function core:StoreChannel(_,_,_,_,_,_,_,_,_,name)
local number,channelName,category = core:GetChannelInfo(name);
2011-08-09 05:30:28 -04:00
if category == 'CHANNEL_CATEGORY_CUSTOM' then
ChatSaverDB[name] = {};
ChatSaverDB[name]['frames'] = {};
ChatSaverDB[name]['index'] = number;
ChatSaverDB[name]['frames'][DEFAULT_CHAT_FRAME:GetID()] = true;
end
self:UnregisterEvent('CHAT_MSG_CHANNEL_NOTICE');
end
--[[ HOOKED FUNCTIONS ]] --
function core:ToggleChatChannel(checked,channel)
2011-08-09 05:30:28 -04:00
if ChatSaverDB[channel] == nil then
2011-05-25 23:18:21 -04:00
return;
end
2011-08-09 05:30:28 -04:00
if checked then
ChatSaverDB[channel]['frames'][FCF_GetCurrentChatFrameID()] = true;
else
ChatSaverDB[channel]['frames'][FCF_GetCurrentChatFrameID()] = nil;
end
end