102 lines
2.9 KiB
C
102 lines
2.9 KiB
C
SharedResourceState shared_resource_state = ZI;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Startup
|
|
|
|
void InitResourceSystem(u64 archive_strings_count, String *archive_strings)
|
|
{
|
|
SharedResourceState *g = &shared_resource_state;
|
|
Arena *perm = PermArena();
|
|
for (u64 archive_string_index = 0; archive_string_index < archive_strings_count; ++archive_string_index)
|
|
{
|
|
String archive = archive_strings[archive_string_index];
|
|
if (archive.len > 0)
|
|
{
|
|
BB_Buff bb = BB_BuffFromString(archive);
|
|
BB_Reader br = BB_ReaderFromBuff(&bb);
|
|
|
|
u64 magic = BB_ReadUBits(&br, 64);
|
|
Assert(magic == ResourceEmbeddedMagic);
|
|
|
|
/* Create & insert entries */
|
|
u64 num_entries = BB_ReadUBits(&br, 64);
|
|
for (u64 i = 0; i < num_entries; ++i)
|
|
{
|
|
u64 name_start = BB_ReadUBits(&br, 64);
|
|
u64 name_len = BB_ReadUBits(&br, 64);
|
|
u64 data_start = BB_ReadUBits(&br, 64);
|
|
u64 data_len = BB_ReadUBits(&br, 64);
|
|
|
|
ResourceEntry *entry = PushStruct(perm, ResourceEntry);
|
|
entry->name = STRING(name_len, archive.text + name_start);
|
|
entry->data = STRING(data_len, archive.text + data_start);
|
|
entry->hash = HashFnv64(Fnv64Basis, entry->name);
|
|
|
|
ResourceEntryBin *bin = &g->bins[entry->hash % NumResourceEntryBins];
|
|
SllQueuePushN(bin->first, bin->last, entry, next_in_bin);
|
|
SllQueuePushN(g->first_entry, g->last_entry, entry, next);
|
|
}
|
|
g->entries_count += num_entries;
|
|
}
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Resource helpers
|
|
|
|
b32 IsResourceNil(ResourceKey resource)
|
|
{
|
|
return resource.hash == 0;
|
|
}
|
|
|
|
ResourceKey ResourceKeyFromStore(ResourceStore *store, String name)
|
|
{
|
|
ResourceKey result = ZI;
|
|
result.hash = HashFnv64(store->hash, name);
|
|
return result;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Resource cache operations
|
|
|
|
ResourceEntry *ResourceEntryFromHash(u64 hash)
|
|
{
|
|
ResourceEntry *result = 0;
|
|
SharedResourceState *g = &shared_resource_state;
|
|
ResourceEntryBin *bin = &g->bins[hash % NumResourceEntryBins];
|
|
for (ResourceEntry *e = bin->first; e; e = e->next_in_bin)
|
|
{
|
|
if (e->hash == hash)
|
|
{
|
|
result = e;
|
|
break;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Resource operations
|
|
|
|
String DataFromResource(ResourceKey resource)
|
|
{
|
|
String result = ZI;
|
|
ResourceEntry *entry = ResourceEntryFromHash(resource.hash);
|
|
if (entry)
|
|
{
|
|
result = entry->data;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
String NameFromResource(ResourceKey resource)
|
|
{
|
|
String result = ZI;
|
|
ResourceEntry *entry = ResourceEntryFromHash(resource.hash);
|
|
if (entry)
|
|
{
|
|
result = entry->name;
|
|
}
|
|
return result;
|
|
}
|