using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using System.IO; using Windows.Storage; namespace DrumKit { static class DataManager { public static AppSettings Settings { get; private set; } /// /// Checks if this is the first time application was launched. /// public static async Task IsFirstLaunch() { // See if 'installed.xml' exists var folder = ApplicationData.Current.RoamingFolder; var files = await folder.GetFilesAsync(); return files.Count(x => x.Name == "installed.xml") == 0; } /// /// Copies the content of the source folder into the destination folder recursively. /// //private static async Task CopyFolder(StorageFolder source, StorageFolder dest) //{ // // Copy folders recursively // var folders = await source.GetFoldersAsync(); // foreach (var i in folders) // { // var newfolder = await dest.CreateFolderAsync(i.Name, CreationCollisionOption.OpenIfExists); // await CopyFolder(i, newfolder); // } // // Copy files // var files = await source.GetFilesAsync(); // foreach (var i in files) // await i.CopyAsync(dest); //} /// /// Installs the assets at first launch. /// /// private static async Task InstallAssets() { // Read content of 'ApplicationData' var reader = new DrumKit.Archiving.TarballReader(); await reader.Unpack(new Uri("ms-appx:///Assets/ApplicationData.tar"), ApplicationData.Current.RoamingFolder); } /// /// Creates the 'installed.xml' file. /// /// private static async Task MarkInstalled() { // Open stream StorageFile file = await ApplicationData.Current.RoamingFolder.CreateFileAsync("installed.xml"); var stream = await file.OpenAsync(FileAccessMode.ReadWrite); var iostream = stream.AsStream(); // Generate xml var writer = System.Xml.XmlWriter.Create(iostream, new System.Xml.XmlWriterSettings() { Async = true, CloseOutput = true }); writer.WriteStartDocument(); writer.WriteStartElement("drumkit"); writer.WriteString(DateTime.UtcNow.ToString()); writer.WriteEndElement(); writer.WriteEndDocument(); // Cleanup await writer.FlushAsync(); writer.Dispose(); iostream.Dispose(); } /// /// Resets everything to factory settings. /// The application must be reinitialized after (or closed). /// public static async Task FactoryReset() { await ApplicationData.Current.ClearAsync(); } /// /// Loads the settings file. /// public static async Task LoadSettings() { // If all else fails, default settings Settings = new AppSettings(); // Get settings file var files = await ApplicationData.Current.RoamingFolder.GetFilesAsync(); var sf = files.FirstOrDefault(x => x.Name == "settings.xml"); // File found if (sf != null) { // Open file var fstream = await sf.OpenReadAsync(); var fstream_net = fstream.AsStream(); // Deserialize XmlSerializer s = new XmlSerializer(Settings.GetType()); var settings = s.Deserialize(fstream_net) as AppSettings; // All good if (settings != null) Settings = settings; } } /// /// Loads the settings file. /// public static async Task SaveSettings() { // Get settings file var file = await ApplicationData.Current.RoamingFolder.CreateFileAsync("settings.xml", CreationCollisionOption.ReplaceExisting); // Open file var fstream = await file.OpenAsync(FileAccessMode.ReadWrite); var fstream_net = fstream.AsStream(); // Serialize XmlSerializer s = new XmlSerializer(Settings.GetType()); s.Serialize(fstream_net, Settings); // Cleanup await fstream_net.FlushAsync(); fstream_net.Dispose(); } /// /// Initializes the application (prepares the application at first launch, loads settings and drums). /// public static async Task Initialize() { // Is this the first time the user launches the application? if (await IsFirstLaunch()) { // Clean up any junk await FactoryReset(); // Copy local assets to app data await InstallAssets(); // Generate 'installed.xml' file await MarkInstalled(); } // Load settings await LoadSettings(); // Load drum packages } public static async Task Close() { // Save settings await SaveSettings(); // Save modified layout & stuff } } }