查看: 5543|回复: 8

[原创发布] 获取Fiber实例的状态编号,定义alive?

[复制链接]

老黄鸡

梦石
4
星屑
46133
在线时间
7978 小时
注册时间
2009-7-6
回帖
13342

MZ评测员RM创作大赛01组委会开拓者贵宾

发表于 2017-12-4 20:54:41 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
根据官方文档:http://ruby-doc.org/core-2.2.0/Fiber.html
使用fiber实例的alive?方法需要
You need to require 'fiber' before using this method.

有时候VA里也需要这么个操作,所以就出现了下面的玩意儿
思路也很简单,因为在resume一个status为TERMINATED的fiber时,会抛出dead fiber异常,
也就是说本身Fiber就存在判定自身状态的方法。通过这个提示的位置逆向出储存状态编号的地址,接着根据官方提供的源码逻辑,来定义alive?方法

  1. class Fiber
  2.   
  3.   Memcopy = Win32API.new('kernel32','RtlMoveMemory','pll','l')
  4.   
  5.   CREATING = 0
  6.   RUNNING = 1
  7.   TERMINATED = 2
  8.   
  9.   def status
  10.     buf = "\0"*4
  11.     Memcopy.call(buf,self.object_id*2+16,4)
  12.     objPtr = buf.unpack("L").first
  13.     return 0 if objPtr == 0
  14.     Memcopy.call(buf,objPtr+480,4)
  15.     return buf.unpack("L").first
  16.   end
  17.   
  18.   def alive?
  19.     return self.status != TERMINATE
  20.   end
  21.   
  22. end
复制代码



评分

参与人数 2赞 +2 收起 理由
魔法☆梅莉 + 1 666
⑨姐姐 + 1 我很赞同,精品文章,认可答案.

查看全部评分

RGDirect - DirectX驱动的RGSS,点我了解.
(排满,暂停)RM全系列成套系统定制请联系QQ1213237796
不接受对其他插件维护的委托
梦石
28
星屑
10170
在线时间
4673 小时
注册时间
2011-8-22
回帖
1216

开拓者

发表于 2017-12-5 08:59:55 | 显示全部楼层
也来回复一下自己写的不用黑科技的#alive,以及带了个::current和::fork

  1. class Fiber
  2.   @@call_stack = []
  3.   
  4.   attr_accessor :alive
  5.   attr_accessor :children
  6.   
  7.   alias old_initialize initialize
  8.   def initialize(&block)
  9.     old_initialize(&block)
  10.     @alive = true
  11.     @children = []
  12.   end
  13.   
  14.   def self.current
  15.     @@call_stack.last
  16.   end
  17.   
  18.   alias old_resume resume
  19.   def resume
  20.     @@call_stack.push(self)
  21.     @children.each { |child|
  22.       child.resume
  23.     }
  24.     @children.delete_if { |child|
  25.       !child.alive
  26.     }
  27.     begin
  28.       old_resume
  29.     rescue FiberError => e
  30.       @alive = false
  31.     end
  32.     @@call_stack.pop
  33.   end
  34.   
  35.   def self.fork(&block)
  36.     if Fiber.current
  37.       child = Fiber.new(&block)
  38.       Fiber.current.children << child
  39.       child.resume
  40.     end
  41.   end
  42. end
复制代码


测试代码
  1. f = Fiber.new do
  2.   p "Main Fiber Begin"
  3.   Fiber.yield
  4.   p "Main Fiber Before Forking"
  5.   Fiber.fork do
  6.     p "Child Fiber Begin"
  7.     Fiber.yield
  8.     p "Child Fiber Run"
  9.     Fiber.yield
  10.     p "Child Fiber End"
  11.   end
  12.   p "Main Fiber After Forking"
  13.   Fiber.yield
  14.   p "Main Fiber End"
  15. end
  16. p f.alive
  17. f.resume
  18. p f.alive
  19. f.resume
  20. p f.alive
  21. f.resume
  22. p f.alive
  23. f.resume
  24. p f.alive
复制代码

评分

参与人数 1赞 +1 收起 理由
fux2 + 1 精品文章

查看全部评分

回复

使用道具 举报

梦石
0
星屑
9292
在线时间
2504 小时
注册时间
2011-5-20
回帖
14521

开拓者

发表于 2017-12-5 14:09:01 | 显示全部楼层
和dead取反的区别是什么呢?

点评

如果存在dead的话,没有  发表于 2017-12-5 16:13
[img]http://service.t.sina.com.cn/widget/qmd/5339802982/c02e16bd/7.png
回复

使用道具 举报

梦石
0
星屑
4628
在线时间
1208 小时
注册时间
2016-4-7
回帖
944

开拓者

发表于 2017-12-5 20:39:49 | 显示全部楼层
  1.       @fiber = Fiber.new do
  2.         @callback.call(helper, info)
  3.         @fiber = nil
  4.       end
复制代码


弱鸡如我就用这样的土办法了

点评

嗯 主要还是我当时用fiber的地方是用个类对原版fiber做过包装的 所以实际上也没啥重复工作量233 思路上其实和9姐姐alias initialize差不多  发表于 2017-12-6 16:05
单纯为了减少重复工作量  发表于 2017-12-6 10:10
附庸的附庸不是我的附庸,女儿的女儿还是我的女儿。CK2沉迷ing
回复

使用道具 举报

梦石
1
星屑
24088
在线时间
3339 小时
注册时间
2011-7-8
回帖
3862

开拓者

发表于 2021-5-24 08:58:55 | 显示全部楼层
本帖最后由 guoxiaomi 于 2021-5-26 16:17 编辑

写了几天上论坛一搜Fiber,果然造了轮子……本意是准备写一个桌游模拟器,但是想在游戏主流程结算卡住的时候也能打开菜单或者播放动画等,所以决定使用Fiber。
然后发现坑爹的rgss3居然没有alive?和current方法,跟文档上不一致啊哭……

Fiber_Tree
  1. # encoding: utf-8
  2. module OJ
  3.   class Fiber_Tree
  4.     Node = Struct.new(:data, :parent, :childern)
  5.     Data = Struct.new(:fiber, :args, :condition, :results, :alive, :alive_childern)
  6.     attr_reader :current, :leafs

  7.     def initialize
  8.       # root can only be used as parent node
  9.       @root = Node.new
  10.       @leafs = []
  11.       @hold_parents = []
  12.     end

  13.     def insert_leafs(data_list, parent_ix)
  14.       if parent_ix == -1
  15.         nodes = data_list.collect do |data|
  16.           Node.new(data, @root, [])
  17.         end
  18.         @leafs += nodes
  19.       else
  20.         parent = @leafs[parent_ix]
  21.         parent.data.alive = false
  22.         @hold_parents.push(parent)
  23.         # parent = @leafs.delete_at(parent_ix)
  24.         nodes = data_list.collect do |data|
  25.           Node.new(data, parent, [])
  26.         end
  27.         parent.childern.replace(nodes)
  28.         parent.data.results.replace([nil] * data_list.size)
  29.         if parent.data.alive_childern == 0
  30.           parent.data.alive_childern = data_list.size
  31.         end
  32.         @leafs += nodes
  33.       end
  34.     end

  35.     def delete_leaf(leaf_ix)
  36.       leaf = @leafs[leaf_ix]
  37.       leaf.data.alive = false
  38.       # only @root.parent is nil
  39.       if !leaf.parent.parent.nil?
  40.         if leaf.parent.data.alive_childern == -1
  41.           leaf.parent.childern.each { |node| node.data.alive = false }
  42.           leaf.parent.data.alive_childern = 0
  43.           @leafs.push(leaf.parent)
  44.           leaf.parent.childern.clear
  45.         else
  46.           leaf.parent.data.alive_childern -= 1
  47.           if leaf.parent.data.alive_childern == 0
  48.             @leafs.push(leaf.parent)
  49.             leaf.parent.childern.clear
  50.           end
  51.         end
  52.       end
  53.     end

  54.     def update
  55.       flag_idle = true
  56.       flag_dead_fiber = false
  57.       @leafs.each_with_index do |leaf, leaf_ix|
  58.         @current, @current_ix = leaf, leaf_ix
  59.         if !leaf.data.alive
  60.           flag_dead_fiber ||= true
  61.           next
  62.         end
  63.         # produce with condition
  64.         condition = leaf.data.condition
  65.         if condition.is_a?(Proc)
  66.           next if !condition.call()
  67.           # condition is a proc, fiber must not finish yet
  68.           # run condition and resume if condition return true
  69.           ret = leaf.data.fiber.resume
  70.         else
  71.           # condition is nil
  72.           # the fiber might dead without return value
  73.           # or just simple called: Fiber.yield
  74.           ret = nil
  75.           if leaf.data.args.nil?
  76.             begin
  77.               ret = leaf.data.fiber.resume
  78.             rescue FiberError
  79.               # fiber is terminated, delete leaf
  80.               # the last results is nil, no need save to results
  81.               flag_dead_fiber ||= true
  82.               delete_leaf(leaf_ix)
  83.               next
  84.             end
  85.           else
  86.             # pass args when first resume fiber
  87.             ret = leaf.data.fiber.resume(*leaf.data.args)
  88.             # clear args
  89.             leaf.data.args = nil
  90.           end
  91.         end
  92.         flag_idle &&= false
  93.         # produce with result
  94.         if ret.is_a?(Proc) || ret.nil?
  95.           # condition can only be nil or lambda
  96.           leaf.data.condition = ret
  97.         else
  98.           # only @root.parent is nil
  99.           if !leaf.parent.parent.nil?
  100.             # result is not nil or lambda,
  101.             # the fiber should be terminated
  102.             # save result in parent's results
  103.             ix = leaf.parent.childern.find_index(leaf)
  104.             leaf.parent.data.results[ix] = ret
  105.           end
  106.           flag_dead_fiber ||= true
  107.           delete_leaf(leaf_ix)
  108.         end
  109.         # -------------------------------------------------
  110.         # the yield block is used for Graphics.update,
  111.         # when there's slow process, such as, lage data or heavy calculation
  112.         # pass empty block or comment next line when there's too much fibers
  113.         # -------------------------------------------------
  114.         yield leaf, leaf_ix if block_given?
  115.         @current = nil
  116.       end
  117.       if flag_dead_fiber
  118.         @leafs.delete_if { |leaf| leaf.data.alive == false }
  119.         @hold_parents.each { |parent| parent.data.alive = true }
  120.       end
  121.       flag_idle
  122.     end

  123.     def insert_fibers(fibers, root = false)
  124.       data_list = fibers.collect { |f, args|
  125.         Data.new(f, args, nil, [], true, 0)
  126.       }
  127.       insert_leafs(data_list, root ? -1 : @current_ix)
  128.     end
  129.   end

  130.   Time_Limit = (1000 / Graphics.frame_rate - 1) / 1000.0
  131.   Fiber_Dict = {}
  132.   @_wait_flag = false
  133.   @_wait_fibers = []
  134.   @_root_flag = false
  135.   @fiber_tree = Fiber_Tree.new

  136.   module_function

  137.   def update
  138.     @t = Time.now
  139.     nil until @fiber_tree.update { |leaf, leaf_ix|
  140.       if Time.now - @t >= Time_Limit
  141.         # puts "time out: %.3f, %d of %d" % [Time.now - @t - Time_Limit, leaf_ix, @fiber_tree.leafs.size]
  142.         Graphics.update
  143.         @t = Time.now
  144.       end
  145.     }
  146.     # nil until @fiber_tree.update
  147.     if @fiber_tree.leafs.empty?
  148.       # no fibers, use leafs.clear to force flush all fibers
  149.       SceneManager.return
  150.     end
  151.   end

  152.   def process(name, &block)
  153.     Fiber_Dict[name] = block
  154.   end

  155.   def start
  156.     @_root_flag = true
  157.     yield
  158.     @_root_flag = false
  159.   end

  160.   def join
  161.     @_wait_fibers.clear
  162.     @_wait_flag = true
  163.     yield
  164.     @_wait_flag = false
  165.     @fiber_tree.insert_fibers(@_wait_fibers)
  166.     @_wait_fibers.clear
  167.     Fiber.yield
  168.     return @fiber_tree.current.data.results
  169.   end

  170.   def listen(&b)
  171.     @fiber_tree.current.data.alive_childern = -1
  172.     join(&b)
  173.   end

  174.   def method_missing(method_name, *args, &block)
  175.     if !block_given?
  176.       proc = Fiber_Dict[method_name]
  177.       if proc
  178.         if !@_root_flag && !@_wait_flag
  179.           return proc.call(*args)
  180.         end
  181.         f = Fiber.new(&proc)
  182.         if @_root_flag
  183.           @fiber_tree.insert_fibers([[f, args]], true)
  184.           return
  185.         end
  186.         if @_wait_flag
  187.           @_wait_fibers.push([f, args])
  188.           return
  189.         end
  190.       end
  191.     end
  192.     super
  193.   end
  194. end
复制代码

定义好process后,简单的写一个scene就可以调用了。Fiber有3种唤起模式:start,join,listen。Fiber.yield里抛出一个lambda表达式作为此fiber的苏醒条件。
点击展开/收起
  1. module OJ
  2.   process :main do
  3.     puts "main start ..."
  4.     wait_for_n_frames(120)
  5.     join {
  6.       wait_for_n_frames(60)
  7.       wait_for_n_frames(120)
  8.       wait_for_n_frames(180)
  9.     }
  10.     puts "main finish ..."
  11.   end

  12.   process :wait_for_n_frames do |n|
  13.     puts "start wait"
  14.     c = Graphics.frame_count + n
  15.     Fiber.yield -> { Graphics.frame_count >= c }
  16.     puts "finish wait for #{n} frames"
  17.   end
  18. end

  19. class Scene_OJVA < Scene_Base
  20.   def start
  21.     OJ.start { OJ.main }
  22.   end

  23.   def update_basic
  24.     Graphics.update
  25.     # Input.update
  26.     OJ.update
  27.   end
  28. end
复制代码

2021/05/26:修复了几个BUG。黄鸡说得对,这种东西自己实现一遍理解会更深。这些代码就给后来的人做个参考吧。

点评

造轮子不是坏事,做过理解更深  发表于 2021-5-25 06:15

评分

参与人数 1赞 +1 收起 理由
fux2 + 1 塞糖

查看全部评分

熟悉rgss和ruby,xp区版主~
正在填坑:《膜拜组传奇》讲述膜拜组和学霸们的故事。
已上steam:与TXBD合作的Reformers《变革者》
* 战斗调用公共事件 *
* RGSOS 网络脚本 *
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部