Last active
October 29, 2024 21:55
-
-
Save koster/5b4ad07e07ebaed5728e377db06c69f0 to your computer and use it in GitHub Desktop.
A funky description system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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