dir_access_macos.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**************************************************************************/
  2. /* dir_access_macos.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "dir_access_macos.h"
  31. #include "core/config/project_settings.h"
  32. #if defined(UNIX_ENABLED)
  33. #include <errno.h>
  34. #import <AppKit/NSWorkspace.h>
  35. #import <Foundation/Foundation.h>
  36. String DirAccessMacOS::fix_unicode_name(const char *p_name) const {
  37. String fname;
  38. if (p_name != nullptr) {
  39. NSString *nsstr = [[NSString stringWithUTF8String:p_name] precomposedStringWithCanonicalMapping];
  40. fname.parse_utf8([nsstr UTF8String]);
  41. }
  42. return fname;
  43. }
  44. int DirAccessMacOS::get_drive_count() {
  45. NSArray *res_keys = [NSArray arrayWithObjects:NSURLVolumeURLKey, NSURLIsSystemImmutableKey, nil];
  46. NSArray *vols = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:res_keys options:NSVolumeEnumerationSkipHiddenVolumes];
  47. return [vols count];
  48. }
  49. String DirAccessMacOS::get_drive(int p_drive) {
  50. NSArray *res_keys = [NSArray arrayWithObjects:NSURLVolumeURLKey, NSURLIsSystemImmutableKey, nil];
  51. NSArray *vols = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:res_keys options:NSVolumeEnumerationSkipHiddenVolumes];
  52. int count = [vols count];
  53. ERR_FAIL_INDEX_V(p_drive, count, "");
  54. String volname;
  55. NSString *path = [vols[p_drive] path];
  56. volname.parse_utf8([path UTF8String]);
  57. return volname;
  58. }
  59. bool DirAccessMacOS::is_hidden(const String &p_name) {
  60. String f = get_current_dir().path_join(p_name);
  61. NSURL *url = [NSURL fileURLWithPath:@(f.utf8().get_data())];
  62. NSNumber *hidden = nil;
  63. if (![url getResourceValue:&hidden forKey:NSURLIsHiddenKey error:nil]) {
  64. return DirAccessUnix::is_hidden(p_name);
  65. }
  66. return [hidden boolValue];
  67. }
  68. bool DirAccessMacOS::is_case_sensitive(const String &p_path) const {
  69. String f = p_path;
  70. if (!f.is_absolute_path()) {
  71. f = get_current_dir().path_join(f);
  72. }
  73. f = fix_path(f);
  74. NSURL *url = [NSURL fileURLWithPath:@(f.utf8().get_data())];
  75. NSNumber *cs = nil;
  76. if (![url getResourceValue:&cs forKey:NSURLVolumeSupportsCaseSensitiveNamesKey error:nil]) {
  77. return false;
  78. }
  79. return [cs boolValue];
  80. }
  81. #endif // UNIX_ENABLED