Author Topic: Find all files in folder in an addon, and execute them  (Read 1397 times)

[this has been asked on Blockland Content Creators (BCC), and I figured forwarding that here would aid in response.]

So I'm working on an addon that has module loading for the sake of organization. Based on the topic title you should know where I'm going with this.
To try to make this work, I did my searching and ended up finding a script by Ahead on GitHub that loads files in a "modules folder" with each module inside their own folder, similarly to loadClientAddons();, except the code is different and unique:
Code: [Select]
//------------------------------------
// Ahead Client Tools
// Module loading system
// by Ahead (ID: 33159)
//------------------------------------

echo("Ahead Client Tools | Loading main functions");
function loadAheadModules() //function for finding & loading modules; basically it's just loadClientAddOns
{
%dir = "Add-Ons/Client_Ahead/Modules/*/module.cs";
%fileCount = getFileCount(%dir);
%filename = findFirstFile(%dir);
%dirCount = 0;
while(%filename !$= "")
{
%path = filePath(%filename);
%dirName = getSubStr(%path, strlen("Add-Ons/Client_Ahead/Modules/"), strlen(%path) - strlen("Add-Ons/Client_Ahead/Modules/"));
%dirNameList[%dirCount] = %dirName;
%dirCount = %dirCount + 1.0;
%filename = findNextFile(%dir);
}
%i = 0;
while(%i < %dirCount)
{
%dirName = %dirNameList[%i];
echo("Ahead Client Tools | Client checking module: " @ %dirName);
%name = %dirName;
echo("Ahead Client Tools | Loading module: " @ %dirName);
exec("Add-Ons/Client_Ahead/Modules/" @ %dirName @ "/module.cs");
%i = %i + 1.0;
}
}
echo("Ahead Client Tools | Looking for modules");
loadAheadModules();
echo("Ahead Client Tools | Done!");
However, I did my looking, testing, etcetera and found that, of course, it wouldn't work (after editing the file paths, of course), so I figured I can reach out to the forums/BCC to get some help.
I'm not using exec(); because a user would have to extract the base server.cs file, add a line to the code, and replace the original file every time the user adds a module, so instead, by making all the files in a single folder be executed when the initial addon loads, it would make things easier for intrigued users in adding functions to the addon.

All help is appreciated, and I hope you understand what I'm looking for. Thanks.
(Yes, I will elaborate more on specific aspects if needed.)
-----
Update 1: code block received via BCC, will change this as testing is done
« Last Edit: December 09, 2018, 11:07:05 AM by Rocket Launcher »

So you want to execute Add-Ons/Client_Ahead/Modules/FOLDERS/module.cs?

Why do you go through the list of files to create a list and then go through the list and execute the files? Why not just execute on the first go around?
Code: [Select]
function loadAheadModules()
{
%search = "Add-Ons/Client_Ahead/Modules/*/module.cs";

for(%file = findFirstFile(%search); isFile(%file); %file = findNextFile(%search))
{
exec(%file);
}
}

loadAheadModules();

So you want to execute Add-Ons/Client_Ahead/Modules/FOLDERS/module.cs?

Why do you go through the list of files to create a list and then go through the list and execute the files? Why not just execute on the first go around?
Code: [Select]
function loadAheadModules()
{
%search = "Add-Ons/Client_Ahead/Modules/*/module.cs";

for(%file = findFirstFile(%search); isFile(%file); %file = findNextFile(%search))
{
exec(%file);
}
}

loadAheadModules(); you
I only put the block to demonstrate the code I found so people reading weren't forced to click a link to view what I found. I want to find all files in the same folder (specifically all files with the .cs extension) and load them from there
McTwist posted a function in BCC as I mentioned in the update from the edit I made that accomplished this, but since I'm currently away from my computer I can't update it. here's the function they posted:
Code: [Select]
%pattern = "Add-Ons/Server_ExampleAddOn/test/*.cs";
for (%file = findFirstFile(%pattern); %file !$= ""; %file = findNextFile(%pattern))
{
    exec(%file);
}

So can't you just do
Code: [Select]
%search = "Add-Ons/Client_Ahead/Folder/*.cs";

for(%file = findFirstFile(%search); isFile(%file); %file = findNextFile(%search))
{
exec(%file);
}

ah...didn't realize I could do that. I'll be taking a stab at implementing that and update the thread as stuff comes up.

Looks like the function Jes00 posted works. After some tweaking everything loaded like a charm. Locking topic for others to refer to if they have a similar request.

(thanks jes00 via BLF and McTwist via BCC for helping)