protected override void OnItemDoubleClick(AcObjectNew obj)
        {
            var championship = obj as UserChampionshipObject;

            if (championship == null)
            {
                return;
            }
            UserChampionships.NavigateToChampionshipPage(championship);
        }
Beispiel #2
0
        private static void OnUserChampionshipsManagerCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems.Count == 1 && (e.NewItems[0] as AcItemWrapper)?.Id == _openId)
            {
                UserChampionshipsManager.Instance.WrappersList.CollectionChanged -= OnUserChampionshipsManagerCollectionChanged;

                if (_openId != null)
                {
                    UserChampionships.NavigateToChampionshipPage(UserChampionshipsManager.Instance.GetById(_openId));
                    UserChampionships_SelectedPage.IgnoreIntro(_openId);
                }
            }
        }
Beispiel #3
0
        private static ArgumentHandleResult ProcessSharedUserChampionshipExt(SharedEntry shared, byte[] data, bool justGo)
        {
            byte[] mainData = null, extraData = null, previewImage = null;

            string sharedId = null;

            using (var stream = new MemoryStream(data)) {
                var reader = ReaderFactory.Open(stream);

                var written = 0;
                try {
                    while (reader.MoveToNextEntry())
                    {
                        if (!reader.Entry.IsDirectory)
                        {
                            var name = reader.Entry.Key;
                            if (name.EndsWith(UserChampionshipObject.FileExtension))
                            {
                                sharedId = name;
                                mainData = reader.OpenEntryStream().ReadAsBytesAndDispose();
                            }
                            else if (name.EndsWith(UserChampionshipObject.FileDataExtension))
                            {
                                extraData = reader.OpenEntryStream().ReadAsBytesAndDispose();
                            }
                            else if (name.EndsWith(UserChampionshipObject.FilePreviewExtension))
                            {
                                previewImage = reader.OpenEntryStream().ReadAsBytesAndDispose();
                            }

                            written++;
                        }
                    }
                } catch (EndOfStreamException) {
                    if (written < 1)
                    {
                        throw;
                    }
                }
            }

            if (sharedId == null || mainData == null)
            {
                throw new InformativeException("Can’t install championship", "Main file is missing.");
            }

            var mainDataJson  = JObject.Parse(mainData.ToUtf8String());
            var extraDataJson = extraData == null ? null : JObject.Parse(extraData.ToUtf8String());

            var information = new UserChampionshipInformation {
                Name        = mainDataJson.GetStringValueOnly("name"),
                Description = extraDataJson?.GetStringValueOnly("description"),
                Author      = extraDataJson?.GetStringValueOnly("author"),
                Difficulty  = extraDataJson?.GetStringValueOnly("difficulty"),
            };

            if (previewImage != null)
            {
                var temp = FileUtils.GetTempFileName(Path.GetTempPath(), @".jpg");
                File.WriteAllBytes(temp, previewImage);
                information.PreviewImage = temp;
            }

            var existing = UserChampionshipsManager.Instance.GetById(sharedId);
            var dialog   = new UserChampionshipIntro(information, existing == null ? UserChampionshipIntroMode.InstallationPreview :
                                                     UserChampionshipIntroMode.InstallationAlreadyExistingPreview, existing?.Name);

            dialog.ShowDialog();

            switch (dialog.MessageBoxResult)
            {
            case MessageBoxResult.OK:
            case MessageBoxResult.Yes:
                string replacementId = null;
                if (existing != null && dialog.MessageBoxResult == MessageBoxResult.OK)
                {
                    for (var i = 0; i < 999; i++)
                    {
                        var candidate = Guid.NewGuid() + UserChampionshipObject.FileExtension;
                        if (UserChampionshipsManager.Instance.GetById(candidate) == null)
                        {
                            replacementId = candidate;
                            break;
                        }
                    }

                    if (replacementId == null)
                    {
                        throw new InformativeException("Can’t install championship", "Can’t find a new ID.");
                    }
                }

                var directory = UserChampionshipsManager.Instance.Directories.GetMainDirectory();
                Directory.CreateDirectory(directory);

                if (existing == null || replacementId != null)
                {
                    _openId = replacementId ?? sharedId;
                    UserChampionshipsManager.Instance.WrappersList.CollectionChanged += OnUserChampionshipsManagerCollectionChanged;
                }
                else
                {
                    UserChampionships.NavigateToChampionshipPage(UserChampionshipsManager.Instance.GetById(sharedId));
                }

                using (var stream = new MemoryStream(data)) {
                    var reader = ReaderFactory.Open(stream);

                    var written = 0;
                    try {
                        while (reader.MoveToNextEntry())
                        {
                            if (!reader.Entry.IsDirectory)
                            {
                                var name = reader.Entry.Key;
                                if (replacementId != null)
                                {
                                    name = name.Replace(sharedId, replacementId);
                                }

                                reader.WriteEntryToFile(Path.Combine(directory, name), new ExtractionOptions {
                                    Overwrite = true
                                });
                                written++;
                            }
                        }
                    } catch (EndOfStreamException) {
                        if (written < 1)
                        {
                            throw;
                        }
                    }
                }
                return(ArgumentHandleResult.SuccessfulShow);

            default:
                return(ArgumentHandleResult.Failed);
            }
        }