nwnxee / unified

Binaries available under the Releases tab on Github
https://nwnxee.github.io/unified
GNU General Public License v3.0
128 stars 91 forks source link

Add 'NWNX_Player_SetBicFileName' #1727

Open Rapophage opened 5 months ago

Rapophage commented 5 months ago

Tried doing this myself, but I can't get nwnx to compile and I figured this would be something that fits right in with the player plugin.

NSS

/// @brief Sets the player's bic file's name
/// @param oPlayer The player
/// @param sBicName The filename
void NWNX_Player_SetBicFileName(object oPlayer, string sBicName);
void NWNX_Player_SetBicFileName(object oPlayer, string sBicName)
{
    string sFunc = "SetBicFileName";

    NWNX_PushArgumentString(sBicName);
    NWNX_PushArgumentObject(oPlayer);

    NWNX_CallFunction(NWNX_Player, sFunc);
}

CPP

NWNX_EXPORT ArgumentStack SetBicFileName(ArgumentStack&& args)
{
    if (auto *pPlayer = Utils::PopPlayer(args))
    {
        auto name = args.extract<std::string>();
        const uint8_t chartype = 4; // servervault subdir

        pPlayer->m_resFileName = name.c_str();
        pPlayer->m_nCharacterType = chartype;

        pPlayer->SaveServerCharacter();
    }
    return {};
}
Rapophage commented 5 months ago

Modified the code slightly.

I managed to get nwnx to compile and I've been running some tests. The function works fine, though only when a player already has a folder in the server vault. If a new player logs in and selects a character that's in the server vault's root folder, a player folder isn't created the changed character won't save. A player folder needs to be created first, though I currently have no idea how to do that.

Daztek commented 5 months ago

You can try adding:

CExoString sDirectory;
CNetLayerPlayerInfo *pPlayerInfo = Globals::AppManager()->m_pServerExoApp->GetNetLayer()->GetPlayerInfo(pPlayer->m_nPlayerID);
CExoString sSubdirectory = pPlayerInfo->m_lstKeys[0].sPublic;
if (Globals::AppManager()->m_pServerExoApp->GetServerInfo()->m_PersistantWorldOptions.bServerVaultByPlayerName)
    sSubdirectory = pPlayer->GetPlayerName();

if (sSubdirectory.IsEmpty())
    sDirectory.Format( "SERVERVAULT:%s", "lost+found");
else
    sDirectory.Format( "SERVERVAULT:%s", sSubdirectory.CStr());

Globals::ExoResMan()->CreateDirectory(sDirectory);

before the SaveServerCharacter() call.

Rapophage commented 5 months ago

You can try adding:

CExoString sDirectory;
CNetLayerPlayerInfo *pPlayerInfo = Globals::AppManager()->m_pServerExoApp->GetNetLayer()->GetPlayerInfo(pPlayer->m_nPlayerID);
CExoString sSubdirectory = pPlayerInfo->m_lstKeys[0].sPublic;
if (Globals::AppManager()->m_pServerExoApp->GetServerInfo()->m_PersistantWorldOptions.bServerVaultByPlayerName)
    sSubdirectory = pPlayer->GetPlayerName();

if (sSubdirectory.IsEmpty())
    sDirectory.Format( "SERVERVAULT:%s", "lost+found");
else
    sDirectory.Format( "SERVERVAULT:%s", sSubdirectory.CStr());

Globals::ExoResMan()->CreateDirectory(sDirectory);

before the SaveServerCharacter() call.

Yep, that worked!