|
| 1 | +;;; unity.el --- Unity integration for Emacs -*- lexical-binding:t -*- |
| 2 | +;;; Commentary: |
| 3 | +;;; Code: |
| 4 | + |
| 5 | +(defgroup unity nil |
| 6 | + "Unity game engine integration." |
| 7 | + :group 'external) |
| 8 | + |
| 9 | +(defcustom unity-var-directory |
| 10 | + (expand-file-name (convert-standard-filename "var/unity/") |
| 11 | + user-emacs-directory) |
| 12 | + "Directory for persistent data." |
| 13 | + :type 'string |
| 14 | + :group 'unity) |
| 15 | + |
| 16 | +(defcustom unity-cc |
| 17 | + "gcc" |
| 18 | + "C compiler command to build code shim on Unix-like systems." |
| 19 | + :type 'string |
| 20 | + :group 'unity) |
| 21 | + |
| 22 | +(defun unity--code-binary-file () |
| 23 | + "Return the file name of the code shim." |
| 24 | + (concat unity-var-directory (if (eq system-type 'windows-nt) |
| 25 | + "code.exe" |
| 26 | + "code"))) |
| 27 | + |
| 28 | +(defun unity--project-path-p (path) |
| 29 | + "Return t if PATH is in a Unity project." |
| 30 | + (string-match-p "/[Aa]ssets/" path)) |
| 31 | + |
| 32 | +(defun unity--rename-file-advice (file newname &optional ok-if-already-exists) |
| 33 | + "Advice function for `rename-file' for renaming Unity files. |
| 34 | +
|
| 35 | +FILE, NEWNAME, and OK-IF-ALREADY-EXISTS are documented by `rename-file'." |
| 36 | + (when (unity--project-path-p file) |
| 37 | + (let ((meta-file (concat file ".meta"))) |
| 38 | + (when (file-exists-p meta-file) |
| 39 | + (rename-file meta-file (concat newname ".meta") |
| 40 | + ok-if-already-exists))))) |
| 41 | + |
| 42 | +(defun unity--build-code-shim () |
| 43 | + "Build the code shim." |
| 44 | + (make-directory unity-var-directory t) |
| 45 | + (when (get-buffer "*unity-subprocess*") |
| 46 | + (kill-buffer "*unity-subprocess*")) |
| 47 | + (let ((subprocess-buffer (get-buffer-create "*unity-subprocess*")) |
| 48 | + (source-name |
| 49 | + (concat (file-name-directory (or load-file-name buffer-file-name)) |
| 50 | + (if (eq system-type 'windows-nt) |
| 51 | + "code-windows.c" |
| 52 | + "code-unix.c")))) |
| 53 | + (if (eq (call-process unity-cc nil subprocess-buffer nil |
| 54 | + source-name "-o" (unity--code-binary-file)) |
| 55 | + 0) |
| 56 | + (progn |
| 57 | + (kill-buffer subprocess-buffer) |
| 58 | + (message "Unity code shim built successfully.")) |
| 59 | + (progn |
| 60 | + (switch-to-buffer-other-window subprocess-buffer) |
| 61 | + (special-mode))))) |
| 62 | + |
| 63 | +(defun unity-build-code-shim () |
| 64 | + "Build the code shim if it's not already built." |
| 65 | + (unless (file-exists-p (unity--code-binary-file)) |
| 66 | + (unity-build-code-shim))) |
| 67 | + |
| 68 | +(defun unity-rebuild-code-shim () |
| 69 | + "Force rebuild the code shim." |
| 70 | + (when (file-exists-p (unity--code-binary-file)) |
| 71 | + (delete-file (unity--code-binary-file))) |
| 72 | + (unity--build-code-shim)) |
| 73 | + |
| 74 | +(defun unity-setup () |
| 75 | + "Activate Unity.el integration." |
| 76 | + (interactive) |
| 77 | + (advice-add #'rename-file :after #'unity--rename-file-advice)) |
| 78 | + |
| 79 | +(provide 'unity) |
| 80 | + |
| 81 | +;;; unity.el ends here |
0 commit comments