Thread Tools Display Modes
06-13-24, 06:29 AM   #1
Sharpedge
A Cliff Giant
 
Sharpedge's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2022
Posts: 75
Tables vs Strings issue

As you can see from the screen shot that I have tables populating instead of strings. I have looked and I can't seem to figure out why it's showing a table instead of a string. Can I get another set of eyes on this please? Here is the relevant parts:
Code:
local LibStub = LibStub
MDH.L = LibStub("AceLocale-3.0"):GetLocale("MisDirectionHelper", true)
local L = MDH.L
local icon = LibStub("LibDBIcon-1.0")
local _G = _G
local MDHText = "MDH"
local channels = {["RAID"] = _G.RAID, ["PARTY"] = _G.PARTY, ["WHISPER"] = _G.WHISPER}
local misdtarget
local GetSpellInfo = C_Spell.GetSpellInfo

-- Fetch spell info and store as strings
local imd = { name = tostring(select(1, GetSpellInfo(34477)) or "Unknown Spell"), id = 34477 }
local itt = { name = tostring(select(1, GetSpellInfo(57934)) or "Unknown Spell"), id = 57934 }
local iua = { name = tostring(select(1, GetSpellInfo(51672)) or "Unknown Spell"), id = 51672 }

local hiconinfo = {
    [imd.name] = {"Interface\\Icons\\Ability_Hunter_Misdirection", 151},
}
local hicons = {[151] = imd.name}
local iconm = {
    [151] = "Ability_Hunter_Misdirection",
    [454] = "Ability_Rogue_TricksOftheTrade",
    [457] = "Ability_Rogue_UnfairAdvantage",
}
local riconinfo = {[itt.name] = {"Interface\\Icons\\Ability_Rogue_TricksOftheTrade", 454},}
local ricons = {[454] = itt.name, [457] = iua.name}

local callpet = {
    [1] = { name = select(1, GetSpellInfo(883)) or "Unknown Spell", id = 883 },
    [2] = { name = select(1, GetSpellInfo(83242)) or "Unknown Spell", id = 83242 },
    [3] = { name = select(1, GetSpellInfo(83243)) or "Unknown Spell", id = 83243 },
    [4] = { name = select(1, GetSpellInfo(83244)) or "Unknown Spell", id = 83244 },
    [5] = { name = select(1, GetSpellInfo(83245)) or "Unknown Spell", id = 83245 },
}
local dismisspet = { name = select(1, GetSpellInfo(2641)) or "Unknown Spell", id = 2641 }

local UnitName = UnitName
local GetPartyAssignment = GetPartyAssignment
local UnitGetAvailableRoles = UnitGetAvailableRoles
local UnitGroupRolesAssigned = UnitGroupRolesAssigned
local UnitHealthMax = UnitHealthMax
local CreateFont = CreateFont
local InCombatLockdown = InCombatLockdown
local string, unpack, type, select, format = string, unpack, type, select, format
local pairs, ipairs, strsplit, tonumber = pairs, ipairs, strsplit, tonumber
local CreateMacro, EditMacro, GetMacroIndexByName, IsAddOnLoaded = CreateMacro, EditMacro, GetMacroIndexByName, IsAddOnLoaded
local GetNumGroupMembers, GetNumSubgroupMembers, IsInRaid, IsInGroup = GetNumGroupMembers, GetNumSubgroupMembers, IsInRaid, IsInGroup
local GetInstanceInfo, UnitIsGhost, UnitExists, GetSpellLink = GetInstanceInfo, UnitIsGhost, UnitExists, GetSpellLink
local UnitAffectingCombat, UnitInRaid, GetStablePetInfo, UnitIsPlayer = UnitAffectingCombat, UnitInRaid, GetStablePetInfo, UnitIsPlayer
local SendChatMessage, CreateFrame = SendChatMessage, CreateFrame
local GameTooltipText, GameTooltipHeaderText = GameTooltipText, GameTooltipHeaderText

-- GLOBALS: MDH MDHWaitFrame Tukui ElvUI InterfaceOptionsFrame_OpenToCategory StaticPopup_Show GetAddOnMetadata InterfaceOptions_AddCategory

local function set(info, value)
    local key = info[#info]
    if type(value) == "table" then
        print("Warning: Attempted to set a table value to ", key, " instead of a string.")
        value = tostring(value)
    end
    MDH.db.profile[key] = value
end

local function get(info)
    local key = info[#info]
    local value = MDH.db.profile[key]
    if type(value) == "table" then
        print("Warning: Retrieved a table value from ", key, " instead of a string.")
        value = tostring(value)
    end
    return value
end

local modkeys = {[1] = "shift", [2] = "ctrl", [3] = "alt",}
local modopts = {[1] = _G.SHIFT_KEY, [2] = _G.CTRL_KEY, [3] = _G.ALT_KEY,}

local defaults = {
    profile = {
        minimap = {hide = false},
        cChannel = "PARTY",
        name = "Pet",
        petname = _G.UNKNOWN,
        bAnnounce = nil,
        hicon = 151,
        ricon = 454,
        hname = tostring(imd.name), 
        rname = tostring(itt.name),
        target = "pet",
        target2 = nil,
        target3 = nil,
        name2 = nil,
        clearjoin = nil,
        remind = nil,
        modkey = 1,
        autotank = nil,
        autopet = nil,
        theme = _G.DEFAULT,
    },
    global = { custom = {} },
}

--************ THEMES ************
local themelist, customlist
local fontlist = {[1] = "MDH", [2] = "Arial Narrow", [3] = "Morpheus", [4] = "Skurri"}
local tmpcopy, tempname, tempdata
local temptheme = {headerfont="MDHHeaderFont",linefont="MDHLineFont",title={1,0,0,1,"ffffff00"},spacer={0,0,1,1},group1={1,0,0,1},group2={0,1,1,1},group3={0,0.8,0.2,1},group4={1,0,1,0,"ffffff00","ffff0033"},group5={1,0,1,1,"ffffff00"}}
MDH.themes = {
	[_G.DEFAULT] = {headerfont="MDHHeaderFont",linefont="MDHLineFont",title={1,0,0,1,"ffffff00"},spacer={0,0,1,1},group1={1,0,0,1},group2={0,1,1,1},group3={0,0.8,0.2,1},group4={1,0,1,0,"ffffff00","ffff0033"},group5={1,0,1,1,"ffffff00"}},
	["Basic"] = {headerfont="MDHHeaderFont",linefont="MDHLineFont",title={0,0,0,0,"ffffffff"},spacer={0,0,0,0},group1={0,0,0,0},group2={0,0,0,0},group3={0,0,0,0},group4={0,0,0,0,"ffffffff","ffffffff"},group5={0,0,0,0,"ffffffff"}},
	["ElvUI"] = {headerfont="ElvUIHeaderFont",linefont="ElvUILineFont",title={0,0,0,0,"ff1784d1"},spacer={0,0,1,1},group1={0,0,0,0},group2={0,0,0,0},group3={0,0,0,0},group4={0,0,0,0,"ff1784d1","ff778899"},group5={0,0,0,0,"ff1784d1"}},
}

local function GetTTFont(font)
	local pos = string.find(font, "Header") or string.find(font, "Line")
	local name = string.sub(font, 1, pos - 1)
	local fpos, k, v
	for k, v in pairs(fontlist) do if v == name then fpos = k; break end end
	return fpos
end

function MDH:MDHTextUpdate() MDH.dataObject.text = MDH:TTText("both") end

function MDH:MDHLoad()
	if UnitExists("pet") then MDH:MDHgetpet() end
	MDH:MDHEditMacro()
end

function MDH:MDHEditMacro()
    -- cannot edit/create macros during combat
    if InCombatLockdown() then return end

    local singlemacro, multiplemacro, macro, macroid
    local spell, id, mname, modkey

    if uc == "HUNTER" then
        spell = imd
        id = MDH.db.profile.hicon or hiconinfo[imd.name][2]
        mname = MDH.db.profile.hname or imd.name
    elseif uc == "ROGUE" then
        spell = itt
        id = MDH.db.profile.ricon or riconinfo[itt.name][2]
        mname = MDH.db.profile.rname or itt.name
    else
        print("Error: Unsupported class")
        return
    end

    modkey = modkeys[MDH.db.profile.modkey]

    local spellName = tostring(spell.name or "Unknown Spell")

    mname = tostring(mname)

    MDH:MDHTextUpdate()

    -- Define the macro templates
    singlemacro = "#showtooltip\n/use [mod:" .. modkey .. ",@none][@%s,nodead]%s;%s"
    multiplemacro = "#showtooltip\n/use [mod:" .. modkey .. ",@none][btn:1,@%s,nodead][btn:2,@%s,nodead]%s;%s"

    local target = tostring(MDH.db.profile.target or "target")
    local target2 = tostring(MDH.db.profile.target2 or "target")

    -- Format the macro
    if MDH.db.profile.target2 then
        macro = format(multiplemacro, target, target2, spellName, spellName)
    else
        macro = format(singlemacro, target, spellName, spellName)
    end
    print("Formatted macro: ", macro)

    -- Get or create the macro
    macroid = GetMacroIndexByName(mname)
    if macroid == 0 then
        CreateMacro(mname, iconm[id], macro, 1, 1)
    else
        EditMacro(macroid, mname, iconm[id], macro)
    end
end

-- Fetch spell info and store as strings
local imd = { name = (select(1, GetSpellInfo(34477)) or "Unknown Spell"), id = 34477 }
local itt = { name = (select(1, GetSpellInfo(57934)) or "Unknown Spell"), id = 57934 }
local iua = { name = (select(1, GetSpellInfo(51672)) or "Unknown Spell"), id = 51672 }


function MDH:MDHChat()
	if IsAddOnLoaded("CastYeller2") or IsAddOnLoaded("CastYeller") then return end
	--local msg = format((uc == "HUNTER") and L["%s Misdirects to %s"] or L["%s casts Tricks of the Trade on %s"], UnitName("player"), misdtarget)
	local chan = MDH.db.profile.cChannel or "RAID"
	local s
	local spelllink = (uc == "HUNTER") and GetSpellLink(35079) or GetSpellLink(57934)
	local msg = format(L["%s casts %s on %s"], UnitName("player"), spelllink, misdtarget)

	if chan == "PARTY" and GetNumSubgroupMembers() ~= 0 then
		if (IsInGroup(_G.LE_PARTY_CATEGORY_INSTANCE)) then chan = "INSTANCE_CHAT" end
		s = true

	elseif chan == "RAID" and IsInRaid() then s = true
	elseif chan == "WHISPER" then if UnitIsPlayer(misdtarget) then s = true end end
	if s then SendChatMessage(msg, chan, nil, misdtarget) end
end

--First visible options frame
local function createMainPanel()
	local frame = CreateFrame("Frame", "MisdirectionHelperMain")
	local title = frame:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
	local version = frame:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
	local author = frame:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
	title:SetFormattedText("|T%s:%d|t %s", "Interface\\ICONS\\Ability_Hunter_Misdirection", 32, "Misdirection Helper 2")
	title:SetPoint("CENTER", frame, "CENTER", 0, 170)
	
	version:SetPoint("CENTER", frame, "CENTER", 0, 130)
	author:SetText(L["Author"] .. ": Deepac")
	author:SetPoint("CENTER", frame, "CENTER", 0, 100)
	return frame
end

local function stringify(array)
	local out = ""
	for _, v in ipairs(array) do out = out .. v .. ";" end
	out = string.sub(out, 1, string.len(out) - 1)
	return out
end

local function encodeTheme(theme)
	local encoded = "1" .. GetTTFont(theme.headerfont) .. ":2" .. GetTTFont(theme.linefont)
	local keys = {["title"] = 3, ["spacer"] = 4, ["group1"] = 5, ["group2"] = 6, ["group3"] = 7, ["group4"] = 8, ["group5"] = 9}
	for k, v in pairs(theme) do if k ~= "headerfont" and k ~= "linefont" then encoded = encoded .. ":" .. keys[k] .. stringify(v) end end
	return encoded
end

local function destringify(val)
	local vals = {strsplit(";", val)}
	if vals[6] then return vals[1], vals[2], vals[3], vals[4], vals[5], vals[6]
	elseif vals[5] then return vals[1], vals[2], vals[3], vals[4], vals[5]
	else return vals[1], vals[2], vals[3], vals[4] end
end

function MDH:decodeTheme(encoded)
	local keys = {[3] = "title", [4] = "spacer", [5] = "group1", [6] = "group2", [7] = "group3", [8] = "group4", [9] = "group5"}
	local vals = {strsplit(":", encoded)}
	local ord, v2
	for _, v in ipairs(vals) do
		ord = tonumber(string.sub(v, 1, 1))
		if ord == 1 then temptheme.headerfont = fontlist[tonumber(string.sub(v, 2, 2))] .. "HeaderFont"
		elseif ord == 2 then temptheme.linefont = fontlist[tonumber(string.sub(v, 2, 2))] .. "LineFont"
		else temptheme[keys[ord]] = {destringify(string.sub(v, 2))} end
	end
end

local function validateThemeName(info, value)
	local out
	local val = MDH:trim(value or "")
	if val == "" then out = L["Please enter a valid theme name"] end
	if MDH.themes[value] then out = L["Theme name already in use"] end
	if out then MDH:ShowError(out); tempname = nil end
	return out or true
end

local function updateThemeList()
	themelist, customlist = {}, {}
	for k in pairs(MDH.themes) do themelist[k] = k end
	for k in pairs(MDH.db.global.custom) do customlist[k] = k end
end

local function saveTheme()
	if type(validateThemeName(nil, tempname)) ~= "boolean" then return end
	MDH.db.global.custom[tempname] = temptheme
	MDH.themes[tempname] = temptheme
	updateThemeList()
	tempname = nil
	tempdata = nil
	MDH:ShowError(L["Theme saved"])
end

local function editTheme()
	MDH.db.global.custom[tempname] = temptheme
	MDH.themes[tempname] = temptheme
	tempname = nil
	MDH:ShowError(L["Theme saved"])
end

local function deleteTheme()
	if not tempname then MDH:ShowError(L["Please enter a valid theme name"]); return end
	if MDH.db.profile.theme == tempname then MDH.db.profile.theme = _G.DEFAULT end
	MDH.db.global.custom[tempname] = nil
	MDH.themes[tempname] = nil
	updateThemeList()
	tempname = nil
	MDH:ShowError(L["Theme deleted"])
end

local function checkThemes()
	for k, v in pairs(MDH.db.global.custom) do if k then return false end end
	return true
end

function MDH:OnInitialize()
	local AceConfig = LibStub("AceConfig-3.0")
	local AceConfigDialog = LibStub("AceConfigDialog-3.0")
	local AceDBOptions = LibStub("AceDBOptions-3.0")
	local mainPanel = createMainPanel()
	local optionsTable, themesTable
	local k, v

	MDH.db = LibStub("AceDB-3.0"):New("MisDirectionHelperDB", defaults)
	optionsTable = {
		type = "group",
		name = _G.MAIN_MENU,
		inline = false,
		args = {
			showMinimapIcon = {
				order = 1,
				type = "toggle",
				width = "full",
				name = L["Minimap Icon"],
				desc = L["Toggle Minimap icon"],
				get = function() return not MDH.db.profile.minimap.hide end,
				set = function(info, value)
					MDH.db.profile.minimap.hide = not value
					if value then icon:Show("MisdirectionHelper")
					else icon:Hide("MisdirectionHelper") end
				end,
			},
			clearleave = {
				order = 2,
				type = "toggle",
				width = "full",
				name = L["Clear when joining group"],
				desc = L["Clear both targets when joining a raid / party"],
				get = function() return MDH.db.profile.clearjoin end,
				set = function(info, value)
					MDH.db.profile.clearjoin = value
					if value then MDH:RegisterEvent("GROUP_ROSTER_UPDATE")
					else MDH:UnregisterEvent("GROUP_ROSTER_UPDATE") end
				end,
			},
			autotank = {
				order = 2,
				type = "toggle",
				width = "full",
				name = L["Set to tank when joining party"],
				desc = L["Set the target to the main tank when joining a party. May not pick the right character if roles have not been set."],
				get = function() return MDH.db.profile.autotank end,
				set = function(info, value)
					MDH.db.profile.autotank = value
					if value then MDH:RegisterEvent("GROUP_ROSTER_UPDATE")
					else MDH:UnregisterEvent("GROUP_ROSTER_UPDATE") end
				end,
			},
			autopet = {
				order = 3,
				type = "toggle",
				width = "full",
				name = L["Set to pet when leaving party"],
				desc = L["Set the target to your pet when leaving a party"],
				get = function() return MDH.db.profile.autopet end,
				set = function(info, value)
					MDH.db.profile.autopet = value
					if value then MDH:RegisterEvent("GROUP_ROSTER_UPDATE")
					else MDH:UnregisterEvent("GROUP_ROSTER_UPDATE") end
				end,
			},
			remind = {
				order = 4,
				type = "toggle",
				width = "full",
				name = L["Show reminder"],
				desc = L["Display a reminder to set targets on entering an instance / raid"],
				get = function() return MDH.db.profile.remind end,
				set = function(info, value)
					MDH.db.profile.remind = value
					if value then MDH:RegisterEvent("ZONE_CHANGED_NEW_AREA")
					else MDH:UnregisterEvent("ZONE_CHANGED_NEW_AREA") end
				end,
			},
			buffmessage = {
				order = 5,
				type = "toggle",
				name = L["Buff Alert"],
				desc = L["Toggle Misdirection Applied Announcment"],
				get = function() return MDH.db.profile.bAnnounce end,
				set = function(info, value) MDH.db.profile.bAnnounce = value end,
			},
			cChannel = {
				type = "select",
				name = L["Buff Channel"],
				desc = L["Select Channel for Buff Announcements"],
				hidden = function() return not MDH.db.profile.bAnnounce end,
				order = 6,
				get = function() return MDH.db.profile.cChannel end,
				set = function(info, value) MDH.db.profile.cChannel = value end,
				values = channels,
			},
			spacer1 = {
				order = 7,
				type = "description",
				name = "\n",
			},
			hicon = {
				type = "select",
				name = L["Misdirection macro icon"],
				order = 8,
				hidden = function() return uc == "ROGUE" end,
				get = function() return MDH.db.profile.hicon or hiconinfo[imd][2] end,
				set = function(info, value)
					MDH.db.profile.hicon = value
					MDH:MDHEditMacro()
				end,
				values = hicons,
			},
			ricon = {
				type = "select",
				name = L["Tricks of the Trade macro icon"],
				order = 9,
				hidden = function() return uc == "HUNTER" end,
				get = function() return MDH.db.profile.ricon or riconinfo[itt][2] end,
				set = function(info, value)
					MDH.db.profile.ricon = value
					MDH:MDHEditMacro()
				end,
				values = ricons,
			},
			bkey = {
				type = "select",
				name = L["Macro bypass key"],
				order = 10,
				get = function() return MDH.db.profile.modkey end,
				set = function(info, value)
						MDH.db.profile.modkey = value
						MDH:MDHEditMacro()
					end,
				values = modopts,
			},
			hmname = {
				type = "input",
				name = L["Macro name"],
				width = "double",
				order = 11,
				desc = L["Name to use for the macro"],
				get = function() return MDH.db.profile.hname end,
				set = function(info, value)
					MDH.db.profile.hname = value
					MDH:MDHEditMacro()
				end,
				hidden = function() return uc == "ROGUE" end,
			},
			rmname = {
				type = "input",
				name = L["Macro name"],
				desc = L["Name to use for the macro"],
				order = 12,
				width = "double",
				get = function() return MDH.db.profile.rname end,
				set = function(info, value)
					MDH.db.profile.rname = value
					MDH:MDHEditMacro()
				end,
				hidden = function() return uc == "HUNTER" end,
			},
		},
	}
I also created a "test-repo" if anyone needs to see the full code: https://github.com/The-Sickness/Misd...Repo/tree/main


https://www.wowinterface.com/forums/...1&d=1718281353
Attached Thumbnails
Click image for larger version

Name:	WoWScrnShot_061224_211647.jpg
Views:	61
Size:	133.1 KB
ID:	9887  
  Reply With Quote
06-13-24, 11:21 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,930
Lua Code:
  1. local imd = { name = tostring(select(1, GetSpellInfo(34477)) or "Unknown Spell"), id = 34477 }
With the move to C_Spell, things like C_Spell.GetSpellInfo now return a table of spell information rather than x number of returns containing the information

Code:
Name = "SpellInfo",
Type = "Structure",
Fields =
{
	{ Name = "name", Type = "string", Nilable = false },
	{ Name = "iconID", Type = "fileID", Nilable = false, Documentation = { "Icon for this spell; If spell has been overriden, this may be the icon for the overriding spell; See originalIconID for spell's non-overriden icon" } },
	{ Name = "originalIconID", Type = "fileID", Nilable = false },
	{ Name = "castTime", Type = "number", Nilable = false },
	{ Name = "minRange", Type = "number", Nilable = false },
	{ Name = "maxRange", Type = "number", Nilable = false },
	{ Name = "spellID", Type = "number", Nilable = false },
},
Lua Code:
  1. local spellInfo = GetSpellInfo(34477)
  2. local imd = { name = spellInfo and spellInfo.name or "Unknown Spell"), id = 34477 }
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-13-24 at 11:32 AM.
  Reply With Quote
06-13-24, 12:14 PM   #3
Sharpedge
A Cliff Giant
 
Sharpedge's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2022
Posts: 75
As usual you are awesome. Thank you very much!!
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Tables vs Strings issue

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off