Skip to content

Commit d7708f5

Browse files
committed
Initial commit
0 parents  commit d7708f5

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BasedOnStyle: Google

code-unix.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <errno.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
5+
int main(int argc, char **argv) {
6+
if (argc < 2) {
7+
fprintf(stderr, "error: no arguments provided\n");
8+
return 1;
9+
}
10+
execvp(argv[1], &argv[1]);
11+
perror("execvp() failed");
12+
return errno;
13+
}

code-windows.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#define WIN32_LEAN_AND_MEAN
2+
3+
#include <shlwapi.h>
4+
#include <windows.h>
5+
6+
static const WCHAR *NextArgument(const WCHAR *commandLine) {
7+
if (*commandLine == '"') {
8+
commandLine++;
9+
while (*commandLine) {
10+
if (*commandLine++ == '"') {
11+
break;
12+
}
13+
}
14+
} else {
15+
while (*commandLine && *commandLine != ' ' && *commandLine != '\t') {
16+
commandLine++;
17+
}
18+
}
19+
while (*commandLine == ' ' || *commandLine == '\t') {
20+
commandLine++;
21+
}
22+
return commandLine;
23+
}
24+
25+
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline,
26+
int cmdshow) {
27+
WCHAR *commandLine = StrDupW(NextArgument(GetCommandLineW()));
28+
29+
STARTUPINFOW si;
30+
ZeroMemory(&si, sizeof(si));
31+
si.cb = sizeof(si);
32+
33+
PROCESS_INFORMATION pi;
34+
ZeroMemory(&pi, sizeof(pi));
35+
36+
if (CreateProcessW(
37+
/*lpApplicationName=*/NULL, commandLine,
38+
/*lpProcessAttributes=*/NULL,
39+
/*lpThreadAttributes=*/NULL,
40+
/*bInheritHandles=*/FALSE,
41+
/*dwCreationFlags=*/0,
42+
/*lpEnvironment=*/NULL,
43+
/*lpCurrentDirectory=*/NULL, &si, &pi)) {
44+
return 0;
45+
}
46+
47+
DWORD error = GetLastError();
48+
WCHAR *message;
49+
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
50+
/*lpSource=*/NULL, /*dwMessageId=*/error,
51+
/*dwLanguageId=*/0, (WCHAR *)&message, /*nSize=*/0,
52+
/*Arguments=*/NULL);
53+
MessageBoxW(NULL, message, L"code.exe", MB_OK | MB_ICONERROR);
54+
return error;
55+
}

unity.el

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)