MetaValueProxy.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local MetaValueProxy = class("RemoteProxy", RemoteProxy)
  3. MetaValueProxy.NAME = global.ProxyTable.MetaValueProxy
  4. local MetaValueGetDef = require("config/MetaValueGetDef")
  5. local MetaValueSetDef = require("config/MetaValueSetDef")
  6. function MetaValueProxy:ctor()
  7. MetaValueProxy.super.ctor(self)
  8. self._cacheMeta = {}
  9. end
  10. -- 元变量替换,将本地元变量替换
  11. function MetaValueProxy:MetaValueFormat(str)
  12. local source = str
  13. local results = {}
  14. while true do
  15. local sIdx, eIdx, oriStr,param = string.find(source, "(&<(.-)>&)")
  16. if not (oriStr and param) then
  17. break
  18. end
  19. if self._cacheMeta[oriStr] then
  20. results[oriStr] = self._cacheMeta[oriStr]
  21. else
  22. -- 本地取元变量
  23. local tokens = string.split(param, "/")
  24. local metaKey = tokens[1]
  25. local successFunc = loadstring(string.format(
  26. [[
  27. local metaValueProxy = global.Facade:retrieveProxy(global.ProxyTable.MetaValueProxy)
  28. return metaValueProxy:GetValueByKey('%s', '%s') or ""
  29. ]], metaKey, tokens[2]))
  30. if successFunc then
  31. self._cacheMeta[oriStr] = successFunc
  32. results[oriStr] = successFunc
  33. end
  34. end
  35. local s1 = string.sub(source, 1, sIdx - 1)
  36. local s2 = string.sub(source, eIdx + 1, string.len(source))
  37. source = s1 .. s2
  38. end
  39. -- 本地替换一波
  40. for key, result in pairs(results) do
  41. local sIdx, eIdx = string.find(str, key, 1, true)
  42. str = string.sub(str, 1, sIdx - 1) .. tostring(result()) .. string.sub(str, eIdx + 1, string.len(str))
  43. end
  44. return str
  45. end
  46. function MetaValueProxy:GetValueByKey(key, ...)
  47. if not key then
  48. return global.MMO.MeTaInvalidKey
  49. end
  50. local func = MetaValueGetDef[key]
  51. if not func then
  52. return global.MMO.MeTaInvalidValue
  53. end
  54. return func(...)
  55. end
  56. function MetaValueProxy:PrintMetaKey()
  57. print("---------------------------------------------")
  58. SL:print(" Meta Value Get begin --------------------")
  59. for key, func in pairs(MetaValueGetDef) do
  60. SL:print(" -----> " .. key)
  61. end
  62. SL:print(" Meta Value Get end ----------------------")
  63. -- Set
  64. print("")
  65. print("---------------------------------------------")
  66. SL:print(" Meta Value Set begin --------------------")
  67. for key, func in pairs(MetaValueSetDef) do
  68. SL:print(" -----> " .. key)
  69. end
  70. SL:print(" Meta Value Set end ----------------------")
  71. end
  72. function MetaValueProxy:PrintAllMetaValue()
  73. SL:print(" Meta Value Get begin --------------------")
  74. for key, func in pairs(MetaValueGetDef) do
  75. SL:print(key, " -----> ", func())
  76. end
  77. SL:print(" Meta Value Get end ----------------------")
  78. end
  79. function MetaValueProxy:SetValueByKey(key, ...)
  80. if not key then
  81. return global.MMO.MeTaInvalidKey
  82. end
  83. local func = MetaValueSetDef[key]
  84. if not func then
  85. return global.MMO.MeTaInvalidValue
  86. end
  87. return func(...)
  88. end
  89. return MetaValueProxy