Class: Lich::Util::Update::FileUpdater

Inherits:
Object
  • Object
show all
Defined in:
documented/common/update/file_updater.rb

Overview

Handles the updating of files from repositories.

This class is responsible for managing updates to scripts, libraries, and data files, including handling repository-specific updates.

Instance Method Summary collapse

Constructor Details

#initialize(client, resolver) ⇒ FileUpdater

Initializes a new FileUpdater instance.

Parameters:

  • client (Object)

    the client used to fetch data from repositories

  • resolver (Object)

    the resolver for handling versioning



25
26
27
28
# File 'documented/common/update/file_updater.rb', line 25

def initialize(client, resolver)
  @client = client
  @resolver = resolver
end

Instance Method Details

#update_core_data_and_scripts(version = LICH_VERSION) ⇒ void

This method returns an undefined value.

Updates core data and scripts based on the game version.

Parameters:

  • version (String) (defaults to: LICH_VERSION)

    the version to use for updates (default: LICH_VERSION)

Raises:

  • (StandardError)

    if the game type is invalid



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'documented/common/update/file_updater.rb', line 207

def update_core_data_and_scripts(version = LICH_VERSION)
  if XMLData.game !~ /^GS|^DR/
    respond "invalid game type, unsure what scripts to update via Update.update_core_scripts"
    return
  end

  if XMLData.game =~ /^GS/
    ["effect-list.xml"].each do |file|
      transition_filename = "#{file}".sub(".xml", '')
      newfilename = File.join(DATA_DIR, "#{transition_filename}-#{Time.now.to_i}.xml")
      if File.exist?(File.join(DATA_DIR, file))
        File.open(File.join(DATA_DIR, file), 'rb') { |r| File.open(newfilename, 'wb') { |w| w.write(r.read) } }
        respond "The prior version of #{file} was renamed to #{newfilename}."
      end
      update_file('data', file)
    end
  end

  Lich.core_updated_with_lich_version = version
end

#update_file(type, rf, version = 'production') ⇒ void

This method returns an undefined value.

Updates a specified file based on its type and version.

Parameters:

  • type (String)

    the type of file to update (e.g., "script", "library", "data")

  • rf (String)

    the requested file name

  • version (String) (defaults to: 'production')

    the version channel to use (default: 'production')

Raises:

  • (StandardError)

    if the file cannot be updated



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'documented/common/update/file_updater.rb', line 115

def update_file(type, rf, version = 'production')
  if version =~ /^(?:staging|master)$/i
    respond 'Requested channel %s mapped to main (stable).' % [version]
    version = 'production'
  end
  requested_file = rf
  case type
  when "script"
    location = SCRIPT_DIR
    if requested_file.downcase == 'dependency.lic'
      remote_repo = "https://raw.githubusercontent.com/elanthia-online/dr-scripts/main"
    else
      remote_repo = "https://raw.githubusercontent.com/elanthia-online/scripts/master/scripts"
    end
    requested_file_ext = requested_file =~ /\.lic$/ ? ".lic" : "bad extension"
  when "library"
    location = LIB_DIR
    case version
    when "production"
      remote_repo = "https://raw.githubusercontent.com/#{GITHUB_REPO}/#{@resolver.resolve_channel_ref(:stable)}/lib"
    when "beta"
      ref = @resolver.resolve_channel_ref(:beta)
      if ref.nil?
        respond 'No viable beta found. Aborting beta update.'
        return
      end
      remote_repo = "https://raw.githubusercontent.com/#{GITHUB_REPO}/#{ref}/lib"
    end
    requested_file_ext = requested_file =~ /\.rb$/ ? ".rb" : "bad extension"
  when "data"
    location = DATA_DIR
    remote_repo = "https://raw.githubusercontent.com/elanthia-online/scripts/master/scripts"
    requested_file_ext = requested_file =~ /(\.(?:xml|ui))$/ ? $1&.dup : "bad extension"
  end

  unless requested_file_ext == "bad extension"
    file_path = File.join(location, requested_file)
    tmp_file_path = file_path + ".tmp"
    old_file_path = file_path + ".old"

    File.rename(file_path, old_file_path) if File.exist?(file_path)

    begin
      File.open(tmp_file_path, "wb") do |file|
        file.write URI.parse(File.join(remote_repo, requested_file)).open.read
      end

      File.rename(tmp_file_path, file_path)
      File.delete(old_file_path) if File.exist?(old_file_path)

      respond
      respond "#{requested_file} has been updated."
    rescue StandardError => e
      respond
      respond "Error updating #{requested_file}: #{e.class} - #{e.message}"
      respond "Backtrace: #{e.backtrace.first(3).join(' | ')}" if $debug

      if File.exist?(tmp_file_path)
        begin
          File.delete(tmp_file_path)
          respond "Cleaned up incomplete temporary file."
        rescue => cleanup_error
          respond "Warning: Could not delete temporary file: #{cleanup_error.message}"
        end
      end

      if File.exist?(old_file_path)
        begin
          File.rename(old_file_path, file_path)
          respond "Restored original file."
        rescue => restore_error
          respond "Warning: Could not restore original file: #{restore_error.message}"
        end
      end

      respond
      respond "The filename #{requested_file} is not available via lich5-update."
      respond "Check the spelling of your requested file, or use '#{$clean_lich_char}jinx' to"
      respond "download #{requested_file} from another repository."
    end
  else
    respond
    respond "The requested file #{requested_file} has an incorrect extension."
    respond "Valid extensions are '.lic' for scripts, '.rb' for library files,"
    respond "and '.xml' or '.ui' for data files. Please correct and try again."
  end
end

#update_file_from_repo(type, repo_key, filename) ⇒ void

This method returns an undefined value.

Updates a file from the specified repository.

Parameters:

  • type (String)

    the type of file to update (e.g., "script", "data")

  • repo_key (String)

    the key identifying the repository

  • filename (String)

    the name of the file to update

Raises:

  • (StandardError)

    if the file cannot be updated



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
104
105
106
107
# File 'documented/common/update/file_updater.rb', line 36

def update_file_from_repo(type, repo_key, filename)
  config = SCRIPT_REPOS[repo_key]
  unless config
    custom_reg = CustomRepos.all[repo_key]
    config = CustomRepos.build_config(repo_key, custom_reg) if custom_reg
  end
  unless config
    all_keys = (SCRIPT_REPOS.keys + CustomRepos.all.keys).join(', ')
    respond "[lich5-update: Unknown repository '#{repo_key}'. Known: #{all_keys}]"
    return
  end

  case type
  when "script"
    location = config[:dest_dir] || SCRIPT_DIR
    FileUtils.mkdir_p(location) if config[:dest_dir]
  when "data"
    data_subdir = (config[:subdirs] || {})['data']
    location = data_subdir ? data_subdir[:dest] : File.join(SCRIPT_DIR, 'data')
    FileUtils.mkdir_p(location)
  else
    respond "[lich5-update: repo:filename syntax is only supported for --script= and --data=.]"
    return
  end

  prefix = config[:script_prefix]
  tree_data = @client.fetch_github_json(config[:api_url])
  unless tree_data && tree_data['tree']
    name = config[:display_name] || repo_key
    StatusReporter.respond_mono("[lich5-update: Failed to fetch repository tree for #{name}.]")
    return
  end

  raw_path = if type == "data"
               data_subdir = (config[:subdirs] || {})['data']
               if data_subdir && data_subdir[:pattern]
                 match = tree_data['tree'].find { |e| e['path'] =~ data_subdir[:pattern] && File.basename(e['path']) == filename }
                 match ? match['path'] : nil
               else
                 prefix ? "#{prefix}/#{filename}" : filename
               end
             elsif prefix
               "#{prefix}/#{filename}"
             else
               filename
             end
  remote_entry = raw_path ? tree_data['tree'].find { |e| e['path'] == raw_path } : nil
  if remote_entry
    local_path = File.join(location, filename)
    if File.exist?(local_path)
      local_sha = Digest::SHA1.hexdigest("blob #{File.binread(local_path).bytesize}\0#{File.binread(local_path)}")
      if local_sha == remote_entry['sha']
        StatusReporter.respond_mono("[lich5-update: #{filename} is already up to date.]")
        return
      end
    end
  else
    name = config[:display_name] || repo_key
    StatusReporter.respond_mono("[lich5-update: #{filename} not found in #{name} repository.]")
    return
  end

  name = config[:display_name] || repo_key
  url = "#{config[:raw_base_url]}/#{raw_path}"
  content = @client.http_get(url, auth: false)
  if content
    FileWriter.safe_write(File.join(location, filename), content)
    StatusReporter.respond_mono("[lich5-update: #{filename} has been updated from #{name}.]")
  else
    StatusReporter.respond_mono("[lich5-update: Failed to download #{filename} from #{name}.]")
  end
end