module RetroFlix

Emulator configuration Part of RetroFlix

Constants

CONFIG_FILES
Config
DISPLAY

Run on main display

Public Class Methods

background_job(&bl) click to toggle source
# File lib/workers.rb, line 9
def self.background_job(&bl)
  RFPool.perform &bl
end
base_url() click to toggle source
# File lib/scrape.rb, line 26
def self.base_url
  @base_url ||= db_config.url
end
cached(id, &setter) click to toggle source
# File lib/scrape.rb, line 9
def self.cached(id, &setter)
  @cache            ||= {}
  @cache_timestamps ||= {}
  return @cache[id] if @cache.key?(id) &&
                      ((Time.now - @cache_timestamps[id]) <
                       (60 * 60 * Config.meta.cache_time))
  @cache_timestamps[id] = Time.now
  @cache[id]            = setter.call
end
db_config() click to toggle source

TODO support using multiple databases (how resolve conflicts ?)

# File lib/scrape.rb, line 22
def self.db_config
  @db_config ||= Config.databases[Config.databases.default]
end
delete_game(system, game) click to toggle source
# File lib/library.rb, line 36
def self.delete_game(system, game)
  FileUtils.rm_rf("#{game_dir_for(system)}/#{game}")
end
desc_for(game_page) click to toggle source

Return html content of game descriptions

# File lib/scrape.rb, line 92
def self.desc_for(game_page)
  game_page.xpath(db_config.paths.desc).collect { |node|
    node.inner_text.encode("UTF-8", invalid: :replace, undef: :replace)
  }
end
download(system, game) click to toggle source

Download game, saves to system/game file

# File lib/scrape.rb, line 121
def self.download(system, game)
  dl_url = download_link_for(system, game)

  url  = "#{base_url}/#{dl_url}"

  http = Curl.get(url) { |http|
    http.headers['Referer'] = url
    http.follow_location = true
  }

  http.body_str
end
download_url_for(system, game) click to toggle source

Return download url for given system / game

# File lib/scrape.rb, line 71
def self.download_url_for(system, game)
  "#{game_url_for(system, game)}-download"
end
emulator_for(system) click to toggle source

Return configured emulator for system

# File lib/emulator.rb, line 9
def self.emulator_for(system)
  emulator = Config.emulators.send(system)
  "#{Config.emulators.env} #{emulator.bin} #{emulator.flags}"
end
extract_archive(archive) click to toggle source

Extract archive if valid format is detected, else simply return Currently only supports Zip files but other archive types may be added

# File lib/archive.rb, line 11
def self.extract_archive(archive)
  f = Tempfile.new('retroflix')
  f.write archive

  begin
    zf = Zip::File.open(f.path).first
    [zf.name, zf.get_input_stream.read]
  rescue Zip::Error => e
    archive
  end
end
game_dir_for(system) click to toggle source
# File lib/library.rb, line 9
def self.game_dir_for(system)
  "#{Config.meta.games_dir}#{system}"
end
game_meta_for(system, game) click to toggle source

Return hash of all metadata (screens, desc) for system/game

# File lib/scrape.rb, line 100
def self.game_meta_for(system, game)
  page = game_page_for(system, game)

  {:screens => screens_for(page),
   :descs   =>    desc_for(page) }
end
game_page_for(system, game) click to toggle source

Return parsed game info page

# File lib/scrape.rb, line 76
def self.game_page_for(system, game)
  url = game_url_for(system, game)
  cached(url) do
    Nokogiri::HTML(Curl.get(url).body_str)
  end
end
game_path_for(system, game) click to toggle source
# File lib/library.rb, line 13
def self.game_path_for(system, game)
  dir = "#{game_dir_for(system)}/#{game}"
  Dir.glob("#{dir}/*").first
end
game_url_for(system, game) click to toggle source

Return game url for given system / game

# File lib/scrape.rb, line 66
def self.game_url_for(system, game)
  "#{base_url}#{games_for(system)[game]}"
end
games_for(system) click to toggle source

Return a hash of all games (name to relative url of info page) for given systems

# File lib/scrape.rb, line 47
def self.games_for(system)
  url = system_url(system)
  cached(url) do
    http    = Curl.get url
    parser  = Nokogiri::HTML(http.body_str)
    games = {}
    parser.xpath(db_config.paths.games).each { |node|
      href  = node.attribute("href").to_s
      title = node.text
      games[title] = href unless db_config.omit.any? { |o| title =~ Regexp.new(o) }
    }

    games
  end
end
have_game?(system, game) click to toggle source
# File lib/library.rb, line 32
def self.have_game?(system, game)
  library_games_for(system).include?(game)
end
library_games_for(systems) click to toggle source
# File lib/library.rb, line 24
def self.library_games_for(systems)
  return library_games_for([systems]) unless systems.is_a?(Array)
  systems.collect { |sys|
    dir = "#{game_dir_for(sys)}/"
    Dir.glob("#{dir}*").collect { |g| g.gsub(dir, "") }
  }.flatten
end
play_game(system, game) click to toggle source

fork / execs emaulator process & detaches

# File lib/emulator.rb, line 16
def self.play_game(system, game)
  emulator = emulator_for(system).gsub("GAME", game_path_for(system, game))

  pid = fork{ exec emulator }
  Process.detach pid
  nil
end
screens_for(game_page) click to toggle source

Return full urls to all game screens

# File lib/scrape.rb, line 84
def self.screens_for(game_page)
  game_page.xpath(db_config.paths.screens).collect { |node|
    src = node.attribute("data-original").to_s
    src == "" || src.nil? ? nil : "http:#{src}"
  }.compact
end
system_url(system) click to toggle source
# File lib/scrape.rb, line 36
def self.system_url(system)
  "#{base_url}/#{db_config.systems[system]}"
end
systems() click to toggle source
# File lib/scrape.rb, line 32
def self.systems
  db_config.systems.keys
end
valid_system?(sys) click to toggle source
# File lib/scrape.rb, line 40
def self.valid_system?(sys)
  systems.collect { |s| s.to_s }.include? sys
end
write_game(system, game, game_file, contents) click to toggle source
# File lib/library.rb, line 18
def self.write_game(system, game, game_file, contents)
  dir = game_dir_for(system)
  FileUtils.mkdir_p("#{dir}/#{game}/") unless File.directory?("#{dir}/#{game}/")
  File.write("#{dir}/#{game}/#{game_file}", contents)
end