AWReadBinaryData.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using wf;
  6. public class AWReadBinaryData
  7. {
  8. public AWReadBinaryData()
  9. {
  10. this.file_name_ = string.Empty;
  11. }
  12. public static float FrameToTime(int frame, float frameRate)
  13. {
  14. return (float)System.Math.Round((double)((float)frame / frameRate), 2);
  15. }
  16. public static int TimeToFrame(float time, float frameRate)
  17. {
  18. return Mathf.FloorToInt(time * frameRate);
  19. }
  20. public void ReadFile(string file_name)
  21. {
  22. this.file_name_ = string.Empty;
  23. this.binary_data_list_.Clear();
  24. if (string.IsNullOrEmpty(file_name))
  25. {
  26. return;
  27. }
  28. this.file_name_ = file_name.Replace(Path.GetExtension(file_name), string.Empty);
  29. if (0 <= this.file_name_.IndexOf("Assets/Resources/"))
  30. {
  31. this.file_name_ = this.file_name_.Replace("Assets/Resources/", string.Empty);
  32. }
  33. TextAsset textAsset = Resources.Load(this.file_name_) as TextAsset;
  34. MemoryStream input = new MemoryStream(textAsset.bytes);
  35. BinaryReader binaryReader = new BinaryReader(input);
  36. string text = binaryReader.ReadString();
  37. if (text != "BaseData")
  38. {
  39. this.file_name_ = string.Empty;
  40. return;
  41. }
  42. this.total_frame_ = binaryReader.ReadInt32();
  43. this.frame_rate_ = binaryReader.ReadInt32();
  44. text = binaryReader.ReadString();
  45. while (text != "Finish")
  46. {
  47. AMBinaryDataBaseObject ambinaryDataBaseObject = null;
  48. if (AMBinaryTranslation.kTypeName == text)
  49. {
  50. AMBinaryTranslation ambinaryTranslation = new AMBinaryTranslation();
  51. ambinaryDataBaseObject = ambinaryTranslation;
  52. }
  53. else if (AMBinaryRotation.kTypeName == text)
  54. {
  55. AMBinaryRotation ambinaryRotation = new AMBinaryRotation();
  56. ambinaryDataBaseObject = ambinaryRotation;
  57. }
  58. else if (AMBinaryProperty.kTypeName == text)
  59. {
  60. AMBinaryProperty ambinaryProperty = new AMBinaryProperty();
  61. ambinaryDataBaseObject = ambinaryProperty;
  62. }
  63. else if (AMBinaryMethod.kTypeName == text)
  64. {
  65. AMBinaryMethod ambinaryMethod = new AMBinaryMethod();
  66. ambinaryDataBaseObject = ambinaryMethod;
  67. }
  68. if (ambinaryDataBaseObject != null)
  69. {
  70. ambinaryDataBaseObject.Read(binaryReader);
  71. this.binary_data_list_.Add(ambinaryDataBaseObject);
  72. }
  73. text = binaryReader.ReadString();
  74. }
  75. }
  76. public void Update(float time)
  77. {
  78. if (!this.enabled)
  79. {
  80. return;
  81. }
  82. int frame = wf.Math.RoundMinMax(AWReadBinaryData.TimeToFrame(time, (float)this.frame_rate), 0, this.total_frame - 1);
  83. for (int i = 0; i < this.binary_data_list_.Count; i++)
  84. {
  85. this.binary_data_list_[i].Update(frame);
  86. }
  87. }
  88. public List<int> GetTrackIDList()
  89. {
  90. List<int> list = new List<int>();
  91. if (this.enabled)
  92. {
  93. for (int i = 0; i < this.binary_data_list_.Count; i++)
  94. {
  95. list.Add(this.binary_data_list_[i].track_id);
  96. }
  97. }
  98. return list;
  99. }
  100. public bool IsExistTrackID(int track_id)
  101. {
  102. for (int i = 0; i < this.binary_data_list_.Count; i++)
  103. {
  104. if (this.binary_data_list_[i].track_id == track_id)
  105. {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. public bool enabled
  112. {
  113. get
  114. {
  115. return !string.IsNullOrEmpty(this.file_name_);
  116. }
  117. }
  118. public int total_frame
  119. {
  120. get
  121. {
  122. return this.total_frame_;
  123. }
  124. }
  125. public int frame_rate
  126. {
  127. get
  128. {
  129. return this.frame_rate_;
  130. }
  131. }
  132. public AMBinaryDataBaseObject[] binary_data_list
  133. {
  134. get
  135. {
  136. return this.binary_data_list_.ToArray();
  137. }
  138. }
  139. private List<AMBinaryDataBaseObject> binary_data_list_ = new List<AMBinaryDataBaseObject>();
  140. private string file_name_;
  141. private int total_frame_;
  142. private int frame_rate_;
  143. }