using System;
using System.Runtime.InteropServices;
using Cpp2IlInjected;
using UnityEngine;

namespace Obi
{
	// Token: 0x02000541 RID: 1345
	[Token(Token = "0x2000541")]
	[ExecuteInEditMode]
	[DisallowMultipleComponent]
	[AddComponentMenu("Physics/Obi/Obi Cloth Proxy")]
	public class ObiClothProxy : MonoBehaviour
	{
		// Token: 0x17000332 RID: 818
		// (get) Token: 0x06002EC9 RID: 11977 RVA: 0x000662A4 File Offset: 0x000644A4
		[Token(Token = "0x17000332")]
		public bool SelfSkinning
		{
			[Token(Token = "0x6002EC9")]
			[Address(RVA = "0x109A110", Offset = "0x1099110", VA = "0x18109A110")]
			get
			{
				ObiClothBase obiClothBase = this.source;
				int num = 0;
				bool flag = obiClothBase != num;
				if (!flag)
				{
					return flag;
				}
				GameObject gameObject = this.source.gameObject;
				GameObject gameObject2 = base.gameObject;
				return gameObject == gameObject2;
			}
		}

		// Token: 0x17000333 RID: 819
		// (get) Token: 0x06002ECB RID: 11979 RVA: 0x000663A8 File Offset: 0x000645A8
		// (set) Token: 0x06002ECA RID: 11978 RVA: 0x000662EC File Offset: 0x000644EC
		[Token(Token = "0x17000333")]
		public ObiClothBase Proxy
		{
			[Token(Token = "0x6002ECB")]
			[Address(RVA = "0x36B6A0", Offset = "0x36A6A0", VA = "0x18036B6A0")]
			get
			{
				return this.source;
			}
			[Token(Token = "0x6002ECA")]
			[Address(RVA = "0x109A1E0", Offset = "0x10991E0", VA = "0x18109A1E0")]
			set
			{
				ObiClothBase obiClothBase = this.source;
				int num = 0;
				if (obiClothBase != num)
				{
					ObiClothBase obiClothBase2 = this.source;
					EventHandler eventHandler = new EventHandler(this.Source_OnAddedToSolver);
					obiClothBase2.OnDeformableMeshSetup -= eventHandler;
					ObiClothBase obiClothBase3 = this.source;
					EventHandler eventHandler2 = new EventHandler(this.Source_OnRemovedFromSolver);
					obiClothBase3.OnDeformableMeshTeardown -= eventHandler2;
				}
				this.source = value;
				ObiClothBase obiClothBase4 = this.source;
				int num2 = 0;
				if (obiClothBase4 != num2)
				{
					ObiClothBase obiClothBase5 = this.source;
					EventHandler eventHandler3 = new EventHandler(this.Source_OnAddedToSolver);
					obiClothBase5.OnDeformableMeshSetup += eventHandler3;
					ObiClothBase obiClothBase6 = this.source;
					EventHandler eventHandler4 = new EventHandler(this.Source_OnRemovedFromSolver);
					obiClothBase6.OnDeformableMeshTeardown += eventHandler4;
				}
			}
		}

		// Token: 0x06002ECC RID: 11980 RVA: 0x000663BC File Offset: 0x000645BC
		[Token(Token = "0x6002ECC")]
		[Address(RVA = "0x1099250", Offset = "0x1098250", VA = "0x181099250")]
		private void OnEnable()
		{
			SkinnedMeshRenderer component = base.GetComponent<SkinnedMeshRenderer>();
			this.skinnedMeshRenderer = component;
			if (!this.SelfSkinning)
			{
				SkinnedMeshRenderer skinnedMeshRenderer = this.skinnedMeshRenderer;
				int num = 0;
				if (!(skinnedMeshRenderer == num))
				{
					ObiMeshTopology obiMeshTopology = this.targetTopology;
					int num2 = 0;
					if (!(obiMeshTopology != num2))
					{
						Debug.LogError("You need to provide a mesh topology in order to use a SkinnedMeshRenderer as a proxy target.");
					}
					Mesh input = this.targetTopology.input;
					this.sharedMesh = input;
					Mesh mesh = UnityEngine.Object.Instantiate<Mesh>(this.sharedMesh);
					this.skinnedMesh = mesh;
					int num3 = 0;
					Mesh mesh2 = this.skinnedMesh;
					BoneWeight[] array = new BoneWeight[num3];
					mesh2.SetBoneWeightsImpl(array);
					this.skinnedMesh.MarkDynamic();
					if (!Application.isPlaying)
					{
						goto IL_CB;
					}
					SkinnedMeshRenderer skinnedMeshRenderer2 = this.skinnedMeshRenderer;
					Mesh mesh3 = this.skinnedMesh;
					skinnedMeshRenderer2.sharedMesh = mesh3;
				}
				this.InitializeWithRegularMesh();
			}
			IL_CB:
			ObiClothBase obiClothBase = this.source;
			int num4 = 0;
			if (obiClothBase != num4)
			{
				ObiClothBase obiClothBase2 = this.source;
				EventHandler eventHandler = new EventHandler(this.Source_OnAddedToSolver);
				obiClothBase2.OnDeformableMeshSetup += eventHandler;
				ObiClothBase obiClothBase3 = this.source;
				EventHandler eventHandler2 = new EventHandler(this.Source_OnRemovedFromSolver);
				obiClothBase3.OnDeformableMeshTeardown += eventHandler2;
			}
		}

		// Token: 0x06002ECD RID: 11981 RVA: 0x000664F0 File Offset: 0x000646F0
		[Token(Token = "0x6002ECD")]
		[Address(RVA = "0x1099050", Offset = "0x1098050", VA = "0x181099050")]
		private void OnDisable()
		{
			if (!this.SelfSkinning)
			{
				MeshFilter meshFilter = this.meshFilter;
				int num = 0;
				if (meshFilter != num)
				{
					MeshFilter meshFilter2 = this.meshFilter;
					Mesh mesh = this.sharedMesh;
					meshFilter2.mesh = mesh;
				}
				SkinnedMeshRenderer skinnedMeshRenderer = this.skinnedMeshRenderer;
				int num2 = 0;
				if (skinnedMeshRenderer != num2)
				{
					SkinnedMeshRenderer skinnedMeshRenderer2 = this.skinnedMeshRenderer;
					Mesh mesh2 = this.sharedMesh;
					skinnedMeshRenderer2.sharedMesh = mesh2;
				}
				UnityEngine.Object.DestroyImmediate(this.skinnedMesh);
			}
			ObiClothBase obiClothBase = this.source;
			int num3 = 0;
			if (obiClothBase != num3)
			{
				ObiClothBase obiClothBase2 = this.source;
				EventHandler eventHandler = new EventHandler(this.Source_OnAddedToSolver);
				obiClothBase2.OnDeformableMeshSetup -= eventHandler;
				ObiClothBase obiClothBase3 = this.source;
				EventHandler eventHandler2 = new EventHandler(this.Source_OnRemovedFromSolver);
				obiClothBase3.OnDeformableMeshTeardown -= eventHandler2;
			}
		}

		// Token: 0x06002ECE RID: 11982 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002ECE")]
		[Address(RVA = "0x1099D30", Offset = "0x1098D30", VA = "0x181099D30")]
		private void UpdateTransformData()
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002ECF RID: 11983 RVA: 0x000665C4 File Offset: 0x000647C4
		[Token(Token = "0x6002ECF")]
		[Address(RVA = "0x1099810", Offset = "0x1098810", VA = "0x181099810")]
		private void Source_OnAddedToSolver(object sender, EventArgs e)
		{
			this.UpdateTransformData();
			bool selfSkinning = this.SelfSkinning;
			ObiClothBase obiClothBase = this.source;
			if (!selfSkinning)
			{
				IntPtr oniSolver = obiClothBase.solver.oniSolver;
				ObiMeshTopology obiMeshTopology = this.targetTopology;
				int num = 0;
				if (obiMeshTopology != num)
				{
					ObiMeshTopology obiMeshTopology2 = this.targetTopology;
				}
				int num2 = 0;
				Mesh mesh = this.sharedMesh;
				float[] array = this.transformData;
				int vertexCount = mesh.vertexCount;
				int vertexCount2 = this.sharedMesh.vertexCount;
				int num3 = 0;
				ulong num4;
				IntPtr intPtr = Oni.CreateDeformableMesh(oniSolver, (IntPtr)num2, (IntPtr)num3, array, (IntPtr)num4, vertexCount, vertexCount2);
				this.deformableMesh = intPtr;
				Mesh mesh2 = this.skinnedMesh;
				this.GetMeshDataArrays(mesh2);
				this.SetSkinnedMeshAnimationInfo();
			}
			IntPtr intPtr2 = obiClothBase.deformableMesh;
			this.deformableMesh = intPtr2;
			IntPtr triangleSkinMap = this.skinMap.triangleSkinMap;
			IntPtr intPtr3 = obiClothBase.deformableMesh;
			Oni.SetDeformableMeshSkinMap(this.deformableMesh, intPtr3, triangleSkinMap);
			ObiSolver solver = this.source.solver;
			EventHandler eventHandler = new EventHandler(this.Source_Solver_OnFrameEnd);
			solver.OnFrameEnd += eventHandler;
			ObiSolver solver2 = this.source.solver;
			EventHandler eventHandler2 = new EventHandler(this.Source_Solver_OnStepEnd);
			solver2.OnStepEnd += eventHandler2;
		}

		// Token: 0x06002ED0 RID: 11984 RVA: 0x000666F8 File Offset: 0x000648F8
		[Token(Token = "0x6002ED0")]
		[Address(RVA = "0x1099A80", Offset = "0x1098A80", VA = "0x181099A80")]
		private void Source_OnRemovedFromSolver(object sender, EventArgs e)
		{
			ObiSolver solver = this.source.solver;
			EventHandler eventHandler = new EventHandler(this.Source_Solver_OnFrameEnd);
			solver.OnFrameEnd -= eventHandler;
			ObiSolver solver2 = this.source.solver;
			EventHandler eventHandler2 = new EventHandler(this.Source_Solver_OnStepEnd);
			solver2.OnStepEnd -= eventHandler2;
			if (!this.SelfSkinning)
			{
				ObiSolver solver3 = this.source.solver;
				IntPtr intPtr = this.deformableMesh;
				Oni.DestroyDeformableMesh(solver3.oniSolver, intPtr);
				Oni.UnpinMemory(this.meshVerticesHandle);
				Oni.UnpinMemory(this.meshNormalsHandle);
				Oni.UnpinMemory(this.meshTangentsHandle);
			}
		}

		// Token: 0x06002ED1 RID: 11985 RVA: 0x00066798 File Offset: 0x00064998
		[Token(Token = "0x6002ED1")]
		[Address(RVA = "0x1099BD0", Offset = "0x1098BD0", VA = "0x181099BD0")]
		private void Source_Solver_OnFrameEnd(object sender, EventArgs e)
		{
			if (!this.SelfSkinning)
			{
				Mesh mesh = this.skinnedMesh;
				int num = 0;
				if (mesh != num && this.skinnedMesh.isReadable)
				{
					Mesh mesh2 = this.skinnedMesh;
					Vector3[] array = this.meshVertices;
					mesh2.vertices = array;
					Mesh mesh3 = this.skinnedMesh;
					Vector3[] array2 = this.meshNormals;
					mesh3.normals = array2;
					Mesh mesh4 = this.skinnedMesh;
					Vector4[] array3 = this.meshTangents;
					mesh4.tangents = array3;
					this.skinnedMesh.RecalculateBounds();
					return;
				}
			}
		}

		// Token: 0x06002ED2 RID: 11986 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002ED2")]
		[Address(RVA = "0x1099CC0", Offset = "0x1098CC0", VA = "0x181099CC0")]
		private void Source_Solver_OnStepEnd(object sender, EventArgs e)
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002ED3 RID: 11987 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002ED3")]
		[Address(RVA = "0x1098780", Offset = "0x1097780", VA = "0x181098780", Slot = "4")]
		public virtual void GetMeshDataArrays(Mesh mesh)
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002ED4 RID: 11988 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002ED4")]
		[Address(RVA = "0x1098D20", Offset = "0x1097D20", VA = "0x181098D20")]
		private void InitializeWithRegularMesh()
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002ED5 RID: 11989 RVA: 0x00066828 File Offset: 0x00064A28
		[Token(Token = "0x6002ED5")]
		[Address(RVA = "0x1098F30", Offset = "0x1097F30", VA = "0x181098F30")]
		protected void InitializeWithSkinnedMesh()
		{
			Mesh input = this.targetTopology.input;
			this.sharedMesh = input;
			Mesh mesh = UnityEngine.Object.Instantiate<Mesh>(this.sharedMesh);
			this.skinnedMesh = mesh;
			int num = 0;
			Mesh mesh2 = this.skinnedMesh;
			BoneWeight[] array = new BoneWeight[num];
			mesh2.SetBoneWeightsImpl(array);
			this.skinnedMesh.MarkDynamic();
			if (Application.isPlaying)
			{
				SkinnedMeshRenderer skinnedMeshRenderer = this.skinnedMeshRenderer;
				Mesh mesh3 = this.skinnedMesh;
				skinnedMeshRenderer.sharedMesh = mesh3;
			}
		}

		// Token: 0x06002ED6 RID: 11990 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002ED6")]
		[Address(RVA = "0x1099570", Offset = "0x1098570", VA = "0x181099570")]
		protected void SetSkinnedMeshAnimationInfo()
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002ED7 RID: 11991 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002ED7")]
		[Address(RVA = "0x10989B0", Offset = "0x10979B0", VA = "0x1810989B0")]
		public void GrabSkeletonBones()
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002ED8 RID: 11992 RVA: 0x000668AC File Offset: 0x00064AAC
		[Token(Token = "0x6002ED8")]
		[Address(RVA = "0x109A050", Offset = "0x1099050", VA = "0x18109A050")]
		public void Update()
		{
			this.UpdateTransformData();
			IntPtr intPtr = this.deformableMesh;
			int num = 0;
			if (intPtr != (IntPtr)num)
			{
				float[] array = this.transformData;
				Oni.SetDeformableMeshTransform(this.deformableMesh, array);
				return;
			}
		}

		// Token: 0x06002ED9 RID: 11993 RVA: 0x000668E8 File Offset: 0x00064AE8
		[Token(Token = "0x6002ED9")]
		[Address(RVA = "0x109A0B0", Offset = "0x10990B0", VA = "0x18109A0B0")]
		public ObiClothProxy()
		{
			float[] array = new float[16];
			this.transformData = array;
			base..ctor();
		}

		// Token: 0x04003754 RID: 14164
		[Cpp2IlInjected.FieldOffset(Offset = "0x18")]
		[Token(Token = "0x4003754")]
		public ObiTriangleSkinMap skinMap;

		// Token: 0x04003755 RID: 14165
		[Cpp2IlInjected.FieldOffset(Offset = "0x20")]
		[Token(Token = "0x4003755")]
		public ObiMeshTopology targetTopology;

		// Token: 0x04003756 RID: 14166
		[Cpp2IlInjected.FieldOffset(Offset = "0x28")]
		[Token(Token = "0x4003756")]
		[SerializeField]
		[HideInInspector]
		private ObiClothBase source;

		// Token: 0x04003757 RID: 14167
		[Cpp2IlInjected.FieldOffset(Offset = "0x30")]
		[Token(Token = "0x4003757")]
		[HideInInspector]
		public Mesh skinnedMesh;

		// Token: 0x04003758 RID: 14168
		[Cpp2IlInjected.FieldOffset(Offset = "0x38")]
		[Token(Token = "0x4003758")]
		[HideInInspector]
		public Mesh sharedMesh;

		// Token: 0x04003759 RID: 14169
		[Cpp2IlInjected.FieldOffset(Offset = "0x40")]
		[Token(Token = "0x4003759")]
		protected int[] meshTriangles;

		// Token: 0x0400375A RID: 14170
		[Cpp2IlInjected.FieldOffset(Offset = "0x48")]
		[Token(Token = "0x400375A")]
		protected Vector3[] meshVertices;

		// Token: 0x0400375B RID: 14171
		[Cpp2IlInjected.FieldOffset(Offset = "0x50")]
		[Token(Token = "0x400375B")]
		protected Vector3[] meshNormals;

		// Token: 0x0400375C RID: 14172
		[Cpp2IlInjected.FieldOffset(Offset = "0x58")]
		[Token(Token = "0x400375C")]
		protected Vector4[] meshTangents;

		// Token: 0x0400375D RID: 14173
		[Cpp2IlInjected.FieldOffset(Offset = "0x60")]
		[Token(Token = "0x400375D")]
		protected IntPtr deformableMesh;

		// Token: 0x0400375E RID: 14174
		[Cpp2IlInjected.FieldOffset(Offset = "0x68")]
		[Token(Token = "0x400375E")]
		protected GCHandle meshTrianglesHandle;

		// Token: 0x0400375F RID: 14175
		[Cpp2IlInjected.FieldOffset(Offset = "0x6C")]
		[Token(Token = "0x400375F")]
		protected GCHandle meshVerticesHandle;

		// Token: 0x04003760 RID: 14176
		[Cpp2IlInjected.FieldOffset(Offset = "0x70")]
		[Token(Token = "0x4003760")]
		protected GCHandle meshNormalsHandle;

		// Token: 0x04003761 RID: 14177
		[Cpp2IlInjected.FieldOffset(Offset = "0x74")]
		[Token(Token = "0x4003761")]
		protected GCHandle meshTangentsHandle;

		// Token: 0x04003762 RID: 14178
		[Cpp2IlInjected.FieldOffset(Offset = "0x78")]
		[Token(Token = "0x4003762")]
		private MeshRenderer meshRenderer;

		// Token: 0x04003763 RID: 14179
		[Cpp2IlInjected.FieldOffset(Offset = "0x80")]
		[Token(Token = "0x4003763")]
		private MeshFilter meshFilter;

		// Token: 0x04003764 RID: 14180
		[Cpp2IlInjected.FieldOffset(Offset = "0x88")]
		[Token(Token = "0x4003764")]
		private SkinnedMeshRenderer skinnedMeshRenderer;

		// Token: 0x04003765 RID: 14181
		[Cpp2IlInjected.FieldOffset(Offset = "0x90")]
		[Token(Token = "0x4003765")]
		protected float[] transformData;
	}
}
