Class: Doom::WadDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/doom/wad_downloader.rb

Overview

Downloads the shareware DOOM1.WAD if not present

Defined Under Namespace

Classes: DownloadError

Constant Summary collapse

SHAREWARE_URL =

Official Doom shareware WAD, uploaded by id Software to archive.org

'https://archive.org/download/doom-shareware_1996/DOOM1.WAD'
SHAREWARE_SIZE =

Expected size in bytes

4_196_020
SHAREWARE_FILENAME =
'doom1.wad'

Class Method Summary collapse

Class Method Details

.download_from_url(url, destination) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/doom/wad_downloader.rb', line 105

def self.download_from_url(url, destination)
  uri = URI.parse(url)
  downloaded = 0

  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    request = Net::HTTP::Get.new(uri)

    http.request(request) do |response|
      case response
      when Net::HTTPRedirection
        return download_from_url(response['location'], destination)
      when Net::HTTPSuccess
        total_size = response['content-length']&.to_i || SHAREWARE_SIZE

        File.open(destination, 'wb') do |file|
          response.read_body do |chunk|
            file.write(chunk)
            downloaded += chunk.size
            print_progress(downloaded, total_size)
          end
        end
      else
        raise DownloadError, "Download failed: #{response.code} #{response.message}"
      end
    end
  end
end

.download_shareware(destination) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/doom/wad_downloader.rb', line 59

def self.download_shareware(destination)
  puts
  puts "Downloading DOOM shareware..."

  # Create directory if needed
  FileUtils.mkdir_p(File.dirname(destination))

  uri = URI.parse(SHAREWARE_URL)
  downloaded = 0

  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    request = Net::HTTP::Get.new(uri)

    http.request(request) do |response|
      case response
      when Net::HTTPRedirection
        # Follow redirect
        return download_from_url(response['location'], destination)
      when Net::HTTPSuccess
        total_size = response['content-length']&.to_i || SHAREWARE_SIZE

        File.open(destination, 'wb') do |file|
          response.read_body do |chunk|
            file.write(chunk)
            downloaded += chunk.size
            print_progress(downloaded, total_size)
          end
        end
      else
        raise DownloadError, "Download failed: #{response.code} #{response.message}"
      end
    end
  end

  puts
  puts "Downloaded to: #{destination}"
  puts

  # Verify file size
  actual_size = File.size(destination)
  if actual_size < 1_000_000  # Less than 1MB is suspicious
    File.delete(destination)
    raise DownloadError, "Download appears incomplete (#{actual_size} bytes)"
  end
end

.ensure_wad_available(custom_path = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/doom/wad_downloader.rb', line 17

def self.ensure_wad_available(custom_path = nil)
  # If custom path provided and exists, use it
  return custom_path if custom_path && File.exist?(custom_path)

  # Check current directory for doom1.wad
  local_wad = File.join(Dir.pwd, SHAREWARE_FILENAME)
  return local_wad if File.exist?(local_wad)

  # Check home directory .doom folder
  home_wad = File.join(Dir.home, '.doom', SHAREWARE_FILENAME)
  return home_wad if File.exist?(home_wad)

  # No WAD found - offer to download
  if custom_path
    raise DownloadError, "WAD file not found: #{custom_path}"
  end

  prompt_and_download(home_wad)
end


133
134
135
136
137
138
139
140
141
# File 'lib/doom/wad_downloader.rb', line 133

def self.print_progress(downloaded, total)
  percent = (downloaded.to_f / total * 100).to_i
  bar_width = 40
  filled = (percent * bar_width / 100)
  bar = '=' * filled + '-' * (bar_width - filled)
  mb_downloaded = (downloaded / 1_048_576.0).round(1)
  mb_total = (total / 1_048_576.0).round(1)
  print "\r[#{bar}] #{percent}% (#{mb_downloaded}/#{mb_total} MB)"
end

.prompt_and_download(destination) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/doom/wad_downloader.rb', line 37

def self.prompt_and_download(destination)
  puts "No DOOM WAD file found."
  puts
  puts "Would you like to download the shareware version of DOOM (4 MB)?"
  puts "This is the free, legally distributable version with Episode 1."
  puts
  print "Download shareware DOOM? [Y/n] "

  response = $stdin.gets&.strip&.downcase
  if response.nil? || response.empty? || response == 'y' || response == 'yes'
    download_shareware(destination)
    destination
  else
    puts
    puts "To play DOOM, you need a WAD file. Options:"
    puts "  1. Run 'doom' again and accept the shareware download"
    puts "  2. Copy your own doom1.wad or doom.wad to the current directory"
    puts "  3. Specify a WAD path: doom /path/to/your.wad"
    exit 1
  end
end