Thread Tools Display Modes
10/19/14, 03:03 AM   #1
klaro00
 
klaro00's Avatar
Join Date: Apr 2014
Posts: 31
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

Last edited by klaro00 : 10/22/14 at 01:13 AM.
  Reply With Quote
10/19/14, 06:23 AM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
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).
  Reply With Quote
10/19/14, 08:49 AM   #3
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
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

Last edited by Garkin : 10/19/14 at 02:19 PM. Reason: fixed typo
  Reply With Quote
10/19/14, 01:09 PM   #4
klaro00
 
klaro00's Avatar
Join Date: Apr 2014
Posts: 31
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.)
  Reply With Quote
10/19/14, 02:14 PM   #5
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by klaro00 View Post
...

(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/)

Last edited by Garkin : 10/19/14 at 02:17 PM.
  Reply With Quote
10/21/14, 09:12 AM   #6
ZOS_ChipHilseberg
ZOS Staff!
Premium Member
Yes this person is from ZeniMax!
Join Date: Oct 2014
Posts: 551
I quickly threw in a GetAPIVersion function to make this easier in the future.
  Reply With Quote
10/21/14, 10:11 AM   #7
Halja
 
Halja's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 111
Originally Posted by ZOS_ChipHilseberg View Post
I quickly threw in a GetAPIVersion function to make this easier in the future.
Thank you Chip! Will that make it to 1.5.x or a bit more down the road like 1.6?
  Reply With Quote
10/21/14, 12:11 PM   #8
klaro00
 
klaro00's Avatar
Join Date: Apr 2014
Posts: 31
Originally Posted by ZOS_ChipHilseberg View Post
I quickly threw in a GetAPIVersion function to make this easier in the future.
Great news!
  Reply With Quote
10/21/14, 04:04 PM   #9
ZOS_ChipHilseberg
ZOS Staff!
Premium Member
Yes this person is from ZeniMax!
Join Date: Oct 2014
Posts: 551
It's likely to be in 1.5.
  Reply With Quote
10/21/14, 07:07 PM   #10
katkat42
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 155
Originally Posted by ZOS_ChipHilseberg View Post
It's likely to be in 1.5.
Wow, sweet! Thank you, Chip!
  Reply With Quote
10/21/14, 11:31 PM   #11
Fyrakin
 
Fyrakin's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 129
Good one
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » How to find out API version


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