Custom Parsers
Quantum Console comes with a variety of parsers for the commonly used data types built in, however you may want to use an argument that isn't currently supported; for this you can write a custom parser to do the job
Any class implementing IQcParser
will automatically be injected into the QuantumParser
and used when parsing data
Warning
Since parsers are injected they may be stripped out when building with managed stripping. To avoid this use see bytecode stripping
To make the job of writing parsers simpler, Quantum Console comes with several base types to help:
BasicQcParser<T>
BasicCachedQcParser<T>
GenericQcParser
GenericCachedQcParser
PolymorphicQcParser<T>
PolymorphicCachedQcParser<T>
MassGenericParser
Click each item to see its API reference for more
Tip
The Cached
variant of each parser base caches the output for a given input, and thus can yield better performance.
This caching behaviour may be undesirable in certain cases (specifically reference types) so use with caution.
Examples
BasicQcParser<T>
using QFSW.QC;
public class BoolParser : BasicQcParser<bool>
{
public override bool Parse(string value)
{
value = value.ToLower().Trim();
switch (value)
{
case "true": return true;
case "on": return true;
case "1": return true;
case "yes": return true;
case "false": return false;
case "off": return false;
case "0": return false;
case "no": return false;
default: throw new ParserInputException($"Cannot parse '{value}' to a bool.");
}
}
}
PolymorphicQcParser<T>
Tip
Each base parser provides a ParseRecursive
method; this allows you to use the full QuantumParser
recursively in your implementation.
A useful case of this is parsing the contents of a collection.
using QFSW.QC;
using QFSW.QC.Utilities;
using System;
public class ComponentParser : PolymorphicQcParser<Component>
{
public override Component Parse(string value, Type type)
{
GameObject obj = ParseRecursive<GameObject>(value);
Component objComponent = obj.GetComponent(type);
if (!objComponent)
{
throw new ParserInputException($"No component on the object '{value}' of type {type.GetDisplayName()} existed.");
}
return objComponent;
}
}