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 (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 T SaveableFromNode <T>(XmlNode subNode, object[] ctorArgs) { if (Scribe.mode != LoadSaveMode.LoadingVars) { Log.Error("Called SaveableFromNode(), but mode is " + Scribe.mode); return(default(T)); } if (subNode == null) { return(default(T)); } XmlAttribute xmlAttribute = subNode.Attributes["IsNull"]; T result; if (xmlAttribute != null && xmlAttribute.Value == "True") { result = default(T); } else { try { XmlAttribute xmlAttribute2 = subNode.Attributes["Class"]; string text = (xmlAttribute2 == null) ? typeof(T).FullName : xmlAttribute2.Value; Type type = BackCompatibility.GetBackCompatibleType(typeof(T), text, subNode); if (type == null) { Log.Error(string.Concat(new object[] { "Could not find class ", text, " while resolving node ", subNode.Name, ". Trying to use ", typeof(T), " instead. Full node: ", subNode.OuterXml })); type = typeof(T); } if (type.IsAbstract) { throw new ArgumentException("Can't load abstract class " + type); } IExposable exposable = (IExposable)Activator.CreateInstance(type, ctorArgs); bool flag = typeof(T).IsValueType || typeof(Name).IsAssignableFrom(typeof(T)); if (!flag) { Scribe.loader.crossRefs.RegisterForCrossRefResolve(exposable); } XmlNode curXmlParent = Scribe.loader.curXmlParent; IExposable curParent = Scribe.loader.curParent; string curPathRelToParent = Scribe.loader.curPathRelToParent; Scribe.loader.curXmlParent = subNode; Scribe.loader.curParent = exposable; Scribe.loader.curPathRelToParent = null; try { exposable.ExposeData(); } finally { Scribe.loader.curXmlParent = curXmlParent; Scribe.loader.curParent = curParent; Scribe.loader.curPathRelToParent = curPathRelToParent; } if (!flag) { Scribe.loader.initer.RegisterForPostLoadInit(exposable); } result = (T)((object)exposable); } catch (Exception ex) { result = default(T); Log.Error(string.Concat(new object[] { "SaveableFromNode exception: ", ex, "\nSubnode:\n", subNode.OuterXml })); } } return(result); }