You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
4.6 KiB
129 lines
4.6 KiB
|
1 year ago
|
-- JSON encoding function
|
||
|
|
local function _jsonEncode(value)
|
||
|
|
if type(value) == "table" then
|
||
|
|
local is_array = true
|
||
|
|
local max = 0
|
||
|
|
for k, v in pairs(value) do
|
||
|
|
if type(k) ~= "number" then
|
||
|
|
is_array = false
|
||
|
|
else
|
||
|
|
if k > max then max = k end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
if is_array then
|
||
|
|
local items = {}
|
||
|
|
for i = 1, max do
|
||
|
|
table.insert(items, _jsonEncode(value[i]))
|
||
|
|
end
|
||
|
|
return "[" .. table.concat(items, ",") .. "]"
|
||
|
|
else
|
||
|
|
local items = {}
|
||
|
|
for k, v in pairs(value) do
|
||
|
|
table.insert(items, _jsonEncode(k) .. ":" .. _jsonEncode(v))
|
||
|
|
end
|
||
|
|
return "{" .. table.concat(items, ",") .. "}"
|
||
|
|
end
|
||
|
|
elseif type(value) == "string" then
|
||
|
|
return '"' .. value:gsub('"', '\\"') .. '"'
|
||
|
|
elseif type(value) == "number" or type(value) == "boolean" then
|
||
|
|
return tostring(value)
|
||
|
|
else
|
||
|
|
return "null"
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
local function jsonEncode(value)
|
||
|
|
local status, result = pcall(function() return _jsonEncode(value) end)
|
||
|
|
if status then return result end
|
||
|
|
return nil, result
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Function to gather collection data
|
||
|
|
local function gatherCollections()
|
||
|
|
local collections = {
|
||
|
|
toys = {},
|
||
|
|
pets = {},
|
||
|
|
mounts = {},
|
||
|
|
transmogs = {}
|
||
|
|
}
|
||
|
|
|
||
|
|
-- Gather toy data
|
||
|
|
for i = 1, C_ToyBox.GetNumToys() do
|
||
|
|
local toyID = C_ToyBox.GetToyFromIndex(i)
|
||
|
|
local itemID, toyName, icon, isFavorite, hasFanfare, itemQuality = C_ToyBox.GetToyInfo(toyID)
|
||
|
|
local hasToy = PlayerHasToy(itemId)
|
||
|
|
if toyID and toyName and hasToy then
|
||
|
|
table.insert(collections.toys, {id = toyID, name = toyName})
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Gather pet data
|
||
|
|
local numPets, numOwned = C_PetJournal.GetNumPets()
|
||
|
|
for i = 1, numOwned do
|
||
|
|
local petID, speciesID, owned, customName, level, favorite, isRevoked, speciesName, icon, petType, companionID, tooltip, description, isWild, canBattle, isTradeable, isUnique, obtainable = C_PetJournal.GetPetInfoByIndex(i)
|
||
|
|
if petID and speciesID and speciesName then
|
||
|
|
table.insert(collections.pets, {id = speciesID, name = speciesName})
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Gather mount data
|
||
|
|
|
||
|
|
C_MountJournal.SetDefaultFilters()
|
||
|
|
C_MountJournal.SetCollectedFilterSetting(LE_MOUNT_JOURNAL_FILTER_COLLECTED, true)
|
||
|
|
C_MountJournal.SetCollectedFilterSetting(LE_MOUNT_JOURNAL_FILTER_NOT_COLLECTED, false)
|
||
|
|
C_MountJournal.SetCollectedFilterSetting(LE_MOUNT_JOURNAL_FILTER_UNUSABLE, true)
|
||
|
|
|
||
|
|
for i = 1, C_MountJournal.GetNumDisplayedMounts() do
|
||
|
|
local mountID = C_MountJournal.GetDisplayedMountID(i)
|
||
|
|
local mountName = C_MountJournal.GetMountInfoByID(mountID)
|
||
|
|
if mountID and mountName then
|
||
|
|
table.insert(collections.mounts, {id = mountID, name = mountName})
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Gather transmog data (collected appearances)
|
||
|
|
-- Gather transmog data (collected appearances)
|
||
|
|
local appearanceSources = C_TransmogCollection.GetCategoryAppearances(1)
|
||
|
|
for _, appearance in ipairs(appearanceSources) do
|
||
|
|
local sourceID = appearance.visualID
|
||
|
|
local categoryID, visualID, canEnchant, icon, isCollected, itemLink, transmogLink, unknown1, itemSubTypeIndex = C_TransmogCollection.GetAppearanceSourceInfo(sourceID)
|
||
|
|
if sourceID and itemLink then
|
||
|
|
table.insert(collections.transmogs, {id = sourceID, name = itemLink})
|
||
|
|
else
|
||
|
|
print("Error: Invalid sourceInfo for sourceID " .. tostring(sourceID)) -- Debug print
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
|
||
|
|
return collections
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Function to save data to a file (in-game saved variables)
|
||
|
|
local function saveCollectionsToFile()
|
||
|
|
local collections = gatherCollections()
|
||
|
|
local encodedData, error = jsonEncode(collections)
|
||
|
|
if not encodedData then
|
||
|
|
print("Error encoding data: " .. tostring(error))
|
||
|
|
return
|
||
|
|
end
|
||
|
|
-- Save to SavedVariables
|
||
|
|
_G["BronzeAppraiser_Data"] = encodedData
|
||
|
|
print("Collection data has been saved.")
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Register a slash command to export collections
|
||
|
|
SLASH_BRONZEAPPRAISER1 = "/exportcollections"
|
||
|
|
SlashCmdList["BRONZEAPPRAISER"] = function(msg)
|
||
|
|
saveCollectionsToFile()
|
||
|
|
end
|
||
|
|
|
||
|
|
-- Register event to initialize saved variables
|
||
|
|
local frame = CreateFrame("Frame")
|
||
|
|
frame:RegisterEvent("ADDON_LOADED")
|
||
|
|
frame:SetScript("OnEvent", function(self, event, addonName)
|
||
|
|
if addonName == "BronzeAppraiser" then
|
||
|
|
-- Initialize saved variables
|
||
|
|
_G["BronzeAppraiser_Data"] = _G["BronzeAppraiser_Data"] or {}
|
||
|
|
end
|
||
|
|
end)
|