Skip to content

Commit

Permalink
config: Implement subdirectory traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
nosoop committed Aug 5, 2021
1 parent 019332f commit 06cfb87
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
70 changes: 35 additions & 35 deletions scripting/cwx/item_config.sp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Contains functionality for the item config.
*/

#include <stocksoup/files>

enum struct CustomItemDefinition {
KeyValues source;

Expand Down Expand Up @@ -48,42 +50,40 @@ void LoadCustomItemConfig() {
itemSchema.ImportFromFile(schemaPath);

char schemaDir[PLATFORM_MAX_PATH];
BuildPath(Path_SM, schemaDir, sizeof(schemaDir), "configs/%s", "cwx/");
DirectoryListing cwxConfigs = OpenDirectory(schemaDir, false);

if (cwxConfigs) {
// find files within `configs/cwx/` and import them, too
FileType ftype;
char schemaRelPath[PLATFORM_MAX_PATH];
while (cwxConfigs.GetNext(schemaRelPath, sizeof(schemaRelPath), ftype)) {
if (ftype != FileType_File) {
continue;
}

BuildPath(Path_SM, schemaPath, sizeof(schemaPath), "configs/cwx/%s", schemaRelPath);

KeyValues importKV = new KeyValues("import");
importKV.ImportFromFile(schemaPath);

char uid[MAX_ITEM_IDENTIFIER_LENGTH];
importKV.GotoFirstSubKey(false);
do {
importKV.GetSectionName(uid, sizeof(uid));
if (importKV.GetDataType(NULL_STRING) == KvData_None) {
if (itemSchema.JumpToKey(uid)) {
LogMessage("Item uid %s already exists in schema, ignoring entry in %s",
uid, schemaRelPath);
} else {
itemSchema.JumpToKey(uid, true);
itemSchema.Import(importKV);
}
itemSchema.GoBack();
}
} while (importKV.GotoNextKey(false));
importKV.GoBack();

delete importKV;
BuildPath(Path_SM, schemaDir, sizeof(schemaDir), "configs/%s", "cwx");

// find files within `configs/cwx/` and import them, too
ArrayList configFiles = GetFilesInDirectoryRecursive(schemaDir);
for (int i, n = configFiles.Length; i < n; i++) {
configFiles.GetString(i, schemaPath, sizeof(schemaPath));
NormalizePathToPOSIX(schemaPath);

// skip files in directories named "disabled", much like SourceMod
if (StrContains(schemaPath, "/disabled/") != -1) {
continue;
}

KeyValues importKV = new KeyValues("import");
importKV.ImportFromFile(schemaPath);

char uid[MAX_ITEM_IDENTIFIER_LENGTH];
importKV.GotoFirstSubKey(false);
do {
importKV.GetSectionName(uid, sizeof(uid));
if (importKV.GetDataType(NULL_STRING) == KvData_None) {
if (itemSchema.JumpToKey(uid)) {
LogMessage("Item uid %s already exists in schema, ignoring entry in %s",
uid, schemaPath);
} else {
itemSchema.JumpToKey(uid, true);
itemSchema.Import(importKV);
}
itemSchema.GoBack();
}
} while (importKV.GotoNextKey(false));
importKV.GoBack();

delete importKV;
}

// TODO add a forward that allows other plugins to hook registered attribute names and
Expand Down
2 changes: 1 addition & 1 deletion third_party/include_submodules/stocksoup
Submodule stocksoup updated 1 files
+100 −2 files.inc

0 comments on commit 06cfb87

Please sign in to comment.