Browse Source

Move export into task system

I'm very pleased with how this turned out. In my tests, the export
function took ~150ms, and opening the file explorer took ~22ms. This
completely offloads that work to keep the UI responsive.
filter-focus
Macoy Madson 2 years ago
parent
commit
2d14aae90a
  1. 48
      src/Export.cake
  2. 6
      src/FileHelper.cake

48
src/Export.cake

@ -1,10 +1,13 @@
;; This file is somewhat misnamed. It handles taking categorized entries and processing them into a
;; format which can be exported. It also helps in cases where pure entries are desired
(import "FileHelper.cake" "FileSystem.cake"
"Dictionary.cake" "DynamicArray.cake"
"Dictionary.cake" "DynamicArray.cake" "TaskSystem.cake"
"Utilities.cake"
&comptime-only "CHelpers.cake")
;; TODO Need infect
(add-c-search-directory-module "Dependencies/enkiTS/src")
(c-import "<cstdio>" "<cassert>")
(forward-declare (struct directory-entry-userdata)
@ -177,8 +180,8 @@
(dynarray-free child-entries)
(set child-entries null))
(defun export-category-paths (entries (* directory-entry-userdata) ;; strdict
categories (* category-spec)) ;; dictionary
(def-task export-category-paths (entries (* directory-entry-userdata) ;; strdict
categories (* category-spec)) ;; dictionary
(var num-entries (unsigned int) (strdict-length entries))
(unless num-entries
(return))
@ -258,3 +261,42 @@
(fclose (path category-file > file-handle))))
(dynarray-free category-out-files)
(dynarray-free sorted-entries))
(var g-is-export-running bool false)
(def-task on-export-complete (entries (* directory-entry-userdata) ;; strdict
categories (* category-spec)) ;; dictionary
(dict-free entries)
(strdict-free categories)
(fprintf stderr "Finished export\n")
(set g-is-export-running false))
;; Do this on a separate thread because it takes a while. A quick scan of SDL code seems to suggest
;; this is fine. This isn't in on-export-complete because that's only on the main thread
(def-task open-userdata-system-file-explorer (should-open bool)
(when should-open
(open-system-file-explorer g-userdata-output-dir)))
(defun export-category-paths-start (entries (* directory-entry-userdata) ;; strdict
categories (* category-spec) ;; dictionary
open-file-explorer-on-complete bool)
;; Only one can be running at a time. TODO: User feedback?
(when g-is-export-running
(fprintf stderr "Already running an export job\n")
(return))
;; TODO: Use Introspection to more easily copy
(var copied-entries (* directory-entry-userdata) null) ;; strdict
(var copied-categories (* category-spec) null) ;; dictionary
(each-item-in-dict entries i entry (* directory-entry-userdata)
(strdict-set-struct copied-entries (deref entry)))
(each-item-in-dict categories i category (* category-spec)
(dict-set-struct copied-categories (deref category)))
(set g-is-export-running true)
(task-system-execute
(export-category-paths copied-entries copied-categories)
(parallel
(open-userdata-system-file-explorer open-file-explorer-on-complete)
(on-export-complete :pin-to-main-thread
copied-entries copied-categories))))

6
src/FileHelper.cake

@ -458,7 +458,7 @@
(path-append-divider-if-necessary path)
(dynstring-append path file-or-dir-name))
(defun-local open-system-file-explorer (in-directory (* (const char)))
(defun open-system-file-explorer (in-directory (* (const char)))
(var url-current-directory ([] 1024 char) (array 0))
(snprintf url-current-directory (sizeof url-current-directory) "file://%s"
in-directory)
@ -1026,9 +1026,7 @@
(var-static open-explorer-on-export bool true)
(when (imgui-call Button "Export all to text")
(export-category-paths g-userdata-dict g-categories-dict)
(when open-explorer-on-export
(open-system-file-explorer g-userdata-output-dir)))
(export-category-paths-start g-userdata-dict g-categories-dict open-explorer-on-export))
(when (imgui-call IsItemHovered)
(imgui-call BeginTooltip)
(imgui-call Text "Export a list of all files and directories in each category.")

Loading…
Cancel
Save