view Devian.lua @ 1:a13243a4118b

empty output detection cleaning old code
author Nenue
date Thu, 17 Dec 2015 03:07:00 -0500
parents 585221424a93
children 77adceb8ebe4
line wrap: on
line source
-- User: Krakyn
-- Created: 11/30/2015 7:46 AM
if not LibStub then
  print('Something has happened...')
end
Devian = LibStub("AceAddon-3.0"):NewAddon("Devian", "AceConsole-3.0", "AceEvent-3.0")
local D = _G.Devian
local db
local STATE_LOW, STATE_HIGH = 1, 2
local PLAYER_REALM = UnitName("player") .. '-' .. GetRealmName()
local DEVIAN_FRAME = DevianDebugInfo

local ScanAddOnList = function(args)
  local list_state

  local mode = tonumber(args:match('^%d$'))

  print('ScanAddOnList(', mode, ')')
  if mode == nil then
    list_state = db.enabled and STATE_LOW or STATE_HIGH
    db.enabled = (db.enabled == false) and true or false
    print(list_state, db.enabled)

    if list_state == STATE_LOW then
      BNSetDND(true)
    end

  else
    if mode > 2 then
      print('ScanAddOnList(',mode,'): Something has happened.')
      return
    end
    list_state = mode == STATE_LOW and STATE_LOW or STATE_HIGH

    if not db.chat_size then
      db.chat_size = {}
    end
    if mode == STATE_LOW or mode == STATE_HIGH then
      db.chat_size[list_state] = ChatFrame1:GetHeight()
    end

  end
  local char_list, global_list = db[PLAYER_REALM][list_state], db.global[list_state]

  local playername = UnitName("player")

  for i = 1, GetNumAddOns() do
    local name = GetAddOnInfo(i)
    local enableState, globalState = GetAddOnEnableState(playername, i), GetAddOnEnableState(nil, i)

    if mode == STATE_LOW or mode == STATE_HIGH then
      char_list[name] = enableState
      global_list[name] = globalState
    else

      if char_list[name] ~= 0 and global_list[name] ~= 0 then
        local value = false
        if char_list[name] == 2 and global_list[name] == 1 then
          value = UnitName("player")
        elseif global_list[name] == 2 then
          value = true
        end
        print('EnableAddOn(', i, ',', value,')')
        EnableAddOn(i, value)
      else
        local value = true
        if char_list[name] == 2 and global_list[name] == 1 then
          value = UnitName("player")
        end
        print('DisableAddOn(', i, ',', value,')')
        DisableAddOn(i,value)
      end

    end
  end

  if mode == nil then
    ReloadUI()
  end
  if mode == STATE_LOW then
    D:Print('Developement AddOn list saved.')
  else
    D:Print('Standard AddOn list saved.')
  end
end

-- Debug info
local function CreateMessenger()
  -- TODO: throw this into an XML file
  if not DEVIAN_FRAME then
    DEVIAN_FRAME = CreateFrame('ScrollingMessageFrame', DEVIAN_FRAME, UIParent)
    if not db.console then
      DEVIAN_FRAME:Hide()
    end
  end

  local f = DEVIAN_FRAME
  f.backdrop = f:CreateTexture('backdrop', 'BACKGROUND')
  f.backdrop:SetTexture(1,1,1,1)
  f.backdrop:SetGradient('HORIZONTAL', 0.2, 0.2, 0.2, 1, 1, 1)
  f.backdrop:SetBlendMode('MOD')
  f.backdrop:SetAllPoints(f)
  f:AddMessage('init a thing')

  f:EnableMouseWheel(true)
  f:EnableKeyboard(true)
  f:SetScript('OnMouseWheel', function (self, delta)
    local up =  delta > 0
    if IsControlKeyDown() then
      if up then self:ScrollToTop()
      else self:ScrollToBottom() end
    elseif IsShiftKeyDown() then
      if up then self:PageUp()
      else self:PageDown() end
    else
      if up then self:ScrollUp()
      else self:ScrollDown() end
    end
  end)

  f:ScrollToBottom()
  f:SetMaxLines(100)
  f:SetInsertMode('BOTTOM')
  f:SetPoint('BOTTOMLEFT', GENERAL_CHAT_DOCK, 'TOPLEFT', 0, 25)
  f:SetPoint('TOPRIGHT', UIParent, 'TOP', 0, 0- PlayerFrame:GetHeight())
  f:SetFrameStrata('BACKGROUND')
  f:SetFont([[Interface\Addons\Turok\Media\font\SourceCodePro-Regular.ttf]], 14, 'NONE')

  f:SetFading(false)
  f:SetTimeVisible(2147483647)
  f:SetJustifyH('LEFT')


  D.debug_init = true
end

local prefix_cache = {}
local function Message(prefix, ...)
  if prefix == nil then
    oldprint('Tried to print nothing.')
    return
  end
  -- colorize
  if not prefix_cache[prefix] then
    local c  = {0, 0, 0 }
    local max = string.len(prefix)
    for i = 1, max, 3 do
        for k, v in ipairs(c) do
          local j = i + (k - 1)
          c[k] = c[k] + (j <= max and string.byte(prefix,j) or 0)
        end
    end
    for k,v in ipairs(c) do
      c[k] = c[k] % 255
      if c[k] < 64 then
        c[k] = 0
      elseif c[k] > 127 then
        c[k] = 255
      end
    end
    prefix_cache[prefix] = string.format('%02X%02X%02X', unpack(c))
  end

  local buffer = {'|cFF'.. prefix_cache[prefix]..prefix ..'|r'}
  for i = 1, select('#',...) do
    local var = select(i, ...)

    if type(var) == 'table' then
      var = '<table>'
    elseif type(var) == 'boolean' then
      var = var and 'true' or 'false'
    elseif type(var) == 'function' then
      var = '<funcref>'
    elseif type(var) == 'nil' then
      var = 'nil'
    end

    table.insert(buffer, var)
  end
  DEVIAN_FRAME:AddMessage(table.concat(buffer, ' '))
  table.wipe(buffer)
end

function D:OnEnable()
  if db.enabled then
    DEVIAN_FRAME:Hide()
    D:Print('Standard AddOn list active. Type /dvn to switch to development mode.')
  else
    D:Print('Development AddOn list active. Type /dvn to revert to regular operation.')
  end
end

function D:OnInitialize()

  local cherry = false
  if not DevianDB then
    DevianDB = {
      ['global'] = {[STATE_LOW] = {}, [STATE_HIGH] = {}},
      console = true,
      dnd_status = true,
      dnd_message = "Debugging. Your messages may get eaten.",
    }
    cherry = "This is probably the first time, so:\n  /dvn 2 to save your regular addon list\n  /dvn 1 to save your development addon list\n  /dvn to switch between the two\n  /dvc to toggle print() frame"
  end
  db = DevianDB

  if not db[PLAYER_REALM] then
    db[PLAYER_REALM] = {[STATE_LOW] = {}, [STATE_HIGH] = {} }
    if not cherry then
      cherry = "This character didn't have an AddOn table."
    end
  end

  -- inform the user if we have a virgin data table
  if cherry then
    D:Print(cherry)
  end

  -- replace print handler and make the original available in global if possible

  CreateMessenger()
  D.oldprint = getprinthandler()
  if not _G.oldprint then
    _G.oldprint = D.oldprint
  end
  setprinthandler(Message)
  self:RegisterChatCommand("dvn", ScanAddOnList)
  self:RegisterChatCommand("dvc", function(args)
    db.console = (db.console == false) and true or false
    if db.console then
      DEVIAN_FRAME:Show()
    else
      DEVIAN_FRAME:Hide()
    end
  end)

  print('Master! I am here.')
end