public static void Look <T>(ref T target, bool saveDestroyedThings, string label, params object[] ctorArgs) { if (Scribe.mode == LoadSaveMode.Saving) { Thing thing = target as Thing; if (thing != null && thing.Destroyed) { if (!saveDestroyedThings) { Log.Warning(string.Concat(new object[] { "Deep-saving destroyed thing ", thing, " with saveDestroyedThings==false. label=", label }), false); } else if (thing.Discarded) { Log.Warning(string.Concat(new object[] { "Deep-saving discarded thing ", thing, ". This mode means that the thing is no longer managed by anything in the code and should not be deep-saved anywhere. (even with saveDestroyedThings==true) , label=", label }), false); } } IExposable exposable = target as IExposable; if (target != null && exposable == null) { Log.Error(string.Concat(new object[] { "Cannot use LookDeep to save non-IExposable non-null ", label, " of type ", typeof(T) }), false); return; } if (target == null) { if (Scribe.EnterNode(label)) { try { Scribe.saver.WriteAttribute("IsNull", "True"); } finally { Scribe.ExitNode(); } } } else if (Scribe.EnterNode(label)) { try { if (target.GetType() != typeof(T) || typeof(T).IsGenericTypeDefinition) { Scribe.saver.WriteAttribute("Class", GenTypes.GetTypeNameWithoutIgnoredNamespaces(target.GetType())); } exposable.ExposeData(); } catch (OutOfMemoryException) { throw; } catch (Exception ex) { Log.Error(string.Concat(new object[] { "Exception while saving ", exposable.ToStringSafe <IExposable>(), ": ", ex }), false); } finally { Scribe.ExitNode(); } } Scribe.saver.loadIDsErrorsChecker.RegisterDeepSaved(target, label); } else if (Scribe.mode == LoadSaveMode.LoadingVars) { try { target = ScribeExtractor.SaveableFromNode <T>(Scribe.loader.curXmlParent[label], ctorArgs); } catch (Exception ex2) { Log.Error(string.Concat(new object[] { "Exception while loading ", Scribe.loader.curXmlParent[label].ToStringSafe <XmlElement>(), ": ", ex2 }), false); target = default(T); } } }
public static XElement XElementFromObject(object obj, Type expectedType, string nodeName, FieldInfo owningField = null, bool saveDefsAsRefs = false) { DefaultValueAttribute customAttribute; if (owningField != null && owningField.TryGetAttribute(out customAttribute) && customAttribute.ObjIsDefault(obj)) { return(null); } if (obj == null) { XElement xElement = new XElement(nodeName); xElement.SetAttributeValue("IsNull", "True"); return(xElement); } Type type = obj.GetType(); XElement xElement2 = new XElement(nodeName); if (IsSimpleTextType(type)) { xElement2.Add(new XText(obj.ToString())); } else if (saveDefsAsRefs && typeof(Def).IsAssignableFrom(type)) { string defName = ((Def)obj).defName; xElement2.Add(new XText(defName)); } else { if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(List <>)) { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <, >)) { Type expectedType2 = type.GetGenericArguments()[0]; Type expectedType3 = type.GetGenericArguments()[1]; IEnumerator enumerator = (obj as IEnumerable).GetEnumerator(); try { while (enumerator.MoveNext()) { object current = enumerator.Current; object value = current.GetType().GetProperty("Key").GetValue(current, null); object value2 = current.GetType().GetProperty("Value").GetValue(current, null); XElement xElement3 = new XElement("li"); xElement3.Add(XElementFromObject(value, expectedType2, "key", null, saveDefsAsRefs: true)); xElement3.Add(XElementFromObject(value2, expectedType3, "value", null, saveDefsAsRefs: true)); xElement2.Add(xElement3); } return(xElement2); } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } } if (type != expectedType) { XAttribute content = new XAttribute("Class", GenTypes.GetTypeNameWithoutIgnoredNamespaces(obj.GetType())); xElement2.Add(content); } { foreach (FieldInfo item in from f in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) orderby f.MetadataToken select f) { try { XElement xElement4 = XElementFromField(item, obj); if (xElement4 != null) { xElement2.Add(xElement4); } } catch { throw; } } return(xElement2); } } Type expectedType4 = type.GetGenericArguments()[0]; int num = (int)type.GetProperty("Count").GetValue(obj, null); for (int i = 0; i < num; i++) { object[] index = new object[1] { i }; object value3 = type.GetProperty("Item").GetValue(obj, index); XNode content2 = XElementFromObject(value3, expectedType4, "li", null, saveDefsAsRefs: true); xElement2.Add(content2); } } return(xElement2); }