123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- namespace VertexAnimationTools_30 {
- [System.Serializable]
- public struct MeshSequenceInfo {
- public enum StateEnum {
- Empty_path,
- File_not_found,
- Ready
- }
- public enum SortModeEnum {
- ByDate,
- ByNumber
- }
- [System.Serializable]
- public struct ObjFileInfo {
- public string FileName;
- public int Hash;
- public int ParsedNumber;
- public FileInfo fi;
- }
- public StateEnum State;
- public ObjFileInfo[] infos;
- public string SequenceName;
- public string DirectoryPath;
- //public string Date;
- SortModeEnum SortMode ;
- public MeshSequenceInfo( string pathToAnyObj, SortModeEnum sortMode) {
- SortMode = sortMode;
- State = StateEnum.Empty_path;
- SequenceName = "";
- DirectoryPath = "";
- infos = new ObjFileInfo[0];
- // Date = System.DateTime.Now.ToString();
- if (string.IsNullOrEmpty(pathToAnyObj)) {
- return;
- }
- if (!File.Exists(pathToAnyObj)) {
- State = StateEnum.File_not_found;
- return;
- }
- DirectoryInfo di = (new FileInfo(pathToAnyObj)).Directory;
- FileInfo[] fis = di.GetFiles("*.obj");
- DirectoryPath = di.FullName;
- infos = new ObjFileInfo[fis.Length];
-
- for (int i = 0; i < infos.Length; i++) {
- infos[i] = new ObjFileInfo();
- infos[i].fi = fis[i];
- infos[i].FileName = Path.GetFileNameWithoutExtension(fis[i].FullName);
- infos[i].ParsedNumber = GetNumberFromName(infos[i].FileName, ref infos[i].Hash);
- }
- if (SortMode == SortModeEnum.ByDate) {
- System.Array.Sort(infos, InfosDateComparer);
- } else {
- System.Array.Sort(infos, InfosNumberComparer);
- }
- SequenceName = string.Format("[{0}-{1}]", infos[0].FileName, infos[infos.Length-1].FileName );
-
- State = StateEnum.Ready;
-
- }
- public string ShortInfo {
- get {
- if (State != StateEnum.Ready) {
- return State.ToString().Replace('_',' ');
- } else {
- return string.Format("{0} {1} frames", SequenceName, Count.ToString());
- }
- }
- }
- public int Count {
- get {
- if (infos == null) {
- return 0;
- } else {
- return infos.Length;
- }
- }
- }
- public ObjFileInfo this[int index] {
- get {
- return infos[index];
- }
- }
- int InfosNumberComparer(ObjFileInfo a, ObjFileInfo b) {
- if (a.ParsedNumber == b.ParsedNumber) {
- return a.Hash - b.Hash;
- } else {
- return a.ParsedNumber - b.ParsedNumber;
- }
- }
- int InfosDateComparer(ObjFileInfo a, ObjFileInfo b) {
- int result;
- if (a.fi.LastWriteTime == b.fi.LastWriteTime) {
- result = 0;
- } else if (a.fi.LastWriteTime < b.fi.LastWriteTime) {
- result = -1;
- } else {
- result = 1;
- }
- return result;
- }
- int GetNumberFromName(string str, ref int prefixHash) {
- char[] nameChars = str.ToCharArray();
- int splitIdx = nameChars.Length;
- for (int i = nameChars.Length - 1; i >= 0; i--) {
- if (!char.IsDigit (nameChars [i])) {
- break;
- } else {
- splitIdx = i;
- }
- }
- string digitsPart = str.Substring( splitIdx );
- string prefixPart = str.Substring(0, splitIdx);
- prefixHash = prefixPart.GetHashCode();
- int index = 0;
- int.TryParse(digitsPart, out index);
- return index;
- }
-
- }
- }
|