using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;

public class AMBinaryMethod : AMBinaryDataBaseObject
{
	public static bool CheckType(AMTrack track)
	{
		return !(track == null) && track.getTrackType() == AMBinaryMethod.kTypeName;
	}

	public override void Update(int frame)
	{
		if (frame < this.prev_frame)
		{
			this.prev_loop = 0;
		}
		this.prev_frame = frame;
		for (int i = this.prev_loop; i < this.method_data_array.Length; i++)
		{
			if (this.method_data_array[i].start_flame > frame)
			{
				this.prev_loop = i;
				break;
			}
			if (!this.method_data_array[i].has_execution)
			{
				this.method_data_array[i].has_execution = true;
				this.method_data_array[i].method_info.Invoke(this.method_data_array[i].component, this.method_data_array[i].object_param_array);
			}
		}
	}

	public override void Write(BinaryWriter binary)
	{
		base.Write(binary);
		binary.Write(this.method_data_array.Length);
		for (int i = 0; i < this.method_data_array.Length; i++)
		{
			binary.Write(this.method_data_array[i].start_flame);
			binary.Write(this.method_data_array[i].component_name);
			binary.Write(this.method_data_array[i].method_name);
			bool flag = this.method_data_array[i].param_list != null && 0 < this.method_data_array[i].param_list.Count;
			binary.Write(flag);
			if (flag)
			{
				binary.Write(this.method_data_array[i].param_list.Count);
				for (int j = 0; j < this.method_data_array[i].param_list.Count; j++)
				{
					this.method_data_array[i].param_list[j].Write(binary);
				}
			}
		}
	}

	public override void Read(BinaryReader binary)
	{
		this.prev_frame = -1;
		this.prev_loop = 0;
		base.Read(binary);
		int num = binary.ReadInt32();
		this.method_data_array = new AMBinaryMethod.MethodData[num];
		for (int i = 0; i < this.method_data_array.Length; i++)
		{
			AMBinaryMethod.MethodData methodData = default(AMBinaryMethod.MethodData);
			methodData.start_flame = binary.ReadInt32();
			methodData.component_name = binary.ReadString();
			methodData.method_name = binary.ReadString();
			methodData.component = base.obj.GetComponent(methodData.component_name);
			methodData.method_info = methodData.component.GetType().GetMethod(methodData.method_name);
			NDebug.AssertNull(methodData.method_info != null);
			methodData.param_list = null;
			methodData.object_param_array = null;
			bool flag = binary.ReadBoolean();
			if (flag)
			{
				num = binary.ReadInt32();
				methodData.object_param_array = new object[num];
				for (int j = 0; j < num; j++)
				{
					AMEventParameter ameventParameter = ScriptableObject.CreateInstance<AMEventParameter>();
					ameventParameter.Read(binary);
					if (!ameventParameter.isArray())
					{
						methodData.object_param_array[j] = ameventParameter.toObject();
					}
					else
					{
						methodData.object_param_array[j] = ameventParameter.toArray();
					}
				}
			}
			this.method_data_array[i] = methodData;
		}
	}

	public override void SetGameObject(GameObject new_obj)
	{
		base.obj = new_obj;
		for (int i = 0; i < this.method_data_array.Length; i++)
		{
			this.method_data_array[i].component = base.obj.GetComponent(this.method_data_array[i].component_name);
			this.method_data_array[i].method_info = this.method_data_array[i].component.GetType().GetMethod(this.method_data_array[i].method_name);
			NDebug.AssertNull(this.method_data_array[i].method_info != null);
		}
	}

	public override string type_name
	{
		get
		{
			return AMBinaryMethod.kTypeName;
		}
	}

	public static string kTypeName = "Event";

	public AMBinaryMethod.MethodData[] method_data_array;

	private int prev_frame = -1;

	private int prev_loop;

	public struct MethodData
	{
		public bool has_execution;

		public int start_flame;

		public string component_name;

		public string method_name;

		public MethodInfo method_info;

		public Component component;

		public List<AMEventParameter> param_list;

		public object[] object_param_array;
	}
}
