private void SaveComponent(Component comp) { if (handledComponents.ContainsKey(comp.Id) == false) { handledComponents.Add(comp.Id, comp); ComponentXML xmlComponent = new ComponentXML(); if (comp is Script) { xmlComponent.id = comp.Id; xmlComponent.type = typeof(Script).FullName; SaveValueComponent(xmlComponent, new FieldValue("__ScriptName__", typeof(String), comp.GetType().Name)); } else { xmlComponent.id = comp.Id; xmlComponent.type = comp.GetType().FullName; } FieldValue[] valores = comp.GetValores(); foreach (FieldValue valor in valores) { SaveValueComponent(xmlComponent, valor); } xmlComponents.Add(xmlComponent); } }
private void LoadComponent(ComponentXML xmlComponent) { Type tipoComponente; if (xmlComponent.type == typeof(Script).FullName) { FieldValueXML fvScriptName = xmlComponent.FindAttribute("__ScriptName__"); if (fvScriptName != null && fvScriptName.value != null) { ScriptResource scriptRes = (ScriptResource)Resource.FindResource(typeof(ScriptResource), (String)fvScriptName.ValueAs(typeof(String))); if (scriptRes != null) { tipoComponente = scriptRes.ScriptType; } else { tipoComponente = null; } } else { tipoComponente = null; } } else { tipoComponente = Type.GetType(xmlComponent.type); } if (tipoComponente != null) { Component comp = (Component)tipoComponente.GetConstructor(Type.EmptyTypes).Invoke(null); comp.ForceId(xmlComponent.id); handledComponents.Add(comp.Id, comp); AccesorValor[] accesoresValores = comp.GetAccesoresValores(); object val; foreach (AccesorValor accesor in accesoresValores) { if (LoadValueComponent(xmlComponent, accesor, out val)) { accesor.Value = val; } } } }
private void SaveValueComponent(ComponentXML xmlComponent, FieldValue valor) { if (valor.value != null) { if (typeof(Component).IsAssignableFrom(valor.type)) { Component compVal = (Component)valor.value; if (compVal != Entity.Root) { SaveComponent(compVal); } if (compVal != Entity.Root) { xmlComponent.attributes.Add(new FieldValueXML(valor.name, compVal.Id)); } else { xmlComponent.attributes.Add(new FieldValueXML(valor.name, uint.MaxValue)); } } else if (typeof(Resource).IsAssignableFrom(valor.type)) { Resource resVal = (Resource)valor.value; xmlComponent.attributes.Add(new FieldValueXML(valor.name, resVal.Name)); } else if (valor.type.IsEnum) { xmlComponent.attributes.Add(new FieldValueXML(valor.name, valor.value.ToString())); } else if (FieldValueXML.CanSerialize(valor.value)) { xmlComponent.attributes.Add(new FieldValueXML(valor.name, valor.value)); } else { throw new NotSupportedException("No se puede serializar el tipo de dato: " + valor.value.GetType()); } } else { xmlComponent.attributes.Add(new FieldValueXML(valor.name, null)); } }
private bool LoadValueComponent(ComponentXML xmlComponent, AccesorValor accesor, out object val) { FieldValueXML fv = xmlComponent.FindAttribute(accesor.ValueName); Type tipo = accesor.ValueType; if (fv != null && fv.value != null) { if (typeof(Component).IsAssignableFrom(tipo)) { uint id = (uint)fv.ValueAs(typeof(uint)); Component otComp; if (id != uint.MaxValue) { if (handledComponents.TryGetValue(id, out otComp) == false) { otComp = null; } } else { otComp = Entity.Root; } if (otComp != null && tipo.IsAssignableFrom(otComp.GetType())) { val = otComp; return(true); } } else if (typeof(Resource).IsAssignableFrom(tipo)) { String resName = (String)fv.ValueAs(typeof(string)); Resource res = Resource.FindResource(tipo, resName); if (res != null) { val = res; return(true); } else { val = null; return(false); } } else if (tipo.IsEnum) { try { val = Enum.Parse(tipo, (String)fv.ValueAs(typeof(String)), true); return(true); } catch (ArgumentException) { } } else { val = fv.ValueAs(tipo); return(true); } } val = null; return(false); }