/// <summary> /// Save the current state of this object components. /// </summary> public void SaveComponentValues() { foreach (ObjectComponent component in this.components) { try { #if WINDOWS List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetProperties()); #elif WINRT List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetRuntimeProperties()); #endif foreach (PropertyInfo propInfo in props) { PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); //if(propInfo.GetValue(component, null).GetType().IsSerializable) componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } } catch (Exception ex) { Console.WriteLine("SaveComponentValues(): " + ex.Message); } } foreach (GameObject gameObject in children) { gameObject.SaveComponentValues(); } }
/// <summary> /// Adds a component to this object. /// </summary> /// <param name="component"></param> public bool AddComponent(ObjectComponent component) { #if WINDOWS if (!CheckAttributes(component)) { return(false); } // Check existing components attributes: foreach (var cmp in components) { System.Reflection.MemberInfo info = cmp.GetType(); object[] attributes = info.GetCustomAttributes(true); for (int i = 0; i < attributes.Length; i++) { if (attributes[i] is Unique) { if ((attributes[i] as Unique).Options == Unique.UniqueOptions.Explicit) { if (component.GetType() == cmp.GetType()) { return(false); } } else { if (component.GetType().IsAssignableFrom(cmp.GetType()) || cmp.GetType().IsAssignableFrom(component.GetType())) { return(false); } var baseA = component.GetType().BaseType; var baseB = cmp.GetType().BaseType; while (true) { if (baseA == typeof(ExtendedObjectComponent) || baseB == typeof(ExtendedObjectComponent) || baseA == typeof(ObjectComponent) || baseB == typeof(ObjectComponent)) { break; } else if (baseA == baseB) { return(false); } else if (baseA.IsAssignableFrom(baseB) || baseB.IsAssignableFrom(baseA)) { return(false); } baseA = baseA.BaseType; baseB = baseB.BaseType; } } } } } #endif // This component is already assigned? if (!this.componentValues.ContainsKey(component.GetType().FullName)) { this.componentValues[component.GetType().FullName] = new Dictionary <PropertyLabel, object>(new PropertyLabel.EqualityComparer()); } else { // Component already added, nothing to do here, return. return(false); } component.Transform = this.transform; component.Name = component.GetType().Name; this.components.Add(component); this.componentReferences.Add(component.GetType().FullName); // Get through all the properties in the component and assign them #if WINDOWS List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetProperties()); #elif WINRT List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetRuntimeProperties()); #endif foreach (PropertyInfo propInfo in props) { PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } if (!SceneManager.IsEditor) { component.Initialize(); } else if (SceneManager.IsEditor && component is ExtendedObjectComponent) { component.Initialize(); } return(true); }
private void LoadComponentValues(ObjectComponent component) { // The component is assigned? if (!componentValues.ContainsKey(component.GetType().FullName)) { return; } #if WINDOWS List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetProperties()); //foreach (PropertyInfo pinfo in props) //{ // Console.WriteLine("c:" + component.Transform.gameObject + " b: " + pinfo.Name); //} #elif WINRT List <PropertyInfo> props = new List <PropertyInfo>(component.GetType().GetRuntimeProperties()); //foreach (PropertyInfo pinfo in props) //{ // Debug.WriteLine("cc: " + component.GetType().Name + " c:" + component.Transform.gameObject + " b: " + pinfo.Name); // Debug.WriteLine("VALUE: " + pinfo.GetValue(component, null)); //} #endif foreach (PropertyInfo propInfo in props) { try { #if WINRT bool found = false; foreach (var item in componentValues[component.GetType().FullName]) { if (item.Key.Name == propInfo.Name && item.Key.TypeName == propInfo.PropertyType.FullName) { propInfo.SetValue(component, componentValues[component.GetType().FullName][item.Key], null); found = true; break; } } if (!found) { PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } #elif WINDOWS // dummy label PropertyLabel label = new PropertyLabel(propInfo.PropertyType.FullName, propInfo.Name); // There is a place to store the component value? if (!componentValues[component.GetType().FullName].ContainsKey(label)) { this.componentValues[component.GetType().FullName][label] = propInfo.GetValue(component, null); } else { propInfo.SetValue(component, componentValues[component.GetType().FullName][label], null); } #endif } catch (Exception ex) { Debug.WriteLine("Property not loaded: " + ex.ToString()); } } }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Equals(PropertyLabel other) { return(this.typeName.Equals(other.typeName) && this.name.Equals(other.name)); }