Skip to content

Commit

Permalink
Add Profile Hashing
Browse files Browse the repository at this point in the history
(note: delete previous profiles.json file as it is incompatible)
  • Loading branch information
TrinityDevelopers committed Apr 22, 2020
1 parent 9f11427 commit 8a35e57
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 27 deletions.
2 changes: 1 addition & 1 deletion ZenovaLauncher/Dialogs/AddProfileDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AddProfileDialog()

private void CreateProfile(object sender, ContentDialogButtonClickEventArgs e)
{
ProfileManager.instance.Add(new Profile(ProfileNameBox.Text, VersionBox.SelectedItem as MinecraftVersion));
ProfileManager.instance.Add(new Profile(ProfileNameBox.Text, VersionBox.SelectedItem as MinecraftVersion, created: DateTime.Now));
}
}
}
2 changes: 1 addition & 1 deletion ZenovaLauncher/Pages/ProfilesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected void FilterProfileList()

protected void SortProfileList(Profile.ProfileSortType sortType)
{
string sortTypeString = sortType == Profile.ProfileSortType.ByLastPlayed ? "LastPlayed" : "Name";
string sortTypeString = sortType == Profile.ProfileSortType.ByLastPlayed ? "LastUsed" : "Name";
ListSortDirection sortDirection = sortType == Profile.ProfileSortType.ByLastPlayed ? ListSortDirection.Descending : ListSortDirection.Ascending;
if (ProfileListBox != null)
{
Expand Down
23 changes: 14 additions & 9 deletions ZenovaLauncher/Profiles/Profile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Diagnostics;

namespace ZenovaLauncher
{
Expand All @@ -20,31 +21,35 @@ public class Profile : NotifyPropertyChangedBase
return (item as Profile).Historical == true;
};

public Profile(string name, MinecraftVersion version, DateTime lastPlayed = default, ProfileType type = ProfileType.Custom)
public Profile(string name, MinecraftVersion version, DateTime lastUsed = default, DateTime created = default, ProfileType type = ProfileType.Custom)
{
Name = string.IsNullOrEmpty(name) ? "<unnamed profile>" : name;
Version = version;
LastPlayed = lastPlayed;
LastUsed = lastUsed;
Created = created;
Type = type;
}

public Profile(Profile profile) : this(profile.Name, profile.Version, profile.LastPlayed) { }
public Profile(Profile profile) : this(profile.Name, profile.Version, profile.LastUsed, profile.Created, profile.Type) { }

[JsonConstructor]
public Profile(string name, DateTime lastPlayed, ProfileType type, string versionId) :
this(name, VersionManager.instance.GetVersionFromString(versionId), lastPlayed, type)
public Profile(DateTime created, DateTime lastUsed, string versionId, string name, ProfileType type) :
this(name, VersionManager.instance.GetVersionFromString(versionId), lastUsed, created, type)
{ }

[JsonProperty]
public string Name { get; set; }
public DateTime Created { get; set; }
[JsonProperty]
public DateTime LastUsed { get; set; }
[JsonProperty]
public DateTime LastPlayed { get; set; }
public string VersionId => Version.InternalName;
[JsonProperty]
public string Name { get; set; }
[JsonProperty]
[JsonConverter(typeof(StringEnumConverter))]
public ProfileType Type { get; set; }
public MinecraftVersion Version { get; set; }
[JsonProperty]
public string VersionId => Version.InternalName;
public string Hash => Utils.ComputeHash(this);
public string VersionName => Version.Name;
public bool Beta => Version.Beta;
public bool Historical => Version.Historical;
Expand Down
2 changes: 1 addition & 1 deletion ZenovaLauncher/Profiles/ProfileLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void LaunchProfile(Profile p)
await Launch(p);
LaunchInfo = null;
LaunchedProfile.LastPlayed = DateTime.Now;
LaunchedProfile.LastUsed = DateTime.Now;
LaunchedProfile = null;
});
}
Expand Down
43 changes: 29 additions & 14 deletions ZenovaLauncher/Profiles/ProfileManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;

Expand All @@ -9,6 +11,7 @@ namespace ZenovaLauncher
public class ProfileManager : ObservableCollection<Profile>
{
public static ProfileManager instance;
private static JsonSerializerSettings camelCaseSerialization;

private readonly string _profilesFile = "profiles.json";
private readonly string _profilesDir;
Expand All @@ -18,6 +21,8 @@ public ProfileManager(string profileDir)
_profilesDir = profileDir;
}

public Profile SelectedProfile { get; set; }

public Profile LatestRelease
{
get
Expand Down Expand Up @@ -55,30 +60,39 @@ public void AddProfiles()
public void AddDefaultProfiles()
{
LatestRelease = new Profile("Latest release", VersionManager.instance.LatestRelease, type: Profile.ProfileType.LatestRelease);
LatestBeta = new Profile("Latest beta", VersionManager.instance.LatestBeta, type: Profile.ProfileType.LatestBeta); ;
LatestBeta = new Profile("Latest beta", VersionManager.instance.LatestBeta, type: Profile.ProfileType.LatestBeta);
SelectedProfile = this.First();
}

public void LoadProfiles()
{
//string[] profileFiles = Directory.GetFiles(_profilesDir, "*.json", SearchOption.AllDirectories);
//foreach (string file in profileFiles)
//{
camelCaseSerialization = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
if (File.Exists(Path.Combine(_profilesDir, _profilesFile)))
{
List<Profile> profileList = JsonConvert.DeserializeObject<List<Profile>>(File.ReadAllText(Path.Combine(_profilesDir, _profilesFile)));
foreach (Profile p in profileList)
Dictionary<string, Profile> profileList = JsonConvert.DeserializeObject<Dictionary<string, Profile>>(File.ReadAllText(Path.Combine(_profilesDir, _profilesFile)), camelCaseSerialization);
foreach (var p in profileList)
{
switch (p.Type)
if (p.Key == p.Value.Hash)
{
switch (p.Value.Type)
{
case Profile.ProfileType.Custom:
Add(p.Value);
break;
case Profile.ProfileType.LatestBeta:
LatestBeta = p.Value;
break;
case Profile.ProfileType.LatestRelease:
LatestRelease = p.Value;
break;
}
}
else
{
case Profile.ProfileType.Custom:
Add(p);
break;
case Profile.ProfileType.LatestBeta:
LatestBeta = p;
break;
case Profile.ProfileType.LatestRelease:
LatestRelease = p;
break;
Trace.WriteLine("Failed to load profile due to incorrect hash: " + p.Key);
}
}
}
Expand All @@ -90,7 +104,8 @@ public void SaveProfiles()
//DirectoryInfo di = new DirectoryInfo(_profilesDir);
//foreach (FileInfo file in di.EnumerateFiles()) file.Delete();
//foreach (DirectoryInfo dir in di.EnumerateDirectories()) dir.Delete(true);
File.WriteAllText(Path.Combine(_profilesDir, _profilesFile), JsonConvert.SerializeObject(this, Formatting.Indented));
Dictionary<string, Profile> profilesDic = this.ToDictionary(x => x.Hash, x => x);
File.WriteAllText(Path.Combine(_profilesDir, _profilesFile), JsonConvert.SerializeObject(profilesDic, Formatting.Indented, camelCaseSerialization));
}
}
}
29 changes: 28 additions & 1 deletion ZenovaLauncher/Utils/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System.Security.Principal;
using Newtonsoft.Json;
using System;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;

namespace ZenovaLauncher
{
Expand All @@ -12,5 +17,27 @@ public static bool IsElevated
.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid);
}
}

public static string ComputeHash(object objectToHash)
{
MD5 md5 = new MD5CryptoServiceProvider();
try
{
byte[] result = md5.ComputeHash(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(objectToHash)));

StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}

return sb.ToString();
}
catch (ArgumentNullException)
{
Console.WriteLine("Hash has not been generated.");
return null;
}
}
}
}

0 comments on commit 8a35e57

Please sign in to comment.