simObject.cpp 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "platform/platformMemory.h"
  28. #include "console/simObject.h"
  29. #include "console/console.h"
  30. #include "console/consoleInternal.h"
  31. #include "console/engineAPI.h"
  32. #include "console/simFieldDictionary.h"
  33. #include "console/simPersistID.h"
  34. #include "console/typeValidators.h"
  35. #include "console/arrayObject.h"
  36. #include "console/codeBlock.h"
  37. #include "core/frameAllocator.h"
  38. #include "core/stream/fileStream.h"
  39. #include "core/fileObject.h"
  40. #include "persistence/taml/tamlCustom.h"
  41. #include "sim/netObject.h"
  42. #include "cinterface/cinterface.h"
  43. IMPLEMENT_CONOBJECT( SimObject );
  44. // See full description in the new CHM manual
  45. ConsoleDocClass( SimObject,
  46. "@brief Base class for almost all objects involved in the simulation.\n\n"
  47. "@ingroup Console\n"
  48. );
  49. bool SimObject::smForceId = false;
  50. SimObjectId SimObject::smForcedId = 0;
  51. bool SimObject::preventNameChanging = false;
  52. IMPLEMENT_CALLBACK(SimObject, onInspectPostApply, void, (SimObject* obj), (obj), "Generic callback for when an object is edited");
  53. namespace Sim
  54. {
  55. // Defined in simManager.cpp
  56. extern SimGroup *gRootGroup;
  57. extern SimManagerNameDictionary *gNameDictionary;
  58. extern SimIdDictionary *gIdDictionary;
  59. extern U32 gNextObjectId;
  60. }
  61. //-----------------------------------------------------------------------------
  62. SimObject::SimObject()
  63. {
  64. mObjectName = NULL;
  65. mOriginalName = NULL;
  66. mInternalName = NULL;
  67. mInheritFrom = NULL;
  68. nextNameObject = nullptr;
  69. nextManagerNameObject = nullptr;
  70. nextIdObject = NULL;
  71. mFilename = NULL;
  72. mDeclarationLine = -1;
  73. mId = 0;
  74. mIdString[ 0 ] = '\0';
  75. mGroup = 0;
  76. mNameSpace = NULL;
  77. mNotifyList = NULL;
  78. mFlags.set( ModStaticFields | ModDynamicFields );
  79. mProgenitorFile = StringTable->EmptyString();
  80. mFieldDictionary = NULL;
  81. mCanSaveFieldDictionary = true;
  82. mClassName = NULL;
  83. mSuperClassName = NULL;
  84. mCopySource = NULL;
  85. mPersistentId = NULL;
  86. is_temp_clone = false;
  87. }
  88. //-----------------------------------------------------------------------------
  89. SimObject::~SimObject()
  90. {
  91. // if this is a temp-clone, we don't delete any members that were shallow-copied
  92. // over from the source datablock.
  93. if (is_temp_clone)
  94. return;
  95. if( mFieldDictionary )
  96. {
  97. delete mFieldDictionary;
  98. mFieldDictionary = NULL;
  99. }
  100. // Release persistent ID.
  101. if( mPersistentId )
  102. {
  103. mPersistentId->unresolve();
  104. mPersistentId->decRefCount();
  105. mPersistentId = NULL;
  106. }
  107. if( mCopySource )
  108. mCopySource->unregisterReference( &mCopySource );
  109. AssertFatal(nextNameObject == nullptr,avar(
  110. "SimObject::~SimObject: Not removed from dictionary: name %s, id %i",
  111. mObjectName, mId));
  112. AssertFatal(nextManagerNameObject == nullptr,avar(
  113. "SimObject::~SimObject: Not removed from manager dictionary: name %s, id %i",
  114. mObjectName,mId));
  115. AssertFatal(mFlags.test(Added) == 0, "SimObject::object "
  116. "missing call to SimObject::onRemove");
  117. }
  118. //-----------------------------------------------------------------------------
  119. bool SimObject::processArguments(S32 argc, ConsoleValue *argv)
  120. {
  121. return argc == 0;
  122. }
  123. //-----------------------------------------------------------------------------
  124. void SimObject::initPersistFields()
  125. {
  126. addGroup( "Ungrouped" );
  127. addProtectedField( "name", TypeName, Offset(mObjectName, SimObject), &setProtectedName, &defaultProtectedGetFn,
  128. "Optional global name of this object." );
  129. addProtectedField("inheritFrom", TypeString, Offset(mInheritFrom, SimObject), &setInheritFrom, &defaultProtectedGetFn,
  130. "Optional Name of object to inherit from as a parent.");
  131. endGroup( "Ungrouped" );
  132. addGroup( "Object" );
  133. addField( "internalName", TypeString, Offset(mInternalName, SimObject),
  134. "Optional name that may be used to lookup this object within a SimSet.");
  135. addProtectedField( "parentGroup", TYPEID< SimObject >(), Offset(mGroup, SimObject), &setProtectedParent, &defaultProtectedGetFn,
  136. "Group hierarchy parent of the object." );
  137. addProtectedField( "class", TypeString, Offset(mClassName, SimObject), &setClass, &defaultProtectedGetFn,
  138. "Script class of object." );
  139. addProtectedField( "superClass", TypeString, Offset(mSuperClassName, SimObject), &setSuperClass, &defaultProtectedGetFn,
  140. "Script super-class of object." );
  141. // For legacy support
  142. addProtectedField( "className", TypeString, Offset(mClassName, SimObject), &setClass, &defaultProtectedGetFn,
  143. "Script class of object.", AbstractClassRep::FIELD_HideInInspectors );
  144. endGroup( "Object" );
  145. addGroup( "Editing" );
  146. addProtectedField( "hidden", TypeBool, 0,
  147. &_setHidden, &_getHidden,
  148. "Whether the object is visible." );
  149. addProtectedField( "locked", TypeBool, 0,
  150. &_setLocked, &_getLocked,
  151. "Whether the object can be edited." );
  152. endGroup( "Editing" );
  153. addGroup( "Persistence" );
  154. addProtectedField( "canSave", TypeBool, Offset( mFlags, SimObject ),
  155. &_setCanSave, &_getCanSave,
  156. "Whether the object can be saved out. If false, the object is purely transient in nature." );
  157. addField( "canSaveDynamicFields", TypeBool, Offset(mCanSaveFieldDictionary, SimObject),
  158. "True if dynamic fields (added at runtime) should be saved. Defaults to true." );
  159. addProtectedField( "persistentId", TypePID, Offset( mPersistentId, SimObject ),
  160. &_setPersistentID, &defaultProtectedGetFn,
  161. "The universally unique identifier for the object." );
  162. endGroup( "Persistence" );
  163. Parent::initPersistFields();
  164. }
  165. //-----------------------------------------------------------------------------
  166. String SimObject::describeSelf() const
  167. {
  168. String desc = Parent::describeSelf();
  169. if( mId != 0 )
  170. desc = avar( "%s|id: %i", desc.c_str(), mId );
  171. if(mObjectName)
  172. desc = avar( "%s|name: %s", desc.c_str(), mObjectName);
  173. if( mInternalName )
  174. desc = avar( "%s|internal: %s", desc.c_str(), mInternalName );
  175. if( mNameSpace )
  176. desc = avar( "%s|nspace: %s", desc.c_str(), mNameSpace->mName );
  177. if( mGroup )
  178. desc = avar( "%s|group: %s", desc.c_str(), mGroup->getName() );
  179. if( mCopySource )
  180. desc = avar( "%s|copy: %s", desc.c_str(), mCopySource->getName() );
  181. if( mPersistentId )
  182. desc = avar( "%s|pid: %s", desc.c_str(), mPersistentId->getUUID().toString().c_str() );
  183. return desc;
  184. }
  185. // Copies dynamic fields from one object to another, optionally limited by the settings for
  186. // <filter> and <no_replace>. When true, <no_replace> prohibits the replacement of fields that
  187. // already have a value. When <filter> is specified, only fields with leading characters that
  188. // exactly match the characters in <filter> are copied.
  189. void SimObject::assignDynamicFieldsFrom(SimObject* from, const char* filter, bool no_replace)
  190. {
  191. if (from->mFieldDictionary)
  192. {
  193. if( mFieldDictionary == NULL )
  194. mFieldDictionary = new SimFieldDictionary;
  195. mFieldDictionary->assignFrom(from->mFieldDictionary, filter, no_replace);
  196. }
  197. }
  198. //=============================================================================
  199. // Persistence.
  200. //=============================================================================
  201. // MARK: ---- Persistence ----
  202. //-----------------------------------------------------------------------------
  203. bool SimObject::writeField(StringTableEntry fieldname, const char* value)
  204. {
  205. // Don't write empty fields.
  206. if (!value || !*value)
  207. return false;
  208. // Don't write owner field for components
  209. static StringTableEntry sOwner = StringTable->insert( "owner" );
  210. if( fieldname == sOwner )
  211. return false;
  212. // Don't write ParentGroup
  213. static StringTableEntry sParentGroup = StringTable->insert( "parentGroup" );
  214. if( fieldname == sParentGroup )
  215. return false;
  216. // Don't write name, is within the parenthesis already
  217. static StringTableEntry sName = StringTable->insert( "name" );
  218. if( fieldname == sName )
  219. return false;
  220. // Don't write className, it is read for legacy support but we
  221. // write it out as class.
  222. static StringTableEntry sClassName = StringTable->insert( "className" );
  223. if( fieldname == sClassName )
  224. return false;
  225. // Write persistent ID only if present.
  226. static StringTableEntry sPersistentId = StringTable->insert( "persistentId" );
  227. if( fieldname == sPersistentId && ( !value || !value[ 0 ] ) )
  228. return false;
  229. // Don't write hidden and locked flags if they are at their default value.
  230. static StringTableEntry sHidden = StringTable->insert( "hidden" );
  231. static StringTableEntry sLocked = StringTable->insert( "locked" );
  232. if( fieldname == sHidden && !dAtob( value ) )
  233. return false;
  234. if( fieldname == sLocked && !dAtob( value ) )
  235. return false;
  236. return true;
  237. }
  238. //-----------------------------------------------------------------------------
  239. void SimObject::writeFields(Stream &stream, U32 tabStop)
  240. {
  241. // Write static fields.
  242. // Create a default object of the same type
  243. ConsoleObject* defaultConObject = ConsoleObject::create(getClassName());
  244. SimObject* defaultObject = dynamic_cast<SimObject*>(defaultConObject);
  245. const AbstractClassRep::FieldList &list = getFieldList();
  246. for(U32 i = 0; i < list.size(); i++)
  247. {
  248. const AbstractClassRep::Field* f = &list[i];
  249. // Skip the special field types.
  250. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  251. continue;
  252. for(U32 j = 0; S32(j) < f->elementCount; j++)
  253. {
  254. char array[8];
  255. dSprintf( array, 8, "%d", j );
  256. const char *val = getDataField(StringTable->insert( f->pFieldname ), array );
  257. // Make a copy for the field check.
  258. if (!val)
  259. continue;
  260. U32 nBufferSize = dStrlen( val ) + 1;
  261. FrameTemp<char> valCopy( nBufferSize );
  262. dStrcpy( (char *)valCopy, val, nBufferSize );
  263. if (!writeField(f->pFieldname, valCopy))
  264. continue;
  265. //If the field hasn't been changed from the default value, then don't bother writing it out
  266. const char* defaultValueCheck = defaultObject->getDataField(f->pFieldname, array);
  267. if (defaultValueCheck && defaultValueCheck[0] != '\0' && dStricmp(defaultValueCheck, valCopy) == 0)
  268. continue;
  269. val = valCopy;
  270. U32 expandedBufferSize = ( nBufferSize * 2 ) + dStrlen(f->pFieldname) + 32;
  271. FrameTemp<char> expandedBuffer( expandedBufferSize );
  272. if(f->elementCount == 1)
  273. dSprintf(expandedBuffer, expandedBufferSize, "%s = \"", f->pFieldname);
  274. else
  275. dSprintf(expandedBuffer, expandedBufferSize, "%s[%d] = \"", f->pFieldname, j);
  276. // detect and collapse relative path information
  277. char fnBuf[1024];
  278. if (f->type == TypeFilename ||
  279. f->type == TypeStringFilename ||
  280. f->type == TypeImageFilename ||
  281. f->type == TypePrefabFilename ||
  282. f->type == TypeShapeFilename ||
  283. f->type == TypeSoundFilename )
  284. {
  285. Con::collapseScriptFilename(fnBuf, 1024, val);
  286. val = fnBuf;
  287. }
  288. expandEscape((char*)expandedBuffer + dStrlen(expandedBuffer), val);
  289. dStrcat(expandedBuffer, "\";\r\n", expandedBufferSize);
  290. stream.writeTabs(tabStop);
  291. stream.write(dStrlen(expandedBuffer),expandedBuffer);
  292. }
  293. }
  294. // Write dynamic fields, if enabled.
  295. if(mFieldDictionary && mCanSaveFieldDictionary)
  296. mFieldDictionary->writeFields(this, stream, tabStop);
  297. // Cleanup our created default object
  298. delete defaultConObject;
  299. }
  300. //-----------------------------------------------------------------------------
  301. void SimObject::write(Stream &stream, U32 tabStop, U32 flags)
  302. {
  303. if( !getCanSave() && !( flags & IgnoreCanSave ) )
  304. return;
  305. // Only output selected objects if they want that.
  306. if((flags & SelectedOnly) && !isSelected())
  307. return;
  308. stream.writeTabs(tabStop);
  309. char buffer[1024];
  310. dSprintf(buffer, sizeof(buffer), "new %s(%s) {\r\n", getClassName(), getName() && !(flags & NoName) ? getName() : "");
  311. stream.write(dStrlen(buffer), buffer);
  312. writeFields(stream, tabStop + 1);
  313. stream.writeTabs(tabStop);
  314. stream.write(4, "};\r\n");
  315. }
  316. //-----------------------------------------------------------------------------
  317. bool SimObject::save(const char *pcFileName, bool bOnlySelected, const char *preappend)
  318. {
  319. static const char *beginMessage = "//--- OBJECT WRITE BEGIN ---";
  320. static const char *endMessage = "//--- OBJECT WRITE END ---";
  321. FileStream *stream;
  322. FileObject f;
  323. f.readMemory(pcFileName);
  324. // check for flags <selected, ...>
  325. U32 writeFlags = 0;
  326. if(bOnlySelected)
  327. writeFlags |= SimObject::SelectedOnly;
  328. if((stream = FileStream::createAndOpen( pcFileName, Torque::FS::File::Write )) == NULL)
  329. return false;
  330. char docRoot[256];
  331. char modRoot[256];
  332. dStrcpy(docRoot, pcFileName, 256);
  333. char *p = dStrrchr(docRoot, '/');
  334. if (p) *++p = '\0';
  335. else docRoot[0] = '\0';
  336. dStrcpy(modRoot, pcFileName, 256);
  337. p = dStrchr(modRoot, '/');
  338. if (p) *++p = '\0';
  339. else modRoot[0] = '\0';
  340. Con::setVariable("$DocRoot", docRoot);
  341. Con::setVariable("$ModRoot", modRoot);
  342. const char *buffer;
  343. while(!f.isEOF())
  344. {
  345. buffer = (const char *) f.readLine();
  346. if(!String::compare(buffer, beginMessage))
  347. break;
  348. stream->write(dStrlen(buffer), buffer);
  349. stream->write(2, "\r\n");
  350. }
  351. stream->write(dStrlen(beginMessage), beginMessage);
  352. stream->write(2, "\r\n");
  353. if ( preappend != NULL )
  354. stream->write(dStrlen(preappend),preappend);
  355. write(*stream, 0, writeFlags);
  356. stream->write(dStrlen(endMessage), endMessage);
  357. stream->write(2, "\r\n");
  358. while(!f.isEOF())
  359. {
  360. buffer = (const char *) f.readLine();
  361. if(!String::compare(buffer, endMessage))
  362. break;
  363. }
  364. while(!f.isEOF())
  365. {
  366. buffer = (const char *) f.readLine();
  367. stream->write(dStrlen(buffer), buffer);
  368. stream->write(2, "\r\n");
  369. }
  370. Con::setVariable("$DocRoot", NULL);
  371. Con::setVariable("$ModRoot", NULL);
  372. delete stream;
  373. return true;
  374. }
  375. //-----------------------------------------------------------------------------
  376. SimPersistID* SimObject::getOrCreatePersistentId()
  377. {
  378. if( !mPersistentId )
  379. {
  380. mPersistentId = SimPersistID::create( this );
  381. mPersistentId->incRefCount();
  382. }
  383. return mPersistentId;
  384. }
  385. void SimObject::onTamlCustomRead(TamlCustomNodes const& customNodes)
  386. {
  387. // Debug Profiling.
  388. //PROFILE_SCOPE(SimObject_OnTamlCustomRead);
  389. // Fetch field list.
  390. const AbstractClassRep::FieldList& fieldList = getFieldList();
  391. const U32 fieldCount = fieldList.size();
  392. for (U32 index = 0; index < fieldCount; ++index)
  393. {
  394. // Fetch field.
  395. const AbstractClassRep::Field* pField = &fieldList[index];
  396. // Ignore if field not appropriate.
  397. if (pField->type == AbstractClassRep::StartArrayFieldType || pField->elementCount > 1)
  398. {
  399. // Find cell custom node.
  400. const TamlCustomNode* pCustomCellNodes = NULL;
  401. if (pField->pGroupname != NULL)
  402. pCustomCellNodes = customNodes.findNode(pField->pGroupname);
  403. if (!pCustomCellNodes)
  404. {
  405. char* niceFieldName = const_cast<char *>(pField->pFieldname);
  406. niceFieldName[0] = dToupper(niceFieldName[0]);
  407. String str_niceFieldName = String(niceFieldName);
  408. pCustomCellNodes = customNodes.findNode(str_niceFieldName + "s");
  409. }
  410. // Continue if we have explicit cells.
  411. if (pCustomCellNodes != NULL)
  412. {
  413. // Fetch children cell nodes.
  414. const TamlCustomNodeVector& cellNodes = pCustomCellNodes->getChildren();
  415. U8 idx = 0;
  416. // Iterate cells.
  417. for (TamlCustomNodeVector::const_iterator cellNodeItr = cellNodes.begin(); cellNodeItr != cellNodes.end(); ++cellNodeItr)
  418. {
  419. char buf[5];
  420. dSprintf(buf, 5, "%d", idx);
  421. // Fetch cell node.
  422. TamlCustomNode* pCellNode = *cellNodeItr;
  423. // Fetch node name.
  424. StringTableEntry nodeName = pCellNode->getNodeName();
  425. // Is this a valid alias?
  426. if (nodeName != pField->pFieldname)
  427. {
  428. // No, so warn.
  429. Con::warnf("SimObject::onTamlCustomRead() - Encountered an unknown custom name of '%s'. Only '%s' is valid.", nodeName, pField->pFieldname);
  430. continue;
  431. }
  432. // Fetch fields.
  433. const TamlCustomFieldVector& fields = pCellNode->getFields();
  434. // Iterate property fields.
  435. for (TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr)
  436. {
  437. // Fetch field.
  438. const TamlCustomField* cField = *fieldItr;
  439. // Fetch field name.
  440. StringTableEntry fieldName = cField->getFieldName();
  441. const AbstractClassRep::Field* field = findField(fieldName);
  442. // Check common fields.
  443. if (field)
  444. {
  445. setDataField(fieldName, buf, cField->getFieldValue());
  446. }
  447. else
  448. {
  449. // Unknown name so warn.
  450. Con::warnf("SimObject::onTamlCustomRead() - Encountered an unknown custom field name of '%s'.", fieldName);
  451. continue;
  452. }
  453. }
  454. idx++;
  455. }
  456. }
  457. }
  458. }
  459. }
  460. //-----------------------------------------------------------------------------
  461. bool SimObject::_setPersistentID( void* object, const char* index, const char* data )
  462. {
  463. SimObject* simObject = reinterpret_cast< SimObject* >( object );
  464. // Make sure we don't already have a PID.
  465. if( simObject->getPersistentId() )
  466. {
  467. Con::errorf( "SimObject::_setPersistentID - cannot set a persistent ID on an object that already has a persistent ID assigned." );
  468. return false;
  469. }
  470. SimPersistID* pid;
  471. Con::setData( TypePID, &pid, 0, 1, &data );
  472. if ( !pid )
  473. return false;
  474. // Make sure it's not already bound to an object.
  475. if( pid->getObject() )
  476. {
  477. AssertWarn( pid->getObject() != simObject, "Sim::_setPersistentID - PID is bound to this object yet not assigned to it!" );
  478. SimObject* otherObj = pid->getObject();
  479. Con::errorf( "SimObject::_setPersistentID - UUID is already used by another object: '%s' -> %i:%s (%s)",
  480. data, otherObj->getId(), otherObj->getClassName(), otherObj->getName() );
  481. return false;
  482. }
  483. pid->resolve( simObject );
  484. pid->incRefCount();
  485. simObject->mPersistentId = pid;
  486. return false;
  487. }
  488. //-----------------------------------------------------------------------------
  489. void SimObject::setFilename( const char* file )
  490. {
  491. if( file )
  492. mFilename = StringTable->insert( file );
  493. else
  494. mFilename = StringTable->EmptyString();
  495. }
  496. //-----------------------------------------------------------------------------
  497. void SimObject::setDeclarationLine(U32 lineNumber)
  498. {
  499. mDeclarationLine = lineNumber;
  500. }
  501. //=============================================================================
  502. // Management.
  503. //=============================================================================
  504. // MARK: ---- Management ----
  505. //-----------------------------------------------------------------------------
  506. bool SimObject::registerObject()
  507. {
  508. AssertFatal( !mFlags.test( Added ), "reigsterObject - Object already registered!");
  509. mFlags.clear(Deleted | Removed);
  510. if(smForceId)
  511. {
  512. setId(smForcedId);
  513. smForceId = false;
  514. }
  515. if( !mId )
  516. {
  517. mId = Sim::gNextObjectId++;
  518. dSprintf( mIdString, sizeof( mIdString ), "%u", mId );
  519. }
  520. AssertFatal(Sim::gIdDictionary && Sim::gNameDictionary,
  521. "SimObject::registerObject - tried to register an object before Sim::init()!");
  522. Sim::gIdDictionary->insert(this);
  523. Sim::gNameDictionary->insert(this);
  524. // Notify object
  525. bool ret = onAdd();
  526. if(!ret)
  527. unregisterObject();
  528. AssertFatal(!ret || isProperlyAdded(), "Object did not call SimObject::onAdd()");
  529. return ret;
  530. }
  531. //-----------------------------------------------------------------------------
  532. void SimObject::unregisterObject()
  533. {
  534. mFlags.set(Removed);
  535. // Notify object first
  536. onRemove();
  537. // Clear out any pending notifications before
  538. // we call our own, just in case they delete
  539. // something that we have referenced.
  540. clearAllNotifications();
  541. // Notify all objects that are waiting for delete
  542. // messages
  543. if (getGroup())
  544. getGroup()->removeObject(this);
  545. processDeleteNotifies();
  546. // Do removals from the Sim.
  547. Sim::gNameDictionary->remove(this);
  548. Sim::gIdDictionary->remove(this);
  549. Sim::cancelPendingEvents(this);
  550. }
  551. //-----------------------------------------------------------------------------
  552. void SimObject::deleteObject()
  553. {
  554. Parent::destroySelf();
  555. }
  556. //-----------------------------------------------------------------------------
  557. void SimObject::_destroySelf()
  558. {
  559. AssertFatal( !isDeleted(), "SimObject::destroySelf - Object has already been deleted" );
  560. AssertFatal( !isRemoved(), "SimObject::destroySelf - Object in the process of being removed" );
  561. mFlags.set( Deleted );
  562. if( mFlags.test( Added ) )
  563. unregisterObject();
  564. Parent::_destroySelf();
  565. }
  566. //-----------------------------------------------------------------------------
  567. void SimObject::destroySelf()
  568. {
  569. // When using the legacy console interop, we don't delete objects
  570. // when their reference count drops to zero but rather defer their
  571. // deletion until deleteObject() is called.
  572. if( engineAPI::gUseConsoleInterop )
  573. return;
  574. Parent::destroySelf();
  575. }
  576. //-----------------------------------------------------------------------------
  577. class SimObjectDeleteEvent : public SimEvent
  578. {
  579. public:
  580. void process(SimObject *object)
  581. {
  582. object->deleteObject();
  583. }
  584. };
  585. void SimObject::safeDeleteObject()
  586. {
  587. Sim::postEvent( this, new SimObjectDeleteEvent, Sim::getCurrentTime() + 1 );
  588. }
  589. //-----------------------------------------------------------------------------
  590. void SimObject::setId(SimObjectId newId)
  591. {
  592. if(!mFlags.test(Added))
  593. mId = newId;
  594. else
  595. {
  596. // get this object out of the id dictionary if it's in it
  597. Sim::gIdDictionary->remove(this);
  598. // Free current Id.
  599. // Assign new one.
  600. mId = newId ? newId : Sim::gNextObjectId++;
  601. Sim::gIdDictionary->insert(this);
  602. }
  603. dSprintf( mIdString, sizeof( mIdString ), "%u", mId );
  604. }
  605. //-----------------------------------------------------------------------------
  606. void SimObject::assignName(const char *name)
  607. {
  608. if(mObjectName && !isNameChangeAllowed() )
  609. {
  610. Con::errorf( "SimObject::assignName - not allowed to change name of object '%s'", mObjectName);
  611. return;
  612. }
  613. // Added this assert 3/30/2007 because it is dumb to try to name
  614. // a SimObject the same thing as it's class name -patw
  615. //AssertFatal( dStricmp( getClassName(), name ), "Attempted to assign a name to a SimObject which matches it's type name." );
  616. if( dStricmp( getClassName(), name ) == 0 )
  617. Con::errorf( "SimObject::assignName - Assigning name '%s' to instance of object with type '%s'."
  618. " This can cause namespace linking issues.", getClassName(), name );
  619. StringTableEntry newName = NULL;
  620. if(name[0])
  621. newName = StringTable->insert(name);
  622. onNameChange( newName );
  623. if( mGroup )
  624. mGroup->mNameDictionary.remove( this );
  625. if( isProperlyAdded() )
  626. {
  627. unlinkNamespaces();
  628. Sim::gNameDictionary->remove( this );
  629. }
  630. mObjectName = newName;
  631. if( mGroup )
  632. mGroup->mNameDictionary.insert( this );
  633. if( isProperlyAdded() )
  634. {
  635. Sim::gNameDictionary->insert( this );
  636. linkNamespaces();
  637. }
  638. }
  639. //-----------------------------------------------------------------------------
  640. bool SimObject::registerObject(U32 id)
  641. {
  642. setId(id);
  643. return registerObject();
  644. }
  645. //-----------------------------------------------------------------------------
  646. bool SimObject::registerObject(const char *name)
  647. {
  648. assignName(name);
  649. return registerObject();
  650. }
  651. //-----------------------------------------------------------------------------
  652. bool SimObject::registerObject(const char *name, U32 id)
  653. {
  654. setId(id);
  655. assignName(name);
  656. return registerObject();
  657. }
  658. //=============================================================================
  659. // Introspection.
  660. //=============================================================================
  661. // MARK: ---- Introspection ----
  662. //-----------------------------------------------------------------------------
  663. bool SimObject::isMethod( const char* methodName )
  664. {
  665. if( !methodName || !methodName[0] )
  666. return false;
  667. if (CInterface::isMethod(this->getName(), methodName) || CInterface::isMethod(this->getClassName(), methodName)) {
  668. return true;
  669. }
  670. StringTableEntry stname = StringTable->insert( methodName );
  671. if( getNamespace() )
  672. return ( getNamespace()->lookup( stname ) != NULL );
  673. return false;
  674. }
  675. //-----------------------------------------------------------------------------
  676. bool SimObject::isField( const char* fieldName, bool includeStatic, bool includeDynamic )
  677. {
  678. const char* strFieldName = StringTable->insert( fieldName );
  679. if( includeStatic && getClassRep()->findField( strFieldName ) )
  680. return true;
  681. if( includeDynamic && getFieldDictionary() && getFieldDictionary()->findDynamicField( strFieldName ) )
  682. return true;
  683. return false;
  684. }
  685. //-----------------------------------------------------------------------------
  686. void SimObject::assignDynamicFieldsFrom(SimObject* parent)
  687. {
  688. if(parent->mFieldDictionary)
  689. {
  690. if( mFieldDictionary == NULL )
  691. mFieldDictionary = new SimFieldDictionary;
  692. mFieldDictionary->assignFrom(parent->mFieldDictionary);
  693. }
  694. }
  695. //-----------------------------------------------------------------------------
  696. void SimObject::assignFieldsFrom(SimObject *parent)
  697. {
  698. // Only allow field assigns from objects of the same class or
  699. // a superclass.
  700. if( getClassRep()->isClass( parent->getClassRep() ) )
  701. {
  702. const AbstractClassRep::FieldList &list = parent->getFieldList();
  703. // copy out all the fields:
  704. for(U32 i = 0; i < list.size(); i++)
  705. {
  706. const AbstractClassRep::Field* f = &list[i];
  707. // Skip the special field types.
  708. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  709. continue;
  710. // Skip certain fields that we don't want to see copied so we don't
  711. // get error messages from their setters.
  712. static StringTableEntry sName = StringTable->insert( "name" );
  713. static StringTableEntry sPersistentId = StringTable->insert( "persistentId" );
  714. if( f->pFieldname == sName || f->pFieldname == sPersistentId )
  715. continue;
  716. S32 lastField = f->elementCount - 1;
  717. for(S32 j = 0; j <= lastField; j++)
  718. {
  719. const char* fieldVal = (*f->getDataFn)( parent, Con::getData(f->type, (void *) (((const char *)parent) + f->offset), j, f->table, f->flag));
  720. // Don't assign the field is the pointer is null or if
  721. // the field is not empty and writing it was disallowed.
  722. if ( !fieldVal || ( fieldVal[0] && !writeField( f->pFieldname, fieldVal ) ) )
  723. continue;
  724. // code copied from SimObject::setDataField().
  725. // TODO: paxorr: abstract this into a better setData / getData that considers prot fields.
  726. FrameTemp<char> buffer(2048);
  727. FrameTemp<char> bufferSecure(2048); // This buffer is used to make a copy of the data
  728. ConsoleBaseType *cbt = ConsoleBaseType::getType( f->type );
  729. const char* szBuffer = cbt->prepData( fieldVal, buffer, 2048 );
  730. dMemset( bufferSecure, 0, 2048 );
  731. dMemcpy( bufferSecure, szBuffer, dStrlen( szBuffer ) );
  732. //If we have an index worth mentioning, process it for pass-along as well to ensure we set stuff correctly
  733. char* elementIdxBuffer = nullptr;
  734. if (f->elementCount > 1)
  735. {
  736. elementIdxBuffer = Con::getArgBuffer(256);
  737. dSprintf(elementIdxBuffer, 256, "%i", j);
  738. }
  739. if((*f->setDataFn)( this, elementIdxBuffer, bufferSecure ) )
  740. Con::setData(f->type, (void *) (((const char *)this) + f->offset), j, 1, &fieldVal, f->table);
  741. if (f->networkMask != 0)
  742. {
  743. NetObject* netObj = static_cast<NetObject*>(this);
  744. netObj->setMaskBits(f->networkMask);
  745. }
  746. }
  747. }
  748. }
  749. else
  750. {
  751. Con::errorf( "SimObject::assignFieldsFrom() - cannot assigned fields from object of type '%s' to object of type '%s'",
  752. parent->getClassName(), getClassName()
  753. );
  754. }
  755. assignDynamicFieldsFrom(parent);
  756. }
  757. //-----------------------------------------------------------------------------
  758. void SimObject::setDataField(StringTableEntry slotName, const char *array, const char *value)
  759. {
  760. // first search the static fields if enabled
  761. if(mFlags.test(ModStaticFields))
  762. {
  763. const AbstractClassRep::Field *fld = findField(slotName);
  764. if(fld)
  765. {
  766. // Skip the special field types as they are not data.
  767. if ( fld->type >= AbstractClassRep::ARCFirstCustomField )
  768. return;
  769. S32 array1 = array ? dAtoi(array) : 0;
  770. // Here we check to see if <this> is a datablock and if <value>
  771. // starts with "$$". If both true than save value as a runtime substitution.
  772. if (dynamic_cast<SimDataBlock*>(this) && value[0] == '$' && value[1] == '$')
  773. {
  774. if (!this->allowSubstitutions())
  775. {
  776. Con::errorf("Substitution Error: %s datablocks do not allow \"$$\" field substitutions. [%s]",
  777. this->getClassName(), this->getName());
  778. return;
  779. }
  780. if (fld->doNotSubstitute)
  781. {
  782. Con::errorf("Substitution Error: field \"%s\" of datablock %s prohibits \"$$\" field substitutions. [%s]",
  783. slotName, this->getClassName(), this->getName());
  784. return;
  785. }
  786. // add the substitution
  787. ((SimDataBlock*)this)->addSubstitution(slotName, array1, value);
  788. return;
  789. }
  790. if(array1 >= 0 && array1 < fld->elementCount && fld->elementCount >= 1)
  791. {
  792. // If the set data notify callback returns true, then go ahead and
  793. // set the data, otherwise, assume the set notify callback has either
  794. // already set the data, or has deemed that the data should not
  795. // be set at all.
  796. FrameTemp<char> buffer(2048);
  797. FrameTemp<char> bufferSecure(2048); // This buffer is used to make a copy of the data
  798. // so that if the prep functions or any other functions use the string stack, the data
  799. // is not corrupted.
  800. ConsoleBaseType *cbt = ConsoleBaseType::getType( fld->type );
  801. AssertFatal( cbt != NULL, "Could not resolve Type Id." );
  802. const char* szBuffer = cbt->prepData( value, buffer, 2048 );
  803. dMemset( bufferSecure, 0, 2048 );
  804. dMemcpy( bufferSecure, szBuffer, dStrlen( szBuffer ) );
  805. if( (*fld->setDataFn)( this, array, bufferSecure ) )
  806. Con::setData(fld->type, (void *) (((const char *)this) + fld->offset), array1, 1, &value, fld->table);
  807. if(fld->validator)
  808. fld->validator->validateType(this, (void *) (((const char *)this) + fld->offset));
  809. if (fld->networkMask != 0)
  810. {
  811. NetObject* netObj = static_cast<NetObject*>(this);
  812. netObj->setMaskBits(fld->networkMask);
  813. }
  814. onStaticModified( slotName, value );
  815. return;
  816. }
  817. if(fld->validator)
  818. fld->validator->validateType(this, (void *) (((const char *)this) + fld->offset));
  819. onStaticModified( slotName, value );
  820. return;
  821. }
  822. }
  823. if(mFlags.test(ModDynamicFields))
  824. {
  825. if(!mFieldDictionary)
  826. mFieldDictionary = new SimFieldDictionary;
  827. if(!array)
  828. {
  829. mFieldDictionary->setFieldValue(slotName, value);
  830. onDynamicModified( slotName, value );
  831. }
  832. else
  833. {
  834. char buf[256];
  835. dStrcpy(buf, slotName, 256);
  836. dStrcat(buf, array, 256);
  837. StringTableEntry permanentSlotName = StringTable->insert(buf);
  838. mFieldDictionary->setFieldValue(permanentSlotName, value);
  839. onDynamicModified( permanentSlotName, value );
  840. }
  841. }
  842. }
  843. //-----------------------------------------------------------------------------
  844. const char *SimObject::getDataField(StringTableEntry slotName, const char *array)
  845. {
  846. if(mFlags.test(ModStaticFields))
  847. {
  848. S32 array1 = array ? dAtoi(array) : -1;
  849. const AbstractClassRep::Field *fld = findField(slotName);
  850. if(fld)
  851. {
  852. if(array1 == -1 && fld->elementCount == 1)
  853. return (*fld->getDataFn)( this, Con::getData(fld->type, (void *) (((const char *)this) + fld->offset), 0, fld->table, fld->flag) );
  854. if(array1 >= 0 && array1 < fld->elementCount)
  855. return (*fld->getDataFn)( this, Con::getData(fld->type, (void *) (((const char *)this) + fld->offset), array1, fld->table, fld->flag) );// + typeSizes[fld.type] * array1));
  856. return "";
  857. }
  858. }
  859. if(mFlags.test(ModDynamicFields))
  860. {
  861. if(!mFieldDictionary)
  862. return "";
  863. if(!array)
  864. {
  865. if (const char* val = mFieldDictionary->getFieldValue(slotName))
  866. return val;
  867. }
  868. else
  869. {
  870. static char buf[256];
  871. dStrcpy(buf, slotName, 256);
  872. dStrcat(buf, array, 256);
  873. if (const char* val = mFieldDictionary->getFieldValue(StringTable->insert(buf)))
  874. return val;
  875. }
  876. }
  877. return "";
  878. }
  879. const char *SimObject::getPrefixedDataField(StringTableEntry fieldName, const char *array)
  880. {
  881. // Sanity!
  882. AssertFatal(fieldName != NULL, "Cannot get field value with NULL field name.");
  883. // Fetch field value.
  884. const char* pFieldValue = getDataField(fieldName, array);
  885. // Sanity.
  886. //AssertFatal(pFieldValue != NULL, "Field value cannot be NULL.");
  887. if (!pFieldValue)
  888. return NULL;
  889. // Return without the prefix if there's no value.
  890. if (*pFieldValue == 0)
  891. return StringTable->EmptyString();
  892. // Fetch the field prefix.
  893. StringTableEntry fieldPrefix = getDataFieldPrefix(fieldName);
  894. // Sanity!
  895. AssertFatal(fieldPrefix != NULL, "Field prefix cannot be NULL.");
  896. // Calculate a buffer size including prefix.
  897. const U32 valueBufferSize = dStrlen(fieldPrefix) + dStrlen(pFieldValue) + 1;
  898. // Fetch a buffer.
  899. char* pValueBuffer = Con::getReturnBuffer(valueBufferSize);
  900. // Format the value buffer.
  901. dSprintf(pValueBuffer, valueBufferSize, "%s%s", fieldPrefix, pFieldValue);
  902. return pValueBuffer;
  903. }
  904. //-----------------------------------------------------------------------------
  905. void SimObject::setPrefixedDataField(StringTableEntry fieldName, const char *_array, const char *value)
  906. {
  907. // Sanity!
  908. AssertFatal(fieldName != NULL, "Cannot set object field value with NULL field name.");
  909. AssertFatal(value != NULL, "Field value cannot be NULL.");
  910. // Set value without prefix if there's no value.
  911. if (*value == 0)
  912. {
  913. setDataField(fieldName, _array, value);
  914. return;
  915. }
  916. // Fetch the field prefix.
  917. StringTableEntry fieldPrefix = getDataFieldPrefix(fieldName);
  918. // Sanity.
  919. AssertFatal(fieldPrefix != NULL, "Field prefix cannot be NULL.");
  920. // Do we have a field prefix?
  921. if (fieldPrefix == StringTable->EmptyString())
  922. {
  923. // No, so set the data field in the usual way.
  924. setDataField(fieldName, _array, value);
  925. return;
  926. }
  927. // Yes, so fetch the length of the field prefix.
  928. const U32 fieldPrefixLength = dStrlen(fieldPrefix);
  929. // Yes, so does it start with the object field prefix?
  930. if (dStrnicmp(value, fieldPrefix, fieldPrefixLength) != 0)
  931. {
  932. // No, so set the data field in the usual way.
  933. setDataField(fieldName, _array, value);
  934. return;
  935. }
  936. // Yes, so set the data excluding the prefix.
  937. setDataField(fieldName, _array, value + fieldPrefixLength);
  938. }
  939. //-----------------------------------------------------------------------------
  940. const char *SimObject::getPrefixedDynamicDataField(StringTableEntry fieldName, const char *_array, const S32 fieldType)
  941. {
  942. // Sanity!
  943. AssertFatal(fieldName != NULL, "Cannot get field value with NULL field name.");
  944. // Fetch field value.
  945. const char* pFieldValue = getDataField(fieldName, _array);
  946. // Sanity.
  947. AssertFatal(pFieldValue != NULL, "Field value cannot be NULL.");
  948. // Return the field if no field type is specified.
  949. if (fieldType == -1)
  950. return pFieldValue;
  951. // Return without the prefix if there's no value.
  952. if (*pFieldValue == 0)
  953. return StringTable->EmptyString();
  954. // Fetch the console base type.
  955. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType(fieldType);
  956. // Did we find the console base type?
  957. if (pConsoleBaseType == NULL)
  958. {
  959. // No, so warn.
  960. Con::warnf("getPrefixedDynamicDataField() - Invalid field type '%d' specified for field '%s' with value '%s'.",
  961. fieldType, fieldName, pFieldValue);
  962. }
  963. // Fetch the field prefix.
  964. StringTableEntry fieldPrefix = pConsoleBaseType->getTypePrefix();
  965. // Sanity!
  966. AssertFatal(fieldPrefix != NULL, "Field prefix cannot be NULL.");
  967. // Calculate a buffer size including prefix.
  968. const U32 valueBufferSize = dStrlen(fieldPrefix) + dStrlen(pFieldValue) + 1;
  969. // Fetch a buffer.
  970. char* pValueBuffer = Con::getReturnBuffer(valueBufferSize);
  971. // Format the value buffer.
  972. dSprintf(pValueBuffer, valueBufferSize, "%s%s", fieldPrefix, pFieldValue);
  973. return pValueBuffer;
  974. }
  975. //-----------------------------------------------------------------------------
  976. void SimObject::setPrefixedDynamicDataField(StringTableEntry fieldName, const char *array, const char *value, const S32 fieldType)
  977. {
  978. // Sanity!
  979. AssertFatal(fieldName != NULL, "Cannot set object field value with NULL field name.");
  980. AssertFatal(value != NULL, "Field value cannot be NULL.");
  981. // Set value without prefix if no field type was specified.
  982. if (fieldType == -1)
  983. {
  984. setDataField(fieldName, NULL, value);
  985. return;
  986. }
  987. // Fetch the console base type.
  988. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType(fieldType);
  989. // Did we find the console base type?
  990. if (pConsoleBaseType == NULL)
  991. {
  992. // No, so warn.
  993. Con::warnf("setPrefixedDynamicDataField() - Invalid field type '%d' specified for field '%s' with value '%s'.",
  994. fieldType, fieldName, value);
  995. }
  996. // Set value without prefix if there's no value or we didn't find the console base type.
  997. if (*value == 0 || pConsoleBaseType == NULL)
  998. {
  999. setDataField(fieldName, NULL, value);
  1000. return;
  1001. }
  1002. // Fetch the field prefix.
  1003. StringTableEntry fieldPrefix = pConsoleBaseType->getTypePrefix();
  1004. // Sanity.
  1005. AssertFatal(fieldPrefix != NULL, "Field prefix cannot be NULL.");
  1006. // Do we have a field prefix?
  1007. if (fieldPrefix == StringTable->EmptyString())
  1008. {
  1009. // No, so set the data field in the usual way.
  1010. setDataField(fieldName, NULL, value);
  1011. return;
  1012. }
  1013. // Yes, so fetch the length of the field prefix.
  1014. const U32 fieldPrefixLength = dStrlen(fieldPrefix);
  1015. // Yes, so does it start with the object field prefix?
  1016. if (dStrnicmp(value, fieldPrefix, fieldPrefixLength) != 0)
  1017. {
  1018. // No, so set the data field in the usual way.
  1019. setDataField(fieldName, NULL, value);
  1020. return;
  1021. }
  1022. // Yes, so set the data excluding the prefix.
  1023. setDataField(fieldName, NULL, value + fieldPrefixLength);
  1024. }
  1025. //-----------------------------------------------------------------------------
  1026. StringTableEntry SimObject::getDataFieldPrefix(StringTableEntry fieldName)
  1027. {
  1028. // Sanity!
  1029. AssertFatal(fieldName != NULL, "Cannot get field prefix with NULL field name.");
  1030. // Find the field.
  1031. const AbstractClassRep::Field* pField = findField(fieldName);
  1032. // Return nothing if field was not found.
  1033. if (pField == NULL)
  1034. return StringTable->EmptyString();
  1035. // Yes, so fetch the console base type.
  1036. ConsoleBaseType* pConsoleBaseType = ConsoleBaseType::getType(pField->type);
  1037. // Fetch the type prefix.
  1038. return pConsoleBaseType->getTypePrefix();
  1039. }
  1040. //-----------------------------------------------------------------------------
  1041. U32 SimObject::getDataFieldType( StringTableEntry slotName, const char* array )
  1042. {
  1043. const AbstractClassRep::Field* field = findField( slotName );
  1044. if(field)
  1045. return field->type;
  1046. // Check dynamic fields
  1047. if(!mFieldDictionary)
  1048. return 0;
  1049. if(array == NULL || *array == 0)
  1050. return mFieldDictionary->getFieldType( slotName );
  1051. else
  1052. {
  1053. static char buf[256];
  1054. dStrcpy( buf, slotName, 256 );
  1055. dStrcat( buf, array, 256 );
  1056. return mFieldDictionary->getFieldType( StringTable->insert( buf ) );
  1057. }
  1058. }
  1059. //-----------------------------------------------------------------------------
  1060. void SimObject::setDataFieldType(const U32 fieldTypeId, StringTableEntry slotName, const char *array)
  1061. {
  1062. // This only works on dynamic fields, bail if we have no field dictionary
  1063. if(!mFieldDictionary)
  1064. return;
  1065. if(array == NULL || *array == 0)
  1066. {
  1067. mFieldDictionary->setFieldType( slotName, fieldTypeId );
  1068. onDynamicModified( slotName, mFieldDictionary->getFieldValue(slotName) );
  1069. }
  1070. else
  1071. {
  1072. static char buf[256];
  1073. dStrcpy( buf, slotName, 256 );
  1074. dStrcat( buf, array, 256 );
  1075. mFieldDictionary->setFieldType( StringTable->insert( buf ), fieldTypeId );
  1076. onDynamicModified( slotName, mFieldDictionary->getFieldValue(slotName) );
  1077. }
  1078. }
  1079. //-----------------------------------------------------------------------------
  1080. void SimObject::setDataFieldType(const char *typeName, StringTableEntry slotName, const char *array)
  1081. {
  1082. // This only works on dynamic fields, bail if we have no field dictionary
  1083. if(!mFieldDictionary)
  1084. return;
  1085. if(array == NULL || *array == 0)
  1086. mFieldDictionary->setFieldType( slotName, typeName );
  1087. else
  1088. {
  1089. static char buf[256];
  1090. dStrcpy( buf, slotName, 256 );
  1091. dStrcat( buf, array, 256 );
  1092. StringTableEntry permanentSlotName = StringTable->insert(buf);
  1093. mFieldDictionary->setFieldType( permanentSlotName, typeName );
  1094. onDynamicModified( permanentSlotName, mFieldDictionary->getFieldValue(permanentSlotName) );
  1095. }
  1096. }
  1097. // This is the copy-constructor used to create temporary datablock clones.
  1098. // The <temp_clone> argument is added to distinguish this copy-constructor
  1099. // from any general-purpose copy-constructor that might be needed in the
  1100. // future. <temp_clone> should always be true when creating temporary
  1101. // datablock clones.
  1102. //
  1103. SimObject::SimObject(const SimObject& other, bool temp_clone)
  1104. {
  1105. is_temp_clone = temp_clone;
  1106. mObjectName = other.mObjectName;
  1107. mOriginalName = other.mOriginalName;
  1108. nextNameObject = other.nextNameObject;
  1109. nextManagerNameObject = other.nextManagerNameObject;
  1110. nextIdObject = other.nextIdObject;
  1111. mGroup = other.mGroup;
  1112. mFlags = other.mFlags;
  1113. mProgenitorFile = other.mProgenitorFile;
  1114. mCopySource = other.mCopySource;
  1115. mFieldDictionary = other.mFieldDictionary;
  1116. //mIdString = other.mIdString; // special treatment (see below)
  1117. mFilename = other.mFilename;
  1118. mDeclarationLine = other.mDeclarationLine;
  1119. mNotifyList = other.mNotifyList;
  1120. mId = other.mId;
  1121. mInternalName = other.mInternalName;
  1122. mCanSaveFieldDictionary = other.mCanSaveFieldDictionary;
  1123. mPersistentId = other.mPersistentId;
  1124. mNameSpace = other.mNameSpace;
  1125. mClassName = other.mClassName;
  1126. mSuperClassName = other.mSuperClassName;
  1127. preventNameChanging = other.preventNameChanging;
  1128. if (mId)
  1129. dSprintf( mIdString, sizeof( mIdString ), "%u", mId );
  1130. else
  1131. mIdString[ 0 ] = '\0';
  1132. }
  1133. //-----------------------------------------------------------------------------
  1134. void SimObject::dumpClassHierarchy()
  1135. {
  1136. AbstractClassRep* pRep = getClassRep();
  1137. while(pRep)
  1138. {
  1139. Con::warnf("%s ->", pRep->getClassName());
  1140. pRep = pRep->getParentClass();
  1141. }
  1142. }
  1143. //-----------------------------------------------------------------------------
  1144. SimObject* SimObject::clone()
  1145. {
  1146. if( !getClassRep() )
  1147. return NULL;
  1148. ConsoleObject* conObject = getClassRep()->create();
  1149. if( !conObject )
  1150. return NULL;
  1151. SimObject* simObject = dynamic_cast< SimObject* >( conObject );
  1152. if( !simObject )
  1153. {
  1154. delete conObject;
  1155. return NULL;
  1156. }
  1157. simObject->assignFieldsFrom( this );
  1158. String name = Sim::getUniqueName( getName() );
  1159. if( !simObject->registerObject( name ) )
  1160. {
  1161. delete simObject;
  1162. return NULL;
  1163. }
  1164. if( getGroup() )
  1165. getGroup()->addObject( simObject );
  1166. return simObject;
  1167. }
  1168. //-----------------------------------------------------------------------------
  1169. SimObject* SimObject::deepClone()
  1170. {
  1171. return clone();
  1172. }
  1173. //=============================================================================
  1174. // Grouping.
  1175. //=============================================================================
  1176. // MARK: ---- Grouping ----
  1177. //-----------------------------------------------------------------------------
  1178. SimObject* SimObject::findObject( const char* )
  1179. {
  1180. return NULL;
  1181. }
  1182. //-----------------------------------------------------------------------------
  1183. bool SimObject::isChildOfGroup(SimGroup* pGroup)
  1184. {
  1185. if(!pGroup)
  1186. return false;
  1187. //if we *are* the group in question,
  1188. //return true:
  1189. if(pGroup == dynamic_cast<SimGroup*>(this))
  1190. return true;
  1191. SimGroup* temp = mGroup;
  1192. while(temp)
  1193. {
  1194. if(temp == pGroup)
  1195. return true;
  1196. temp = temp->mGroup;
  1197. }
  1198. return false;
  1199. }
  1200. //-----------------------------------------------------------------------------
  1201. bool SimObject::addToSet(SimObjectId spid)
  1202. {
  1203. if (mFlags.test(Added) == false)
  1204. return false;
  1205. SimObject* ptr = Sim::findObject(spid);
  1206. if (ptr)
  1207. {
  1208. SimSet* sp = dynamic_cast<SimSet*>(ptr);
  1209. AssertFatal(sp != 0,
  1210. "SimObject::addToSet: "
  1211. "ObjectId does not refer to a set object");
  1212. sp->addObject(this);
  1213. return true;
  1214. }
  1215. return false;
  1216. }
  1217. //-----------------------------------------------------------------------------
  1218. bool SimObject::addToSet(const char *ObjectName)
  1219. {
  1220. if (mFlags.test(Added) == false)
  1221. return false;
  1222. SimObject* ptr = Sim::findObject(ObjectName);
  1223. if (ptr)
  1224. {
  1225. SimSet* sp = dynamic_cast<SimSet*>(ptr);
  1226. AssertFatal(sp != 0,
  1227. "SimObject::addToSet: "
  1228. "ObjectName does not refer to a set object");
  1229. sp->addObject(this);
  1230. return true;
  1231. }
  1232. return false;
  1233. }
  1234. //-----------------------------------------------------------------------------
  1235. bool SimObject::removeFromSet(SimObjectId sid)
  1236. {
  1237. if (mFlags.test(Added) == false)
  1238. return false;
  1239. SimSet *set;
  1240. if(Sim::findObject(sid, set))
  1241. {
  1242. set->removeObject(this);
  1243. return true;
  1244. }
  1245. return false;
  1246. }
  1247. //-----------------------------------------------------------------------------
  1248. bool SimObject::removeFromSet(const char *objectName)
  1249. {
  1250. if (mFlags.test(Added) == false)
  1251. return false;
  1252. SimSet *set;
  1253. if(Sim::findObject(objectName, set))
  1254. {
  1255. set->removeObject(this);
  1256. return true;
  1257. }
  1258. return false;
  1259. }
  1260. //-----------------------------------------------------------------------------
  1261. void SimObject::dumpGroupHierarchy()
  1262. {
  1263. String className( getClassName() );
  1264. String objectName( getName() );
  1265. Con::warnf( "[%i] %s - %s ->", getId(), className.c_str(), objectName.c_str() );
  1266. if ( mGroup )
  1267. mGroup->dumpGroupHierarchy();
  1268. }
  1269. //=============================================================================
  1270. // Events.
  1271. //=============================================================================
  1272. // MARK: ---- Events ----
  1273. //-----------------------------------------------------------------------------
  1274. bool SimObject::onAdd()
  1275. {
  1276. mFlags.set(Added);
  1277. linkNamespaces();
  1278. return true;
  1279. }
  1280. //-----------------------------------------------------------------------------
  1281. void SimObject::onRemove()
  1282. {
  1283. mFlags.clear(Added);
  1284. unlinkNamespaces();
  1285. }
  1286. //-----------------------------------------------------------------------------
  1287. void SimObject::onGroupAdd()
  1288. {
  1289. }
  1290. //-----------------------------------------------------------------------------
  1291. void SimObject::onGroupRemove()
  1292. {
  1293. }
  1294. //-----------------------------------------------------------------------------
  1295. void SimObject::onDeleteNotify(SimObject*)
  1296. {
  1297. }
  1298. //-----------------------------------------------------------------------------
  1299. void SimObject::onNameChange(const char*)
  1300. {
  1301. }
  1302. //-----------------------------------------------------------------------------
  1303. void SimObject::onStaticModified(const char* slotName, const char* newValue)
  1304. {
  1305. }
  1306. //-----------------------------------------------------------------------------
  1307. void SimObject::onDynamicModified(const char* slotName, const char* newValue)
  1308. {
  1309. }
  1310. //=============================================================================
  1311. // Notifications.
  1312. //=============================================================================
  1313. // MARK: ---- Notifications ----
  1314. static Chunker<SimObject::Notify> notifyChunker(128000);
  1315. SimObject::Notify *SimObject::mNotifyFreeList = NULL;
  1316. //-----------------------------------------------------------------------------
  1317. SimObject::Notify *SimObject::allocNotify()
  1318. {
  1319. if(mNotifyFreeList)
  1320. {
  1321. SimObject::Notify *ret = mNotifyFreeList;
  1322. mNotifyFreeList = ret->next;
  1323. return ret;
  1324. }
  1325. return notifyChunker.alloc();
  1326. }
  1327. //-----------------------------------------------------------------------------
  1328. void SimObject::freeNotify(SimObject::Notify* note)
  1329. {
  1330. AssertFatal(note->type != SimObject::Notify::Invalid, "Invalid notify");
  1331. note->type = SimObject::Notify::Invalid;
  1332. note->next = mNotifyFreeList;
  1333. mNotifyFreeList = note;
  1334. }
  1335. //-----------------------------------------------------------------------------
  1336. SimObject::Notify* SimObject::removeNotify(void *ptr, SimObject::Notify::Type type)
  1337. {
  1338. Notify **list = &mNotifyList;
  1339. while(*list)
  1340. {
  1341. if((*list)->ptr == ptr && (*list)->type == type)
  1342. {
  1343. SimObject::Notify *ret = *list;
  1344. *list = ret->next;
  1345. return ret;
  1346. }
  1347. list = &((*list)->next);
  1348. }
  1349. return NULL;
  1350. }
  1351. //-----------------------------------------------------------------------------
  1352. void SimObject::deleteNotify(SimObject* obj)
  1353. {
  1354. AssertFatal(!obj->isDeleted(),
  1355. "SimManager::deleteNotify: Object is being deleted");
  1356. Notify *note = allocNotify();
  1357. note->ptr = (void *) this;
  1358. note->next = obj->mNotifyList;
  1359. note->type = Notify::DeleteNotify;
  1360. obj->mNotifyList = note;
  1361. note = allocNotify();
  1362. note->ptr = (void *) obj;
  1363. note->next = mNotifyList;
  1364. note->type = Notify::ClearNotify;
  1365. mNotifyList = note;
  1366. //obj->deleteNotifyList.pushBack(this);
  1367. //clearNotifyList.pushBack(obj);
  1368. }
  1369. //-----------------------------------------------------------------------------
  1370. void SimObject::registerReference(SimObject **ptr)
  1371. {
  1372. Notify *note = allocNotify();
  1373. note->ptr = (void *) ptr;
  1374. note->next = mNotifyList;
  1375. note->type = Notify::ObjectRef;
  1376. mNotifyList = note;
  1377. }
  1378. //-----------------------------------------------------------------------------
  1379. void SimObject::unregisterReference(SimObject **ptr)
  1380. {
  1381. Notify *note = removeNotify((void *) ptr, Notify::ObjectRef);
  1382. if(note)
  1383. {
  1384. freeNotify(note);
  1385. if( mFlags.test( AutoDelete ) )
  1386. {
  1387. for( Notify* n = mNotifyList; n != NULL; n = n->next )
  1388. if( n->type == Notify::ObjectRef )
  1389. return;
  1390. deleteObject();
  1391. }
  1392. }
  1393. }
  1394. //-----------------------------------------------------------------------------
  1395. void SimObject::clearNotify(SimObject* obj)
  1396. {
  1397. Notify *note = obj->removeNotify((void *) this, Notify::DeleteNotify);
  1398. if(note)
  1399. freeNotify(note);
  1400. note = removeNotify((void *) obj, Notify::ClearNotify);
  1401. if(note)
  1402. freeNotify(note);
  1403. }
  1404. //-----------------------------------------------------------------------------
  1405. void SimObject::processDeleteNotifies()
  1406. {
  1407. // clear out any delete notifies and
  1408. // object refs.
  1409. while(mNotifyList)
  1410. {
  1411. Notify *note = mNotifyList;
  1412. mNotifyList = note->next;
  1413. AssertFatal(note->type != Notify::ClearNotify, "Clear notes should be all gone.");
  1414. if(note->type == Notify::DeleteNotify)
  1415. {
  1416. SimObject *obj = (SimObject *) note->ptr;
  1417. Notify *cnote = obj->removeNotify((void *)this, Notify::ClearNotify);
  1418. obj->onDeleteNotify(this);
  1419. freeNotify(cnote);
  1420. }
  1421. else
  1422. {
  1423. // it must be an object ref - a pointer refs this object
  1424. *((SimObject **) note->ptr) = NULL;
  1425. }
  1426. freeNotify(note);
  1427. }
  1428. }
  1429. //-----------------------------------------------------------------------------
  1430. void SimObject::clearAllNotifications()
  1431. {
  1432. for(Notify **cnote = &mNotifyList; *cnote; )
  1433. {
  1434. Notify *temp = *cnote;
  1435. if(temp->type == Notify::ClearNotify)
  1436. {
  1437. *cnote = temp->next;
  1438. Notify *note = ((SimObject *) temp->ptr)->removeNotify((void *) this, Notify::DeleteNotify);
  1439. freeNotify(temp);
  1440. if ( note )
  1441. freeNotify(note);
  1442. }
  1443. else
  1444. cnote = &(temp->next);
  1445. }
  1446. }
  1447. //=============================================================================
  1448. // Namespaces.
  1449. //=============================================================================
  1450. // MARK: ---- Namespaces ----
  1451. //-----------------------------------------------------------------------------
  1452. void SimObject::linkNamespaces()
  1453. {
  1454. // Don't link if we already have a namespace linkage in place.
  1455. // If you want to change namespace linking, first call unlinkNamespaces()
  1456. // while still having the class namespace fields matching the current
  1457. // setup.
  1458. if (mNameSpace)
  1459. {
  1460. Con::warnf("SimObject::linkNamespaces -- Namespace linkage already in place %s", mNameSpace->getName());
  1461. return;
  1462. }
  1463. // Get the namespace for the C++ class.
  1464. Namespace* cppNamespace = getClassRep()->getNameSpace();
  1465. // Parent namespace defaults to namespace of C++ class.
  1466. Namespace* parentNamespace = cppNamespace;
  1467. // Perform superclass linking, if requested.
  1468. if( mSuperClassName && mSuperClassName[ 0 ] )
  1469. {
  1470. // Look up the superclass namespace.
  1471. Namespace* superClassNamespace = Con::lookupNamespace( mSuperClassName );
  1472. // If packages are active and adding to the superclass namespace, then we will
  1473. // have multiple packages in a parent chain that all have the same name.
  1474. // Con::lookupNamespace returns the bottom-most package in the chain to us so
  1475. // in order to properly link namespace here without conflicting with the package
  1476. // mechanism, we need to properly link child namespaces to the bottom-most namespace
  1477. // while linking parent namespaces to the topmost namespace. To find the latter
  1478. // one, we walk up the hierarchy here.
  1479. Namespace* superClassNamespacePackageRoot = superClassNamespace->getPackageRoot();
  1480. // Link the superclass namespace to the C++ class namespace.
  1481. if( superClassNamespacePackageRoot->getParent() == NULL )
  1482. {
  1483. // The superclass namespace isn't linked yet so we just
  1484. // link it to the C++ class namespace and make that our parent.
  1485. // No increasing parent reference counts is needed in this case.
  1486. bool ok = superClassNamespacePackageRoot->classLinkTo( cppNamespace );
  1487. AssertFatal( ok, "SimObject::linkNamespaces - failed to link new namespace to c++ class name" );
  1488. parentNamespace = superClassNamespace;
  1489. }
  1490. else
  1491. {
  1492. // In debug builds, make sure the namespace hierarchy that's been
  1493. // put into place actually makes sense and leads back to the C++
  1494. // class namespace.
  1495. #ifdef TORQUE_DEBUG
  1496. bool foundClassNameNS = false;
  1497. for( Namespace* linkWalk = superClassNamespacePackageRoot->getParent(); linkWalk != NULL;
  1498. linkWalk = linkWalk->getParent() )
  1499. {
  1500. if( linkWalk == cppNamespace )
  1501. {
  1502. foundClassNameNS = true;
  1503. break;
  1504. }
  1505. }
  1506. if( !foundClassNameNS )
  1507. {
  1508. // C++ class namespace not in parent link chain. Warn about it.
  1509. Con::errorf(
  1510. "SimObject::linkNamespaces - cannot link object to superclass %s because c++ class %s is not in the parent namespace chain. Linking object to c++ class.",
  1511. mSuperClassName,
  1512. getClassName()
  1513. );
  1514. // Clear out superclass name so we don't come across it during
  1515. // unlinking.
  1516. mSuperClassName = NULL;
  1517. }
  1518. else
  1519. #endif
  1520. {
  1521. // Super link is ok.
  1522. parentNamespace = superClassNamespace;
  1523. // Now increase the reference count of all namespaces in the parent hierarchy
  1524. // (up to the C++ class).
  1525. for( Namespace* linkWalk = parentNamespace;
  1526. linkWalk != NULL && linkWalk != cppNamespace && linkWalk->getParent() != NULL;
  1527. linkWalk = linkWalk->getParent() )
  1528. {
  1529. // Skip namespaces coming from packages.
  1530. if( linkWalk->getPackage() != NULL )
  1531. continue;
  1532. linkWalk->incRefCountToParent();
  1533. }
  1534. }
  1535. }
  1536. }
  1537. // If class name is set, link it in as the new parent
  1538. // which itself inherits from the current parent.
  1539. if( mClassName && mClassName[ 0 ] )
  1540. {
  1541. Namespace* classNamespace = Con::lookupNamespace( mClassName );
  1542. if( classNamespace && classNamespace->classLinkTo( parentNamespace ) )
  1543. {
  1544. parentNamespace = classNamespace;
  1545. }
  1546. else
  1547. {
  1548. // Clear out class name so we don't perform a bogus unlink
  1549. // in unlinkNamespaces().
  1550. mClassName = NULL;
  1551. }
  1552. }
  1553. // Finally, if we have an object name, link its namespace
  1554. // as the child to the current parent namespace and let it
  1555. // become the final namespace of this object.
  1556. StringTableEntry objectName = getName();
  1557. if( objectName && objectName[ 0 ] )
  1558. {
  1559. Namespace* objectNamespace = Con::lookupNamespace( objectName );
  1560. if( objectNamespace && objectNamespace->classLinkTo( parentNamespace ) )
  1561. {
  1562. parentNamespace = objectNamespace;
  1563. }
  1564. }
  1565. // Store our namespace.
  1566. mNameSpace = parentNamespace;
  1567. }
  1568. //-----------------------------------------------------------------------------
  1569. void SimObject::unlinkNamespaces()
  1570. {
  1571. if( !mNameSpace )
  1572. return;
  1573. Namespace* cppNamespace = getClassRep()->getNameSpace();
  1574. Namespace* parentNamespace = cppNamespace;
  1575. // Handle superclass.
  1576. if( mSuperClassName && mSuperClassName[ 0 ] )
  1577. {
  1578. // Get the superclass namespace.
  1579. Namespace* superClassNamespace = Con::lookupNamespace( mSuperClassName );
  1580. // Make it the parent namespace.
  1581. parentNamespace = superClassNamespace;
  1582. // Decrease parent refcounts on the superclass hierarchy.
  1583. for( Namespace* linkWalk = superClassNamespace;
  1584. linkWalk != NULL && linkWalk != cppNamespace && linkWalk->getParent() != NULL; )
  1585. {
  1586. // Store the parent link since it may disappear once we
  1587. // decrease the reference count.
  1588. Namespace* parent = linkWalk->getParent();
  1589. // Decrease the refcount.
  1590. if( linkWalk->getPackage() == NULL ) // Skip namespaces coming from packages.
  1591. linkWalk->decRefCountToParent();
  1592. // Walk up.
  1593. linkWalk = parent;
  1594. }
  1595. }
  1596. // Handle class.
  1597. if( mClassName && mClassName[ 0 ] )
  1598. {
  1599. Namespace* classNamespace = Con::lookupNamespace( mClassName );
  1600. if( classNamespace )
  1601. {
  1602. classNamespace->decRefCountToParent();
  1603. parentNamespace = classNamespace;
  1604. }
  1605. }
  1606. // Handle object name.
  1607. StringTableEntry objectName = getName();
  1608. if( objectName && objectName[ 0 ] )
  1609. mNameSpace->decRefCountToParent();
  1610. mNameSpace = NULL;
  1611. }
  1612. //-----------------------------------------------------------------------------
  1613. void SimObject::setClassNamespace( const char *classNamespace )
  1614. {
  1615. StringTableEntry oldClassNamespace = mClassName;
  1616. StringTableEntry newClassNamespace = StringTable->insert( classNamespace );
  1617. if( oldClassNamespace == newClassNamespace )
  1618. return;
  1619. if( isProperlyAdded() )
  1620. unlinkNamespaces();
  1621. mClassName = newClassNamespace;
  1622. if( isProperlyAdded() )
  1623. {
  1624. linkNamespaces();
  1625. // Restore old namespace setup if linkage failed.
  1626. if( mClassName != newClassNamespace )
  1627. {
  1628. mClassName = oldClassNamespace;
  1629. linkNamespaces();
  1630. }
  1631. }
  1632. }
  1633. //-----------------------------------------------------------------------------
  1634. void SimObject::setSuperClassNamespace( const char *superClassNamespace )
  1635. {
  1636. StringTableEntry oldSuperClassNamespace = mSuperClassName;
  1637. StringTableEntry newSuperClassNamespace = StringTable->insert( superClassNamespace );
  1638. if( oldSuperClassNamespace == newSuperClassNamespace )
  1639. return;
  1640. if( isProperlyAdded() )
  1641. unlinkNamespaces();
  1642. mSuperClassName = newSuperClassNamespace;
  1643. if( isProperlyAdded() )
  1644. {
  1645. linkNamespaces();
  1646. // Restore old setup if linkage failed.
  1647. if( mSuperClassName != newSuperClassNamespace )
  1648. {
  1649. mSuperClassName = oldSuperClassNamespace;
  1650. linkNamespaces();
  1651. }
  1652. }
  1653. }
  1654. //=============================================================================
  1655. // Misc.
  1656. //=============================================================================
  1657. // MARK: ---- Misc ----
  1658. //-----------------------------------------------------------------------------
  1659. void SimObject::setInternalName( const char* newname )
  1660. {
  1661. if( newname )
  1662. mInternalName = StringTable->insert( newname );
  1663. else
  1664. mInternalName = StringTable->EmptyString();
  1665. }
  1666. //-----------------------------------------------------------------------------
  1667. void SimObject::setOriginalName( const char* originalName )
  1668. {
  1669. if( originalName )
  1670. mOriginalName = StringTable->insert( originalName );
  1671. else
  1672. mOriginalName = StringTable->EmptyString();
  1673. }
  1674. //-----------------------------------------------------------------------------
  1675. const char *SimObject::tabComplete(const char *prevText, S32 baseLen, bool fForward)
  1676. {
  1677. return mNameSpace->tabComplete(prevText, baseLen, fForward);
  1678. }
  1679. //-----------------------------------------------------------------------------
  1680. void SimObject::setSelected( bool sel )
  1681. {
  1682. if( mFlags.test( Selected ) == sel )
  1683. return; // No change.
  1684. if( sel )
  1685. {
  1686. mFlags.set( Selected );
  1687. _onSelected();
  1688. }
  1689. else
  1690. {
  1691. mFlags.clear( Selected );
  1692. _onUnselected();
  1693. }
  1694. }
  1695. //-----------------------------------------------------------------------------
  1696. bool SimObject::isSelectedRecursive() const
  1697. {
  1698. const SimObject *walk = this;
  1699. while ( walk )
  1700. {
  1701. if ( walk->isSelected() )
  1702. return true;
  1703. walk = walk->getGroup();
  1704. }
  1705. return false;
  1706. }
  1707. //-----------------------------------------------------------------------------
  1708. void SimObject::setLocked( bool b )
  1709. {
  1710. if( b )
  1711. mFlags.set( Locked );
  1712. else
  1713. mFlags.clear( Locked );
  1714. }
  1715. //-----------------------------------------------------------------------------
  1716. void SimObject::setHidden( bool b )
  1717. {
  1718. if( b )
  1719. mFlags.set( Hidden );
  1720. else
  1721. mFlags.clear( Hidden );
  1722. }
  1723. //-----------------------------------------------------------------------------
  1724. void SimObject::setCopySource( SimObject* object )
  1725. {
  1726. if( mCopySource )
  1727. mCopySource->unregisterReference( &mCopySource );
  1728. mCopySource = object;
  1729. if( mCopySource )
  1730. mCopySource->registerReference( &mCopySource );
  1731. }
  1732. //---------------------------------------------------------------------------
  1733. bool SimObject::_setCanSave( void* object, const char* index, const char* data )
  1734. {
  1735. SimObject* obj = reinterpret_cast< SimObject* >( object );
  1736. obj->setCanSave( dAtob( data ) );
  1737. return false;
  1738. }
  1739. //-----------------------------------------------------------------------------
  1740. const char* SimObject::_getCanSave( void* object, const char* data )
  1741. {
  1742. SimObject* obj = reinterpret_cast< SimObject* >( object );
  1743. if( obj->getCanSave() )
  1744. return "1";
  1745. else
  1746. return "0";
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. // Copy SimObject to another SimObject (Originally designed for T2D).
  1750. void SimObject::copyTo( SimObject* object )
  1751. {
  1752. object->mClassName = mClassName;
  1753. object->mSuperClassName = mSuperClassName;
  1754. linkNamespaces();
  1755. }
  1756. //-----------------------------------------------------------------------------
  1757. bool SimObject::setProtectedParent( void *obj, const char *index, const char *data )
  1758. {
  1759. SimGroup *parent = NULL;
  1760. SimObject *object = static_cast<SimObject*>(obj);
  1761. if(Sim::findObject(data, parent))
  1762. parent->addObject(object);
  1763. // always return false, because we've set mGroup when we called addObject
  1764. return false;
  1765. }
  1766. //-----------------------------------------------------------------------------
  1767. bool SimObject::setProtectedName(void *obj, const char *index, const char *data)
  1768. {
  1769. if (preventNameChanging)
  1770. return false;
  1771. SimObject* object = static_cast<SimObject*>(obj);
  1772. if (object->isProperlyAdded())
  1773. object->assignName(data);
  1774. // always return false because we assign the name here
  1775. return false;
  1776. }
  1777. //-----------------------------------------------------------------------------
  1778. bool SimObject::setInheritFrom(void* obj, const char* index, const char* data)
  1779. {
  1780. SimObject* object = static_cast<SimObject*>(obj);
  1781. SimObject* parent;
  1782. if (Sim::findObject(data, parent))
  1783. {
  1784. object->setCopySource(parent);
  1785. object->assignFieldsFrom(parent);
  1786. // copy any substitution statements
  1787. SimDataBlock* parent_db = dynamic_cast<SimDataBlock*>(parent);
  1788. if (parent_db)
  1789. {
  1790. SimDataBlock* currentNewObject_db = dynamic_cast<SimDataBlock*>(object);
  1791. if (currentNewObject_db)
  1792. currentNewObject_db->copySubstitutionsFrom(parent_db);
  1793. }
  1794. }
  1795. return true;
  1796. }
  1797. //-----------------------------------------------------------------------------
  1798. void SimObject::inspectPreApply()
  1799. {
  1800. }
  1801. //-----------------------------------------------------------------------------
  1802. void SimObject::inspectPostApply()
  1803. {
  1804. onInspectPostApply_callback(this);
  1805. }
  1806. //-----------------------------------------------------------------------------
  1807. String SimObject::_getLogMessage(const char* fmt, va_list args) const
  1808. {
  1809. String objClass = "UnknownClass";
  1810. if(getClassRep())
  1811. objClass = getClassRep()->getClassName();
  1812. String objName = getName();
  1813. if(objName.isEmpty())
  1814. objName = "Unnamed";
  1815. String formattedMessage = String::VToString(fmt, args);
  1816. return String::ToString("%s - %s(%i) - %s",
  1817. objClass.c_str(), objName.c_str(), getId(), formattedMessage.c_str());
  1818. }
  1819. //=============================================================================
  1820. // API.
  1821. //=============================================================================
  1822. // MARK: ---- API ----
  1823. //-----------------------------------------------------------------------------
  1824. DefineEngineMethod( SimObject, dumpGroupHierarchy, void, (),,
  1825. "Dump the hierarchy of this object up to RootGroup to the console." )
  1826. {
  1827. object->dumpGroupHierarchy();
  1828. }
  1829. //-----------------------------------------------------------------------------
  1830. DefineEngineMethod( SimObject, isMethod, bool, ( const char* methodName ),,
  1831. "Test whether the given method is defined on this object.\n"
  1832. "@param The name of the method.\n"
  1833. "@return True if the object implements the given method." )
  1834. {
  1835. return object->isMethod( methodName );
  1836. }
  1837. //-----------------------------------------------------------------------------
  1838. DefineEngineMethod( SimObject, isChildOfGroup, bool, ( SimGroup* group ),,
  1839. "Test whether the object belongs directly or indirectly to the given group.\n"
  1840. "@param group The SimGroup object.\n"
  1841. "@return True if the object is a child of the given group or a child of a group that the given group is directly or indirectly a child to." )
  1842. {
  1843. return object->isChildOfGroup( group );
  1844. }
  1845. //-----------------------------------------------------------------------------
  1846. DefineEngineMethod( SimObject, getClassNamespace, const char*, (),,
  1847. "Get the name of the class namespace assigned to this object.\n"
  1848. "@return The name of the 'class' namespace." )
  1849. {
  1850. return object->getClassNamespace();
  1851. }
  1852. //-----------------------------------------------------------------------------
  1853. DefineEngineMethod( SimObject, getSuperClassNamespace, const char*, (),,
  1854. "Get the name of the superclass namespace assigned to this object.\n"
  1855. "@return The name of the 'superClass' namespace." )
  1856. {
  1857. return object->getSuperClassNamespace();
  1858. }
  1859. //-----------------------------------------------------------------------------
  1860. DefineEngineMethod( SimObject, setClassNamespace, void, ( const char* name ),,
  1861. "Assign a class namespace to this object.\n"
  1862. "@param name The name of the 'class' namespace for this object." )
  1863. {
  1864. object->setClassNamespace( name );
  1865. }
  1866. //-----------------------------------------------------------------------------
  1867. DefineEngineMethod( SimObject, setSuperClassNamespace, void, ( const char* name ),,
  1868. "Assign a superclass namespace to this object.\n"
  1869. "@param name The name of the 'superClass' namespace for this object." )
  1870. {
  1871. object->setSuperClassNamespace( name );
  1872. }
  1873. //-----------------------------------------------------------------------------
  1874. DefineEngineMethod( SimObject, isSelected, bool, (),,
  1875. "Get whether the object has been marked as selected. (in editor)\n"
  1876. "@return True if the object is currently selected." )
  1877. {
  1878. return object->isSelected();
  1879. }
  1880. //-----------------------------------------------------------------------------
  1881. DefineEngineMethod( SimObject, setIsSelected, void, ( bool state ), ( true ),
  1882. "Set whether the object has been marked as selected. (in editor)\n"
  1883. "@param state True if object is to be marked selected; false if not." )
  1884. {
  1885. object->setSelected( state );
  1886. }
  1887. //-----------------------------------------------------------------------------
  1888. DefineEngineMethod( SimObject, isExpanded, bool, (),,
  1889. "Get whether the object has been marked as expanded. (in editor)\n"
  1890. "@return True if the object is marked expanded." )
  1891. {
  1892. return object->isExpanded();
  1893. }
  1894. //-----------------------------------------------------------------------------
  1895. DefineEngineMethod( SimObject, setIsExpanded, void, ( bool state ), ( true ),
  1896. "Set whether the object has been marked as expanded. (in editor)\n"
  1897. "@param state True if the object is to be marked expanded; false if not." )
  1898. {
  1899. object->setExpanded( state );
  1900. }
  1901. //-----------------------------------------------------------------------------
  1902. DefineEngineMethod( SimObject, getFilename, const char*, (),,
  1903. "Returns the filename the object is attached to.\n"
  1904. "@return The name of the file the object is associated with; usually the file the object was loaded from." )
  1905. {
  1906. return object->getFilename();
  1907. }
  1908. //-----------------------------------------------------------------------------
  1909. DefineEngineMethod( SimObject, setFilename, void, ( const char* fileName ),,
  1910. "Sets the object's file name and path\n"
  1911. "@param fileName The name of the file to associate this object with." )
  1912. {
  1913. return object->setFilename( fileName );
  1914. }
  1915. //-----------------------------------------------------------------------------
  1916. DefineEngineMethod( SimObject, getDeclarationLine, S32, (),,
  1917. "Get the line number at which the object is defined in its file.\n\n"
  1918. "@return The line number of the object's definition in script.\n"
  1919. "@see getFilename()")
  1920. {
  1921. return object->getDeclarationLine();
  1922. }
  1923. //-----------------------------------------------------------------------------
  1924. #ifdef TORQUE_DEBUG
  1925. static const char* sEnumCallbackFunction;
  1926. static void sEnumCallback( EngineObject* object )
  1927. {
  1928. SimObject* simObject = dynamic_cast< SimObject* >( object );
  1929. if( !simObject )
  1930. return;
  1931. Con::evaluatef( "%s( %i );", sEnumCallbackFunction, simObject->getId() );
  1932. }
  1933. DefineEngineFunction( debugEnumInstances, void, ( const char* className, const char* functionName ),,
  1934. "Call the given function for each instance of the given class.\n"
  1935. "@param className Name of the class for which to enumerate instances.\n"
  1936. "@param functionName Name of function to call and pass each instance of the given class.\n"
  1937. "@note This function is only available in debug builds and primarily meant as an aid in debugging."
  1938. "@ingroup Console")
  1939. {
  1940. sEnumCallbackFunction = functionName;
  1941. ConsoleObject::debugEnumInstances( className, sEnumCallback );
  1942. }
  1943. #endif
  1944. //-----------------------------------------------------------------------------
  1945. DefineEngineMethod( SimObject, assignFieldsFrom, void, ( SimObject* fromObject ),,
  1946. "Copy fields from another object onto this one. The objects must "
  1947. "be of same type. Everything from the object will overwrite what's "
  1948. "in this object; extra fields in this object will remain. This "
  1949. "includes dynamic fields.\n"
  1950. "@param fromObject The object from which to copy fields." )
  1951. {
  1952. if( fromObject )
  1953. object->assignFieldsFrom( fromObject );
  1954. }
  1955. //-----------------------------------------------------------------------------
  1956. DefineEngineMethod( SimObject, assignPersistentId, void, (),,
  1957. "Assign a persistent ID to the object if it does not already have one." )
  1958. {
  1959. object->getOrCreatePersistentId();
  1960. }
  1961. //-----------------------------------------------------------------------------
  1962. DefineEngineMethod( SimObject, getCanSave, bool, (),,
  1963. "Get whether the object will be included in saves.\n"
  1964. "@return True if the object will be saved; false otherwise." )
  1965. {
  1966. return object->getCanSave();
  1967. }
  1968. //-----------------------------------------------------------------------------
  1969. DefineEngineMethod( SimObject, setCanSave, void, ( bool value ), ( true ),
  1970. "Set whether the object will be included in saves.\n"
  1971. "@param value If true, the object will be included in saves; if false, it will be excluded." )
  1972. {
  1973. object->setCanSave( value );
  1974. }
  1975. //-----------------------------------------------------------------------------
  1976. DefineEngineMethod( SimObject, isEditorOnly, bool, (),,
  1977. "Return true if the object is only used by the editor.\n"
  1978. "@return True if this object exists only for the sake of editing." )
  1979. {
  1980. return object->isEditorOnly();
  1981. }
  1982. //-----------------------------------------------------------------------------
  1983. DefineEngineMethod( SimObject, setEditorOnly, void, ( bool value ), ( true ),
  1984. "Set/clear the editor-only flag on this object.\n"
  1985. "@param value If true, the object is marked as existing only for the editor." )
  1986. {
  1987. object->setEditorOnly( value );
  1988. }
  1989. //-----------------------------------------------------------------------------
  1990. DefineEngineMethod( SimObject, isNameChangeAllowed, bool, (),,
  1991. "Get whether this object may be renamed.\n"
  1992. "@return True if this object can be renamed; false otherwise." )
  1993. {
  1994. return object->isNameChangeAllowed();
  1995. }
  1996. //-----------------------------------------------------------------------------
  1997. DefineEngineMethod( SimObject, setNameChangeAllowed, void, ( bool value ), ( true ),
  1998. "Set whether this object can be renamed from its first name.\n"
  1999. "@param value If true, renaming is allowed for this object; if false, trying to change the name of the object will generate a console error." )
  2000. {
  2001. object->setNameChangeAllowed( value );
  2002. }
  2003. //-----------------------------------------------------------------------------
  2004. DefineEngineMethod( SimObject, clone, SimObject*, (),,
  2005. "Create a copy of this object.\n"
  2006. "@return An exact duplicate of this object." )
  2007. {
  2008. return object->clone();
  2009. }
  2010. //-----------------------------------------------------------------------------
  2011. DefineEngineMethod( SimObject, deepClone, SimObject*, (),,
  2012. "Create a copy of this object and all its subobjects.\n"
  2013. "@return An exact duplicate of this object and all objects it references." )
  2014. {
  2015. return object->deepClone();
  2016. }
  2017. //-----------------------------------------------------------------------------
  2018. DefineEngineMethod( SimObject, setLocked, void, ( bool value ), ( true ),
  2019. "Lock/unlock the object in the editor.\n"
  2020. "@param value If true, the object will be locked; if false, the object will be unlocked." )
  2021. {
  2022. object->setLocked( value );
  2023. }
  2024. //-----------------------------------------------------------------------------
  2025. DefineEngineMethod( SimObject, setHidden, void, ( bool value ), ( true ),
  2026. "Hide/unhide the object.\n"
  2027. "@param value If true, the object will be hidden; if false, the object will be unhidden." )
  2028. {
  2029. object->setHidden( value );
  2030. }
  2031. //-----------------------------------------------------------------------------
  2032. DefineEngineMethod( SimObject, dumpMethods, ArrayObject*, (),,
  2033. "List the methods defined on this object.\n\n"
  2034. "Each description is a newline-separated vector with the following elements:\n"
  2035. "- Minimum number of arguments.\n"
  2036. "- Maximum number of arguments.\n"
  2037. "- Prototype string.\n"
  2038. "- Full script file path (if script method).\n"
  2039. "- Line number of method definition in script (if script method).\n\n"
  2040. "- Documentation string (not including prototype). This takes up the remainder of the vector.\n"
  2041. "@return An ArrayObject populated with (name,description) pairs of all methods defined on the object." )
  2042. {
  2043. Namespace *ns = object->getNamespace();
  2044. if( !ns )
  2045. return 0;
  2046. ArrayObject* dictionary = new ArrayObject();
  2047. dictionary->registerObject();
  2048. VectorPtr<Namespace::Entry *> vec(__FILE__, __LINE__);
  2049. ns->getEntryList(&vec);
  2050. for(Vector< Namespace::Entry* >::iterator j = vec.begin(); j != vec.end(); j++)
  2051. {
  2052. Namespace::Entry* e = *j;
  2053. if( e->mType < 0 )
  2054. continue;
  2055. StringBuilder str;
  2056. str.append( String::ToString( e->mMinArgs ) );
  2057. str.append( '\n' );
  2058. str.append( String::ToString( e->mMaxArgs ) );
  2059. str.append( '\n' );
  2060. str.append( e->getPrototypeString() );
  2061. str.append( '\n' );
  2062. if( e->mCode && e->mCode->fullPath )
  2063. str.append( e->mCode->fullPath );
  2064. str.append( '\n' );
  2065. if( e->mCode )
  2066. str.append( String::ToString( e->mFunctionLineNumber ) );
  2067. str.append( '\n' );
  2068. String docs = e->getDocString();
  2069. if( !docs.isEmpty() )
  2070. str.append( docs );
  2071. dictionary->push_back( e->mFunctionName, str.end() );
  2072. }
  2073. return dictionary;
  2074. }
  2075. //-----------------------------------------------------------------------------
  2076. namespace {
  2077. S32 QSORT_CALLBACK compareFields( const void* a,const void* b )
  2078. {
  2079. const AbstractClassRep::Field* fa = *((const AbstractClassRep::Field**)a);
  2080. const AbstractClassRep::Field* fb = *((const AbstractClassRep::Field**)b);
  2081. return dStricmp(fa->pFieldname, fb->pFieldname);
  2082. }
  2083. struct DocString
  2084. {
  2085. char mPadding[ 8 ];
  2086. String mPrototype;
  2087. String mDescription;
  2088. const char* mReturnType;
  2089. DocString( Namespace::Entry* entry )
  2090. : mPrototype( entry->getArgumentsString() ),
  2091. mDescription( entry->getBriefDescription() )
  2092. {
  2093. mReturnType = " ";
  2094. mPadding[ 0 ] = 0;
  2095. if( entry->mType == -4 )
  2096. {
  2097. //TODO: need to have script callbacks set up proper return type info
  2098. }
  2099. else
  2100. {
  2101. switch( entry->mType )
  2102. {
  2103. case Namespace::Entry::StringCallbackType:
  2104. mReturnType = "string";
  2105. mPadding[ 0 ] = ' ';
  2106. mPadding[ 1 ] = ' ';
  2107. mPadding[ 2 ] = 0;
  2108. break;
  2109. case Namespace::Entry::IntCallbackType:
  2110. mReturnType = "int";
  2111. mPadding[ 0 ] = ' ';
  2112. mPadding[ 1 ] = ' ';
  2113. mPadding[ 2 ] = ' ';
  2114. mPadding[ 3 ] = ' ';
  2115. mPadding[ 4 ] = ' ';
  2116. mPadding[ 5 ] = 0;
  2117. break;
  2118. case Namespace::Entry::FloatCallbackType:
  2119. mReturnType = "float";
  2120. mPadding[ 0 ] = ' ';
  2121. mPadding[ 1 ] = ' ';
  2122. mPadding[ 2 ] = ' ';
  2123. mPadding[ 3 ] = 0;
  2124. break;
  2125. case Namespace::Entry::VoidCallbackType:
  2126. mReturnType = "void";
  2127. mPadding[ 0 ] = ' ';
  2128. mPadding[ 1 ] = ' ';
  2129. mPadding[ 2 ] = ' ';
  2130. mPadding[ 3 ] = ' ';
  2131. mPadding[ 4 ] = 0;
  2132. break;
  2133. case Namespace::Entry::BoolCallbackType:
  2134. mReturnType = "bool";
  2135. mPadding[ 0 ] = ' ';
  2136. mPadding[ 1 ] = ' ';
  2137. mPadding[ 2 ] = ' ';
  2138. mPadding[ 3 ] = ' ';
  2139. mPadding[ 4 ] = 0;
  2140. break;
  2141. }
  2142. }
  2143. }
  2144. };
  2145. }
  2146. DefineEngineMethod( SimObject, dump, void, ( bool detailed ), ( false ),
  2147. "Dump a description of all fields and methods defined on this object to the console.\n"
  2148. "@param detailed Whether to print detailed information about members." )
  2149. {
  2150. Con::printf( "Class: %s", object->getClassName() );
  2151. const AbstractClassRep::FieldList &list = object->getFieldList();
  2152. char expandedBuffer[4096];
  2153. Con::printf( "Static Fields:" );
  2154. Vector<const AbstractClassRep::Field *> flist(__FILE__, __LINE__);
  2155. for(U32 i = 0; i < list.size(); i++)
  2156. flist.push_back(&list[i]);
  2157. dQsort(flist.address(),flist.size(),sizeof(AbstractClassRep::Field *),compareFields);
  2158. for(Vector<const AbstractClassRep::Field *>::iterator itr = flist.begin(); itr != flist.end(); itr++)
  2159. {
  2160. const AbstractClassRep::Field* f = *itr;
  2161. // The special field types can be skipped.
  2162. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  2163. continue;
  2164. for(U32 j = 0; S32(j) < f->elementCount; j++)
  2165. {
  2166. // [neo, 07/05/2007 - #3000]
  2167. // Some objects use dummy vars and projected fields so make sure we call the get functions
  2168. //const char *val = Con::getData(f->type, (void *) (((const char *)object) + f->offset), j, f->table, f->flag);
  2169. const char *val = (*f->getDataFn)( object, Con::getData(f->type, (void *) (((const char *)object) + f->offset), j, f->table, f->flag) );// + typeSizes[fld.type] * array1));
  2170. ConsoleBaseType* conType = ConsoleBaseType::getType( f->type );
  2171. const char* conTypeName = "<unknown>";
  2172. if( conType )
  2173. conTypeName = conType->getTypeClassName();
  2174. if( !val /*|| !*val*/ )
  2175. continue;
  2176. if( f->elementCount == 1 )
  2177. dSprintf( expandedBuffer, sizeof( expandedBuffer ), " %s %s = \"", conTypeName, f->pFieldname );
  2178. else
  2179. dSprintf( expandedBuffer, sizeof( expandedBuffer ), " %s %s[ %d ] = \"", conTypeName, f->pFieldname, j );
  2180. expandEscape( expandedBuffer + dStrlen(expandedBuffer), val);
  2181. Con::printf( "%s\"", expandedBuffer );
  2182. if( detailed && f->pFieldDocs && f->pFieldDocs[ 0 ] )
  2183. Con::printf( " %s", f->pFieldDocs );
  2184. }
  2185. }
  2186. // If the object is a datablock with substitution statements,
  2187. // they get printed out as part of the dump.
  2188. if (dynamic_cast<SimDataBlock*>(object))
  2189. {
  2190. if (((SimDataBlock*)object)->getSubstitutionCount() > 0)
  2191. {
  2192. Con::printf("Substitution Fields:");
  2193. ((SimDataBlock*)object)->printSubstitutions();
  2194. }
  2195. }
  2196. Con::printf( "Dynamic Fields:" );
  2197. if(object->getFieldDictionary())
  2198. object->getFieldDictionary()->printFields(object);
  2199. Con::printf( "Methods:" );
  2200. Namespace *ns = object->getNamespace();
  2201. VectorPtr<Namespace::Entry *> vec(__FILE__, __LINE__);
  2202. if(ns)
  2203. ns->getEntryList(&vec);
  2204. bool sawCBs = false;
  2205. for(Vector<Namespace::Entry *>::iterator j = vec.begin(); j != vec.end(); j++)
  2206. {
  2207. Namespace::Entry *e = *j;
  2208. if(e->mType == Namespace::Entry::ScriptCallbackType)
  2209. sawCBs = true;
  2210. if(e->mType < 0)
  2211. continue;
  2212. DocString doc( e );
  2213. Con::printf( " %s%s%s%s", doc.mReturnType, doc.mPadding, e->mFunctionName, doc.mPrototype.c_str() );
  2214. if( detailed && !doc.mDescription.isEmpty() )
  2215. Con::printf( " %s", doc.mDescription.c_str() );
  2216. }
  2217. if( sawCBs )
  2218. {
  2219. Con::printf( "Callbacks:" );
  2220. for(Vector<Namespace::Entry *>::iterator j = vec.begin(); j != vec.end(); j++)
  2221. {
  2222. Namespace::Entry *e = *j;
  2223. if(e->mType != Namespace::Entry::ScriptCallbackType)
  2224. continue;
  2225. DocString doc( e );
  2226. Con::printf( " %s%s%s%s", doc.mReturnType, doc.mPadding, e->cb.mCallbackName, doc.mPrototype.c_str() );
  2227. if( detailed && !doc.mDescription.isEmpty() )
  2228. Con::printf( " %s", doc.mDescription.c_str() );
  2229. }
  2230. }
  2231. }
  2232. //-----------------------------------------------------------------------------
  2233. DefineEngineMethod( SimObject, save, bool, ( const char* fileName, bool selectedOnly, const char* preAppendString ), ( false, "" ),
  2234. "Save out the object to the given file.\n"
  2235. "@param fileName The name of the file to save to."
  2236. "@param selectedOnly If true, only objects marked as selected will be saved out.\n"
  2237. "@param preAppendString Text which will be preprended directly to the object serialization.\n"
  2238. "@param True on success, false on failure." )
  2239. {
  2240. return object->save( fileName, selectedOnly, preAppendString );
  2241. }
  2242. //-----------------------------------------------------------------------------
  2243. DefineEngineMethod( SimObject, setName, void, ( const char* newName ),,
  2244. "Set the global name of the object.\n"
  2245. "@param newName The new global name to assign to the object.\n"
  2246. "@note If name changing is disallowed on the object, the method will fail with a console error." )
  2247. {
  2248. object->assignName( newName );
  2249. }
  2250. //-----------------------------------------------------------------------------
  2251. DefineEngineMethod( SimObject, getName, const char*, (),,
  2252. "Get the global name of the object.\n"
  2253. "@return The global name assigned to the object." )
  2254. {
  2255. const char *ret = object->getName();
  2256. return ret ? ret : "";
  2257. }
  2258. //-----------------------------------------------------------------------------
  2259. DefineEngineMethod( SimObject, getClassName, const char*, (),,
  2260. "Get the name of the C++ class which the object is an instance of.\n"
  2261. "@return The name of the C++ class of the object." )
  2262. {
  2263. const char *ret = object->getClassName();
  2264. return ret ? ret : "";
  2265. }
  2266. //-----------------------------------------------------------------------------
  2267. DefineEngineMethod( SimObject, isField, bool, ( const char* fieldName ),,
  2268. "Test whether the given field is defined on this object.\n"
  2269. "@param fieldName The name of the field.\n"
  2270. "@return True if the object implements the given field." )
  2271. {
  2272. return object->isField( fieldName );
  2273. }
  2274. //-----------------------------------------------------------------------------
  2275. DefineEngineMethod( SimObject, getFieldValue, const char*, ( const char* fieldName, S32 index ), ( -1 ),
  2276. "Return the value of the given field on this object.\n"
  2277. "@param fieldName The name of the field. If it includes a field index, the index is parsed out.\n"
  2278. "@param index Optional parameter to specify the index of an array field separately.\n"
  2279. "@return The value of the given field or \"\" if undefined." )
  2280. {
  2281. const U32 nameLen = dStrlen( fieldName );
  2282. if (nameLen == 0)
  2283. return "";
  2284. char fieldNameBuffer[ 1024 ];
  2285. char arrayIndexBuffer[ 64 ];
  2286. // Parse out index if the field is given in the form of 'name[index]'.
  2287. const char* arrayIndex = NULL;
  2288. if( fieldName[ nameLen - 1 ] == ']' )
  2289. {
  2290. const char* leftBracket = dStrchr( fieldName, '[' );
  2291. const char* rightBracket = &fieldName[ nameLen - 1 ];
  2292. const U32 fieldNameLen = getMin( U32( leftBracket - fieldName ), sizeof( fieldNameBuffer ) - 1 );
  2293. const U32 arrayIndexLen = getMin( U32( rightBracket - leftBracket - 1 ), sizeof( arrayIndexBuffer ) - 1 );
  2294. dMemcpy( fieldNameBuffer, fieldName, fieldNameLen );
  2295. dMemcpy( arrayIndexBuffer, leftBracket + 1, arrayIndexLen );
  2296. fieldNameBuffer[ fieldNameLen ] = '\0';
  2297. arrayIndexBuffer[ arrayIndexLen ] = '\0';
  2298. fieldName = fieldNameBuffer;
  2299. arrayIndex = arrayIndexBuffer;
  2300. }
  2301. fieldName = StringTable->insert( fieldName );
  2302. if( index != -1 )
  2303. {
  2304. dSprintf( arrayIndexBuffer, sizeof( arrayIndexBuffer ), "%i", index );
  2305. arrayIndex = arrayIndexBuffer;
  2306. }
  2307. return object->getDataField( fieldName, arrayIndex );
  2308. }
  2309. //-----------------------------------------------------------------------------
  2310. DefineEngineMethod( SimObject, setFieldValue, bool, ( const char* fieldName, const char* value, S32 index ), ( -1 ),
  2311. "Set the value of the given field on this object.\n"
  2312. "@param fieldName The name of the field to assign to. If it includes an array index, the index will be parsed out.\n"
  2313. "@param value The new value to assign to the field.\n"
  2314. "@param index Optional argument to specify an index for an array field.\n"
  2315. "@return True." )
  2316. {
  2317. char fieldNameBuffer[ 1024 ];
  2318. char arrayIndexBuffer[ 64 ];
  2319. // Parse out index if the field is given in the form of 'name[index]'.
  2320. const char* arrayIndex = NULL;
  2321. const U32 nameLen = dStrlen( fieldName );
  2322. if( fieldName[ nameLen - 1 ] == ']' )
  2323. {
  2324. const char* leftBracket = dStrchr( fieldName, '[' );
  2325. const char* rightBracket = &fieldName[ nameLen - 1 ];
  2326. const U32 fieldNameLen = getMin( U32( leftBracket - fieldName ), sizeof( fieldNameBuffer ) - 1 );
  2327. const U32 arrayIndexLen = getMin( U32( rightBracket - leftBracket - 1 ), sizeof( arrayIndexBuffer ) - 1 );
  2328. dMemcpy( fieldNameBuffer, fieldName, fieldNameLen );
  2329. dMemcpy( arrayIndexBuffer, leftBracket + 1, arrayIndexLen );
  2330. fieldNameBuffer[ fieldNameLen ] = '\0';
  2331. arrayIndexBuffer[ arrayIndexLen ] = '\0';
  2332. fieldName = fieldNameBuffer;
  2333. arrayIndex = arrayIndexBuffer;
  2334. }
  2335. fieldName = StringTable->insert( fieldName );
  2336. if( index != -1 )
  2337. {
  2338. dSprintf( arrayIndexBuffer, sizeof( arrayIndexBuffer ), "%i", index );
  2339. arrayIndex = arrayIndexBuffer;
  2340. }
  2341. object->setDataField( fieldName, arrayIndex, value );
  2342. return true;
  2343. }
  2344. //-----------------------------------------------------------------------------
  2345. DefineEngineMethod( SimObject, getFieldType, const char*, ( const char* fieldName ),,
  2346. "Get the console type code of the given field.\n"
  2347. "@return The numeric type code for the underlying console type of the given field." )
  2348. {
  2349. U32 typeID = object->getDataFieldType( StringTable->insert( fieldName ), NULL );
  2350. ConsoleBaseType* type = ConsoleBaseType::getType( typeID );
  2351. if( type )
  2352. return type->getTypeName();
  2353. return "";
  2354. }
  2355. //-----------------------------------------------------------------------------
  2356. DefineEngineMethod( SimObject, setFieldType, void, ( const char* fieldName, const char* type ),,
  2357. "Set the console type code for the given field.\n"
  2358. "@param fieldName The name of the dynamic field to change to type for.\n"
  2359. "@param type The name of the console type.\n"
  2360. "@note This only works for dynamic fields. Types of static fields cannot be changed." )
  2361. {
  2362. object->setDataFieldType( type, StringTable->insert( fieldName ), NULL );
  2363. }
  2364. //-----------------------------------------------------------------------------
  2365. DefineEngineStringlyVariadicMethod( SimObject, call, const char*, 3, 0, "( string method, string args... ) Dynamically call a method on an object.\n"
  2366. "@param method Name of method to call.\n"
  2367. "@param args Zero or more arguments for the method.\n"
  2368. "@return The result of the method call." )
  2369. {
  2370. argv[1].setString(argv[2].getString());
  2371. ConsoleValue returnValue = Con::execute(object, argc - 1, argv + 1);
  2372. return Con::getReturnBuffer(returnValue.getString());
  2373. }
  2374. //-----------------------------------------------------------------------------
  2375. DefineEngineMethod( SimObject, setInternalName, void, ( const char* newInternalName ),,
  2376. "Set the internal name of the object.\n"
  2377. "@param newInternalName The new internal name for the object." )
  2378. {
  2379. object->setInternalName( newInternalName );
  2380. }
  2381. //-----------------------------------------------------------------------------
  2382. DefineEngineMethod( SimObject, getInternalName, const char*, (),,
  2383. "Get the internal name of the object.\n"
  2384. "@return The internal name of the object." )
  2385. {
  2386. return object->getInternalName();
  2387. }
  2388. //-----------------------------------------------------------------------------
  2389. DefineEngineMethod( SimObject, dumpClassHierarchy, void, (),,
  2390. "Dump the native C++ class hierarchy of this object's C++ class to the console." )
  2391. {
  2392. object->dumpClassHierarchy();
  2393. }
  2394. //-----------------------------------------------------------------------------
  2395. DefineEngineMethod( SimObject, isMemberOfClass, bool, ( const char* className ),,
  2396. "Test whether this object is a member of the specified class.\n"
  2397. "@param className Name of a native C++ class.\n"
  2398. "@return True if this object is an instance of the given C++ class or any of its super classes." )
  2399. {
  2400. AbstractClassRep* pRep = object->getClassRep();
  2401. while(pRep)
  2402. {
  2403. if( !dStricmp(pRep->getClassName(), className ) )
  2404. {
  2405. //matches
  2406. return true;
  2407. }
  2408. pRep = pRep->getParentClass();
  2409. }
  2410. return false;
  2411. }
  2412. //-----------------------------------------------------------------------------
  2413. DefineEngineMethod( SimObject, isInNamespaceHierarchy, bool, ( const char* name ),,
  2414. "Test whether the namespace of this object is a direct or indirect child to the given namespace.\n"
  2415. "@param name The name of a namespace.\n"
  2416. "@return True if the given namespace name is within the namespace hierarchy of this object." )
  2417. {
  2418. Namespace* nspace = object->getNamespace();
  2419. while( nspace && dStricmp( nspace->mName, name ) != 0 )
  2420. nspace = nspace->mParent;
  2421. return ( nspace != NULL );
  2422. }
  2423. //-----------------------------------------------------------------------------
  2424. DefineEngineMethod( SimObject, getId, S32, (),,
  2425. "Get the underlying unique numeric ID of the object.\n"
  2426. "@note Object IDs are unique only during single engine runs.\n"
  2427. "@return The unique numeric ID of the object." )
  2428. {
  2429. return object->getId();
  2430. }
  2431. //-----------------------------------------------------------------------------
  2432. DefineEngineMethod( SimObject, getGroup, SimGroup*, (),,
  2433. "Get the group that this object is contained in.\n"
  2434. "@note If not assigned to particular SimGroup, an object belongs to RootGroup.\n"
  2435. "@return The SimGroup object to which the object belongs." )
  2436. {
  2437. return object->getGroup();
  2438. }
  2439. //-----------------------------------------------------------------------------
  2440. DefineEngineMethod( SimObject, delete, void, (),,
  2441. "Delete and remove the object." )
  2442. {
  2443. object->deleteObject();
  2444. }
  2445. //-----------------------------------------------------------------------------
  2446. DefineEngineStringlyVariadicMethod( SimObject,schedule, S32, 4, 0, "( float time, string method, string args... ) Delay an invocation of a method.\n"
  2447. "@param time The number of milliseconds after which to invoke the method. This is a soft limit.\n"
  2448. "@param method The method to call.\n"
  2449. "@param args The arguments with which to call the method.\n"
  2450. "@return The numeric ID of the created schedule. Can be used to cancel the call.\n" )
  2451. {
  2452. U32 timeDelta = U32(argv[2].getFloat());
  2453. argv[2].setString(argv[3].getString());
  2454. argv[3].setString(argv[1].getString());
  2455. SimConsoleEvent *evt = new SimConsoleEvent(argc - 2, argv + 2, true);
  2456. S32 ret = Sim::postEvent(object, evt, Sim::getCurrentTime() + timeDelta);
  2457. // #ifdef DEBUG
  2458. // Con::printf("obj %s schedule(%s) = %d", argv[3], argv[2], ret);
  2459. // Con::executef( "backtrace");
  2460. // #endif
  2461. return ret;
  2462. }
  2463. //-----------------------------------------------------------------------------
  2464. DefineEngineMethod( SimObject, getDynamicFieldCount, S32, (),,
  2465. "Get the number of dynamic fields defined on the object.\n"
  2466. "@return The number of dynamic fields defined on the object." )
  2467. {
  2468. S32 count = 0;
  2469. SimFieldDictionary* fieldDictionary = object->getFieldDictionary();
  2470. for (SimFieldDictionaryIterator itr(fieldDictionary); *itr; ++itr)
  2471. count++;
  2472. return count;
  2473. }
  2474. //-----------------------------------------------------------------------------
  2475. DefineEngineMethod( SimObject, getDynamicField, const char*, ( S32 index ),,
  2476. "Get a value of a dynamic field by index.\n"
  2477. "@param index The index of the dynamic field.\n"
  2478. "@return The value of the dynamic field at the given index or \"\"." )
  2479. {
  2480. SimFieldDictionary* fieldDictionary = object->getFieldDictionary();
  2481. SimFieldDictionaryIterator itr(fieldDictionary);
  2482. for (S32 i = 0; i < index; i++)
  2483. {
  2484. if (!(*itr))
  2485. {
  2486. Con::warnf("Invalid dynamic field index passed to SimObject::getDynamicField!");
  2487. return NULL;
  2488. }
  2489. ++itr;
  2490. }
  2491. static const U32 bufSize = 256;
  2492. char* buffer = Con::getReturnBuffer(bufSize);
  2493. if (*itr)
  2494. {
  2495. SimFieldDictionary::Entry* entry = *itr;
  2496. dSprintf(buffer, bufSize, "%s\t%s", entry->slotName, entry->value);
  2497. return buffer;
  2498. }
  2499. Con::warnf("Invalid dynamic field index passed to SimObject::getDynamicField!");
  2500. return NULL;
  2501. }
  2502. //-----------------------------------------------------------------------------
  2503. DefineEngineMethod( SimObject, getFieldCount, S32, (),,
  2504. "Get the number of static fields on the object.\n"
  2505. "@return The number of static fields defined on the object." )
  2506. {
  2507. const AbstractClassRep::FieldList &list = object->getFieldList();
  2508. const AbstractClassRep::Field* f;
  2509. U32 numDummyEntries = 0;
  2510. for(S32 i = 0; i < list.size(); i++)
  2511. {
  2512. f = &list[i];
  2513. // The special field types do not need to be counted.
  2514. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  2515. numDummyEntries++;
  2516. }
  2517. return list.size() - numDummyEntries;
  2518. }
  2519. //-----------------------------------------------------------------------------
  2520. DefineEngineMethod( SimObject, getField, const char*, ( S32 index ),,
  2521. "Retrieve the value of a static field by index.\n"
  2522. "@param index The index of the static field.\n"
  2523. "@return The value of the static field with the given index or \"\"." )
  2524. {
  2525. const AbstractClassRep::FieldList &list = object->getFieldList();
  2526. if( ( index < 0 ) || ( index >= list.size() ) )
  2527. return "";
  2528. const AbstractClassRep::Field* f;
  2529. S32 currentField = 0;
  2530. for ( U32 i = 0; i < list.size() && currentField <= index; i++ )
  2531. {
  2532. f = &list[i];
  2533. // The special field types can be skipped.
  2534. if ( f->type >= AbstractClassRep::ARCFirstCustomField )
  2535. continue;
  2536. if(currentField == index)
  2537. return f->pFieldname;
  2538. currentField++;
  2539. }
  2540. // if we found nada, return nada.
  2541. return "";
  2542. }
  2543. DefineEngineFunction(getClassHierarchy, const char*, (const char* name), ,
  2544. "Returns the inheritance hierarchy for a given class.")
  2545. {
  2546. AbstractClassRep* pRep = AbstractClassRep::findClassRep(name);
  2547. if (!pRep)
  2548. {
  2549. //Con::errorf("%s does not exist", name);
  2550. return StringTable->EmptyString();
  2551. }
  2552. StringBuilder buffer;
  2553. while (pRep != NULL)
  2554. {
  2555. StringTableEntry className = pRep->getClassName();
  2556. buffer.append(className);
  2557. buffer.append(" ");
  2558. pRep = pRep->getParentClass();
  2559. }
  2560. String result = buffer.end().trim();
  2561. //Con::printf("getClassHierarchy for %s=%s", name, result.c_str());
  2562. return Con::getReturnBuffer(result.c_str());
  2563. }
  2564. //-----------------------------------------------------------------------------
  2565. #ifdef TORQUE_DEBUG
  2566. DefineEngineMethod( SimObject, getDebugInfo, ArrayObject*, (),,
  2567. "Return some behind-the-scenes information on the object.\n"
  2568. "@return An ArrayObject filled with internal information about the object." )
  2569. {
  2570. ArrayObject* array = new ArrayObject();
  2571. array->registerObject();
  2572. array->push_back( "C++|Address", String::ToString( "0x%x", object ) );
  2573. array->push_back( "C++|Size", String::ToString( object->getClassRep()->getSizeof() ) );
  2574. array->push_back( "Object|Description", object->describeSelf() );
  2575. array->push_back( "Object|FileName", object->getFilename() );
  2576. array->push_back( "Object|DeclarationLine", String::ToString( object->getDeclarationLine() ) );
  2577. array->push_back( "Object|CopySource", object->getCopySource() ?
  2578. String::ToString( "%i:%s (%s)", object->getCopySource()->getId(), object->getCopySource()->getClassName(), object->getCopySource()->getName() ) : "" );
  2579. array->push_back( "Flag|EditorOnly", object->isEditorOnly() ? "true" : "false" );
  2580. array->push_back( "Flag|NameChangeAllowed", object->isNameChangeAllowed() ? "true" : "false" );
  2581. array->push_back( "Flag|AutoDelete", object->isAutoDeleted() ? "true" : "false" );
  2582. array->push_back( "Flag|Selected", object->isSelected() ? "true" : "false" );
  2583. array->push_back( "Flag|Expanded", object->isExpanded() ? "true" : "false" );
  2584. array->push_back( "Flag|ModStaticFields", object->canModStaticFields() ? "true" : "false" );
  2585. array->push_back( "Flag|ModDynamicFields", object->canModDynamicFields() ? "true" : "false" );
  2586. array->push_back( "Flag|CanSave", object->getCanSave() ? "true" : "false" );
  2587. #ifndef TORQUE_DISABLE_MEMORY_MANAGER
  2588. Memory::Info memInfo;
  2589. Memory::getMemoryInfo( object, memInfo );
  2590. array->push_back( "Memory|AllocNumber", String::ToString( memInfo.mAllocNumber ) );
  2591. array->push_back( "Memory|AllocSize", String::ToString( memInfo.mAllocSize ) );
  2592. array->push_back( "Memory|AllocFile", memInfo.mFileName );
  2593. array->push_back( "Memory|AllocLine", String::ToString( memInfo.mLineNumber ) );
  2594. array->push_back( "Memory|IsGlobal", memInfo.mIsGlobal ? "true" : "false" );
  2595. array->push_back( "Memory|IsStatic", memInfo.mIsStatic ? "true" : "false" );
  2596. #endif
  2597. return array;
  2598. }
  2599. #endif