Package.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifdef PUAP_NAMESPACE_CHANGE
  23. #include "platform/platform.h"
  24. #include "console/console.h"
  25. #include "console/ast.h"
  26. #include "core/tAlgorithm.h"
  27. #include "core/resManager.h"
  28. #include "core/findMatch.h"
  29. #include "console/consoleInternal.h"
  30. #include "core/fileStream.h"
  31. #include "console/compiler.h"
  32. #include "Package.h"
  33. #include "Namespace.h"
  34. #include "tHashTable.h"
  35. //-Mat contains ALL packages
  36. Package *Package::smRootPackage = NULL;
  37. //-Mat the main package for all non-packaged namespaces
  38. Package *Package::smMainPackage = NULL;
  39. Vector<Package *> gActivePackages;
  40. Vector<Package *> gOldActivePackages;
  41. Package::Package() {
  42. mName = NULL;
  43. mActivePosition = -1;
  44. mChildNamespaceList = new tHashTable< StringTableEntry, Namespace* >;
  45. mChildPackageList = new tHashTable< StringTableEntry, Package* >;
  46. Namespace *ret = findAndCreateNamespace( DEFAULT_PACKAGE_NAMESPACE_NAME );//make our default namespace
  47. mDefaultNamespace = ret;
  48. mParent = NULL;
  49. }
  50. Package::~Package() {
  51. }
  52. void Package::init()
  53. {
  54. // create the root package
  55. smRootPackage = new Package;
  56. //constructInPlace( smRootPackage );
  57. smRootPackage->mName = StringTable->insert( "RootPackage" );
  58. //create main package
  59. smMainPackage = new Package;
  60. //constructInPlace( smMainPackage );
  61. smMainPackage->mName = StringTable->insert( DEFAULT_PACKAGE_NAME );
  62. //add to Main Package Root Package
  63. smRootPackage->addPackage( smMainPackage );
  64. //the main namespace will be the default namespace for the main Pacakge
  65. //this should always be the last active namespace
  66. activatePackage( smMainPackage->mName );
  67. }
  68. void Package::shutdown()
  69. {
  70. }
  71. Package *Package::findPackage(StringTableEntry name) {
  72. if( name == NULL ) {
  73. //we cannot find this package so use the current one
  74. return Package::getCurrentPackage();
  75. }
  76. Package *currentPackage = smRootPackage->mChildPackageList->find( name ).getValue();
  77. return currentPackage;
  78. }
  79. Namespace *Package::findNamespace(StringTableEntry name) {
  80. Namespace *currentNamespace = mChildNamespaceList->find( name ).getValue();
  81. return currentNamespace;
  82. }
  83. Namespace *Package::findAndCreateNamespace(StringTableEntry name) {
  84. Namespace *ret = findNamespace( name );
  85. if( ret == NULL ) {
  86. ret = addAndCreateNamespace( name );
  87. }
  88. return ret;
  89. }
  90. Package *Package::addPackage( Package *ns ) {
  91. Package *existing = findPackage( ns->mName );
  92. if( existing != NULL ) {
  93. Con::warnf( "Package %s has already been added to the Root Package List", ns->mName );
  94. return ns;
  95. }
  96. smRootPackage->mChildPackageList->insertUnique( ns->mName, ns );
  97. ns->mParent = smRootPackage;
  98. return ns;
  99. }
  100. Namespace *Package::addAndCreateNamespace( StringTableEntry name ) {
  101. Namespace *ret = NULL;
  102. if( name != NULL ) {
  103. ret = findNamespace( name );
  104. }
  105. if( ret == NULL ) {
  106. ret = new Namespace( this, StringTable->insert( name ) );//add namespace to us
  107. }
  108. return ret;
  109. }
  110. Namespace *Package::addNamespace( Namespace *ns ) {
  111. if( ns->mOwner ) {
  112. if( ns->mOwner == this ) {
  113. return ns;//we alread own this Namespace
  114. }
  115. return swapNamespace( ns );//if it has a parent then swap with that parent
  116. }
  117. mChildNamespaceList->insertUnique( ns->mName, ns );
  118. ns->mOwner = this;
  119. return ns;
  120. }
  121. //Private, no one else needs to know about this,
  122. //otherwise, it may be use innapropriately (Namespaces with NULL mparents)
  123. Namespace *Package::removeNamespace( Namespace *ns ) {
  124. //check if it's in the list first
  125. mChildNamespaceList->erase( ns->mName );
  126. ns->mOwner = NULL;
  127. return ns;
  128. }
  129. Namespace *Package::swapNamespace( Namespace *ns ) {
  130. //Namespaces must ALWAYS belong to some Package, removing them just puts them in smMainPackage
  131. //so if that is "this", then do nothing
  132. if( this == smMainPackage ) {
  133. return ns;
  134. }
  135. //check if it's in the list first
  136. if( ns->mOwner == this ) {
  137. return ns;
  138. }
  139. ns->mOwner->removeNamespace( ns );
  140. //add it to the global instead, since we're not deleting it
  141. smMainPackage->addNamespace( ns );
  142. return ns;
  143. }
  144. Package *Package::findAndCreatePackage(StringTableEntry packageName ) {
  145. //Iterate through all our package to find this, if not, make a new one
  146. Package *package = findPackage( packageName );
  147. if( package == NULL ) {
  148. package = new Package;
  149. //constructInPlace(package);
  150. package->mName = packageName;
  151. smRootPackage->addPackage( package );
  152. }
  153. return package;
  154. }
  155. Namespace *Package::lookupNamespaceInActivePackages( StringTableEntry nsName ) {
  156. nsName = StringTable->insert( nsName );
  157. Package *currentPackage = NULL;
  158. Namespace *currentNS = NULL;
  159. S32 size = gActivePackages.size();
  160. for( S32 i = size; i > 0 ; i-- ) {
  161. currentPackage = gActivePackages[i-1];//-1 because size counts from 1
  162. currentNS = currentPackage->mChildNamespaceList->find( nsName ).getValue();
  163. if( currentNS ) {
  164. return currentNS;
  165. }
  166. }
  167. return NULL;
  168. }
  169. /*
  170. Namespace::Entry *Package::lookupEntryInActivePackages( StringTableEntry entryName, StringTableEntry nsName ) {
  171. entryName = StringTable->insert( entryName );
  172. Package *currentPackage = NULL;
  173. Namespace *packageNS = NULL;
  174. Namespace::Entry *ret = NULL;
  175. //if we're here, we've already checked our active package so only check the ones below us
  176. S32 size = mActivePosition-1;
  177. for( S32 i = size; i >= 0 ; i-- ) {
  178. currentPackage = gActivePackages[i];
  179. packageNS = currentPackage->findNamespace( nsName );
  180. if( packageNS ) {
  181. ret = packageNS->lookupNoRecurse( entryName );
  182. }
  183. //-Mat make sure the Namespace still matches
  184. if( ret != NULL ) {
  185. return ret;
  186. }
  187. }
  188. return NULL;
  189. }
  190. */
  191. Package *Package::getCurrentPackage() {
  192. //first, check if there are any activate, if so, return the most recently ectivated one
  193. U32 size = gActivePackages.size();
  194. if( size > 0 ) {
  195. Package *ret = gActivePackages[size-1];
  196. return ret;
  197. }
  198. //if there is no current Package, use the main Package
  199. return smMainPackage;
  200. }
  201. /*
  202. -Mat here we'll just iterate through the global package's list of packages and check each package's
  203. mActive == true
  204. */
  205. void Package::activatePackage(StringTableEntry name)
  206. {
  207. if(!name)
  208. return;
  209. name = StringTable->insert( name );
  210. S32 size = gActivePackages.size();
  211. Package *targetPackage = smRootPackage->findPackage( name );
  212. if( targetPackage ) {
  213. //see if it's already active
  214. for( S32 i = size; i > 0 ; i-- ) {
  215. Package *currentPackage = gActivePackages[i-1];//-1 because size counts from 1
  216. if( currentPackage->mName == name ) {
  217. if( currentPackage->isActive() ) {
  218. Con::warnf( "Package %s already active", currentPackage->mName );
  219. //-Mat may want to remove this from the list and add it to the bottom
  220. return;
  221. }
  222. }
  223. }
  224. //activate it and add it to the list size is array size so set it first to save the -1
  225. targetPackage->activate(gActivePackages.size());
  226. gActivePackages.push_back( targetPackage );
  227. return;
  228. } else {
  229. Con::errorf( "Package %s does not exist", name );
  230. }
  231. }
  232. void Package::deactivatePackage(StringTableEntry name)
  233. {
  234. if(!name)
  235. return;
  236. name = StringTable->insert( name );
  237. // see if this one's already active
  238. U32 packageNumber = -1;
  239. U32 size = gActivePackages.size();
  240. for( S32 i = 0; i < size; i++ ) {
  241. if( gActivePackages[i]->mName == name ) {
  242. packageNumber = i;
  243. gActivePackages[i]->deActivate();
  244. gActivePackages.erase( i );
  245. break;
  246. }
  247. }
  248. if( packageNumber >= 0 ) {
  249. size = gActivePackages.size();
  250. for( S32 i = packageNumber; i <size; i++ ) {
  251. gActivePackages[i]->mActivePosition--;//to account for the one we just deleted
  252. }
  253. } else {
  254. Con::errorf( "Package %s does not exist", name );
  255. }
  256. }
  257. void Package::unlinkPackages()
  258. {
  259. #if 0
  260. //-Mat warning, not using these two properly can screw up packages, make sure to relink these
  261. Package *currentPackage = NULL;
  262. //Deactivate all active packages
  263. gOldActivePackages = gActivePackages;
  264. gActivePackages.clear();
  265. //this one is always active
  266. activatePackage( smMainPackage->mName );
  267. #endif
  268. }
  269. void Package::relinkPackages()
  270. {
  271. #if 0
  272. gActivePackages = gOldActivePackages;
  273. gOldActivePackages.clear();
  274. #endif
  275. }
  276. ConsoleFunctionGroupBegin( Packages, "Functions relating to the control of packages.");
  277. ConsoleFunction(isPackage,bool,2,2,"( packageName ) Use the isPackage function to check if the name or ID specified in packageName is a valid package.\n"
  278. "@param packagename The name or ID of an existing package.\n"
  279. "@return Returns true if packageName is a valid package, false otherwise.\n"
  280. "@sa activatePackage, deactivatePackage")
  281. {
  282. StringTableEntry packageName = StringTable->insert(argv[1]);
  283. //-Mat do a lookup of the package name on the global package
  284. if( Package::findPackage( packageName ) != NULL ) {
  285. return true;//such a package exists
  286. }
  287. return false;
  288. }
  289. ConsoleFunction(activatePackage, void,2,2,"( packageName ) Use the activatePackage function to activate a package definition and to re-define all functions named within this package with the definitions provided in the package body.\n"
  290. "This pushes the newly activated package onto the top of the package stack.\n"
  291. "@param packagename The name or ID of an existing package.\n"
  292. "@return No return value.\n"
  293. "@sa deactivatePackage, isPackage")
  294. {
  295. StringTableEntry packageName = StringTable->insert(argv[1]);
  296. Package::activatePackage(packageName);
  297. }
  298. ConsoleFunction(deactivatePackage, void,2,2,"( packageName ) Use the deactivatePackage function to deactivate a package definition and to pop any definitions from this package off the package stack.\n"
  299. "This also causes any subsequently stacked packages to be popped. i.e. If any packages were activated after the one specified in packageName, they too will be deactivated and popped.\n"
  300. "@param packagename The name or ID of an existing package.\n"
  301. "@return No return value.\n"
  302. "@sa activatePackage, isPackage")
  303. {
  304. StringTableEntry packageName = StringTable->insert(argv[1]);
  305. Package::deactivatePackage(packageName);
  306. }
  307. ConsoleFunctionGroupEnd( Packages );
  308. #else
  309. //normal TGB
  310. #endif //PUAP_NAMESPACE_CHANGE