Class: MCDotArtMaker::TextureLoader

Inherits:
Object
  • Object
show all
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

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

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/mc_dot_art_maker/texture_loader.rb', line 55

def exist?(filename)
  filename = filename + '.png' if File.extname(filename).empty?
  [
    Dir.exist?(TEXTURE_DIR),
    File.exist?(File.join(TEXTURE_DIR,filename))
  ].all?
end

#jarsObject



96
97
98
99
100
101
102
103
# File 'lib/mc_dot_art_maker/texture_loader.rb', line 96

def jars
  jars = Dir.glob(File.join(search_dir,"1.[8-9]{.[1-9],}")).map do |v|
    # File.join(search_dir, v,"#{v}.jar")
    basename = File.basename(v)
    File.join(v,"#{basename}.jar")
  end
  jars
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
47
48
49
50
51
52
53
# 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|
        # puts "entry #{entry.to_s}"

        # if md = /\Aassets\/minecraft\/textures\/blocks\/(.*).png\Z/.match(entry.to_s)
        #   # p md[1]
        #   entry.extract(File.join("textures", md[1]) + ".png")
        # end

        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

#osObject

private



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mc_dot_art_maker/texture_loader.rb', line 65

def os
  @os ||= (
  host_os = RbConfig::CONFIG['host_os']
  case host_os
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    :windows
  when /darwin|mac os/
    :macosx
  when /linux/
    :linux
  when /solaris|bsd/
    :unix
  else
    raise(UnknownOSError,"unknown os: #{host_os.inspect}")
  end
  )
end

#search_dirObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mc_dot_art_maker/texture_loader.rb', line 83

def search_dir
  case os
  when :windows
    File.join(ENV['AppData'],".minecraft", "versions")
  when :macosx
    File.join(ENV['HOME'], "Library", "Application Support", "minecraft", "versions")
  when :linux,:unix
    File.join(ENV['HOME'], ".minecraft", "versions")
  else
    raise(UnsupportedOSError,"unsupported os")
  end
end