Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggles store true/false #232

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void InitTrueValue()
TrueValue = vals is not null ? string.Join("", vals) : string.Empty;
});

int i = 0;
}
}
}
32 changes: 19 additions & 13 deletions ModManager_Classes/Models/ModTweaker/DataModel/Tweaking/ModOp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class ModOp

public string? Skip
{
get => GetShortcut(nameof(Skip));
set => SetShortcut(nameof(Skip), value);
get => GetShortcut(nameof(Skip), acceptNull: true);
set => SetShortcut(nameof(Skip), value, acceptNull: true);
}

//Mod Op related things
Expand All @@ -40,30 +40,30 @@ public string? Path
set => SetShortcut(nameof(Path), value, acceptNull: true);
}

public XmlAttributeCollection XmlAttributes { get; init; }
public Dictionary<string, string?> Attributes { get; init; }

public bool IsValid => GUID is string || Path is string;
public bool HasID => ID is string;

private string? GetShortcut(string attributeKey, bool acceptNull = false)
{
return XmlAttributes.GetNamedItem(attributeKey)?.Value
return Attributes.GetValueOrDefault(attributeKey)
?? (acceptNull ? null : throw new InvalidDataException($"ModOp without {attributeKey} attribute!"));
}

private void SetShortcut(string attributeKey, string? value, bool acceptNull = false)
{
try
if (!acceptNull && value is null)
{
XmlAttributes.GetNamedItem(attributeKey)!.Value ??= value;
throw new ArgumentException($"Cannot assign a null value to {attributeKey} attribute!");
}
catch (NullReferenceException e)
if (Attributes.ContainsKey(attributeKey))
{
if (!acceptNull)
{
throw new InvalidDataException($"ModOp without {attributeKey} attribute!");
}
Attributes[attributeKey] = value;
return;
}

Attributes.Add(attributeKey, value);
}

public static ModOp? FromXmlNode(XmlNode ModOp)
Expand All @@ -80,11 +80,17 @@ private void SetShortcut(string attributeKey, string? value, bool acceptNull = f
if (type is not null)
{
ModOp.TryGetAttribute(TweakerConstants.MODOP_ID, out string? ID);
var dict = new Dictionary<string, string>();
foreach (XmlAttribute attribute in ModOp.Attributes!)
{
dict.TryAdd(attribute.Name, attribute.Value);
}
return new ModOp
{
ID = ID!,
Code = ModOp.ChildNodes.Cast<XmlNode>().ToList(),
XmlAttributes = ModOp.Attributes!
Attributes = dict,
Type = type
};
}
return null;
Expand All @@ -96,7 +102,7 @@ public ModOp Clone()
{
ID = ID,
Code = Code.Select(x => x.CloneNode(true)).ToList(),
XmlAttributes = XmlAttributes
Attributes = Attributes
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ public string GetDefaultNodeValue(IExposedModValue expose)

var elem = TargetDocument.CreateElement("ModOp");

foreach (XmlAttribute xmlAttribute in modop.XmlAttributes)
foreach (var (key, value) in modop.Attributes)
{
if (TweakerConstants.ArgumentBlacklist.Contains(xmlAttribute.Name))
if (TweakerConstants.ArgumentBlacklist.Contains(key))
continue;
elem.Attributes.Append((XmlAttribute)TargetDocument.ImportNode(xmlAttribute, true));
var attribute = TargetDocument.CreateAttribute(key);
attribute.Value = value;
elem.Attributes.Append(attribute);
}

if (modop.Path is not null)
Expand Down
7 changes: 7 additions & 0 deletions ModManager_Classes/Models/ModTweaker/IO/ModTweaksExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ private TweakerFileStorageModel GetTweak(TweakerFile tweakerFile)
{
var tweak = new TweakerFileStorageModel();
foreach (IExposedModValue expose in tweakerFile.Exposes)
{
if (expose is ExposedToggleModValue toggleExpose)
{
tweak.SetTweakValue(expose.ExposeID, toggleExpose.IsTrue.ToString());
continue;
}
tweak.SetTweakValue(expose.ExposeID, expose.Value);
}
return tweak;
}
}
Expand Down
15 changes: 13 additions & 2 deletions ModManager_Classes/Models/ModTweaker/IO/ModTweaksLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,19 @@ private void ApplyToTweakerFile(TweakerFile file, TweakerFileStorageModel tweak)
{
foreach (IExposedModValue expose in file.Exposes)
{
if (tweak.HasStoredValue(expose.ExposeID))
expose.Value = tweak.GetTweakValue(expose.ExposeID)!;
if (!tweak.HasStoredValue(expose.ExposeID))
continue;

if (expose is ExposedToggleModValue toggleExpose)
{
if (!Boolean.TryParse(tweak.GetTweakValue(expose.ExposeID), out bool result))
{
result = false;
}
toggleExpose.IsTrue = result;
continue;
}
expose.Value = tweak.GetTweakValue(expose.ExposeID)!;
}
}
}
Expand Down