Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play Time #3543

Merged
merged 2 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
CSutter5 marked this conversation as resolved.
Show resolved Hide resolved
</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