Class: MCDotArtMaker::TextureLoader
- Inherits:
-
Object
- Object
- MCDotArtMaker::TextureLoader
- Defined in:
- lib/mc_dot_art_maker/texture_loader.rb
Constant Summary collapse
- TEXTURE_DIR =
File.join(File.dirname(__FILE__),"textures")
- TEXTURE_TMP_DIR =
File.join(TEXTURE_DIR,"tmp")
Instance Method Summary collapse
- #exist?(filename) ⇒ Boolean
-
#initialize(jar = nil) ⇒ TextureLoader
constructor
A new instance of TextureLoader.
- #load_texture(filename) ⇒ Object
Constructor Details
#initialize(jar = nil) ⇒ TextureLoader
Returns a new instance of TextureLoader.
11 12 13 |
# File 'lib/mc_dot_art_maker/texture_loader.rb', line 11 def initialize(jar = nil) @jar = jar || jars.last end |
Instance Method Details
#exist?(filename) ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'lib/mc_dot_art_maker/texture_loader.rb', line 48 def exist?(filename) filename = filename + '.png' if File.extname(filename).empty? [ Dir.exist?(TEXTURE_DIR), File.exist?(File.join(TEXTURE_DIR,filename)) ].all? end |
#load_texture(filename) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mc_dot_art_maker/texture_loader.rb', line 15 def load_texture(filename) filename = filename + '.png' if File.extname(filename).empty? confirm = [ Dir.exist?(TEXTURE_DIR), File.exist?(File.join(TEXTURE_DIR,filename)) ] unless confirm.all? # copy jar and extract jar_basename = File.basename(@jar) Dir.mkdir(TEXTURE_DIR) unless Dir.exist?(TEXTURE_DIR) Dir.mkdir(TEXTURE_TMP_DIR) unless Dir.exist?(TEXTURE_TMP_DIR) jar_file_path = File.join(TEXTURE_TMP_DIR,jar_basename) FileUtils.cp(@jar,jar_file_path) if Dir.glob(File.join(TEXTURE_TMP_DIR,'*.jar')).empty? Zip::File.open(jar_file_path) do |zip| zip.each do |entry| if md = /\Aassets\/minecraft\/textures\/blocks\/#{filename}\Z/.match(entry.to_s) entry.extract(File.join(TEXTURE_DIR, filename)) return Magick::ImageList.new(File.join(TEXTURE_DIR,filename)).first end end end raise(TextureNotFoundError,"Texture \"#{filename}\" was not found") unless confirm.all? end Magick::ImageList.new(File.join(TEXTURE_DIR,filename)).first end |