Thread Tools Display Modes
11-20-20, 05:48 PM   #1
Draugor
A Murloc Raider
Join Date: Nov 2020
Posts: 5
Hook into a MountJournal function ?

Hello everyone,

I'm currently trying add an extra option to the right click context menu in the mount collection.
After some digging in wowpedia and the Blizzard_MountCollection.lua i think i need to hook into MountOptionsMenu_Init and add my button via UIDropDownMenu_AddButton, but thats where i'm stumped.

Hooking into the C_MountJournal functions works just fine like so:
Lua Code:
  1. local orgMountUP = C_MountJournal.SummonByID;
  2. C_MountJournal.SummonByID = function(...)
  3.         print("hooked on a feeling ...")
  4.     orgMountUP(...);
  5. end

but hooking into MountOptionsMenu_Init not ...

Lua Code:
  1. local orgMountOptionsMenu_Init  = MountJournal.MountOptionsMenu_Init ; -- <-- Error global "MountJournal" is nil
  2. MountJournal.MountOptionsMenu_Init  = function(...)
  3.     print("hooked on a feeling ...")
  4.     orgMountOptionsMenu_Init(...);
  5. end
  6.  
  7. local orgCMountOptionsMenu_Init  = C_MountJournal.MountOptionsMenu_Init ;
  8. C_MountJournal.MountOptionsMenu_Init  = function(...)  --- <-- doesn't seem to be called
  9.     print("hooked on a feeling ...")
  10.     orgCMountOptionsMenu_Init(...);
  11. end

I also tried to hook into the ADDON_LOADED event and from there hook into MountJournal because at this point it shouldn't be nil, but to no avail. for code see here: https://pastebin.com/pXth1U6a

any tips ? is there an other way i haven't tried ? is this even possible ?
thx for your help

greetings
Draugor
  Reply With Quote
11-20-20, 08:53 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
If you mark you addons .toc to load when the Collections addon is loaded, you don't have to worry about tracking ADDON_LOADED (assuming that's the only addon you're waiting for and your addon isn't doing anything before the collections load, I didn't look).

Code:
## LoadOnDemand: 1
## LoadWith: Blizzard_Collections
You can use a hook like:
Lua Code:
  1. hooksecurefunc(C_MountJournal, "SummonByID", function(mountID)
  2.      print("hooked on a feeling ...", mountID)
  3. end)

and because MountOptionsMenu_Init is a standalone function (not a method of C_MountJournal):

Lua Code:
  1. hooksecurefunc("MountOptionsMenu_Init", function(self, level)
  2.      print("hooked on a feeling ...", self, level)
  3. end)

This will run your code after the original functions have completed running.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-20-20 at 09:09 PM.
  Reply With Quote
11-21-20, 03:57 AM   #3
Draugor
A Murloc Raider
Join Date: Nov 2020
Posts: 5
thanks for the fast reply

i tried this, but
Lua Code:
  1. hooksecurefunc("MountOptionsMenu_Init", function(self, level)
  2.     print("are we hooked yet?", self, level)
  3.     local info = UIDropDownMenu_CreateInfo();
  4.     info.text = "Extra Option"
  5.     info.func = function()
  6.         print("clicked")
  7.     end
  8.     UIDropDownMenu_AddButton(info, level)
  9. end)

that doesn't seem to get called, and i believe the problem here is that this function only gets called in the Loading-Phase of Blizzard_Collections so if i hook into it after it's loaded my hook never gets called, but also hooking before Blizzard_Collections is loaded doesn't work because then it isn't defined yet ... i think any ideas ? maybe i need to call it again after i hooked into it ...

aaaaand that worked just tested it with this line just below the hook call:
Lua Code:
  1. UIDropDownMenu_Initialize(MountJournal.mountOptionsMenu, MountOptionsMenu_Init, "MENU");

big thanks for your help !



edit: after some more testing and writing i came across an other problem,
using either
Lua Code:
  1. hooksecurefunc(C_MountJournal, "SummonByID", function(mountID)
  2.      print("hooked on a feeling ...", mountID)
  3. end)
or
Lua Code:
  1. local orgMountUP = C_MountJournal.SummonByID;
  2. C_MountJournal.SummonByID = function(...)
  3.     print("Mount Up !")
  4.     orgMountUP(...);
  5. end

only works if i click the Button in the MountJournal-Panel, but not if i track it in my hotbars and click it there :/
also regardless if hooksecurefunc(...) works it is probably not what i want in this case, because i want to replace some functionality of SummonByID(0) and not just do stuff afterwards, which means i need to replace the call (at least when its called with 0 )

Last edited by Draugor : 11-21-20 at 07:57 AM.
  Reply With Quote
11-21-20, 09:29 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Your code for the hooked MountOptionsMenu_Init function doesn't seem to require any information so the hook itself is not required. Just run your code when your addon loads or when you've detected the collections addon loading, whichever way you're doing it.

I can't see where you're using C_MountJournal.SummonByID so I don't know what you are trying to do with it but I can't imagine that trying to replace it with you own code is going to work.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
11-21-20, 11:24 AM   #5
Draugor
A Murloc Raider
Join Date: Nov 2020
Posts: 5
as i want to add an extra option to the context menu of the mountjournal i need to hook to that function as i still need the buttons blizzard is adding (un/favorite, cancel and un/mount) so thats fine, that part is working and doing what it should.

what i want to do with SummonByID is the following:

if ID is not 0 just call C_MountJournal.SummonByID
else
- choose a random mount from your favorites after a different logic then the blizzard one
- call SummonByID with ID i provide decided by my logic

that works so far, but only when i click the button in the journal or use a macro with
Code:
/run C_MountJournal.SummonByID(0)
but not when i drag the button into the actionbar and use that for some reason



edit: pastebin with everything i have currently: https://pastebin.com/iDBy0dhF

Last edited by Draugor : 11-21-20 at 11:28 AM.
  Reply With Quote
11-21-20, 01:23 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
I suspect you're trying to do two different things that need different approaches

Updating the menu requires the Blizzard Collection addon to be loaded so that needs to be tracked.

If you're not hooking the summon function until the collection is loaded then any call to summon won't have your "extra" bits until the Collections have been loaded (either by opened the collection UI or loading the addon yourself).

This will require you to track ADDON_LOADED for when the Blizzard Collection loads and update the menu then, but also hook the Summon function when "your" addon loads (as your character is entering the world).

Something like:
(remove the LoadOnDemand and LoadWith lines from the .toc if you have them)
Lua Code:
  1. DMU = {
  2.     MaxSourceFilters = 10,
  3.     MountGroundExceptions = {},
  4.     MountTypeToTravelType = {},
  5.     ZoneToExpansion = {},
  6.     TravelType = {
  7.         GROUND = 1,
  8.         FLYING = 2,
  9.         SWIMMING = 4,
  10.     },
  11.     Exp = {
  12.        BASE = 0,
  13.        BC = 1,
  14.        WRATH = 2,
  15.        CATA = 3,
  16.        PANDA = 4,
  17.        WOD = 5,
  18.        LEGION = 6,
  19.        BFA = 7,
  20.        SL = 8
  21.     },
  22. }
  23.  
  24. DMU.MountTypeToTravelType = {
  25.     [230] = DMU.TravelType.GROUND,
  26.     [231] = DMU.TravelType.SWIMMING,
  27.     [232] = DMU.TravelType.SWIMMING,
  28.     [241] = DMU.TravelType.GROUND,
  29.     [247] = DMU.TravelType.FLYING,
  30.     [248] = DMU.TravelType.FLYING,
  31.     [254] = DMU.TravelType.SWIMMING,
  32.     [269] = DMU.TravelType.GROUND,
  33.     [284] = DMU.TravelType.GROUND,
  34.     [398] = DMU.TravelType.FLYING
  35. }
  36.  
  37. DMU.ZoneToExpansion = {
  38.     [13]    = DMU.Exp.BASE, --- EasternKingdoms
  39.     [12]    = DMU.Exp.BASE, --- Kalimdor
  40.     [101]   = DMU.Exp.BC,   --- Outland
  41.     [113]   = DMU.Exp.WRATH,    --- Northrend
  42.     [424]   = DMU.Exp.PANDA,    --- Pandaria
  43.     [572]   = DMU.Exp.WOD,  --- Draenor
  44.     [619]   = DMU.Exp.LEGION,   --- BrokenIsles
  45.     [875]   = DMU.Exp.BFA,  --- Zandalar
  46.     [876]   = DMU.Exp.BFA,  --- KulTiras
  47.     [905]   = DMU.Exp.LEGION,   --- Argus
  48.     [948]   = DMU.Exp.CATA, --- TheMaelstrom
  49.     [985]   = DMU.Exp.BASE, --- EasternKingdoms
  50.     [986]   = DMU.Exp.BASE, --- Kalimdor
  51.     [987]   = DMU.Exp.BC,   --- Outland
  52.     [988]   = DMU.Exp.WRATH,    --- Northrend
  53.     [989]   = DMU.Exp.PANDA,    --- Pandaria
  54.     [990]   = DMU.Exp.WOD,  --- Draenor
  55.     [991]   = DMU.Exp.BFA,  --- Zandalar
  56.     [992]   = DMU.Exp.BFA,  --- KulTiras
  57.     [993]   = DMU.Exp.LEGION,   --- BrokenIsles
  58.     [994]   = DMU.Exp.LEGION,   --- Argus
  59.     [1011]  = DMU.Exp.BFA,  --- Zandalar
  60.     [1014]  = DMU.Exp.BFA,  --- KulTiras
  61.     [1208]  = DMU.Exp.BASE, --- EasternKingdoms
  62.     [1209]  = DMU.Exp.BASE, --- Kalimdor
  63.     [1384]  = DMU.Exp.WRATH,    --- Northrend
  64.     [1467]  = DMU.Exp.BC,   --- Outland
  65.     [1504]  = DMU.Exp.BFA,  --- Nazjatar
  66.     [1550]  = DMU.Exp.SL,   --- TheShadowlands
  67.     [1645]  = DMU.Exp.SL,   --- Torghast
  68.     [1647]  = DMU.Exp.SL    --- TheShadowlands
  69. }
  70. --------------------------------- file 2 --------------------------------------------------
  71. local panel = CreateFrame("FRAME")
  72. panel.name = "Draugor's Mount Up"
  73. panel:RegisterEvent("ADDON_LOADED")
  74. InterfaceOptions_AddCategory(panel)
  75. panel:SetScript("OnEvent", function(self, event, arg1)  
  76.     if event == "ADDON_LOADED" and arg1 == "Blizzard_Collections" then  
  77. -- This section loads when the Blizzard Collection does
  78.         local function MenuInt(self, level)
  79.             if not MountJournal.menuMountIndex then return; end
  80.             if C_MountJournal.NeedsFanfare(MountJournal.menuMountID) then return; end
  81.             --- local isFavorite, canFavorite = C_MountJournal.GetIsFavorite(MountJournal.menuMountIndex);
  82.             local mountTypeID = select(5,C_MountJournal.GetMountInfoExtraByID(MountJournal.menuMountID));
  83.             if DMU.MountTypeToTravelType[mountTypeID] == DMU.TravelType.GROUND then return; end
  84.             local info = UIDropDownMenu_CreateInfo();  
  85.             info.text = "Exception";
  86.             info.checked = DMU.MountGroundExceptions[MountJournal.menuMountID];
  87.             info.func = function()
  88.             DMU.MountGroundExceptions[MountJournal.menuMountID] = not DMU.MountGroundExceptions[MountJournal.menuMountID]
  89.             for i,v in pairs(DMU.MountGroundExceptions) do
  90.                 print(i,v)
  91.                 -- printresult = printresult .. tostring(i) .. ": " .. tostring(v) .. "\n"
  92.             end
  93.             end
  94.             UIDropDownMenu_AddButton(info, level)
  95.             MountOptionsMenu_Init(self, level)
  96.         end
  97.         MenuInt(MountJournal, 1)
  98.         UIDropDownMenu_Initialize(MountJournal.mountOptionsMenu, MenuInt, "MENU");
  99. -- This section might need to move to the PLAYER_LOGIN event?
  100.         if DMU.MountGroundExceptions == nil then
  101.             -- This is the first time this addon is loaded; set SVs to default values
  102.             DMU.MountGroundExceptions = {
  103.                 [1011] = true   -- Shu zen
  104.             }
  105.         end
  106.     end
  107. end)
  108.  
  109. -- This section loaded when your addon does (when the character is logging in)
  110. hooksecurefunc(C_MountJournal, "SummonByID", function(summonID)
  111.     print("Mount Up !", summonID)
  112.     if summonID == 0 then
  113.         C_MountJournal.Dismiss() -- Cancel the current random summon
  114.         DMU.MountUp ();
  115.     end
  116.  end)
  117.  
  118.  
  119.  DMU.MountUp = function()
  120.  --- deactivate all filters
  121.  
  122.  local tmp_filter1 = C_MountJournal.GetCollectedFilterSetting(1)
  123.  local tmp_filter2 = C_MountJournal.GetCollectedFilterSetting(2)
  124.  local SourceSet = {}
  125.  for i = 0, DMU.MaxSourceFilters do
  126.     if C_MountJournal.IsValidSourceFilter(i) then
  127.         SourceSet[i] =  C_MountJournal.IsSourceChecked(i)
  128.     end
  129.  end
  130.  
  131.  C_MountJournal.SetSearch("")
  132.  C_MountJournal.SetAllSourceFilters(true)
  133.  C_MountJournal.SetCollectedFilterSetting(1, true)
  134.  C_MountJournal.SetCollectedFilterSetting(2, false)
  135.  
  136. --- C_MountJournal.SummonByID(0)
  137. --- /script print(IsSpellKnown(233368))
  138. --- /script cn, si, i, a, iu, st, iF, iFS, f, hoc, iC, mi =C_MountJournal.GetDisplayedMountInfo(1) print(mi)
  139. --- /script a,b,c,d,mountTypeID,e,f,g,h = C_MountJournal.GetDisplayedMountInfoExtra(1) print(mountTypeID)
  140. --- /script print(C_Map.GetMapInfo(C_Map.GetBestMapForUnit("player")))
  141.  
  142. ---1011 --- Shu zen
  143.  
  144.     local canFlyAtAll = IsSpellKnown(90265) or IsSpellKnown(34090) or IsSpellKnown(34091)
  145.     local canFlyInArea = IsFlyableArea()
  146.  
  147.  
  148.     local underWater = IsSubmerged()
  149.     local summonType = DMU.TravelType.FLYING
  150.     if not(canFlyInArea) then
  151.         summonType = DMU.TravelType.GROUND
  152.     elseif underWater then
  153.         summonType = DMU.TravelType.SWIMMING
  154.     else
  155.         local currentZoneID = C_Map.GetBestMapForUnit("player")
  156.         local currentZoneInfo = C_Map.GetMapInfo(currentZoneID)  
  157.         while currentZoneInfo.mapType > 2 do
  158.             currentZoneID = currentZoneInfo.parentMapID
  159.             currentZoneInfo = C_Map.GetMapInfo(currentZoneID)
  160.         end
  161.         if  currentZoneInfo.mapType == 2 then
  162.             local currentExpansion = DMU.ZoneToExpansion[currentZoneID]
  163.             local canFlyBFA = IsSpellKnown(278833) and canFlyAtAll
  164.             -- local canFlySL = IsSpellKnown(278833)// ToDo Spell ID
  165.             if ((currentExpansion == DMU.Exp.BFA) and not(canFlyBFA)) or (currentExpansion == DMU.Exp.SL ) or false then
  166.                 summonType = DMU.TravelType.GROUND
  167.             else
  168.                 if(canFlyAtAll and canFlyInArea) then
  169.                     summonType = DMU.TravelType.FLYING
  170.                 else
  171.                     summonType = DMU.TravelType.GROUND
  172.                 end
  173.             end
  174.         end
  175.     end
  176.     local summonID = 0
  177.         local ids = {}
  178.         local counter = 0
  179.         local numMounts = C_MountJournal.GetNumDisplayedMounts()
  180.         for i= 1, numMounts do
  181.             local name, spellID, icon, isActive, isUsable, sourceType, isFavorite, isFactionSpecific, faction, shouldHideOnChar, isCollected, mountID = C_MountJournal.GetDisplayedMountInfo(i)
  182.             if isUsable and isFavorite then
  183.                 local mountTypeID = select(5,C_MountJournal.GetMountInfoExtraByID(mountID))
  184.                 if (summonType == DMU.MountTypeToTravelType[mountTypeID]) or (DMU.MountGroundExceptions[mountID] and (summonType ~= DMU.TravelType.SWIMMING)) then
  185.                     print(summonType, DMU.MountGroundExceptions[mountID], DMU.MountTypeToTravelType[mountTypeID])
  186.                     table.insert(ids, mountID)
  187.                     counter = counter + 1
  188.                 end
  189.                
  190.             end
  191.         end
  192.        
  193.  
  194.         if counter>0 then
  195.             local rand = random(counter)
  196.             summonID = ids[rand]
  197.         end
  198.     if IsMounted() then
  199.         Dismount()
  200.     end
  201.  
  202.     for i = 0, DMU.MaxSourceFilters do
  203.         if C_MountJournal.IsValidSourceFilter(i) then
  204.             C_MountJournal.SetSourceFilter(i, SourceSet[i])
  205.         end
  206.     end
  207.     C_MountJournal.SetCollectedFilterSetting(1, tmp_filter1)
  208.     C_MountJournal.SetCollectedFilterSetting(2, tmp_filter2)
  209.     C_MountJournal.SummonByID(summonID)
  210. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-22-20 at 12:57 AM.
  Reply With Quote
11-23-20, 03:38 AM   #7
Draugor
A Murloc Raider
Join Date: Nov 2020
Posts: 5
hmm sadly removing the loadWith and LoadOnDemand and putting the menuInit in ADDON_LOADED doesn't work either, now the context menu doesn't show up at all, although that worked before.

And even with this approach the hook for the "Summon Random Favorit Mount"-button gets called only when i manually press it in the MountCollection tab and not if i drag it on my hotbars :/

is it possible the dragged button becomes a spell and thats why it doesn't call the hook anymore ?

any other ideas ? appreciate your help, though


i mean last resort would be to auto-create a macro with just "/run C_MountJournal.SummonById(0)", because that works, and see if my addon can replace the "Summon Random Favorit Mount"-button in the hotbars with it. (or notify the user to replace the actionbar spell manually, but i would prefer if it just works out of the box)
  Reply With Quote
11-23-20, 09:51 AM   #8
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
If comment out your current code, copy/paste the code above and use that instead, it should work.

If you create a macro with:
Code:
/run C_MountJournal.SummonByID(0)
And drag that to your action bar.

I didn't do anything with the menu other than add the "Exception" entry to it.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
11-23-20, 12:03 PM   #9
Draugor
A Murloc Raider
Join Date: Nov 2020
Posts: 5
yeah, as stated above, using a macro works fine, but i wanted it to work without a macro.
oh well


If comment out your current code, copy/paste the code above and use that instead, it should work.
hmm i did that now, i even disabled all other addons, no luck ^^°
anyway i think i revert to your code from before and use it with the loadOnDemand part, that worked for everything except the hotbar-spell, but as that seemingly doesn't work without a macro anyway it should be fine.

thanks again for all your help, wouldn't have come this far without it !
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Hook into a MountJournal function ?

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