@@ -547,60 +547,144 @@ function Data:ScanProfessions()
547547 if InCombatLockdown and InCombatLockdown () then return end
548548 local character = self :GetCharacter ()
549549 if not character then return end
550+ local skillLineVariants = self :GetSkillLineVariants ()
551+ local allSkillLineIDs = C_TradeSkillUI .GetAllProfessionTradeSkillLines ()
550552
551- -- Get all profession trade skill lines variants from the game that we care about
552- local skillLineVariants = C_TradeSkillUI .GetAllProfessionTradeSkillLines ()
553- local filteredSkillLineVariants = Utils :TableFilter (skillLineVariants or {}, function (skillLineVariantID )
553+ -- Filter skillLineVariants that we care about
554+ local filteredSkillLineIDs = Utils :TableFilter (allSkillLineIDs or {}, function (skillLineVariantID )
554555 local skillLineVariant = self :GetSkillLineVariantByID (skillLineVariantID )
555556 if not skillLineVariant then return false end
556557 local expansion = self :GetExpansionByID (skillLineVariant .expansionID )
557- if not expansion then return false end
558- if not expansion .enabled then return false end
558+ if not expansion or not expansion .enabled then return false end
559559 return true
560560 end )
561- if Utils :TableCount (filteredSkillLineVariants ) == 0 then
561+
562+ -- Something went wrong and the game doesn't have any profession data available
563+ if Utils :TableCount (filteredSkillLineIDs ) == 0 then
562564 return
563565 end
564566
565- Utils :TableForEach (skillLineVariants , function (skillLineVariantID )
567+ --- @class WK_LearnedProfession
568+ --- @field skillLineID number
569+ --- @field skillLineName string
570+ --- @field skillLineVariant WK_SkillLineVariant | nil
571+ --- @field skillLineVariantName string | nil
572+ --- @field skillLevel number
573+ --- @field skillMaxLevel number
574+
575+ -- Let's get the ProfessionsBook professions
576+ --- @type WK_LearnedProfession[]
577+ local learnedProfessions = {}
578+ local professionIndex1 , professionIndex2 = GetProfessions ()
579+ Utils :TableForEach ({professionIndex1 or 0 , professionIndex2 or 0 }, function (professionIndex )
580+ local skillLineName , _ , skillLevel , skillMaxLevel , _ , _ , skillLineID , _ , _ , _ , skillLineVariantName = GetProfessionInfo (professionIndex )
581+ if not skillLineName then return end
582+ local skillLine = self :GetSkillLineByID (skillLineID )
583+ if not skillLine then return end
584+ local skillLineVariant = Utils :TableGet (skillLineVariants , " name" , skillLineVariantName )
585+ --- @type WK_LearnedProfession
586+ local learnedProfession = {
587+ skillLineID = skillLineID ,
588+ skillLineName = skillLineName ,
589+ skillLineVariant = skillLineVariant ,
590+ skillLineVariantName = skillLineVariantName ,
591+ skillLevel = skillLevel ,
592+ skillMaxLevel = skillMaxLevel ,
593+ }
594+ table.insert (learnedProfessions , learnedProfession )
595+ Utils :Debug (" ├ Found Spellbook Profession: " .. (skillLineVariantName or skillLineName ))
596+ end )
597+
598+ local learnedSkillLineIDs = Utils :TableMap (learnedProfessions , function (spellbookProfession )
599+ return spellbookProfession .skillLineID
600+ end )
601+
602+ -- Detect if an old character profession should be removed (not in spellbook)
603+ character .professions = Utils :TableFilter (character .professions , function (characterProfession )
604+ local skillLineVariant = self :GetSkillLineVariantByID (characterProfession .skillLineVariantID )
605+ if not skillLineVariant or not Utils :TableContains (learnedSkillLineIDs or {}, skillLineVariant .skillLineID ) then
606+ addon .Core :Print (format (" Removing old profession: %s" , skillLineVariant and skillLineVariant .name or " Unknown" ))
607+ return false
608+ end
609+ return true
610+ end )
611+
612+ -- Detect if an invalid character profession should be removed (knowledge = 0/0)
613+ character .professions = Utils :TableFilter (character .professions , function (characterProfession )
614+ local skillLineVariant = self :GetSkillLineVariantByID (characterProfession .skillLineVariantID )
615+ if characterProfession .knowledgeLevel == 0 and characterProfession .knowledgeMaxLevel == 0 then
616+ addon .Core :Print (format (" Removing invalid profession: %s" , skillLineVariant and skillLineVariant .name or " Unknown" ))
617+ return false
618+ end
619+ return true
620+ end )
621+
622+ -- Add/update character professions based on the spellbook professions
623+ Utils :TableForEach (learnedProfessions , function (learnedProfession )
624+ local skillLineVariant = learnedProfession .skillLineVariant
625+ if not skillLineVariant then return end
626+ local characterProfession = Utils :TableFind (character .professions , function (characterProfession )
627+ return characterProfession .skillLineVariantID == skillLineVariant .id
628+ end )
629+ if not characterProfession then
630+ --- @type WK_CharacterProfession
631+ characterProfession = {
632+ enabled = true ,
633+ skillLineVariantID = skillLineVariant .id ,
634+ skillLevel = learnedProfession .skillLevel ,
635+ skillMaxLevel = learnedProfession .skillMaxLevel ,
636+ knowledgeLevel = 0 ,
637+ knowledgeMaxLevel = 0 ,
638+ knowledgeUnspent = 0 ,
639+ specializations = {},
640+ }
641+ table.insert (character .professions , characterProfession )
642+ addon .Core :Print (format (" Added profession: %s" , learnedProfession .skillLineVariantName or learnedProfession .skillLineName or " Unknown" ))
643+ end
644+ characterProfession .skillLevel = learnedProfession .skillLevel
645+ characterProfession .skillMaxLevel = learnedProfession .skillMaxLevel
646+ end )
647+
648+ -- Let's update all character professions
649+ Utils :TableForEach (filteredSkillLineIDs , function (skillLineVariantID )
566650 local skillLineVariant = self :GetSkillLineVariantByID (skillLineVariantID )
567651 if not skillLineVariant then return end
568- local expansion = self :GetExpansionByID (skillLineVariant .expansionID )
569- if not expansion then return end
570- if not expansion .enabled then return end
652+ local skillLine = self :GetSkillLineByID (skillLineVariant .skillLineID )
653+ if not skillLine then return end
571654
572- -- Find the character profession if it exists.
573655 local characterProfession = Utils :TableFind (character .professions , function (characterProfession )
574656 return characterProfession .skillLineVariantID == skillLineVariantID
575657 end )
576658
577- -- We are now only adding a new profession if we can actually access the profession info.
578- -- This means that the TradeSKillUI must have been opened once this session.
579- local professionInfo = C_TradeSkillUI .GetBaseProfessionInfo ()
580- if professionInfo and professionInfo .professionID > 0 then
581- local professionVariantInfo = C_TradeSkillUI .GetProfessionInfoBySkillLineID (skillLineVariantID )
582- if professionVariantInfo and professionVariantInfo .skillLevel and professionVariantInfo .skillLevel > 0 and professionVariantInfo .maxSkillLevel and professionVariantInfo .maxSkillLevel > 0 then
583- if not characterProfession then
584- --- @type WK_CharacterProfession
585- characterProfession = {
586- enabled = true ,
587- skillLineVariantID = skillLineVariantID ,
588- skillLevel = professionVariantInfo .skillLevel ,
589- skillMaxLevel = professionVariantInfo .maxSkillLevel ,
590- knowledgeLevel = 0 ,
591- knowledgeMaxLevel = 0 ,
592- knowledgeUnspent = 0 ,
593- specializations = {},
594- }
595- table.insert (character .professions , characterProfession )
596- else
659+ -- Only update if the opened profession window is our own
660+ if not C_TradeSkillUI .IsTradeSkillGuild () and not C_TradeSkillUI .IsTradeSkillLinked () then
661+ local professionInfo = C_TradeSkillUI .GetBaseProfessionInfo ()
662+ if professionInfo and professionInfo .professionID > 0 then
663+ local professionVariantInfo = C_TradeSkillUI .GetProfessionInfoBySkillLineID (skillLineVariantID )
664+ if professionVariantInfo and professionVariantInfo .skillLevel and professionVariantInfo .skillLevel > 0 and professionVariantInfo .maxSkillLevel and professionVariantInfo .maxSkillLevel > 0 then
665+ -- Add missing/new profession variants if they're relevant
666+ if not characterProfession then
667+ --- @type WK_CharacterProfession
668+ characterProfession = {
669+ enabled = true ,
670+ skillLineVariantID = skillLineVariantID ,
671+ skillLevel = professionVariantInfo .skillLevel ,
672+ skillMaxLevel = professionVariantInfo .maxSkillLevel ,
673+ knowledgeLevel = 0 ,
674+ knowledgeMaxLevel = 0 ,
675+ knowledgeUnspent = 0 ,
676+ specializations = {},
677+ }
678+ table.insert (character .professions , characterProfession )
679+ addon .Core :Print (format (" Added profession: %s" , skillLineVariant .name or skillLine .name or professionVariantInfo .professionName or " Unknown" ))
680+ end
597681 characterProfession .skillLevel = professionVariantInfo .skillLevel
598682 characterProfession .skillMaxLevel = professionVariantInfo .maxSkillLevel
599683 end
600684 end
601685 end
602686
603- -- Okay let's just give up here. This is not a profession we care about .
687+ -- Okay let's just stop here.
604688 if not characterProfession then
605689 return
606690 end
@@ -673,20 +757,6 @@ function Data:ScanProfessions()
673757 characterProfession .specializations = specializations
674758 end )
675759
676- -- Detect if a profession still exists on the character
677- local prof1 , prof2 = GetProfessions ()
678- if not prof1 or not prof2 then
679- local _ , _ , _ , _ , _ , _ , skillLine1 = GetProfessionInfo (prof1 or 0 )
680- local _ , _ , _ , _ , _ , _ , skillLine2 = GetProfessionInfo (prof2 or 0 )
681- Utils :TableForEach (character .professions , function (characterProfession , index )
682- local skillLineVariant = self :GetSkillLineVariantByID (characterProfession .skillLineVariantID )
683- if not skillLineVariant or not Utils :TableContains ({skillLine1 or 0 , skillLine2 or 0 }, skillLineVariant .skillLineID ) then
684- addon .Core :Print (format (" Removing profession: %s" , skillLineVariant and skillLineVariant .name or " Unknown" ))
685- table.remove (character .professions , index )
686- end
687- end )
688- end
689-
690760 character .lastUpdate = GetServerTime ()
691761 Utils :Debug (" ├ Professions: " , Utils :TableCount (character .professions ))
692762 Utils :Debug (" └ Finshed" )
0 commit comments