directoryHandling.tscript 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. function makedirectoryHandler(%targetTree, %folderExclusionList, %searchFilter)
  2. {
  3. %newHandler = new ScriptObject()
  4. {
  5. class = "directoryHandler";
  6. };
  7. %newHandler.currentAddress = "";
  8. %newHandler.treeCtrl = %targetTree;
  9. %newHandler.folderExclusionList = %folderExclusionList;
  10. %newHandler.searchFilter = %searchFilter;
  11. %newHandler.prevHistoryList = new ArrayObject();
  12. %newHandler.foreHistoryList = new ArrayObject();
  13. return %newHandler;
  14. }
  15. function directoryHandler::loadFolders(%this, %path, %parentId)
  16. {
  17. %modulesList = ModuleDatabase.findModules();
  18. //utilize home dir project setting here
  19. %paths = getDirectoryList(%path);
  20. for(%i=0; %i < getFieldCount(%paths); %i++)
  21. {
  22. %childPath = getField(%paths, %i);
  23. %fullChildPath = makeFullPath(%path @ "/" @ %childPath);
  24. %folderCount = getTokenCount(%childPath, "/");
  25. for(%f=0; %f < %folderCount; %f++)
  26. {
  27. %folderName = getToken(%childPath, "/", %f);
  28. %parentName = %this.treeCtrl.getItemText(%parentId);
  29. //we don't need to display the shadercache folder
  30. if(%parentName $= "Data" && (%folderName $= "shaderCache" || %folderName $= "cache"))
  31. continue;
  32. if(%folderName $= ".git")
  33. continue;
  34. %iconIdx = 3;
  35. //Lets see if any modules match our current path)
  36. for(%m=0; %m < getWordCount(%modulesList); %m++)
  37. {
  38. %moduleDef = getWord(%modulesList, %m);
  39. if(%moduleDef.modulePath $= %fullChildPath)
  40. {
  41. %iconIdx = 1;
  42. break;
  43. }
  44. }
  45. //if(ModuleDatabase.findModule(%folderName) !$= "")
  46. // %iconIdx = 1;
  47. %searchFoldersText = %this.searchFilter;
  48. if(%searchFoldersText !$= "")
  49. {
  50. if(strstr(strlwr(%folderName), strlwr(%searchFoldersText)) != -1)
  51. {
  52. %folderID = %this.treeCtrl.insertItem(%parentId, %folderName, %path, "", %iconIdx, %iconIdx+1);
  53. %this.loadFolders(%path @ "/" @ %folderName, %folderID);
  54. }
  55. }
  56. else
  57. {
  58. %folderID = %this.treeCtrl.insertItem(%parentId, %folderName, %path, "", %iconIdx, %iconIdx);
  59. %this.loadFolders(%path @ "/" @ %folderName, %folderID);
  60. }
  61. }
  62. }
  63. }
  64. function directoryHandler::navigateTo(%this, %address, %historyNav, %selectionNav)
  65. {
  66. //Don't bother navigating if it's to the place we already are
  67. if(%this.currentAddress $= %address)
  68. return;
  69. //clear the breadcrumb bar
  70. AssetBrowser_BreadcrumbBar.clear();
  71. //break down the address
  72. %folderCount = getTokenCount(%address, "/");
  73. //find our folder tree and action on it tree
  74. %folderId = %this.getFolderTreeItemFromAddress(%address);
  75. %this.oldAddress = %this.currentAddress;
  76. %this.currentAddress = %address;
  77. %this.selectedItem = %folderId;
  78. //This lets us update the tree selection if we didn't get here because of that
  79. if(%selectionNav $= "")
  80. {
  81. %this.treeCtrl.clearSelection();
  82. %this.treeCtrl.selectItem(%folderId);
  83. }
  84. //remove any history records that are 'newer' than this one
  85. if(%historyNav $= "")
  86. {
  87. %this.foreHistoryList.empty();
  88. if(%this.oldAddress !$= "")
  89. %this.prevHistoryList.push_front(%this.oldAddress);
  90. }
  91. %this.treeCtrl.buildVisibleTree(true);
  92. }
  93. function directoryHandler::navigateHistoryForward(%this)
  94. {
  95. if(%this.foreHistoryList.count() == 0)
  96. return;
  97. %newAddress = %this.foreHistoryList.getKey(0);
  98. %prevHistory = %this.currentAddress;
  99. %this.prevHistoryList.push_front(%prevHistory);
  100. %this.foreHistoryList.pop_front();
  101. %this.navigateTo(%newAddress, true);
  102. }
  103. function directoryHandler::navigateHistoryBack(%this)
  104. {
  105. if(%this.prevHistoryList.count() == 0)
  106. return;
  107. %newAddress = %this.prevHistoryList.getKey(0);
  108. %foreHistory = %this.currentAddress;
  109. %this.foreHistoryList.push_front(%foreHistory);
  110. %this.prevHistoryList.pop_front();
  111. %this.navigateTo(%newAddress, true);
  112. }
  113. function directoryHandler::getModuleFromAddress(%this, %address)
  114. {
  115. %moduleList = ModuleDatabase.findModules();
  116. for(%i=0; %i < getWordCount(%moduleList); %i++)
  117. {
  118. %module = getWord(%moduleList, %i);
  119. %modulePath = makeRelativePath(%module.ModulePath);
  120. //We don't want to add stuff directly to the root core or tools modules
  121. //if(%modulePath $= "Core" || %modulePath $= "Tools")
  122. // continue;
  123. if(startsWith(%address, %modulePath))
  124. {
  125. return %module;
  126. }
  127. }
  128. /*//break down the address
  129. %folderCount = getTokenCount(%address, "/");
  130. for(%f=0; %f < %folderCount; %f++)
  131. {
  132. %folderName = getToken(%address, "/", %f);
  133. %module = ModuleDatabase.findModule(%folderName);
  134. if(%module !$= "")
  135. return %module;
  136. }*/
  137. return "";
  138. }
  139. function directoryHandler::getFolderTreeItemFromAddress(%this, %address)
  140. {
  141. //break down the address
  142. %folderCount = getTokenCount(%address, "/");
  143. if(startsWith(%address, "data/") || startsWith(%address, "tools/") || startsWith(%address, "core/"))
  144. {
  145. %curItem = %this.treeCtrl.findChildItemByName(1, "Modules");
  146. }
  147. else
  148. {
  149. %curItem = 1;
  150. }
  151. %rebuiltPath = "";
  152. for(%f=0; %f < %folderCount; %f++)
  153. {
  154. %folderName = getToken(%address, "/", %f);
  155. %curItem = %this.treeCtrl.findChildItemByName(%curItem, %folderName);
  156. }
  157. return %curItem;
  158. }
  159. function directoryHandler::expandTreeToAddress(%this, %address)
  160. {
  161. //break down the address
  162. %folderCount = getTokenCount(%address, "/");
  163. %rootId = AssetBrowser-->filterTree.findItemByName("Content");
  164. %this.treeCtrl.expandItem(%rootId);
  165. if(startsWith(%address, "data/") || startsWith(%address, "tools/") || startsWith(%address, "core/"))
  166. {
  167. %curItem = %this.treeCtrl.findChildItemByName(1, "Modules");
  168. }
  169. else
  170. {
  171. %curItem = 1;
  172. }
  173. %rebuiltPath = "";
  174. for(%f=0; %f < %folderCount; %f++)
  175. {
  176. %folderName = getToken(%address, "/", %f);
  177. %curItem = %this.treeCtrl.findChildItemByName(%curItem, %folderName);
  178. %this.treeCtrl.expandItem(%curItem);
  179. }
  180. %this.treeCtrl.expandItem(%rootId);
  181. }
  182. function directoryHandler::createFolder(%this, %folderPath)
  183. {
  184. //make a dummy file
  185. %file = new FileObject();
  186. %file.openForWrite(%folderPath @ "/test");
  187. %file.close();
  188. fileDelete(%folderPath @ "/test");
  189. }
  190. function directoryHandler::deleteFolder(%this, %folderPath)
  191. {
  192. %fullPath = makeFullPath(%folderPath);
  193. //First, wipe out any files inside the folder first
  194. %file = findFirstFileMultiExpr( %fullPath @ "/*", true);
  195. while( %file !$= "" )
  196. {
  197. if (isFile(%file))
  198. {
  199. %success = fileDelete( %file );
  200. if(!%success)
  201. {
  202. error("doDeleteFolder - unable to delete file " @ %file);
  203. return;
  204. }
  205. }
  206. %file = findNextFileMultiExpr( %fullPath @ "/*" );
  207. }
  208. //next, walk through and delete any subfolders that may be remaining
  209. %finalDeleteAttempt = false;
  210. while(IsDirectory(%fullPath) && fileDelete(%fullPath) == 0)
  211. {
  212. //We couldn't delete the folder, so get a directory list and recurse through it, deleteing them as we go
  213. %paths = getDirectoryList(%fullPath);
  214. // If nothing is in this directory, let the loop run once more and if that fails we're not going
  215. // to delete the folder. This prevents an infinite loop if for some reason the directory cannot
  216. // be deleted.
  217. %pathCount = getFieldCount(%paths);
  218. if (%pathCount == 0)
  219. {
  220. if (%finalDeleteattempt)
  221. {
  222. error("doDeleteFolder - unable to delete directory " @ %fullPath);
  223. return;
  224. }
  225. %finalDeleteAttempt = true;
  226. continue;
  227. }
  228. for(%i=0; %i < %pathCount; %i++)
  229. {
  230. %childPath = getField(%paths, %i);
  231. %this.deleteFolder(%fullPath @ "/" @ %childPath);
  232. }
  233. }
  234. }
  235. function directoryHandler::copyFolder(%this, %fromFolder, %toFolder)
  236. {
  237. if(!isDirectory(%toFolder))
  238. %this.createFolder(%toFolder);
  239. %file = findFirstFileMultiExpr( %fromFolder @ "/*.*", false);
  240. while( %file !$= "" )
  241. {
  242. %copiedFile = strreplace(%file, %fromFolder, %toFolder);
  243. %copiedPath = filePath(%copiedFile);
  244. if(!isDirectory(%copiedPath))
  245. createPath(%copiedPath);
  246. %success = pathCopy(%file, %copiedFile, false);
  247. if(!%success)
  248. error("copyProjectFolder() - failed to copy file: " @ %file);
  249. %file = findNextFileMultiExpr( %fromFolder @ "/*.*" );
  250. }
  251. //do sub directories
  252. %paths = getDirectoryList(%fromFolder);
  253. for(%i=0; %i < getFieldCount(%paths); %i++)
  254. {
  255. %childPath = getField(%paths, %i);
  256. %this.copyFolder(%fromFolder @ %childPath @ "/", %toFolder @ %childPath @ "/");
  257. }
  258. return true;
  259. }