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

namespace Obi
{
	// Token: 0x020004E9 RID: 1257
	[Token(Token = "0x20004E9")]
	[ExecuteInEditMode]
	[DisallowMultipleComponent]
	[AddComponentMenu("Physics/Obi/Obi Cloth Proxy")]
	public class ObiClothProxy : MonoBehaviour
	{
		// Token: 0x1700030D RID: 781
		// (get) Token: 0x06002C00 RID: 11264 RVA: 0x0005F010 File Offset: 0x0005D210
		[Token(Token = "0x1700030D")]
		public bool SelfSkinning
		{
			[Token(Token = "0x6002C00")]
			[Address(RVA = "0x1006DE0", Offset = "0x1005BE0", VA = "0x181006DE0")]
			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: 0x1700030E RID: 782
		// (get) Token: 0x06002C02 RID: 11266 RVA: 0x0005F114 File Offset: 0x0005D314
		// (set) Token: 0x06002C01 RID: 11265 RVA: 0x0005F058 File Offset: 0x0005D258
		[Token(Token = "0x1700030E")]
		public ObiClothBase Proxy
		{
			[Token(Token = "0x6002C02")]
			[Address(RVA = "0x502B30", Offset = "0x501930", VA = "0x180502B30")]
			get
			{
				return this.source;
			}
			[Token(Token = "0x6002C01")]
			[Address(RVA = "0x1006EB0", Offset = "0x1005CB0", VA = "0x181006EB0")]
			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: 0x06002C03 RID: 11267 RVA: 0x0005F128 File Offset: 0x0005D328
		[Token(Token = "0x6002C03")]
		[Address(RVA = "0x1005F20", Offset = "0x1004D20", VA = "0x181005F20")]
		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: 0x06002C04 RID: 11268 RVA: 0x0005F25C File Offset: 0x0005D45C
		[Token(Token = "0x6002C04")]
		[Address(RVA = "0x1005D20", Offset = "0x1004B20", VA = "0x181005D20")]
		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: 0x06002C05 RID: 11269 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002C05")]
		[Address(RVA = "0x1006A00", Offset = "0x1005800", VA = "0x181006A00")]
		private void UpdateTransformData()
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002C06 RID: 11270 RVA: 0x0005F330 File Offset: 0x0005D530
		[Token(Token = "0x6002C06")]
		[Address(RVA = "0x10064E0", Offset = "0x10052E0", VA = "0x1810064E0")]
		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: 0x06002C07 RID: 11271 RVA: 0x0005F464 File Offset: 0x0005D664
		[Token(Token = "0x6002C07")]
		[Address(RVA = "0x1006750", Offset = "0x1005550", VA = "0x181006750")]
		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: 0x06002C08 RID: 11272 RVA: 0x0005F504 File Offset: 0x0005D704
		[Token(Token = "0x6002C08")]
		[Address(RVA = "0x10068A0", Offset = "0x10056A0", VA = "0x1810068A0")]
		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: 0x06002C09 RID: 11273 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002C09")]
		[Address(RVA = "0x1006990", Offset = "0x1005790", VA = "0x181006990")]
		private void Source_Solver_OnStepEnd(object sender, EventArgs e)
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002C0A RID: 11274 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002C0A")]
		[Address(RVA = "0x1005450", Offset = "0x1004250", VA = "0x181005450", Slot = "4")]
		public virtual void GetMeshDataArrays(Mesh mesh)
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002C0B RID: 11275 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002C0B")]
		[Address(RVA = "0x10059F0", Offset = "0x10047F0", VA = "0x1810059F0")]
		private void InitializeWithRegularMesh()
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002C0C RID: 11276 RVA: 0x0005F594 File Offset: 0x0005D794
		[Token(Token = "0x6002C0C")]
		[Address(RVA = "0x1005C00", Offset = "0x1004A00", VA = "0x181005C00")]
		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: 0x06002C0D RID: 11277 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002C0D")]
		[Address(RVA = "0x1006240", Offset = "0x1005040", VA = "0x181006240")]
		protected void SetSkinnedMeshAnimationInfo()
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002C0E RID: 11278 RVA: 0x000020D3 File Offset: 0x000002D3
		[Token(Token = "0x6002C0E")]
		[Address(RVA = "0x1005680", Offset = "0x1004480", VA = "0x181005680")]
		public void GrabSkeletonBones()
		{
			throw new AnalysisFailedException("CPP2IL failed to recover any usable IL for this method.");
		}

		// Token: 0x06002C0F RID: 11279 RVA: 0x0005F618 File Offset: 0x0005D818
		[Token(Token = "0x6002C0F")]
		[Address(RVA = "0x1006D20", Offset = "0x1005B20", VA = "0x181006D20")]
		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: 0x06002C10 RID: 11280 RVA: 0x0005F654 File Offset: 0x0005D854
		[Token(Token = "0x6002C10")]
		[Address(RVA = "0x1006D80", Offset = "0x1005B80", VA = "0x181006D80")]
		public ObiClothProxy()
		{
			float[] array = new float[16];
			this.transformData = array;
			base..ctor();
		}

		// Token: 0x0400346A RID: 13418
		[Cpp2IlInjected.FieldOffset(Offset = "0x18")]
		[Token(Token = "0x400346A")]
		public ObiTriangleSkinMap skinMap;

		// Token: 0x0400346B RID: 13419
		[Cpp2IlInjected.FieldOffset(Offset = "0x20")]
		[Token(Token = "0x400346B")]
		public ObiMeshTopology targetTopology;

		// Token: 0x0400346C RID: 13420
		[Cpp2IlInjected.FieldOffset(Offset = "0x28")]
		[Token(Token = "0x400346C")]
		[SerializeField]
		[HideInInspector]
		private ObiClothBase source;

		// Token: 0x0400346D RID: 13421
		[Cpp2IlInjected.FieldOffset(Offset = "0x30")]
		[Token(Token = "0x400346D")]
		[HideInInspector]
		public Mesh skinnedMesh;

		// Token: 0x0400346E RID: 13422
		[Cpp2IlInjected.FieldOffset(Offset = "0x38")]
		[Token(Token = "0x400346E")]
		[HideInInspector]
		public Mesh sharedMesh;

		// Token: 0x0400346F RID: 13423
		[Cpp2IlInjected.FieldOffset(Offset = "0x40")]
		[Token(Token = "0x400346F")]
		protected int[] meshTriangles;

		// Token: 0x04003470 RID: 13424
		[Cpp2IlInjected.FieldOffset(Offset = "0x48")]
		[Token(Token = "0x4003470")]
		protected Vector3[] meshVertices;

		// Token: 0x04003471 RID: 13425
		[Cpp2IlInjected.FieldOffset(Offset = "0x50")]
		[Token(Token = "0x4003471")]
		protected Vector3[] meshNormals;

		// Token: 0x04003472 RID: 13426
		[Cpp2IlInjected.FieldOffset(Offset = "0x58")]
		[Token(Token = "0x4003472")]
		protected Vector4[] meshTangents;

		// Token: 0x04003473 RID: 13427
		[Cpp2IlInjected.FieldOffset(Offset = "0x60")]
		[Token(Token = "0x4003473")]
		protected IntPtr deformableMesh;

		// Token: 0x04003474 RID: 13428
		[Cpp2IlInjected.FieldOffset(Offset = "0x68")]
		[Token(Token = "0x4003474")]
		protected GCHandle meshTrianglesHandle;

		// Token: 0x04003475 RID: 13429
		[Cpp2IlInjected.FieldOffset(Offset = "0x6C")]
		[Token(Token = "0x4003475")]
		protected GCHandle meshVerticesHandle;

		// Token: 0x04003476 RID: 13430
		[Cpp2IlInjected.FieldOffset(Offset = "0x70")]
		[Token(Token = "0x4003476")]
		protected GCHandle meshNormalsHandle;

		// Token: 0x04003477 RID: 13431
		[Cpp2IlInjected.FieldOffset(Offset = "0x74")]
		[Token(Token = "0x4003477")]
		protected GCHandle meshTangentsHandle;

		// Token: 0x04003478 RID: 13432
		[Cpp2IlInjected.FieldOffset(Offset = "0x78")]
		[Token(Token = "0x4003478")]
		private MeshRenderer meshRenderer;

		// Token: 0x04003479 RID: 13433
		[Cpp2IlInjected.FieldOffset(Offset = "0x80")]
		[Token(Token = "0x4003479")]
		private MeshFilter meshFilter;

		// Token: 0x0400347A RID: 13434
		[Cpp2IlInjected.FieldOffset(Offset = "0x88")]
		[Token(Token = "0x400347A")]
		private SkinnedMeshRenderer skinnedMeshRenderer;

		// Token: 0x0400347B RID: 13435
		[Cpp2IlInjected.FieldOffset(Offset = "0x90")]
		[Token(Token = "0x400347B")]
		protected float[] transformData;
	}
}
