Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Added support for auto updating .json files.
Browse files Browse the repository at this point in the history
resolves #9
  • Loading branch information
sir-wilhelm committed Feb 15, 2020
1 parent 2a6ce2e commit 9147c08
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
33 changes: 24 additions & 9 deletions SmartHunter/Core/Config/ConfigContainer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SmartHunter.Game.Helpers;
using ErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;

namespace SmartHunter.Core.Config
Expand Down Expand Up @@ -36,16 +36,19 @@ override protected void OnChanged()

void Load()
{
if (File.Exists(FullPathFileName))// && FileName.Equals("Config.json"))
if (File.Exists(FullPathFileName))
{
try
{
string contents = null;
using var stream = File.Open(FullPathFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using var reader = new StreamReader(stream, System.Text.Encoding.UTF8);
contents = reader.ReadToEnd();
using (var stream = File.Open(FullPathFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var reader = new StreamReader(stream, System.Text.Encoding.UTF8))
{
contents = reader.ReadToEnd();
}

if (contents != GetAutoGenerateedJson() && FileName != "Config.json")
var contentsEqualsAutoGen = contents == GetAutoGenerateedJson();
if (!contentsEqualsAutoGen && FileName != "Config.json")
{
Log.WriteWarning($"Warning: {FileName} differs from autogenerated version.");
}
Expand All @@ -65,9 +68,21 @@ void Load()
settings.Converters.Add(new StringEnumConverter());
settings.Converters.Add(new StringFloatConverter());

JsonConvert.PopulateObject(contents, Values, settings);
if (FileName.Equals("Config.json") || contentsEqualsAutoGen
|| (!FileName.Equals("Config.json") && ConfigHelper.Main.Values.UseCustomData))
{
JsonConvert.PopulateObject(contents, Values, settings);
Log.WriteLine($"{FileName} loaded");
}
else
{
Log.WriteLine($"{FileName} will be renamed to custom_{FileName} and recreated...");
Log.WriteLine("\tThis can be disabled by setting [\"UseCustomData\": true,] in Config.json.");

Log.WriteLine($"{FileName} loaded");
File.Delete($"custom_{FileName}");
File.Move(FileName, $"custom_{FileName}");
Save();
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -108,7 +123,7 @@ private string GetAutoGenerateedJson()
ContractResolver = new ContractResolver()
};

settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
settings.Converters.Add(new StringEnumConverter());
settings.Converters.Add(new StringFloatConverter());

return JsonConvert.SerializeObject(Values, settings);
Expand Down
3 changes: 2 additions & 1 deletion SmartHunter/Game/Config/MainConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class MainConfig
public string MemoryFileName = "Memory.json";

public bool ShutdownWhenProcessExits = false;
public bool AutomaticallyCheckAndDownloadUpdates = true; // TODO: Rimetti a true
public bool UseCustomData = false;
public bool AutomaticallyCheckAndDownloadUpdates = true;

public OverlayConfig Overlay = new OverlayConfig();

Expand Down
6 changes: 4 additions & 2 deletions SmartHunter/Ui/Windows/ConsoleWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<Window x:Class="SmartHunter.Ui.Windows.ConsoleWindow"
<Window x:Class="SmartHunter.Ui.Windows.ConsoleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:SmartHunter.Game.Data.ViewModels"
d:DataContext="{x:Static viewModels:ConsoleViewModel.Instance}"
mc:Ignorable="d"
Title="SmartHunter" Height="450" Width="800"
Title="SmartHunter"
Height="400"
Width="1000"
>

<Window.Resources>
Expand Down

0 comments on commit 9147c08

Please sign in to comment.