ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   How to find out API version (https://www.esoui.com/forums/showthread.php?t=2340)

klaro00 10/19/14 03:03 AM

How to find out API version
 
Hi,

ist there a reliable, robust way to find out the API version under which an addon is running? This would be useful when changing addon code in order to support an upcoming ESO version on the PTS server if the addon should run on both the current and the future API version.

I couldn't find any function like GetAPIversion() or so. Is there any?

I was thinking of writing such a function by examining "typical properties" of a certain API version, such as the existence of a function or a global value, like this:

Lua Code:
  1. function GetAPIVersion()
  2.     if not functionWhichWasRemovedInVersion100010 then
  3.         return 100010
  4.     end
  5.  
  6.     return 100009
  7. end

The shown approach is not very reliable since it checks for existence of a "magic" function which is no longer supported in a certain API version.

Maybe there is a better way? Any ideas?

// Klaro

merlight 10/19/14 06:23 AM

IIRC, at some point APIVersion on PTS wasn't even bumped immediately after the API changed, so even if there was GetAPIVersion() function, it wouldn't be 100% reliable.

I believe branching your code on some kind of GetAPIVersion (regardless of whether it's provided by the API or guessed from missing functions) is not the right way to support different APIs. If you need functionWhichWasRemovedInVersion100010, check for it's existence before you call it. Or alternatively, write a safety wrapper which will do the test and provide fallback functionality. Then your add-on will have a chance to function properly without being updated much longer.

The fact that APIVersion>=100010 doesn't mean functionWhichWasRemovedInVersion100010 doesn't exist. An compatibility add-on could provide it. Or the function may be revived in 100012. There is also the possibility of a function changing arguments (which has already happened, and the compatibility tool provided a wrapper that worked with add-ons using either) or returned values (which no compatibility tool can fix, because it can't tell what type of return you want; here you'd have to use your own wrapper that would catch unexpected returns after the call).

Garkin 10/19/14 08:49 AM

It's better to check if some new function exists instead:

Lua Code:
  1. function GetAPIVersion()
  2.     if GetLatency ~= nil then
  3.         return 100010
  4.     end
  5.  
  6.     return 100009
  7. end

My Compatibility Tool tries to emulate removed functions, so you can get wrong results.

Warning: Spoiler

klaro00 10/19/14 01:09 PM

Thanks,

so I was right that there is no direct way. I think I will test for new functions like GetLatency (in fact, I currently test for GetMaxLevel). Also thank you Garkin for the list of functions emulating older API functions; might be useful one day...

Cheers,
Klaro00

(Just in case you are interested in my current problem which led me to my questions, here are some details. It's not about missing functions, it's rather about access to the "settings menu" interface via ZO_OptionsWindow_InitializeControl(). ZO changed quite a lot behind the scenes, and kept this function for backward compatibility. But the data structure has been slightly changed by ZO as well, so I needed to do some fixes for 10010 which go beyond missing functions. This is solved, so I'm not asking for help with the settings menu; I'm also aware that there are approches like "LibAddonMenu" on the market... Because I don't want to fork my code to maintain two code versions, I was looking for a cheap way to support the current and the upcoming API version. That's it.)

Garkin 10/19/14 02:14 PM

Quote:

Originally Posted by klaro00 (Post 12698)
...

(Just in case you are interested in my current problem which led me to my questions, here are some details. It's not about missing functions, it's rather about access to the "settings menu" interface via ZO_OptionsWindow_InitializeControl(). ZO changed quite a lot behind the scenes, and kept this function for backward compatibility. But the data structure has been slightly changed by ZO as well, so I needed to do some fixes for 10010 which go beyond missing functions. This is solved, so I'm not asking for help with the settings menu; I'm also aware that there are approches like "LibAddonMenu" on the market... Because I don't want to fork my code to maintain two code versions, I was looking for a cheap way to support the current and the upcoming API version. That's it.)

This is why my compatibility tool uses function AddDataTable.

For example this is part of the function which creates checkbox in LibAddonMenu-1.0 (works on live server):
Lua Code:
  1. function lam:AddCheckbox(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
  2.     local isSubMenu = type(panelID) == "userdata"
  3.     local checkbox = wm:CreateControlFromVirtual(controlName, isSubMenu and panelID or optionsWindow, "ZO_Options_Checkbox")
  4.     checkbox:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 6)
  5.     checkbox.controlType = OPTIONS_CHECKBOX
  6.     checkbox.system = SETTING_TYPE_UI
  7.     checkbox.settingId = _G[strformat("SETTING_%s", controlName)]
  8.     checkbox.panel = isSubMenu and panelID.panel or panelID
  9.     checkbox.text = text
  10.     checkbox.tooltipText = tooltip

This is the same part of the function modified for Update 5 (keys are moved to the "data" table):
Lua Code:
  1. function lam:AddCheckbox(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
  2.     local isSubMenu = type(panelID) == "userdata"
  3.     local checkbox = wm:CreateControlFromVirtual(controlName, isSubMenu and panelID or optionsWindow, "ZO_Options_Checkbox")
  4.     checkbox:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 6)
  5.     checkbox.data = {
  6.         controlType = OPTIONS_CHECKBOX,
  7.         system = SETTING_TYPE_UI,
  8.         settingId = _G[strformat("SETTING_%s", controlName)],
  9.         panel = (isSubMenu and panelID.data.panel or panelID),
  10.         text = text,
  11.         tooltipText = tooltip,
  12.     }

If you want it working for both live and PTS, use original function + metatable with __index metamethod:
Lua Code:
  1. function lam:AddCheckbox(panelID, controlName, text, tooltip, getFunc, setFunc, warning, warningText)
  2.     local isSubMenu = type(panelID) == "userdata"
  3.     local checkbox = wm:CreateControlFromVirtual(controlName, isSubMenu and panelID or optionsWindow, "ZO_Options_Checkbox")
  4.     checkbox:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 6)
  5.     checkbox.controlType = OPTIONS_CHECKBOX
  6.     checkbox.system = SETTING_TYPE_UI
  7.     checkbox.settingId = _G[strformat("SETTING_%s", controlName)]
  8.     checkbox.panel = isSubMenu and panelID.panel or panelID
  9.     checkbox.text = text
  10.     checkbox.tooltipText = tooltip
  11.  
  12.     checkbox.data = setmetatable({}, {__index = checkbox}) --this is what it makes compatible with Update 5

If key doesn't exist in the checkbox.data table, it will look for metamethod __index. If __index is a function, it will return value from the function. If __index is a table, it will return key from that table. I.e. calling d(checkbox.data.tooltipText) will give the same result as calling d(checkbox.tooltipText).
(A bit more about metatables: http://nova-fusion.com/2011/06/30/lu...bles-tutorial/)

ZOS_ChipHilseberg 10/21/14 09:12 AM

I quickly threw in a GetAPIVersion function to make this easier in the future.

Halja 10/21/14 10:11 AM

Quote:

Originally Posted by ZOS_ChipHilseberg (Post 12712)
I quickly threw in a GetAPIVersion function to make this easier in the future.

:eek: Thank you Chip! Will that make it to 1.5.x or a bit more down the road like 1.6?
:banana:

klaro00 10/21/14 12:11 PM

Quote:

Originally Posted by ZOS_ChipHilseberg (Post 12712)
I quickly threw in a GetAPIVersion function to make this easier in the future.

Great news!

ZOS_ChipHilseberg 10/21/14 04:04 PM

It's likely to be in 1.5.

katkat42 10/21/14 07:07 PM

Quote:

Originally Posted by ZOS_ChipHilseberg (Post 12717)
It's likely to be in 1.5.

Wow, sweet! Thank you, Chip! :banana:

Fyrakin 10/21/14 11:31 PM

Good one :)


All times are GMT -6. The time now is 02:56 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI