Skip to content

Commit

Permalink
Add fileValueGenerator parameter to gumshoe when using struct mode, w…
Browse files Browse the repository at this point in the history
…hich is a function that takes the directory, file, extension, and index for the file and returns the value that should be set on the struct for that file

Add example of fileValueGenerator to test object
Add draw event to test object directing user to check the output log for the test case output
  • Loading branch information
shdwcat committed Jun 3, 2022
1 parent 81b92c7 commit 4db0d21
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
8 changes: 6 additions & 2 deletions objects/obj_test/Create_0.gml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
show_debug_message("struct version:");
show_debug_message(gumshoe("", "txt", true));
show_debug_message("array version:");
show_debug_message(gumshoe("", "txt", false));
show_debug_message("struct version:");
show_debug_message(gumshoe("", "txt", true));
show_debug_message("struct version with value generator:");
show_debug_message(gumshoe("", "txt", true, , function(directory, file, extension, index) {
return directory + file; // the full path to the file
}));
show_debug_message("end.");
3 changes: 3 additions & 0 deletions objects/obj_test/Draw_64.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// @description

draw_text(10, 10, "See output log for test cases.");
3 changes: 2 additions & 1 deletion objects/obj_test/obj_test.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions scripts/gumshoe/gumshoe.gml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ On Microsoft platforms this flag does nothing as the file system is not case sen
/// @param fileExtension
/// @param returnStruct
/// @param [forceLCNames] Force lowercase names on non-MSFT platforms, see comment(s) above for an explanation.
function gumshoe()
/// @param [structValueGenerator] when returnStruct = true, function that generates the value for the file (params: directiory, file, extension, index)
function gumshoe(_directory, _extension, _return_struct = false, _force_lcnames = true, _generator = undefined)
{
//Microsoft platforms handle wildcards and patterns slightly different than others (Linux or web based).
static _is_microsoft =
Expand All @@ -53,10 +54,9 @@ function gumshoe()
//(see comments above for more ramblings)
static _match_all_mask = _is_microsoft? "*.*" : "*";

var _directory = argument[0];
var _extension = argument[1];
var _return_struct = argument[2];
var _force_lcnames = (argument_count > 3)? argument[3] : true;
if (_return_struct == false && _generator != undefined) {
throw "Gumshoe: structValueGenerator can only be specified when returning a struct";
}

//The JS export does not support file search at all.
if (os_browser != browser_not_a_browser)
Expand Down Expand Up @@ -87,7 +87,7 @@ function gumshoe()
if (_return_struct)
{
global.__gumshoe_count = 0;
return __gumshoe_struct(_directory, _extension, _match_all_mask, _path_separator);
return __gumshoe_struct(_directory, _extension, _match_all_mask, _path_separator, _generator);
}
else
{
Expand Down Expand Up @@ -131,7 +131,7 @@ function __gumshoe_array(_directory, _extension, _result, _match_all_mask, _path
var _i = 0;
repeat(array_length(_directories))
{
__gumshoe_array(_directories[_i], _extension, _result);
__gumshoe_array(_directories[_i], _extension, _result, _match_all_mask, _path_sep);
++_i;
}

Expand All @@ -142,7 +142,8 @@ function __gumshoe_array(_directory, _extension, _result, _match_all_mask, _path
/// @param fileExtension
/// @param matchAllMask
/// @param pathSeparator
function __gumshoe_struct(_directory, _extension, _match_all_mask, _path_sep)
/// @param structValueGenerator
function __gumshoe_struct(_directory, _extension, _match_all_mask, _path_sep, _generator)
{
var _directories = [];
var _result = {};
Expand All @@ -162,7 +163,8 @@ function __gumshoe_struct(_directory, _extension, _match_all_mask, _path_sep)
else if ((_extension == ".*") || (filename_ext(_file) == _extension))
{
//Add this matching file to the output array
variable_struct_set(_result, _file, global.__gumshoe_count);
var value = _generator ? _generator(_directory, _file, _extension, global.__gumshoe_count) : global.__gumshoe_count;
variable_struct_set(_result, _file, value);
++global.__gumshoe_count;
}
}
Expand All @@ -173,9 +175,9 @@ function __gumshoe_struct(_directory, _extension, _match_all_mask, _path_sep)
var _i = 0;
repeat(array_length(_directories))
{
variable_struct_set(_result, _directories[_i], __gumshoe_struct(_directory + _directories[_i] + _path_sep, _extension));
variable_struct_set(_result, _directories[_i], __gumshoe_struct(_directory + _directories[_i] + _path_sep, _extension, _match_all_mask, _path_sep, _generator));
++_i;
}

return _result;
}
}

0 comments on commit 4db0d21

Please sign in to comment.