Ejemplo n.º 1
0
        async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                Items.Clear();
                await PCLSourceManager.InitAsync().ConfigureAwait(false);

                BuilderContext TinyContext = new BuilderContext();
                ConfigManager  config      = await TinyContext.LoadConfigAsync(await PCLSourceManager.Data.GetFileAsync("Config.xml").ConfigureAwait(false)).ConfigureAwait(false);

                await TinyContext.LoadLevelAsync(await PCLSourceManager.Data.GetFileAsync(config.Levels).ConfigureAwait(false)).ConfigureAwait(false);

                await TinyContext.ImportClassesAsync(true, false).ConfigureAwait(true);

                await TinyContext.ImportSubClassesAsync(true, false).ConfigureAwait(true);

                ExistenceCheckResult res = await App.Storage.CheckExistsAsync("Characters");

                IFolder characters = await App.Storage.CreateFolderAsync("Characters", CreationCollisionOption.OpenIfExists);

                if (res == ExistenceCheckResult.NotFound)
                {
                    IFile example = await characters.CreateFileAsync("Ex Ample.cb5", CreationCollisionOption.ReplaceExisting);

                    var assembly = typeof(PCLSourceManager).GetTypeInfo().Assembly;
                    using (Stream stream = assembly.GetManifestResourceStream("CB_5e.Ex Ample.cb5"))
                    {
                        using (Stream ex = await example.OpenAsync(FileAccess.ReadAndWrite))
                        {
                            await stream.CopyToAsync(ex);
                        }
                    }
                }
                items.Clear();
                foreach (IFile c in await characters.GetFilesAsync().ConfigureAwait(false))
                {
                    if (!c.Name.EndsWith(".cb5", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                    try
                    {
                        Player p = await TinyContext.LoadPlayerAsync(c);

                        items.Add(new Models.Character(p, "--"));
                        //{
                        //    Player = p
                        //});
                    } catch (Exception e)
                    {
                        ConfigManager.LogError(e);
                    }
                }
                foreach (IFolder c in await characters.GetFoldersAsync().ConfigureAwait(false))
                {
                    await Add(c, items, c.Name, TinyContext);
                }
                UpdateItems();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                MessagingCenter.Send(new MessagingCenterAlert
                {
                    Title   = "Error",
                    Message = "Unable to load items.",
                    Cancel  = "OK"
                }, "message");
            }
            finally
            {
                IsBusy = false;
            }
        }