scriptFilename.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #include "platform/platform.h"
  23. #include "console/scriptFilename.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/tSimpleHashTable.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "core/stringTable.h"
  28. #include "console/console.h"
  29. #include "console/compiler.h"
  30. namespace Con
  31. {
  32. //-----------------------------------------------------------------------------
  33. // Local Globals
  34. //-----------------------------------------------------------------------------
  35. struct PathExpando
  36. {
  37. StringTableEntry mPath;
  38. bool mIsToolsOnly;
  39. };
  40. static SimpleHashTable<PathExpando> sgPathExpandos(64, false);
  41. //-----------------------------------------------------------------------------
  42. // Global Functions
  43. //-----------------------------------------------------------------------------
  44. void setScriptPathExpando(const char *expando, const char *path, bool toolsOnly /*= false*/)
  45. {
  46. PathExpando *exp = sgPathExpandos.retreive(expando);
  47. if(exp)
  48. {
  49. exp->mPath = StringTable->insert(path);
  50. exp->mIsToolsOnly = toolsOnly;
  51. }
  52. else
  53. {
  54. exp = new PathExpando;
  55. exp->mPath = StringTable->insert(path);
  56. exp->mIsToolsOnly = toolsOnly;
  57. sgPathExpandos.insert(exp, expando);
  58. }
  59. }
  60. void removeScriptPathExpando(const char *expando)
  61. {
  62. PathExpando *exp = sgPathExpandos.remove(expando);
  63. if(exp)
  64. delete exp;
  65. }
  66. bool isScriptPathExpando(const char *expando)
  67. {
  68. PathExpando *exp = sgPathExpandos.retreive(expando);
  69. return ( exp != NULL);
  70. }
  71. //-----------------------------------------------------------------------------
  72. // [tom, 5/18/2006] FIXME: This needs some bounds checking
  73. bool expandToolScriptFilename(char *filename, U32 size, const char *src)
  74. {
  75. // [tom, 10/16/2006] Note: I am purposefully not early-outing here in the
  76. // same way the old code did as it is now possible that something could
  77. // be expanded if the name or mod is NULL. This was previously not the case.
  78. const StringTableEntry cbMod = CodeBlock::getCurrentCodeBlockModName();
  79. const StringTableEntry cbFullPath = CodeBlock::getCurrentCodeBlockFullPath();
  80. char varBuf[1024], modBuf[1024];
  81. const char *ptr = src;
  82. char *retPtr = filename;
  83. char *slash;
  84. const char *catPath = NULL;
  85. #ifndef TORQUE_DEBUG
  86. bool isTools = Con::isCurrentScriptToolScript();
  87. #endif
  88. // Check leading character
  89. switch(*ptr)
  90. {
  91. case '^':
  92. {
  93. // Variable
  94. const char *varPtr = ptr+1;
  95. char *insertPtr = varBuf;
  96. bool valid = true;
  97. while(*varPtr != '/')
  98. {
  99. if(*varPtr == 0)
  100. {
  101. valid = false;
  102. break;
  103. }
  104. *insertPtr++ = *varPtr++;
  105. }
  106. if(valid)
  107. {
  108. // Got a valid variable
  109. *insertPtr = 0;
  110. PathExpando *exp = sgPathExpandos.retreive(varBuf);
  111. if(exp == NULL)
  112. {
  113. Con::errorf("expandScriptFilename - Ignoring invalid path expando \"%s\"", varBuf);
  114. break;
  115. }
  116. #ifndef TORQUE_DEBUG
  117. // [tom, 12/13/2006] This stops tools expandos from working in the console,
  118. // which is useful behavior when debugging so I'm ifdefing this out for debug builds.
  119. if(! isTools && exp->mIsToolsOnly)
  120. {
  121. Con::errorf("expandScriptFilename - attempting to use tools only expando \"%s\" from outside of tools", varBuf);
  122. *filename = 0;
  123. return false;
  124. }
  125. #endif
  126. catPath = exp ? exp->mPath : "";
  127. // swallow the expando and the slash after the expando
  128. ptr += dStrlen(varBuf) + 1;
  129. if(*ptr == '/')
  130. ptr++;
  131. }
  132. }
  133. break;
  134. case '~':
  135. // Relative to mod
  136. if(cbMod && cbFullPath)
  137. {
  138. Platform::makeFullPathName(cbMod, modBuf, sizeof(modBuf));
  139. catPath = modBuf;
  140. }
  141. else
  142. {
  143. // Probably not a mod, so we'll use the ^game expando
  144. PathExpando *exp = sgPathExpandos.retreive("game");
  145. if(exp == NULL)
  146. {
  147. Con::errorf("expandScriptFilename - ~ expansion failed for mod and ^game when processing '%s'", src);
  148. break;
  149. }
  150. catPath = exp ? exp->mPath : "";
  151. }
  152. // swallow ~ and optional slash
  153. switch(ptr[1])
  154. {
  155. case '/': ptr += 2; break;
  156. default: ptr++;
  157. }
  158. break;
  159. case '.':
  160. // Relative to script directory
  161. if(cbFullPath)
  162. {
  163. dStrcpy(varBuf, cbFullPath);
  164. slash = dStrrchr(varBuf, '/');
  165. if(slash) *slash = 0;
  166. catPath = varBuf;
  167. // swallow dot and optional slash, but dont swallow .. relative path token
  168. switch(ptr[1])
  169. {
  170. case '.': break;
  171. case '/': ptr += 2; break;
  172. default: ptr++;
  173. }
  174. }
  175. break;
  176. }
  177. // [tom, 11/20/2006] Handing off to makeFullPathName() allows us to process .. correctly.
  178. Platform::makeFullPathName(ptr, retPtr, size, catPath);
  179. return true;
  180. }
  181. //-----------------------------------------------------------------------------
  182. bool expandOldScriptFilename(char *filename, U32 size, const char *src)
  183. {
  184. const StringTableEntry cbName = CodeBlock::getCurrentCodeBlockName();
  185. if (!cbName)
  186. {
  187. dStrcpy(filename, src);
  188. return true;
  189. }
  190. const char *slash;
  191. if (dStrncmp(src, "~/", 2) == 0)
  192. // tilde path means load from current codeblock/mod root
  193. slash = dStrchr(cbName, '/');
  194. else if (dStrncmp(src, "./", 2) == 0)
  195. // dot path means load from current codeblock/mod path
  196. slash = dStrrchr(cbName, '/');
  197. else
  198. {
  199. // otherwise path must be fully specified
  200. if (dStrlen(src) > size)
  201. {
  202. Con::errorf("Buffer overflow attempting to expand filename: %s", src);
  203. *filename = 0;
  204. return false;
  205. }
  206. dStrcpy(filename, src);
  207. return true;
  208. }
  209. if (slash == NULL)
  210. {
  211. Con::errorf("Illegal CodeBlock path detected (no mod directory): %s", cbName);
  212. *filename = 0;
  213. return false;
  214. }
  215. U32 length = slash-cbName;
  216. if ((length+dStrlen(src)) > size)
  217. {
  218. Con::errorf("Buffer overflow attempting to expand filename: %s", src);
  219. *filename = 0;
  220. return false;
  221. }
  222. dStrncpy(filename, cbName, length);
  223. dStrcpy(filename+length, src+1);
  224. return true;
  225. }
  226. //-----------------------------------------------------------------------------
  227. bool expandScriptFilename(char *filename, U32 size, const char *src)
  228. {
  229. #ifndef TORQUE2D_TOOLS_FIXME
  230. return expandOldScriptFilename(filename, size, src);
  231. #else
  232. return expandToolScriptFilename(filename, size, src);
  233. #endif
  234. }
  235. //-----------------------------------------------------------------------------
  236. static StringTableEntry tryGetBasePath(const char *path, const char *base)
  237. {
  238. U32 len = dStrlen(base);
  239. if(dStrnicmp(path, base, len) == 0)
  240. {
  241. if(*(path + len) == '/') --len;
  242. return StringTable->insertn(path, len, true);
  243. }
  244. return NULL;
  245. }
  246. struct CollapseTest
  247. {
  248. const char *path;
  249. const char *replace;
  250. };
  251. bool collapseScriptFilename(char *filename, U32 size, const char *src)
  252. {
  253. PathExpando *tools = sgPathExpandos.retreive("tools");
  254. PathExpando *game = sgPathExpandos.retreive("game");
  255. CollapseTest test[]=
  256. {
  257. { game ? game->mPath : NULL, "~" },
  258. { tools ? tools->mPath : NULL, "^tools" },
  259. { Platform::getCurrentDirectory(), "" },
  260. { Platform::getMainDotCsDir(), "" },
  261. { NULL, NULL }
  262. };
  263. for(S32 i = 0;!(test[i].path == NULL && test[i].replace == NULL);++i)
  264. {
  265. if(test[i].path == NULL) continue;
  266. StringTableEntry base = tryGetBasePath(src, test[i].path);
  267. if(base == NULL)
  268. continue;
  269. StringTableEntry rel = Platform::makeRelativePathName(src, test[i].path);
  270. *filename = 0;
  271. if(*test[i].replace)
  272. dSprintf(filename, size, "%s/", test[i].replace);
  273. dStrcat(filename, rel);
  274. return true;
  275. }
  276. dStrncpy(filename, src, size - 1);
  277. filename[size-1] = 0;
  278. return true;
  279. }
  280. //-----------------------------------------------------------------------------
  281. } // end namespace Con
  282. //-----------------------------------------------------------------------------
  283. // Console Functions
  284. //-----------------------------------------------------------------------------
  285. ConsoleFunction(expandFilename, const char*, 2, 2, "(string filename)"
  286. "@brief Grabs the full path of a specified file\n\n"
  287. "@param filename Name of the local file to locate\n"
  288. "@return String containing the full filepath on disk\n"
  289. "@ingroup FileSystem")
  290. {
  291. TORQUE_UNUSED(argc);
  292. static const U32 bufSize = 1024;
  293. char* ret = Con::getReturnBuffer( bufSize );
  294. Con::expandScriptFilename(ret, bufSize, argv[1]);
  295. return ret;
  296. }
  297. ConsoleFunction(expandOldFilename, const char*, 2, 2, "(string filename)"
  298. "@brief Retrofits a filepath that uses old Torque style\n\n"
  299. "@return String containing filepath with new formatting\n"
  300. "@ingroup FileSystem")
  301. {
  302. TORQUE_UNUSED(argc);
  303. static const U32 bufSize = 1024;
  304. char* ret = Con::getReturnBuffer( bufSize );
  305. Con::expandOldScriptFilename(ret, bufSize, argv[1]);
  306. return ret;
  307. }
  308. //-----------------------------------------------------------------------------
  309. // Tool Functions
  310. //-----------------------------------------------------------------------------
  311. ConsoleToolFunction(collapseFilename, const char*, 2, 2, "(string filename)"
  312. "@internal Editor use only")
  313. {
  314. TORQUE_UNUSED(argc);
  315. static const U32 bufSize = 1024;
  316. char* ret = Con::getReturnBuffer( bufSize );
  317. Con::collapseScriptFilename(ret, bufSize, argv[1]);
  318. return ret;
  319. }
  320. ConsoleToolFunction(setScriptPathExpando, void, 3, 4, "(string expando, string path[, bool toolsOnly])"
  321. "@internal Editor use only")
  322. {
  323. if(argc == 4)
  324. Con::setScriptPathExpando(argv[1], argv[2], dAtob(argv[3]));
  325. else
  326. Con::setScriptPathExpando(argv[1], argv[2]);
  327. }
  328. ConsoleToolFunction(removeScriptPathExpando, void, 2, 2, "(string expando)"
  329. "@internal Editor use only")
  330. {
  331. Con::removeScriptPathExpando(argv[1]);
  332. }
  333. ConsoleToolFunction(isScriptPathExpando, bool, 2, 2, "(string expando)"
  334. "@internal Editor use only")
  335. {
  336. return Con::isScriptPathExpando(argv[1]);
  337. }