WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Checking for files and [brackets] (https://www.wowinterface.com/forums/showthread.php?t=36555)

Sideshow 11-05-10 03:25 PM

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"


:confused:

Dridzt 11-05-10 05:33 PM

Quote:

Originally Posted by Sideshow (Post 216488)
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"


:confused:

"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 '\')

eberkain 11-06-10 06:04 AM

Quote:

Originally Posted by Dridzt (Post 216512)
"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?

Dridzt 11-06-10 08:02 AM

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.

Xubera 11-06-10 06:18 PM

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


Taroven 11-07-10 08:26 AM

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.

Xubera 11-07-10 05:17 PM

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?

Taroven 11-07-10 10:00 PM

Quote:

Originally Posted by Xubera (Post 216774)
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"].

Quote:

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.

Xubera 11-07-10 10:51 PM

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.

Taroven 11-08-10 02:01 AM

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.

Sideshow 11-08-10 03:00 AM

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

Dridzt 11-08-10 03:46 AM

Quote:

Originally Posted by Sideshow (Post 216809)
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.

nightcracker 11-08-10 04:47 AM

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.

Sideshow 11-08-10 04:55 AM

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.

nightcracker 11-08-10 05:11 AM

Quote:

Originally Posted by Sideshow (Post 216815)
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 :p

Seerah 11-08-10 04:26 PM

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?


All times are GMT -6. The time now is 08:46 AM.

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