macCocoaPlatform.mm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import <Cocoa/Cocoa.h>
  23. #import <Carbon/Carbon.h>
  24. #include <unistd.h>
  25. #include "platform/platform.h"
  26. #include "console/console.h"
  27. #include "core/stringTable.h"
  28. #include "platform/platformInput.h"
  29. #include "platform/threads/thread.h"
  30. #pragma mark ---- Various Directories ----
  31. //-----------------------------------------------------------------------------
  32. const char* Platform::getUserDataDirectory()
  33. {
  34. // application support directory is most in line with the current usages of this function.
  35. // this may change with later usage
  36. // perhaps the user data directory should be pref-controlled?
  37. NSString *nsDataDir = [@"~/Library/Application Support/" stringByStandardizingPath];
  38. return StringTable->insert([nsDataDir UTF8String]);
  39. }
  40. //-----------------------------------------------------------------------------
  41. const char* Platform::getUserHomeDirectory()
  42. {
  43. return StringTable->insert([[@"~/" stringByStandardizingPath] UTF8String]);
  44. }
  45. //-----------------------------------------------------------------------------
  46. StringTableEntry osGetTemporaryDirectory()
  47. {
  48. NSString *tdir = NSTemporaryDirectory();
  49. const char *path = [tdir UTF8String];
  50. return StringTable->insert(path);
  51. }
  52. #pragma mark ---- Administrator ----
  53. //-----------------------------------------------------------------------------
  54. bool Platform::getUserIsAdministrator()
  55. {
  56. // if we can write to /Library, we're probably an admin
  57. // HACK: this is not really very good, because people can chmod Library.
  58. return (access("/Library", W_OK) == 0);
  59. }
  60. #pragma mark ---- Cosmetic ----
  61. //-----------------------------------------------------------------------------
  62. bool Platform::displaySplashWindow()
  63. {
  64. return false;
  65. }
  66. #pragma mark ---- File IO ----
  67. //-----------------------------------------------------------------------------
  68. bool dPathCopy(const char* source, const char* dest, bool nooverwrite)
  69. {
  70. NSFileManager *manager = [NSFileManager defaultManager];
  71. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  72. NSString *nsource = [[NSString stringWithUTF8String:source] stringByStandardizingPath];
  73. NSString *ndest = [[NSString stringWithUTF8String:dest] stringByStandardizingPath];
  74. NSString *ndestFolder = [ndest stringByDeletingLastPathComponent];
  75. if(! [manager fileExistsAtPath:nsource])
  76. {
  77. Con::errorf("dPathCopy: no file exists at %s",source);
  78. return false;
  79. }
  80. if( [manager fileExistsAtPath:ndest] )
  81. {
  82. if(nooverwrite)
  83. {
  84. Con::errorf("dPathCopy: file already exists at %s",dest);
  85. return false;
  86. }
  87. Con::warnf("Deleting files at path: %s", dest);
  88. bool deleted = [manager removeFileAtPath:ndest handler:nil];
  89. if(!deleted)
  90. {
  91. Con::errorf("Copy failed! Could not delete files at path: %s", dest);
  92. return false;
  93. }
  94. }
  95. if([manager fileExistsAtPath:ndestFolder] == NO)
  96. {
  97. ndestFolder = [ndestFolder stringByAppendingString:@"/"]; // createpath requires a trailing slash
  98. Platform::createPath([ndestFolder UTF8String]);
  99. }
  100. bool ret = [manager copyPath:nsource toPath:ndest handler:nil];
  101. [pool release];
  102. return ret;
  103. }
  104. //-----------------------------------------------------------------------------
  105. bool dFileRename(const char *source, const char *dest)
  106. {
  107. if(source == NULL || dest == NULL)
  108. return false;
  109. NSFileManager *manager = [NSFileManager defaultManager];
  110. NSString *nsource = [manager stringWithFileSystemRepresentation:source length:dStrlen(source)];
  111. NSString *ndest = [manager stringWithFileSystemRepresentation:dest length:dStrlen(dest)];
  112. if(! [manager fileExistsAtPath:nsource])
  113. {
  114. Con::errorf("dFileRename: no file exists at %s",source);
  115. return false;
  116. }
  117. if( [manager fileExistsAtPath:ndest] )
  118. {
  119. Con::warnf("dFileRename: Deleting files at path: %s", dest);
  120. }
  121. bool ret = [manager movePath:nsource toPath:ndest handler:nil];
  122. return ret;
  123. }
  124. #pragma mark -
  125. #pragma mark ---- ShellExecute ----
  126. class ExecuteThread : public Thread
  127. {
  128. const char* zargs;
  129. const char* directory;
  130. const char* executable;
  131. public:
  132. ExecuteThread(const char *_executable, const char *_args /* = NULL */, const char *_directory /* = NULL */) : Thread(0, NULL, false, true)
  133. {
  134. zargs = dStrdup(_args);
  135. directory = dStrdup(_directory);
  136. executable = dStrdup(_executable);
  137. start();
  138. }
  139. virtual void run(void* arg);
  140. };
  141. static char* _unDoubleQuote(char* arg)
  142. {
  143. U32 len = dStrlen(arg);
  144. if(!len)
  145. return arg;
  146. if(arg[0] == '"' && arg[len-1] == '"')
  147. {
  148. arg[len - 1] = '\0';
  149. return arg + 1;
  150. }
  151. return arg;
  152. }
  153. void ExecuteThread::run(void* arg)
  154. {
  155. // 2k should be enough. if it's not, bail.
  156. // char buf[2048];
  157. // U32 len = dSprintf(buf, sizeof(buf), "%s %s -workingDir %s", executable, args, directory);
  158. // if( len >= sizeof(buf))
  159. // {
  160. // Con::errorf("shellExecute(): the command was too long, and won't be run.");
  161. // return;
  162. // }
  163. // // calls sh with the string and blocks until the command returns.
  164. // system(buf);
  165. // FIXME: there is absolutely no error checking in here.
  166. printf("creating nstask\n");
  167. NSTask *aTask = [[NSTask alloc] init];
  168. NSMutableArray *array = [NSMutableArray array];
  169. // scan the args list, breaking it up, space delimited, backslash escaped.
  170. U32 len = dStrlen(zargs);
  171. char args[len+1];
  172. dStrncpy(args, zargs, len+1);
  173. char *lastarg = args;
  174. bool escaping = false;
  175. for(int i = 0; i< len; i++)
  176. {
  177. char c = args[i];
  178. // a backslash escapes the next character
  179. if(escaping)
  180. continue;
  181. if(c == '\\')
  182. escaping = true;
  183. if(c == ' ')
  184. {
  185. args[i] = '\0';
  186. if(*lastarg)
  187. [array addObject:[NSString stringWithUTF8String: _unDoubleQuote(lastarg)]];
  188. lastarg = args + i + 1;
  189. }
  190. }
  191. if(*lastarg)
  192. [array addObject:[NSString stringWithUTF8String: _unDoubleQuote(lastarg)]];
  193. [aTask setArguments: array];
  194. [aTask setCurrentDirectoryPath:[NSString stringWithUTF8String: this->directory]];
  195. [aTask setLaunchPath:[NSString stringWithUTF8String:executable]];
  196. [aTask launch];
  197. [aTask waitUntilExit];
  198. U32 ret = [aTask terminationStatus];
  199. Con::executef("onExecuteDone", Con::getIntArg(ret));
  200. printf("done nstask\n");
  201. }
  202. ConsoleFunction(shellExecute, bool, 2, 4, "(executable, [args], [directory])")
  203. {
  204. ExecuteThread *et = new ExecuteThread(argv[1], argc > 2 ? argv[2] : NULL, argc > 3 ? argv[3] : NULL);
  205. TORQUE_UNUSED(et);
  206. return true; // Bug: BPNC error: need feedback on whether the command was sucessful
  207. }