Module: Lich::Util::Update

Defined in:
documented/update.rb

Constant Summary collapse

STABLE_REF =

Update channel constants Update channel constants The stable reference branch for updates.

'main'
BETA_BRANCH_PREFIX =

Fallback branch prefix for beta updates.

'pre/beta'
ASSET_TARBALL_NAME =

The name of the asset tarball for updates.

'lich-5.tar.gz'

Class Method Summary collapse

Class Method Details

.announcevoid

This method returns an undefined value.

Announces the availability of a new version if applicable.

Examples:

Lich::Util::Update.announce


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'documented/update.rb', line 61

def self.announce
  self.prep_update
  if "#{LICH_VERSION}".chr == '5'
    if Gem::Version.new(@current) < Gem::Version.new(@update_to)
      if !@new_features.empty?
        respond ''; _respond monsterbold_start() + "*** NEW VERSION AVAILABLE ***" + monsterbold_end()
        respond ''; respond ''
        respond ''; respond @new_features
        respond ''
        respond ''; respond "If you are interested in updating, run '#{$clean_lich_char}lich5-update --update' now."
        respond ''
      end
    else
      respond ''; respond "Lich version #{LICH_VERSION} is good.  Enjoy!"; respond ''
    end
  else
    # lich version 4 - just say 'no'
    respond "This script does not support Lich #{LICH_VERSION}."
  end
end

.download_updatevoid

This method returns an undefined value.

Downloads and applies the update to the Lich5 ecosystem.

Examples:

Lich::Util::Update.download_update


399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'documented/update.rb', line 399

def self.download_update
  ## This is the workhorse routine that does the file moves from an update
  self.prep_update if @update_to.nil? or @update_to.empty?
  if Gem::Version.new("#{@update_to}") <= Gem::Version.new("#{@current}")
    respond ''; respond "Lich version #{LICH_VERSION} is good.  Enjoy!"; respond ''
  else
    respond; respond 'Getting reaady to update.  First we will create a'
    respond 'snapshot in case there are problems with the update.'

    self.snapshot

    # download the requested update (can be prod release, or beta)
    respond; respond "Downloading Lich5 version #{@update_to}"; respond
    filename = "lich5-#{@update_to}"
    File.open(File.join(TEMP_DIR, "#{filename}.tar.gz"), "wb") do |file|
      file.write URI.parse(@zipfile).open.read
    end

    # unpack and prepare to use the requested update
    FileUtils.mkdir_p(File.join(TEMP_DIR, filename))
    Gem::Package.new("").extract_tar_gz(File.open(File.join(TEMP_DIR, "#{filename}.tar.gz"), "rb"), File.join(TEMP_DIR, filename))
    new_target = Dir.children(File.join(TEMP_DIR, filename))
    FileUtils.cp_r(File.join(TEMP_DIR, filename, new_target[0]), TEMP_DIR)
    FileUtils.remove_dir(File.join(TEMP_DIR, filename))
    FileUtils.mv(File.join(TEMP_DIR, new_target[0]), File.join(TEMP_DIR, filename))

    # delete all existing lib files to not leave old ones behind
    FileUtils.rm_rf(Dir.glob(File.join(LIB_DIR, "*")))

    respond; respond 'Copying updated lich files to their locations.'

    ## We do not care about local edits from players in the Lich5 / lib location
    FileUtils.copy_entry(File.join(TEMP_DIR, filename, "lib"), File.join(LIB_DIR))
    respond; respond "All Lich lib files have been updated."; respond

    ## Use new method so can be reused to do a blanket update of core data & scripts
    self.update_core_data_and_scripts(@update_to)

    ## Finally we move the lich.rbw file into place to complete the update.  We do
    ## not need to save a copy of this in the TEMP_DIR as previously done, since we
    ## took the snapshot at the beginning.
    lich_to_update = File.join(LICH_DIR, File.basename($PROGRAM_NAME))
    update_to_lich = File.join(TEMP_DIR, filename, "lich.rbw")
    File.open(update_to_lich, 'rb') { |r| File.open(lich_to_update, 'wb') { |w| w.write(r.read) } }

    ## And we clen up after ourselves
    FileUtils.remove_dir(File.join(TEMP_DIR, filename)) # we know these exist because
    FileUtils.rm(File.join(TEMP_DIR, "#{filename}.tar.gz")) # we just processed them

    respond; respond "Lich5 has been updated to Lich5 version #{@update_to}"
    respond "You should exit the game, then log back in.  This will start the game"
    respond "with your updated Lich.  Enjoy!"
  end
end

.fetch_github_json(url) ⇒ Hash?

Fetches JSON data from the specified GitHub URL with caching.

Examples:

Lich::Util::Update.fetch_github_json('https://api.github.com/repos/elanthia-online/lich-5/releases')

Parameters:

  • url (String)

    The URL to fetch JSON data from.

Returns:

  • (Hash, nil)

    The parsed JSON data or nil on error.

Raises:

  • (StandardError)

    If there is a network error.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'documented/update.rb', line 259

def self.fetch_github_json(url)
  now = Time.now.to_i
  entry = @_http_cache[url]
  if entry && (now - entry[:ts] < @_http_cache_ttl)
    return entry[:data]
  end
  begin
    raw = URI.parse(url).open.read
    data = JSON::parse(raw)
    @_http_cache[url] = { ts: now, data: data }
    data
  rescue => e
    respond "Update notice: network error fetching #{url.split('/repos/').last || url} (fetch_github_json): #{e.message}"
    nil
  end
end

.helpvoid

This method returns an undefined value.

Displays help information for update commands.

Examples:

Lich::Util::Update.help


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'documented/update.rb', line 86

def self.help
  respond "
    --help                   Display this message
    --announce               Get summary of changes for next version
    --update                 Update all changes for next version
    --snapshot               Grab current snapshot of Lich5 ecosystem and put in backup
    --revert                 Roll the Lich5 ecosystem back to the most recent snapshot

  Example usage:

  [One time suggestions]
    #{$clean_lich_char}autostart add --global lich5-update --announce    Check for new version at login
    #{$clean_lich_char}autostart add --global lich5-update --update      To auto accept all updates at login

  [On demand suggestions]
    #{$clean_lich_char}lich5-update --announce                  Check to see if a new version is available
    #{$clean_lich_char}lich5-update --update                    Update the Lich5 ecosystem to the current release
    #{$clean_lich_char}lich5-update --revert                    Roll the Lich5 ecosystem back to latest snapshot
    #{$clean_lich_char}lich5-update --script=<NAME>             Update an individual script file found in Lich-5
    #{$clean_lich_char}lich5-update --library=<NAME>            Update an individual library file found in Lich-5
    #{$clean_lich_char}lich5-update --data=<NAME>               Update an individual data file found in Lich-5

    *NOTE* If you use '--snapshot' in '#{$clean_lich_char}autostart' you will create a new
          snapshot folder every time you log a character in.  NOT recommended.
    "
end

.latest_prefixed_branch_greater_than(prefix, stable_major, stable_minor) ⇒ String?

Finds the latest branch with the specified prefix that is greater than the stable version.

Examples:

Lich::Util::Update.latest_prefixed_branch_greater_than('pre/beta', 5, 0)

Parameters:

  • prefix (String)

    The prefix to filter branches.

  • stable_major (Integer)

    The major version of the stable release.

  • stable_minor (Integer)

    The minor version of the stable release.

Returns:

  • (String, nil)

    The latest branch name or nil if not found.



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'documented/update.rb', line 315

def self.latest_prefixed_branch_greater_than(prefix, stable_major, stable_minor)
  branches = fetch_github_json('https://api.github.com/repos/elanthia-online/lich-5/branches?per_page=100')
  return nil unless branches.is_a?(Array)
  names = branches.map { |b| b['name'] }.compact
  candidates = names.select { |n| n.start_with?(prefix) }
  filtered = candidates.select do |n|
    maj, min = self.major_minor_from(n)
    maj && min && ((maj > stable_major) || (maj == stable_major && min > stable_minor))
  end
  return nil if filtered.empty?
  begin
    filtered.max_by { |n| self.version_key(n) }
  rescue => e
    respond "Update notice: ordering branches (latest_prefixed_branch_greater_than): #{e.message}"
    filtered.sort.last
  end
end

.latest_prerelease_tag_greater_than(stable_major, stable_minor) ⇒ String?

Finds the latest prerelease tag greater than the specified stable version.

Examples:

Lich::Util::Update.latest_prerelease_tag_greater_than(5, 0)

Parameters:

  • stable_major (Integer)

    The major version of the stable release.

  • stable_minor (Integer)

    The minor version of the stable release.

Returns:

  • (String, nil)

    The latest prerelease tag or nil if not found.



293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'documented/update.rb', line 293

def self.latest_prerelease_tag_greater_than(stable_major, stable_minor)
  releases = fetch_github_json('https://api.github.com/repos/elanthia-online/lich-5/releases')
  return nil unless releases.is_a?(Array)
  prereleases = releases.select { |r| r['prerelease'] && r['tag_name'] }
  return nil if prereleases.empty?
  candidates = prereleases.select do |r|
    maj, min = self.major_minor_from(r['tag_name'])
    next false unless maj && min
    (maj > stable_major) || (maj == stable_major && min > stable_minor)
  end
  return nil if candidates.empty?
  tag = candidates.max_by { |r| self.version_key(r['tag_name']) }['tag_name']
  tag.sub(/^v/, '')
end

.latest_stable_tagString?

Retrieves the latest stable tag from GitHub releases.

Examples:

Lich::Util::Update.latest_stable_tag

Returns:

  • (String, nil)

    The latest stable tag or nil if not found.



280
281
282
283
284
285
# File 'documented/update.rb', line 280

def self.latest_stable_tag
  releases = fetch_github_json('https://api.github.com/repos/elanthia-online/lich-5/releases')
  return nil unless releases.is_a?(Array)
  stable = releases.select { |r| !r['prerelease'] && r['tag_name'] }.max_by { |r| self.version_key(r['tag_name']) }
  stable && stable['tag_name']
end

.major_minor_from(str) ⇒ Array<Integer, Integer>

Extracts the major and minor version numbers from a version string.

Examples:

Lich::Util::Update.major_minor_from('v5.0.0')

Parameters:

  • str (String)

    The version string to parse.

Returns:

  • (Array<Integer, Integer>)

    An array containing the major and minor version numbers.



357
358
359
360
361
362
363
364
365
366
367
368
# File 'documented/update.rb', line 357

def self.major_minor_from(str)
  return [nil, nil] if str.nil?
  s = str.to_s
  s = s.sub(/^v/, '')
  if s =~ /(\d+)\.(\d+)\.(\d+)/
    return [$1.to_i, $2.to_i]
  elsif s =~ /(\d+)\.(\d+)/
    return [$1.to_i, $2.to_i]
  else
    [nil, nil]
  end
end

.prep_betatest(type = nil, requested_file = nil) ⇒ void

This method returns an undefined value.

Prepares for beta testing of the next Lich release.

Examples:

Lich::Util::Update.prep_betatest('--beta')

Parameters:

  • type (String, nil) (defaults to: nil)

    The type of beta test to prepare for.

  • requested_file (String, nil) (defaults to: nil)

    The specific file to update for beta testing.



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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'documented/update.rb', line 158

def self.prep_betatest(type = nil, requested_file = nil)
  if type.nil?
    respond 'You are electing to participate in the beta testing of the next Lich release.'
    respond 'This beta test will include only Lich code, and does not include Ruby upates.'
    respond 'While we will do everything we can to ensure you have a smooth experience, '
    respond 'it is a test, and untoward things can result.  Please confirm your choice:'
    respond "Please confirm your participation:  #{$clean_lich_char}send Y or #{$clean_lich_char}send N"
    respond "You have 10 seconds to confirm, otherwise will be cancelled."
    # we are only going to get the next client-input line, and if it does not confirm, we bail
    # we are doing this to prevent hanging the client with various other inputs by the user
    sync_thread = $_CLIENT_ || $_DETACHABLE_CLIENT_
    timeout = Time.now + 10
    line = nil
    loop do
      line = sync_thread.gets
      break if line.is_a?(String) && line.strip =~ /^(?:<c>)?(?:#{$clean_lich_char}send|#{$clean_lich_char}s) /i
      break if Time.now > timeout
    end
    if line.is_a?(String) && line =~ /send Y|s Y/i
      @beta_response = 'accepted'
      respond 'Beta test installation accepted.  Thank you for considering!'
    else
      @beta_response = 'rejected'
      respond 'Aborting beta test installation request.  Thank you for considering!'
      respond
    end

    if @beta_response =~ /accepted/
      # Resolve a viable beta ref (enforces: prerelease minor > stable minor)
      ref = self.resolve_channel_ref(:beta)
      if ref.nil?
        respond 'No viable beta found. Aborting beta update.'
        return
      end
      releases_url = "https://api.github.com/repos/elanthia-online/lich-5/releases"
      update_info = URI.parse(releases_url).open.read
      releases = JSON::parse(update_info)
      record = releases.find { |r| r['prerelease'] && r['tag_name'] == (ref.start_with?('v') ? ref : "v#{ref}") }
      if record
        record.each { |entry, value|
          if entry.include? 'tag_name'
            @update_to = value.sub('v', '')
          elsif entry.include? 'assets'
            @holder = value
          elsif entry.include? 'body'
            @new_features = value.gsub(/\#\# What's Changed.+$/m, '').gsub(/<!--[\s\S]*?-->/, '')
          end
        }
        release_asset = @holder && @holder.find { |x| x['name'] =~ /\b#{ASSET_TARBALL_NAME}\b/ }
        if release_asset
          @zipfile = release_asset.fetch('browser_download_url')
        else
          @zipfile = "https://codeload.github.com/elanthia-online/lich-5/tar.gz/#{ref}"
        end
      else
        @update_to = ref.sub(/^v/, '')
        @zipfile = "https://codeload.github.com/elanthia-online/lich-5/tar.gz/#{ref}"
      end
      Lich::Util::Update.download_update
    elsif @beta_response =~ /rejected/
      nil
    else
      respond 'This is not where I want to be on a beta test request.'
      respond
    end
  else
    self.update_file(type, requested_file, 'beta')
  end
end

.prep_updatevoid

This method returns an undefined value.

Prepares for an update by fetching the latest release information.

Examples:

Lich::Util::Update.prep_update


374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'documented/update.rb', line 374

def self.prep_update
  latest = fetch_github_json("https://api.github.com/repos/elanthia-online/lich-5/releases/latest")
  if latest.is_a?(Hash) && latest['prerelease']
    all = fetch_github_json('https://api.github.com/repos/elanthia-online/lich-5/releases')
    if all.is_a?(Array)
      stable = all.select { |r| !r['prerelease'] && r['tag_name'] }.max_by { |r| self.version_key(r['tag_name']) }
      latest = stable if stable
    end
  end
  unless latest.is_a?(Hash)
    respond "Update notice: could not read latest release payload (prep_update)."
    return
  end

  @update_to = latest['tag_name'].to_s.sub('v', '')
  @holder = latest['assets']
  @new_features = latest['body'].to_s.gsub(/\#\# What's Changed.+$/m, '').gsub(/<!--[\s\S]*?-->/, '')
  release_asset = @holder && @holder.find { |x| x['name'] =~ /\b#{ASSET_TARBALL_NAME}\b/ }
  @zipfile = release_asset.fetch('browser_download_url')
end

.request(type = '--announce') ⇒ void

This method returns an undefined value.

Handles various update requests based on the command type.

Examples:

Lich::Util::Update.request('--update')

Parameters:

  • type (String) (defaults to: '--announce')

    The type of request to process.

Raises:

  • (StandardError)

    If the command is unknown.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'documented/update.rb', line 34

def self.request(type = '--announce')
  case type
  when /^(?:--announce|-a)\b/
    self.announce
  when /^--(?:beta|test)(?: --(?:(script|library|data))=(.+))?\b/
    self.prep_betatest($1&.dup, $2&.dup)
  when /^(?:--help|-h)\b/
    self.help # Ok, that's just wrong.
  when /^(?:--update|-u)\b/
    self.download_update
  when /^--refresh\b/
    respond; respond "This command has been removed."
  when /^(?:--revert|-r)\b/
    self.revert
  when /^--(?:(script|library|data))=(.+)\b/
    self.update_file($1&.dup, $2&.dup)
  when /^(?:--snapshot|-s)\b/ # this one needs to be after --script
    self.snapshot
  else
    respond; respond "Command '#{type}' unknown, illegitimate and ignored.  Exiting . . ."; respond
  end
end

.resolve_channel_ref(channel) ⇒ String?

Resolves the reference for the specified update channel.

Examples:

Lich::Util::Update.resolve_channel_ref(:beta)

Parameters:

  • channel (Symbol, String)

    The channel to resolve (e.g., :stable, :beta).

Returns:

  • (String, nil)

    The resolved reference or nil if not found.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'documented/update.rb', line 233

def self.resolve_channel_ref(channel)
  case channel
  when :stable, 'production'
    STABLE_REF
  when :beta
    # Determine latest stable (non-prerelease) to enforce minor-bump rule
    stable_tag = self.latest_stable_tag # e.g., "v5.14.3"
    stable_major, stable_minor = self.major_minor_from(stable_tag)
    env = ENV['LICH_BETA_REF']
    return env unless env.nil? || env.empty?
    tag = self.latest_prerelease_tag_greater_than(stable_major, stable_minor)
    return tag if tag
    branch = self.latest_prefixed_branch_greater_than(BETA_BRANCH_PREFIX, stable_major, stable_minor)
    return branch if branch
    nil
  else
    STABLE_REF
  end
end

.revertvoid

This method returns an undefined value.

Reverts the Lich5 ecosystem to the most recent snapshot.

Examples:

Lich::Util::Update.revert


458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'documented/update.rb', line 458

def self.revert
  ## Since the request is to roll-back, we will do so destructively
  ## without another snapshot and without worrying about saving files
  ## that can be reinstalled with the lich5-update --update command

  respond; respond 'Reverting Lich5 to previously installed version.'
  revert_array = Dir.glob(File.join(BACKUP_DIR, "*")).sort.reverse
  restore_snapshot = revert_array[0]
  if restore_snapshot.empty? or /L5-snapshot/ !~ restore_snapshot
    respond "No prior Lich5 version found. Seek assistance."
  else
    # delete all lib files
    FileUtils.rm_rf(Dir.glob(File.join(LIB_DIR, "*")))
    # copy all backed up lib files
    FileUtils.cp_r(File.join(restore_snapshot, "lib", "."), LIB_DIR)
    # delete array of core scripts
    @snapshot_core_script.each { |file|
      File.delete(File.join(SCRIPT_DIR, file)) if File.exist?(File.join(SCRIPT_DIR, file))
    }
    # copy all backed up core scripts (array to save, only array files in backup)
    FileUtils.cp_r(File.join(restore_snapshot, "scripts", "."), SCRIPT_DIR)

    # skip gameobj-data and spell-list (non-functional logically, previous versions
    # already present and current files may contain local edits)

    # update lich.rbw in stream because it is active (we hope)
    lich_to_update = File.join(LICH_DIR, File.basename($PROGRAM_NAME))
    update_to_lich = File.join(restore_snapshot, "lich.rbw")
    File.open(update_to_lich, 'rb') { |r| File.open(lich_to_update, 'wb') { |w| w.write(r.read) } }

    # as a courtesy to the player, remind which version they were rev'd back to
    targetversion = ''
    targetfile = File.open(File.join(LIB_DIR, "version.rb")).read
    targetfile.each_line do |line|
      if line =~ /LICH_VERSION\s+?=\s+?/
        targetversion = line.sub(/LICH_VERSION\s+?=\s+?/, '').sub('"', '')
      end
    end
    respond
    respond "Lich5 has been reverted to Lich5 version #{targetversion}"
    respond "You should exit the game, then log back in.  This will start the game"
    respond "with your previous version of Lich.  Enjoy!"
  end
end

.snapshotvoid

This method returns an undefined value.

Creates a snapshot of the current Lich core files.

Examples:

Lich::Util::Update.snapshot


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
# File 'documented/update.rb', line 117

def self.snapshot
  respond
  respond 'Creating a snapshot of current Lich core files ONLY.'
  respond
  respond 'You may also wish to copy your entire Lich5 folder to'
  respond 'another location for additional safety, after any'
  respond 'additional requested updates are completed.'

  ## Let's make the snapshot folder

  snapshot_subdir = File.join(BACKUP_DIR, "L5-snapshot-#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}")
  FileUtils.mkdir_p(snapshot_subdir)

  ## lich.rbw main file backup

  FileUtils.cp(File.join(LICH_DIR, File.basename($PROGRAM_NAME)), File.join(snapshot_subdir, File.basename($PROGRAM_NAME)))

  ## LIB folder backup and it's subfolders

  FileUtils.mkdir_p(File.join(snapshot_subdir, "lib"))
  FileUtils.cp_r(LIB_DIR, snapshot_subdir)

  ## here we should maintain a discrete array of script files (450K versus 10M plus)
  ## we need to find a better way without hving to maintain this list

  FileUtils.mkdir_p(File.join(snapshot_subdir, "scripts"))
  @snapshot_core_script.each { |file|
    FileUtils.cp(File.join(SCRIPT_DIR, file), File.join(snapshot_subdir, "scripts", file)) if File.exist?(File.join(SCRIPT_DIR, file))
  }

  respond
  respond 'Current Lich ecosystem files (only) backed up to:'
  respond "    #{snapshot_subdir}"
end

.update_core_data_and_scripts(version = LICH_VERSION) ⇒ void

This method returns an undefined value.

Updates core data and scripts for the specified version.

Examples:

Lich::Util::Update.update_core_data_and_scripts('5.0.0')

Parameters:

  • version (String) (defaults to: LICH_VERSION)

    The version to update to (default is the current LICH_VERSION).



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'documented/update.rb', line 613

def self.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

  updatable_scripts = {
    "all" => ["alias.lic", "autostart.lic", "go2.lic", "jinx.lic", "log.lic", "logxml.lic", "map.lic", "repository.lic", "vars.lic", "version.lic"],
    "gs"  => ["ewaggle.lic", "foreach.lic"],
    "dr"  => ["dependency.lic"]
  }

  ## We DO care about local edits from players to the Lich5 / data files
  ## specifically gameobj-data.xml and spell-list.xml.
  ## Let's be a little more purposeful and gentle with these two files.
  ["effect-list.xml"].each { |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
    self.update_file('data', file)
  }

  ## We do not care about local edits from players to the Lich5 / script location
  ## for CORE scripts (those required to run Lich5 properly)
  updatable_scripts["all"].each { |script| self.update_file('script', script) }
  updatable_scripts["gs"].each { |script| self.update_file('script', script) } if XMLData.game =~ /^GS/
  updatable_scripts["dr"].each { |script| self.update_file('script', script) } if XMLData.game =~ /^DR/

  ## Update Lich.db value with last updated version
  Lich.core_updated_with_lich_version = version
end

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

This method returns an undefined value.

Updates a specific file based on the type and requested file name.

Examples:

Lich::Util::Update.update_file('script', 'alias.lic')

Parameters:

  • type (String)

    The type of file to update (script, library, or data).

  • rf (String)

    The name of the file to update.

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

    The version channel to use (default is ‘production’).

Raises:

  • (StandardError)

    If the file cannot be updated.



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'documented/update.rb', line 511

def self.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 =~ /\.lic$/ ? requested_file_ext = ".lic" : requested_file_ext = "bad extension"
  when "library"
    location = LIB_DIR
    case version
    when "production"
      remote_repo = "https://raw.githubusercontent.com/elanthia-online/lich-5/#{resolve_channel_ref(:stable)}/lib"
    when "beta"
      ref = resolve_channel_ref(:beta)
      if ref.nil?
        respond 'No viable beta found. Aborting beta update.'
        return
      end
      remote_repo = "https://raw.githubusercontent.com/elanthia-online/lich-5/#{ref}/lib"
    end
    requested_file =~ /\.rb$/ ? requested_file_ext = ".rb" : requested_file_ext = "bad extension"
  when "data"
    location = DATA_DIR
    remote_repo = "https://raw.githubusercontent.com/elanthia-online/scripts/master/scripts"
    requested_file =~ /(\.(?:xml|ui))$/ ? requested_file_ext = $1&.dup : requested_file_ext = "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"

    # Rename existing file to .old if it exists
    if File.exist?(file_path)
      File.rename(file_path, old_file_path)
    end

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

      # If successful, move .tmp to final location
      File.rename(tmp_file_path, file_path)

      # Clean up .old file if everything succeeded
      File.delete(old_file_path) if File.exist?(old_file_path)

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

      # Clean up the .tmp file if it exists
      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

      # Restore the .old file if it exists
      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

.version_key(tag_or_name) ⇒ Gem::Version

Generates a version key from a tag or branch name for comparison.

Examples:

Lich::Util::Update.version_key('v5.0.0')

Parameters:

  • tag_or_name (String)

    The tag or branch name to generate a key from.

Returns:

  • (Gem::Version)

    The version key for comparison.



338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'documented/update.rb', line 338

def self.version_key(tag_or_name)
  s = tag_or_name.to_s
  # strip leading 'v'
  s = s.sub(/^v/, '')
  # if this looks like a branch or path (e.g., "pre/beta/5.15.0" or "pre/beta-5.15.0"),
  # extract the first version-looking substring to avoid prefix text interfering
  if s =~ /(\d+\.\d+(?:\.\d+)?(?:-[0-9A-Za-z\.]+)?)/ # captures 1.2 or 1.2.3 and optional suffix like -beta.
    s = Regexp.last_match(1)
  end
  # normalize beta prerelease so beta.10 > beta.9
  s = s.gsub('-beta.', '.beta.').gsub(/-beta(?!\.)/, '.beta')
  Gem::Version.new(s)
end