using RustTools.Core.Helpers; using Windows.Storage; using Windows.Storage.Streams; namespace RustTools.Helpers; // Use these extension methods to store and retrieve local and roaming app data // More details regarding storing and retrieving app data at https://docs.microsoft.com/windows/apps/design/app-settings/store-and-retrieve-app-data public static class SettingsStorageExtensions { private const string FileExtension = ".json"; public static bool IsRoamingStorageAvailable(this ApplicationData appData) { return appData.RoamingStorageQuota == 0; } public static async Task SaveAsync(this StorageFolder folder, string name, T content) { var file = await folder.CreateFileAsync(GetFileName(name), CreationCollisionOption.ReplaceExisting); var fileContent = await Json.StringifyAsync(content); await FileIO.WriteTextAsync(file, fileContent); } public static async Task ReadAsync(this StorageFolder folder, string name) { if (!File.Exists(Path.Combine(folder.Path, GetFileName(name)))) { return default; } var file = await folder.GetFileAsync($"{name}.json"); var fileContent = await FileIO.ReadTextAsync(file); return await Json.ToObjectAsync(fileContent); } public static async Task SaveAsync(this ApplicationDataContainer settings, string key, T value) { settings.SaveString(key, await Json.StringifyAsync(value)); } public static void SaveString(this ApplicationDataContainer settings, string key, string value) { settings.Values[key] = value; } public static async Task ReadAsync(this ApplicationDataContainer settings, string key) { object? obj; if (settings.Values.TryGetValue(key, out obj)) { return await Json.ToObjectAsync((string)obj); } return default; } public static async Task SaveFileAsync(this StorageFolder folder, byte[] content, string fileName, CreationCollisionOption options = CreationCollisionOption.ReplaceExisting) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("File name is null or empty. Specify a valid file name", nameof(fileName)); } var storageFile = await folder.CreateFileAsync(fileName, options); await FileIO.WriteBytesAsync(storageFile, content); return storageFile; } public static async Task ReadFileAsync(this StorageFolder folder, string fileName) { var item = await folder.TryGetItemAsync(fileName).AsTask().ConfigureAwait(false); if ((item != null) && item.IsOfType(StorageItemTypes.File)) { var storageFile = await folder.GetFileAsync(fileName); var content = await storageFile.ReadBytesAsync(); return content; } return null; } public static async Task ReadBytesAsync(this StorageFile file) { if (file != null) { using IRandomAccessStream stream = await file.OpenReadAsync(); using var reader = new DataReader(stream.GetInputStreamAt(0)); await reader.LoadAsync((uint)stream.Size); var bytes = new byte[stream.Size]; reader.ReadBytes(bytes); return bytes; } return null; } private static string GetFileName(string name) { return string.Concat(name, FileExtension); } }