From 920204cf45286e5837f1b3cbcdb5e30d03c67b3d Mon Sep 17 00:00:00 2001 From: Tobias Pott Date: Fri, 1 Mar 2019 13:32:05 +0100 Subject: [PATCH 1/2] Changed condition in StorageController.LoadAsync method to check for null reference prior to create new StorageDictionary instance and trying loading its data from disk Changed file write and read methods in StorageManager class to use unified Unicode encoding Changed use of VolumeSeparatorChar to DirectorySeparatorChar as it is used in context of creating a relative folder insider the local app data special folder Changed ParseClient.Initialize behaviour to use MetadataBasedStorageConfiguration.NoCompanyInferred as default configuration of no other was set (did override any existing with the old switch statement) Added subclass registration of ParseInstallation class which caused an error with the other changes made when reading and parsing local cached information to an ParseInstallation instance at runtime. --- .../Internal/Storage/Controller/StorageController.cs | 9 ++++++++- Parse/Internal/Utilities/StorageManager.cs | 12 ++++++++---- Parse/Public/ParseClient.cs | 5 +++-- Parse/Public/ParseInstallation.cs | 2 +- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Parse/Internal/Storage/Controller/StorageController.cs b/Parse/Internal/Storage/Controller/StorageController.cs index f5619882..9a159d8b 100644 --- a/Parse/Internal/Storage/Controller/StorageController.cs +++ b/Parse/Internal/Storage/Controller/StorageController.cs @@ -156,7 +156,14 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() /// Loads a settings dictionary from the file wrapped by . /// /// A storage dictionary containing the deserialized content of the storage file targeted by the instance - public Task> LoadAsync() => Queue.Enqueue(toAwait => toAwait.ContinueWith(_ => Task.FromResult((IStorageDictionary) Storage) ?? (Storage = new StorageDictionary(File)).LoadAsync().OnSuccess(__ => Storage as IStorageDictionary)).Unwrap(), CancellationToken.None); + public Task> LoadAsync() + { + // check if storage dictionary is already created from the controllers file (create if not) + if (Storage == null) + Storage = new StorageDictionary(File); + // load storage dictionary content async and return the resulting dictionary type + return Queue.Enqueue(toAwait => toAwait.ContinueWith(_ => Storage.LoadAsync().OnSuccess(__ => Storage as IStorageDictionary)).Unwrap(), CancellationToken.None); + } /// /// diff --git a/Parse/Internal/Utilities/StorageManager.cs b/Parse/Internal/Utilities/StorageManager.cs index 4843057e..a921c4fd 100644 --- a/Parse/Internal/Utilities/StorageManager.cs +++ b/Parse/Internal/Utilities/StorageManager.cs @@ -31,7 +31,10 @@ internal static class StorageManager public static async Task WriteToAsync(this FileInfo file, string content) { using (FileStream stream = new FileStream(Path.GetFullPath(file.FullName), FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan | FileOptions.Asynchronous)) - await stream.WriteAsync(Encoding.Unicode.GetBytes(content), 0, content.Length * 2 /* UTF-16, so two bytes per character of length. */); + { + byte[] data = Encoding.Unicode.GetBytes(content); + await stream.WriteAsync(data, 0, data.Length); + } } /// @@ -41,7 +44,7 @@ public static async Task WriteToAsync(this FileInfo file, string content) /// A task that should contain the little-endian 16-bit character string (UTF-16) extracted from the if the read completes successfully public static async Task ReadAllTextAsync(this FileInfo file) { - using (StreamReader reader = file.OpenText()) + using (StreamReader reader = new StreamReader(file.OpenRead(), Encoding.Unicode)) return await reader.ReadToEndAsync(); } @@ -72,14 +75,15 @@ public static FileInfo GetWrapperForRelativePersistentStorageFilePath(string pat { path = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), path)); - Directory.CreateDirectory(path.Substring(0, path.LastIndexOf(Path.VolumeSeparatorChar))); + Directory.CreateDirectory(path.Substring(0, path.LastIndexOf(Path.DirectorySeparatorChar))); return new FileInfo(path); } public static async Task TransferAsync(string originFilePath, string targetFilePath) { if (!String.IsNullOrWhiteSpace(originFilePath) && !String.IsNullOrWhiteSpace(targetFilePath) && new FileInfo(originFilePath) is FileInfo originFile && originFile.Exists && new FileInfo(targetFilePath) is FileInfo targetFile) - using (StreamWriter writer = targetFile.CreateText()) using (StreamReader reader = originFile.OpenText()) + using (StreamWriter writer = new StreamWriter(targetFile.OpenWrite(), Encoding.Unicode)) + using (StreamReader reader = new StreamReader(originFile.OpenRead(), Encoding.Unicode)) await writer.WriteAsync(await reader.ReadToEndAsync()); } } diff --git a/Parse/Public/ParseClient.cs b/Parse/Public/ParseClient.cs index 24ba5061..5cbca097 100644 --- a/Parse/Public/ParseClient.cs +++ b/Parse/Public/ParseClient.cs @@ -251,10 +251,10 @@ public static void Initialize(Configuration configuration) switch (configuration.StorageConfiguration) { - case IStorageController controller when !(controller is null): + case null: + configuration.StorageConfiguration = Configuration.MetadataBasedStorageConfiguration.NoCompanyInferred; break; default: - configuration.StorageConfiguration = Configuration.MetadataBasedStorageConfiguration.NoCompanyInferred; break; } @@ -263,6 +263,7 @@ public static void Initialize(Configuration configuration) ParseObject.RegisterSubclass(); ParseObject.RegisterSubclass(); ParseObject.RegisterSubclass(); + ParseObject.RegisterSubclass(); ParseModuleController.Instance.ParseDidInitialize(); } diff --git a/Parse/Public/ParseInstallation.cs b/Parse/Public/ParseInstallation.cs index 4622e7c2..d4f33cdc 100644 --- a/Parse/Public/ParseInstallation.cs +++ b/Parse/Public/ParseInstallation.cs @@ -284,7 +284,7 @@ protected override Task SaveAsync(Task toAwait, CancellationToken cancellationTo return base.SaveAsync(toAwait, cancellationToken); }).Unwrap().OnSuccess(_ => { - if (CurrentInstallationController.IsCurrent(this)) + if (!CurrentInstallationController.IsCurrent(this)) { return Task.FromResult(0); } From 17f79e9a0fdead61fcce1b2f35438d0f999e95fb Mon Sep 17 00:00:00 2001 From: Tobias Pott Date: Thu, 29 Aug 2019 13:33:59 +0200 Subject: [PATCH 2/2] Fixed negation not required (slipped in somehow) --- Parse/Public/ParseInstallation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parse/Public/ParseInstallation.cs b/Parse/Public/ParseInstallation.cs index d4f33cdc..4622e7c2 100644 --- a/Parse/Public/ParseInstallation.cs +++ b/Parse/Public/ParseInstallation.cs @@ -284,7 +284,7 @@ protected override Task SaveAsync(Task toAwait, CancellationToken cancellationTo return base.SaveAsync(toAwait, cancellationToken); }).Unwrap().OnSuccess(_ => { - if (!CurrentInstallationController.IsCurrent(this)) + if (CurrentInstallationController.IsCurrent(this)) { return Task.FromResult(0); }