Quantcast Checking for files and [brackets] - WoWInterface
Thread Tools Display Modes
11-05-10, 03:25 PM   #1
Sideshow
A Warpwood Thunder Caller
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 99
Checking for files and [brackets]

Hello

2 little questions:

- is there a way to check if a file is loaded. Some sort of isFile function
- what's the difference between using myfile = [[myfile]] and myfile = "myfile"



Last edited by Sideshow : 11-05-10 at 03:29 PM.
  Reply With Quote
11-05-10, 05:33 PM   #2
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,251
Originally Posted by Sideshow View Post
Hello

2 little questions:

- is there a way to check if a file is loaded. Some sort of isFile function
- what's the difference between using myfile = [[myfile]] and myfile = "myfile"


"No" to the first.

Difference is first version uses the string verbatim.
You can use [[Interface\AddOns\MyAddon\Sounds\my.mp3]]
as opposed to
"Interface\\AddOns\\MyAddon\\Sounds\\my.mp3" for the second. (had to escape '\')
  Reply With Quote
11-06-10, 06:04 AM   #3
eberkain
A Fallenroot Satyr
Join Date: Nov 2009
Posts: 20
Originally Posted by Dridzt View Post
"No" to the first.

Difference is first version uses the string verbatim.
You can use [[Interface\AddOns\MyAddon\Sounds\my.mp3]]
as opposed to
"Interface\\AddOns\\MyAddon\\Sounds\\my.mp3" for the second. (had to escape '\')
I agree there is no file checking function, but couldn't he declare a unique global variable in a file, and then do an if varname == nil in a different file, wouldn't that serve the same function in most cases?
  Reply With Quote
11-06-10, 08:02 AM   #4
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,251
Yea I put no in double-quotes because there's tricks you can do to "fake it".

But I got the OPs question to mean checking for a file that's not under your control.
(ie a file of your own addon that might or might not be present but you have control over what it contains if it's actually there)

And wowlua has no generic file io capabilities whatsoever so you cannot check for arbitrary files.
  Reply With Quote
11-06-10, 06:18 PM   #5
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
Well, I'm kinda confused on what file your trying to test for.

If its a file within your addon folder, simply make sure it gets called before the one that needs it in the .toc (or .xml <Script />)

if its a file from another addon, in your .toc add # Dependencies: OtherAddon

and that addon (and its associated files) will be loaded first.


also

Code:
local bool, retVal1, retVal2, reValn = pcall(someFunc, param1, param2, paramn)

if bool then
    --function was a success,
else
    --function threw an error :(
end
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
11-07-10, 08:26 AM   #6
Taroven
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 49
Post

Never felt the need myself, but this should work fine.

When an addon is loaded, each file is passed two arguments (accessed via "..."): The addon's name as WoW sees it, and a table. The table is specific to the addon and intended to be its namespace, allowing you to share variables and other info between files without polluting the global namespace (_G).

Knowing that, we can use that namespace to get around the file-checking issue.

Code:
-- Top of every file (change 'fileName' as needed)
local AddonName,ns = ...
ns.loadedFiles = ns.loadedFiles or {}
ns.loadedFiles['fileName'] = true

...

-- Check for the existence (or removal) of files. This only really needs to be in one file, can be folded into an existing OnEvent, and can do whatever you want.
local frame = CreateFrame('Frame',nil,UIParent)
frame:SetScript('OnEvent', function (self,event,addon)
    if addon == AddonName then
        self:UnregisterEvent('ADDON_LOADED')
        if ns.loadedFiles['otherFile'] then
            print('Found a file!')
        end
    end
end)
frame:RegisterEvent('ADDON_LOADED')
You want to check at ADDON_LOADED or your normal init event to be sure that every file is loaded. You can do some pretty cool stuff with this, like calling a certain function for each file or creating dummy variables if a specific file isn't found.

If you're working with libraries and such, you probably don't want to change their code. There are other, less invasive methods of checking for them.

Edit: And yes, you can turn that into an on-demand function instead.
Code:
local function isFile(file)
    return ns.loadedFiles[file]
end
...though that does the same thing as just using if(ns.loadedFiles[file]) directly.
__________________
Former author of EventHorizon Continued and Other Releases.

Last edited by Taroven : 11-07-10 at 09:04 AM.
  Reply With Quote
11-07-10, 05:17 PM   #7
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
well, what if her mod's name is "Ze best mod Eva"

if her addon is loaded last, wouldnt her onload only be called on functions after hers?
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
11-07-10, 10:00 PM   #8
Taroven
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 49
Originally Posted by Xubera View Post
well, what if her mod's name is "Ze best mod Eva"
Doesn't matter. Table entries may contain spaces, accessed via table["Long Spaced Out Entry Name"].

if her addon is loaded last, wouldnt her onload only be called on functions after hers?
ADDON_LOADED fires once an addon's files are loaded and its savedvariables are available ingame. I'm not entirely sure what you're asking here... The OnEvent script I posted can check any other addons loaded before the event fires - If more is needed, changing the event to PLAYER_ENTERING_WORLD should cover just about everything.

Basically, that script without any modifications will have info available for every file contained within that addon specifically, which is honestly all that should be needed.

If you're checking for another addon entirely, a simple IsAddOnLoaded('addonName') check can confirm that it's there. You can check for another addon's frames easily as long as they're named and in the global namespace, and if the addon uses global variables or makes its namespace available globally, those are also accessible.
__________________
Former author of EventHorizon Continued and Other Releases.
  Reply With Quote
11-07-10, 10:51 PM   #9
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
oh i wasnt saying Ze best mod ever because of spaces, but because it started with a "Z"

If the addons load in order, wouldn't her frame be unavailable until it was declared. Therefor A-Y addons wouldnt be added to her list. And if she wants to check for addons loaded status, theres an API for that. :/ I'm just trying to understand the purpose of the file the OP wants to know exists.
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
11-08-10, 02:01 AM   #10
Taroven
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 49
We're checking the contents of a table, not setting up an automatic indexing metamethod. Two completely different things you're thinking there. Besides, all ADDON_LOADED events are visible to all addons, the name or load order of the one checking shouldn't generally matter.

Here's how the earlier example code works. Let's say I had an addon with the following structure:
Code:
metaAddon.toc
metaAddon.lua
1.lua
2.lua
3.lua
4.lua
5.lua
Within each file is the first few lines of the example code, adding an entry to the loadedFiles entry in the addon's namespace table. Within metaAddon.lua is the rest of the code, checking that loadedFiles entry and doing stuff depending on what it finds.

Here's what the table would look like.
Code:
ns.loadedFiles = {
    1 = true,
    2 = true,
    3 = true,
    4 = true,
    5 = true,
}
That would let metaAddon.lua know if, say, a user deleted 4.lua. It might make something different happen depending on which files are present (like, if I added foo.lua, the addon could use a different function than normal for something complicated). It will not have any information regarding what files were loaded in Recount or Pitbull, because that's not what it was designed for.

It isn't meant to work with other addons at all - It's meant to keep track of which files within that specific addon are loaded. I went with the most obviously useful route, since Sideshow didn't specify - There's plenty of other tools for checking other addons, but not much in the way of checking for existence of files within your own.
__________________
Former author of EventHorizon Continued and Other Releases.

Last edited by Taroven : 11-08-10 at 02:11 AM. Reason: Code tags are awesome.
  Reply With Quote
11-08-10, 03:00 AM   #11
Sideshow
A Warpwood Thunder Caller
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 99
Hello

Because I'm to lame to include SharedMedia, I'm wondering if users could just drop their own font(s) in tinydps\fonts... so offcourse I need a way then to check if a certain file is loaded

Last edited by Sideshow : 11-08-10 at 03:06 AM.
  Reply With Quote
11-08-10, 03:46 AM   #12
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,251
Originally Posted by Sideshow View Post
I'm wondering if users could just drop their own font(s) in tinydps\fonts... so off-course I need a way then to check if a certain file is loaded
That's what I got from the initial post (checking of arbitrary files) and that's why I replied no.
  Reply With Quote
11-08-10, 04:47 AM   #13
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Ofcourse that's possible. You supply one default font with your addon, let's say Arial.

Step 1. You put Arial.ttf in TinyDPS and rename it font.ttf.
Step 2. You use Interface/AddOns/TinyDPS/font.ttf in your .lua file(s).
Step 3. You tell your users if they want a different font they must place it in the TinyDPS folder and rename it to font.ttf.
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
11-08-10, 04:55 AM   #14
Sideshow
A Warpwood Thunder Caller
 
Sideshow's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 99
That's the dirty way ...
I was just curious if it would possible to drop in a random font file and then recognise/use it in my script.
  Reply With Quote
11-08-10, 05:11 AM   #15
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Originally Posted by Sideshow View Post
That's the dirty way ...
I was just curious if it would possible to drop in a random font file and then recognise/use it in my script.
Well, according to wowprogramming it would be possible.

Code:
isValid = FontInstance:SetFont("filename", fontHeight, "flags")

<snip>

Returns:
isValid - 1 if filename refers to a valid font file; otherwise nil (1nil)
You can load files by checking for font1.ttf, font2.ttf, etc.

Example snippet:
Code:
local fonts, object = {}, CreateFont()

for i=1, 100 do
    local file = "Interface\\AddOns\\TinyDPS\\fonts\\font" .. i
    if object:SetFont(file, 12, "") then
        tinsert(fonts, file)
    end
end
All from the top of my head and untested.

You could also bruteforce aa, ab, ac, ba, zz, zzzzzzzzzzzzz, but that wouldn't be that efficient
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.

Last edited by nightcracker : 11-08-10 at 05:13 AM.
  Reply With Quote
11-08-10, 04:26 PM   #16
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,402
But then you would have to know the file name of the font in order to check if it was valid.

What's so hard about LSM support?
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Checking for files and [brackets]

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