public void ImportData(WillowTwoPlayerSaveGame saveGame, Platform platform)
        {
            this.Slots.Clear();
            this._BrokenSlots.Clear();
            foreach (var bankSlot in saveGame.BankSlots)
            {
                IPackableSlot slot;
                try
                {
                    slot = BaseDataHelper.Decode(bankSlot.InventorySerialNumber, platform);
                }
                catch (Exception e)
                {
                    this._BrokenSlots.Add(new KeyValuePair <BankSlot, Exception>(bankSlot, e));
                    continue;
                }

                var test = BaseDataHelper.Encode(slot, platform);
                if (bankSlot.InventorySerialNumber.SequenceEqual(test) == false)
                {
                    throw new FormatException("bank slot reencode mismatch");
                }

                if (slot is BaseWeapon)
                {
                    var viewModel = new BaseWeaponViewModel((BaseWeapon)slot);
                    this.Slots.Add(viewModel);
                }
                else if (slot is BaseItem)
                {
                    var viewModel = new BaseItemViewModel((BaseItem)slot);
                    this.Slots.Add(viewModel);
                }
            }
        }
        public void DuplicateSelectedSlot()
        {
            if (this.SelectedSlot == null)
            {
                return;
            }

            var copy = (IPackableSlot)this.SelectedSlot.BaseSlot.Clone();

            copy.UniqueId = new Random().Next(int.MinValue, int.MaxValue);
            // TODO: check other item unique IDs to prevent rare collisions

            if (copy is BaseWeapon)
            {
                var weapon = (BaseWeapon)copy;

                var viewModel = new BaseWeaponViewModel(weapon);
                this.Slots.Add(viewModel);
                this.SelectedSlot = viewModel;
            }
            else if (copy is BaseItem)
            {
                var item = (BaseItem)copy;

                var viewModel = new BaseItemViewModel(item);
                this.Slots.Add(viewModel);
                this.SelectedSlot = viewModel;
            }
            else
            {
                throw new NotSupportedException();
            }
        }
        public void DoNewItem(int assetLibrarySetId)
        {
            var item = new BaseItem()
            {
                UniqueId = new Random().Next(int.MinValue, int.MaxValue),
                // TODO: check other item unique IDs to prevent rare collisions
                AssetLibrarySetId = assetLibrarySetId,
            };
            var viewModel = new BaseItemViewModel(item);

            this.Slots.Add(viewModel);
            this.SelectedSlot = viewModel;
        }
        public IEnumerable <IResult> PasteCode()
        {
            bool containsText;
            bool containsUnicodeText = false;

            if (MyClipboard.ContainsText(TextDataFormat.Text, out containsText) != MyClipboard.Result.Success ||
                MyClipboard.ContainsText(TextDataFormat.UnicodeText, out containsUnicodeText) !=
                MyClipboard.Result.Success)
            {
                yield return(new MyMessageBox("Clipboard failure.", "Error")
                             .WithIcon(MessageBoxImage.Error));
            }

            if (containsText == false &&
                containsUnicodeText == false)
            {
                yield break;
            }

            var errors     = 0;
            var viewModels = new List <IBaseSlotViewModel>();

            yield return(new DelegateResult(
                             () =>
            {
                string codes;
                if (MyClipboard.GetText(out codes) != MyClipboard.Result.Success)
                {
                    MessageBox.Show("Clipboard failure.",
                                    "Error",
                                    MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                    return;
                }

                // strip whitespace
                codes = Regex.Replace(codes, @"\s+", "");

                foreach (var match in _CodeSignature.Matches(codes).Cast <Match>()
                         .Where(m => m.Success == true))
                {
                    var code = match.Groups["data"].Value;

                    IPackableSlot packable;

                    try
                    {
                        var data = Convert.FromBase64String(code);
                        packable = BaseDataHelper.Decode(data, Platform.PC);
                    }
                    catch (Exception)
                    {
                        errors++;
                        continue;
                    }

                    // TODO: check other item unique IDs to prevent rare collisions
                    packable.UniqueId = new Random().Next(int.MinValue, int.MaxValue);

                    if (packable is BaseWeapon)
                    {
                        var weapon = (BaseWeapon)packable;
                        var viewModel = new BaseWeaponViewModel(weapon);
                        viewModels.Add(viewModel);
                    }
                    else if (packable is BaseItem)
                    {
                        var item = (BaseItem)packable;
                        var viewModel = new BaseItemViewModel(item);
                        viewModels.Add(viewModel);
                    }
                }
            }));

            if (viewModels.Count > 0)
            {
                viewModels.ForEach(vm => this.Slots.Add(vm));
                this.SelectedSlot = viewModels.First();
            }

            if (errors > 0)
            {
                yield return
                    (new MyMessageBox("Failed to load " + errors.ToString(CultureInfo.InvariantCulture) + " codes.",
                                      "Warning")
                     .WithIcon(MessageBoxImage.Warning));
            }
            else if (viewModels.Count == 0)
            {
                yield return
                    (new MyMessageBox("Did not find any codes in clipboard.",
                                      "Warning")
                     .WithIcon(MessageBoxImage.Warning));
            }
        }