Space Engineers

Space Engineers

EasyAPI
EasyEXT By examples (02): Set/Get block properties values thru strings
Here we will extend the EasyBlock class to add one method to get a block property value and another one to set a block property value. Because we don't really want to be concerned by casting etc the property value type will always be String.

There is also a little helper method to return the type name of the selected property (or return "Unknow" or fail if not found) and we have added the EasyEXTHelpersColors (see examples (01)) class to be able to set a color value from a string.

At this time only types: Boolean, Single and Color are supported. StringBuilder is bugged and there is no access to the other ones.

As always paste the code that will follow at the very end of the EasyAPI script.

LKS
Laatst bewerkt door LukeStrike; 24 jun 2016 om 21:09
< >
1-6 van 6 reacties weergegeven

} //Trick: close the Program class so we can define Extensions classes
public static class EasyEXTProperties
{
// Returns a block propertyid typename, if found, "Unknown" or exeption if not
public static Program.EasyBlock GetPropertyTypeName(this Program.EasyBlock easyblock, String propertyid, ref String propertytypename, bool throwit = false)
{
List<ITerminalProperty> properties = easyblock.GetProperties();

// Loop on properties to find the required one
for(int i = 0; i < properties.Count; i++)
{
if(properties[i].Id == propertyid)
{
propertytypename = properties[i].TypeName;
return easyblock;
}
}

// Property not found
propertytypename = "Unknown";
if(throwit)
{
throw new Exception(easyblock.Type() + " \"" + easyblock.Name() + "\" : Property \"" + propertyid + "\" " + propertytypename + ".");
}
return easyblock;
}


// Returns a block propertyid value as a string, if found, "Unknown" or exeption if not
// Other values are "Bugged" and "Unsupported" until Keen fixes the problems with some types
public static Program.EasyBlock GetPropertyStringValue(this Program.EasyBlock easyblock, String propertyid, ref String stringvalue, bool throwit = false)
{
// Get the TypeName of the required property
String typename = "";
easyblock.GetPropertyTypeName(propertyid, ref typename, throwit);

switch(typename)
{
case "Unknown": // Will return "Unknown" if the propertyid does not exists and throwit was set to false
stringvalue = "Unknown";
break;

case "Boolean":
stringvalue = Convert.ToString(easyblock.GetProperty<Boolean>(propertyid));
break;

case "Single":
stringvalue = Convert.ToString(easyblock.GetProperty<Single>(propertyid));
break;

case "Color":
stringvalue = Convert.ToString(easyblock.GetProperty<Color>(propertyid));
break;

case "StringBuilder": // WARNING: at this time StringBuilder is bugged (no access) ... Wait for Keen (as always)
//stringvalue = Convert.ToString(easyblock.GetProperty<StringBuilder>(propertyid));
stringvalue = "Bugged";
break;

default: // WARNING: at this time some alien types are not readable/writable. Improve them when possible
stringvalue = "Unsupported";
break;
}

return easyblock;
}


// Sets a block propertyid value from a string, if found, stringresult "Unknown" or exeption if not
// Other stringresult values are "Bugged" and "Unsupported" until Keen fixes the problems with some types
// And stringresult will be set to "Done" if the property was set as asked
public static Program.EasyBlock SetPropertyStringValue(this Program.EasyBlock easyblock, String propertyid, String stringvalue, ref String stringresult, bool throwit = false)
{
// Get the TypeName of the required property
String typename = "";
easyblock.GetPropertyTypeName(propertyid, ref typename, throwit);

switch(typename)
{
case "Unknown": // Will return "Unknown" if the propertyid does not exists and throwit was set to false
stringresult = "Unknown";
break;

case "Boolean":
easyblock.SetProperty(propertyid, Boolean.Parse(stringvalue));
stringresult = "Done";
break;

case "Single":
easyblock.SetProperty(propertyid, Single.Parse(stringvalue));
stringresult = "Done";
break;

case "Color":
easyblock.SetProperty(propertyid, stringvalue.ToColor());
stringresult = "Done";
break;

case "StringBuilder": //WARNING: at this time StringBuilder is bugged (no access) ... Wait for Keen (as always)
stringresult = "Bugged";
break;

default: //WARNING: at this time some alien types are not readable/writable. Improve them when possible
stringresult = "Unsupported";
break;
}

return easyblock;
}
//Trick: no closing brace here, SE will close it automaticaly

} //Trick: close the Program class so we can define Extensions classes
public static class EasyEXTHelpersColors // Helpers for KeenAPI/EasyAPI. Should be included in some of EasyEXT(ensions)
{
// Returns a Color object builded from a string. Warning: in the SE format so "{R:<> G:<> B:<> A:<>}"
public static Color ToColor(this String colorstring)
{
var matches = System.Text.RegularExpressions.Regex.Matches(colorstring, @"\d+");
Color color = new Color();

color.R = Byte.Parse(matches[0].Value);
color.G = Byte.Parse(matches[1].Value);
color.B = Byte.Parse(matches[2].Value);
color.A = Byte.Parse(matches[3].Value);

return color;
}
//Trick: no closing brace here, SE will close it automaticaly
Now few examples how to use it:


public class Example : EasyAPI
{
public Example(IMyGridTerminalSystem grid, IMyProgrammableBlock me, Action<string> echo, TimeSpan elapsedTime) : base(grid, me, echo, elapsedTime)
{
// Start your code here
String output = "";
String typenamegravity = "";
String typenameonoff = "";
Blocks.Named("Grav [AP]").GetBlock(0).GetPropertyTypeName("Gravity", ref typenamegravity).GetPropertyTypeName("OnOff", ref typenameonoff);
output += typenamegravity + "\n" + typenameonoff + "\n";
Echo(output);

... As you can see the fact we get the property type name (same for it's value, see below) by ref allows us to combine multiple requests for the same block in one line/pass. Here we get the type name of the properties "Gravity" and "OnOff" in two separate strings ...
(... code cont)

output = "";
Blocks.Named("Grav [AP]").GetBlock(0).GetPropertyStringValue("OnOff", ref output);
Echo(output);

Here we handle a Boolean property and just display its string value "True" or "False"


output = "";
Blocks.Named("Grav [AP]").GetBlock(0).GetPropertyStringValue("Gravity", ref output);
Single gravity = Single.Parse(output);
if(gravity == 0)
{
output += "\nGravity OFF";
} else {
output += "\nGravity ON";
}
Echo(output);

Here we handle a Single property. but we convert the returned string value to a real Single so we can use it for computing, conditions etc
(... code cont)

output = "";
Blocks.Named("LCD [AP] (O1)").GetBlock(0).GetPropertyStringValue("FontColor", ref output);
Color color = output.ToColor();
Echo(Convert.ToString(color));
Echo("Repeat:" + output);

Here we get the font color of an LCD, convert it to a real Color, then into a string again to be sure that they produce both the same results ... nothing tricky ...
(... code cont)

output = "";
Blocks.Named("Cockpit [AP]").GetBlock(0).SetPropertyStringValue("DampenersOverride", "False", ref output);
Echo("Set:" + output);

Here is a simple example how to set a property value. In this case we shut off the dampeners of a cockpit. As you can see we have a "ref output" in parameters, that allows us to know if the action was made (returns "Done") or not.
(... code cont)

Every(1 * Seconds, delegate()
{
output = "";
Blocks.Named("LCD [AP] (O1)").GetBlock(0).GetPropertyStringValue("FontColor", ref output);
Blocks.Named("LCD [AP] (02)").GetBlock(0).SetPropertyStringValue("FontColor", output, ref output);
Echo("Color:" + output);
});

In this last example we use the Every loop to adjust the font color of the second LCD to the font color of the first one. So whenever I change it, all my LCDs will have the same font color (I do not have to set them up one by one.

Of course you can also extend the EasyBlocks class the same way (what I will probably do) to apply those methods to a group of blocks, get summarized informations etc ...

So, more to come ...
LKS
< >
1-6 van 6 reacties weergegeven
Per pagina: 1530 50