Author Topic: Best Practices for Handling Add-on Dependencies?  (Read 1673 times)

Say I have two add-ons, Script_Test1 and Script_Test2. The server.cs in Script_Test1 has the code

Code: [Select]
if(forceRequiredAddOn("Script_NotAnAddOn") == $Error::AddOn_NotFound) {
error("Script_Test1 will not load");
$test1 = false;
return;
}
$test1 = true;

and the server.cs in Script_Test1 has the code
Code: [Select]
if(forceRequiredAddOn("Script_Test1") == $Error::AddOn_NotFound) {
error("Script_Test2 will not load");
$test2 = false;
return;
}
$test2 = true;

To be explicitly clear, Script_NotAnAddOn When I enabled them both and start a server, $test1 is given the value 0, as expected. However, $test2 is given the value 1, which is not satisfactory. How do I ensure Script_Test2 cannot be loaded when Script_Test1 cannot be loaded? Is there a standard way to handle this, or would it require some sort of support add-on?

Perhaps there's some way to force forceRequiredAddOn() to return $Error::AddOn_NotFound?

EDIT: made title more pertinent
« Last Edit: January 04, 2018, 10:22:36 AM by Platypi »

The addons load in backward alphabet order, unfortunately this is a lot worse on Linux. Script_Test2 will load before Script_Test1. Linux is entirely random.

Create a main.cs with the main code of the mod, execute it when you have the right if statement checks, otherwise print out stuff. You could manually load the other addon, but why do you need to do all of this? Seems like a headache.
« Last Edit: January 04, 2018, 09:24:43 AM by Kyuande »

The addons load in backward alphabet order, unfortunately this is a lot worse on Linux. Script_Test2 will load before Script_Test1. Linux is entirely random.
I'm just trying to show the method I'm currently using to handle dependencies. Ideally, I would like to develop a method for handling dependencies that works regardless of the order files are loaded in.

Create a main.cs with the main code of the mod, execute it when you have the right if statement checks, otherwise print out stuff. You could manually load the other addon, but why do you need to do all of this? Seems like a headache.
Just trying to establish the best practice for checking add-on dependencies. My main concern is how to make sure an add-on that is not mine has loaded in correctly. I can add all the checks I want for dependencies I create. But not for dependencies from other creators.
« Last Edit: January 04, 2018, 10:21:11 AM by Platypi »

There is a function to tell if an addon is loaded but I currently cannot get it, I will provide the function later

In Script_Test2, simply require Script_Test1, along with the same things that Script_Test1 requires.


In Script_Test2, simply require Script_Test1, along with the same things that Script_Test1 requires.
I suppose that would work for most add-ons.

Check if $Test1 is true.
Not every add-on has some sort of "$Test1" flag, though. Even if they did, I'm hoping for a more standardized method.

Example:

Code: [Select]
//This is stupid do this since ForceRequiredAddOn WILL disconnect you if you are hosting non-dedicated for a missing/disabled add-on if using a non-custom gamemode
//This is the most right way to exec required add-ons

//Please alert me if there is a better way to do this
$doReturn = 0;
if($GameModeArg $= "Add-Ons/GameMode_Custom/gamemode.txt" || $GameModeArg $= "")
{
$error = ForceRequiredAddOn("Support_NewHealth");
if($error == $Error::AddOn_NotFound)
{
warn("ERROR: GameMode_Randomizer - required add-on Support_NewHealth not found");
return;
}

//--------------------------------------

$error = ForceRequiredAddOn("Support_FindItemByName");
if($error == $Error::AddOn_NotFound)
{
warn("ERROR: GameMode_Randomizer - required add-on Support_FindItemByName not found");
return;
}

//--------------------------------------

$error = ForceRequiredAddOn("Support_SpeedFactor");
if($error == $Error::AddOn_NotFound)
{
warn("ERROR: GameMode_Randomizer - required add-on Support_SpeedFactor not found");
return;
}

//--------------------------------------

$error = ForceRequiredAddOn("Support_CodeLibrary");
if($error == $Error::AddOn_NotFound)
{
warn("ERROR: GameMode_Randomizer - required add-on Support_CodeLibrary not found");
return;
}
}
else
{
//I am hoping other add-ons that require stuff would use this kind of method
//I looked through stuff and didn't see an easier way for this but oh well
if($GameMode::LastAddOnCount $= "" || $GameMode::AddOnCount != $GameMode::LastAddOnCount)
{
$GameMode::LastAddOnCount = $GameMode::AddOnCount;
deleteVariables("$GameMode::NameAddOn*");
for($FC = 0; $FC < $GameMode::AddOnCount; $FC++)
{
$addonName = $GameMode::AddOn[$FC];
$GameMode::NameAddOn[$addonName] = 1;
}
}

if(!isFile("Add-Ons/Support_NewHealth/server.cs") || !$GameMode::NameAddOn["Support_NewHealth"])
{
$doReturn = 1;
warn("ERROR: GameMode_Randomizer - required add-on Support_NewHealth not found");
}

if(!isFile("Add-Ons/Support_FindItemByName/server.cs") || !$GameMode::NameAddOn["Support_FindItemByName"])
{
$doReturn = 1;
warn("ERROR: GameMode_Randomizer - required add-on Support_FindItemByName not found");
}

if(!isFile("Add-Ons/Support_SpeedFactor/server.cs") || !$GameMode::NameAddOn["Support_SpeedFactor"])
{
$doReturn = 1;
warn("ERROR: GameMode_Randomizer - required add-on Support_SpeedFactor not found");
}

if(!isFile("Add-Ons/Support_CodeLibrary/server.cs") || !$GameMode::NameAddOn["Support_CodeLibrary"])
{
$doReturn = 1;
warn("ERROR: GameMode_Randomizer - required add-on Support_CodeLibrary not found");
}

//I did this so people can see ALL the required add-ons if there are more than 1
if($doReturn)
return;
}

exec("./main.cs");