OSDN Git Service

Port over Vanilla Angband's file_open_hook vauto3.0.0-alpha85-02f29bbf0
authorEric Branlund <ebranlund@fastmail.com>
Tue, 20 Jun 2023 01:46:19 +0000 (19:46 -0600)
committerbackwardsEric <55062581+backwardsEric@users.noreply.github.com>
Tue, 20 Jun 2023 03:09:57 +0000 (21:09 -0600)
Resolves https://github.com/backwardsEric/hengband/issues/20 : on macOS, double clicking on save files generated after this change will open the application with that savefile.

src/cmd-io/cmd-process-screen.cpp
src/cocoa/Angband-Cocoa.xml
src/main-cocoa.mm
src/save/floor-writer.cpp
src/save/save.cpp
src/util/angband-files.cpp
src/util/angband-files.h

index 3806329..df79c36 100644 (file)
@@ -200,7 +200,8 @@ void do_cmd_save_screen_html_aux(char *filename, int message)
 {
     TERM_LEN wid, hgt;
     term_get_size(&wid, &hgt);
-    auto *fff = angband_fopen(filename, FileOpenMode::WRITE);
+    auto *fff = angband_fopen(filename, FileOpenMode::WRITE, false,
+        FileOpenType::HTML);
     if (!check_screen_html_can_open(fff, filename, message)) {
         return;
     }
index 7c20a8a..f64f28a 100644 (file)
@@ -45,7 +45,7 @@
                        <string>$NAME$ saved game</string>
                        <key>CFBundleTypeOSTypes</key>
                        <array>
-                               <string>SAVE</string>
+                               <string>HENG</string>
                        </array>
                        <key>CFBundleTypeRole</key>
                        <string>Editor</string>
index 8db8dbf..79346c4 100644 (file)
 #include "system/grafmode.h"
 
 #ifdef MACH_O_COCOA
+
+/* Default creator signature */
+#ifndef ANGBAND_CREATOR
+# define ANGBAND_CREATOR 'H300'
+#endif
+
 /* Mac headers */
 #import "cocoa/AppDelegate.h"
 #import "cocoa/SoundAndMusic.h"
@@ -5531,6 +5537,34 @@ static void init_windows(void)
 }
 
 /**
+ * Set HFS file type and creator codes on a path
+ */
+static void cocoa_file_open_hook(const std::filesystem::path &path, const FileOpenType ftype)
+{
+    @autoreleasepool {
+       NSString *pathString = [NSString stringWithUTF8String:path.native().data()];
+       if (pathString)
+        {
+           uint32_t mac_type = 'TEXT';
+            if (ftype == FileOpenType::RAW)
+                mac_type = 'DATA';
+            else if (ftype == FileOpenType::SAVE)
+                mac_type = 'HENG';
+
+           NSDictionary *attrs =
+               [NSDictionary dictionaryWithObjectsAndKeys:
+                             [NSNumber numberWithUnsignedLong:mac_type],
+                             NSFileHFSTypeCode,
+                             [NSNumber numberWithUnsignedLong:ANGBAND_CREATOR],
+                             NSFileHFSCreatorCode,
+                             nil];
+           [[NSFileManager defaultManager]
+               setAttributes:attrs ofItemAtPath:pathString error:NULL];
+       }
+    }
+}
+
+/**
  * ------------------------------------------------------------------------
  * Main program
  * ------------------------------------------------------------------------ */
@@ -5695,6 +5729,9 @@ static void init_windows(void)
        plog_aux = hook_plog;
        quit_aux = hook_quit;
 
+       /* Hook in to the file open routine */
+       file_open_hook = cocoa_file_open_hook;
+
        /* Initialize file paths */
        prepare_paths_and_directories();
 
index a19b2f8..54b90e0 100644 (file)
@@ -270,7 +270,8 @@ bool save_floor(PlayerType *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mode
     if (fd >= 0) {
         (void)fd_close(fd);
         safe_setuid_grab();
-        saving_savefile = angband_fopen(floor_savefile, FileOpenMode::WRITE, true);
+        saving_savefile = angband_fopen(floor_savefile, FileOpenMode::WRITE,
+            true, FileOpenType::RAW);
         safe_setuid_drop();
         if (saving_savefile) {
             if (save_floor_aux(player_ptr, sf_ptr)) {
index 231d76a..f73f618 100644 (file)
@@ -265,7 +265,8 @@ static bool save_player_aux(PlayerType *player_ptr, const std::filesystem::path
     if (fd >= 0) {
         (void)fd_close(fd);
         safe_setuid_grab();
-        saving_savefile = angband_fopen(path, FileOpenMode::WRITE, true);
+        saving_savefile = angband_fopen(path, FileOpenMode::WRITE, true,
+            FileOpenType::SAVE);
         safe_setuid_drop();
         if (saving_savefile) {
             if (wr_savefile_new(player_ptr, type)) {
index f62db2d..14cc171 100644 (file)
@@ -5,6 +5,8 @@
 #include <sstream>
 #include <string>
 
+void (*file_open_hook)(const std::filesystem::path &path, const FileOpenType ftype) = 0;
+
 #ifdef SET_UID
 
 #ifndef HAVE_USLEEP
@@ -207,11 +209,17 @@ static std::string make_file_mode(const FileOpenMode mode, const bool is_binary)
  * @param is_binary バイナリモードか否か (無指定の場合false:テキストモード)
  * @return ファイルポインタ
  */
-FILE *angband_fopen(const std::filesystem::path &path, const FileOpenMode mode, const bool is_binary)
+FILE *angband_fopen(const std::filesystem::path &path, const FileOpenMode mode, const bool is_binary, const FileOpenType ftype)
 {
+    FILE *result;
+
     const auto &parsed_path = path_parse(path);
     const auto &open_mode = make_file_mode(mode, is_binary);
-    return fopen(parsed_path.string().data(), open_mode.data());
+    result = fopen(parsed_path.string().data(), open_mode.data());
+    if (result && mode != FileOpenMode::READ && file_open_hook) {
+        file_open_hook(path, ftype);
+    }
+    return result;
 }
 
 /*
index 6cdbbea..083bf7d 100644 (file)
@@ -41,9 +41,19 @@ enum class FileOpenMode {
     APPEND,
 };
 
+// Specifies what king of thing a file is, when writing.  See file_open().
+enum class FileOpenType {
+    TEXT,
+    SAVE,
+    RAW,
+    HTML,
+};
+
+extern void (*file_open_hook)(const std::filesystem::path &path, const FileOpenType ftype);
+
 std::filesystem::path path_parse(const std::filesystem::path &path);
 std::filesystem::path path_build(const std::filesystem::path &path, std::string_view file);
-FILE *angband_fopen(const std::filesystem::path &path, const FileOpenMode mode, const bool is_binary = false);
+FILE *angband_fopen(const std::filesystem::path &path, const FileOpenMode mode, const bool is_binary = false, const FileOpenType ftype = FileOpenType::TEXT);
 FILE *angband_fopen_temp(char *buf, int max);
 errr angband_fgets(FILE *fff, char *buf, ulong n);
 errr angband_fputs(FILE *fff, concptr buf, ulong n);