3 using System.Collections.Generic;
4 using System.Collections.Immutable;
10 using Microsoft.Xna.Framework;
25 public readonly
string Name;
31 Name = doc.Root!.GetAttributeString(
"name",
"");
33 CorePackage corePackage = ContentPackageManager.VanillaCorePackage!;
34 List<RegularPackage> regularPackages =
new List<RegularPackage>();
37 if (pkg is
CorePackage core) { corePackage = core; }
41 foreach (var element
in doc.Root!.Elements())
43 ModType modType = Enum.TryParse<
ModType>(element.Name.LocalName, ignoreCase:
true, out var mt) ? mt :
ModType.Local;
48 CorePackage = ContentPackageManager.VanillaCorePackage!;
52 var
id = element.GetAttributeUInt64(
"id", 0);
53 if (
id == 0) {
continue; }
54 var pkg = ContentPackageManager.WorkshopPackages.FirstOrDefault(p =>
55 p.TryExtractSteamWorkshopId(out var workshopId) && workshopId.Value == id);
56 if (pkg !=
null) { addPkg(pkg); }
61 var name = element.GetAttributeString(
"name",
"");
62 if (name.IsNullOrEmpty()) {
continue; }
63 var pkg = ContentPackageManager.LocalPackages.FirstOrDefault(p => p.NameMatches(name));
64 if (pkg !=
null) { addPkg(pkg); }
84 +
"\n " + TextManager.AddPunctuation(
':', TextManager.Get(
"CorePackage"))
88 retVal +=
"\n " + TextManager.AddPunctuation(
':', TextManager.Get(
"RegularPackages"))
98 XDocument newDoc =
new XDocument();
99 XElement newRoot =
new XElement(
"mods",
new XAttribute(
"name",
Name));
104 if (pkg == ContentPackageManager.VanillaCorePackage) {
return ModType.Vanilla; }
105 if (ContentPackageManager.WorkshopPackages.Contains(pkg)) {
return ModType.Workshop; }
110 var pkgType = determineType(pkg);
111 var pkgElem =
new XElement(pkgType.ToString());
115 pkgElem.SetAttributeValue(
"name", pkg.
Name);
116 if (pkg.
UgcId.TryUnwrap(out var ugcId))
118 pkgElem.SetAttributeValue(
"id", ugcId.ToString());
122 pkgElem.SetAttributeValue(
"name", pkg.
Name);
125 newRoot.Add(pkgElem);
130 if (!Directory.Exists(
SavePath)) { Directory.CreateDirectory(
SavePath, catchUnauthorizedAccessExceptions:
false); }
131 newDoc.SaveSafe(Path.Combine(
SavePath, ToolBox.RemoveInvalidFileNameChars($
"{Name}.xml")));
140 private bool OpenLoadPreset(GUIButton _,
object __)
146 private void OpenLoadPreset()
148 var msgBox =
new GUIMessageBox(
149 TextManager.Get(
"LoadModListPresetHeader"),
151 buttons:
new [] { TextManager.Get(
"Load"), TextManager.Get(
"Cancel") },
152 relativeSize: (0.4f, 0.6f));
154 var presetListBox =
new GUIListBox(
new RectTransform((1.0f, 0.7f), msgBox.Content.RectTransform));
156 (
string Path, XDocument? Doc) tryLoadXml(
string path)
157 => (path, XMLExtensions.TryLoadXml(path));
159 var presets = Directory.Exists(ModListPreset.SavePath)
160 ? Directory.GetFiles(ModListPreset.SavePath)
162 .Where(d => d.Doc !=
null)
164 : Array.Empty<(
string Path, XDocument? Doc)>();
166 foreach (var doc
in presets)
168 ModListPreset preset =
new ModListPreset(doc.Doc!);
169 var presetFrame =
new GUIFrame(
new RectTransform((1.0f, 0.09f), presetListBox.Content.RectTransform),
170 style:
"ListBoxElement")
173 ToolTip = preset.GetTooltip()
175 new GUITextBlock(
new RectTransform(Vector2.One, presetFrame.RectTransform), preset.Name)
180 =
new GUIButton(
new RectTransform((0.2f, 1.0f), presetFrame.RectTransform,
Anchor.CenterRight),
181 TextManager.Get(
"Delete"), style:
"GUIButtonSmall")
183 OnClicked = (button, o) =>
185 File.Delete(doc.Path);
186 presetListBox.Content.RemoveChild(presetFrame);
192 msgBox.Buttons[0].OnClicked = (button, o) =>
194 if (presetListBox.SelectedData is ModListPreset preset)
202 allChildren.Where(c => c.UserData is RegularPackage p
203 && preset.RegularPackages.Contains(p))
204 .OrderBy(c => c.UserData is RegularPackage p ? preset.RegularPackages.IndexOf(p) :
int.MaxValue)
206 var toDisable = allChildren.Where(c => !toEnable.Contains(c)).ToArray();
210 enabledCoreDropdown.SelectItem(preset.CorePackage);
215 msgBox.Buttons[1].OnClicked = msgBox.Close;
218 private bool OpenSavePreset(GUIButton _,
object __)
224 private void OpenSavePreset()
226 var msgBox =
new GUIMessageBox(
227 TextManager.Get(
"SaveModListPresetHeader"),
229 buttons:
new [] { TextManager.Get(
"Save"), TextManager.Get(
"Cancel") },
230 relativeSize: (0.4f, 0.2f));
232 var nameBox =
new GUITextBox(
new RectTransform((1.0f, 0.3f), msgBox.Content.RectTransform),
"");
234 msgBox.Buttons[0].OnClicked = (button, o) =>
236 if (nameBox.Text.IsNullOrEmpty())
238 nameBox.Flash(GUIStyle.Red);
242 if (enabledCoreDropdown.
SelectedData is CorePackage corePackage)
244 ModListPreset preset =
new ModListPreset(nameBox.Text,
247 .Select(c => c.UserData)
248 .OfType<RegularPackage>().ToArray());
254 msgBox.Buttons[1].OnClicked = msgBox.Close;
readonly Option< ContentPackageId > UgcId
RectTransform RectTransform
IEnumerable< GUIComponent > Children
GUIFrame Content
A frame that contains the contents of the listbox. The frame itself is not rendered.
override void ClearChildren()
static LocalizedString Join(string separator, params LocalizedString[] subStrs)
static RichString Rich(LocalizedString str, Func< string, string >? postProcess=null)
ModListPreset(string name, CorePackage corePackage, IReadOnlyList< RegularPackage > regularPackages)
ModListPreset(XDocument doc)
readonly CorePackage CorePackage
readonly ImmutableArray< RegularPackage > RegularPackages