/// <summary> /// Pass a Game Class type (eg, Item) and get the corresponding Dataminer class (eg, DM_Item). /// </summary> /// <param name="_gameType">Eg, typeof(Item)</param> /// <param name="logging">If you want to log debug messages.</param> public static Type GetDMType(Type _gameType, bool logging = true) { var name = $"Dataminer.DM_{_gameType.Name}"; if (!m_typeCache.ContainsKey(name)) { Type t = null; try { t = DM_Assembly.GetType(name); if (t == null) { throw new Exception("Null"); } m_typeCache.Add(name, t); } catch (Exception e) { if (logging) { SL.LogWarning($"Could not get DM_Assembly Type '{name}'"); SL.LogWarning(e.Message); SL.LogWarning(e.StackTrace); } } return(t); } else { return(m_typeCache[name]); } }
/// <summary> /// Load an SL_Type object from XML. /// </summary> public static object LoadFromXml(string path) { if (!File.Exists(path)) { return(null); } // First we have to find out what kind of Type this xml was serialized as. string typeName = ""; using (XmlReader reader = XmlReader.Create(path)) { while (reader.Read()) // just get the first element (root) then break. { if (reader.NodeType == XmlNodeType.Element) { // the real type might be saved as an attribute if (!string.IsNullOrEmpty(reader.GetAttribute("type"))) { typeName = reader.GetAttribute("type"); } else { typeName = reader.Name; } break; } } } if (!string.IsNullOrEmpty(typeName) && DM_Assembly.GetType($"Dataminer.{typeName}") is Type type) { var xml = GetXmlSerializer(type); FileStream file = File.OpenRead(path); var obj = xml.Deserialize(file); file.Close(); return(obj); } else { return(null); } }