public void TestInstanceMethodsWithParameters() { MethodTests tests1 = new MethodTests { Value = 10 }; MethodTests tests2 = new MethodTests { Value = 10 }; tests1.Value.ShouldBe(tests2.Value); tests1.IncrementValueBy(5); using (Redirection redirection = Redirection.Redirect <Action <int> >(IncrementValueBy, DecrementValueBy)) { tests2.IncrementValueBy(5); tests1.Value.ShouldNotBe(tests2.Value); tests1.Value.ShouldBe(15); tests2.Value.ShouldBe(5); redirection.Stop(); tests1.Value = tests2.Value = 0; tests1.IncrementValueBy(3); tests2.IncrementValueBy(3); tests1.Value.ShouldBe(tests2.Value); tests1.Value.ShouldBe(3); tests2.Value.ShouldBe(3); } }
public void TestInstanceMethods() { MethodTests tests1 = new MethodTests { Value = 10 }; MethodTests tests2 = new MethodTests { Value = 10 }; tests1.Value.ShouldBe(tests2.Value); tests1.IncrementValue(); using (Redirection redirection = Redirection.Redirect <Action>(IncrementValue, DecrementValue)) { tests2.IncrementValue(); tests1.Value.ShouldNotBe(tests2.Value); tests1.Value.ShouldBe(11); tests2.Value.ShouldBe(9); redirection.Stop(); tests1.Value = tests2.Value = 0; tests1.IncrementValue(); tests2.IncrementValue(); tests1.Value.ShouldBe(tests2.Value); tests1.Value.ShouldBe(1); tests2.Value.ShouldBe(1); } }
public void TestStaticMethodsWithParameters() { PlusOne(1).ShouldNotBe(PlusTwo(1)); using (Redirection.Redirect <Func <int, int> >(PlusOne, PlusTwo)) { PlusOne(1).ShouldBe(PlusTwo(1)); } PlusOne(1).ShouldNotBe(PlusTwo(1)); }
public void TestStaticMethods() { Original().ShouldNotBe(Replacement()); using (Redirection.Redirect <Func <bool> >(Original, Replacement)) { Original().ShouldBe(Replacement()); } Original().ShouldNotBe(Replacement()); }
public void TestInvokeOriginal() { Original().ShouldNotBe(Replacement()); using (var redirection = Redirection.Redirect <Func <bool> >(Original, Replacement)) { Original().ShouldBe(Replacement()); Original().ShouldNotBe(redirection.InvokeOriginal(null)); redirection.Stop(); Original().ShouldBe(redirection.InvokeOriginal(null)); } }
public void TestStaticEvents() { // Note: Calling "OnMultiply" will still call "Multiply". // In order to completely override this behavior, you'd have // to replace the underlying delegate. You can simply do this by finding // the compiler-generated "OnMultiply" field, and setting its value to the // compiler-generated "OnAdd" field. EventInfo multiplyEvent = typeof(EventTests).GetEvent(nameof(OnMultiply), BindingFlags.Static | BindingFlags.Public); EventInfo addEvent = typeof(EventTests).GetEvent(nameof(OnAdd), BindingFlags.Static | BindingFlags.Public); OnMultiply += Multiply; OnAdd += Add; using (Redirection.Redirect(multiplyEvent, addEvent)) { OnMultiply.GetInvocationList().Length.ShouldBe(1); OnMultiply -= Multiply; OnMultiply.GetInvocationList().Length.ShouldBe(1); } OnMultiply.GetInvocationList().Length.ShouldBe(1); OnMultiply -= Multiply; OnMultiply.ShouldBeNull(); }
public static void Intitialize() { if (_factors == null) { _factors = new float[ushort.MaxValue + 1]; _lastFactors = new float[ushort.MaxValue + 1]; _lastLaneIDs = new uint[ushort.MaxValue + 1]; // Precalculate factors for better performances for (int i = 0; i < _factors.Length; i++) { _factors[i] = Randomize(1f, 10f, i); _lastFactors[i] = _factors[i]; } } _pathManager = PathManager.instance; _netManager = NetManager.instance; List <NetInfo> highways = new List <NetInfo>(); for (uint i = 0; i < PrefabCollection <NetInfo> .PrefabCount(); i++) { NetInfo info = PrefabCollection <NetInfo> .GetPrefab(i); if (info != null && info.name.ToLower().Contains("highway")) { int count = 0; for (int j = 0; j < info.m_lanes.Length; j++) { if (info.m_lanes[j] != null && info.m_lanes[j].m_laneType == NetInfo.LaneType.Vehicle) { count++; } } if (count == 3) { highways.Add(info); } } } _highways = highways.ToArray(); if (_enabled && !_detoured) { if (_CalculateTargetSpeed == null) { _CalculateTargetSpeed = new Dictionary <Type, Redirection>(); MethodInfo CalculateTargetSpeed_detour = typeof(VehicleAIDetour).GetMethod("CalculateTargetSpeed", BindingFlags.NonPublic | BindingFlags.Instance); for (uint i = 0; i < PrefabCollection <VehicleInfo> .PrefabCount(); i++) { VehicleInfo prefab = PrefabCollection <VehicleInfo> .GetPrefab(i); if (prefab == null) { continue; } Type aiType = prefab.m_vehicleAI.GetType(); if (prefab.m_vehicleAI is CarAI && !_CalculateTargetSpeed.ContainsKey(aiType)) { Redirection redirect = new Redirection(); redirect.original = aiType.GetMethod("CalculateTargetSpeed", BindingFlags.NonPublic | BindingFlags.Instance); redirect.detour = CalculateTargetSpeed_detour; _CalculateTargetSpeed.Add(aiType, redirect); } } try { // Traffic++ support Type csl_traffic = Type.GetType("CSL_Traffic.CustomVehicleAI, CSL-Traffic"); if (csl_traffic != null) { DebugUtils.Log("Traffic++ found. Adding support"); _RestrictSpeed = new Redirection(); _RestrictSpeed.original = csl_traffic.GetMethod("RestrictSpeed", BindingFlags.Public | BindingFlags.Static); _RestrictSpeed.detour = typeof(RandomSpeed).GetMethod("RestrictSpeed", BindingFlags.NonPublic | BindingFlags.Static); } } catch { } } foreach (Redirection redirect in _CalculateTargetSpeed.Values) { redirect.Redirect(); } if (_RestrictSpeed != null) { _RestrictSpeed.Redirect(); } _detoured = true; } else if (!_enabled && _detoured) { foreach (Redirection redirect in _CalculateTargetSpeed.Values) { redirect.Revert(); } if (_RestrictSpeed != null) { _RestrictSpeed.Revert(); } _detoured = false; } }