CommonMsg.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using CommonLang;
  2. using CommonLang.Log;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. //此类只用作老版本的消息转换,把string 变成对应的 short
  8. namespace Pomelo.DotNetClient
  9. {
  10. public class CommonMsg
  11. {
  12. private static Logger log = LoggerFactory.GetLogger("MPQDirver");
  13. private Dictionary<int, string> m_Msg;
  14. static private CommonMsg _Instance = null;
  15. static public CommonMsg GetInstance
  16. {
  17. get
  18. {
  19. if(_Instance == null)
  20. {
  21. _Instance = new CommonMsg();
  22. }
  23. return _Instance;
  24. }
  25. }
  26. CommonMsg()
  27. {
  28. m_Msg = new Dictionary<int, string>();
  29. }
  30. public int GetMsgID(string strMsg)
  31. {
  32. foreach(KeyValuePair<int ,string> kvp in m_Msg)
  33. {
  34. if(kvp.Value.Equals(strMsg))
  35. {
  36. return kvp.Key;
  37. }
  38. }
  39. return 0;
  40. }
  41. public string GetMsgStr(int msgId)
  42. {
  43. if(m_Msg.ContainsKey(msgId))
  44. {
  45. return m_Msg[msgId];
  46. }
  47. log.Error($"没有找到{msgId}对应的一串协议描述");
  48. return "";
  49. }
  50. public bool AddMsgID( short msgid, String msgname)
  51. {
  52. if(m_Msg.ContainsKey(msgid))
  53. {
  54. return false;
  55. }
  56. if(m_Msg.ContainsValue(msgname))
  57. {
  58. return false;
  59. }
  60. m_Msg.Add(msgid, msgname);
  61. return true;
  62. }
  63. }
  64. }