public void TestUnavailableValue() { var funcCalled = false; Action<Action> assertThatFuncIsCalled = action => { Assert.IsFalse(funcCalled); action(); Assert.IsTrue(funcCalled); funcCalled = false; }; var importantNumber = 42; var proxy = new LazyProxy<int, string>(x => { funcCalled = true; return importantNumber < 50 ? null : x.ToString(); }); Assert.AreEqual(null, proxy.GetValue()); proxy.SetData(3); assertThatFuncIsCalled(() => Assert.AreEqual(null, proxy.GetValue())); for (int i = 0; i < LazyProxy<int, string>.NULL_WAIT_RETRY_COUNT; i++) Assert.AreEqual(null, proxy.GetValue()); importantNumber = 69; assertThatFuncIsCalled(() => Assert.AreEqual("3", proxy.GetValue())); importantNumber = 0; // doesn't affect GetValue any more Assert.AreEqual("3", proxy.GetValue()); Assert.IsFalse(funcCalled); }
public void TestLazyString() { var proxy = new LazyProxy<int, string>(x => new System.Text.StringBuilder().Append('x', x).ToString()); Assert.AreEqual(null, proxy.GetValue()); proxy.SetData(5); string proxyValue = proxy; Assert.AreEqual("xxxxx", proxyValue); }
public void TestSetDataTwice() { var proxy = new LazyProxy<int, string>(x => new System.Text.StringBuilder().Append('x', x).ToString()); proxy.SetData(5); Assert.Throws<InvalidOperationException>(() => proxy.SetData(6)); }
public override void Deserialize(NetworkBinaryReader reader, SerializationModeFlags mode, int framesAgo) { var reducedMode = mode.HasFlag(SerializationModeFlags.ConstantDataFromServer) ? SerializationModeFlags.AllFromServer : SerializationModeFlags.None; base.Deserialize(reader, reducedMode, framesAgo); if (mode.HasFlag(SerializationModeFlags.VaryingDataFromServer)) { var repairingCount = reader.ReadByte(); _repairingGobsOnClient.Clear(); for (int i = 0; i < repairingCount; i++) { var proxy = new LazyProxy<int, Gob>(FindGob); proxy.SetData(reader.ReadInt16()); _repairingGobsOnClient.Add(proxy); } } }
public void TestEager() { var proxy = new LazyProxy<int, string>("6"); Assert.AreEqual("6", proxy.GetValue()); Assert.Throws<InvalidOperationException>(() => proxy.SetData(42)); }
public LazyProxy<int, Spectator> ReadSpectatorID(Func<int, Spectator> findSpectator) { int spectatorID = ReadSByte(); var proxy = new LazyProxy<int, Spectator>(findSpectator); proxy.SetData(spectatorID); return proxy; }
public LazyProxy<int, Team> ReadTeamID(Func<int, Team> findTeam) { int teamID = ReadSByte(); var proxy = new LazyProxy<int, Team>(findTeam); proxy.SetData(teamID); return proxy; }
public override void Deserialize(NetworkBinaryReader reader, SerializationModeFlags mode, int framesAgo) { // Note: Gob.{Pos,Rotation,Move,RotationSpeed} are computed from Host // and don't need to be serialized except at birth. var baseSerializationMode = mode.HasFlag(SerializationModeFlags.ConstantDataFromServer) ? mode : mode & ~SerializationModeFlags.VaryingDataFromServer; base.Deserialize(reader, baseSerializationMode, framesAgo); if (mode.HasFlag(SerializationModeFlags.ConstantDataFromServer)) { int hostID = reader.ReadInt16(); _hostProxy = new LazyProxy<int, Gob>(FindGob); _hostProxy.SetData(hostID); HostBoneIndex = reader.ReadByte(); } if (mode.HasFlag(SerializationModeFlags.VaryingDataFromServer)) { int wallPunchCount = reader.ReadByte(); var punchedPoses = new List<Vector2>(wallPunchCount); for (int i = 0; i < wallPunchCount; i++) punchedPoses.Add(reader.ReadHalfVector2()); foreach (var pos in punchedPoses) GobHelper.CreateGobs(_wallPunchEffects, Arena, pos); PlayWallHitSound(punchedPoses); } }
public override void Deserialize(NetworkBinaryReader reader, SerializationModeFlags mode, int framesAgo) { base.Deserialize(reader, mode, framesAgo); if ((mode & SerializationModeFlags.VaryingDataFromServer) != 0) { int targetID = reader.ReadInt16(); _targetProxy = new LazyProxy<int, Gob>(FindGob); _targetProxy.SetData(targetID); } }
public override void Deserialize(NetworkBinaryReader reader, SerializationModeFlags mode, int framesAgo) { base.Deserialize(reader, mode, framesAgo); if (mode.HasFlag(SerializationModeFlags.ConstantDataFromServer)) { Shooter = new LazyProxy<int, Gob>(FindGob); Target = new LazyProxy<int, Gob>(FindGob); Shooter.SetData(reader.ReadInt16()); ShooterBoneIndex = reader.ReadByte(); int targetID = reader.ReadInt16(); if (targetID != 0) Target.SetData(targetID); ChainIndex = reader.ReadByte(); } }