#FolderCrawler

1 messages · Page 1 of 1 (latest)

late flume
#

https://github.com/HannulaTero/FolderCrawler
https://terohannula.itch.io/foldercrawler

This asset can be used to crawl out larger folder/file-structures without making the game to freeze up - mainly if structures gotta visit lot of folders inside folders. This returns structure made out of Folder and File -constructs, Folder containing arrays for Folder and File contained within.

Technically, if folder just has lot of items on same level, it could still freeze up, as I decided to not split up file_find_* calls as they ue global state; which could have ill effects if game logic elsewhere happens to use it too. Though most likely this is not the case, I may make a revision to split it up too, or add that as option. So in current state, this is good when looking folder structures, which branches a lot, not when they are wide.

Had this idea yesterday, when I was pondering getting all items within GM project, and then also wanted to split it up to several frames.

latent valve
#

So if I needed to find all files of a certain file type withing a folder or sub folder this could do that? I've been looking for a solution to do that.

late flume
#

This tries to find out all folders, subfolders and files within the given path, and returns structure representing these. Haven't put filter options there, I guess that could be a parameter.

But for now, after finding everything, you could iterate though the resulting structure, and find all those related to your criteria.

latent valve
#

I'll play around with it

late flume
#

If you wish, you could add a new array to the crawler, and in __Crawl function you can put a bit of code to push files to this array whenever they meet criteria.

#

Though today I most likely do some updates, such as adding "unsafe" crawl (split file_find_* loop over several frames).
I'll check whether I just make structure construction into function, so it can be changed more easily

late flume
# latent valve So if I needed to find all files of a certain file type withing a folder or sub ...

Okay, made new version, which allows to do this.
In this example it will push all files which fit the criteria into the same array with custom action.
Though I am not happy how the function call looks right now.

// Stores all found images.
self.images = [ ];


// Dispatch the crawl.
folder_crawl(_path, function(_status, _result, _crawler)
{
  // This does crawl out full structure anyways.
  // -> Here it is stored.
  self.structure = _result;
}, {
  // Using optional parameters.
  // This allows further splitting to avoid stutters. 
  // -> But have to be more careful with file_find_* elsewhere.
  unsafe : true,

  // Custom action done with each item. 
  action : method(self, function(_item)
  {
    // Whether folder or file.
    if (_item.type != "file")
    {
      return;
    }
    
    // Check for the extension.
    var _ext = filename_ext(_item.name);
    if (_ext != ".png")
    {
      return;
    }
    
    // Push into array of images.
    array_push(self.images, _item);
  })
});

latent valve
#

I could hug you. This is awesome

late flume
#

Making v1.1.0 update, which would allow something like this instead:

handle = folder_crawl(path , {
  unsafe : true,
  context : self.items,
  file : function(_file, _context)
  {
    var _ext = filename_ext(_file.name);
    if (_ext == ".png")
    {
      array_push(_context, _file);
    }
  },
  callback : function(_crawler, _context)
  {
    self.timeTaken  = (get_timer() - self.timeBegin);
    self.foundCount = _crawler.DebugCount();
    self.status     = _crawler.GetStatus();
    self.structure  = _crawler.GetRoot();
    self.json       = json_stringify(self.structure, true);
  }
});

Also directory_exists seems pretty slow, and making two file_find_*seems to be faster.

edit. I have made the update.