public static void LookDeep <T>(ref T target, string label, params object[] ctorArgs)
 {
     if (Scribe.mode == LoadSaveMode.Saving)
     {
         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)
             //}));
             return;
         }
         if (target == null)
         {
             Scribe.EnterNode(label);
             Scribe.WriteAttribute("IsNull", "True");
             Scribe.ExitNode();
         }
         else
         {
             Scribe.EnterNode(label);
             if (target.GetType() != typeof(T))
             {
                 Scribe.WriteAttribute("Class", target.GetType().ToString());
             }
             exposable.ExposeData();
             Scribe.ExitNode();
         }
     }
     else if (Scribe.mode == LoadSaveMode.LoadingVars)
     {
         target = ScribeExtractor.SaveableFromNode <T>(Scribe.curParent[label], ctorArgs);
     }
 }
 public static void LookValue <T>(ref T value, string label, T defaultValue = default(T), bool forceSave = false)
 {
     if (Scribe.mode == LoadSaveMode.Saving)
     {
         //if (typeof(T) == typeof(TargetInfo))
         //{
         //    Log.Error("Saving a TargetInfo " + label + " with Scribe_Values. TargetInfos must be saved with Scribe_TargetInfo.");
         //    return;
         //}
         //if (typeof(Thing).IsAssignableFrom(typeof(T)))
         //{
         //    Log.Error("Using Scribe_Values with a Thing reference " + label + ". Use Scribe_References or Scribe_Deep instead.");
         //    return;
         //}
         //if (typeof(IExposable).IsAssignableFrom(typeof(T)))
         //{
         //    Log.Error("Using Scribe_Values with a IExposable reference " + label + ". Use Scribe_References or Scribe_Deep instead.");
         //    return;
         //}
         if (forceSave || (value == null && defaultValue != null) || (value != null && !value.Equals(defaultValue)))
         {
             if (value != null)
             {
                 Scribe.WriteElement(label, value.ToString());
             }
             else
             {
                 Scribe.WriteElement(label, "");
             }
         }
     }
     else if (Scribe.mode == LoadSaveMode.LoadingVars)
     {
         value = ScribeExtractor.ValueFromNode <T>(Scribe.curParent[label], defaultValue);
     }
 }
Exemplo n.º 3
0
 public static void LookList <T>(ref List <T> list, bool saveDestroyedThings, string label, LookMode lookMode = LookMode.Undefined, params object[] ctorArgs)
 {
     if (lookMode == LookMode.Undefined)
     {
         if (Helper_Parsing.HandlesType(typeof(T)))
         {
             lookMode = LookMode.Value;
         }
         else
         {
             lookMode = LookMode.Deep;
         }
     }
     if (Scribe.mode == LoadSaveMode.Saving)
     {
         if (list == null && lookMode == LookMode.Reference)
         {
             //Log.Warning(string.Concat(new object[]
             //{
             //    "Saving null list \"",
             //    label,
             //    "\" with look mode ",
             //    lookMode,
             //    ". This will cause bugs because null lists are not registered during loading so CrossRefResolver will break."
             //}));
         }
         Scribe.EnterNode(label);
         if (list == null)
         {
             Scribe.WriteAttribute("IsNull", "True");
         }
         else
         {
             foreach (T current in list)
             {
                 if (lookMode == LookMode.Value)
                 {
                     T t = current;
                     Scribe_Values.LookValue <T>(ref t, "li", default(T), true);
                 }
                 else if (lookMode == LookMode.Deep)
                 {
                     T t2 = current;
                     Scribe_Deep.LookDeep <T>(ref t2, "li", ctorArgs);
                 }
             }
         }
         Scribe.ExitNode();
     }
     else if (Scribe.mode == LoadSaveMode.LoadingVars)
     {
         if (Scribe.curParent == null)
         {
             //Log.Error("XmlHandling.curParent is null. I'm not sure why.");
             list = null;
             return;
         }
         XmlNode xmlNode = Scribe.curParent[label];
         if (xmlNode == null)
         {
             list = null;
             return;
         }
         XmlAttribute xmlAttribute = xmlNode.Attributes["IsNull"];
         if (xmlAttribute != null && xmlAttribute.Value.ToLower() == "true")
         {
             list = null;
             return;
         }
         if (lookMode == LookMode.Value)
         {
             list = new List <T>(xmlNode.ChildNodes.Count);
             foreach (XmlNode subNode in xmlNode.ChildNodes)
             {
                 T item = ScribeExtractor.ValueFromNode <T>(subNode, default(T));
                 list.Add(item);
             }
         }
         else if (lookMode == LookMode.Deep)
         {
             list = new List <T>(xmlNode.ChildNodes.Count);
             foreach (XmlNode subNode2 in xmlNode.ChildNodes)
             {
                 T item2 = ScribeExtractor.SaveableFromNode <T>(subNode2, ctorArgs);
                 list.Add(item2);
             }
         }
     }
 }