Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatConnectionEngine.cs 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Text.RegularExpressions;
  6. using ExitGames.Client.Photon;
  7. using ExitGames.Client.Photon.Chat;
  8. using HarmonyLib;
  9. using Svelto.Context;
  10. namespace CLre_server.Tweaks.Chat
  11. {
  12. public class ChatConnectionEngine: API.Engines.ServerEnginePostBuild, IWaitForFrameworkInitialization, IWaitForFrameworkDestruction
  13. {
  14. private bool _running = false;
  15. private ChatClient _chatClient;
  16. private ChatListener _chatListener;
  17. public delegate void CommandHandler(Match messageMatch, ChatClient connection, string username);
  18. private Dictionary<ChatCommandAttribute, CommandHandler> _handlers;
  19. internal static List<Assembly> _assembliesToCheck = new List<Assembly>(new []{typeof(CLre).Assembly});
  20. public override void Ready()
  21. {
  22. _running = true;
  23. }
  24. private IEnumerator connectify()
  25. {
  26. LoadHandlers(); // find & load commands
  27. // wait for login to succeed (it may never)
  28. while (!ChatHandler.IsAuthenticationReady && _running)
  29. {
  30. yield return null;
  31. }
  32. // login with authenticated credentials
  33. // shout-out to however made an identical AuthenticationValues struct in the global namespace
  34. ExitGames.Client.Photon.Chat.AuthenticationValues auth = new ExitGames.Client.Photon.Chat.AuthenticationValues();
  35. auth.AuthType = ExitGames.Client.Photon.Chat.CustomAuthenticationType.Custom;
  36. auth.AddAuthParameter("publicId", ChatHandler.PublicId);
  37. auth.AddAuthParameter("token", ChatHandler.Token);
  38. auth.UserId = ChatHandler.PublicId;
  39. _chatListener= new ChatListener(_handlers);
  40. _chatClient = new ChatClient(_chatListener, ConnectionProtocol.Udp);
  41. _chatListener.ChatClient = _chatClient;
  42. _chatClient.Connect(Game.Utilities.CardLifePhotonSettings.PhotonChatAppID, "1.0", auth);
  43. // run forever (until server shutsdown)
  44. while (_running)
  45. {
  46. _chatClient.Service();
  47. yield return null;
  48. }
  49. }
  50. public void OnFrameworkInitialized()
  51. {
  52. connectify().Run();
  53. }
  54. public void OnFrameworkDestroyed()
  55. {
  56. _running = false;
  57. }
  58. private void LoadHandlers()
  59. {
  60. _handlers = new Dictionary<ChatCommandAttribute, CommandHandler>();
  61. foreach (Assembly asm in _assembliesToCheck.ToArray())
  62. {
  63. foreach (Type t in asm.GetTypes())
  64. {
  65. foreach (MethodInfo m in t.GetMethods(AccessTools.all))
  66. {
  67. ChatCommandAttribute attr = m.GetCustomAttribute<ChatCommandAttribute>();
  68. if (attr != null)
  69. {
  70. // TODO validate that method signature matches that of CommandHandler
  71. API.Utility.Logging.MetaLog($"{t.FullName}:{m.Name} is handling {attr.Name}");
  72. _handlers.Add(attr, (CommandHandler) Delegate.CreateDelegate(typeof(CommandHandler), m));
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }