Skip to content

Commit

Permalink
implement progress events
Browse files Browse the repository at this point in the history
  • Loading branch information
tappi287 committed Jan 30, 2022
1 parent 5b7c165 commit 74246cf
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 10 deletions.
5 changes: 3 additions & 2 deletions app/app_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import app
import app.mod
from app.app_settings import AppSettings
from app.util.manifest_worker import ManifestWorker
from app.util.manifest_worker import ManifestWorker, run_update_steam_apps
from app.util.custom_app import create_custom_app, scan_custom_library
from app.util.utils import get_name_id

Expand Down Expand Up @@ -133,7 +133,8 @@ def get_steam_lib_fn():
return json.dumps({'result': False, 'msg': msg})

logging.debug('Acquiring OpenVR Dll locations for %s Steam Apps.', len(steam_apps.keys()))
steam_apps = ManifestWorker.update_steam_apps(steam_apps)
# steam_apps = ManifestWorker.update_steam_apps(steam_apps)
steam_apps = run_update_steam_apps(steam_apps)

# -- Restore Mod settings cached on disk and add custom apps
cached_steam_apps = AppSettings.load_steam_apps()
Expand Down
43 changes: 43 additions & 0 deletions app/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import logging
from typing import Optional

import eel
import gevent
import gevent.event


class AppBaseEvent:
@classmethod
def get_nowait(cls) -> Optional[gevent.event.AsyncResult]:
if hasattr(cls, 'result'):
try:
return cls.result.get_nowait()
except gevent.Timeout:
pass

@classmethod
def reset(cls):
if hasattr(cls, 'event') and hasattr(cls, 'result'):
cls.event.clear()
cls.result = gevent.event.AsyncResult()


class ProgressEvent(AppBaseEvent):
event = gevent.event.Event()
result = gevent.event.AsyncResult()

@classmethod
def set(cls, value):
cls.result.set(value)


def progress_update(message):
ProgressEvent.set(message)


def app_event_loop():
progress_event = ProgressEvent.get_nowait()
if progress_event:
logging.debug('Progress event callback to FrontEnd: %s', progress_event)
eel.update_progress(progress_event)
ProgressEvent.reset()
4 changes: 2 additions & 2 deletions app/util/custom_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import app
import app.mod
from app.util.manifest_worker import ManifestWorker
from app.util.manifest_worker import ManifestWorker, run_update_steam_apps
from app.util.utils import get_name_id


Expand Down Expand Up @@ -63,7 +63,7 @@ def scan_custom_library(dir_id: str, path: Path):
custom_apps[app_id] = custom_app

# -- Scan
custom_apps = ManifestWorker.update_steam_apps(custom_apps)
custom_apps = run_update_steam_apps(custom_apps)

# -- Remove empty entries
remove_ids = set()
Expand Down
35 changes: 33 additions & 2 deletions app/util/manifest_worker.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import threading
from queue import Queue
import concurrent.futures
import logging
import os
from pathlib import Path
from typing import Optional, List

import gevent

from app.globals import OPEN_VR_DLL, EXE_NAME
from app.mod import get_available_mods
from app.events import progress_update


def run_update_steam_apps(steam_apps: dict) -> dict:
""" Calls ManifestWorker.update_steam_apps from thread to not block the event loop """
q = Queue()
t = threading.Thread(target=ManifestWorker.update_steam_apps, args=(steam_apps, q))
t.start()

# -- Wait for thread to finish
while t.is_alive():
gevent.sleep(1)
t.join(timeout=0.1)

return q.get()


class ManifestWorker:
Expand All @@ -14,7 +33,7 @@ class ManifestWorker:
chunk_size = 16 # Number of Manifests per worker

@classmethod
def update_steam_apps(cls, steam_apps: dict) -> dict:
def update_steam_apps(cls, steam_apps: dict, queue: Queue = None) -> dict:
app_id_list = list(steam_apps.keys())

# -- Split server addresses into chunks for workers
Expand All @@ -30,8 +49,10 @@ def update_steam_apps(cls, steam_apps: dict) -> dict:
[steam_apps.get(app_id) for app_id in id_chunk_ls]
)

logging.debug('Using %s worker threads to search for OpenVr Api Dll in %s SteamApps in %s chunks.',
logging.debug('Using maximum of %s worker threads to search thru %s Apps in %s chunks.',
cls.max_workers, len(steam_apps.keys()), len(manifest_ls_chunks))
progress = 0
progress_update(f'{progress} / {len(steam_apps.keys())}')

with concurrent.futures.ThreadPoolExecutor(max_workers=cls.max_workers) as executor:
future_info = {
Expand All @@ -55,6 +76,14 @@ def update_steam_apps(cls, steam_apps: dict) -> dict:
for manifest in manifest_ls:
steam_apps[manifest.get('appid')] = manifest

# -- Update Progress
progress += len(manifest_ls)
manifest = manifest_ls[-1:][0]
progress_update(f'{manifest.get("path", " ")[0:2]} {progress} / {len(steam_apps.keys())}')

if queue is not None:
queue.put(steam_apps)

return steam_apps

@staticmethod
Expand All @@ -71,6 +100,8 @@ def worker(manifest_ls):
logging.error('Error reading path for: %s %s', manifest.get('name', 'Unknown'), e)
continue

progress_update(f'{manifest["path"][0:2]} {Path(manifest["path"]).stem}')

# -- LookUp OpenVr Api location(s)
try:
open_vr_dll_path_ls = ManifestWorker.find_open_vr_dll(Path(manifest['path']))
Expand Down
5 changes: 4 additions & 1 deletion openvr_fsr_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import eel

from app import expose_app_methods, CLOSE_EVENT
from app.events import app_event_loop
from app.app_settings import AppSettings
from app.globals import FROZEN, get_version
from app.log import setup_logging
Expand Down Expand Up @@ -74,8 +75,10 @@ def start_eel():

# -- Run until window/tab closed
while not CLOSE_EVENT.is_set():
CLOSE_EVENT.wait(timeout=1)

# --- Event loop ---
CLOSE_EVENT.wait(timeout=10)
app_event_loop()

# Capture exception events
AppExceptionHook.exception_event_loop()
Expand Down
12 changes: 12 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ async function appExceptionFunc (event) {
window.dispatchEvent(excEvent)
}
// --- />
// --- </ Prepare receiving Progress Updates
window.eel.expose(updateProgressFunc, 'update_progress')
async function updateProgressFunc (event) {
const progressEvent = new CustomEvent('update-progress-event', {detail: event})
window.dispatchEvent(progressEvent)
}
// --- />
export default {
name: 'App',
Expand All @@ -76,6 +83,9 @@ export default {
resetAdmin: async function () {
await window.eel.reset_admin()
},
emitProgressEvent: function (event) {
this.$eventHub.$emit('update-progress', event.detail)
},
},
components: {
Updater,
Expand All @@ -88,11 +98,13 @@ export default {
created() {
window.addEventListener('beforeunload', this.requestClose)
window.addEventListener('app-exception-event', this.setException)
window.addEventListener('update-progress-event', this.emitProgressEvent)
},
computed: {
},
destroyed() {
window.removeEventListener('app-exception-event', this.setException)
window.removeEventListener('update-progress-event', this.emitProgressEvent)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/components/DirManager.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export default {
if (this.addDir === '') { return }
this.$eventHub.$emit('set-busy', true)
this.$eventHub.$emit('toggle-dir-manager')
this.$eventHub.$emit('update-progress', '')
const r = await getEelJsonObject(window.eel.add_custom_dir(this.addDir)())
if (!r.result) {
Expand Down
23 changes: 21 additions & 2 deletions src/components/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@

<!-- Busy Overlay -->
<b-overlay no-wrap fixed :show="isBusy" blur="1px" variant="dark" rounded>
<b-spinner></b-spinner>
<template #overlay>
<div class="text-center">
<b-spinner></b-spinner>
<template v-if="progressMessage !== ''">
<div></div>
<span class="mt-4">{{ progressMessage }}</span>
</template>
</div>
</template>
</b-overlay>
</div>
</template>
Expand All @@ -102,7 +110,7 @@ export default {
return {
showDirManager: false, showDirManagerMod: true, showDirManagerCustom: true,
version: version,
isBusy: false,
isBusy: false, progressMessage: '',
appName: process.env.VUE_APP_FRIENDLY_NAME,
}
},
Expand All @@ -120,6 +128,15 @@ export default {
solid: true,
})
},
setProgressMessage: function (message) {
// Set progress message
this.progressMessage = message
// Clear after timeout
setTimeout(() => {
this.setProgressMessage('')
}, 15000)
},
toggleDirManager: function (showMod = true, showCustom = true) {
this.showDirManagerMod = showMod; this.showDirManagerCustom = showCustom
this.showDirManager = !this.showDirManager
Expand All @@ -133,11 +150,13 @@ export default {
this.$eventHub.$on('make-toast', this.makeToast)
this.$eventHub.$on('set-busy', this.setBusy)
this.$eventHub.$on('toggle-dir-manager', this.toggleDirManager)
this.$eventHub.$on('update-progress', this.setProgressMessage)
},
beforeDestroy() {
this.$eventHub.$off('make-toast')
this.$eventHub.$off('set-busy')
this.$eventHub.$off('toggle-dir-manager')
this.$eventHub.$off('update-progress')
},
components: {
DirManager,
Expand Down
22 changes: 21 additions & 1 deletion src/components/SteamLibTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<b-input-group-text>
<!-- Background Busy Indicator -->
<b-spinner class="ml-2" small v-if="backgroundBusy"></b-spinner>
<span class="ml-2" v-if="backgroundBusy">{{ $t('lib.bgSearch') }}</span>
<span class="ml-2" v-if="backgroundBusy">{{ steamLibBusyMessage }}</span>
<b-icon icon="filter" aria-hidden="true"></b-icon>
</b-input-group-text>
</b-input-group-prepend>
Expand Down Expand Up @@ -188,10 +188,20 @@ export default {
{ key: 'openVr', label: 'Open VR', sortable: true, class: 'text-right' },
],
currentFsrVersion: '', currentFovVersion: '', currentVrpVersion: '',
progressMessage: ''
}
},
methods: {
isBusy: function () { return this.backgroundBusy || this.steamlibBusy },
setProgressMessage: function (message) {
// Set progress message
this.progressMessage = message
// Clear after timeout
setTimeout(() => {
this.setProgressMessage('')
}, 10000)
},
updateTableSort(sortByRow = 'name', descending = false) {
this.sortBy = sortByRow; this.sortDesc = descending
},
Expand Down Expand Up @@ -258,6 +268,8 @@ export default {
// Scan the disk in the background
this.backgroundBusy = true
const r = await getEelJsonObject(window.eel.get_steam_lib()())
this.$eventHub.$emit('update-progress', '')
if (!r.result) {
this.$eventHub.$emit('make-toast',
'Could not load Steam Library!', 'danger', 'Steam Library', true, -1)
Expand Down Expand Up @@ -313,6 +325,12 @@ export default {
tableBusy() {
return this.isBusy()
},
steamLibBusyMessage() {
if (this.progressMessage !== '') {
return this.$t('lib.bgSearchPre') + this.progressMessage + ' ...'
}
return this.$t('lib.bgSearch')
},
computedList() {
let steamTableData = []
for (const appId in this.steamApps) {
Expand All @@ -327,6 +345,7 @@ export default {
created() {
this.$eventHub.$on('reload-steam-lib', this.loadSteamLib)
this.$eventHub.$on('sort-steam-lib', this.updateTableSort)
this.$eventHub.$on('update-progress', this.setProgressMessage)
},
async mounted() {
await this.loadSteamLib()
Expand All @@ -340,6 +359,7 @@ export default {
destroyed() {
this.$eventHub.$off('reload-steam-lib')
this.$eventHub.$off('sort-steam-lib')
this.$eventHub.$off('update-progress')
}
}
</script>
Expand Down
1 change: 1 addition & 0 deletions src/lang/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"appId": "App ID",
"name": "Anwendungs Name",
"sizeGb": "Größe",
"bgSearchPre": "Scanne: ",
"bgSearch": "Durchsuche Steam Bibliothek im Hintergrund ...",
"busy": "Steam Bibliothek wird gescannt ...",
"noResults": "Keine Ergebnisse mit aktuellen Filter Einstellungen",
Expand Down
1 change: 1 addition & 0 deletions src/lang/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"appId": "App ID",
"name": "Name",
"sizeGb": "Size",
"bgSearchPre": "Scanning: ",
"bgSearch": "Updating SteamLib in the background ...",
"busy": "Steam library is being scanned ...",
"noResults": "No results with current filter settings",
Expand Down

0 comments on commit 74246cf

Please sign in to comment.