Source

dark-hammer / src / engine / res-mgr.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/***********************************************************************************
 * Copyright (c) 2012, Sepehr Taghdisian
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 *
 ***********************************************************************************/

#include "res-mgr.h"
#include "core/core.h"
#include "core/array.h"
#include "core/stack.h"
#include "core/pool-alloc.h"
#include "core/hash-table.h"
#include "core/file-mgr.h"
#include "core/hash.h"
#include "core/atomic.h"
#include "mem-ids.h"

#include "engine.h"
#include "gfx-device.h"

#include "gfx-texture.h"
#include "gfx-model.h"
#include "script.h"
#include "anim.h"
#include "components/cmp-model.h"
#include "components/cmp-rbody.h"
#include "phx-prefab.h"

/*************************************************************************************************
 * defines
 */
#define GET_INDEX(hdl)        ((hdl)>>32)
#define GET_ID(hdl)           ((hdl)&0xffffffff)
#define MAKE_HANDLE(idx, id)  ((((uint64)(idx))<<32) | (((uint64)(id))&0xffffffff))
#define DICT_BLOCK_SIZE 4096

/*************************************************************************************************
 * types
 */
typedef void (*pfn_unload_res)(void* res);

struct resource
{
    reshandle_t hdl;
    uint32 ref_cnt;
    void* ptr;
    char filepath[128];
    pfn_unload_res unload_func;
    struct stack node;
};

struct freeslot_item
{
    uint32 idx;
    struct stack node;
};

struct res_mgr
{
    bool_t init;
    uint32 flags;
    struct array ress; /* item = struct resource */
    struct stack* freeslots; /* item = struct freeslot_item */
    struct pool_alloc freeslot_pool;
    struct hashtable_chained dict;
    struct pool_alloc dict_itempool;
    struct allocator dict_itemalloc;
    atomptr_t alloc;    /* data allocator */
};

/*************************************************************************************************
 * globals
 */
static struct res_mgr g_rs;

/*************************************************************************************************
 * forward declarations
 */
reshandle_t rs_add_resource(const char* filepath, void* ptr, reshandle_t override_hdl, 
                         pfn_unload_res unload_funcs);
reshandle_t rs_add_todb(const char* filepath, void* ptr, pfn_unload_res unload_funcs);
void rs_remove_fromdb(reshandle_t hdl);

void texture_unload(void* tex);
void model_unload(void* model);
void rs_anim_unload(void* anim);

/* callback for reloading textures */
void texture_reload(const char* filepath, reshandle_t hdl, uptr_t param1, uptr_t param2);

/*************************************************************************************************/
void rs_zero()
{
    memset(&g_rs, 0x00, sizeof(g_rs));
}

result_t rs_init(uint32 flags)
{
    result_t r;

    memset(&g_rs, 0x00, sizeof(g_rs));
    log_print(LOG_TEXT, "init res-mgr ...");

    r = arr_create(mem_heap(), &g_rs.ress, sizeof(struct resource), 100, 100, MID_RES);
    r |= mem_pool_create(mem_heap(), &g_rs.freeslot_pool, sizeof(struct freeslot_item),
        100, MID_RES);
    r |= mem_pool_create(mem_heap(), &g_rs.dict_itempool, sizeof(struct hashtable_item_chained),
        DICT_BLOCK_SIZE, MID_RES);
    mem_pool_bindalloc(&g_rs.dict_itempool, &g_rs.dict_itemalloc);
    r |= hashtable_chained_create(mem_heap(), &g_rs.dict_itemalloc, &g_rs.dict, 64*1024, MID_RES);
    if (IS_FAIL(r)) {
        err_printn(__FILE__, __LINE__, RET_OUTOFMEMORY);
        return r;
    }

    if (BIT_CHECK(flags, RS_FLAG_BGLOADING) || BIT_CHECK(flags, RS_FLAG_TEXTURESTREAMING))    {
        err_print(__FILE__, __LINE__, "res-mgr init failed: specified flag is not implemented");
        return RET_FAIL;
    }

    g_rs.alloc = mem_heap();    /* default allocator is heap */
    g_rs.flags = flags;
    g_rs.init = TRUE;
    return RET_OK;
}

void rs_release()
{
    hashtable_chained_destroy(&g_rs.dict);
    mem_pool_destroy(&g_rs.dict_itempool);
    mem_pool_destroy(&g_rs.freeslot_pool);
    arr_destroy(&g_rs.ress);

    rs_zero();

    log_print(LOG_TEXT, "res-mgr released.");
}

void texture_reload(const char* filepath, uint64 hdl, uptr_t param1, uptr_t param2)
{
    if (!g_rs.init)
        return;

    /* reload texture */
    rs_load_texture(filepath, (uint32)param1, (uint32)param2, RS_LOAD_REFRESH);
}

reshandle_t rs_load_texture(const char* tex_filepath, uint32 first_mipidx,
		bool_t srgb, uint32 flags)
{
    reshandle_t res_hdl = INVALID_HANDLE;
    reshandle_t override_hdl = res_hdl;

    if (!g_rs.init)
        return INVALID_HANDLE;

    struct hashtable_item_chained* item = hashtable_chained_find(&g_rs.dict, 
        hashtable_str(tex_filepath));
    if (item != NULL)   {
        uint32 idx = item->value;
        struct resource* res = (struct resource*)g_rs.ress.buffer + idx;
        res_hdl = res->hdl;
    }

    if (res_hdl != INVALID_HANDLE && BIT_CHECK(flags, RS_LOAD_REFRESH))    {
        /* resource already loaded, but refresh flag is set, so we reload it */
        if (BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))   {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
            override_hdl =  res_hdl;
            res_hdl = INVALID_HANDLE;
        }
    }   else if (res_hdl != INVALID_HANDLE) {
        /* add ref count */
        struct resource* ress = g_rs.ress.buffer;
        ress[GET_INDEX(res_hdl)].ref_cnt ++;
    }

    /* resource is not loaded before, so we just have to load it for the first time */
    if (res_hdl == INVALID_HANDLE)  {
        if(BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))  {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
        	/* determine file extension, then load the texture based on that */
        	char ext[128];
        	path_getfileext(ext, tex_filepath);
        	gfx_texture tex = NULL;

        	if (str_isequal_nocase(ext, "dds"))
        		tex  = gfx_texture_loaddds(tex_filepath, first_mipidx, srgb, 0);

            if (tex == NULL) {
                log_printf(LOG_WARNING, "res-mgr: loading resource '%s' failed:"
                    " could not load texture", tex_filepath);
                err_clear();
                if (override_hdl != INVALID_HANDLE)
                    rs_remove_fromdb(override_hdl);
                return INVALID_HANDLE;
            }

            res_hdl = rs_add_resource(tex_filepath, tex, override_hdl, texture_unload);

            /* add to hot-loading files */
            if (BIT_CHECK(g_rs.flags, RS_FLAG_HOTLOADING) && !BIT_CHECK(flags, RS_LOAD_REFRESH))   {
                io_mon_reg(tex_filepath, texture_reload, res_hdl, (uptr_t)first_mipidx, 
                    (uptr_t)srgb);
            }

            log_printf(LOG_LOAD, "(texture) \"%s\" - id: %d", tex_filepath, GET_ID(res_hdl));
        }
    }

    return res_hdl;
}

reshandle_t rs_add_resource(const char* filepath, void* ptr, reshandle_t override_hdl,
                         pfn_unload_res unload_func)
{
    reshandle_t res_hdl = override_hdl;

    /* if resource handle is overrided, release previous resource and put new one into 
     * current resource slot 
     */
    if (res_hdl != INVALID_HANDLE)  {
        struct resource* rss = g_rs.ress.buffer;
        struct resource* rs = &rss[GET_INDEX(res_hdl)];
        if (rs->hdl != INVALID_HANDLE)
            rs->unload_func(rs->ptr);

        rs->ptr = ptr;
        rs->hdl = res_hdl;
        rs->unload_func = unload_func;
        ASSERT(str_isequal(rs->filepath, filepath));
    }   else    {
        /* just add it to resource database */
        res_hdl = rs_add_todb(filepath, ptr, unload_func);
        if (res_hdl == INVALID_HANDLE)
            return INVALID_HANDLE;

        hashtable_chained_add(&g_rs.dict, hashtable_str(filepath), GET_INDEX(res_hdl));
    }

    return res_hdl;
}

reshandle_t rs_add_todb(const char* filepath, void* ptr, pfn_unload_res unload_func)
{
    static uint32 global_id = 0;
    global_id ++;

    reshandle_t res_hdl;
    struct resource* rs;

    /* if we have freeslots in free-stack, pop it and re-update it */
    struct stack* sitem = stack_pop(&g_rs.freeslots);
    if (sitem != NULL)  {
        uptr_t idx = (uptr_t)sitem->data;
        res_hdl = MAKE_HANDLE(idx, global_id);
        rs = &((struct resource*)g_rs.ress.buffer)[idx];
    }   else    {
        res_hdl = MAKE_HANDLE(g_rs.ress.item_cnt, global_id);
        rs = arr_add(&g_rs.ress);
        ASSERT(rs);
    }

    rs->hdl = res_hdl;
    rs->ptr = ptr;
    rs->ref_cnt = 1;
    rs->unload_func = unload_func;
    ASSERT(strlen(filepath) < 128);
    strcpy(rs->filepath, filepath);

    return res_hdl;
}

void rs_unload(reshandle_t hdl)
{
    ASSERT(hdl != INVALID_HANDLE);

    if (!g_rs.init)
        return;

    /* make sure handle is binded properly and is not invalid */
    uptr_t idx = GET_INDEX(hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct resource* rs = &((struct resource*)g_rs.ress.buffer)[idx];
    ASSERT(rs->hdl != INVALID_HANDLE);

    /* decr reference count, and see if have to release it */
    rs->ref_cnt --;
    if (rs->ref_cnt == 0)   {
        /* remove fron hot-loading list */
        if (BIT_CHECK(g_rs.flags, RS_FLAG_HOTLOADING))
            io_mon_unreg(rs->filepath);

        rs_remove_fromdb(hdl);
    }
}

/* removes from resoouce data base, and unloads resource data */
void rs_remove_fromdb(reshandle_t hdl)
{
    /* make sure handle is binded properly and is not invalid */
    uptr_t idx = GET_INDEX(hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct resource* rs = &((struct resource*)g_rs.ress.buffer)[idx];
    ASSERT(rs->hdl != INVALID_HANDLE);

    struct hashtable_item_chained* item = hashtable_chained_find(&g_rs.dict,
        hashtable_str(rs->filepath));
    ASSERT(item);
    hashtable_chained_remove(&g_rs.dict, item);

    /* add item to free slots */
    stack_push(&g_rs.freeslots, &rs->node, (void*)idx);
    
    rs->unload_func(rs->ptr);
    rs->hdl = INVALID_HANDLE;
}

void texture_unload(void* tex)
{
	gfx_destroy_texture(tex);
}

reshandle_t rs_load_model(const char* model_filepath, uint32 flags)
{
    reshandle_t res_hdl = INVALID_HANDLE;
    reshandle_t override_hdl = res_hdl;

    if (!g_rs.init)
        return INVALID_HANDLE;

    struct hashtable_item_chained* item = hashtable_chained_find(&g_rs.dict,
        hashtable_str(model_filepath));
    if (item != NULL)   {
        uint32 idx = item->value;
        struct resource* res = (struct resource*)g_rs.ress.buffer + idx;
        res_hdl = res->hdl;
    }

    if (res_hdl != INVALID_HANDLE && BIT_CHECK(flags, RS_LOAD_REFRESH))    {
        /* resource already loaded, but refresh flag is set, so we reload it */
        if (BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))   {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
            override_hdl =  res_hdl;
            res_hdl = INVALID_HANDLE;
        }
    }   else if (res_hdl != INVALID_HANDLE) {
        /* add ref count */
        struct resource* ress = g_rs.ress.buffer;
        ress[GET_INDEX(res_hdl)].ref_cnt ++;
    }

    /* resource is not loaded before, so we just have to load it for the first time */
    if (res_hdl == INVALID_HANDLE)  {
        if(BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))  {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
        	/* determine file extension, then load the texture based on that */
        	char ext[128];
        	path_getfileext(ext, model_filepath);
        	struct gfx_model* model = NULL;

        	/* model files should be 'h3dm' extension */
        	if (str_isequal_nocase(ext, "h3dm"))
        		model = gfx_model_loadh3d(g_rs.alloc, model_filepath, 0);

            if (model == NULL) {
                log_printf(LOG_WARNING, "res-mgr: loading resource '%s' failed:"
                    " could not load model", model_filepath);
                err_clear();
                if (override_hdl != INVALID_HANDLE)
                    rs_remove_fromdb(override_hdl);
                return INVALID_HANDLE;
            }

            res_hdl = rs_add_resource(model_filepath, model, override_hdl, model_unload);

            /* add to hot-loading files */
            if (BIT_CHECK(g_rs.flags, RS_FLAG_HOTLOADING) && !BIT_CHECK(flags, RS_LOAD_REFRESH))
                io_mon_reg(model_filepath, cmp_model_reload, res_hdl, 0, 0);
                
            log_printf(LOG_LOAD, "(model) \"%s\" - id: %d", model_filepath, GET_ID(res_hdl));
        }
    }

    return res_hdl;
}

void model_unload(void* model)
{
	gfx_model_unload(model);
}

reshandle_t rs_load_anim(const char* anim_filepath, uint32 flags)
{
    reshandle_t res_hdl = INVALID_HANDLE;
    reshandle_t override_hdl = res_hdl;

    if (!g_rs.init)
        return INVALID_HANDLE;

    struct hashtable_item_chained* item = hashtable_chained_find(&g_rs.dict,
        hashtable_str(anim_filepath));
    if (item != NULL)   {
        uint32 idx = item->value;
        struct resource* res = (struct resource*)g_rs.ress.buffer + idx;
        res_hdl = res->hdl;
    }

    if (res_hdl != INVALID_HANDLE && BIT_CHECK(flags, RS_LOAD_REFRESH))    {
        /* resource already loaded, but refresh flag is set, so we reload it */
        if (BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))   {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
            override_hdl =  res_hdl;
            res_hdl = INVALID_HANDLE;
        }
    }   else if (res_hdl != INVALID_HANDLE) {
        /* add ref count */
        struct resource* ress = g_rs.ress.buffer;
        ress[GET_INDEX(res_hdl)].ref_cnt ++;
    }

    /* resource is not loaded before, so we just have to load it for the first time */
    if (res_hdl == INVALID_HANDLE)  {
        if(BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))  {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
            /* determine file extension, then load the texture based on that */
            char ext[128];
            path_getfileext(ext, anim_filepath);
            struct anim_clip* clip = NULL;

            /* model files should be valid extension */
            if (str_isequal_nocase(ext, "h3da"))
                clip = anim_loadh3da(g_rs.alloc, anim_filepath, 0);

            if (clip == NULL) {
                log_printf(LOG_WARNING, "res-mgr: loading resource '%s' failed:"
                    " could not load anim-clip", anim_filepath);
                err_clear();
                return INVALID_HANDLE;
            }

            res_hdl = rs_add_resource(anim_filepath, clip, override_hdl, rs_anim_unload);
            log_printf(LOG_LOAD, "(anim) \"%s\" - id: %d", anim_filepath, GET_ID(res_hdl));
        }
    }

    return res_hdl;
}

void rs_anim_unload(void* anim)
{
    anim_unload(anim);
}

reshandle_t rs_load_script(const char* lua_filepath, uint32 flags)
{
    reshandle_t res_hdl = INVALID_HANDLE;
    reshandle_t override_hdl = res_hdl;

    if (!g_rs.init)
        return INVALID_HANDLE;

    struct hashtable_item_chained* item = hashtable_chained_find(&g_rs.dict,
        hashtable_str(lua_filepath));
    if (item != NULL)   {
        uint32 idx = item->value;
        struct resource* res = (struct resource*)g_rs.ress.buffer + idx;
        res_hdl = res->hdl;
    }

    if (res_hdl != INVALID_HANDLE && BIT_CHECK(flags, RS_LOAD_REFRESH))    {
        /* resource already loaded, but refresh flag is set, so we reload it */
        if (BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))   {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
            override_hdl =  res_hdl;
            res_hdl = INVALID_HANDLE;
        }
    }   else if (res_hdl != INVALID_HANDLE) {
        /* add ref count */
        struct resource* ress = g_rs.ress.buffer;
        ress[GET_INDEX(res_hdl)].ref_cnt ++;
    }

    /* resource is not loaded before, so we just have to load it for the first time */
    if (res_hdl == INVALID_HANDLE)  {
        if(BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))  {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
            /* determine file extension, then load the texture based on that */
            char ext[128];
            path_getfileext(ext, lua_filepath);
            sct_t script = NULL;

            /* model files should be valid extension */
            if (str_isequal_nocase(ext, "lua"))
                script = sct_load(lua_filepath);

            if (script == NULL) {
                err_sendtolog(TRUE);
                log_printf(LOG_WARNING, "res-mgr: loading resource '%s' failed:"
                    " could not load script", lua_filepath);
                if (override_hdl != INVALID_HANDLE)
                    rs_remove_fromdb(override_hdl);
                return INVALID_HANDLE;
            }

            res_hdl = rs_add_resource(lua_filepath, script, override_hdl, sct_unload);

            /* add to hot-loading files */
            if (BIT_CHECK(g_rs.flags, RS_FLAG_HOTLOADING) && !BIT_CHECK(flags, RS_LOAD_REFRESH))
                io_mon_reg(lua_filepath, sct_reload, res_hdl, 0, 0);

            log_printf(LOG_LOAD, "(script) \"%s\" - id: %d", lua_filepath, GET_ID(res_hdl));
        }
    }

    return res_hdl;
}

sct_s rs_get_script(reshandle_t script_hdl)
{
    ASSERT(g_rs.init);

    uint32 idx = GET_INDEX(script_hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct resource* r = &((struct resource*)g_rs.ress.buffer)[idx];

    ASSERT(r->hdl != INVALID_HANDLE);
    ASSERT(GET_ID(r->hdl) == GET_ID(script_hdl));
    return r->ptr;
}

reshandle_t rs_load_phxprefab(const char* phx_filepath, uint32 flags)
{
    reshandle_t res_hdl = INVALID_HANDLE;
    reshandle_t override_hdl = res_hdl;

    if (!g_rs.init)
        return INVALID_HANDLE;

    struct hashtable_item_chained* item = hashtable_chained_find(&g_rs.dict,
        hashtable_str(phx_filepath));
    if (item != NULL)   {
        uint32 idx = item->value;
        struct resource* res = (struct resource*)g_rs.ress.buffer + idx;
        res_hdl = res->hdl;
    }

    if (res_hdl != INVALID_HANDLE && BIT_CHECK(flags, RS_LOAD_REFRESH))    {
        /* resource already loaded, but refresh flag is set, so we reload it */
        if (BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))   {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
            override_hdl =  res_hdl;
            res_hdl = INVALID_HANDLE;
        }
    }   else if (res_hdl != INVALID_HANDLE) {
        /* add ref count */
        struct resource* ress = g_rs.ress.buffer;
        ress[GET_INDEX(res_hdl)].ref_cnt ++;
    }

    /* resource is not loaded before, so we just have to load it for the first time */
    if (res_hdl == INVALID_HANDLE)  {
        if(BIT_CHECK(g_rs.flags, RS_FLAG_BGLOADING))  {
            ASSERT(0);  /* TODO: load in background */
        }   else    {
            /* determine file extension, then load the texture based on that */
            char ext[128];
            path_getfileext(ext, phx_filepath);
            phx_prefab prefab = NULL;

            /* model files should be valid extension */
            if (str_isequal_nocase(ext, "h3dp"))
                prefab = phx_prefab_load(phx_filepath, g_rs.alloc, 0);

            if (prefab == NULL) {
                err_sendtolog(TRUE);
                log_printf(LOG_WARNING, "res-mgr: loading resource '%s' failed:"
                    " could not load phx-prefab", phx_filepath);
                if (override_hdl != INVALID_HANDLE)
                    rs_remove_fromdb(override_hdl);
                return INVALID_HANDLE;
            }

            res_hdl = rs_add_resource(phx_filepath, prefab, override_hdl, phx_prefab_unload);

            /* add to hot-loading files */
            if (BIT_CHECK(g_rs.flags, RS_FLAG_HOTLOADING) && !BIT_CHECK(flags, RS_LOAD_REFRESH))
                io_mon_reg(phx_filepath, cmp_rbody_reload, res_hdl, 0, 0);

            log_printf(LOG_LOAD, "(physics) \"%s\" - id: %d", phx_filepath, GET_ID(res_hdl));
        }
    }

    return res_hdl;
}

phx_prefab rs_get_phxprefab(reshandle_t prefab_hdl)
{
    ASSERT(g_rs.init);

    uint32 idx = GET_INDEX(prefab_hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct resource* r = &((struct resource*)g_rs.ress.buffer)[idx];

    ASSERT(r->hdl != INVALID_HANDLE);
    ASSERT(GET_ID(r->hdl) == GET_ID(prefab_hdl));
    return r->ptr;
}

void rs_reportleaks()
{
    struct resource* rss = g_rs.ress.buffer;
    for (uint32 i = 0; i < g_rs.ress.item_cnt; i++)   {
        const struct resource* r = &rss[i];
        if (r->hdl != INVALID_HANDLE && r->ref_cnt > 0) {
            log_printf(LOG_WARNING, "res-mgr: unreleased \"%s\" (ref_cnt = %d, id = %d)", 
                r->filepath, r->ref_cnt, GET_ID(r->hdl));
        }
    }
}

gfx_texture rs_get_texture(reshandle_t tex_hdl)
{
    ASSERT(g_rs.init);

    uint32 idx = GET_INDEX(tex_hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct resource* r = &((struct resource*)g_rs.ress.buffer)[idx];

    ASSERT(r->hdl != INVALID_HANDLE);
    ASSERT(GET_ID(r->hdl) == GET_ID(tex_hdl));
    return r->ptr;
}

struct gfx_model* rs_get_model(reshandle_t mdl_hdl)
{
    ASSERT(g_rs.init);

    uint32 idx = GET_INDEX(mdl_hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct resource* r = &((struct resource*)g_rs.ress.buffer)[idx];

    ASSERT(r->hdl != INVALID_HANDLE);
    ASSERT(GET_ID(r->hdl) == GET_ID(mdl_hdl));
    return r->ptr;
}

struct anim_clip* rs_get_anim(reshandle_t anim_hdl)
{
    ASSERT(g_rs.init);

    uint32 idx = GET_INDEX(anim_hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct resource* r = &((struct resource*)g_rs.ress.buffer)[idx];

    ASSERT(r->hdl != INVALID_HANDLE);
    ASSERT(GET_ID(r->hdl) == GET_ID(anim_hdl));
    return r->ptr;
}

void rs_set_dataalloc(struct allocator* alloc)
{
    ATOMIC_SET_PTR(g_rs.alloc, alloc);
}

bool_t rs_isinit()
{
    return g_rs.init;
}

const char* rs_get_filepath(reshandle_t hdl)
{
    if (!g_rs.init)
        return "";

    /* make sure handle is binded properly and is not invalid */
    uptr_t idx = GET_INDEX(hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct resource* rs = &((struct resource*)g_rs.ress.buffer)[idx];
    ASSERT(rs->hdl != INVALID_HANDLE);

    return rs->filepath;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.