diff --git a/lib/ico.rb b/lib/ico.rb index 0f88d2b..12c4e0e 100644 --- a/lib/ico.rb +++ b/lib/ico.rb @@ -2,6 +2,8 @@ require "stringio" class W3DHub class ICO + PNG_IDENT = "\211PNG\r\n\032\n".force_encoding("ASCII-8BIT").freeze + IconDirectory = Struct.new(:reserved, :type, :image_count) IconDirectoryEntity = Struct.new( @@ -50,6 +52,9 @@ class W3DHub read_u32, read_u32 ) + + @images.last.width = 256 if @images.last.width == 0 + @images.last.height = 256 if @images.last.height == 0 end end @@ -65,26 +70,56 @@ class W3DHub @file.read(4).unpack1("V") end - def to_rgba32_blob(image) - @file.pos = image.image_offset - buf = @file.read(image.image_size) - - File.write("TEMP.bmp", buf) - end - def select_pngs @images.select do |image| - @file.pos = image.image_offset - buf = @file.read(8).unpack1("a*") - buf == "\211PNG\r\n\032\n".force_encoding("ASCII-8BIT") + image_png?(image) end end + def image_png?(image) + @file.pos = image.image_offset + buf = @file.read(8).unpack1("a*") + + buf == PNG_IDENT + end + def select_bmps + @images.select do |image| + image_bmp?(image) + end + end + + def image_bmp?(image) + !image_png?(image) + end + + def to_rgba32_blob(image) + @file.pos = image.image_offset + buf = StringIO.new(@file.read(image.image_size)) + + raise NotImplementedError "Cannot parse png based icons!" unless image_bmp?(image) + + raise NotImplementedError "Cannot parse #{image.bit_depth}" unless image.bit_depth == 32 + + blob = "".force_encoding("ASCII-8BIT") + + image.height.times do |y| + image.width.times do |x| + buf.pos = ((image.height - y) * image.width + x) * 4 + + blue = buf.read(1) + green = buf.read(1) + red = buf.read(1) + alpha = buf.read(1) + + blob << red + blob << green + blob << blue + blob << alpha + end + end + + Gosu::Image.from_blob(image.width, image.height, blob) end end end - -data = W3DHub::ICO.new(file: "/home/cyberarm/Downloads/icos/ar.ico") -pp data.select_pngs.size, data.images.size -# data.to_rgba32_blob(data.images.first) diff --git a/lib/pages/download_manager.rb b/lib/pages/download_manager.rb index bb682b5..f9206c6 100644 --- a/lib/pages/download_manager.rb +++ b/lib/pages/download_manager.rb @@ -28,7 +28,7 @@ class W3DHub background task.application.color flow(width: 0.70, height: 1.0) do - image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{task.app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{task.app_id}.png" : "#{GAME_ROOT_PATH}/media/ui_icons/question.png" + image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{task.app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{task.app_id}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png" @application_image = image image_path, height: 1.0 stack(margin_left: 8, width: 0.75) do diff --git a/lib/pages/games.rb b/lib/pages/games.rb index 6fccc12..1a6c46f 100644 --- a/lib/pages/games.rb +++ b/lib/pages/games.rb @@ -40,7 +40,7 @@ class W3DHub image "#{GAME_ROOT_PATH}/media/ui_icons/return.png", width: 1.0, color: Gosu::Color::GRAY if Store.application_manager.updateable?(game.id, game.channels.first.id) image "#{GAME_ROOT_PATH}/media/ui_icons/import.png", width: 0.5, color: 0x88_ffffff unless Store.application_manager.installed?(game.id, game.channels.first.id) end - image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{game.id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{game.id}.png" : "#{GAME_ROOT_PATH}/media/ui_icons/question.png" + image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{game.id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{game.id}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png" image image_path, height: 48, color: Store.application_manager.installed?(game.id, game.channels.first.id) ? 0xff_ffffff : 0x88_ffffff end diff --git a/lib/pages/server_browser.rb b/lib/pages/server_browser.rb index eb57ff7..683c74a 100644 --- a/lib/pages/server_browser.rb +++ b/lib/pages/server_browser.rb @@ -26,7 +26,7 @@ class W3DHub @filters.each do |app_id, enabled| app = Store.applications.games.find { |a| a.id == app_id.to_s } - image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{app_id}.png" : "#{GAME_ROOT_PATH}/media/ui_icons/question.png" + image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{app_id}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{app_id}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png" image image_path, tip: "#{app.name}", height: 1.0, border_thickness_bottom: 1, border_color_bottom: 0x00_000000, @@ -361,7 +361,7 @@ class W3DHub end def game_icon(server) - image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{server.game.nil? ? 'ren' : server.game}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{server.game.nil? ? 'ren' : server.game}.png" : "#{GAME_ROOT_PATH}/media/ui_icons/question.png" + image_path = File.exist?("#{GAME_ROOT_PATH}/media/icons/#{server.game.nil? ? 'ren' : server.game}.png") ? "#{GAME_ROOT_PATH}/media/icons/#{server.game.nil? ? 'ren' : server.game}.png" : "#{GAME_ROOT_PATH}/media/icons/default_icon.png" if server.status.password @server_locked_icons[server.game] ||= Gosu.render(96, 96) do diff --git a/media/icons/default_icon.png b/media/icons/default_icon.png new file mode 100644 index 0000000..1045fb8 Binary files /dev/null and b/media/icons/default_icon.png differ