Skip to content

Commit

Permalink
Save and Load Profiles through JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
TrinityDevelopers committed Apr 10, 2020
1 parent 5f16fbf commit 6e50434
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
14 changes: 13 additions & 1 deletion ZenovaLauncher/Profiles/Profile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
Expand All @@ -7,6 +9,7 @@

namespace ZenovaLauncher
{
[JsonObject(MemberSerialization.OptIn)]
public class Profile
{
public static Predicate<object> releaseFilter = (object item) =>
Expand All @@ -32,10 +35,19 @@ public Profile(string name, MinecraftVersion version, DateTime lastPlayed = defa

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

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

[JsonProperty]
public string Name { get; set; }
[JsonProperty]
public DateTime LastPlayed { get; set; }
[JsonProperty]
[JsonConverter(typeof(StringEnumConverter))]
public ProfileType Type { get; set; }
public MinecraftVersion Version { get; set; }
[JsonProperty]
public string VersionName { get { return Version.Name; } }
public bool Beta { get { return Version.Beta; } }
public bool Historical { get { return Version.Historical; } }
Expand Down
18 changes: 15 additions & 3 deletions ZenovaLauncher/Profiles/ProfileManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -21,7 +22,8 @@ public ProfileManager(string cacheFile)

public void AddProfiles()
{
AddDefaultProfiles();
if(!LoadProfiles())
AddDefaultProfiles();
}

public void AddDefaultProfiles()
Expand All @@ -30,9 +32,19 @@ public void AddDefaultProfiles()
Add(new Profile("Latest beta", VersionManager.instance.LatestBeta, type: Profile.ProfileType.LatestBeta));
}

public void SaveProfiles()
public bool LoadProfiles()
{
if (!File.Exists(_cacheFile))
return false;
List<Profile> profileList = JsonConvert.DeserializeObject<List<Profile>>(File.ReadAllText(_cacheFile));
foreach (Profile p in profileList)
Add(p);
return true;
}

public void SaveProfiles()
{
File.WriteAllText(_cacheFile, JsonConvert.SerializeObject(this));
}
}
}
5 changes: 5 additions & 0 deletions ZenovaLauncher/Profiles/VersionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public VersionManager(string cacheFile)
_cacheFile = cacheFile;
}

public MinecraftVersion GetVersionFromString(string versionName)
{
return this.SingleOrDefault(v => v.Name == versionName);
}

public MinecraftVersion LatestRelease
{
get
Expand Down

0 comments on commit 6e50434

Please sign in to comment.