-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from ToniMacaroni/development
fixed trail offset
- Loading branch information
Showing
6 changed files
with
104 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using IPA.Utilities.Async; | ||
using UnityEngine; | ||
|
||
namespace SaberFactory.UI.CustomSaber.Views | ||
{ | ||
public class SaberFileWatcher | ||
{ | ||
private const string Filter = "*.saber"; | ||
|
||
public bool IsWatching { get; private set; } | ||
private readonly DirectoryInfo _dir; | ||
|
||
private FileSystemWatcher _watcher; | ||
|
||
public SaberFileWatcher(PluginDirectories dirs) | ||
{ | ||
_dir = dirs.CustomSaberDir; | ||
} | ||
|
||
public event Action<string> OnSaberUpdate; | ||
|
||
public void Watch() | ||
{ | ||
if (_watcher is { }) StopWatching(); | ||
|
||
_watcher = new FileSystemWatcher(_dir.FullName); | ||
|
||
_watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Attributes | NotifyFilters.LastAccess; | ||
|
||
_watcher.Changed += WatcherOnChanged; | ||
|
||
_watcher.Filter = Filter; | ||
_watcher.IncludeSubdirectories = true; | ||
_watcher.EnableRaisingEvents = true; | ||
|
||
IsWatching = true; | ||
|
||
Debug.LogError($"Started watching in {_dir.FullName}"); | ||
} | ||
|
||
private void WatcherOnChanged(object sender, FileSystemEventArgs e) | ||
{ | ||
HMMainThreadDispatcher.instance.Enqueue(Initiate(e.FullPath)); | ||
} | ||
|
||
private IEnumerator Initiate(string filename) | ||
{ | ||
var seconds = 0f; | ||
while (seconds < 10) | ||
{ | ||
if (File.Exists(filename)) | ||
{ | ||
yield return new WaitForSeconds(0.5f); | ||
OnSaberUpdate?.Invoke(filename); | ||
yield break; | ||
} | ||
|
||
yield return new WaitForSeconds(0.5f); | ||
seconds += 0.5f; | ||
} | ||
} | ||
|
||
public void StopWatching() | ||
{ | ||
if (_watcher is null) return; | ||
|
||
_watcher.Changed -= WatcherOnChanged; | ||
_watcher.EnableRaisingEvents = false; | ||
_watcher.Dispose(); | ||
_watcher = null; | ||
IsWatching = false; | ||
|
||
Debug.LogError("Stopped watching"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters