/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2025 Mauricio G.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#define RB_FILESYSTEM_OS
#include <stdlib.h>
#include <string.h>

/* this is part of CTRDL and includes 3ds.h */
#include <dlfcn.h>
#ifdef RGB565
#undef RGB565
#endif

#include "system.h"
#include "load_code.h"
#include "filesystem-ctru.h"
#include "debug.h"

extern u32 __ctrl_code_allocator_pages;

void* programResolver(const char* sym, void *userData);
void * lc_open(const char *filename, unsigned char *buf, size_t buf_size)
{
    DEBUGF("dlopen(path=\"%s\")\n", filename);

    /* We need to increase __ctrl_code_allocator_pages value to 8 MB
       to enable big plugins */
    __ctrl_code_allocator_pages = 2048;

    /* note: the 3ds dlopen implementation needs a custom resolver
       for the unresolved symbols in shared objects */
    /* void *handle = dlopen(filename, RTLD_NOW | RTLD_LOCAL); */
    void *handle = ctrdlOpen(filename,
                             RTLD_NOW | RTLD_LOCAL,
                             programResolver,
                             NULL);
    if (handle == NULL)
    {
        DEBUGF("%s(\"%s\") failed\n", __func__, filename);
        DEBUGF("  lc_open error '%s'\n", dlerror());
    }
    DEBUGF("handle = %p\n", handle);

    return handle;
    (void) buf; (void) buf_size;
}

void * lc_get_header(void *handle)
{
    void *symbol = dlsym(handle, "__header");
    if (!symbol) {
        symbol = dlsym(handle, "___header");
    }

    return symbol;
}

void lc_close(void *handle)
{
    if (handle) {
        dlclose(handle);
    }
}
