Exemple #1
0
        public override void HandleCommand(Command command, bool isImport = false)
        {
            Assert.ArgumentNotNull(command, "command");

            var textCommand = command as TextCommand;

            if (textCommand == null)
            {
                return;
            }

            var commandText = _whiteSpaceRx.Replace(textCommand.CommandText.Trim(), " ");

            if (commandText.StartsWith(Resources.LoreHelpCommand + " ", StringComparison.CurrentCultureIgnoreCase) ||
                commandText.Equals(Resources.LoreHelpCommand, StringComparison.CurrentCultureIgnoreCase))
            {
                PushMessageToConveyor(new InfoMessage(Resources.LoreHelp, TextColor.BrightYellow));
                PushMessageToConveyor(new InfoMessage(Resources.LoreCommentsHelp, TextColor.BrightYellow));
                command.Handled = true;
                return;
            }

            if (commandText.StartsWith(Resources.LoreCommentCommand + " ", StringComparison.CurrentCultureIgnoreCase) ||
                commandText.Equals(Resources.LoreCommentCommand, StringComparison.CurrentCultureIgnoreCase))
            {
                if (!string.IsNullOrEmpty(_lastShownObjectName))
                {
                    var         fileName = Path.Combine(GetStuffDbFolder(), _lastShownObjectName.Replace(" ", "_"));
                    LoreMessage lore     = null;
                    if (File.Exists(fileName))
                    {
                        using (var inStream = File.OpenRead(fileName))
                        {
                            var serializer = new XmlSerializer(typeof(LoreMessage));
                            lore          = (LoreMessage)serializer.Deserialize(inStream);
                            lore.Comments = commandText.Equals(Resources.LoreCommentCommand, StringComparison.CurrentCultureIgnoreCase)
                                                ? string.Empty
                                                : commandText.Remove(0, Resources.LoreCommentCommand.Length + 1);
                        }
                    }

                    if (lore != null)
                    {
                        SaveOrUpdateObjectLore(lore);
                    }
                }
                else
                {
                    PushMessageToConveyor(new ErrorMessage(Resources.LoreCommentError));
                }

                command.Handled = true;
                return;
            }

            if (commandText.StartsWith(Resources.LoreCommand + " ", StringComparison.CurrentCultureIgnoreCase) ||
                commandText.Equals(Resources.LoreCommand, StringComparison.CurrentCultureIgnoreCase))
            {
                command.Handled = true;

                var searchQuery = commandText.Equals(Resources.LoreCommand, StringComparison.CurrentCultureIgnoreCase)
                                      ? string.Empty
                                      : commandText.Remove(0, Resources.LoreCommand.Length + 1).Trim().Replace(" ", "_").Replace("\"", string.Empty);

                const int maxDisplayItems = 4;

                int foundItems = 0;
                if (Directory.Exists(GetStuffDbFolder()))
                {
                    foreach (var file in Directory.GetFiles(GetStuffDbFolder()))
                    {
                        if (file.IndexOf(searchQuery, StringComparison.CurrentCultureIgnoreCase) < 0)
                        {
                            continue;
                        }

                        foundItems++;
                        if (foundItems > maxDisplayItems)
                        {
                            PushMessageToConveyor(new InfoMessage(Resources.LoreTooMuchFound, TextColor.BrightYellow));
                            break;
                        }

                        using (var stream = File.OpenRead(file))
                        {
                            var serializer = new XmlSerializer(typeof(LoreMessage));
                            var message    = (LoreMessage)serializer.Deserialize(stream);
                            PushMessageToConveyor(new InfoMessage(string.Format(CultureInfo.CurrentCulture, Resources.LoreFoundObject, message.ObjectName), TextColor.BrightYellow));
                            foreach (var displayMessage in message.ConvertToMessages())
                            {
                                PushMessageToConveyor(displayMessage);
                            }

                            _lastShownObjectName = message.ObjectName;
                        }
                    }

                    if (foundItems == 0)
                    {
                        PushMessageToConveyor(new InfoMessage(Resources.LoreNothingFound, TextColor.BrightYellow));
                    }
                }
                else
                {
                    PushMessageToConveyor(new InfoMessage(Resources.LoreNothingFound, TextColor.BrightYellow));
                }
            }
        }
Exemple #2
0
        private void SaveOrUpdateObjectLore([NotNull] LoreMessage loreMessage)
        {
            Assert.ArgumentNotNull(loreMessage, "loreMessage");

            if (!Directory.Exists(GetStuffDbFolder()))
            {
                Directory.CreateDirectory(GetStuffDbFolder());
            }

            var  fileName  = Path.Combine(GetStuffDbFolder(), loreMessage.ObjectName.Replace(" ", "_").Replace("\"", string.Empty));
            bool isUpdated = false;

            if (File.Exists(fileName))
            {
                LoreMessage oldLore;
                using (var inStream = File.OpenRead(fileName))
                {
                    var serializer = new XmlSerializer(typeof(LoreMessage));
                    oldLore = (LoreMessage)serializer.Deserialize(inStream);
                    if (!string.IsNullOrEmpty(oldLore.Comments) && string.IsNullOrEmpty(loreMessage.Comments))
                    {
                        loreMessage.Comments = oldLore.Comments;
                    }

                    isUpdated = true;
                }
            }

            FileStream stream = null;

            try
            {
                stream = File.Open(fileName, FileMode.Create, FileAccess.Write);
                using (var streamWriter = new XmlTextWriter(stream, Encoding.Unicode))
                {
                    stream = null;
                    streamWriter.Formatting = Formatting.Indented;
                    var serializer = new XmlSerializer(typeof(LoreMessage));
                    serializer.Serialize(streamWriter, loreMessage);
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            _lastShownObjectName = loreMessage.ObjectName;
            if (isUpdated)
            {
                PushMessageToConveyor(new InfoMessage(string.Format(CultureInfo.CurrentCulture, Resources.LoreUpdated, loreMessage.ObjectName), TextColor.BrightYellow));
            }
            else
            {
                PushMessageToConveyor(new InfoMessage(string.Format(CultureInfo.CurrentCulture, Resources.LoreCreated, loreMessage.ObjectName), TextColor.BrightYellow));
            }

            PushMessageToConveyor(new InfoMessage(Resources.LoreGetHelp, TextColor.BrightYellow));
        }