Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game Importer Fixes for cloud and local files #2354

Merged
merged 2 commits into from
Oct 19, 2024

Conversation

proskd
Copy link
Contributor

@proskd proskd commented Oct 18, 2024

User description

What does this PR do

Generally, this PR should ensure that upon first successful launch, the app will be in a state to properly accept imports from cloud and local files sources.

  1. Fixes setup of database and caches at first launch
  2. Fixes case where the file being accessed isn't a secure file (and doesn't need to be accessed as such)

Where should the reviewer start

How should this be manually tested

  1. Clean launch of the app
  2. import any valid ROM
  3. Observe

After imports are processed, the library should be updated properly with the ROMs selected.

Cases I tested:

  1. Single ROM
  2. Multiple ROM selection
  3. BIOS selection
  4. CD ROM zip file

Any background context you want to provide

What are the relevant tickets

Screenshots (important for UI changes)

Questions


PR Type

enhancement, bug_fix


Description

  • Enhanced RomDatabase with async cache management methods and synchronous access options.
  • Improved directory setup in GameImporter to ensure necessary directories are created.
  • Fixed potential issues with security-scoped resource access in PVRootViewController.
  • Updated various components to use synchronous cache access where appropriate for performance and reliability.

Changes walkthrough 📝

Relevant files
Enhancement
PVEmulatorConfiguration+Frameworks.swift
Update database refresh and BIOS addition logic                   

PVLibrary/Sources/PVLibrary/Configuration/PVEmulatorConfiguration+Frameworks.swift

  • Changed RomDatabase method call from refresh to reloadCache.
  • Updated database.add to avoid conflicts with duplicate BIOS names.
  • +4/-2     
    PVEmulatorConfiguration.swift
    Use synchronous system cache access                                           

    PVLibrary/Sources/PVLibrary/Configuration/PVEmulatorConfiguration.swift

  • Replaced getSystemCache with getSystemCacheSync for synchronous
    access.
  • +1/-1     
    RomDatabase+Caches.swift
    Implement async cache management in RomDatabase                   

    PVLibrary/Sources/PVLibrary/Database/Realm Database/RomDatabase+Caches.swift

  • Introduced async versions of cache reload methods.
  • Added synchronous cache retrieval methods.
  • Updated cache reload logic to use async tasks.
  • +50/-27 
    PVGame+Paths.swift
    Use synchronous system cache access in PVGame                       

    PVLibrary/Sources/PVLibrary/Game Media/PVGame+Paths.swift

    • Updated method to use synchronous system cache access.
    +1/-1     
    GameImporter.swift
    Refactor GameImporter directory setup and cache access     

    PVLibrary/Sources/PVLibrary/Importer/Services/GameImporter/GameImporter.swift

  • Added biosPath property for BIOS directory management.
  • Refactored directory creation logic into a separate method.
  • Updated cache access to use synchronous methods where necessary.
  • +31/-24 
    PVGameLibraryViewController.swift
    Use synchronous BIOS cache access in Game Library               

    PVUI/Sources/PVUIBase/Game Library/CollectionViewController/PVGameLibraryViewController.swift

    • Switched to synchronous BIOS cache access.
    +1/-1     
    PVGameLibraryUpdatesController.swift
    Use async cache retrieval in Game Library Updates               

    PVUI/Sources/PVUIBase/Game Library/PVGameLibraryUpdatesController.swift

    • Updated to use async cache retrieval for games and systems.
    +3/-3     
    Bug fix
    PVRootViewController.swift
    Improve security-scoped resource handling                               

    PVUI/Sources/PVSwiftUI/RootView/PVRootViewController.swift

    • Added logic to handle security-scoped resource access.
    +8/-6     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    ensures directories and caches are set up on first launch
    ensures cloud and local files import correctly
    various sequencing fixes for async stuff for RomDatabase and GameImporter
    @qodo-merge-pro qodo-merge-pro bot added enhancement improvements, enhancements, new features, additions bug_fix Review effort [1-5]: 3 labels Oct 18, 2024
    Copy link

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Potential Race Condition
    The reloadCache() method now uses a detached task to reload caches asynchronously. This could potentially lead to race conditions if other parts of the code access the cache while it's being reloaded.

    Error Handling
    The createDefaultDirectory method catches errors but only logs them. Consider whether more robust error handling or recovery is needed.

    Resource Management
    The stopAccessingSecurityScopedResource() is now conditionally called. Ensure this doesn't lead to resource leaks if securityScoped is incorrectly set.

    Copy link

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Implement proper error handling for asynchronous operations

    Consider using a more robust error handling mechanism instead of force unwrapping
    with try! to improve the stability of the code.

    PVLibrary/Sources/PVLibrary/Database/Realm Database/RomDatabase+Caches.swift [34-36]

     Task.detached { @MainActor in
    -    await self.reloadCaches()
    +    do {
    +        try await self.reloadCaches()
    +    } catch {
    +        ELOG("Failed to reload caches: \(error)")
    +    }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion enhances the stability of the code by adding error handling for asynchronous operations, which is crucial for maintaining robust and reliable software.

    8
    Implement proper error handling for database operations

    Consider using a more robust error handling mechanism instead of force unwrapping
    with try! to improve the stability of the code.

    PVLibrary/Sources/PVLibrary/Importer/Services/GameImporter/GameImporter.swift [176]

    -try! database.add(newBIOS, update: true)
    +do {
    +    try database.add(newBIOS, update: true)
    +} catch {
    +    ELOG("Failed to add BIOS: \(error)")
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion to replace force unwrapping with proper error handling significantly improves the code's stability and reliability, making it a valuable enhancement.

    8
    Use more descriptive variable names to improve code clarity

    Consider using a more descriptive variable name instead of database to improve code
    readability and maintainability.

    PVLibrary/Sources/PVLibrary/Configuration/PVEmulatorConfiguration+Frameworks.swift [174-176]

    -database.refresh()
    +romDatabase.refresh()
     //avoids conflicts if two BIOS share the same name - looking at you jagboot.rom
    -try! database.add(newBIOS, update: true)
    +try! romDatabase.add(newBIOS, update: true)
    • Apply this suggestion
    Suggestion importance[1-10]: 5

    Why: The suggestion to use a more descriptive variable name like 'romDatabase' instead of 'database' can improve code readability and maintainability. However, the impact is moderate as it does not address any functional issues.

    5
    Enhancement
    Refactor security-scoped resource handling into a separate method for better organization

    Consider moving the security-scoped resource access logic into a separate method to
    improve code organization and reusability.

    PVUI/Sources/PVSwiftUI/RootView/PVRootViewController.swift [206-217]

    -var securityScoped = false
    -
    -sortedUrls.forEach { url in
    +func handleSecurityScopedResource(_ url: URL, action: () -> Void) {
    +    let securityScoped = url.startAccessingSecurityScopedResource()
         defer {
             if securityScoped {
                 url.stopAccessingSecurityScopedResource()
             }
         }
    -    
    -    // Doesn't seem we need access in dev builds?
    -    // if this returns false, we don't need to balance with a stop call, so just hang on to the value
    -    securityScoped = url.startAccessingSecurityScopedResource()
    +    action()
    +}
     
    +sortedUrls.forEach { url in
    +    handleSecurityScopedResource(url) {
    +        // Original code here
    +    }
    +}
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Refactoring the security-scoped resource handling into a separate method enhances code organization and reusability, which is beneficial for maintaining and extending the codebase.

    7

    💡 Need additional feedback ? start a PR chat

    @JoeMatt JoeMatt merged commit 5bd9b54 into Provenance-Emu:develop-spm-2024 Oct 19, 2024
    2 of 3 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    bug_fix enhancement improvements, enhancements, new features, additions Review effort [1-5]: 3
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants