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.
{
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;
}
<string>$NAME$ saved game</string>
<key>CFBundleTypeOSTypes</key>
<array>
- <string>SAVE</string>
+ <string>HENG</string>
</array>
<key>CFBundleTypeRole</key>
<string>Editor</string>
#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"
}
/**
+ * 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
* ------------------------------------------------------------------------ */
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();
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)) {
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)) {
#include <sstream>
#include <string>
+void (*file_open_hook)(const std::filesystem::path &path, const FileOpenType ftype) = 0;
+
#ifdef SET_UID
#ifndef HAVE_USLEEP
* @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;
}
/*
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);