Skip to content

Commit ca2286d

Browse files
committed
Misc updates.
1 parent d1521f3 commit ca2286d

File tree

12 files changed

+375
-210
lines changed

12 files changed

+375
-210
lines changed

src/SAM.Core/Common/Enums/SAMExitCode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
public struct SAMExitCode
44
{
5+
public const int InvalidArguments = -8;
56
public const int SteamNotRunning = -7;
67
public const int DispatcherException = -6;
78
public const int AppDomainException = -5;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Net.Http;
2+
3+
namespace SAM.Core;
4+
5+
public static class NetworkHelper
6+
{
7+
private const string STEAM_STORE_URL = @"https://store.steampowered.com";
8+
9+
public static bool IsOnline()
10+
{
11+
try
12+
{
13+
using var client = new HttpClient();
14+
using var request = new HttpRequestMessage(HttpMethod.Get, STEAM_STORE_URL);
15+
using var response = client.Send(request);
16+
17+
return true;
18+
}
19+
catch
20+
{
21+
return false;
22+
}
23+
}
24+
}

src/SAM.Core/Common/Helpers/SAMHelper.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,34 @@ public static Process OpenManager(uint appId)
5959
{
6060
throw new FileNotFoundException($"Unable to start '{SAM_EXE}' because it does not exist.", SAM_EXE);
6161
}
62-
63-
var proc = Process.Start(SAM_EXE, appId.ToString());
62+
63+
string[] args = [ "manage", $"{appId}" ];
64+
var psi = new ProcessStartInfo(SAM_EXE, args);
65+
var proc = Process.Start(psi);
6466

6567
proc.SetActive();
6668

6769
return proc;
6870
}
71+
72+
public static Process UnlockAll(uint appId)
73+
{
74+
if (appId == default) throw new ArgumentException($"App id {appId} is not valid.", nameof(appId));
75+
76+
if (!File.Exists(SAM_EXE))
77+
{
78+
throw new FileNotFoundException($"Unable to start '{SAM_EXE}' because it does not exist.", SAM_EXE);
79+
}
80+
81+
string[] args = [ $"{appId}", "-u" ];
82+
var psi = new ProcessStartInfo(SAM_EXE, args)
83+
{
84+
CreateNoWindow = true
85+
};
86+
var proc = Process.Start(psi);
87+
88+
return proc;
89+
}
6990

7091
public static void CloseAllManagers()
7192
{

src/SAM.Core/Storage/Keys/CacheKey.cs

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,125 +6,124 @@
66
using log4net;
77
using SAM.Core.Extensions;
88

9-
namespace SAM.Core.Storage
9+
namespace SAM.Core.Storage;
10+
11+
// TODO: Add configurable cache expiration
12+
[DebuggerDisplay("{GetFullPath()}")]
13+
public class CacheKey : ICacheKey
1014
{
11-
// TODO: Add configurable cache expiration
12-
[DebuggerDisplay("{GetFullPath()}")]
13-
public class CacheKey : ICacheKey
14-
{
15-
private const string DEFAULT_EXTENSION = ".json";
15+
private const string DEFAULT_EXTENSION = ".json";
1616

17-
protected readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.ReflectedType ?? typeof(CacheKey));
17+
protected readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod()?.ReflectedType ?? typeof(CacheKey));
1818

19-
private string _fullPath;
19+
private string _fullPath;
2020

21-
public string Key { get; protected set; }
22-
public string FilePath { get; protected set; }
23-
/// <summary>
24-
/// The maximum number of days the cached item is valid for. By <see langword="default"/> (<see langword="null"/>) the cached item does not expire.
25-
/// </summary>
26-
public uint? DaysValid { get; internal set; }
27-
public bool HasExpiration => DaysValid.HasValue;
21+
public string Key { get; protected set; }
22+
public string FilePath { get; protected set; }
23+
/// <summary>
24+
/// The maximum number of days the cached item is valid for. By <see langword="default"/> (<see langword="null"/>) the cached item does not expire.
25+
/// </summary>
26+
public uint? DaysValid { get; internal set; }
27+
public bool HasExpiration => DaysValid.HasValue;
2828

29-
protected CacheKey()
30-
{
29+
protected CacheKey()
30+
{
3131

32-
}
32+
}
3333

34-
public CacheKey(object key, CacheKeyType type)
35-
{
36-
if (key == null) throw new ArgumentNullException(nameof(key));
34+
public CacheKey(object key, CacheKeyType type)
35+
{
36+
if (key == null) throw new ArgumentNullException(nameof(key));
3737

38-
SetKey(key);
38+
SetKey(key);
3939

40-
if (type == CacheKeyType.App)
41-
{
42-
throw new NotSupportedException(@$"{CacheKeyType.App} cache keys require an additional id parameter.");
43-
}
44-
45-
FilePath = type.GetDescription();
40+
if (type == CacheKeyType.App)
41+
{
42+
throw new NotSupportedException(@$"{CacheKeyType.App} cache keys require an additional id parameter.");
4643
}
44+
45+
FilePath = type.GetDescription();
46+
}
4747

48-
public CacheKey(object key, CacheKeyType type, uint daysValid)
49-
{
50-
if (key == null) throw new ArgumentNullException(nameof(key));
48+
public CacheKey(object key, CacheKeyType type, uint daysValid)
49+
{
50+
if (key == null) throw new ArgumentNullException(nameof(key));
5151

52-
SetKey(key);
52+
SetKey(key);
5353

54-
if (type == CacheKeyType.App)
55-
{
56-
throw new NotSupportedException(@$"{CacheKeyType.App} cache keys require an additional id parameter.");
57-
}
54+
if (type == CacheKeyType.App)
55+
{
56+
throw new NotSupportedException(@$"{CacheKeyType.App} cache keys require an additional id parameter.");
57+
}
5858

59-
DaysValid = daysValid;
59+
DaysValid = daysValid;
6060

61-
FilePath = type.GetDescription();
62-
}
61+
FilePath = type.GetDescription();
62+
}
6363

64-
public CacheKey(string fileName, object id, CacheKeyType type = CacheKeyType.Default, CacheKeySubType subType = CacheKeySubType.None)
65-
{
66-
if (string.IsNullOrWhiteSpace(fileName)) throw new ArgumentNullException(nameof(fileName));
64+
public CacheKey(string fileName, object id, CacheKeyType type = CacheKeyType.Default, CacheKeySubType subType = CacheKeySubType.None)
65+
{
66+
if (string.IsNullOrWhiteSpace(fileName)) throw new ArgumentNullException(nameof(fileName));
6767

68-
SetKey(fileName);
68+
SetKey(fileName);
6969

70-
if (type != CacheKeyType.App)
71-
{
72-
throw new NotSupportedException(@$"Only {CacheKeyType.App} cache keys support {nameof(id)}.");
73-
}
74-
75-
var path = new List<object>
76-
{
77-
type.GetDescription(),
78-
id
79-
};
80-
81-
// add subtype to path if set (currently only for app images)
82-
if (subType != CacheKeySubType.None)
83-
{
84-
path.Add(subType.GetDescription());
85-
}
86-
87-
FilePath = string.Join(Path.DirectorySeparatorChar, path);
70+
if (type != CacheKeyType.App)
71+
{
72+
throw new NotSupportedException(@$"Only {CacheKeyType.App} cache keys support {nameof(id)}.");
8873
}
89-
90-
public virtual string GetFullPath()
74+
75+
var path = new List<object>
9176
{
92-
if (_fullPath != null) return _fullPath;
77+
type.GetDescription(),
78+
id
79+
};
9380

94-
return _fullPath = Path.Combine(FilePath, Key);
81+
// add subtype to path if set (currently only for app images)
82+
if (subType != CacheKeySubType.None)
83+
{
84+
path.Add(subType.GetDescription());
9585
}
9686

97-
public static bool IsExpired(CacheKey key, string fileName)
98-
{
99-
var fi = new FileInfo(fileName);
87+
FilePath = string.Join(Path.DirectorySeparatorChar, path);
88+
}
10089

101-
return IsExpired(key, fi);
102-
}
90+
public virtual string GetFullPath()
91+
{
92+
if (_fullPath != null) return _fullPath;
10393

104-
public static bool IsExpired(CacheKey key, FileInfo fi)
105-
{
106-
// if there's no expiration, it's never expired
107-
if (!key.HasExpiration) return false;
94+
return _fullPath = Path.Combine(FilePath, Key);
95+
}
10896

109-
var cacheDate = fi.CreationTime;
110-
var cacheLimit = key.DaysValid!.Value;
111-
var expirationDate =cacheDate.AddDays(cacheLimit);
97+
public static bool IsExpired(CacheKey key, string fileName)
98+
{
99+
var fi = new FileInfo(fileName);
112100

113-
return DateTime.Now < expirationDate;
114-
}
101+
return IsExpired(key, fi);
102+
}
115103

116-
protected void SetKey(object key)
117-
{
118-
var fileName = key.ToString();
104+
public static bool IsExpired(CacheKey key, FileInfo fi)
105+
{
106+
// if there's no expiration, it's never expired
107+
if (!key.HasExpiration) return false;
108+
109+
var cacheDate = fi.CreationTime;
110+
var cacheLimit = key.DaysValid!.Value;
111+
var expirationDate =cacheDate.AddDays(cacheLimit);
119112

120-
var hasExtension = Path.HasExtension(fileName);
121-
if (!hasExtension)
122-
{
123-
fileName = Path.ChangeExtension(fileName, DEFAULT_EXTENSION);
124-
}
113+
return DateTime.Now < expirationDate;
114+
}
115+
116+
protected void SetKey(object key)
117+
{
118+
var fileName = key.ToString();
125119

126-
Key = fileName;
120+
var hasExtension = Path.HasExtension(fileName);
121+
if (!hasExtension)
122+
{
123+
fileName = Path.ChangeExtension(fileName, DEFAULT_EXTENSION);
127124
}
128125

126+
Key = fileName;
129127
}
128+
130129
}
Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1-
namespace SAM.Core.Storage
2-
{
3-
public static class CacheKeys
4-
{
5-
private const uint GAMES_XML_EXPIRATION = 2;
1+
namespace SAM.Core.Storage;
62

7-
public static ICacheKey AppList => new CacheKey(@"app_list.json", CacheKeyType.Data);
8-
public static ICacheKey CheckedAppList => new CacheKey(@"checked_apps.json", CacheKeyType.Default);
9-
public static ICacheKey UserTheme => new CacheKey(@"default", CacheKeyType.Theme);
10-
public static ICacheKey UserLibrary => new CacheKey(@"user_apps.json", CacheKeyType.Default);
11-
public static ICacheKey UserSettings => new CacheKey(@"user", CacheKeyType.Settings);
12-
public static ICacheKey Games => new CacheKey(@"games.xml", CacheKeyType.Data, GAMES_XML_EXPIRATION);
3+
public static class CacheKeys
4+
{
5+
private const uint DEFAULT_GAMES_XML_EXPIRATION = 2;
6+
7+
public static ICacheKey AppList => new CacheKey(@"app_list.json", CacheKeyType.Data);
8+
public static ICacheKey CheckedAppList => new CacheKey(@"checked_apps.json", CacheKeyType.Default);
9+
public static ICacheKey UserTheme => new CacheKey(@"default", CacheKeyType.Theme);
10+
public static ICacheKey UserLibrary => new CacheKey(@"user_apps.json", CacheKeyType.Default);
11+
public static ICacheKey UserSettings => new CacheKey(@"user", CacheKeyType.Settings);
12+
public static ICacheKey Games => new CacheKey(@"games.xml", CacheKeyType.Data, GetGamesXmlExpiration());
1313

14-
public static ICacheKey CreateAppCacheKey(uint appid)
15-
{
16-
var key = new CacheKey($"{appid}.json", appid, CacheKeyType.App);
17-
return key;
18-
}
14+
public static ICacheKey CreateAppCacheKey(uint appid)
15+
{
16+
var key = new CacheKey($"{appid}.json", appid, CacheKeyType.App);
17+
return key;
18+
}
1919

20-
public static ICacheKey CreateAppSettingsCacheKey(uint appid)
21-
{
22-
var key = new CacheKey($"{appid}_settings.json", appid, CacheKeyType.App);
23-
return key;
24-
}
20+
public static ICacheKey CreateAppSettingsCacheKey(uint appid)
21+
{
22+
var key = new CacheKey($"{appid}_settings.json", appid, CacheKeyType.App);
23+
return key;
24+
}
25+
26+
public static ICacheKey CreateAppImageCacheKey(uint appid, string imageFileName)
27+
{
28+
var key = new CacheKey(imageFileName, appid, CacheKeyType.App, CacheKeySubType.Image);
29+
return key;
30+
}
2531

26-
public static ICacheKey CreateAppImageCacheKey(uint appid, string imageFileName)
32+
private static uint GetGamesXmlExpiration()
33+
{
34+
// TODO: add cache expiration override to all expiring cached items
35+
if (NetworkHelper.IsOnline())
2736
{
28-
var key = new CacheKey(imageFileName, appid, CacheKeyType.App, CacheKeySubType.Image);
29-
return key;
37+
return DEFAULT_GAMES_XML_EXPIRATION;
3038
}
39+
return ushort.MaxValue;
3140
}
41+
3242
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
namespace SAM.Core.Storage
1+
namespace SAM.Core.Storage;
2+
3+
public interface ICacheKey
24
{
3-
public interface ICacheKey
4-
{
5-
string Key { get; }
6-
string FilePath { get; }
5+
string Key { get; }
6+
string FilePath { get; }
77

8-
uint? DaysValid { get; }
9-
bool HasExpiration { get; }
8+
uint? DaysValid { get; }
9+
bool HasExpiration { get; }
1010

11-
string GetFullPath();
12-
}
11+
string GetFullPath();
1312
}

0 commit comments

Comments
 (0)