Skip to content

Commit

Permalink
Updated Weaver to work with Unity 2019.1.
Browse files Browse the repository at this point in the history
Unity now includes Mono.Cecil in each project which was causing conflicts so the included dlls were stripped and an assembly definition added. The ones Unity provided were an updated version of Cecil so I also updated the references.
  • Loading branch information
ByronMayne committed Nov 24, 2019
1 parent a7dd23d commit 1cef7dc
Show file tree
Hide file tree
Showing 71 changed files with 898 additions and 2,080 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ Weaver\.csproj
*.sln

\.vs/

Assembly-CSharp\.csproj

*.csproj
401 changes: 0 additions & 401 deletions Assembly-CSharp-Editor.csproj

This file was deleted.

356 changes: 0 additions & 356 deletions Assembly-CSharp.csproj

This file was deleted.

1 change: 0 additions & 1 deletion Assets/Editor/CaptureGroups
Submodule CaptureGroups deleted from 39af51
48 changes: 24 additions & 24 deletions Assets/ExampleBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using UnityEngine;
using Weaver;

public class ExampleBehaviour : MonoBehaviour
{
private float m_Height = 0f;

[OnChanged("OnHeightChanged")]
using UnityEngine;
using Weaver;

public class ExampleBehaviour : MonoBehaviour
{
private float m_Height = 0f;

[OnChanged("OnHeightChanged")]
public float height
{
get { return m_Height; }
set { m_Height = value; }
}

[OnChanged("OnAgeChanged", isValidated = true)]
public int age { get; set; }

}

[OnChanged("OnAgeChanged", isValidated = true)]
public int age { get; set; }

public int otherAge
{
get { return age; }
Expand All @@ -25,30 +25,30 @@ public int otherAge
OnAgeChanged(value);
}
}
}

}

public void Other()
{

}
}
public void Start()
{
age = 23;
height = 6.1f;
}

}

private void OnHeightChanged(float newHeight)
{
Debug.Log("Height changed from " + m_Height + " to " + newHeight);
}

}

private void OnAgeChanged(int newAge)
{
Debug.Log("Age changed from " + age + " to " + newAge);
}

}

private void OnValidate()
{

}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;

namespace Weaver
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class OnChangedAttribute : Attribute
{
public string callbackMethod { get; set; }

public bool isValidated { get; set; }

public OnChangedAttribute(string callbackMethod)
{
this.callbackMethod = callbackMethod;
}
}
using System;

namespace Weaver
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class OnChangedAttribute : Attribute
{
public string callbackMethod { get; set; }

public bool isValidated { get; set; }

public OnChangedAttribute(string callbackMethod)
{
this.callbackMethod = callbackMethod;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Weaver
{
/// <summary>
/// When put above a method and Weaver has this option
/// turned on this will inject <see cref="UnityEngine.Profiling.Profiler.BeginSample(string)"/>
/// at the start of the function and <see cref="UnityEngine.Profiling.Profiler.EndSample()"/> and the
/// end. This will then output the result.
/// <summary>
/// When put above a method and Weaver has this option
/// turned on this will inject <see cref="UnityEngine.Profiling.Profiler.BeginSample(string)"/>
/// at the start of the function and <see cref="UnityEngine.Profiling.Profiler.EndSample()"/> and the
/// end. This will then output the result.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class ProfileSampleAttribute : Attribute
Expand Down
3 changes: 3 additions & 0 deletions Assets/Weaver/Attributes/Weaver.Attributes.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Weaver.Attributes"
}
7 changes: 7 additions & 0 deletions Assets/Weaver/Attributes/Weaver.Attributes.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Assets/Editor.meta → Assets/Weaver/Editor/Components.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public struct StopwatchDefinition

public StopwatchDefinition(TypeDefinition stopwatchTypeDef, ModuleDefinition module)
{
consturctor = module.Import(stopwatchTypeDef.GetMethod(".ctor"));
start = module.Import(stopwatchTypeDef.GetMethod("Start"));
stop = module.Import(stopwatchTypeDef.GetMethod("Stop"));
getElapsedMilliseconds = module.Import(stopwatchTypeDef.GetProperty("ElapsedMilliseconds").GetMethod);
consturctor = module.ImportReference(stopwatchTypeDef.GetMethod(".ctor"));
start = module.ImportReference(stopwatchTypeDef.GetMethod("Start"));
stop = module.ImportReference(stopwatchTypeDef.GetMethod("Stop"));
getElapsedMilliseconds = module.ImportReference(stopwatchTypeDef.GetProperty("ElapsedMilliseconds").GetMethod);
}
}

Expand Down Expand Up @@ -49,18 +49,18 @@ public override DefinitionType effectedDefintions
public override void VisitModule(ModuleDefinition moduleDefinition)
{
// Import our stopwatch type reference
m_StopwatchTypeReference = moduleDefinition.Import(typeof(Stopwatch));
m_StopwatchTypeReference = moduleDefinition.ImportReference(typeof(Stopwatch));
// Resolve it so we can get the type definition
TypeDefinition stopwatchTypeDef = m_StopwatchTypeReference.Resolve();
// Create our value holder
m_StopWatchTypeDef = new StopwatchDefinition(stopwatchTypeDef, moduleDefinition);
// String
TypeDefinition stringTypeDef = typeSystem.String.Resolve();
m_StringConcatMethodRef = moduleDefinition.Import(stringTypeDef.GetMethod("Concat", 2));
m_StringConcatMethodRef = moduleDefinition.ImportReference(stringTypeDef.GetMethod("Concat", 2));

TypeReference debugTypeRef = moduleDefinition.Import(typeof(Debug));
TypeReference debugTypeRef = moduleDefinition.ImportReference(typeof(Debug));
TypeDefinition debugTypeDeff = debugTypeRef.Resolve();
m_DebugLogMethodRef = moduleDefinition.Import(debugTypeDeff.GetMethod("Log", 1));
m_DebugLogMethodRef = moduleDefinition.ImportReference(debugTypeDeff.GetMethod("Log", 1));
}

public override void VisitMethod(MethodDefinition methodDefinition)
Expand All @@ -79,8 +79,8 @@ public override void VisitMethod(MethodDefinition methodDefinition)
MethodBody body = methodDefinition.Body;
ILProcessor bodyProcessor = body.GetILProcessor();

VariableDefinition stopwatchVariable = new VariableDefinition("stopwatch", m_StopwatchTypeReference);
VariableDefinition elapsedMilliseconds = new VariableDefinition("elapsedMilliseconds", typeSystem.Int64);
VariableDefinition stopwatchVariable = new VariableDefinition(m_StopwatchTypeReference);
VariableDefinition elapsedMilliseconds = new VariableDefinition(typeSystem.Int64);
body.Variables.Add(stopwatchVariable);
body.Variables.Add(elapsedMilliseconds);
// Inject at the start of the function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void VisitModule(ModuleDefinition moduleDefinition)
// Get profiler type
Type profilerType = typeof(Profiler);
// Import the profiler type
m_ProfilerTypeReference = moduleDefinition.Import(profilerType);
m_ProfilerTypeReference = moduleDefinition.ImportReference(profilerType);
// Get the type def by resolving
TypeDefinition profilerTypeDef = m_ProfilerTypeReference.Resolve();
// Get our start sample
Expand All @@ -49,18 +49,18 @@ public override void VisitModule(ModuleDefinition moduleDefinition)
// Get the type GameObject
Type componentType = typeof(Component);
// Get Game Object Type R
TypeReference componentTypeRef = moduleDefinition.Import(componentType);
TypeReference componentTypeRef = moduleDefinition.ImportReference(componentType);
// Get the type def
TypeDefinition componentTypeDef = componentTypeRef.Resolve();
// Get our get property
PropertyDefinition gameObjectPropertyDef = componentTypeDef.GetProperty("gameObject");
m_GetGameObjectMethodRef = gameObjectPropertyDef.GetMethod;

// Import everything
moduleDefinition.Import(typeof(GameObject));
moduleDefinition.Import(m_BeginSampleMethodRef);
moduleDefinition.Import(m_GetGameObjectMethodRef);
moduleDefinition.Import(m_BeginSampleWithGameObjectMethodRef);
moduleDefinition.ImportReference(typeof(GameObject));
moduleDefinition.ImportReference(m_BeginSampleMethodRef);
moduleDefinition.ImportReference(m_GetGameObjectMethodRef);
moduleDefinition.ImportReference(m_BeginSampleWithGameObjectMethodRef);
}

public override void VisitMethod(MethodDefinition methodDefinition)
Expand All @@ -80,8 +80,8 @@ public override void VisitMethod(MethodDefinition methodDefinition)
{
Instruction _00 = Instruction.Create(OpCodes.Ldstr, methodDefinition.DeclaringType.Name + ":" + methodDefinition.Name);
Instruction _01 = Instruction.Create(OpCodes.Ldarg_0);
Instruction _02 = Instruction.Create(OpCodes.Call, methodDefinition.Module.Import(m_GetGameObjectMethodRef));
Instruction _03 = Instruction.Create(OpCodes.Call, methodDefinition.Module.Import(m_BeginSampleWithGameObjectMethodRef));
Instruction _02 = Instruction.Create(OpCodes.Call, methodDefinition.Module.ImportReference(m_GetGameObjectMethodRef));
Instruction _03 = Instruction.Create(OpCodes.Call, methodDefinition.Module.ImportReference(m_BeginSampleWithGameObjectMethodRef));

bodyProcessor.InsertBefore(body.Instructions[0], _00);
bodyProcessor.InsertAfter(_00, _01);
Expand All @@ -93,7 +93,7 @@ public override void VisitMethod(MethodDefinition methodDefinition)
{
if(body.Instructions[i].OpCode == OpCodes.Ret)
{
Instruction _00 = Instruction.Create(OpCodes.Call, methodDefinition.Module.Import(m_EndSampleMethodRef));
Instruction _00 = Instruction.Create(OpCodes.Call, methodDefinition.Module.ImportReference(m_EndSampleMethodRef));
bodyProcessor.InsertBefore(body.Instructions[i], _00);
i++;
}
Expand Down
Loading

0 comments on commit 1cef7dc

Please sign in to comment.