Skip to content

Commit

Permalink
Merge pull request #1724 from Daztek/util-clean-resource-directory
Browse files Browse the repository at this point in the history
Util: add CleanResourceDirectory()
  • Loading branch information
Daztek authored Jan 14, 2024
2 parents 76e123b + 7a8e768 commit 69b2bfa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ https://github.com/nwnxee/unified/compare/build8193.36.9...HEAD
- N/A

##### New NWScript Functions
- N/A
- Util: CleanResourceDirectory()

### Changed
- NWNX_Item_RestoreItemAppearance() will now force an immediate update to the items AC, depending on new appearance.
Expand Down
15 changes: 15 additions & 0 deletions Plugins/Util/NWScript/nwnx_util.nss
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ int NWNX_Util_GetStringLevenshteinDistance(string sString, string sCompareTo);
/// @param oPlayer The player for which the objects needs to update, OBJECT_INVALID for all players
void NWNX_Util_UpdateClientObject(object oObjectToUpdate, object oPlayer = OBJECT_INVALID);

/// @brief Clean a resource directory, deleting all files of nResType.
/// @param sAlias A resource directory alias, NWNX or one defined in the custom resource directory file.
/// @param nResType The type of file to delete or 0xFFFF for all types.
/// @return TRUE if successful, FALSE on error.
int NWNX_Util_CleanResourceDirectory(string sAlias, int nResType = 0xFFFF);

/// @}

string NWNX_Util_GetCurrentScriptName(int depth = 0)
Expand Down Expand Up @@ -631,3 +637,12 @@ void NWNX_Util_UpdateClientObject(object oObjectToUpdate, object oPlayer = OBJEC
NWNX_PushArgumentObject(oObjectToUpdate);
NWNX_CallFunction(NWNX_Util, sFunc);
}

int NWNX_Util_CleanResourceDirectory(string sAlias, int nResType = 0xFFFF)
{
string sFunc = "CleanResourceDirectory";
NWNX_PushArgumentInt(nResType);
NWNX_PushArgumentString(sAlias);
NWNX_CallFunction(NWNX_Util, sFunc);
return NWNX_GetReturnValueInt();
}
27 changes: 22 additions & 5 deletions Plugins/Util/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,10 @@ NWNX_EXPORT ArgumentStack AddNSSFile(ArgumentStack&& args)
}

auto file = CExoFile((alias + ":" + fileName).c_str(), Constants::ResRefType::NSS, "w");

return file.FileOpened() && file.Write(contents) && file.Flush();
bool bOk = file.FileOpened() && file.Write(contents) && file.Flush();
if (bOk)
Globals::ExoResMan()->UpdateResourceDirectory(alias + ":");
return bOk;
}

NWNX_EXPORT ArgumentStack RemoveNWNXResourceFile(ArgumentStack&& args)
Expand All @@ -393,9 +395,10 @@ NWNX_EXPORT ArgumentStack RemoveNWNXResourceFile(ArgumentStack&& args)
alias = "NWNX";
}

CExoString exoFileName = alias + ":" + fileName;

return Globals::ExoResMan()->RemoveFile(exoFileName, type);
bool bOk = Globals::ExoResMan()->RemoveFile(alias + ":" + fileName, type);
if (bOk)
Globals::ExoResMan()->UpdateResourceDirectory(alias + ":");
return bOk;
}

NWNX_EXPORT ArgumentStack SetInstructionLimit(ArgumentStack&& args)
Expand Down Expand Up @@ -775,3 +778,17 @@ NWNX_EXPORT ArgumentStack UpdateClientObject(ArgumentStack&& args)

return {};
}

NWNX_EXPORT ArgumentStack CleanResourceDirectory(ArgumentStack&& args)
{
const auto alias = args.extract<std::string>();
const auto type = args.extract<int32_t>();
if (!Utils::IsValidCustomResourceDirectoryAlias(alias))
{
LOG_WARNING("NWNX_Util_CleanResourceDirectory() called with an invalid alias: %s", alias);
return false;
}
bool bOk = Globals::ExoResMan()->CleanDirectory(alias + ":", false, false, type);
Globals::ExoResMan()->UpdateResourceDirectory(alias + ":");
return bOk;
}

0 comments on commit 69b2bfa

Please sign in to comment.