ShadowMob pullNewOrCached( CanonMob cm ) { if( _objects.ContainsKey( cm.id ) ) return _objects[cm.id]; ShadowMob sm = new ShadowMob( this, cm ); _objects[cm.id] = sm; return sm; }
public CanonMob createObject() { int id = Interlocked.Increment( ref _nextId ); if( !_readOnly ) _wdb.setConfigInt( World.ConfigNextId, _nextId ); CanonMob newMob = new CanonMob( this, id ); if( !_readOnly ) _wdb.saveMob( new Mob( newMob ) ); _objects[id] = newMob; return newMob; }
/// <summary> /// Callback for CanonMobs if their pulsefreq attribute is changed. /// </summary> public void pulseCheck( CanonMob m ) { if( _pulse != null ) _pulse.pulseCheck( m.id ); }
/// <summary> /// Loads the specified Mob from the current checkpoint. /// </summary> /// <param name="objectId">The Mob's object ID (not database ID)</param> /// <param name="world">The World object to attach this Mob to</param> /// <returns> /// The loaded Mob, or null if it doesn't exist in this checkpoint. /// </returns> public CanonMob loadMob( int objectId, CanonWorld world ) { // We don't need to write anything here, but the transaction may give us reader semantics too. lock( _lock ) using( var token = _db.token() ) using( var trans = _db.transaction( token ) ) { // Get the current checkpoint ID. ulong curCheckpoint = getLatestCheckpoint( token ); // Find the existing object in the MobTable. IEnumerable<DBMobTable> results = _db.select( token, new DBMobTable() { objectId = objectId, checkpoint = curCheckpoint }, new string[] { "objectId", "checkpoint" } ); if( !results.Any() ) return null; ulong mobDbId = results.First().mob; // Get the mob itself. IEnumerable<DBMob> mobs = _db.select( token, new DBMob() { id = mobDbId }, new string[] { "id" } ); if( !results.Any() ) throw new ArgumentException( "Database error: Mob is in mobtable, but non-existant" ); DBMob mob = mobs.First(); // Look for all of its attributes. IEnumerable<DBAttr> attrs = _db.select( token, new DBAttr() { mob = mobDbId }, new string[] { "mob" } ); // And all of its verbs. IEnumerable<DBVerb> verbs = _db.select( token, new DBVerb() { mob = mobDbId }, new string[] { "mob" } ); // Now put it all together into a world object. CanonMob cm = new CanonMob( world, objectId ); Mob m = Mob.Wrap( cm ); m.parentId = mob.parent ?? 0; m.locationId = mob.location ?? 0; m.ownerId = mob.owner; m.pathId = mob.pathId ?? null; foreach( DBAttr attr in attrs ) { AttributeSerialized ser = new AttributeSerialized() { mimetype = attr.mime, binvalue = attr.data, strvalue = attr.text }; var ta = TypedAttribute.FromSerialized( ser ); m.attrSet(attr.name, ta); } foreach( DBVerb verb in verbs ) { Verb v = new Verb() { name = verb.name, code = verb.code, }; m.verbSet(verb.name, v); } return cm; } }