/// <summary>
        /// Converts a <see cref="XElement"/> instance to a <see cref="WSBConfigManagerModel"/> instance.
        /// </summary>
        /// <param name="xElement">A <see cref="XElement"/> instance contains configuration</param>
        /// <returns>A <see cref="WSBConfigManagerModel"/> instance</returns>
        public new static WSBConfigManagerModel FromXElement(XElement xElement)
        {
            var wsbConfigManagerModel = new WSBConfigManagerModel();

            // Metadata
            if (xElement.Attribute(nameof(Name)) is XAttribute xName)
            {
                wsbConfigManagerModel.Name = xName.Value;
            }
            if (xElement.Attribute(nameof(CreatedAt)) is XAttribute xCreatedAt &&
                Utility.TryConvert(xCreatedAt.Value, TypeCode.DateTime) is DateTime createdAt)
            {
                wsbConfigManagerModel.CreatedAt = createdAt;
            }
            if (xElement.Attribute(nameof(UUID)) is XAttribute xUUID)
            {
                wsbConfigManagerModel.UUID = xUUID.Value;
            }
            if (xElement.Attribute(nameof(LastLaunchedAt)) is XAttribute xLastLaunchedAt &&
                Utility.TryConvert(xLastLaunchedAt.Value, TypeCode.DateTime) is DateTime lastLaunchedAt)
            {
                wsbConfigManagerModel.LastLaunchedAt = lastLaunchedAt;
            }

            // VGPU
            if (xElement.Element(nameof(VGpu)) is XElement xVGpu &&
                Utility.TryConvert(typeof(VGpu), xVGpu.Value) is VGpu vGpu)
            {
                wsbConfigManagerModel.VGpu = vGpu;
            }
            // Networking
            if (xElement.Element(nameof(Networking)) is XElement xNetworking &&
                Utility.TryConvert(typeof(Networking), xNetworking.Value) is Networking networking)
            {
                wsbConfigManagerModel.Networking = networking;
            }
            // Mapped Folders
            if (xElement.Element(nameof(MappedFolders)) is XElement xMappedFolders)
            {
                foreach (var xMappedFolder in xMappedFolders.Elements(nameof(MappedFolder)))
                {
                    var mf = new MappedFolder();
                    if (xMappedFolder.Element(nameof(mf.HostFolder)) is XElement xHostFolder &&
                        xMappedFolder.Element(nameof(mf.ReadOnly)) is XElement xReadOnly &&
                        Utility.TryConvert(xReadOnly.Value, TypeCode.Boolean) is bool readOnly)
                    {
                        mf.HostFolder = xHostFolder.Value;
                        mf.ReadOnly   = readOnly;
                    }
                    wsbConfigManagerModel.MappedFolders.Add(mf);
                }
            }
            // Logon Command
            if (xElement.Element(nameof(LogonCommand))?.Element(nameof(wsbConfigManagerModel.LogonCommand.Command)) is XElement xCommand)
            {
                wsbConfigManagerModel.LogonCommand.Command = xCommand.Value;
            }

            return(wsbConfigManagerModel);
        }
示例#2
0
        /// <summary>
        /// Loads a sandbox configuration list.
        /// </summary>
        /// <returns>true: Success / false : Failed</returns>
        public async Task <bool> LoadAsync()
        {
            await semaphore.WaitAsync().ConfigureAwait(false);

            try {
                var localFile = await localFolder.TryGetItemAsync(wsbConfigListFileName);

                if (localFile is IStorageFile storageFile)
                {
                    using (var s = await storageFile.OpenStreamForReadAsync())
                        using (var sr = new StreamReader(s)) {
                            WSBConfigCollection.Clear();
                            using (var xr = XmlReader.Create(sr)) {
                                var xElement = XElement.Load(xr);
                                foreach (var configItem in xElement.Elements(WSBConfigModel.RootNodeName))
                                {
                                    WSBConfigCollection.Add(WSBConfigManagerModel.FromXElement(configItem));
                                }

                                LoadCongiugurationListCompleted?.Invoke(this, null);
                            }
                        }
                }
                return(true);
            }
            catch (Exception) {
                return(false);
            }
            finally {
                semaphore.Release();
            }
        }
示例#3
0
        /// <summary>
        /// Imports a sandbox configuration list.
        /// </summary>
        /// <returns>true: Success / false : Failed</returns>
        public async Task <bool> ImportAsync(IStorageFile storageFile)
        {
            await semaphore.WaitAsync().ConfigureAwait(false);

            try
            {
                var uuidSet = new HashSet <string>(WSBConfigCollection.Select(configItem => configItem.UUID));

                using (var s = await storageFile.OpenStreamForReadAsync())
                    using (var sr = new StreamReader(s))
                    {
                        using (var xr = XmlReader.Create(sr))
                        {
                            var xElement = XElement.Load(xr);
                            foreach (var configItem in xElement.Elements(WSBConfigModel.RootNodeName))
                            {
                                var newConfigModel = WSBConfigManagerModel.FromXElement(configItem);
                                if (uuidSet.Contains(newConfigModel.UUID))
                                {
                                    // If there is a UUID conflict, the newly added UUID will be regenerated.
                                    newConfigModel.ResetUUID();
                                }
                                WSBConfigCollection.Add(newConfigModel);
                                uuidSet.Add(newConfigModel.UUID);
                            }

                            LoadCongiugurationListCompleted?.Invoke(this, null);
                        }
                    }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                semaphore.Release();
            }
        }
 /// <summary>
 /// Creates a new instance of the <see cref="WSBConfigManagerModel"/> class from an existing instance.
 /// </summary>
 /// <param name="wSBConfigManagerModel">A existing instance of the <see cref="WSBConfigManagerModel"/> class</param>
 public WSBConfigManagerModel(WSBConfigManagerModel wSBConfigManagerModel) : base(wSBConfigManagerModel)
 {
     UUID      = wSBConfigManagerModel.UUID;
     Name      = wSBConfigManagerModel.Name;
     CreatedAt = wSBConfigManagerModel.CreatedAt;
 }
        /// <summary>
        /// Converts a <see cref="XElement"/> instance to a <see cref="WSBConfigManagerModel"/> instance.
        /// </summary>
        /// <param name="xElement">A <see cref="XElement"/> instance contains configuration</param>
        /// <returns>A <see cref="WSBConfigManagerModel"/> instance</returns>
        public new static WSBConfigManagerModel FromXElement(XElement xElement)
        {
            var wsbConfigManagerModel = new WSBConfigManagerModel();

            // Metadata
            if (xElement.Attribute(nameof(Name)) is XAttribute xName)
            {
                wsbConfigManagerModel.Name = xName.Value;
            }
            if (xElement.Attribute(nameof(CreatedAt)) is XAttribute xCreatedAt &&
                Utility.TryConvert(xCreatedAt.Value, TypeCode.DateTime) is DateTime createdAt)
            {
                wsbConfigManagerModel.CreatedAt = createdAt;
            }
            if (xElement.Attribute(nameof(UUID)) is XAttribute xUUID)
            {
                wsbConfigManagerModel.UUID = xUUID.Value;
            }
            if (xElement.Attribute(nameof(LastLaunchedAt)) is XAttribute xLastLaunchedAt &&
                Utility.TryConvert(xLastLaunchedAt.Value, TypeCode.DateTime) is DateTime lastLaunchedAt)
            {
                wsbConfigManagerModel.LastLaunchedAt = lastLaunchedAt;
            }

            // VGPU
            if (xElement.Element(nameof(VGpu)) is XElement xVGpu &&
                Utility.TryConvert(typeof(ThreeState), xVGpu.Value) is ThreeState vGpu)
            {
                wsbConfigManagerModel.VGpu = vGpu;
            }
            // Networking
            if (xElement.Element(nameof(Networking)) is XElement xNetworking &&
                Utility.TryConvert(typeof(TwoState), xNetworking.Value) is TwoState networking)
            {
                wsbConfigManagerModel.Networking = networking;
            }
            // Mapped Folders
            if (xElement.Element(nameof(MappedFolders)) is XElement xMappedFolders)
            {
                foreach (var xMappedFolder in xMappedFolders.Elements(nameof(MappedFolder)))
                {
                    var mf = new MappedFolder();
                    if (xMappedFolder.Element(nameof(mf.HostFolder)) is XElement xHostFolder &&
                        xMappedFolder.Element(nameof(mf.ReadOnly)) is XElement xReadOnly &&
                        Utility.TryConvert(xReadOnly.Value, TypeCode.Boolean) is bool readOnly)
                    {
                        mf.HostFolder = xHostFolder.Value;
                        mf.ReadOnly   = readOnly;
                    }
                    wsbConfigManagerModel.MappedFolders.Add(mf);
                }
            }
            // Logon Command
            if (xElement.Element(nameof(LogonCommand))?.Element(nameof(wsbConfigManagerModel.LogonCommand.Command)) is XElement xCommand)
            {
                wsbConfigManagerModel.LogonCommand.Command = xCommand.Value;
            }

            // Audio Input
            if (xElement.Element(nameof(AudioInput)) is XElement xAudioInput &&
                Utility.TryConvert(typeof(ThreeState), xAudioInput.Value) is ThreeState audioInput)
            {
                wsbConfigManagerModel.AudioInput = audioInput;
            }

            // Video Input
            if (xElement.Element(nameof(VideoInput)) is XElement xVideoInput &&
                Utility.TryConvert(typeof(ThreeState), xVideoInput.Value) is ThreeState videoInput)
            {
                wsbConfigManagerModel.VideoInput = videoInput;
            }

            // Protected Client
            if (xElement.Element(nameof(ProtectedClient)) is XElement xProtectedClient &&
                Utility.TryConvert(typeof(ThreeState), xProtectedClient.Value) is ThreeState protectedClient)
            {
                wsbConfigManagerModel.ProtectedClient = protectedClient;
            }

            // Printer Redirection
            if (xElement.Element(nameof(PrinterRedirection)) is XElement xPrinterRedirection &&
                Utility.TryConvert(typeof(ThreeState), xPrinterRedirection.Value) is ThreeState printerRedirection)
            {
                wsbConfigManagerModel.PrinterRedirection = printerRedirection;
            }

            // Clipboard Redirection
            if (xElement.Element(nameof(ClipboardRedirection)) is XElement xClipboardRedirection &&
                Utility.TryConvert(typeof(TwoState), xClipboardRedirection.Value) is TwoState clipboardRedirection)
            {
                wsbConfigManagerModel.ClipboardRedirection = clipboardRedirection;
            }

            // Memory in MB
            if (xElement.Element(nameof(MemoryInMB)) is XElement xMemoryInMB)
            {
                if (xMemoryInMB.Element(nameof(wsbConfigManagerModel.MemoryInMB.AmountInMB)) is XElement xAmountInMB &&
                    xMemoryInMB.Element(nameof(wsbConfigManagerModel.MemoryInMB.Enabled)) is XElement xEnabled &&
                    Utility.TryConvert(xAmountInMB.Value, TypeCode.Int32) is int amountInMB &&
                    Utility.TryConvert(xEnabled.Value, TypeCode.Boolean) is bool enabled)
                {
                    wsbConfigManagerModel.MemoryInMB.AmountInMB = amountInMB;
                    wsbConfigManagerModel.MemoryInMB.Enabled    = enabled;
                }
            }

            return(wsbConfigManagerModel);
        }