Client LuaCsForBarotrauma
EditableParams.cs
1 using System.Collections.Generic;
2 using System.Xml.Linq;
3 using Microsoft.Xna.Framework;
4 using File = Barotrauma.IO.File;
5 #if DEBUG
6 using System.IO;
7 using System.Xml;
8 #else
9 using Barotrauma.IO;
10 #endif
11 
12 namespace Barotrauma
13 {
15  {
16  public bool IsLoaded { get; protected set; }
17  public string Name { get; private set; }
18  public string FileName { get; private set; }
19  public string FileNameWithoutExtension { get; private set; }
20  public string Folder { get; private set; }
21  public ContentPath Path { get; protected set; } = ContentPath.Empty;
22  public Dictionary<Identifier, SerializableProperty> SerializableProperties { get; protected set; }
23 
25  protected XDocument doc;
26 
27  private XDocument Doc
28  {
29  get
30  {
31  if (!IsLoaded)
32  {
33  DebugConsole.ThrowError("[Params] Not loaded!");
34  return new XDocument();
35  }
36  return doc;
37  }
38  set
39  {
40  doc = value;
41  }
42  }
43 
45  {
46  get
47  {
48  if (rootElement?.Element != doc.Root)
49  {
50  rootElement = doc.Root.FromPackage(Path.ContentPackage);
51  }
52  return rootElement;
53  }
54  }
55 
56  public ContentXElement OriginalElement { get; protected set; }
57 
58  protected ContentXElement CreateElement(string name, params object[] attrs)
59  => new XElement(name, attrs).FromPackage(Path.ContentPackage);
60 
61  protected virtual string GetName() => System.IO.Path.GetFileNameWithoutExtension(Path.Value).FormatCamelCaseWithSpaces();
62 
63  protected virtual bool Deserialize(XElement element = null)
64  {
65  element ??= MainElement;
67  return SerializableProperties != null;
68  }
69 
70  protected virtual bool Serialize(XElement element = null)
71  {
72  element ??= MainElement;
73  if (element == null)
74  {
75  DebugConsole.ThrowError("[EditableParams] The XML element is null! Failed to save the parameters.");
76  return false;
77  }
78  SerializableProperty.SerializeProperties(this, element, true);
79  return true;
80  }
81 
82  protected virtual bool Load(ContentPath file)
83  {
84  UpdatePath(file);
85  doc = XMLExtensions.TryLoadXml(Path);
86  if (doc == null)
87  {
88  DebugConsole.ThrowError("[EditableParams] The document is null! Failed to load the parameters.",
89  contentPackage: file.ContentPackage);
90  return false;
91  }
92  if (MainElement == null)
93  {
94  DebugConsole.ThrowError("[EditableParams] The main element is null! Failed to load the parameters.",
95  contentPackage: file.ContentPackage);
96  return false;
97  }
99  OriginalElement = new XElement(MainElement).FromPackage(MainElement.ContentPackage);
100  return IsLoaded;
101  }
102 
103  protected virtual void UpdatePath(ContentPath fullPath)
104  {
105  Path = fullPath;
106  Name = GetName();
107  FileName = Barotrauma.IO.Path.GetFileName(Path.Value);
108  FileNameWithoutExtension = Barotrauma.IO.Path.GetFileNameWithoutExtension(Path.Value);
109  Folder = Barotrauma.IO.Path.GetDirectoryName(Path.Value);
110  }
111 
112  public virtual bool Save(string fileNameWithoutExtension = null, System.Xml.XmlWriterSettings settings = null)
113  {
114  if (!Directory.Exists(Folder))
115  {
116  Directory.CreateDirectory(Folder);
117  }
119  Serialize();
120  if (settings == null)
121  {
122  settings = new System.Xml.XmlWriterSettings
123  {
124  Indent = true,
125  OmitXmlDeclaration = true,
126  NewLineOnAttributes = true
127  };
128  }
129  if (fileNameWithoutExtension != null)
130  {
131  UpdatePath(ContentPath.FromRaw(Path.ContentPackage, System.IO.Path.Combine(Folder, $"{fileNameWithoutExtension}.xml")));
132  }
133  using (var writer = XmlWriter.Create(Path.Value, settings))
134  {
135  Doc.WriteTo(writer);
136  writer.Flush();
137  }
138  return true;
139  }
140 
141  public virtual bool Reset(bool forceReload = false)
142  {
143  if (forceReload)
144  {
145  return Load(Path);
146  }
148  }
149 
150 #if CLIENT
151  public SerializableEntityEditor SerializableEntityEditor { get; protected set; }
152  public virtual void AddToEditor(ParamsEditor editor, int space = 0)
153  {
154  if (!IsLoaded)
155  {
156  DebugConsole.ThrowError("[Params] Not loaded!");
157  return;
158  }
159  SerializableEntityEditor = new SerializableEntityEditor(editor.EditorBox.Content.RectTransform, this, false, true, titleFont: GUIStyle.LargeFont);
160  if (space > 0)
161  {
162  new GUIFrame(new RectTransform(new Point(editor.EditorBox.Rect.Width, space), editor.EditorBox.Content.RectTransform), style: null, color: ParamsEditor.Color)
163  {
164  CanBeFocused = false
165  };
166  }
167  }
168 #endif
169  }
170 }
readonly? ContentPackage ContentPackage
Definition: ContentPath.cs:21
static ContentPath FromRaw(string? rawValue)
static readonly ContentPath Empty
Definition: ContentPath.cs:12
string???????????? Value
Definition: ContentPath.cs:27
ContentPackage? ContentPackage
virtual string GetName()
SerializableEntityEditor SerializableEntityEditor
virtual void UpdatePath(ContentPath fullPath)
virtual bool Serialize(XElement element=null)
virtual bool Save(string fileNameWithoutExtension=null, System.Xml.XmlWriterSettings settings=null)
virtual bool Reset(bool forceReload=false)
ContentXElement CreateElement(string name, params object[] attrs)
Dictionary< Identifier, SerializableProperty > SerializableProperties
ContentXElement OriginalElement
ContentXElement rootElement
virtual ContentXElement? MainElement
virtual bool Load(ContentPath file)
virtual bool Deserialize(XElement element=null)
virtual void AddToEditor(ParamsEditor editor, int space=0)
virtual Rectangle Rect
RectTransform RectTransform
GUIFrame Content
A frame that contains the contents of the listbox. The frame itself is not rendered.
Definition: GUIListBox.cs:42
static XmlWriter Create(string path, System.Xml.XmlWriterSettings settings)
Definition: SafeIO.cs:164
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)
static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault=false, bool ignoreEditable=false)