Пример #1
0
        private static bool Prefix(ref Chat __instance)
        {
            var text = __instance.m_input.text;

            // Add the message to the chat history.
            if (VChatPlugin.Settings.MaxPlayerMessageHistoryCount > 0)
            {
                if (VChatPlugin.MessageSendHistory.Count > VChatPlugin.Settings.MaxPlayerMessageHistoryCount)
                {
                    VChatPlugin.MessageSendHistory.RemoveAt(0);
                }

                VChatPlugin.MessageSendHistory.Add(text);
                VChatPlugin.MessageSendHistoryIndex = 0;
            }

            // Attempt to parse a command.
            if (VChatPlugin.CommandHandler.TryFindAndExecuteCommand(text, __instance, out PluginCommand _))
            {
                return(false);
            }

            // Otherwise send the message to the last used channel.
            // Only when not starting with a slash, because that's the default for commands. We still want /sit and such to work :)
            if (!text.StartsWith("/"))
            {
                if (VChatPlugin.LastChatType.IsDefaultType())
                {
                    __instance.SendText(VChatPlugin.LastChatType.DefaultTypeValue.Value, text);
                    return(false);
                }
                else
                {
                    switch (VChatPlugin.LastChatType.CustomTypeValue)
                    {
                    case CustomMessageType.Global:
                        GlobalMessages.SendGlobalMessageToServer(text);
                        break;
                    }
                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
        private void InitialiseCommands()
        {
            CommandHandler.ClearCommands();

            const string changedColorMessageSuccess = "Changed the {0} color to <color={1}>{2}</color>.";
            const string errorParseColorMessage     = "Could not parse the color \"{0}\".";
            const string errorParseNumber           = "Could not convert \"{0}\" to a valid number.";

            var writeErrorMessage = new Action <string>((string message) =>
            {
                Chat.instance.AddString($"<color=red>[{Name}][Error] {message}</color>");
            });

            var writeSuccessMessage = new Action <string>((string message) =>
            {
                Chat.instance.AddString($"<color=#23ff00>[{Name}] {message}</color>");
            });

            var applyChannelColorCommand = new Func <string, CombinedMessageType, Color?>((string text, CombinedMessageType messageType) =>
            {
                text      = text?.Trim();
                var color = text?.ToColor();

                // Get default color if text is empty.
                if (string.IsNullOrEmpty(text))
                {
                    text  = "default";
                    color = GetTextColor(messageType, true);
                }

                // Write the response message.
                if (color != null)
                {
                    writeSuccessMessage(string.Format(changedColorMessageSuccess, messageType.ToString().ToLower(), color?.ToHtmlString(), text));
                }
                else
                {
                    writeErrorMessage(string.Format(errorParseColorMessage, text));
                }
                return(color);
            });


            CommandHandler.AddCommands(
                new PluginCommand(PluginCommandType.SendLocalMessage, Settings.LocalChatCommandName, (text, instance) =>
            {
                LastChatType.Set(Talker.Type.Normal);
                if (!string.IsNullOrEmpty(text))
                {
                    ((Chat)instance).SendText(Talker.Type.Normal, text);
                }
            }),
                new PluginCommand(PluginCommandType.SendShoutMessage, Settings.ShoutChatCommandName, (text, instance) =>
            {
                LastChatType.Set(Talker.Type.Shout);
                if (!string.IsNullOrEmpty(text))
                {
                    ((Chat)instance).SendText(Talker.Type.Shout, text);
                }
            }),
                new PluginCommand(PluginCommandType.SendWhisperMessage, Settings.WhisperChatCommandName, (text, instance) =>
            {
                LastChatType.Set(Talker.Type.Whisper);
                if (!string.IsNullOrEmpty(text))
                {
                    ((Chat)instance).SendText(Talker.Type.Whisper, text);
                }
            }),
                new PluginCommand(PluginCommandType.SendGlobalMessage, Settings.GlobalChatCommandName, (text, instance) =>
            {
                LastChatType.Set(CustomMessageType.Global);
                if (!string.IsNullOrEmpty(text))
                {
                    GlobalMessages.SendGlobalMessageToServer(text);
                }
            }),
                new PluginCommand(PluginCommandType.SetLocalColor, Settings.SetLocalChatColorCommandName, (text, instance) =>
            {
                var color = applyChannelColorCommand(text, new CombinedMessageType(Talker.Type.Normal));
                if (color != null)
                {
                    Settings.LocalChatColor = color;
                }
            }),
                new PluginCommand(PluginCommandType.SetShoutColor, Settings.SetShoutChatColorCommandName, (text, instance) =>
            {
                var color = applyChannelColorCommand(text, new CombinedMessageType(Talker.Type.Shout));
                if (color != null)
                {
                    Settings.ShoutChatColor = color;
                }
            }),
                new PluginCommand(PluginCommandType.SetWhisperColor, Settings.SetWhisperChatColorCommandName, (text, instance) =>
            {
                var color = applyChannelColorCommand(text, new CombinedMessageType(Talker.Type.Whisper));
                if (color != null)
                {
                    Settings.WhisperChatColor = color;
                }
            }),
                new PluginCommand(PluginCommandType.SetGlobalColor, Settings.GlobalWhisperChatColorCommandName, (text, instance) =>
            {
                var color = applyChannelColorCommand(text, new CombinedMessageType(CustomMessageType.Global));
                if (color != null)
                {
                    Settings.GlobalChatColor = color;
                }
            }),
                new PluginCommand(PluginCommandType.ToggleShowChatWindow, "showchat", (text, instance) =>
            {
                Settings.AlwaysShowChatWindow = !Settings.AlwaysShowChatWindow;
                writeSuccessMessage($"{(Settings.AlwaysShowChatWindow ? "Always displaying" : "Auto hiding")} chat window.");
            }),
                new PluginCommand(PluginCommandType.ToggleShowChatWindowOnMessage, Settings.ShowChatOnMessageCommandName, (text, instance) =>
            {
                Settings.ShowChatWindowOnMessageReceived = !Settings.ShowChatWindowOnMessageReceived;
                writeSuccessMessage($"{(Settings.ShowChatWindowOnMessageReceived ? "Displaying" : "Not displaying")} chat window when receiving a message.");
            }),
                new PluginCommand(PluginCommandType.ToggleChatWindowClickThrough, Settings.ChatClickThroughCommandName, (text, instance) =>
            {
                Settings.EnableClickThroughChatWindow = !Settings.EnableClickThroughChatWindow;
                writeSuccessMessage($"{(Settings.EnableClickThroughChatWindow ? "Enabled" : "Disabled")} clicking through the chat window.");
                ((Chat)instance).m_chatWindow?.ChangeClickThroughInChildren(!Settings.EnableClickThroughChatWindow);
            }),
                new PluginCommand(PluginCommandType.SetMaxPlayerHistory, Settings.MaxPlayerChatHistoryCommandName, (text, instance) =>
            {
                if (string.IsNullOrEmpty(text))
                {
                    writeSuccessMessage($"The number of stored player messages is set to {Settings.MaxPlayerMessageHistoryCount}.");
                }
                else
                {
                    if (ushort.TryParse(text, out ushort value))
                    {
                        Settings.MaxPlayerMessageHistoryCount = value;
                        if (value > 0)
                        {
                            writeSuccessMessage($"Changed the maximum stored player messages to {value}.");
                        }
                        else
                        {
                            writeSuccessMessage($"Disabled capturing player chat history.");
                            MessageSendHistory.Clear();
                        }

                        // Readjust buffer size
                        while (MessageSendHistory.Count > 0 && MessageSendHistory.Count < Settings.MaxPlayerMessageHistoryCount)
                        {
                            MessageSendHistory.RemoveAt(0);
                        }
                    }
                    else
                    {
                        writeErrorMessage(string.Format(errorParseNumber, text));
                    }
                }
            }),
                new PluginCommand(PluginCommandType.SetHideDelay, Settings.SetChatHideDelayCommandName, (text, instance) =>
            {
                if (float.TryParse(text, out float delay) && !float.IsNaN(delay))
                {
                    if (delay > 0)
                    {
                        Settings.ChatHideDelay       = delay;
                        ((Chat)instance).m_hideDelay = delay;
                        writeSuccessMessage($"Updated the chat hide delay to {delay} seconds.");
                    }
                    else
                    {
                        writeErrorMessage($"Hide delay must be greater than 0.");
                    }
                }
                else
                {
                    writeErrorMessage(string.Format(errorParseNumber, text));
                }
            }),
                new PluginCommand(PluginCommandType.SetFadeTime, Settings.SetChatFadeTimeCommandName, (text, instance) =>
            {
                if (float.TryParse(text, out float time) && !float.IsNaN(time))
                {
                    time = Math.Max(0f, time);
                    writeSuccessMessage($"Updated the chat fade timer to {time} seconds.");
                    Settings.ChatFadeTimer = time;
                }
                else
                {
                    writeErrorMessage(string.Format(errorParseNumber, text));
                }
            }),
                new PluginCommand(PluginCommandType.SetActiveOpacity, Settings.SetOpacityCommandName, (text, instance) =>
            {
                if (uint.TryParse(text, out uint opacity))
                {
                    opacity = Math.Min(100, Math.Max(0, opacity));
                    writeSuccessMessage($"Updated the chat opacity to {opacity}.");
                    Settings.ChatOpacity = opacity;
                }
                else
                {
                    writeErrorMessage(string.Format(errorParseNumber, text));
                }
            }),
                new PluginCommand(PluginCommandType.SetInactiveOpacity, Settings.SetInactiveOpacityCommandName, (text, instance) =>
            {
                if (uint.TryParse(text, out uint opacity))
                {
                    opacity = Math.Min(100, Math.Max(0, opacity));
                    writeSuccessMessage($"Updated the chat inactive opacity to {opacity}.");
                    Settings.InactiveChatOpacity = opacity;
                }
                else
                {
                    writeErrorMessage(string.Format(errorParseNumber, text));
                }
            }),
                new PluginCommand(PluginCommandType.SetDefaultChatChannel, Settings.SetDefaultChatChannelCommandName, (text, instance) =>
            {
                var type     = new CombinedMessageType(CustomMessageType.Global);
                bool success = false;

                if (Enum.TryParse(text, true, out Talker.Type talkerType) &&
                    talkerType != Talker.Type.Ping)
                {
                    type.Set(talkerType);
                    success = true;
                }
                else
                {
                    if (Enum.TryParse(text, true, out CustomMessageType customType))
                    {
                        type.Set(customType);
                        success = true;
                    }
                }

                if (success)
                {
                    writeSuccessMessage($"Updated the default chat channel to {text}.");
                    Settings.DefaultChatChannel = type;
                }
                else
                {
                    writeErrorMessage($"Failed to convert \"{text}\" into a chat channel name. Accepted values: {string.Join(", ", Enum.GetNames(typeof(Talker.Type)).Except(new[] { nameof(Talker.Type.Ping) }).Concat(Enum.GetNames(typeof(CustomMessageType))).Distinct().Select(x => x.ToLower()))}.");
                }
            })
                );
        }