scriptFilename.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/engineAPI.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, 1024);
  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, size);
  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 if (dStrncmp(src, "^", 1) == 0)
  198. {
  199. Platform::makeFullPathName(src + 1, filename, size);
  200. return true;
  201. }
  202. else
  203. {
  204. // otherwise path must be fully specified
  205. if (dStrlen(src) > size)
  206. {
  207. Con::errorf("Buffer overflow attempting to expand filename: %s", src);
  208. *filename = 0;
  209. return false;
  210. }
  211. dStrcpy(filename, src, size);
  212. return true;
  213. }
  214. if (slash == NULL)
  215. {
  216. Con::errorf("Illegal CodeBlock path detected (no mod directory): %s", cbName);
  217. *filename = 0;
  218. return false;
  219. }
  220. U32 length = slash-cbName;
  221. if ((length+dStrlen(src)) > size)
  222. {
  223. Con::errorf("Buffer overflow attempting to expand filename: %s", src);
  224. *filename = 0;
  225. return false;
  226. }
  227. dStrncpy(filename, cbName, length);
  228. dStrcpy(filename+length, src+1, size - length);
  229. return true;
  230. }
  231. //-----------------------------------------------------------------------------
  232. bool expandScriptFilename(char *filename, U32 size, const char *src)
  233. {
  234. #ifndef TORQUE2D_TOOLS_FIXME
  235. return expandOldScriptFilename(filename, size, src);
  236. #else
  237. return expandToolScriptFilename(filename, size, src);
  238. #endif
  239. }
  240. //-----------------------------------------------------------------------------
  241. static StringTableEntry tryGetBasePath(const char *path, const char *base)
  242. {
  243. U32 len = dStrlen(base);
  244. if(dStrnicmp(path, base, len) == 0)
  245. {
  246. if(*(path + len) == '/') --len;
  247. return StringTable->insertn(path, len, true);
  248. }
  249. return NULL;
  250. }
  251. struct CollapseTest
  252. {
  253. const char *path;
  254. const char *replace;
  255. };
  256. bool collapseScriptFilename(char *filename, U32 size, const char *src)
  257. {
  258. PathExpando *tools = sgPathExpandos.retreive("tools");
  259. PathExpando *game = sgPathExpandos.retreive("game");
  260. CollapseTest test[]=
  261. {
  262. { game ? game->mPath : NULL, "~" },
  263. { tools ? tools->mPath : NULL, "^tools" },
  264. { Platform::getCurrentDirectory(), "" },
  265. { Platform::getMainDotCsDir(), "" },
  266. { NULL, NULL }
  267. };
  268. for(S32 i = 0;!(test[i].path == NULL && test[i].replace == NULL);++i)
  269. {
  270. if(test[i].path == NULL) continue;
  271. StringTableEntry base = tryGetBasePath(src, test[i].path);
  272. if(base == NULL)
  273. continue;
  274. StringTableEntry rel = Platform::makeRelativePathName(src, test[i].path);
  275. *filename = 0;
  276. if(*test[i].replace)
  277. dSprintf(filename, size, "%s/", test[i].replace);
  278. dStrcat(filename, rel, size);
  279. return true;
  280. }
  281. dStrncpy(filename, src, size - 1);
  282. filename[size-1] = 0;
  283. return true;
  284. }
  285. //-----------------------------------------------------------------------------
  286. } // end namespace Con
  287. //-----------------------------------------------------------------------------
  288. // Console Functions
  289. //-----------------------------------------------------------------------------
  290. DefineEngineFunction(expandFilename, const char*, (const char* filename),,
  291. "@brief Grabs the full path of a specified file\n\n"
  292. "@param filename Name of the local file to locate\n"
  293. "@return String containing the full filepath on disk\n"
  294. "@ingroup FileSystem")
  295. {
  296. static const U32 bufSize = 1024;
  297. char* ret = Con::getReturnBuffer(bufSize);
  298. Con::expandScriptFilename(ret, bufSize, filename);
  299. return ret;
  300. }
  301. DefineEngineFunction(expandOldFilename, const char*, (const char* filename),,
  302. "@brief Retrofits a filepath that uses old Torque style\n\n"
  303. "@return String containing filepath with new formatting\n"
  304. "@ingroup FileSystem")
  305. {
  306. static const U32 bufSize = 1024;
  307. char* ret = Con::getReturnBuffer(bufSize);
  308. Con::expandOldScriptFilename(ret, bufSize, filename);
  309. return ret;
  310. }
  311. //-----------------------------------------------------------------------------
  312. // Tool Functions
  313. //-----------------------------------------------------------------------------
  314. ConsoleToolFunction(collapseFilename, const char*, 2, 2, "(string filename)"
  315. "@internal Editor use only")
  316. {
  317. TORQUE_UNUSED(argc);
  318. static const U32 bufSize = 1024;
  319. char* ret = Con::getReturnBuffer( bufSize );
  320. Con::collapseScriptFilename(ret, bufSize, argv[1]);
  321. return ret;
  322. }
  323. ConsoleToolFunction(setScriptPathExpando, void, 3, 4, "(string expando, string path[, bool toolsOnly])"
  324. "@internal Editor use only")
  325. {
  326. if(argc == 4)
  327. Con::setScriptPathExpando(argv[1], argv[2], dAtob(argv[3]));
  328. else
  329. Con::setScriptPathExpando(argv[1], argv[2]);
  330. }
  331. ConsoleToolFunction(removeScriptPathExpando, void, 2, 2, "(string expando)"
  332. "@internal Editor use only")
  333. {
  334. Con::removeScriptPathExpando(argv[1]);
  335. }
  336. ConsoleToolFunction(isScriptPathExpando, bool, 2, 2, "(string expando)"
  337. "@internal Editor use only")
  338. {
  339. return Con::isScriptPathExpando(argv[1]);
  340. }