源战役客户端
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.

123 rivejä
2.2 KiB

4 viikkoa sitten
  1. local create = coroutine.create
  2. local running = coroutine.running
  3. local resume = coroutine.resume
  4. local yield = coroutine.yield
  5. local error = error
  6. local unpack = unpack
  7. local debug = debug
  8. local FrameTimer = FrameTimer
  9. local CoTimer = CoTimer
  10. local comap = {}
  11. setmetatable(comap, {__mode = "kv"})
  12. function coroutine.start(f, ...)
  13. local co = create(f)
  14. if running() == nil then
  15. local flag, msg = resume(co, ...)
  16. if not flag then
  17. msg = debug.traceback(co, msg)
  18. LogError(msg)
  19. end
  20. else
  21. local args = {...}
  22. local timer = nil
  23. local action = function()
  24. local flag, msg = resume(co, unpack(args))
  25. if not flag then
  26. timer:Stop()
  27. msg = debug.traceback(co, msg)
  28. LogError(msg)
  29. end
  30. end
  31. timer = FrameTimer.New(action, 0, 1)
  32. comap[co] = timer
  33. timer:Start()
  34. end
  35. return co
  36. end
  37. function coroutine.wait(t, co, ...)
  38. local args = {...}
  39. co = co or running()
  40. local timer = nil
  41. local action = function()
  42. local flag, msg = resume(co, unpack(args))
  43. if not flag then
  44. timer:Stop()
  45. msg = debug.traceback(co, msg)
  46. LogError(msg)
  47. return
  48. end
  49. end
  50. timer = CoTimer.New(action, t, 1)
  51. comap[co] = timer
  52. timer:Start()
  53. return yield()
  54. end
  55. function coroutine.step(t, co, ...)
  56. local args = {...}
  57. co = co or running()
  58. local timer = nil
  59. local action = function()
  60. local flag, msg = resume(co, unpack(args))
  61. if not flag then
  62. timer:Stop()
  63. msg = debug.traceback(co, msg)
  64. LogError(msg)
  65. return
  66. end
  67. end
  68. timer = FrameTimer.New(action, t or 1, 1)
  69. comap[co] = timer
  70. timer:Start()
  71. return yield()
  72. end
  73. function coroutine.www(www, co)
  74. co = co or running()
  75. local timer = nil
  76. local action = function()
  77. if not www.isDone then
  78. return
  79. end
  80. timer:Stop()
  81. local flag, msg = resume(co)
  82. if not flag then
  83. msg = debug.traceback(co, msg)
  84. LogError(msg)
  85. return
  86. end
  87. end
  88. timer = FrameTimer.New(action, 1, -1)
  89. comap[co] = timer
  90. timer:Start()
  91. return yield()
  92. end
  93. function coroutine.stop(co)
  94. local timer = comap[co]
  95. if timer ~= nil then
  96. comap[co] = nil
  97. timer:Stop()
  98. end
  99. end