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

internal class DanceObjectDataBinary
{
	public void ReadData(string file_name)
	{
		file_name = file_name.Replace(Path.GetExtension(file_name), string.Empty);
		if (0 <= file_name.IndexOf("Assets/Resources/"))
		{
			file_name = file_name.Replace("Assets/Resources/", string.Empty);
		}
		TextAsset textAsset = Resources.Load(file_name) as TextAsset;
		if (textAsset == null)
		{
			return;
		}
		MemoryStream input = new MemoryStream(textAsset.bytes);
		BinaryReader binaryReader = new BinaryReader(input);
		int num = binaryReader.ReadInt32();
		this.datas = new DanceObjectDataBinary.Data[num];
		for (int i = 0; i < this.datas.Length; i++)
		{
			DanceObjectDataBinary.Data data = new DanceObjectDataBinary.Data();
			data.target_maid_no = binaryReader.ReadInt32();
			data.object_name = binaryReader.ReadString();
			data.top_object_name = binaryReader.ReadString();
			data.resources_path = binaryReader.ReadString();
			data.tree_path = binaryReader.ReadString();
			data.object_reference_track_id_list = new List<int>();
			int num2 = binaryReader.ReadInt32();
			for (int j = 0; j < num2; j++)
			{
				data.object_reference_track_id_list.Add(binaryReader.ReadInt32());
			}
			this.datas[i] = data;
		}
	}

	public List<GameObject> Restoration(bool create_empty_obj)
	{
		List<GameObject> list = new List<GameObject>();
		if (this.datas == null)
		{
			return list;
		}
		foreach (DanceObjectDataBinary.Data data in this.datas)
		{
			List<string> list2 = new List<string>();
			if (!string.IsNullOrEmpty(data.tree_path))
			{
				string[] array2 = data.tree_path.Split(new char[]
				{
					'/'
				});
				foreach (string item in array2)
				{
					list2.Add(item);
				}
			}
			GameObject gameObject = null;
			if (!string.IsNullOrEmpty(data.top_object_name))
			{
				gameObject = GameObject.Find(data.top_object_name);
				if (gameObject == null)
				{
					gameObject = new GameObject(data.top_object_name);
					list.Add(gameObject);
				}
				foreach (string text in list2)
				{
					GameObject gameObject2 = UTY.GetChildObjectNoError(gameObject, text, false);
					if (gameObject2 == null)
					{
						gameObject2 = new GameObject(text);
						gameObject2.transform.parent = gameObject.transform;
					}
					gameObject = gameObject2;
				}
			}
			GameObject gameObject3;
			if (gameObject == null)
			{
				gameObject3 = GameObject.Find(data.object_name);
			}
			else
			{
				gameObject3 = UTY.GetChildObjectNoError(gameObject, data.object_name, false);
			}
			if (gameObject3 == null)
			{
				if (create_empty_obj)
				{
					gameObject3 = new GameObject(data.object_name);
				}
				else
				{
					GameObject gameObject4 = Resources.Load<GameObject>(data.resources_path);
					if (!gameObject4)
					{
						Debug.LogErrorFormat("{0}が見つかりません", new object[]
						{
							data.object_name
						});
					}
					gameObject3 = UnityEngine.Object.Instantiate<GameObject>(gameObject4);
					gameObject3.name = data.object_name;
				}
				if (gameObject != null)
				{
					gameObject3.transform.SetParent(gameObject.transform, false);
				}
			}
			data.obj = gameObject3;
		}
		return list;
	}

	public void AttachObject(AMTake take_data)
	{
		if (this.datas == null || this.datas.Length <= 0)
		{
			return;
		}
		Dictionary<int, GameObject> dictionary = new Dictionary<int, GameObject>();
		for (int i = 0; i < this.datas.Length; i++)
		{
			foreach (int key in this.datas[i].object_reference_track_id_list)
			{
				if (!dictionary.ContainsKey(key))
				{
					dictionary.Add(key, this.datas[i].obj);
				}
			}
		}
		List<AMTrack> trackValues = take_data.trackValues;
		for (int j = 0; j < trackValues.Count; j++)
		{
			if (dictionary.ContainsKey(trackValues[j].id) && trackValues[j] is AMAnimationTrack)
			{
				AMAnimationTrack amanimationTrack = trackValues[j] as AMAnimationTrack;
				if (amanimationTrack.obj == null)
				{
					amanimationTrack.obj = dictionary[trackValues[j].id];
				}
				amanimationTrack.updateCache();
			}
		}
	}

	public DanceObjectDataBinary.Data[] datas;

	public class Data
	{
		public GameObject obj;

		public int target_maid_no;

		public string object_name;

		public string top_object_name;

		public string resources_path;

		public string tree_path;

		public List<int> object_reference_track_id_list;
	}
}
