Module: Lich::Common::GUI::YamlState

Defined in:
documented/common/gui/yaml_state.rb

Overview

Provides methods for managing YAML state files for Lich. This module includes functionality for loading, saving, and migrating entries in YAML format.

Examples:

Loading saved entries

entries = Lich::Common::GUI::YamlState.load_saved_entries(data_dir, autosort_state)

Class Method Summary collapse

Class Method Details

.add_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean

Adds a character to the favorites list in the YAML data.

Examples:

success = Lich::Common::GUI::YamlState.add_favorite(data_dir, username, char_name, game_code)

Parameters:

  • data_dir (String)

    The directory where the YAML file is located.

  • username (String)

    The username associated with the character.

  • char_name (String)

    The name of the character to add as a favorite.

  • game_code (String)

    The game code associated with the character.

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

    The frontend associated with the character.

Returns:

  • (Boolean)

    True if the character was added to favorites, false otherwise.

Raises:

  • (StandardError)

    If there is an error adding the favorite.



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
# File 'documented/common/gui/yaml_state.rb', line 469

def self.add_favorite(data_dir, username, char_name, game_code, frontend = nil)
  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)
  return false unless File.exist?(yaml_file)

  begin
    yaml_data = YAML.load_file(yaml_file)
    yaml_data = migrate_to_favorites_format(yaml_data)

    # Find the character with frontend precision
    character = find_character(yaml_data, username, char_name, game_code, frontend)
    return false unless character

    # Don't add if already a favorite
    return true if character['is_favorite']

    # Mark as favorite and assign order
    character['is_favorite'] = true
    character['favorite_order'] = get_next_favorite_order(yaml_data)
    character['favorite_added'] = Time.now.to_s

    # Save updated data directly without conversion round-trip
    # This preserves the original YAML structure and account ordering
    content = generate_yaml_content(yaml_data)
    result = Utilities.safe_file_operation(yaml_file, :write, content)

    result ? true : false
  rescue StandardError => e
    Lich.log "error: Error adding favorite: #{e.message}"
    false
  end
end

.change_encryption_mode(data_dir, new_mode, new_master_password = nil) ⇒ Boolean

Changes the encryption mode for the YAML data and updates the master password if necessary.

Examples:

success = Lich::Common::GUI::YamlState.change_encryption_mode(data_dir, :enhanced, new_master_password)

Parameters:

  • data_dir (String)

    The directory where the YAML file is located.

  • new_mode (Symbol)

    The new encryption mode to set.

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

    The new master password for enhanced mode.

Returns:

  • (Boolean)

    True if the change was successful, false otherwise.

Raises:

  • (StandardError)

    If there is an error changing the encryption mode.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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
# File 'documented/common/gui/yaml_state.rb', line 319

def self.change_encryption_mode(data_dir, new_mode, new_master_password = nil)
  yaml_file = yaml_file_path(data_dir)

  # Load YAML
  begin
    yaml_data = YAML.load_file(yaml_file)
  rescue StandardError => e
    Lich.log "error: Failed to load YAML for encryption mode change: #{e.message}"
    return false
  end

  current_mode = yaml_data['encryption_mode']&.to_sym || :plaintext

  # If already in target mode, return success
  if current_mode == new_mode
    Lich.log "info: Already in #{new_mode} encryption mode"
    return true
  end

  # Determine old_master_password
  old_master_password = nil
  if current_mode == :enhanced
    # Auto-retrieve from keychain when leaving Enhanced
    old_master_password = MasterPasswordManager.retrieve_master_password
    if old_master_password.nil?
      Lich.log "error: Master password not found in keychain for encryption mode change"
      return false
    end
  end

  # Validate new_master_password if entering Enhanced mode
  if new_mode == :enhanced && new_master_password.nil?
    Lich.log "error: New master password required for Enhanced mode encryption"
    return false
  end

  # Create backup
  backup_file = "#{yaml_file}.bak"
  begin
    FileUtils.cp(yaml_file, backup_file)
    Lich.log "info: Backup created for encryption mode change: #{backup_file}"
  rescue StandardError => e
    Lich.log "error: Failed to create backup: #{e.message}"
    return false
  end

  begin
    # Re-encrypt all accounts
    accounts = yaml_data['accounts'] || {}
    accounts.each do |, |
      # Decrypt with current mode
      plaintext = decrypt_password(
        ['password'],
        mode: current_mode,
        account_name: ,
        master_password: old_master_password
      )

      if plaintext.nil?
        Lich.log "error: Failed to decrypt password for #{}"
        return restore_backup_and_return_false(backup_file, yaml_file)
      end

      # Encrypt with new mode
      encrypted = encrypt_password(
        plaintext,
        mode: new_mode,
        account_name: ,
        master_password: new_master_password
      )

      if encrypted.nil?
        Lich.log "error: Failed to encrypt password for #{}"
        return restore_backup_and_return_false(backup_file, yaml_file)
      end

      ['password'] = encrypted
    end

    # Update encryption_mode
    yaml_data['encryption_mode'] = new_mode.to_s

    # Handle Enhanced mode metadata
    if new_mode == :enhanced
      # Create validation test
      validation_test = MasterPasswordManager.create_validation_test(new_master_password)
      yaml_data['master_password_validation_test'] = validation_test

      # Store in keychain
      unless MasterPasswordManager.store_master_password(new_master_password)
        Lich.log "error: Failed to store master password in keychain"
        return restore_backup_and_return_false(backup_file, yaml_file)
      end
    elsif current_mode == :enhanced
      # Remove validation test and keychain when leaving Enhanced
      yaml_data.delete('master_password_validation_test')
      MasterPasswordManager.delete_master_password
    end

    # Save YAML with headers
    write_yaml_file(yaml_file, yaml_data)

    # Clean up backup on success
    FileUtils.rm(backup_file) if File.exist?(backup_file)

    Lich.log "info: Encryption mode changed successfully: #{current_mode}#{new_mode}"
    true
  rescue StandardError => e
    Lich.log "error: Encryption mode change failed: #{e.class}: #{e.message}"
    restore_backup_and_return_false(backup_file, yaml_file)
  end
end

.convert_legacy_to_yaml_format(entry_data, validation_test = nil) ⇒ Hash

Converts legacy entry data to the YAML format.

Parameters:

  • entry_data (Array)

    The legacy entry data to convert.

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

    The validation test for the master password.

Returns:

  • (Hash)

    The converted YAML data.



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'documented/common/gui/yaml_state.rb', line 705

def self.convert_legacy_to_yaml_format(entry_data, validation_test = nil)
  yaml_data = { 'accounts' => {} }

  # Preserve encryption_mode if present in entries
  encryption_mode = entry_data.first&.[](:encryption_mode) || :plaintext
  yaml_data['encryption_mode'] = encryption_mode.to_s

  # Preserve master_password_validation_test if provided
  yaml_data['master_password_validation_test'] = validation_test

  entry_data.each do |entry|
    # Normalize account name to UPCASE for consistent storage
    normalized_username = (entry[:user_id])

    # Initialize account if not exists, with password at account level
    yaml_data['accounts'][normalized_username] ||= {
      'password'   => entry[:password],
      'characters' => []
    }

    character_data = {
      'char_name'         => normalize_character_name(entry[:char_name]),
      'game_code'         => entry[:game_code],
      'game_name'         => entry[:game_name],
      'frontend'          => entry[:frontend],
      'custom_launch'     => entry[:custom_launch],
      'custom_launch_dir' => entry[:custom_launch_dir],
      'is_favorite'       => entry[:is_favorite] || false
    }

    # Add favorite metadata if character is a favorite
    if entry[:is_favorite]
      character_data['favorite_order'] = entry[:favorite_order]
      character_data['favorite_added'] = entry[:favorite_added] || Time.now.to_s
    end

    # Check for duplicate character using precision matching (account/character/game_code/frontend)
    existing_character = yaml_data['accounts'][normalized_username]['characters'].find do |char|
      char['char_name'] == character_data['char_name'] &&
        char['game_code'] == character_data['game_code'] &&
        char['frontend'] == character_data['frontend']
    end

    # Only add if no exact match exists
    unless existing_character
      yaml_data['accounts'][normalized_username]['characters'] << character_data
    end
  end

  yaml_data
end

.convert_yaml_to_legacy_format(yaml_data) ⇒ Array

Converts YAML data to the legacy format for entries.

Parameters:

  • yaml_data (Hash)

    The YAML data to convert.

Returns:

  • (Array)

    The converted entries in legacy format.



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'documented/common/gui/yaml_state.rb', line 657

def self.convert_yaml_to_legacy_format(yaml_data)
  entries = []

  return entries unless yaml_data['accounts']

  encryption_mode = (yaml_data['encryption_mode'] || 'plaintext').to_sym

  yaml_data['accounts'].each do |username, |
    next unless ['characters']

    # Decrypt password if needed (with recovery for missing master password)
    password = if encryption_mode == :plaintext
                 ['password']
               else
                 decrypt_password_with_recovery(
                   ['password'],
                   mode: encryption_mode,
                   account_name: username,
                   validation_test: yaml_data['master_password_validation_test']
                 )
               end

    ['characters'].each do |character|
      entry = {
        user_id: username, # Already normalized to UPCASE in YAML
        password: password, # Decrypted password
        char_name: character['char_name'], # Already normalized to Title case in YAML
        game_code: character['game_code'],
        game_name: character['game_name'],
        frontend: character['frontend'],
        custom_launch: character['custom_launch'],
        custom_launch_dir: character['custom_launch_dir'],
        is_favorite: character['is_favorite'] || false,
        favorite_order: character['favorite_order'],
        encryption_mode: encryption_mode
      }

      entries << entry
    end
  end

  entries
end

.decrypt_password(encrypted_password, mode:, account_name: nil, master_password: nil) ⇒ String

Decrypts the given encrypted password using the specified mode and optional parameters.

Examples:

decrypted = Lich::Common::GUI::YamlState.decrypt_password(encrypted_password, mode: :enhanced)

Parameters:

  • encrypted_password (String)

    The encrypted password to decrypt.

  • mode (Symbol)

    The decryption mode to use.

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

    The account name associated with the password.

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

    The master password for decryption.

Returns:

  • (String)

    The decrypted password.

Raises:

  • (StandardError)

    If decryption fails.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'documented/common/gui/yaml_state.rb', line 217

def self.decrypt_password(encrypted_password, mode:, account_name: nil, master_password: nil)
  return encrypted_password if mode == :plaintext || mode.to_sym == :plaintext

  # For enhanced mode: auto-retrieve from Keychain if not provided
  if mode.to_sym == :enhanced && master_password.nil?
    master_password = MasterPasswordManager.retrieve_master_password
    raise StandardError, "Master password not found in Keychain - cannot decrypt" if master_password.nil?
  end

  PasswordCipher.decrypt(encrypted_password, mode: mode.to_sym, account_name: , master_password: master_password)
rescue StandardError => e
  Lich.log "error: decrypt_password failed - #{e.class}: #{e.message}"
  raise
end

.decrypt_password_with_recovery(encrypted_password, mode:, account_name: nil, master_password: nil, validation_test: nil) ⇒ String

Attempts to decrypt the given encrypted password, with recovery options for missing master password.

Examples:

decrypted = Lich::Common::GUI::YamlState.decrypt_password_with_recovery(encrypted_password, mode: :enhanced)

Parameters:

  • encrypted_password (String)

    The encrypted password to decrypt.

  • mode (Symbol)

    The decryption mode to use.

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

    The account name associated with the password.

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

    The master password for decryption.

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

    The validation test for recovery.

Returns:

  • (String)

    The decrypted password or nil if recovery fails.

Raises:

  • (StandardError)

    If decryption fails and recovery is not possible.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'documented/common/gui/yaml_state.rb', line 242

def self.decrypt_password_with_recovery(encrypted_password, mode:, account_name: nil, master_password: nil, validation_test: nil)
  # Try normal decryption first
  return decrypt_password(encrypted_password, mode: mode, account_name: , master_password: master_password)
rescue StandardError => e
  # Only attempt recovery for enhanced mode with missing master password
  if mode.to_sym == :enhanced && e.message.include?("Master password not found") && validation_test && !validation_test.empty?
    Lich.log "info: Master password missing from Keychain, attempting recovery via user prompt"

    # Show appropriate dialog based on context - use data access for conversion, recovery for actual recovery
    recovery_result = MasterPasswordPromptUI.show_password_for_data_access(validation_test)

    if recovery_result.nil? || recovery_result[:password].nil?
      Lich.log "info: User cancelled master password recovery"
      Gtk.main_quit
      return nil
    end

    recovered_password = recovery_result[:password]
    continue_session = recovery_result[:continue_session]

    # Password was validated by the UI layer, proceed with recovery
    Lich.log "info: Master password recovered and validated, storing to Keychain"

    # Save recovered password to Keychain for future use
    unless MasterPasswordManager.store_master_password(recovered_password)
      Lich.log "warning: Failed to store recovered master password to Keychain"
      # Continue anyway - decryption will still work with in-memory password
    end

    # Handle session continuation decision
    if !continue_session
      Lich.log "info: User chose to close application after password recovery"
      # Exit the application gracefully
      Gtk.main_quit
    end

    # Retry decryption with recovered password
    return decrypt_password(encrypted_password, mode: mode, account_name: , master_password: recovered_password)
  else
    # Re-raise if not recoverable
    raise
  end
end

.encrypt_all_passwords(yaml_data, mode, master_password: nil) ⇒ Hash

Encrypts all passwords in the provided YAML data according to the specified mode.

Examples:

updated_yaml = Lich::Common::GUI::YamlState.encrypt_all_passwords(yaml_data, :enhanced)

Parameters:

  • yaml_data (Hash)

    The YAML data containing accounts and passwords.

  • mode (Symbol)

    The encryption mode to use.

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

    The master password for encryption.

Returns:

  • (Hash)

    The YAML data with encrypted passwords.



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'documented/common/gui/yaml_state.rb', line 293

def self.encrypt_all_passwords(yaml_data, mode, master_password: nil)
  return yaml_data if mode == :plaintext

  yaml_data['accounts'].each do |, |
    next unless ['password']

    # Encrypt password based on mode
    ['password'] = encrypt_password(
      ['password'],
      mode: mode,
      account_name: ,
      master_password: master_password
    )
  end

  yaml_data
end

.encrypt_password(password, mode:, account_name: nil, master_password: nil) ⇒ String

Encrypts the given password using the specified mode and optional parameters.

Examples:

encrypted = Lich::Common::GUI::YamlState.encrypt_password("my_password", mode: :enhanced)

Parameters:

  • password (String)

    The password to encrypt.

  • mode (Symbol)

    The encryption mode to use.

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

    The account name associated with the password.

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

    The master password for encryption.

Returns:

  • (String)

    The encrypted password.

Raises:

  • (StandardError)

    If encryption fails.



199
200
201
202
203
204
205
206
# File 'documented/common/gui/yaml_state.rb', line 199

def self.encrypt_password(password, mode:, account_name: nil, master_password: nil)
  return password if mode == :plaintext || mode.to_sym == :plaintext

  PasswordCipher.encrypt(password, mode: mode.to_sym, account_name: , master_password: master_password)
rescue StandardError => e
  Lich.log "error: encrypt_password failed - #{e.class}: #{e.message}"
  raise
end

.ensure_master_password_existsHash?

Ensures that a master password exists, prompting the user to create one if necessary.

Returns:

  • (Hash, nil)

    The master password and validation test if created, nil otherwise.



970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'documented/common/gui/yaml_state.rb', line 970

def self.ensure_master_password_exists
  # Check if master password already in Keychain
  existing = MasterPasswordManager.retrieve_master_password
  return existing if !existing.nil? && !existing.empty?

  # Show UI prompt to CREATE master password
  master_password = MasterPasswordPrompt.show_create_master_password_dialog

  if master_password.nil?
    Lich.log "info: User declined to create master password"
    return nil
  end

  # Create validation test (expensive 100k iterations, one-time)
  validation_test = MasterPasswordManager.create_validation_test(master_password)

  if validation_test.nil?
    Lich.log "error: Failed to create validation test"
    return nil
  end

  # Store in Keychain
  stored = MasterPasswordManager.store_master_password(master_password)

  unless stored
    Lich.log "error: Failed to store master password in Keychain"
    return nil
  end

  Lich.log "info: Master password created and stored in Keychain"
  # Return both password and validation test for YAML storage
  { password: master_password, validation_test: validation_test }
end

.find_character(yaml_data, username, char_name, game_code, frontend = nil) ⇒ Hash?

Finds a character in the YAML data based on the provided criteria.

Parameters:

  • yaml_data (Hash)

    The YAML data containing accounts and characters.

  • username (String)

    The username associated with the character.

  • char_name (String)

    The name of the character to find.

  • game_code (String)

    The game code associated with the character.

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

    The frontend associated with the character.

Returns:

  • (Hash, nil)

    The found character data or nil if not found.



808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
# File 'documented/common/gui/yaml_state.rb', line 808

def self.find_character(yaml_data, username, char_name, game_code, frontend = nil)
  return nil unless yaml_data['accounts'] && yaml_data['accounts'][username]
   = yaml_data['accounts'][username]
  return nil unless ['characters']

  # If frontend is specified, find exact match first
  if frontend
    exact_match = ['characters'].find do |character|
      character['char_name'] == char_name &&
        character['game_code'] == game_code &&
        character['frontend'] == frontend
    end
    return exact_match if exact_match
  end

  # Fallback to basic matching only if no exact match found and frontend is nil
  if frontend.nil?
    ['characters'].find do |character|
      character['char_name'] == char_name && character['game_code'] == game_code
    end
  else
    # If frontend was specified but no exact match found, return nil
    nil
  end
end

.find_entry_in_legacy_format(entry_data, username, char_name, game_code, frontend = nil) ⇒ Hash?

Finds an entry in the legacy format based on the provided criteria.

Parameters:

  • entry_data (Array)

    The legacy entry data to search.

  • username (String)

    The username associated with the entry.

  • char_name (String)

    The name of the character to find.

  • game_code (String)

    The game code associated with the entry.

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

    The frontend associated with the entry.

Returns:

  • (Hash, nil)

    The found entry data or nil if not found.



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'documented/common/gui/yaml_state.rb', line 860

def self.find_entry_in_legacy_format(entry_data, username, char_name, game_code, frontend = nil)
  entry_data.find do |entry|
    # Match on username first
    next unless entry[:user_id] == username

    # Apply same matching logic as find_character
    matches_basic = entry[:char_name] == char_name && entry[:game_code] == game_code

    if frontend.nil?
      # Backward compatibility: if no frontend specified, match any frontend
      matches_basic
    else
      # Frontend precision: must match exact frontend
      matches_basic && entry[:frontend] == frontend
    end
  end
end

.generate_yaml_content(yaml_data) ⇒ String

Generates the YAML content from the provided data, including headers.

Parameters:

  • yaml_data (Hash)

    The YAML data to generate content from.

Returns:

  • (String)

    The generated YAML content.



945
946
947
948
949
950
951
952
953
# File 'documented/common/gui/yaml_state.rb', line 945

def self.generate_yaml_content(yaml_data)
  # Prepare YAML with password preservation (clones to avoid mutation)
  prepared_yaml = prepare_yaml_for_serialization(yaml_data)

  content = "# Lich 5 Login Entries - YAML Format\n" \
          + "# Generated: #{Time.now}\n" \
          + YAML.dump(prepared_yaml, permitted_classes: [Symbol])
  return content
end

.get_existing_master_password_for_migrationHash?

Retrieves the existing master password for migration purposes, creating a validation test.

Returns:

  • (Hash, nil)

    The existing master password and validation test if found, nil otherwise.



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'documented/common/gui/yaml_state.rb', line 1006

def self.get_existing_master_password_for_migration
  # Retrieve existing master password from keychain
  existing_password = MasterPasswordManager.retrieve_master_password

  if existing_password.nil? || existing_password.empty?
    Lich.log "info: No existing master password found in keychain - user should create one"
    return nil
  end

  Lich.log "info: Found existing master password in keychain - creating validation test for migration"

  # Create a NEW validation test with the existing password
  # This is needed because we don't have the old validation test in YAML yet
  validation_test = MasterPasswordManager.create_validation_test(existing_password)

  if validation_test.nil?
    Lich.log "error: Failed to create validation test for existing master password"
    return nil
  end

  Lich.log "info: Validation test created for existing master password"
  { password: existing_password, validation_test: validation_test }
end

.get_favorites(data_dir) ⇒ Array

Retrieves a list of favorite characters from the YAML data.

Examples:

favorites = Lich::Common::GUI::YamlState.get_favorites(data_dir)

Parameters:

  • data_dir (String)

    The directory where the YAML file is located.

Returns:

  • (Array)

    An array of favorite characters.

Raises:

  • (StandardError)

    If there is an error retrieving favorites.



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
607
608
609
610
611
# File 'documented/common/gui/yaml_state.rb', line 577

def self.get_favorites(data_dir)
  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)
  return [] unless File.exist?(yaml_file)

  begin
    yaml_data = YAML.load_file(yaml_file)
    yaml_data = migrate_to_favorites_format(yaml_data)

    favorites = []

    yaml_data['accounts'].each do |username, |
      next unless ['characters']

      ['characters'].each do |character|
        if character['is_favorite']
          favorites << {
            user_id: username,
            char_name: character['char_name'],
            game_code: character['game_code'],
            game_name: character['game_name'],
            frontend: character['frontend'],
            favorite_order: character['favorite_order'] || 999,
            favorite_added: character['favorite_added']
          }
        end
      end
    end

    # Sort by favorite order
    favorites.sort_by { |fav| fav[:favorite_order] }
  rescue StandardError => e
    Lich.log "error: Error getting favorites: #{e.message}"
    []
  end
end

.get_next_favorite_order(yaml_data) ⇒ Integer

Retrieves the next available favorite order number based on existing favorites.

Parameters:

  • yaml_data (Hash)

    The YAML data containing accounts and characters.

Returns:

  • (Integer)

    The next favorite order number.



837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'documented/common/gui/yaml_state.rb', line 837

def self.get_next_favorite_order(yaml_data)
  max_order = 0

  yaml_data['accounts'].each do |_username, |
    next unless ['characters']

    ['characters'].each do |character|
      if character['is_favorite'] && character['favorite_order']
        max_order = [max_order, character['favorite_order']].max
      end
    end
  end

  max_order + 1
end

.is_favorite?(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean

Checks if a character is marked as a favorite in the YAML data.

Examples:

favorite = Lich::Common::GUI::YamlState.is_favorite?(data_dir, username, char_name, game_code)

Parameters:

  • data_dir (String)

    The directory where the YAML file is located.

  • username (String)

    The username associated with the character.

  • char_name (String)

    The name of the character to check.

  • game_code (String)

    The game code associated with the character.

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

    The frontend associated with the character.

Returns:

  • (Boolean)

    True if the character is a favorite, false otherwise.

Raises:

  • (StandardError)

    If there is an error checking favorite status.



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'documented/common/gui/yaml_state.rb', line 555

def self.is_favorite?(data_dir, username, char_name, game_code, frontend = nil)
  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)
  return false unless File.exist?(yaml_file)

  begin
    yaml_data = YAML.load_file(yaml_file)
    yaml_data = migrate_to_favorites_format(yaml_data)

    character = find_character(yaml_data, username, char_name, game_code, frontend)
    character && character['is_favorite'] == true
  rescue StandardError => e
    Lich.log "error: Error checking favorite status: #{e.message}"
    false
  end
end

.load_saved_entries(data_dir, autosort_state) ⇒ Array

Loads saved entries from a YAML file, falling back to legacy format if necessary.

Examples:

entries = Lich::Common::GUI::YamlState.load_saved_entries(data_dir, true)

Parameters:

  • data_dir (String)

    The directory where the entry files are located.

  • autosort_state (Boolean)

    Indicates whether to sort entries by favorites.

Returns:

  • (Array)

    The loaded entries.

Raises:

  • (StandardError)

    If there is an error loading the YAML file.



31
32
33
34
35
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
# File 'documented/common/gui/yaml_state.rb', line 31

def self.load_saved_entries(data_dir, autosort_state)
  # Guard against nil data_dir
  return [] if data_dir.nil?

  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)
  dat_file = File.join(data_dir, "entry.dat")

  if File.exist?(yaml_file)
    # Load from YAML format
    begin
      yaml_data = YAML.load_file(yaml_file)

      # Migrate data structure if needed to support favorites and encryption
      yaml_data = migrate_to_favorites_format(yaml_data)
      yaml_data = migrate_to_encryption_format(yaml_data)

      entries = convert_yaml_to_legacy_format(yaml_data)

      # Apply sorting with favorites priority if enabled
      sort_entries_with_favorites(entries, autosort_state)

      entries
    rescue StandardError => e
      Lich.log "error: Error loading YAML entry file: #{e.message}"
      []
    end
  elsif File.exist?(dat_file)
    # Fall back to legacy format if YAML doesn't exist
    Lich.log "info: YAML entry file not found, falling back to legacy format"
    State.load_saved_entries(data_dir, autosort_state)
  else
    # No entry file exists
    []
  end
end

.migrate_from_legacy(data_dir, encryption_mode: :plaintext) ⇒ Boolean

Migrates entries from the legacy DAT format to the YAML format.

Examples:

success = Lich::Common::GUI::YamlState.migrate_from_legacy(data_dir, encryption_mode: :enhanced)

Parameters:

  • data_dir (String)

    The directory where the legacy entry files are located.

  • encryption_mode (Symbol) (defaults to: :plaintext)

    The encryption mode to use for the migration.

Returns:

  • (Boolean)

    True if the migration was successful, false otherwise.

Raises:

  • (StandardError)

    If there is an error during migration.



114
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
# File 'documented/common/gui/yaml_state.rb', line 114

def self.migrate_from_legacy(data_dir, encryption_mode: :plaintext)
  dat_file = File.join(data_dir, "entry.dat")
  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)

  # Skip if YAML file already exists or DAT file doesn't exist
  return false unless File.exist?(dat_file)
  return false if File.exist?(yaml_file)

  # ====================================================================
  # Handle master_password mode - check for existing or create new
  # ====================================================================
  master_password = nil
  validation_test = nil
  if encryption_mode == :enhanced
    # First check if master password already exists in keychain
    result = get_existing_master_password_for_migration

    # If no existing password, prompt user to create one
    if result.nil?
      result = ensure_master_password_exists
    end

    if result.nil?
      Lich.log "error: Master password not available for migration"
      return false
    end

    # Handle both new (Hash) and existing (String) password returns
    if result.is_a?(Hash)
      master_password = result[:password]
      validation_test = result[:validation_test]
    else
      master_password = result
    end
  end

  # Load legacy data
  legacy_entries = State.load_saved_entries(data_dir, false)

  # Add encryption_mode to entries
  legacy_entries.each do |entry|
    entry[:encryption_mode] = encryption_mode
  end

  # Encrypt passwords if not plaintext mode
  if encryption_mode != :plaintext
    legacy_entries.each do |entry|
      entry[:password] = encrypt_password(
        entry[:password],
        mode: encryption_mode,
        account_name: entry[:user_id],
        master_password: master_password # NEW: Pass master password
      )
    end
  end

  # Use save_entries to maintain test compatibility
  save_entries(data_dir, legacy_entries)

  # Save validation test to YAML if it was created
  if validation_test && encryption_mode == :enhanced
    yaml_file = yaml_file_path(data_dir)
    if File.exist?(yaml_file)
      yaml_data = YAML.load_file(yaml_file)
      yaml_data['master_password_validation_test'] = validation_test
      write_yaml_file(yaml_file, yaml_data)
    end
  end

  # Log conversion summary
   = legacy_entries.map { |entry| entry[:user_id] }.uniq.sort.join(', ')
  Lich.log "info: Migration complete - Encryption mode: #{encryption_mode.upcase}, Converted accounts: #{}"

  true
end

.migrate_to_encryption_format(yaml_data) ⇒ Hash

Migrates the provided YAML data to include encryption format fields.

Parameters:

  • yaml_data (Hash)

    The YAML data to migrate.

Returns:

  • (Hash)

    The migrated YAML data.



448
449
450
451
452
453
454
455
456
457
# File 'documented/common/gui/yaml_state.rb', line 448

def self.migrate_to_encryption_format(yaml_data)
  return yaml_data unless yaml_data.is_a?(Hash)

  # Add encryption_mode if not present (defaults to plaintext for backward compatibility)
  yaml_data['encryption_mode'] ||= 'plaintext'
  # Add validation test field if master_password mode (for Phase 2)
  yaml_data['master_password_validation_test'] ||= nil

  yaml_data
end

.migrate_to_favorites_format(yaml_data) ⇒ Hash

Migrates the provided YAML data to include favorite fields.

Parameters:

  • yaml_data (Hash)

    The YAML data to migrate.

Returns:

  • (Hash)

    The migrated YAML data.



785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'documented/common/gui/yaml_state.rb', line 785

def self.migrate_to_favorites_format(yaml_data)
  return yaml_data unless yaml_data.is_a?(Hash) && yaml_data['accounts']

  yaml_data['accounts'].each do |_username, |
    next unless ['characters'].is_a?(Array)

    ['characters'].each do |character|
      # Add favorites fields if not present
      character['is_favorite'] ||= false
      # Don't add favorite_order or favorite_added unless character is actually a favorite
    end
  end

  yaml_data
end

.normalize_account_name(name) ⇒ String

Normalizes the account name by stripping whitespace and converting to uppercase.

Parameters:

  • name (String, nil)

    The account name to normalize.

Returns:

  • (String)

    The normalized account name.



929
930
931
932
# File 'documented/common/gui/yaml_state.rb', line 929

def self.(name)
  return '' if name.nil?
  name.to_s.strip.upcase
end

.normalize_character_name(name) ⇒ String

Normalizes the character name by stripping whitespace and capitalizing.

Parameters:

  • name (String, nil)

    The character name to normalize.

Returns:

  • (String)

    The normalized character name.



937
938
939
940
# File 'documented/common/gui/yaml_state.rb', line 937

def self.normalize_character_name(name)
  return '' if name.nil?
  name.to_s.strip.capitalize
end

.prepare_yaml_for_serialization(yaml_data) ⇒ Hash

Prepares YAML data for serialization, ensuring proper formatting and structure.

Parameters:

  • yaml_data (Hash)

    The YAML data to prepare.

Returns:

  • (Hash)

    The prepared YAML data.



904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
# File 'documented/common/gui/yaml_state.rb', line 904

def self.prepare_yaml_for_serialization(yaml_data)
  # Clone to avoid mutating caller's object
  prepared_data = Marshal.load(Marshal.dump(yaml_data))

  # Ensure top-level fields are explicitly present (defensive programming)
  prepared_data['encryption_mode'] ||= 'plaintext'
  prepared_data['master_password_validation_test'] ||= nil

  # Preserve encrypted passwords by ensuring they are serialized as quoted strings
  # This prevents YAML from using multiline formatting (|, >) which breaks Base64 decoding
  if prepared_data['accounts']
    prepared_data['accounts'].each do |_username, |
      if .is_a?(Hash) && ['password']
        # Force password to be treated as a plain scalar string
        ['password'] = ['password'].to_s
      end
    end
  end

  prepared_data
end

.remove_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean

Removes a character from the favorites list in the YAML data.

Examples:

success = Lich::Common::GUI::YamlState.remove_favorite(data_dir, username, char_name, game_code)

Parameters:

  • data_dir (String)

    The directory where the YAML file is located.

  • username (String)

    The username associated with the character.

  • char_name (String)

    The name of the character to remove from favorites.

  • game_code (String)

    The game code associated with the character.

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

    The frontend associated with the character.

Returns:

  • (Boolean)

    True if the character was removed from favorites, false otherwise.

Raises:

  • (StandardError)

    If there is an error removing the favorite.



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
# File 'documented/common/gui/yaml_state.rb', line 511

def self.remove_favorite(data_dir, username, char_name, game_code, frontend = nil)
  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)
  return false unless File.exist?(yaml_file)

  begin
    yaml_data = YAML.load_file(yaml_file)
    yaml_data = migrate_to_favorites_format(yaml_data)

    # Find the character with frontend precision
    character = find_character(yaml_data, username, char_name, game_code, frontend)
    return false unless character

    # Don't remove if not a favorite
    return true unless character['is_favorite']

    # Remove favorite status
    character['is_favorite'] = false
    character.delete('favorite_order')
    character.delete('favorite_added')

    # Reorder remaining favorites
    reorder_all_favorites(yaml_data)

    # Save updated data
    content = generate_yaml_content(yaml_data)
    result = Utilities.safe_file_operation(yaml_file, :write, content)

    result ? true : false
  rescue StandardError => e
    Lich.log "error: Error removing favorite: #{e.message}"
    false
  end
end

.reorder_all_favorites(yaml_data) ⇒ Object

Reorders all favorite characters in the YAML data based on their current order.

Parameters:

  • yaml_data (Hash)

    The YAML data containing accounts and characters.



880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# File 'documented/common/gui/yaml_state.rb', line 880

def self.reorder_all_favorites(yaml_data)
  # Collect all favorites
  all_favorites = []

  yaml_data['accounts'].each do |_username, |
    next unless ['characters']

    ['characters'].each do |character|
      if character['is_favorite']
        all_favorites << character
      end
    end
  end

  # Sort by current order and reassign consecutive numbers
  all_favorites.sort_by! { |char| char['favorite_order'] || 999 }
  all_favorites.each_with_index do |character, index|
    character['favorite_order'] = index + 1
  end
end

.reorder_favorites(data_dir, ordered_favorites) ⇒ Boolean

Reorders the favorites list in the YAML data based on the provided order.

Examples:

success = Lich::Common::GUI::YamlState.reorder_favorites(data_dir, ordered_favorites)

Parameters:

  • data_dir (String)

    The directory where the YAML file is located.

  • ordered_favorites (Array)

    An array of favorite characters in the desired order.

Returns:

  • (Boolean)

    True if the reordering was successful, false otherwise.

Raises:

  • (StandardError)

    If there is an error reordering favorites.



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
647
648
649
650
651
652
# File 'documented/common/gui/yaml_state.rb', line 620

def self.reorder_favorites(data_dir, ordered_favorites)
  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)
  return false unless File.exist?(yaml_file)

  begin
    yaml_data = YAML.load_file(yaml_file)
    yaml_data = migrate_to_favorites_format(yaml_data)

    # Update favorite order for each character in the provided order
    ordered_favorites.each_with_index do |favorite_info, index|
      character = find_character(
        yaml_data,
        favorite_info[:username] || favorite_info['username'],
        favorite_info[:char_name] || favorite_info['char_name'],
        favorite_info[:game_code] || favorite_info['game_code'],
        favorite_info[:frontend] || favorite_info['frontend']
      )

      if character && character['is_favorite']
        character['favorite_order'] = index + 1
      end
    end

    # Save updated data
    content = generate_yaml_content(yaml_data)
    result = Utilities.safe_file_operation(yaml_file, :write, content)

    result ? true : false
  rescue StandardError => e
    Lich.log "error: Error reordering favorites: #{e.message}"
    false
  end
end

.restore_backup_and_return_false(backup_file, yaml_file) ⇒ Boolean

Restores the backup file if it exists and returns false.

Parameters:

  • backup_file (String)

    The path to the backup file.

  • yaml_file (String)

    The path to the original YAML file.

Returns:

  • (Boolean)

    Always returns false after restoring.



436
437
438
439
440
441
442
443
# File 'documented/common/gui/yaml_state.rb', line 436

def self.restore_backup_and_return_false(backup_file, yaml_file)
  if File.exist?(backup_file)
    FileUtils.cp(backup_file, yaml_file)
    FileUtils.rm(backup_file)
    Lich.log "info: Backup restored after encryption mode change failure"
  end
  false
end

.save_entries(data_dir, entry_data) ⇒ Boolean

Saves the provided entry data to a YAML file, preserving validation tests if they exist.

Examples:

success = Lich::Common::GUI::YamlState.save_entries(data_dir, entry_data)

Parameters:

  • data_dir (String)

    The directory where the YAML file will be saved.

  • entry_data (Array)

    The entry data to save.

Returns:

  • (Boolean)

    True if the save was successful, false otherwise.

Raises:

  • (StandardError)

    If there is an error saving the YAML file.



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
# File 'documented/common/gui/yaml_state.rb', line 74

def self.save_entries(data_dir, entry_data)
  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)

  # Preserve validation test from existing YAML if it exists
  original_validation_test = nil
  if File.exist?(yaml_file)
    begin
      original_data = YAML.load_file(yaml_file)
      original_validation_test = original_data['master_password_validation_test'] if original_data.is_a?(Hash)
    rescue StandardError => e
      Lich.log "warning: Could not load existing YAML to preserve validation test: #{e.message}"
    end
  end

  # Convert legacy format to YAML structure, passing validation test to preserve it
  yaml_data = convert_legacy_to_yaml_format(entry_data, original_validation_test)

  # Create backup of existing file if it exists
  if File.exist?(yaml_file)
    backup_file = "#{yaml_file}.bak"
    FileUtils.cp(yaml_file, backup_file)
  end

  # Write YAML data to file with secure permissions
  begin
    write_yaml_file(yaml_file, yaml_data)
    true
  rescue StandardError => e
    Lich.log "error: Error saving YAML entry file: #{e.message}"
    false
  end
end

.sort_entries_with_favorites(entries, autosort_state) ⇒ Array

Sorts entries with favorites prioritized based on the autosort state.

Parameters:

  • entries (Array)

    The entries to sort.

  • autosort_state (Boolean)

    Indicates whether to sort by favorites.

Returns:

  • (Array)

    The sorted entries.



761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'documented/common/gui/yaml_state.rb', line 761

def self.sort_entries_with_favorites(entries, autosort_state)
  # If autosort is disabled, preserve original order without any reordering
  return entries unless autosort_state

  # Autosort enabled: apply favorites-first sorting
  # Separate favorites and non-favorites
  favorites = entries.select { |entry| entry[:is_favorite] }
  non_favorites = entries.reject { |entry| entry[:is_favorite] }

  # Sort favorites by favorite_order
  favorites.sort_by! { |entry| entry[:favorite_order] || 999 }

  # Sort non-favorites by account name (upcase), game name, and character name
  sorted_non_favorites = non_favorites.sort do |a, b|
    [a[:user_id].upcase, a[:game_name], a[:char_name]] <=> [b[:user_id].upcase, b[:game_name], b[:char_name]]
  end

  # Return favorites first, then non-favorites
  favorites + sorted_non_favorites
end

.write_yaml_file(yaml_file, yaml_data) ⇒ Object

Writes the provided YAML data to a file with secure permissions.

Parameters:

  • yaml_file (String)

    The path to the YAML file.

  • yaml_data (Hash)

    The YAML data to write.



958
959
960
961
962
963
964
965
966
# File 'documented/common/gui/yaml_state.rb', line 958

def self.write_yaml_file(yaml_file, yaml_data)
  prepared_yaml = prepare_yaml_for_serialization(yaml_data)

  File.open(yaml_file, 'w', 0o600) do |file|
    file.puts "# Lich 5 Login Entries - YAML Format"
    file.puts "# Generated: #{Time.now}"
    file.write(YAML.dump(prepared_yaml, permitted_classes: [Symbol]))
  end
end

.yaml_file_path(data_dir) ⇒ String

Returns the path to the YAML file in the specified data directory.

Parameters:

  • data_dir (String)

    The directory where the YAML file is located.

Returns:

  • (String)

    The full path to the YAML file.



20
21
22
# File 'documented/common/gui/yaml_state.rb', line 20

def self.yaml_file_path(data_dir)
  File.join(data_dir, "entry.yaml")
end