Provides an API to interact with the content of a Mail instance.
Inheritance: IMailContent
Ejemplo n.º 1
0
        /// <summary>
        /// Update the content of a mail.
        /// </summary>
        /// <param name="mail">The mail to update.</param>
        /// <param name="mailContent">The new content of the mail.</param>
        /// <exception cref="ArgumentNullException">The specified <paramref name="mailContent"/> is <c>null</c>.</exception>
        public static void UpdateMailContent(this Mail mail, MailContent mailContent)
        {
            if (mailContent == null)
            {
                throw new ArgumentNullException(nameof(mailContent));
            }

            switch (mail)
            {
            case ItemMail itemMail:
                itemMail.Text          = mailContent.Text;
                itemMail.AttachedItems = ((ItemMailContent)mailContent).AttachedItems;
                break;

            case MoneyMail moneyMail:
                moneyMail.Text          = mailContent.Text;
                moneyMail.AttachedMoney = ((MoneyMailContent)mailContent).AttachedMoney;
                moneyMail.Currency      = ((MoneyMailContent)mailContent).Currency;
                break;

            case RecipeMail recipeMail:
                recipeMail.Text   = mailContent.Text;
                recipeMail.Recipe = ((RecipeMailContent)mailContent).Recipe;
                break;

            case QuestMail questMail:
                questMail.Text    = mailContent.Text;
                questMail.QuestId = ((QuestMailContent)mailContent).QuestId;
                questMail.IsAutomaticallyAccepted = ((QuestMailContent)mailContent).IsAutomaticallyAccepted;
                break;

            default:
                mail.Text = mailContent.Text;
                break;
            }
        }
 /// <summary>
 /// Create a new instance of the <see cref="MailOpeningEventArgs"/> class.
 /// </summary>
 /// <param name="id">The ID of the mail being opened.</param>
 /// <param name="arrivalDay">The mailbox arrival day of the mail being opened.</param>
 /// <param name="content">The content of the mail being opened.</param>
 /// <exception cref="ArgumentNullException">
 /// The specified <paramref name="id"/> is <c>null</c> -or-
 /// the specified <paramref name="arrivalDay"/> is <c>null</c> -or-
 /// the specified <paramref name="content"/> is <c>null</c>.
 /// </exception>
 public MailOpeningEventArgs(string id, SDate arrivalDay, MailContent content)
 {
     Id         = id ?? throw new ArgumentNullException(nameof(id));
     ArrivalDay = arrivalDay ?? throw new ArgumentNullException(nameof(arrivalDay));
     Content    = content ?? throw new ArgumentNullException(nameof(content));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Called when a game menu is opened. This function is responsible for creating the UI for
        /// the user registered mails.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void OnMenuChanged(object sender, MenuChangedEventArgs e)
        {
            if (!(e.OldMenu is LetterViewerMenu) && e.NewMenu is LetterViewerMenu letterMenu)
            {
                var mailId = reflectionHelper.GetField <string>(letterMenu, "mailTitle").GetValue();

                // If the opened LetterViewerMenu instance does not represent a mail -> do nothing
                if (mailId == null)
                {
                    return;
                }

                // If the opened mail is not a mail registered via the framework, we still parse the mail content
                // for the framework's text coloring API and provide the item selection fix (only showing the last
                // item).
                if (!this.registeredMailsMetaData.TryGetValue(mailId, out MailMetaData mailMetaData))
                {
                    string mailContent = GetMailContentForGameMail(mailId);

                    // Create and show the menu for this mail.
                    var gLetterMenu = new LetterViewerMenuEx(mailId, mailContent);
                    Game1.activeClickableMenu = gLetterMenu;

                    // Since this is a mail which wasn't added to the game using this framework we are done.
                    return;
                }

                // If a mail with the given ID has been registered in the framework, but no mail sender has been found,
                // we remove the mail ID from the framework and don't proceed further.
                if (!this.mailSenders.TryGetValue(mailMetaData.ModId, out IMailSender mailSender))
                {
                    // A mail with this mailId was added to the framework at some point, but there is no sender
                    // owning this mail any longer. This can be due to the removal of a mod consuming the mail API of FeTK
                    // by the user. We can thus savely remove this mail from the framework on saving, as even if the consuming
                    // mod will be added back, for this save, the mail won't be displayed any longer (because it was already shown).
                    RemoveMail(mailId, mailMetaData.ArrivalDay);

                    monitor.Log($"The mail \"{mailMetaData.UserId}\" was added by the mod {mailMetaData.ModId} which seems to be no longer present.");
                    return;
                }

                // Request the actual mail data from the mail service which was used to add this mail to the game.
                var arrivalDate = SDateHelper.GetDateFromDay(mailMetaData.ArrivalDay);
                var mail        = mailSender.GetMailFromId(mailMetaData.UserId, arrivalDate);
                if (mail == null)
                {
                    monitor.Log($"An unexpected error occured. The mail \"{mailId}\" could not be retrieved from the mail service it was registered with.");
                    return;
                }

                // Get the editable content for this mail.
                MailContent content = mail.GetMailContent();

                // Raise the mail-opening event for this mail.
                mailSender.OnMailOpening(new MailOpeningEventArgs(mail.Id, arrivalDate, content));

                // Update the mail's content based on consumer-requested changes.
                mail.UpdateMailContent(content);

                // Create the menu for this mail.
                var nLetterMenu = new LetterViewerMenuWrapper(mail);

                // Setup the mail-closed event for this mail.
                nLetterMenu.MenuClosed += (s, e2) =>
                {
                    // Remove the closed mail from the mail manager.
                    RemoveMail(mailId, mailMetaData.ArrivalDay);

                    // Notify its sender that the mail has been closed.
                    mailSender.OnMailClosed(new MailClosedCoreEventArgs(mailMetaData.UserId, arrivalDate, e2.InteractionRecord));
                };

                monitor.Log($"Opening custom mail \"{mailMetaData.UserId}\".");

                // Show the menu for this mail.
                nLetterMenu.Show();
            }
        }