MeshSequenceInfo.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. namespace VertexAnimationTools_30 {
  6. [System.Serializable]
  7. public struct MeshSequenceInfo {
  8. public enum StateEnum {
  9. Empty_path,
  10. File_not_found,
  11. Ready
  12. }
  13. public enum SortModeEnum {
  14. ByDate,
  15. ByNumber
  16. }
  17. [System.Serializable]
  18. public struct ObjFileInfo {
  19. public string FileName;
  20. public int Hash;
  21. public int ParsedNumber;
  22. public FileInfo fi;
  23. }
  24. public StateEnum State;
  25. public ObjFileInfo[] infos;
  26. public string SequenceName;
  27. public string DirectoryPath;
  28. //public string Date;
  29. SortModeEnum SortMode ;
  30. public MeshSequenceInfo( string pathToAnyObj, SortModeEnum sortMode) {
  31. SortMode = sortMode;
  32. State = StateEnum.Empty_path;
  33. SequenceName = "";
  34. DirectoryPath = "";
  35. infos = new ObjFileInfo[0];
  36. // Date = System.DateTime.Now.ToString();
  37. if (string.IsNullOrEmpty(pathToAnyObj)) {
  38. return;
  39. }
  40. if (!File.Exists(pathToAnyObj)) {
  41. State = StateEnum.File_not_found;
  42. return;
  43. }
  44. DirectoryInfo di = (new FileInfo(pathToAnyObj)).Directory;
  45. FileInfo[] fis = di.GetFiles("*.obj");
  46. DirectoryPath = di.FullName;
  47. infos = new ObjFileInfo[fis.Length];
  48. for (int i = 0; i < infos.Length; i++) {
  49. infos[i] = new ObjFileInfo();
  50. infos[i].fi = fis[i];
  51. infos[i].FileName = Path.GetFileNameWithoutExtension(fis[i].FullName);
  52. infos[i].ParsedNumber = GetNumberFromName(infos[i].FileName, ref infos[i].Hash);
  53. }
  54. if (SortMode == SortModeEnum.ByDate) {
  55. System.Array.Sort(infos, InfosDateComparer);
  56. } else {
  57. System.Array.Sort(infos, InfosNumberComparer);
  58. }
  59. SequenceName = string.Format("[{0}-{1}]", infos[0].FileName, infos[infos.Length-1].FileName );
  60. State = StateEnum.Ready;
  61. }
  62. public string ShortInfo {
  63. get {
  64. if (State != StateEnum.Ready) {
  65. return State.ToString().Replace('_',' ');
  66. } else {
  67. return string.Format("{0} {1} frames", SequenceName, Count.ToString());
  68. }
  69. }
  70. }
  71. public int Count {
  72. get {
  73. if (infos == null) {
  74. return 0;
  75. } else {
  76. return infos.Length;
  77. }
  78. }
  79. }
  80. public ObjFileInfo this[int index] {
  81. get {
  82. return infos[index];
  83. }
  84. }
  85. int InfosNumberComparer(ObjFileInfo a, ObjFileInfo b) {
  86. if (a.ParsedNumber == b.ParsedNumber) {
  87. return a.Hash - b.Hash;
  88. } else {
  89. return a.ParsedNumber - b.ParsedNumber;
  90. }
  91. }
  92. int InfosDateComparer(ObjFileInfo a, ObjFileInfo b) {
  93. int result;
  94. if (a.fi.LastWriteTime == b.fi.LastWriteTime) {
  95. result = 0;
  96. } else if (a.fi.LastWriteTime < b.fi.LastWriteTime) {
  97. result = -1;
  98. } else {
  99. result = 1;
  100. }
  101. return result;
  102. }
  103. int GetNumberFromName(string str, ref int prefixHash) {
  104. char[] nameChars = str.ToCharArray();
  105. int splitIdx = nameChars.Length;
  106. for (int i = nameChars.Length - 1; i >= 0; i--) {
  107. if (!char.IsDigit (nameChars [i])) {
  108. break;
  109. } else {
  110. splitIdx = i;
  111. }
  112. }
  113. string digitsPart = str.Substring( splitIdx );
  114. string prefixPart = str.Substring(0, splitIdx);
  115. prefixHash = prefixPart.GetHashCode();
  116. int index = 0;
  117. int.TryParse(digitsPart, out index);
  118. return index;
  119. }
  120. }
  121. }