Skip to content

🧹 Refactor main function complexity in cia_3ds_decryptor.py#226

Open
Ven0m0 wants to merge 1 commit intomainfrom
refactor-decryptor-main-complexity-1313614530380434342
Open

🧹 Refactor main function complexity in cia_3ds_decryptor.py#226
Ven0m0 wants to merge 1 commit intomainfrom
refactor-decryptor-main-complexity-1313614530380434342

Conversation

@Ven0m0
Copy link
Owner

@Ven0m0 Ven0m0 commented Mar 16, 2026

This PR addresses the high complexity of the main function in Cachyos/Scripts/WIP/emu/cia_3ds_decryptor.py.

🎯 What: The main function was a long, monolithic block of code handling multiple responsibilities. I refactored it into 7 focused helper functions.
💡 Why: Smaller functions are easier to understand, test, and maintain. It also makes the high-level logic of the script clear at a glance in the main orchestrator.
Verification:

  • Verified syntax with python3 -m py_compile.
  • Ran functional tests: python3 Cachyos/Scripts/WIP/emu/test_decryptor_counters.py.
  • Checked code quality with ruff check.
  • Ensured console output remains consistent with the original version.
    Result: Improved code health and reduced cognitive load for future maintainers.

PR created automatically by Jules for task 1313614530380434342 started by @Ven0m0

Extracted logic from the main function into several smaller, modular helper functions:
- initialize_tools
- sanitize_filenames
- get_file_counts
- ask_for_conversion
- run_decryption
- run_conversion
- display_summary

This improves readability and maintainability while preserving the original functionality and sequence of operations. Verified with existing tests and ruff.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 16, 2026 22:19
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@aviator-app
Copy link

aviator-app bot commented Mar 16, 2026

Current Aviator status

Aviator will automatically update this comment as the status of the PR changes.
Comment /aviator refresh to force Aviator to re-examine your PR (or learn about other /aviator commands).

This pull request is currently open (not queued).

How to merge

To merge this PR, comment /aviator merge or add the mergequeue label.


See the real-time status of this PR on the Aviator webapp.
Use the Aviator Chrome Extension to see the status of your PR within GitHub.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the code quality of the cia_3ds_decryptor.py script by modularizing its core logic. The main function, which previously handled multiple responsibilities, has been broken down into several dedicated helper functions. This refactoring enhances the script's maintainability and readability, making the high-level flow clearer and reducing cognitive load for future development.

Highlights

  • Main Function Refactoring: The previously monolithic main function in cia_3ds_decryptor.py was refactored into 7 smaller, focused helper functions. This significantly improves code readability, maintainability, and testability by breaking down complex logic into manageable units.
Changelog
  • Cachyos/Scripts/WIP/emu/cia_3ds_decryptor.py
    • Refactored the main function by extracting its responsibilities into new helper functions.
    • Introduced initialize_tools to handle initial setup, including directory creation, logging, and tool path resolution.
    • Created sanitize_filenames to encapsulate the logic for sanitizing file names in the root directory.
    • Added get_file_counts to count CIA and 3DS files, returning a Counters object.
    • Implemented ask_for_conversion to prompt the user for CCI conversion, separating user interaction logic.
    • Developed run_decryption to manage the parallel decryption of 3DS and CIA files.
    • Created run_conversion to handle the parallel conversion of decrypted CIA files to CCI.
    • Introduced display_summary to present the final statistics and results of the decryption and conversion process.
Activity
  • Verified syntax using python3 -m py_compile.
  • Ran functional tests with python3 Cachyos/Scripts/WIP/emu/test_decryptor_counters.py.
  • Checked code quality using ruff check.
  • Ensured console output remains consistent with the original version of the script.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link
Contributor

Lint/Format Check Failed

Please run ./lint-format.sh locally to fix formatting issues.

@kilo-code-bot
Copy link

kilo-code-bot bot commented Mar 16, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 file)
  • Cachyos/Scripts/WIP/emu/cia_3ds_decryptor.py - Refactoring only, no issues

This PR is a clean refactoring that extracts the monolithic main() function into 7 smaller, focused helper functions. The logic flow is preserved correctly:

  • initialize_tools() - Sets up tools, logging, and cleans up NCCH files
  • sanitize_filenames() - Sanitizes filenames in the root directory
  • get_file_counts() - Counts CIA and 3DS files
  • ask_for_conversion() - Prompts user for CCI conversion (returns boolean)
  • run_decryption() - Runs parallel decryption
  • run_conversion() - Runs parallel CCI conversion
  • display_summary() - Shows final statistics

The boolean return from ask_for_conversion() is correctly assigned to cnt.convert_to_cci in main(). No bugs, security issues, or logic errors detected.


Reviewed by minimax-m2.5 · 126,920 tokens

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively refactors the main function in cia_3ds_decryptor.py, significantly improving its readability and maintainability by breaking down a large monolithic function into smaller, focused helper functions. The new structure makes the script's execution flow much clearer. I have one suggestion to further improve the error logging in one of the new helper functions.

Comment on lines +585 to +616
banner()
print(" Decrypting...\n")
futures = {}
with concurrent.futures.ThreadPoolExecutor() as executor:
if cnt.count_3ds:
logging.info(
"[i] Found %d 3DS file(s). Start decrypting...", cnt.count_3ds
)
logging.info("[i] Found %d 3DS file(s). Start decrypting...", cnt.count_3ds)
for f in sorted(root.glob("*.3ds")):
future = executor.submit(
process_file_task, decrypt_3ds, root, f, tools_list, seeddb
)
futures[future] = "3ds"

if cnt.count_cia:
logging.info(
"[i] Found %d CIA file(s). Start decrypting...", cnt.count_cia
)
logging.info("[i] Found %d CIA file(s). Start decrypting...", cnt.count_cia)
for f in sorted(root.glob("*.cia")):
future = executor.submit(
process_file_task, decrypt_cia, root, f, tools_list, seeddb
)
futures[future] = "cia"

# Wait for completion and accumulate results
for future in concurrent.futures.as_completed(futures):
task_type = futures.get(future)
try:
result_cnt = future.result()
cnt += result_cnt
except Exception as e:
logging.error("Task failed with exception: %s", e)
# Update error counts so failed tasks are reflected in statistics
if task_type == "3ds":
cnt.ds_err += 1
elif task_type == "cia":
cnt.cia_err += 1
# If task_type is None or unrecognized, we intentionally do not guess
if cnt.convert_to_cci:
logging.info("[i] Starting parallel CCI conversion...")
conv_futures = {}
with concurrent.futures.ThreadPoolExecutor() as executor:
for f in sorted(root.glob("*-decrypted.cia")):
future = executor.submit(
process_conversion_task, root, f, tools_list, seeddb
return cnt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error handling in run_decryption could be improved for better diagnostics. When a task fails with an exception, the current implementation logs the exception but doesn't specify which file failed. This is because the futures dictionary only stores the task type ('3ds' or 'cia').

To provide more context, you can store both the task type and the filename in the futures dictionary, similar to how run_conversion stores the filename. This will allow you to log the specific file that caused the error, making debugging much easier.

  banner()
  print("  Decrypting...\n")
  futures = {}
  with concurrent.futures.ThreadPoolExecutor() as executor:
    if cnt.count_3ds:
      logging.info("[i] Found %d 3DS file(s). Start decrypting...", cnt.count_3ds)
      for f in sorted(root.glob("*.3ds")):
        future = executor.submit(
          process_file_task, decrypt_3ds, root, f, tools_list, seeddb
        )
        futures[future] = ("3ds", f.name)

    if cnt.count_cia:
      logging.info("[i] Found %d CIA file(s). Start decrypting...", cnt.count_cia)
      for f in sorted(root.glob("*.cia")):
        future = executor.submit(
          process_file_task, decrypt_cia, root, f, tools_list, seeddb
        )
        futures[future] = ("cia", f.name)

    for future in concurrent.futures.as_completed(futures):
      task_type, filename = futures.get(future, (None, "unknown file"))
      try:
        result_cnt = future.result()
        cnt += result_cnt
      except Exception as e:
        logging.error("Task for file '%s' failed with exception: %s", filename, e)
        if task_type == "3ds":
          cnt.ds_err += 1
        elif task_type == "cia":
          cnt.cia_err += 1
  return cnt

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the monolithic main() function in cia_3ds_decryptor.py into 7 focused helper functions: initialize_tools, sanitize_filenames, get_file_counts, ask_for_conversion, run_decryption, run_conversion, and display_summary. The resulting main() is a clear, linear orchestrator.

Changes:

  • Extracted 7 helper functions from the original main(), each with a single responsibility and proper type annotations/docstrings.
  • Moved the "no files found" early-return logic into the new main() orchestrator (previously inline).
  • Minor formatting cleanup: collapsed multi-line logging.info calls into single lines and removed inline comments.

You can also share your feedback on Copilot code review. Take the survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants