Class: GameMachine::Actor::Reloadable

Inherits:
Object
  • Object
show all
Defined in:
lib/game_machine/actor/reloadable.rb

Class Method Summary collapse

Class Method Details

.actor_changed?(name) ⇒ Boolean

Returns:



71
72
73
74
75
76
77
78
79
# File 'lib/game_machine/actor/reloadable.rb', line 71

def actor_changed?(name)
  path = File.basename(name.underscore)
  if fileinfo = paths.fetch(path,nil)
    if fileinfo[:changed]
      return true
    end
  end
  false
end

.games_rootObject



24
25
26
# File 'lib/game_machine/actor/reloadable.rb', line 24

def games_root
  File.join(GameMachine.app_root,'/games')
end

.pathsObject



8
9
10
11
12
13
14
# File 'lib/game_machine/actor/reloadable.rb', line 8

def paths
  if @paths
    @paths
  else
    @paths = java.util.concurrent.ConcurrentHashMap.new
  end
end

.register(name) ⇒ Object



29
30
31
# File 'lib/game_machine/actor/reloadable.rb', line 29

def register(name)
  registered_actors[File.basename(name.underscore)] = name
end

.registered_actorsObject



16
17
18
19
20
21
22
# File 'lib/game_machine/actor/reloadable.rb', line 16

def registered_actors
  if @registered_actors
    @registered_actors
  else
    @registered_actors = java.util.concurrent.ConcurrentHashMap.new
  end
end

.reload_actor(basename) ⇒ Object



33
34
35
36
37
# File 'lib/game_machine/actor/reloadable.rb', line 33

def reload_actor(basename)
  if klassname = registered_actors.fetch(basename,nil)
    Base.find(klassname).tell('reload_because_changed')
  end
end

.reload_if_changed(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/game_machine/actor/reloadable.rb', line 81

def reload_if_changed(name)
  filename = File.basename(name.underscore)
  if fileinfo = paths.fetch(filename,nil)
    if fileinfo[:changed]
      GameMachine.logger.info "Reloading #{name} (#{fileinfo[:file]})"
      path = fileinfo[:file]
      klassname = name.split('::').last
      module_name = name.sub("::#{klassname}",'')
      module_name.constantize.send(:remove_const,klassname.to_sym)
      load "#{path}"
      fileinfo[:changed] = false
    else
      GameMachine.logger.info "Not reloading #{name} as it has not changed"
    end
  else
    GameMachine.logger.info "#{filename} not found in watch list"
  end
end

.update_paths(first_run = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/game_machine/actor/reloadable.rb', line 39

def update_paths(first_run=false)
  Dir.glob(File.join(games_root,'**','*.rb')).each do |file|
    mtime = File.mtime(file)
    basename = File.basename(file,'.rb')

    # existing file
    if fileinfo = paths.fetch(basename,nil)
      if mtime != fileinfo[:mtime]
        fileinfo[:changed] = true
        fileinfo[:mtime] = mtime
        GameMachine.logger.info "Game actor #{basename} changed"
        reload_actor(basename)
      end

    # new file
    else
      File.open(file,'rb') do |f|
        str = f.read
        if str.match(/Actor\:\:Development/)
          fileinfo = {:changed => true, :file => file, :mtime => mtime}
          if first_run
            fileinfo[:changed] = false
          end
          GameMachine.logger.info "Game actor #{basename} added to watch list changed=#{fileinfo[:changed]}"
          paths[basename] = fileinfo
        end
      end
    end

  end
end