This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Blitzsy's profile picture

Supports api

Started by Blitzsy, 06 January 2016 - 03:21 PM
Blitzsy #1
Posted 06 January 2016 - 04:21 PM
I designed this framework to ensure that the programs that ran on the computer had the required peripherals to successfully run without any problems. The idea behind this api is that you supply it with a table of features for the api to check to see if the computer running the program has peripherals/features to successfully run it. The api also allows you to add custom feature checks incase you need to anything else custom, but it should be able to fallback on the

code:
http://pastebin.com/Emb2Etrg

api:

Supports.features(requiredFeatures)
	Description:	
		When supplied with a list of required features it will check to see if the computer has them.
	Parameters:
		requiredFeatures - A table consisting of strings of the features required.
	Returns:
		true - All features supplied are found running on the computer.
		false - Some features were not found on the computer.

Supports.displayMissingRequirements(missingRequiredFeatures)
	Description:
		When supplied whith a list of missing required features, It will display them in a decently formmated message.
	Parameters:
		missingRequiredFeatures - A table consisting of all the required features.
	Returns:
		Nothing.

Supports.registerCheckedFeatured(checkedFeatureName, checkedFeatureFunc)
	Description:
		When supplied with a name of a feature and a function to check the feature state it will validate that that feature is properly setup for the computer.
	Parameters:
		checkedFeatureName - The name of the feature to register.
		checkedFeatureFunc - A function which returns true or false depending on the features requirements.
	Returns:
		true - The feature was successfully registered.
		false - The feature failed to be registered.
	
Supports.unregisterCheckedFeature(checkedFeatureName)
	Description:
		When supplied with a name of a existing feature being used for validation, It will remove it validation.
	Parameters:
		checkedFeatureName - The name of the feature to remove.
	Returns
		true - The feature was successfully unregistered.
		false - The feature failed to be unregistered.

Features that can be checked for are the following:
  • turtle
  • melee
  • mining
  • digging
  • felling
  • farming
  • crafty
  • wireless
  • wired
  • advanced
  • monitor
  • modem
  • printer
  • drive


sample code:

os.loadAPI("Supports")

local foundRequiredFeatures, missingRequiredFeatures = Supports.features({"advanced", "turtle", "wireless"})

if not foundRequiredFeatures then
	Supports.displayMissingRequirements(missingRequiredFeatures)
	
	return false
end

print("This program has all the requirements to run.")
Edited on 06 January 2016 - 11:11 PM
LDDestroier #2
Posted 06 January 2016 - 06:22 PM
Could this not be done with this?

if not (advanced and turtle and wireless) then
    error("error'd!")
end

I suppose a way to make this more useful is to list all features that are not present, after false.


Example:

Supports.features({"advanced", "turtle", "wireless"}) --returns false, {"turtle","wireless"}
Exerro #3
Posted 06 January 2016 - 10:00 PM
If you look at the code, it does more than those simple checks. I agree that returning the things it failed on would be nice though, or at least the first check it failed. You could even add in some way of getting a nicely formatted error message back for all the lazy people out there.
Blitzsy #4
Posted 06 January 2016 - 11:49 PM
Could this not be done with this?

if not (advanced and turtle and wireless) then
	error("error'd!")
end

I suppose a way to make this more useful is to list all features that are not present, after false.


Example:

Supports.features({"advanced", "turtle", "wireless"}) --returns false, {"turtle","wireless"}

Thanks for the suggestion, I'll definitely will be adding that. There are a few simple ones, however I did also add more complex checks like if a turtle has a certain tool or if a certain peripheral is present.