Skip to content

Commit

Permalink
preperations
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluscream committed May 28, 2020
1 parent 0803043 commit 46c2946
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
24 changes: 14 additions & 10 deletions Classes/Mounts.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Gameloop.Vdf;
using Gameloop.Vdf.JsonConverter;
using Gameloop.Vdf.Linq;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
Expand All @@ -22,26 +25,27 @@ public MountsConfig(FileInfo file)
Logger.Info("Reading mounts from {}", file.FullName);
var text = this.File.ReadAllText();
var vp = VdfConvert.Deserialize(text, VdfSerializerSettings.Default);
Mounts = new BindingList<Mount>(vp.Value.Children().Select(x => new Mount(x.Value.Value<string>(), x.Key)).ToList());
Mounts = new BindingList<Mount>(vp.Value.Children().Select(x => new Mount(x.Value.Value<string>(), x.Key)).ToList()); // TODO: .Properties()
Logger.Info("Read {} mounts successfully", Mounts.Count);
// Logger.Trace(Mounts.ToJSON());
}

public void Save()
public void Save(FileInfo file = null)
{
var bakfile = File.Combine(".bak");
var str = ""; // VdfConvert.Serialize(VToken()); // TODO: https://github.com/shravan2x/Gameloop.Vdf/issues/19
if (str == File.ReadAllText())
file = file ?? File;
var bakfile = file.Combine(".bak");
var str = Extensions.toVdf(Mounts); // VdfConvert.Serialize(VToken()); // TODO: https://github.com/shravan2x/Gameloop.Vdf/issues/19
if (str == file.ReadAllText())
{
Logger.Trace("File already contains proposed changes, discarding save..."); return;
}
if (!bakfile.Exists || bakfile.ReadAllText() != str)
{
Logger.Trace("Backup file does not exist or does not contain current content, copying {} to {}", File.Name, bakfile.Name);
File.CopyTo(bakfile.FullName);
Logger.Trace("Backup file does not exist or does not contain current content, copying {} to {}", file.Name, bakfile.Name);
file.CopyTo(bakfile.FullName);
}
Logger.Trace("Saving to {}", File.FullName);
File.WriteAllText(str);
Logger.Debug("Saving to {}", File.FullName);
file.WriteAllText(str);
}
}

Expand All @@ -60,7 +64,7 @@ public DirectoryInfo Path
get { return _path; }
set
{
SourceMod = (value.Parent.Name == "sourcemods") ? true : false;
SourceMod = (value.Parent.Name.ToLower() == "sourcemods") ? true : false;
_path = value;
}
}
Expand Down
7 changes: 4 additions & 3 deletions UI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal partial class Main : Form
{
private DirectoryInfo gmodDir;
private MountsConfig cfg;
private MountsConfig disabledCfg;
private BindingSource source;

private void InvokeUI(Action a)
Expand Down Expand Up @@ -46,6 +47,7 @@ public Main()
}
InitializeComponent();
LoadMountsCFG(cfgFile);
// LoadMountsCFG(cfgFile.Directory.CombineFile("mount.disabled.cfg");
}

public void LoadMountsCFG(FileInfo cfgFile)
Expand Down Expand Up @@ -227,9 +229,8 @@ private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Saving is not possible until \"https://github.com/shravan2x/Gameloop.Vdf/issues/18\" is solved", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
VObject rootValue = JObject.FromObject(cfg.Mounts).ToVdf();
Logger.Warn(VdfConvert.Serialize(new VProperty("mountcfg", rootValue)));
// MessageBox.Show("Saving is not possible until \"https://github.com/shravan2x/Gameloop.Vdf/issues/18\" is solved", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cfg.Save(cfg.File.Directory.CombineFile("mount.test.cfg"));
}

private void createMapPoolToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
27 changes: 26 additions & 1 deletion Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,28 @@
using System.Web;
using System.Windows.Forms;

using Gameloop.Vdf;
using Gameloop.Vdf.JsonConverter;
using Gameloop.Vdf.Linq;

namespace GModMountManager
{
internal static class Extensions
{
#region VDF

public static string toVdf(BindingList<Mount> Mounts)
{
var mountcfg = new Dictionary<string, string>();
foreach (var mount in Mounts)
{
mountcfg.Add(mount.Name, mount.Path.FullName);
}
return VdfConvert.Serialize(new VProperty("mountcfg", JToken.FromObject(mountcfg).ToVdf()), new VdfSerializerSettings { UsesEscapeSequences = false });
}

#endregion VDF

#region Reflection

public static Dictionary<string, object> ToDictionary(this object instanceToConvert)
Expand Down Expand Up @@ -132,6 +150,8 @@ public static void ShowInExplorer(this DirectoryInfo dir)
Utils.StartProcess("explorer.exe", null, dir.FullName.Quote());
}

public static string PrintablePath(this FileSystemInfo file) => file.FullName.Replace(@"\\", @"\");

#endregion DirectoryInfo

#region FileInfo
Expand Down Expand Up @@ -169,7 +189,12 @@ public static void AppendLine(this FileInfo file, string line)
catch { }
}

public static void WriteAllText(this FileInfo file, string text) => File.WriteAllText(file.FullName, text);
public static void WriteAllText(this FileInfo file, string text)
{
file.Directory.Create();
if (!file.Exists) file.Create().Close();
File.WriteAllText(file.FullName, text);
}

public static string ReadAllText(this FileInfo file) => File.ReadAllText(file.FullName);

Expand Down

0 comments on commit 46c2946

Please sign in to comment.