|
| 1 | +module Input |
| 2 | + # This works by: |
| 3 | + # 1. Overriding the `GTK::Runtime#tick_core method` (example found in lowrez.rb) |
| 4 | + # 2. It fetches 'https://github.com/marcheiligers/dr-input/releases/latest' which will redirect to the latest release. |
| 5 | + # DRGKT follows redirects, so we get the HTML from the final destination. |
| 6 | + # 3. That HTML contains OpenGraph tags including: |
| 7 | + # `<meta property="og:url" content="/marcheiligers/dr-input/releases/tag/v0.0.25" />` |
| 8 | + # which contains the latest version number. |
| 9 | + # 4. That then allows us to fetch that version from the releases using $gtk.download_stb_rb_raw |
| 10 | + # 5. Finally it aliases the original `tick_core` method back, and cleans up the methods and instance variables created. |
| 11 | + # State is maintained through the existence of method aliases and instance variables, which are removed on completion. |
| 12 | + # Error handling ensures everything is cleaned up if there is a failure. |
| 13 | + |
| 14 | + def self.download_update! |
| 15 | + return puts 'Already busy updating' if GTK::Runtime.instance_variable_defined?(:@__input_file_path) |
| 16 | + |
| 17 | + GTK::Runtime.alias_method(:__input_orig_tick_core, :tick_core) |
| 18 | + GTK::Runtime.instance_variable_set(:@__input_file_path, Input::DEVELOPMENT ? 'input.rb' : 'file.rb') |
| 19 | + |
| 20 | + GTK::Runtime.define_method(:tick_core) do |
| 21 | + __input_orig_tick_core |
| 22 | + |
| 23 | + if !instance_variable_defined?(:@__input_version_response) |
| 24 | + @__input_version_response = $gtk.http_get 'https://github.com/marcheiligers/dr-input/releases/latest' |
| 25 | + elsif @__input_version_response[:complete] && !instance_variable_defined?(:@__input_download_response) |
| 26 | + raise "Received status code #{@__input_version_response[:http_response_code]}" unless @__input_version_response[:http_response_code] == 200 |
| 27 | + |
| 28 | + data = @__input_version_response[:response_data] |
| 29 | + search = '<meta property="og:url" content="/marcheiligers/dr-input/releases/tag/v' |
| 30 | + start_char = data.index(search) + search.length |
| 31 | + end_char = data.index('"', start_char) |
| 32 | + @__input_version = data.slice(start_char...end_char) |
| 33 | + puts "Found version #{@__input_version}." |
| 34 | + raise "Remote version is the same as local #{@__input_version}" if @__input_version == Input::VERSION |
| 35 | + |
| 36 | + url = "https://github.com/marcheiligers/dr-input/releases/download/v#{@__input_version}/input.rb" |
| 37 | + @__input_download_response = $gtk.http_get url |
| 38 | + puts "download: #{@__input_download_response.inspect}" |
| 39 | + elsif instance_variable_defined?(:@__input_download_response) && @__input_download_response[:complete] |
| 40 | + raise "Received status code #{@__input_download_response[:http_response_code]}" unless @__input_download_response[:http_response_code] == 200 |
| 41 | + |
| 42 | + path = self.class.instance_variable_get(:@__input_file_path) |
| 43 | + data = @__input_download_response[:response_data] |
| 44 | + $gtk.write_file path, data |
| 45 | + |
| 46 | + puts "Updated to version #{@__input_version} at #{path}" |
| 47 | + __input_cleanup_update |
| 48 | + end |
| 49 | + rescue StandardError => e |
| 50 | + puts "Failed to download update: #{e.message}" |
| 51 | + __input_cleanup_update |
| 52 | + end |
| 53 | + |
| 54 | + GTK::Runtime.define_method(:__input_cleanup_update) do |
| 55 | + self.class.alias_method :tick_core, :__input_orig_tick_core |
| 56 | + self.class.remove_method :__input_orig_tick_core |
| 57 | + self.class.remove_method :__input_cleanup_update |
| 58 | + remove_instance_variable :@__input_version_response if instance_variable_defined? :@__input_version_response |
| 59 | + remove_instance_variable :@__input_version if instance_variable_defined? :@__input_version |
| 60 | + remove_instance_variable :@__input_download_response if instance_variable_defined? :@__input_download_response |
| 61 | + self.class.remove_instance_variable :@__input_file_path if self.class.instance_variable_defined? :@__input_file_path |
| 62 | + end |
| 63 | + end |
| 64 | +end |
0 commit comments