Skip to content

Instantly share code, notes, and snippets.

@koster
Last active October 29, 2024 21:55
Show Gist options
  • Save koster/5b4ad07e07ebaed5728e377db06c69f0 to your computer and use it in GitHub Desktop.
Save koster/5b4ad07e07ebaed5728e377db06c69f0 to your computer and use it in GitHub Desktop.
A funky description system
// this handles strings like
// Ignores %TagDiceModifierIgnoresArmor::value% armor
// and stuff, useful for complex descriptions
[Serializable]
public class TagDescription : EntityComponentDefinition
{
public string loc;
public string ParseTagDescription(CMSEntity entity)
{
var regex = new Regex(@"%(\w+)::(\w+)%");
return regex.Replace(loc, match =>
{
var componentName = match.Groups[1].Value;
var propertyName = match.Groups[2].Value;
var componentType = entity.components.FirstOrDefault(c => c?.GetType()?.Name == componentName)?.GetType();
if (componentType != null)
{
var property = componentType.GetField(propertyName, BindingFlags.Public | BindingFlags.Instance);
var component = entity.components.Find(c => c.GetType() == componentType);
if (property != null && component != null)
{
var value = property.GetValue(component)?.ToString();
return value ?? match.Value;
}
}
return match.Value;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment