private static void ApplyInheritance(BlueprintHeader Header, List <ComponentBlueprint> Components) { // Go through each inheritance (in order) and add missing components / properties. foreach (var Inheritance in Header.Inherits) { EntityBlueprint InheritedBlueprint = EntityBlueprint.GetBlueprint(Inheritance); if (InheritedBlueprint == null) { throw new KeyNotFoundException("Unable to find inherited blueprint '" + Inheritance + "'. Please note inherited blueprints must appear above this blueprint in the blueprint specification."); } foreach (var Component in InheritedBlueprint.Components) { var ExistingComponent = Components.FirstOrDefault(c => c.Name.Equals(Component.Name, StringComparison.InvariantCultureIgnoreCase)); if (ExistingComponent == null) { // Just copy over the complete component. Components.Add(Component); } else { // Otherwise, we already that component, so copy over properties we're missing. foreach (var Property in Component.Properties) { var ExistingProperty = ExistingComponent.Properties.FirstOrDefault(c => c.PropertyName.Equals(Property.PropertyName, StringComparison.InvariantCultureIgnoreCase)); if (ExistingProperty == null) { // Don't have the property, so add it. Otherwise, keep our own value. ExistingComponent.Properties.Add(Property); } } } } } }
/// <summary> /// Parses a blueprint from the specified stream, registering all EntityBlueprints contained by the Stream (and replacing any existing data for them). /// The stream is not closed upon completion, and may have multiple EntityBlueprints defined within it. /// </summary> public static void ParseBlueprint(string Contents) { LineReader Reader = new LineReader(Contents.Split(new char[] { '\r', '\n' })); while (GetNextScope(Reader) != LineScope.EndOfFile) { // Parse the Entity header, such as name and what it inherits. BlueprintHeader Header = ParseHeader(Reader); // Parse all components, including properties. List <ComponentBlueprint> Components = new List <ComponentBlueprint>(); while (GetNextScope(Reader) == LineScope.Component) { var CurrentComponent = ParseComponentHeader(Reader); List <ComponentProperty> Properties = new List <ComponentProperty>(); while (GetNextScope(Reader) == LineScope.Property) { var Property = ParseProperty(CurrentComponent, Reader); Properties.Add(Property); } Type ComponentType = ResolveType(CurrentComponent.Type); var ComponentBlueprint = new ComponentBlueprint(ComponentType, CurrentComponent.Name, Properties); Components.Add(ComponentBlueprint); } ApplyInheritance(Header, Components); var Entity = EntityBlueprint.CreateBlueprint(Header.Name, Components); } }
/// <summary> /// Creates a new EntityBlueprint with the specified name and components, replacing any existing EntityBlueprint with that name. /// </summary> public static EntityBlueprint CreateBlueprint(string Name, IEnumerable <ComponentBlueprint> Components) { var Result = new EntityBlueprint() { _Name = Name, _Components = Components.ToList() }; lock (AllBlueprints) { AllBlueprints[Result.Name] = Result; } return(Result); }
/// <summary> /// Creates a new EntityBlueprint with the specified name and components, replacing any existing EntityBlueprint with that name. /// </summary> public static EntityBlueprint CreateBlueprint(string Name, IEnumerable<ComponentBlueprint> Components) { var Result = new EntityBlueprint() { _Name = Name, _Components = Components.ToList() }; lock(AllBlueprints) { AllBlueprints[Result.Name] = Result; } return Result; }