Skip to content

Commit

Permalink
Merge #3543 Play Time
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Apr 11, 2022
2 parents 39821d2 + 056b6c4 commit 20f663b
Show file tree
Hide file tree
Showing 22 changed files with 794 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file.
- [GUI] Add Release Date column to GUI modinfo versions list (#3481 by: HebaruSan; reviewed: DasSkelett)
- [Multiple] Global install filters (#3458 by: HebaruSan; reviewed: DasSkelett)
- [GUI] Negated search for name, desc, author, lang, relationships, tags (#3460 by: HebaruSan; reviewed: DasSkelett)
- [GUI] Play Time (#3543 by: PizzaRules668; reviewed: HebaruSan)

### Bugfixes

Expand Down
12 changes: 12 additions & 0 deletions Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public static void RemoveWhere<K, V>(
}
}
}

/// <summary>
/// Sum a sequence of TimeSpans.
/// Mysteriously not defined standardly.
/// </summary>
/// <param name="source">Sequence of TimeSpans to sum</param>
/// <returns>
/// Sum of the TimeSpans
/// </returns>
public static TimeSpan Sum(this IEnumerable<TimeSpan> source)
=> source.Aggregate(TimeSpan.Zero,
(a, b) => a + b);
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions Core/GameInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class GameInstance : IEquatable<GameInstance>
private GameVersion version;
private List<GameVersion> _compatibleVersions = new List<GameVersion>();

public TimeLog playTime;

public string Name { get; set; }
/// <summary>
/// Returns a file system safe version of the instance name that can be used within file names.
Expand Down Expand Up @@ -111,6 +113,8 @@ private void SetupCkanDirectories(bool scan = true)
}
}

playTime = TimeLog.Load(TimeLog.GetPath(CkanDir())) ?? new TimeLog();

if (!Directory.Exists(InstallHistoryDir()))
{
User.RaiseMessage("Creating {0}", InstallHistoryDir());
Expand Down
65 changes: 65 additions & 0 deletions Core/Types/TimeLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.IO;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CKAN
{
[JsonObject(MemberSerialization.OptIn)]
public class TimeLog
{
[JsonProperty("time", NullValueHandling = NullValueHandling.Ignore)]
public TimeSpan Time;

public static string GetPath(string dir)
{
return Path.Combine(dir, filename);
}

public static TimeLog Load(string path)
{
try
{
return JsonConvert.DeserializeObject<TimeLog>(File.ReadAllText(path));
}
catch (FileNotFoundException)
{
return null;
}
}

public void Save(string path)
{
File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented));
}

public override String ToString()
{
return Time.TotalHours.ToString("N1");
}

private Stopwatch playTime = new Stopwatch();

public void Start()
{
playTime.Restart();
}

public void Stop(string dir)
{
if (playTime.IsRunning)
{
playTime.Stop();
Time += playTime.Elapsed;
Save(GetPath(dir));
}
}

private const string filename = "playtime.json";
}
}
5 changes: 5 additions & 0 deletions Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ private static void _CopyDirectory(string sourceDirPath, string destDirPath, boo
continue;
}

else if (file.Name == "playtime.json")
{
continue;
}

string temppath = Path.Combine(destDirPath, file.Name);
file_transaction.Copy(file.FullName, temppath, false);
}
Expand Down
20 changes: 18 additions & 2 deletions GUI/CKAN-GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Transactions" />
<Reference Include="System.Windows.Forms" />
Expand Down Expand Up @@ -229,6 +230,12 @@
<Compile Include="Controls\ManageMods.Designer.cs">
<DependentUpon>ManageMods.cs</DependentUpon>
</Compile>
<Compile Include="Controls\PlayTime.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Controls\PlayTime.Designer.cs">
<DependentUpon>PlayTime.cs</DependentUpon>
</Compile>
<Compile Include="Controls\ThemedTabControl.cs">
<SubType>Component</SubType>
</Compile>
Expand All @@ -239,7 +246,7 @@
<SubType>Component</SubType>
</Compile>
<Compile Include="Controls\TriStateToggle.cs">
<SubType>Component</SubType>
<SubType>UserControl</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -293,6 +300,9 @@
<Compile Include="Main\MainTabControl.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Main\MainTime.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Main\MainTrayIcon.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -677,6 +687,12 @@
<EmbeddedResource Include="Controls\ModInfo.resx">
<DependentUpon>ModInfo.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Controls\PlayTime.resx">
<DependentUpon>PlayTime.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Localization\fr-FR\PlayTime.fr-FR.resx">
<DependentUpon>..\..\Controls\PlayTime.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Controls\ManageMods.resx">
<DependentUpon>ManageMods.cs</DependentUpon>
</EmbeddedResource>
Expand Down Expand Up @@ -1004,4 +1020,4 @@
<Exec Command="powershell ../build.ps1 Generate-GlobalAssemblyVersionInfo" Condition="!Exists('../_build/meta/GlobalAssemblyVersionInfo.cs') And '$(OS)' == 'Windows_NT'" />
<Exec Command="sh ../build Generate-GlobalAssemblyVersionInfo" Condition="!Exists('../_build/meta/GlobalAssemblyVersionInfo.cs') And '$(OS)' == 'Unix'" />
</Target>
</Project>
</Project>
156 changes: 156 additions & 0 deletions GUI/Controls/PlayTime.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 20f663b

Please sign in to comment.