Skip to content
This repository has been archived by the owner on Nov 29, 2020. It is now read-only.

Commit

Permalink
Implemented saving of mod settings; added support for mod settings in…
Browse files Browse the repository at this point in the history
… Factorio 0.15.
  • Loading branch information
Artentus committed May 22, 2019
1 parent a95acd6 commit 76cc0eb
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 41 deletions.
2 changes: 1 addition & 1 deletion ModMyFactory/ModMyFactory/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace ModMyFactory
{
public partial class App : Application
{
private const int PreReleaseVersion = 0;
private const int PreReleaseVersion = 1;

/// <summary>
/// The current application instance.
Expand Down
5 changes: 5 additions & 0 deletions ModMyFactory/ModMyFactory/Helpers/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public static void Serialize<T>(T value, FileInfo file)
}
}

public static string Serialize<T>(T value)
{
return JsonConvert.SerializeObject(value, Formatting.Indented, DefaultSettings);
}

public static T Deserialize<T>(FileInfo file)
{
using (var reader = file.OpenText())
Expand Down
30 changes: 25 additions & 5 deletions ModMyFactory/ModMyFactory/ModCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using ModMyFactory.Models;
using ModMyFactory.ModSettings;

namespace ModMyFactory
{
Expand Down Expand Up @@ -123,26 +124,45 @@ public void EndUpdate()
updating = false;
EvaluateDependencies();
LoadSettings();
ModSettingsManager.SaveBinarySettings(this);
}

protected override void RemoveItem(int index)
{
var item = this[index];
bool active = item.Active;

base.RemoveItem(index);
EvaluateDependencies();

if (!updating)
{
EvaluateDependencies();
if (active) ModSettingsManager.SaveBinarySettings(this);
}
}

protected override void InsertItem(int index, Mod item)
{
base.InsertItem(index, item);
EvaluateDependencies();
LoadSettings();

if (!updating)
{
EvaluateDependencies();
LoadSettings();
if (item.Active) ModSettingsManager.SaveBinarySettings(this);
}
}

protected override void SetItem(int index, Mod item)
{
base.SetItem(index, item);
EvaluateDependencies();
LoadSettings();

if (!updating)
{
EvaluateDependencies();
LoadSettings();
if (item.Active) ModSettingsManager.SaveBinarySettings(this);
}
}
}
}
114 changes: 101 additions & 13 deletions ModMyFactory/ModMyFactory/ModSettings/ModSettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ModMyFactory.ModSettings
{
static class ModSettingsManager
{
static readonly Version BehaviourSwitch = new Version(0, 16); // 0.15 uses json format while 0.16 onwards use binary format.

static int updateCount = 0;
static readonly FileInfo settingsFile;
static Dictionary<string, ModSettingsExportTemplate> modSettings;
static Dictionary<Version, BinaryFile> binaryFiles;
Expand All @@ -21,12 +23,79 @@ static ModSettingsManager()
{
settingsFile = new FileInfo(Path.Combine(App.Instance.AppDataPath, "mod-settings.json"));
}
public static void SaveSettings(ModCollection mods, Modpack modpack)

private static ModSettingsExportTemplate GetTemplate(Version version)
{

if (!deserializedBinary.TryGetValue(version, out var result))
{
result = new ModSettingsExportTemplate();
deserializedBinary.Add(version, result);
}
return result;
}

public static void BeginUpdate()
{
updateCount++;
}

public static void EndUpdate(bool force = false)
{
if (force) updateCount = 0;
else updateCount--;
}

public static void SaveBinarySettings(ModCollection mods)
{
if (updateCount > 0) return;

binaryFiles.Clear();
deserializedBinary.Clear();

foreach (var mod in mods)
{
if (mod.Active)
{
var binary = GetTemplate(mod.FactorioVersion);
binary.AddMod(mod);
}
}

var modDir = App.Instance.Settings.GetModDirectory();
foreach (var subDir in modDir.EnumerateDirectories())
{
if (Version.TryParse(subDir.Name, out var version))
GetTemplate(version);
}

foreach (var kvp in deserializedBinary)
{
var version = kvp.Key;
var template = kvp.Value;

if (version >= BehaviourSwitch)
{
string json = JsonHelper.Serialize(template);

var file = new BinaryFile(version, json);
var dir = App.Instance.Settings.GetModDirectory(version);
if (!dir.Exists) dir.Create();
file.Save(new FileInfo(Path.Combine(dir.FullName, "mod-settings.dat")));
}
else
{
var dir = App.Instance.Settings.GetModDirectory(version);
if (!dir.Exists) dir.Create();
JsonHelper.Serialize(template, new FileInfo(Path.Combine(dir.FullName, "mod-settings.json")));
}
}
}

public static void SaveBinarySettings(Modpack modpack)
{
if (updateCount > 0) return;
}

public static void LoadSettings()
{
if (settingsFile.Exists)
Expand Down Expand Up @@ -54,20 +123,39 @@ public static void LoadSettings()
{
if (Version.TryParse(subDir.Name, out var version))
{
var file = new FileInfo(Path.Combine(subDir.FullName, "mod-settings.dat"));
if (file.Exists)
if (version >= BehaviourSwitch)
{
try
var file = new FileInfo(Path.Combine(subDir.FullName, "mod-settings.dat"));
if (file.Exists)
{
var binFile = new BinaryFile(file);
binaryFiles.Add(version, binFile);
try
{
var binFile = new BinaryFile(file);
binaryFiles.Add(version, binFile);

var template = JsonHelper.Deserialize<ModSettingsExportTemplate>(binFile.JsonString);
deserializedBinary.Add(version, template);
var template = JsonHelper.Deserialize<ModSettingsExportTemplate>(binFile.JsonString);
deserializedBinary.Add(version, template);
}
catch (Exception ex) when (ex is ArgumentException || ex is JsonSerializationException)
{
App.Instance.WriteExceptionLog(ex);
}
}
catch (Exception ex) when (ex is ArgumentException || ex is JsonSerializationException)
}
else
{
var file = new FileInfo(Path.Combine(subDir.FullName, "mod-settings.json"));
if (file.Exists)
{
App.Instance.WriteExceptionLog(ex);
try
{
var template = JsonHelper.Deserialize<ModSettingsExportTemplate>(file);
deserializedBinary.Add(version, template);
}
catch (Exception ex) when (ex is ArgumentException || ex is JsonSerializationException)
{
App.Instance.WriteExceptionLog(ex);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ enum PropertyTreeType : byte
static readonly BinaryVersion DefaultWriteVersion = new BinaryVersion(0, 17, 9, 1);
static readonly Dictionary<Version, BinaryVersion> DefaultWriteVersions = new Dictionary<Version, BinaryVersion>()
{
{ new Version(0, 16), new BinaryVersion(0, 16, 36, 2) },
{ new Version(0, 17), new BinaryVersion(0, 17, 9, 1) },
{ new Version(0, 16), new BinaryVersion(0, 16, 51, 0) },
{ new Version(0, 17), new BinaryVersion(0, 17, 42, 4) },
};
static readonly BinaryVersion BehaviourSwitch = new BinaryVersion(0, 17, 0, 0); // Starting with 0.17 there is an additional byte in the file.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,7 @@ public ModSettingsExportTemplate()
public ModSettingsExportTemplate(IHasModSettings mod)
: this()
{
foreach (var setting in mod.Settings)
{
var template = setting.CreateValueTemplate();
switch (setting.LoadTime)
{
case LoadTime.Startup:
StartupTemplates[setting.Name] = template;
break;
case LoadTime.RuntimeGlobal:
RuntimeGlobalTemplates[setting.Name] = template;
break;
case LoadTime.RuntimeUser:
RuntimeUserTemplates[setting.Name] = template;
break;
}
}
AddMod(mod);
}

[JsonConstructor]
Expand All @@ -53,9 +38,9 @@ public ModSettingsExportTemplate(Dictionary<string, ModSettingValueTemplate> sta
RuntimeUserTemplates = runtimeUserTemplates;
}

public void AddSetting<T>(IModSetting<T> setting) where T : IEquatable<T>
public void AddSetting(IModSetting setting)
{
var template = new ModSettingValueTemplate(setting.Value);
var template = setting.CreateValueTemplate();

switch (setting.LoadTime)
{
Expand All @@ -71,6 +56,12 @@ public void AddSetting<T>(IModSetting<T> setting) where T : IEquatable<T>
}
}

public void AddMod(IHasModSettings mod)
{
foreach (var setting in mod.Settings)
AddSetting(setting);
}

public bool TryGetValue<T>(IModSetting<T> setting, out T value) where T : IEquatable<T>
{
Dictionary<string, ModSettingValueTemplate> dict;
Expand Down
6 changes: 6 additions & 0 deletions ModMyFactory/ModMyFactory/Models/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public bool Active

ModManager.SetActive(Name, Version, FactorioVersion, value, IsOnly, IsDefault);

ModSettingsManager.BeginUpdate();

if (active)
{
File.Enable();
Expand All @@ -90,6 +92,9 @@ public bool Active

if (active && App.Instance.Settings.ActivateDependencies)
ActivateDependencies(App.Instance.Settings.ActivateOptionalDependencies);

ModSettingsManager.EndUpdate();
ModSettingsManager.SaveBinarySettings(parentCollection);
}
}
}
Expand Down Expand Up @@ -404,6 +409,7 @@ public void ViewSettings()
settingsWindow.ShowDialog();

ModSettingsManager.SaveSettings(this);
if (Active) ModSettingsManager.SaveBinarySettings(parentCollection);
}

private void DeleteOldVersions()
Expand Down
7 changes: 7 additions & 0 deletions ModMyFactory/ModMyFactory/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public bool? AllModsActive
if (allModsActive.HasValue)
{
ModManager.BeginUpdateTemplates();
ModSettingsManager.BeginUpdate();

foreach (var mod in Mods)
{
Expand All @@ -184,6 +185,8 @@ public bool? AllModsActive

ModManager.EndUpdateTemplates(true);
ModManager.SaveTemplates();
ModSettingsManager.EndUpdate(true);
ModSettingsManager.SaveBinarySettings(Mods);
}

allModsSelectedChanging = false;
Expand Down Expand Up @@ -337,6 +340,7 @@ public bool? AllModpacksActive
if (allModpacksActive.HasValue)
{
ModManager.BeginUpdateTemplates();
ModSettingsManager.BeginUpdate();

foreach (var modpack in Modpacks)
{
Expand All @@ -346,6 +350,8 @@ public bool? AllModpacksActive

ModManager.EndUpdateTemplates(true);
ModManager.SaveTemplates();
ModSettingsManager.EndUpdate(true);
ModSettingsManager.SaveBinarySettings(Mods);
}

allModpacksSelectedChanging = false;
Expand Down Expand Up @@ -639,6 +645,7 @@ private void Refresh()
ModSettingsManager.LoadSettings();
LoadModsAndModpacks();
ModSettingsManager.SaveSettings(Mods);
ModSettingsManager.SaveBinarySettings(Mods);

ModManager.EndUpdateTemplates(true);
ModManager.SaveTemplates();
Expand Down
4 changes: 2 additions & 2 deletions ModMyFactory/ModMyFactory/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,8 @@
<KeyBinding Key="Return" Command="{Binding EndEditCommand}"/>
</TextBox.InputBindings>
</TextBox>
<Button Grid.Column="3" Margin="2,0" Style="{StaticResource SettingsButtonStyle}" ToolTip="{DynamicResource ModSettingsToolTip}"
Visibility="{Binding HasSettings, Converter={StaticResource VisibilityConverter}}" Command="{Binding ViewSettingsCommand}"/>
<!--<Button Grid.Column="3" Margin="2,0" Style="{StaticResource SettingsButtonStyle}" ToolTip="{DynamicResource ModSettingsToolTip}"
Visibility="{Binding HasSettings, Converter={StaticResource VisibilityConverter}}" Command="{Binding ViewSettingsCommand}"/>-->
<Border Grid.Column="5" Margin="2" Background="Transparent" ToolTip="{DynamicResource MissingDependenciesModpackToolTip}"
Visibility="{Binding HasUnsatisfiedDependencies, Converter={StaticResource VisibilityConverter}}">
<Image Margin="1" Source="{StaticResource DependencyWarningIcon}" Stretch="None"/>
Expand Down

0 comments on commit 76cc0eb

Please sign in to comment.