ID:2345987
 
Problem description:

Anyone know how to retrieve a file from the cache when all you know is its file name at compile time? For clarification, what I'm looking for is something like the cacheFile() function in the following example:

var testIcon = 'file.dmi'
var iconName = "[testIcon]" // "file.dmi"
var cacheIcon = cacheFile(iconName)
ASSERT(cachIcon == testIcon)

Any ideas how to achieve this? (And I don't strictly need the ASSERT to pass. They can be technically different file instances so long as the icon is functionally equivalent.) This is for a library, so any solution will have to function on its own.
Only way I know of is to make a list associating names to cache references...
proc/cacheFile(iconName)
var global/list/iconFiles = list(
"file.dmi" = 'file.dmi',
// etc.
)
return iconFiles[iconName]

And of course you can automatically generate that list using flist() and text2file().
Yeah, I see what you're getting at. It won't work for my project as it's a library, and I won't be able to precompile that list. I'll go ahead updated the origin post with that clarification.

I feel like this data has to exist somewhere, because you can do the reverse: "[icon]" => "directory/myIcon.dmi"
var
list/hex_digits = list("0"=0,"1"=1,"2"=2,"3"=3,"4"=4,"5"=5,"6"=6,"7"=7,"8"=8,"9"=9,"a"=10,"b"=11,"c"=12,"d"=13,"e"=14,"f"=15,"A"=10,"B"=11,"C"=12,"D"=13,"E"=14,"F"=15)
list/resource_dict = resource_dictionary()
proc
resource_dictionary()
var/count = 1, ref, rsc = 1, list/l = list()
while(rsc)
ref = "\[0xc" + int2hex(count,3) + "]"
if((rsc = locate(ref)))
l["[rsc]"] = rsc
++count
return l

int2hex(num,bytes)
var/list/l = list()
while(l.len<bytes*2)
l.Insert(1,hex_digits[num%16+1])
num = round(num/16)
l.Insert(1,hex_digits[num%16+1])
num = round(num/16)
return jointext(l,null)

hex2int(str)
var/pos = length(str), mult = 1, num = 0
while(pos)
num += hex_digits[str[pos--]]*mult
mult *= 16
return num


The way it works, is it hunts through the rsc one at a time by building ref strings in sequence and then using them to locate the actual reference. Since we've already hunted down each item in the reference at startup, we're able to store them associated by name in that global list so you can retrieve them by name later.