Guest User

Untitled

a guest
Jan 9th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.IO;
  6. using System.Xml;
  7.  
  8. public class ContentManager : MonoBehaviour
  9. {
  10. public static ContentManager Instance;
  11.  
  12. public bool ignoreVideos;
  13.  
  14. private ProjectData[] projects;
  15. private int projectsLength;
  16.  
  17. [HideInInspector]
  18. public string currentLoadingState;
  19. [HideInInspector]
  20. public string currentLoadingItem;
  21. [HideInInspector]
  22. private int texturesTotal;
  23. [HideInInspector]
  24. private int texturesLoaded;
  25.  
  26. private string previousContent;
  27. private string currentContent;
  28. private string applicationPath;
  29.  
  30. public delegate void OnDoneLoading ();
  31. public event OnDoneLoading OnDoneLoadingEvent = delegate { };
  32. public delegate void OnLoadError (bool _critical, string _message);
  33. public event OnLoadError OnLoadErrorEvent = delegate { };
  34.  
  35. private string localContentFolderPrefix;
  36. private string SharedContentFolder;
  37. private bool checkByteLength;
  38. private bool checkDateModified;
  39. private bool mailLoadingReport;
  40.  
  41. private void Awake ()
  42. {
  43. Instance = this;
  44. applicationPath = GetApplicationPath();
  45. }
  46.  
  47. private void Start ()
  48. {
  49. StartCoroutine( "Load" );
  50. }
  51.  
  52. private bool copySucces;
  53. private IEnumerator Load ()
  54. {
  55. currentLoadingState = "";
  56. currentLoadingItem = "";
  57.  
  58. yield return null;
  59.  
  60. //load settings
  61. localContentFolderPrefix = Settings.localContentFolderPrefix; //Config.Instance.GetValue<string>( "SharedContentFolder" ); <localContentFolderPrefix>Temp_</localContentFolderPrefix>
  62. SharedContentFolder = Settings.SharedContentFolder; //Config.Instance.GetValue<string>( "localContentFolderPrefix" ); <SharedContentFolder>D:\Temp\FORUM_CONTENT_TEST</SharedContentFolder>
  63. checkByteLength = Settings.checkByteLength; //Config.Instance.GetValue<bool>( "checkByteLength" ); <checkByteLength>True</checkByteLength>
  64. checkDateModified = Settings.checkDateModified; //Config.Instance.GetValue<bool>( "checkDateModified" ); <checkDateModified>True</checkDateModified>
  65. mailLoadingReport = Settings.mailLoadingReport; //Config.Instance.GetValue<bool>( "mailLoadingReport" ); <mailLoadingReport>True</mailLoadingReport>
  66.  
  67. //Find previous content folders
  68. previousContent = "";
  69. currentContent = applicationPath + localContentFolderPrefix + System.DateTime.Now.ToString("yyyyMMddHH");
  70. yield return StartCoroutine( GetPreviousContent() );
  71.  
  72. //If previous content is too old
  73. if (previousContent != currentContent)
  74. {
  75. //If couldn't find any older content
  76. if (string.IsNullOrEmpty( previousContent ))
  77. {
  78. //Scan directories
  79. currentLoadingState = "Scanning directories";
  80. ContentDirectory contentShared;
  81. try
  82. {
  83. contentShared = ScanDirectory( SharedContentFolder, "" );
  84. }
  85. catch
  86. {
  87. OnLoadErrorEvent( false, "Unable to scan directories!" );
  88. yield break;
  89. }
  90.  
  91. yield return null;
  92.  
  93. //Copy shared to new content folder
  94. currentLoadingState = "Downloading latest content";
  95. copySucces = true;
  96. StartCoroutine( CopyDirectory( contentShared, true ) );
  97. if (!copySucces)
  98. {
  99. OnLoadErrorEvent( false, "Unable to load new content" );
  100. }
  101. }
  102. //Else could find: compare content
  103. else
  104. {
  105. //Scan directories
  106. currentLoadingState = "Scanning directories";
  107. ContentDirectory contentShared;
  108. ContentDirectory contentLastest;
  109. try
  110. {
  111. contentShared = ScanDirectory( SharedContentFolder, "" );
  112. contentLastest = ScanDirectory( previousContent, "" );
  113. }
  114. catch
  115. {
  116. OnLoadErrorEvent( false, "Unable to scan directories!" );
  117. yield break;
  118. }
  119.  
  120. yield return null;
  121.  
  122. //Compare and copy to new content folder
  123. currentLoadingState = "Copying content";
  124. copySucces = true;
  125. StartCoroutine( CompareAndCopyDirectory( contentShared, contentLastest, true ) );
  126. if (!copySucces)
  127. {
  128. currentContent = previousContent;
  129. OnLoadErrorEvent( false, "Unable to compare with new content" );
  130. }
  131. }
  132. }
  133.  
  134. yield return null;
  135.  
  136. //Load XML from directories into project data list
  137. currentLoadingState = "Preparing to load content";
  138. yield return new WaitForSeconds( 1f );
  139. List<ProjectData> list = new List<ProjectData>();
  140. yield return StartCoroutine( LoadXml( list ) );
  141.  
  142. //Save list to projects array
  143. projects = list.ToArray();
  144. projectsLength = projects.Length;
  145.  
  146. Debug.Log("Done reading xml");
  147.  
  148. //Load all textures from projects array
  149. yield return null;
  150. if(!ignoreVideos)
  151. yield return StartCoroutine( LoadContent( list ) );
  152.  
  153. //End
  154. yield return null;
  155. OnDoneLoadingEvent();
  156. }
  157.  
  158. public ProjectData GetProjectByIndex (int _index)
  159. {
  160. return projects[_index];
  161. }
  162.  
  163. public int GetAmountOfProjects ()
  164. {
  165. return projectsLength;
  166. }
  167.  
  168. #region DOWNLOAD_AND_BACKUP
  169. private IEnumerator GetPreviousContent ()
  170. {
  171. currentLoadingState = "Loading old content folders";
  172. int current = int.Parse( System.DateTime.Now.ToString( "yyyyMMddHH" ) );
  173. int latest = 0;
  174.  
  175. string[] directories;
  176. try
  177. {
  178. directories = Directory.GetDirectories( applicationPath );
  179. }
  180. catch
  181. {
  182. OnLoadErrorEvent( false, "Failed to load directories." );
  183. yield break;
  184. }
  185.  
  186. string name;
  187. for (int i = 0; i < directories.Length; i++)
  188. {
  189. name = GetLastFolderOrFile( directories[i] );
  190. if (name.Contains(localContentFolderPrefix))
  191. {
  192. int date = int.Parse( name.Substring( localContentFolderPrefix.Length ) );
  193. if (date > latest)
  194. {
  195. latest = date;
  196. }
  197. }
  198. }
  199.  
  200. if (latest == 0)
  201. {
  202. previousContent = "";
  203. yield break;
  204. }
  205.  
  206. previousContent = applicationPath + localContentFolderPrefix + latest;
  207. }
  208. private IEnumerator CompareAndCopyDirectory (ContentDirectory _shared, ContentDirectory _backup, bool _skipAllowed)
  209. {
  210. currentLoadingItem = "";
  211.  
  212. if (!CreateDirectory( currentContent + "/" + _shared.local ))
  213. {
  214. OnLoadErrorEvent( false, "Unable to create new folder! " + _shared.local );
  215. copySucces = false;
  216. yield break;
  217. }
  218.  
  219. for (int i = 0; i < _shared.directoriesLength; i++)
  220. {
  221. if (_backup.DoesContainDirectory( _shared.directories[i].name ))
  222. yield return CompareAndCopyDirectory( _shared.directories[i], _backup.GetDirectory( _shared.directories[i].name ), false );
  223. else
  224. yield return CopyDirectory( _shared.directories[i], false );
  225.  
  226. if (!copySucces)
  227. if (_skipAllowed)
  228. copySucces = true;
  229. else
  230. yield break;
  231.  
  232. if (_skipAllowed)
  233. yield return null;
  234. }
  235.  
  236. for (int i = 0; i < _shared.filesLength; i++)
  237. {
  238. ContentFile file = _backup.GetFile( _shared.files[i].name );
  239. if (file != null && (checkByteLength ? _shared.files[i].length == file.length : true) && (checkDateModified ? _shared.files[i].unixModified == file.unixModified : true))
  240. File.Copy( previousContent + "/" + _backup.local + "/" + _backup.files[i].name, currentContent + "/" + _backup.local + "/" + _backup.files[i].name );
  241. else
  242. File.Copy( SharedContentFolder + "/" + _shared.local + "/" + _shared.files[i].name, currentContent + "/" + _shared.local + "/" + _shared.files[i].name );
  243. currentLoadingItem = currentContent + "/" + _shared.local + "/" + _shared.files[i].name;
  244. yield return null;
  245. }
  246.  
  247. yield break;
  248. }
  249. private IEnumerator CopyDirectory (ContentDirectory _shared, bool _skipAllowed)
  250. {
  251. currentLoadingItem = "";
  252.  
  253. if (!CreateDirectory( currentContent + "/" + _shared.local ))
  254. {
  255. OnLoadErrorEvent( false, "Unable to create new folder! " + _shared.local );
  256. copySucces = false;
  257. yield break;
  258. }
  259.  
  260. for (int i = 0; i < _shared.directoriesLength; i++)
  261. {
  262. yield return CopyDirectory( _shared.directories[i], false );
  263.  
  264. if (!copySucces)
  265. if (_skipAllowed)
  266. copySucces = true;
  267. else
  268. yield break;
  269.  
  270. if (_skipAllowed)
  271. yield return null;
  272. }
  273.  
  274. for (int i = 0; i < _shared.filesLength; i++)
  275. {
  276. File.Copy( SharedContentFolder + "/" + _shared.local + "/" + _shared.files[i].name, currentContent + "/" + _shared.local + "/" + _shared.files[i].name );
  277. currentLoadingItem = currentContent + "/" + _shared.local + "/" + _shared.files[i].name;
  278. yield return null;
  279. }
  280.  
  281. yield break;
  282. }
  283. private bool DoesDirectoryExist (string _url, bool _createIfNot)
  284. {
  285. try
  286. {
  287. if (Directory.Exists( _url ))
  288. return true;
  289. if (!_createIfNot)
  290. return false;
  291. Directory.CreateDirectory( _url );
  292. return true;
  293. }
  294. catch
  295. {
  296. return false;
  297. }
  298. }
  299. private bool CreateDirectory (string _url)
  300. {
  301. try
  302. {
  303. Directory.CreateDirectory( _url );
  304. return true;
  305. }
  306. catch
  307. {
  308. Debug.Log( "Failed to create directory" );
  309. return false;
  310. }
  311. }
  312. private bool EmptyDirectory (string _url)
  313. {
  314. try
  315. {
  316. if(Directory.Exists(_url))
  317. Directory.Delete( _url, true );
  318. Directory.CreateDirectory( _url );
  319. return true;
  320. }
  321. catch
  322. {
  323. return false;
  324. }
  325. }
  326. private bool ReplaceDirectory (string _from, string _to)
  327. {
  328. try
  329. {
  330. if (Directory.Exists( _to ))
  331. Directory.Delete( _to, true );
  332. if (Directory.Exists( _from ))
  333. Directory.Move( _from, _to );
  334. return true;
  335. }
  336. catch
  337. {
  338. return false;
  339. }
  340. }
  341. private ContentDirectory ScanDirectory ( string _path, string _local )
  342. {
  343. List<ContentDirectory> directories = new List<ContentDirectory>();
  344. List<ContentFile> files = new List<ContentFile>();
  345. string directoryName = GetLastFolderOrFile( _path );
  346.  
  347. string[] d = Directory.GetDirectories( _path );
  348. int dLength = d.Length;
  349.  
  350. string[] f = Directory.GetFiles( _path );
  351. int fLength = f.Length;
  352.  
  353. for (int i = 0; i < dLength; i++)
  354. directories.Add( ScanDirectory( d[i], _local + "/" + GetLastFolderOrFile(d[i]) ) );
  355. for (int i = 0; i < fLength; i++)
  356. files.Add( ScanFileInfo( f[i] ) );
  357.  
  358. return new ContentDirectory( GetLastFolderOrFile( _path ), _path, _local, directories.ToArray(), dLength, files.ToArray(), fLength );
  359. }
  360. private ContentFile ScanFileInfo ( string _path )
  361. {
  362. FileInfo fi = new FileInfo( _path );
  363. return new ContentFile( GetLastFolderOrFile(_path), _path, fi.Length, fi.LastWriteTime.Ticks);
  364. }
  365. #endregion DOWNLOAD_AND_BACKUP
  366.  
  367. #region XML_HELPERS
  368.  
  369. private IEnumerator LoadXml (List<ProjectData> _list)
  370. {
  371. currentLoadingState = "Loading Directories";
  372. currentLoadingItem = "";
  373. string[] directories = null;
  374. int directoriesLength = 0;
  375. try
  376. {
  377. directories = Directory.GetDirectories( currentContent );
  378. directoriesLength = directories.Length;
  379. }
  380. catch
  381. {
  382. OnLoadErrorEvent( true, "Failed to load directory content directory" );
  383. yield break;
  384. }
  385.  
  386. int contentLength;
  387. string contentFile;
  388. ProjectContentType type;
  389.  
  390. currentLoadingState = "Loading Projects";
  391. for (int i = 0; i < directoriesLength; i++)
  392. {
  393. ProjectData project = new ProjectData();
  394.  
  395. string xml = FindXML( directories[i] + "/" );
  396. currentLoadingItem = xml;
  397.  
  398. if (!string.IsNullOrEmpty( xml ))
  399. {
  400. project.directoryName = GetLastFolderOrFile( directories[i] );
  401. project.xmlName = GetLastFolderOrFile( xml );
  402.  
  403. XmlDocument doc = EncodeXml( project, xml );
  404. if (doc != null)
  405. {
  406. project.imageURL = TryLoadXmlValue( project, doc, ProjectXmlTag.projectImage );
  407. if (!string.IsNullOrEmpty( project.imageURL ))
  408. {
  409. project.imageURL = directories[i] + "/" + project.imageURL;
  410. texturesTotal++;
  411. }
  412.  
  413. project.projectID = TryLoadXmlValue( project, doc, ProjectXmlTag.projectId );
  414. project.tags = TryLoadXmlValue( project, doc, ProjectXmlTag.projectTags ).SplitToArray<string>();
  415. project.budget = TryParseInt( project, ProjectXmlTag.projectBudget, TryLoadXmlValue( project, doc, ProjectXmlTag.projectBudget ) );
  416. project.year = TryParseInt( project, ProjectXmlTag.projectYear, TryLoadXmlValue( project, doc, ProjectXmlTag.projectYear ) );
  417. project.company = TryLoadXmlValue( project, doc, ProjectXmlTag.projectCompany );
  418.  
  419. XmlNode textDE = TryLoadXmlNode( project, doc, ProjectXmlTag.textDE );
  420. if (textDE != null)
  421. {
  422. project.textDE.name = TryLoadXmlValueInNode( project, textDE, ProjectXmlTag.textName );
  423. project.textDE.owner = TryLoadXmlValueInNode( project, textDE, ProjectXmlTag.textOwner );
  424. project.textDE.title = TryLoadXmlValueInNode( project, textDE, ProjectXmlTag.textTitle );
  425. project.textDE.introduction = TryLoadXmlValueInNode( project, textDE, ProjectXmlTag.textIntroduction );
  426. project.textDE.description = TryLoadXmlValueInNode( project, textDE, ProjectXmlTag.textDescription );
  427. }
  428. else
  429. {
  430. OnLoadErrorEvent( false, "Unable to read <" + ProjectXmlTag.textDE + "> in '/" + project.directoryName + "/" + project.xmlName + "'" );
  431. }
  432.  
  433. XmlNode textEN = TryLoadXmlNode( project, doc, ProjectXmlTag.textEN );
  434. if (textEN != null)
  435. {
  436. project.textEN.name = TryLoadXmlValueInNode( project, textEN, ProjectXmlTag.textName );
  437. project.textEN.owner = TryLoadXmlValueInNode( project, textEN, ProjectXmlTag.textOwner );
  438. project.textEN.title = TryLoadXmlValueInNode( project, textEN, ProjectXmlTag.textTitle );
  439. project.textEN.introduction = TryLoadXmlValueInNode( project, textEN, ProjectXmlTag.textIntroduction );
  440. project.textEN.description = TryLoadXmlValueInNode( project, textEN, ProjectXmlTag.textDescription );
  441. }
  442. else
  443. {
  444. OnLoadErrorEvent( false, "Unable to read <" + ProjectXmlTag.textEN + "> in '/" + project.directoryName + "/" + project.xmlName + "'" );
  445. }
  446.  
  447. XmlNodeList contentDE = TryLoadXmlNodeList( project, doc, ProjectXmlTag.contentDE );
  448. if (contentDE != null)
  449. {
  450. List<ProjectContentBase> contentList = new List<ProjectContentBase>();
  451.  
  452. contentLength = contentDE.Count;
  453. for (int c = 0; c < contentLength; c++)
  454. {
  455. contentFile = contentDE[c].InnerText;
  456. type = FileExtensionToContentType( contentFile );
  457. if (type == ProjectContentType.VIDEO)
  458. contentList.Add( new ProjectContentVideo( directories[i] + "/" + contentFile ) );
  459. else if (type == ProjectContentType.IMAGE)
  460. contentList.Add( new ProjectContentImage( directories[i] + "/" + contentFile ) );
  461. else if (string.IsNullOrEmpty( contentFile ))
  462. OnLoadErrorEvent( false, "<" + ProjectXmlTag.contentItem + "> in '/" + project.directoryName + "/" + project.xmlName + "' is empty!" );
  463. else
  464. OnLoadErrorEvent( false, "File format is not supported! '" + contentFile + "' in '/" + project.directoryName + "/" + project.xmlName + "'" );
  465. }
  466.  
  467. project.contentDE = contentList.ToArray();
  468. project.contentDELength = contentList.Count;
  469. texturesTotal += project.contentDELength;
  470. }
  471.  
  472. XmlNodeList contentEN = TryLoadXmlNodeList( project, doc, ProjectXmlTag.contentEN );
  473. if (contentEN != null)
  474. {
  475. List<ProjectContentBase> contentList = new List<ProjectContentBase>();
  476.  
  477. contentLength = contentEN.Count;
  478. for (int c = 0; c < contentLength; c++)
  479. {
  480. contentFile = contentEN[c].InnerText;
  481. type = FileExtensionToContentType( contentFile );
  482. if (type == ProjectContentType.VIDEO)
  483. contentList.Add( new ProjectContentVideo( directories[i] + "/" + contentFile ) );
  484. else if (type == ProjectContentType.IMAGE)
  485. contentList.Add( new ProjectContentImage( directories[i] + "/" + contentFile ) );
  486. else if (string.IsNullOrEmpty( contentFile ))
  487. OnLoadErrorEvent( false, "<" + ProjectXmlTag.contentItem + "> in '/" + project.directoryName + "/" + project.xmlName + "' is empty!" );
  488. else
  489. OnLoadErrorEvent( false, "File format is not supported! '" + contentFile + "' in '/" + project.directoryName + "/" + project.xmlName + "'" );
  490. }
  491.  
  492. project.contentEN = contentList.ToArray();
  493. project.contentENLength = contentList.Count;
  494. texturesTotal += project.contentENLength;
  495. }
  496.  
  497. _list.Add( project );
  498. }
  499. }
  500.  
  501. if (i % 10 == 0)
  502. yield return null;
  503. }
  504. }
  505. private string FindXML (string _url)
  506. {
  507. string xml = "";
  508.  
  509. try
  510. {
  511. string[] files = Directory.GetFiles( _url );
  512. for (int i = files.Length - 1; i > -1; i--)
  513. {
  514. if (GetExtensionFromPath( files[i] ).Contains( "xml" ))
  515. {
  516. xml = files[i];
  517. break;
  518. }
  519. }
  520. }
  521. catch
  522. {
  523. OnLoadErrorEvent( false, "Folder /" + GetLastFolderOrFile( _url ) + "/ does not contain a .xml file!" );
  524. }
  525.  
  526. return xml;
  527. }
  528. private XmlDocument EncodeXml (ProjectData _project, string _xmlUrl)
  529. {
  530. XmlDocument doc = new XmlDocument();
  531. try
  532. {
  533. doc.Load( _xmlUrl );
  534. return doc;
  535. }
  536. catch
  537. {
  538. OnLoadErrorEvent( false, "Unable to load '" + _project.xmlName + "' in '/" + _project.directoryName + "/'" );
  539. return null;
  540. }
  541. }
  542. private XmlNode TryLoadXmlNode (ProjectData _project, XmlDocument _doc, string _tag)
  543. {
  544. try
  545. {
  546. return _doc.GetElementsByTagName( _tag )[0];
  547. }
  548. catch
  549. {
  550. OnLoadErrorEvent( false, "Unable to read <" + _tag + "> in '/" + _project.directoryName + "/" + _project.xmlName + "'" );
  551. return null;
  552. }
  553. }
  554. private XmlNodeList TryLoadXmlNodeList (ProjectData _project, XmlDocument _doc, string _tag)
  555. {
  556. try
  557. {
  558. return _doc.GetElementsByTagName( _tag )[0].ChildNodes;
  559. }
  560. catch
  561. {
  562. OnLoadErrorEvent( false, "Unable to read <" + _tag + "> in '/" + _project.directoryName + "/" + _project.xmlName + "'" );
  563. return null;
  564. }
  565. }
  566. private string TryLoadXmlValue (ProjectData _project, XmlDocument _doc, string _tag)
  567. {
  568. try
  569. {
  570. return _doc.GetElementsByTagName( _tag )[0].InnerText;
  571. }
  572. catch
  573. {
  574. OnLoadErrorEvent( false, "Unable to read <" + _tag + "> in '/" + _project.directoryName + "/" + _project.xmlName + "'" );
  575. return "";
  576. }
  577. }
  578. private string TryLoadXmlValueInNode (ProjectData _project, XmlNode _node, string _tag)
  579. {
  580. try
  581. {
  582. return _node[_tag].InnerText;
  583. }
  584. catch
  585. {
  586. OnLoadErrorEvent( false, "Unable to read <" + _tag + "> in '/" + _project.directoryName + "/" + _project.xmlName + "'" );
  587. return "";
  588. }
  589. }
  590. private int TryParseInt (ProjectData _project, string _tag, string _string)
  591. {
  592. try
  593. {
  594. return int.Parse( _string );
  595. }
  596. catch
  597. {
  598. OnLoadErrorEvent( false, "<" + _tag + "> in '/" + _project.directoryName + "/" + _project.xmlName + "' is not a number!" );
  599. return 0;
  600. }
  601. }
  602.  
  603. #endregion XML_HELPERS
  604.  
  605. #region CONTENT_LOADING
  606.  
  607. private IEnumerator LoadContent (List<ProjectData> _list)
  608. {
  609. currentLoadingState = "Loading Textures";
  610. texturesTotal = 0;
  611. texturesLoaded = 0;
  612.  
  613. int length = _list.Count;
  614. for (int i = 0; i < length; i++)
  615. {
  616. yield return StartCoroutine( LoadProjectImage( _list[i] ) );
  617. yield return StartCoroutine( LoadProjectContent( _list[i], _list[i].contentDE, _list[i].contentDELength, ProjectXmlTag.contentDE ) );
  618. yield return StartCoroutine( LoadProjectContent( _list[i], _list[i].contentEN, _list[i].contentENLength, ProjectXmlTag.contentEN ) );
  619. }
  620. }
  621. private IEnumerator LoadProjectImage ( ProjectData _project )
  622. {
  623. currentLoadingItem = _project.imageURL;
  624. if (!string.IsNullOrEmpty( _project.imageURL ))
  625. {
  626. WWW www = new WWW( "File://" + _project.imageURL );
  627. yield return www;
  628. if (string.IsNullOrEmpty( www.error ))
  629. _project.image = www.texture;
  630. else
  631. OnLoadErrorEvent( false, "Failed to load <" + ProjectXmlTag.projectImage + "> in '" + _project.directoryName + "/" + _project.xmlName + "'" );
  632. texturesLoaded++;
  633. UpdateTextureLoadingProgress();
  634. }
  635. else
  636. {
  637. OnLoadErrorEvent( false, "<" + ProjectXmlTag.projectImage + "> in '" + _project.directoryName + "/" + _project.xmlName + "' is empty!" );
  638. }
  639. }
  640. private IEnumerator LoadProjectContent ( ProjectData _project, ProjectContentBase[] _array, int _arrayLength, string _tag )
  641. {
  642. for (int j = 0; j < _arrayLength; j++)
  643. {
  644. if (_array[j].type != ProjectContentType.IMAGE)
  645. continue;
  646.  
  647. string contentItemURL = (_array[j] as ProjectContentImage).textureURL;
  648. currentLoadingItem = contentItemURL;
  649. if (!string.IsNullOrEmpty( contentItemURL ))
  650. {
  651. WWW www = new WWW( "File://" + contentItemURL );
  652. yield return www;
  653. if (string.IsNullOrEmpty( www.error ))
  654. (_array[j] as ProjectContentImage).texture2d = www.texture;
  655. else
  656. OnLoadErrorEvent( false, "Failed to load <" + ProjectXmlTag.contentItem + "> " + (j + 1) + " in <" + _tag + "> in '" + _project.directoryName + "/" + _project.xmlName + "'" );
  657. texturesLoaded++;
  658. UpdateTextureLoadingProgress();
  659. }
  660. else
  661. {
  662. OnLoadErrorEvent( false, "<" + ProjectXmlTag.contentItem + "> " + (j + 1) + " in <" + _tag + "> in '" + _project.directoryName + "/" + _project.xmlName + "' is empty" );
  663. }
  664. }
  665. }
  666. private ProjectContentType FileExtensionToContentType ( string _url )
  667. {
  668. switch (GetExtensionFromPath( _url ).ToLower())
  669. {
  670. case "mp4":
  671. return ProjectContentType.VIDEO;
  672. case "jpg":
  673. return ProjectContentType.IMAGE;
  674. case "jpeg":
  675. return ProjectContentType.IMAGE;
  676. case "png":
  677. return ProjectContentType.IMAGE;
  678. default:
  679. return ProjectContentType.NULL;
  680. }
  681. }
  682. private void UpdateTextureLoadingProgress ()
  683. {
  684. currentLoadingState = "Loading Textures " + Mathf.FloorToInt( (((float)texturesLoaded) / ((float)texturesTotal)) * 100f ) + "%";
  685. }
  686.  
  687. #endregion CONTENT_LOADING
  688.  
  689. #region FILE_HELPERS
  690.  
  691. private string GetExtensionFromPath (string _path)
  692. {
  693. string[] split = _path.Split( '.' );
  694. return split[split.Length - 1].ToLower();
  695. }
  696. private string GetLastFolderOrFile (string _path)
  697. {
  698. _path = _path.Replace( @"\", "/" );
  699. string[] split = _path.Split( '/' );
  700. return split[split.Length - 1];
  701. }
  702. private string GetApplicationPath ()
  703. {
  704. string applicationPath = Application.dataPath;
  705. string[] split = applicationPath.Split( '/' );
  706. return applicationPath.Substring( 0, applicationPath.Length - split[split.Length - 1].Length );
  707. }
  708.  
  709. #endregion FILE_HELPERS
  710. }
  711.  
  712. #region DOWNLOAD_AND_BACKUP
  713.  
  714. public class ContentDirectory
  715. {
  716. public string name;
  717. public string path;
  718. public string local;
  719.  
  720. public ContentDirectory[] directories;
  721. public int directoriesLength;
  722. public ContentFile[] files;
  723. public int filesLength;
  724.  
  725. public ContentDirectory (string _name, string _path, string _local, ContentDirectory[] _directories, int _directoriesLength , ContentFile[] _files, int _filesLength)
  726. {
  727. name = _name;
  728. path = _path;
  729. local = _local;
  730.  
  731. directories = _directories;
  732. directoriesLength = _directoriesLength;
  733. files = _files;
  734. filesLength = _filesLength;
  735. }
  736.  
  737. public bool DoesContainDirectory (string _directoryName)
  738. {
  739. for (int i = 0; i < directoriesLength; i++)
  740. if (directories[i].name == _directoryName)
  741. return true;
  742. return false;
  743. }
  744.  
  745. public ContentDirectory GetDirectory (string _directoryName)
  746. {
  747. for (int i = 0; i < directoriesLength; i++)
  748. if (directories[i].name == _directoryName)
  749. return directories[i];
  750. return null;
  751. }
  752.  
  753. public bool DoesContainFile (string _fileName)
  754. {
  755. for (int i = 0; i < filesLength; i++)
  756. if (files[i].name == _fileName)
  757. return true;
  758. return false;
  759. }
  760.  
  761. public ContentFile GetFile (string _fileName)
  762. {
  763. for (int i = 0; i < filesLength; i++)
  764. if (files[i].name == _fileName)
  765. return files[i];
  766. return null;
  767. }
  768. }
  769. public class ContentFile
  770. {
  771. public string name;
  772. public string path;
  773. public long length;
  774. public long unixModified;
  775.  
  776. public ContentFile (string _name, string _path, long _length, long _unixModified)
  777. {
  778. name = _name;
  779. path = _path;
  780. length = _length;
  781. unixModified = _unixModified;
  782. }
  783. }
  784.  
  785. #endregion DOWNLOAD_AND_BACKUP
  786.  
  787. #region DATA
  788.  
  789. public class ProjectXmlTag
  790. {
  791. public static string projectId = "projectID";
  792. public static string projectImage = "projectImage";
  793. public static string projectTags = "projectTags";
  794. public static string projectBudget = "projectBudget";
  795. public static string projectYear = "projectYear";
  796. public static string projectCompany = "projectCompany";
  797.  
  798. public static string textDE = "texts_DE";
  799. public static string textEN = "texts_EN";
  800. public static string textName = "name";
  801. public static string textOwner = "owner";
  802. public static string textTitle = "title";
  803. public static string textIntroduction = "introduction";
  804. public static string textDescription = "description";
  805.  
  806. public static string contentDE = "content_DE";
  807. public static string contentEN = "content_EN";
  808. public static string contentItem = "item";
  809. }
  810. public class ProjectData
  811. {
  812. public string projectID = "";
  813.  
  814. public string directoryName = "";
  815. public string xmlName = "";
  816.  
  817. public Texture2D image = null;
  818. public string imageURL = "";
  819.  
  820. public string[] tags = new string[]{};
  821. public int budget = 0;
  822. public int year = 0;
  823. public string company = "";
  824.  
  825. public ProjectText textDE = new ProjectText();
  826. public ProjectText textEN = new ProjectText();
  827.  
  828. public ProjectContentBase[] contentDE = new ProjectContentBase[]{};
  829. public int contentDELength = 0;
  830. public ProjectContentBase[] contentEN = new ProjectContentBase[]{};
  831. public int contentENLength = 0;
  832. }
  833. public class ProjectText
  834. {
  835. public string name = "";
  836. public string owner = "";
  837. public string title = "";
  838. public string introduction = "";
  839. public string description = "";
  840. }
  841. public class ProjectContentBase
  842. {
  843. public ProjectContentType type = ProjectContentType.NULL;
  844. }
  845. public class ProjectContentImage : ProjectContentBase
  846. {
  847. public ProjectContentImage (string _url)
  848. {
  849. textureURL = _url;
  850. type = ProjectContentType.IMAGE;
  851. }
  852.  
  853. public Texture2D texture2d = null;
  854. public string textureURL = "";
  855. }
  856. public class ProjectContentVideo : ProjectContentBase
  857. {
  858. public ProjectContentVideo (string _url)
  859. {
  860. videoURL = _url;
  861. type = ProjectContentType.VIDEO;
  862. }
  863.  
  864. public string videoURL = "";
  865. }
  866. public enum ProjectContentType
  867. {
  868. NULL,
  869. IMAGE,
  870. VIDEO
  871. }
  872.  
  873. #endregion DATA
Add Comment
Please, Sign In to add comment