# #-- # # $Id: controller.rb 197 2005-02-09 13:42:17Z thomas $ # # smagacor - a collection of small games in ruby # Copyright (C) 2004 Thomas Leitner # # This program is free software; you can redistribute it and/or modify it under the terms of the GNU # General Public License as published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License along with this program; if not, # write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # #++ # require 'rbconfig' require "yaml" require 'logger' class Object LOGGER = Logger.new( STDERR ) LOGGER.datetime_format = "%Y-%m-%d %H:%M:%S" def logger LOGGER end end class Logger def set_log_dev( dev ) @logdev = LogDevice.new( dev ) end end module Smagacor # The version of this copy of Smagacor VERSION = [0, 0, 1] class GameInfo Domain = 'thomasleitner,2004' Name = 'gameinfo' attr_reader :name attr_reader :icon attr_reader :description attr_reader :file attr_accessor :directory attr_reader :classname attr_reader :category def get_class_object @classname.split( /::/ ).inject( Object ) {|mod, name| mod.const_get( name )} end def ==( other ) @name == other.name && \ @icon == other.icon && \ @description == other.description && \ @file == other.file && \ @directory == other.directory && \ @classname == other.classname && \ @category == other.category end alias :to_s :to_yaml def to_yaml_type "!#{Domain}/#{Name}" end YAML::add_domain_type( Domain, Name ) do |type, val| YAML::object_maker( GameInfo, val ) end end class Controller attr_reader :gamespath attr_reader :games def initialize @gamespath = [ File.join( ::Config::CONFIG["datadir"], "smagacor" ), 'data/smagacor' ] if defined? Gem::Cache gem = Gem::Cache.from_installed_gems.search( "smagacor", "=#{::Smagacor::VERSION.join('.')}" ).last @gamespath << File.join( gem.full_gem_path, "data", "smagacor" ) if gem end begin homepath = File.expand_path( '~' ) rescue homepath = ENV['USERPROFILE'] || ENV['HOMEDRIVE']+ENV['HOMEPATH'] end @gamespath.unshift File.join( homepath, '.smagacor' ) @games = [] end def get_file( file ) @gamespath.each do |path| name = File.join( path, file ) return name if File.exists?( name ) end end def load_games @games = [] @gamespath.each do |path| Dir[path + '/*/game.info'].each do |file| gi = YAML::load( File.open( file ) ) gi.directory = File.dirname( file ) @games.push gi end end end end end