Skip to content

Commit 7c33b98

Browse files
committed
Merge branch 'v3.8.0-line' into release
2 parents 18c61df + 4742117 commit 7c33b98

File tree

7 files changed

+68
-49
lines changed

7 files changed

+68
-49
lines changed

Assets/Editor/Tests/ValidateLicenseCommandTest.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NUnit.Framework;
55
using PatchKit.Api;
66
using PatchKit.Api.Models.Main;
7+
using PatchKit.Unity.Patcher.AppData.Local;
78
using PatchKit.Unity.Patcher.AppData.Remote;
89
using PatchKit.Unity.Patcher.AppUpdater.Commands;
910
using PatchKit.Unity.Patcher.Cancellation;
@@ -30,6 +31,15 @@ public void Execute_CachesKeyAndKeySecret()
3031
const string key = "this-key-should-be-cached";
3132
const string keySecret = "this-key-secret-should-be-cached";
3233

34+
var remoteMetaData = Substitute.For<IRemoteMetaData>();
35+
remoteMetaData.GetAppInfo().Returns(new App()
36+
{
37+
UseKeys = true
38+
});
39+
remoteMetaData.GetKeySecret(key, Arg.Any<string>()).Returns(keySecret);
40+
41+
var localMetaData = Substitute.For<ILocalMetaData>();
42+
3343
for (int i = 0; i < 2; i++)
3444
{
3545
var licenseDialog = Substitute.For<ILicenseDialog>();
@@ -39,28 +49,22 @@ public void Execute_CachesKeyAndKeySecret()
3949
Type = LicenseDialogResultType.Confirmed
4050
});
4151

42-
var remoteMetaData = Substitute.For<IRemoteMetaData>();
43-
remoteMetaData.GetAppInfo().Returns(new App()
44-
{
45-
UseKeys = true
46-
});
47-
remoteMetaData.GetKeySecret(key, Arg.Any<string>()).Returns(keySecret);
48-
49-
var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, _cache, _logger);
52+
var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, localMetaData, _cache, _logger);
5053
command.Prepare(_statusMonitor);
5154
command.Execute(CancellationToken.Empty);
5255

5356
if (i == 0)
5457
{
5558
licenseDialog.Received(1).Display(Arg.Any<LicenseDialogMessageType>());
56-
Assert.IsTrue(_cache.Dictionary.ContainsValue(key));
59+
localMetaData.Received().SetProductKey(key);
60+
localMetaData.GetProductKey().Returns(key);
5761
Assert.IsTrue(_cache.Dictionary.ContainsValue(keySecret));
5862
}
5963
else
6064
{
6165
licenseDialog.Received(1).SetKey(key);
6266
licenseDialog.DidNotReceive().Display(Arg.Any<LicenseDialogMessageType>());
63-
Assert.IsTrue(_cache.Dictionary.ContainsValue(key));
67+
localMetaData.Received().SetProductKey(key);
6468
Assert.IsTrue(_cache.Dictionary.ContainsValue(keySecret));
6569
}
6670
}
@@ -77,7 +81,9 @@ public void Execute_ProperlyHandlesSitauationWhenKeysAreNotUsed()
7781
UseKeys = false
7882
});
7983

80-
var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, _cache, _logger);
84+
var localMetaData = Substitute.For<ILocalMetaData>();
85+
86+
var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, localMetaData, _cache, _logger);
8187
command.Prepare(_statusMonitor);
8288
command.Execute(CancellationToken.Empty);
8389

@@ -106,6 +112,8 @@ public void Execute_DisplaysDialogMessageForApiError(int statusCode, LicenseDial
106112
{
107113
UseKeys = true
108114
});
115+
116+
var localMetaData = Substitute.For<ILocalMetaData>();
109117

110118
bool firstAttempt = true;
111119

@@ -120,7 +128,7 @@ public void Execute_DisplaysDialogMessageForApiError(int statusCode, LicenseDial
120128
throw new ApiResponseException(statusCode);
121129
});
122130

123-
var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, _cache, _logger);
131+
var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, localMetaData, _cache, _logger);
124132
command.Prepare(_statusMonitor);
125133
command.Execute(CancellationToken.Empty);
126134

@@ -162,7 +170,9 @@ public void Execute_DisplaysProperDialogMessageForConnectionError()
162170
throw new ApiConnectionException(new List<Exception>(), new List<Exception>());
163171
});
164172

165-
var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, _cache, _logger);
173+
var localMetaData = Substitute.For<ILocalMetaData>();
174+
175+
var command = new ValidateLicenseCommand(licenseDialog, remoteMetaData, localMetaData, _cache, _logger);
166176
command.Prepare(_statusMonitor);
167177
command.Execute(CancellationToken.Empty);
168178

Assets/PatchKit Patcher/Scripts/AppData/Local/ILocalMetaData.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,9 @@ public interface ILocalMetaData
4040
/// Returns the file path of JSON file the data was loaded from.
4141
/// </summary>
4242
string GetFilePath();
43+
44+
void SetProductKey(string productKey);
45+
46+
string GetProductKey();
4347
}
4448
}

Assets/PatchKit Patcher/Scripts/AppData/Local/LocalMetaData.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace PatchKit.Unity.Patcher.AppData.Local
1515
/// <seealso cref="ILocalMetaData" />
1616
public class LocalMetaData : ILocalMetaData
1717
{
18+
private const string DeprecatedCachePatchKitKey = "patchkit-key";
19+
1820
/// <summary>
1921
/// Data structure stored in file.
2022
/// </summary>
@@ -114,6 +116,18 @@ public string GetFilePath()
114116
return _filePath;
115117
}
116118

119+
public void SetProductKey(string productKey)
120+
{
121+
_data.ProductKey = productKey;
122+
123+
SaveData();
124+
}
125+
126+
public string GetProductKey()
127+
{
128+
return _data.ProductKey;
129+
}
130+
117131
private void SaveData()
118132
{
119133
DebugLogger.Log("Saving.");
@@ -165,10 +179,10 @@ private bool TryLoadDataFromFile()
165179

166180
#if UNITY_5_3_OR_NEWER // LEGACY: fill productKey from unity prefs and remove it
167181
if (string.IsNullOrEmpty(_data.ProductKey)
168-
&& UnityEngine.PlayerPrefs.HasKey(ValidateLicenseCommand.CachePatchKitKey))
182+
&& UnityEngine.PlayerPrefs.HasKey(DeprecatedCachePatchKitKey))
169183
{
170-
_data.ProductKey = UnityEngine.PlayerPrefs.GetString(ValidateLicenseCommand.CachePatchKitKey);
171-
UnityEngine.PlayerPrefs.DeleteKey(ValidateLicenseCommand.CachePatchKitKey);
184+
_data.ProductKey = UnityEngine.PlayerPrefs.GetString(DeprecatedCachePatchKitKey);
185+
UnityEngine.PlayerPrefs.DeleteKey(DeprecatedCachePatchKitKey);
172186
}
173187
#endif
174188
return true;

Assets/PatchKit Patcher/Scripts/AppData/Remote/WebRequestWraps/WrapRequest.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ private IEnumerator JobCoroutine(string url)
4545

4646
try
4747
{
48-
// HACK: because WWW is broken and sometimes just does not return STATUS in responseHeaders we are returning status code 200 (we can assume that status code is not an error since www.error is null or empty).
4948
if (!www.responseHeaders.ContainsKey("STATUS"))
5049
{
51-
DebugLogger.LogWarning("Response headers doesn't contain status information. Since WWW marks response as one without errors, status code is set to 200 (OK).");
52-
_statusCode = 200;
50+
// Based on tests, if response doesn't contain status it has probably timed out.
51+
_wasTimeout = true;
5352
}
5453
else
5554
{
@@ -63,10 +62,8 @@ private IEnumerator JobCoroutine(string url)
6362
}
6463
else
6564
{
66-
// HACK: Again, we can't parse the status code (it might be in some different format) - so we simply set it to 200.
67-
DebugLogger.LogWarning(
68-
"Unable to parse status code. Since WWW marks response as one without errors, status code is set to 200 (OK).");
69-
_statusCode = 200;
65+
// Based on tests, if response contains invalid status it has probably timed out.
66+
_wasTimeout = true;
7067
}
7168
}
7269

Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/AppUpdaterCommandFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public IUninstallCommand CreateUninstallCommand(AppUpdaterContext context)
9494

9595
public IValidateLicenseCommand CreateValidateLicenseCommand(AppUpdaterContext context)
9696
{
97-
return new ValidateLicenseCommand(context.LicenseDialog, context.App.RemoteMetaData, new UnityCache(), PatcherLogManager.DefaultLogger);
97+
return new ValidateLicenseCommand(context.LicenseDialog, context.App.RemoteMetaData, context.App.LocalMetaData, new UnityCache(), PatcherLogManager.DefaultLogger);
9898
}
9999

100100
public ICheckDiskSpace CreateCheckDiskSpaceCommandForDiff(int versionId, AppUpdaterContext context)

Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/ValidateLicenseCommand.cs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,33 @@
55
using PatchKit.Unity.Patcher.AppData.Local;
66
using PatchKit.Unity.Patcher.AppData.Remote;
77
using PatchKit.Unity.Patcher.Cancellation;
8-
using PatchKit.Unity.Patcher.Debug;
98
using PatchKit.Unity.Patcher.Status;
109
using PatchKit.Unity.Patcher.UI.Dialogs;
1110

1211
namespace PatchKit.Unity.Patcher.AppUpdater.Commands
1312
{
1413
public class ValidateLicenseCommand : BaseAppUpdaterCommand, IValidateLicenseCommand
1514
{
16-
public const string CachePatchKitKey = "patchkit-key";
17-
public const string CachePatchKitKeySecret = "patchkit-keysecret-";
15+
private const string CachePatchKitKeySecret = "patchkit-keysecret-";
1816

1917
[NotNull] private readonly ILicenseDialog _licenseDialog;
2018
[NotNull] private readonly IRemoteMetaData _remoteMetaData;
19+
[NotNull] private readonly ILocalMetaData _localMetaData;
2120
[NotNull] private readonly ICache _cache;
2221
[NotNull] private readonly ILogger _logger;
2322

2423
public ValidateLicenseCommand([NotNull] ILicenseDialog licenseDialog, [NotNull] IRemoteMetaData remoteMetaData,
25-
[NotNull] ICache cache, [NotNull] ILogger logger)
24+
[NotNull] ILocalMetaData localMetaData, [NotNull] ICache cache, [NotNull] ILogger logger)
2625
{
27-
if (licenseDialog == null)
28-
{
29-
throw new ArgumentNullException("licenseDialog");
30-
}
31-
if (remoteMetaData == null)
32-
{
33-
throw new ArgumentNullException("remoteMetaData");
34-
}
35-
if (cache == null)
36-
{
37-
throw new ArgumentNullException("cache");
38-
}
39-
if (logger == null)
40-
{
41-
throw new ArgumentNullException("logger");
42-
}
26+
if (licenseDialog == null) throw new ArgumentNullException("licenseDialog");
27+
if (remoteMetaData == null) throw new ArgumentNullException("remoteMetaData");
28+
if (localMetaData == null) throw new ArgumentNullException("localMetaData");
29+
if (cache == null) throw new ArgumentNullException("cache");
30+
if (logger == null) throw new ArgumentNullException("logger");
4331

4432
_licenseDialog = licenseDialog;
4533
_remoteMetaData = remoteMetaData;
34+
_localMetaData = localMetaData;
4635
_cache = cache;
4736
_logger = logger;
4837
}
@@ -211,23 +200,23 @@ private void HandleApiError(ref LicenseDialogMessageType messageType, bool isUsi
211200
}
212201
}
213202

214-
public override void Prepare(IStatusMonitor statusMonitor)
203+
public override void Prepare([NotNull] IStatusMonitor statusMonitor)
215204
{
216205
base.Prepare(statusMonitor);
217-
218-
Checks.ArgumentNotNull(statusMonitor, "statusMonitor");
206+
207+
if (statusMonitor == null) throw new ArgumentNullException("statusMonitor");
219208
}
220209

221210
public string KeySecret { get; private set; }
222211

223212
private void SetCachedKey(string value)
224213
{
225-
_cache.SetValue(CachePatchKitKey, value);
214+
_localMetaData.SetProductKey(value);
226215
}
227216

228217
private string GetCachedKey()
229218
{
230-
return _cache.GetValue(CachePatchKitKey);
219+
return _localMetaData.GetProductKey();
231220
}
232221

233222
private void SetCachedKeySecret(string key, string value)

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [3.8.1]
8+
### Fixed
9+
- Block license key cache sharing between all non-custom patchers
10+
- Properly handle situation when WWW response doesn't contain status header - previously it was assumed to be 200 (OK), right now response is marked as timed out
11+
712
## [3.8.0]
813
### Added
914
- Patcher now keeps track of keeping only one it's instance running

0 commit comments

Comments
 (0)