Restructure resource managers. Add remove resource interface and directory changed event.
This commit is contained in:
@@ -2,10 +2,17 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Cryville.Crtr.Browsing {
|
namespace Cryville.Crtr.Browsing {
|
||||||
public interface IResourceManager<T> : IReadOnlyList<T> {
|
public interface IResourceManager {
|
||||||
Uri GetItemUri(int id);
|
int Count { get; }
|
||||||
|
|
||||||
|
object this[int index] { get; }
|
||||||
|
Uri GetItemUri(int index);
|
||||||
|
|
||||||
|
event Action ItemChanged;
|
||||||
|
bool IsReadOnly { get; }
|
||||||
|
bool ImportFrom(Uri uri);
|
||||||
|
void RemoveAt(int index);
|
||||||
|
|
||||||
bool ImportItemFrom(Uri uri);
|
|
||||||
[Obsolete]
|
[Obsolete]
|
||||||
string[] GetSupportedFormats();
|
string[] GetSupportedFormats();
|
||||||
[Obsolete]
|
[Obsolete]
|
||||||
@@ -13,13 +20,15 @@ namespace Cryville.Crtr.Browsing {
|
|||||||
|
|
||||||
void Activate();
|
void Activate();
|
||||||
void Deactivate();
|
void Deactivate();
|
||||||
|
}
|
||||||
event Action ItemChanged;
|
public interface IResourceManager<T> : IResourceManager {
|
||||||
|
new T this[int index] { get; }
|
||||||
}
|
}
|
||||||
public interface IPathedResourceManager<T> : IResourceManager<T> {
|
public interface IPathedResourceManager<T> : IResourceManager<T> {
|
||||||
string[] CurrentDirectory { get; }
|
event Action DirectoryChanged;
|
||||||
void ChangeDirectory(string[] dir);
|
IList<string> CurrentDirectory { get; }
|
||||||
void OpenDirectory(int id);
|
void ChangeDirectory(IEnumerable<string> dir);
|
||||||
|
void OpenDirectory(int index);
|
||||||
void ReturnToDirectory(int index);
|
void ReturnToDirectory(int index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ using Cryville.Crtr.Extensions.Umg;
|
|||||||
using Cryville.Crtr.UI;
|
using Cryville.Crtr.UI;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -18,13 +17,14 @@ namespace Cryville.Crtr.Browsing {
|
|||||||
DirectoryInfo _cd;
|
DirectoryInfo _cd;
|
||||||
readonly FileSystemWatcher _watcher = new FileSystemWatcher();
|
readonly FileSystemWatcher _watcher = new FileSystemWatcher();
|
||||||
DirectoryInfo[] _items = new DirectoryInfo[0];
|
DirectoryInfo[] _items = new DirectoryInfo[0];
|
||||||
public string[] CurrentDirectory { get; private set; }
|
readonly List<string> _dirParts = new List<string>();
|
||||||
|
public IList<string> CurrentDirectory { get { return _dirParts.AsReadOnly(); } }
|
||||||
public int Count { get { return _items.Length; } }
|
public int Count { get { return _items.Length; } }
|
||||||
|
|
||||||
static bool _init;
|
static bool _init;
|
||||||
|
|
||||||
public event Action ItemChanged;
|
public event Action ItemChanged;
|
||||||
int _version;
|
public event Action DirectoryChanged;
|
||||||
|
|
||||||
public LegacyResourceManager(string rootPath) {
|
public LegacyResourceManager(string rootPath) {
|
||||||
_rootPath = rootPath;
|
_rootPath = rootPath;
|
||||||
@@ -37,12 +37,12 @@ namespace Cryville.Crtr.Browsing {
|
|||||||
_watcher.Deleted += OnFileChanged;
|
_watcher.Deleted += OnFileChanged;
|
||||||
_watcher.Renamed += OnFileChanged;
|
_watcher.Renamed += OnFileChanged;
|
||||||
_watcher.Error += OnFileWatcherError;
|
_watcher.Error += OnFileWatcherError;
|
||||||
|
OnDirectoryChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnFileWatcherError(object sender, ErrorEventArgs e) {
|
void OnFileWatcherError(object sender, ErrorEventArgs e) {
|
||||||
Game.MainLogger.Log(4, "Data", "An error occurred while watching file changes: {0}", e.GetException());
|
Game.MainLogger.Log(4, "Data", "An error occurred while watching file changes: {0}", e.GetException());
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnFileChanged(object sender, FileSystemEventArgs e) {
|
void OnFileChanged(object sender, FileSystemEventArgs e) {
|
||||||
ReloadFiles();
|
ReloadFiles();
|
||||||
}
|
}
|
||||||
@@ -50,37 +50,34 @@ namespace Cryville.Crtr.Browsing {
|
|||||||
public void Activate() {
|
public void Activate() {
|
||||||
_watcher.EnableRaisingEvents = !string.IsNullOrEmpty(_watcher.Path);
|
_watcher.EnableRaisingEvents = !string.IsNullOrEmpty(_watcher.Path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Deactivate() {
|
public void Deactivate() {
|
||||||
_watcher.EnableRaisingEvents = false;
|
_watcher.EnableRaisingEvents = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReloadFiles() {
|
void ReloadFiles() {
|
||||||
_items = _cd.GetDirectories();
|
_items = _cd.GetDirectories();
|
||||||
_version++;
|
|
||||||
ItemChanged?.Invoke();
|
ItemChanged?.Invoke();
|
||||||
}
|
}
|
||||||
|
void OnDirectoryChange() {
|
||||||
public void ChangeDirectory(string[] dir) {
|
string path = Path.Combine(_rootPath, "charts", string.Join(Path.DirectorySeparatorChar, _dirParts));
|
||||||
CurrentDirectory = dir;
|
|
||||||
string path = Path.Combine(_rootPath, "charts", Path.Combine(dir));
|
|
||||||
_cd = new DirectoryInfo(path);
|
_cd = new DirectoryInfo(path);
|
||||||
_watcher.Path = path;
|
_watcher.Path = path;
|
||||||
_watcher.EnableRaisingEvents = true;
|
_watcher.EnableRaisingEvents = true;
|
||||||
ReloadFiles();
|
ReloadFiles();
|
||||||
|
DirectoryChanged?.Invoke();
|
||||||
|
}
|
||||||
|
public void ChangeDirectory(IEnumerable<string> dir) {
|
||||||
|
_dirParts.Clear();
|
||||||
|
foreach (string dirPart in dir) _dirParts.Add(dirPart);
|
||||||
|
OnDirectoryChange();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OpenDirectory(int id) {
|
public void OpenDirectory(int id) {
|
||||||
string[] nd = new string[CurrentDirectory.Length + 1];
|
_dirParts.Add(_items[id].Name);
|
||||||
Array.Copy(CurrentDirectory, nd, CurrentDirectory.Length);
|
OnDirectoryChange();
|
||||||
nd[CurrentDirectory.Length] = _items[id].Name;
|
|
||||||
ChangeDirectory(nd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReturnToDirectory(int index) {
|
public void ReturnToDirectory(int index) {
|
||||||
string[] nd = new string[index + 1];
|
_dirParts.RemoveRange(index + 1, _dirParts.Count - index - 1);
|
||||||
Array.Copy(CurrentDirectory, nd, index + 1);
|
OnDirectoryChange();
|
||||||
ChangeDirectory(nd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChartDetail this[int index] {
|
public ChartDetail this[int index] {
|
||||||
@@ -109,6 +106,7 @@ namespace Cryville.Crtr.Browsing {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
object IResourceManager.this[int index] { get { return this[index]; } }
|
||||||
|
|
||||||
public Uri GetItemUri(int id) {
|
public Uri GetItemUri(int id) {
|
||||||
var item = _items[id];
|
var item = _items[id];
|
||||||
@@ -120,7 +118,11 @@ namespace Cryville.Crtr.Browsing {
|
|||||||
return new Uri(PlatformConfig.FileProtocolPrefix + Path.Combine(_items[id].FullName, string.Format("{0}.json", meta.data)));
|
return new Uri(PlatformConfig.FileProtocolPrefix + Path.Combine(_items[id].FullName, string.Format("{0}.json", meta.data)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ImportItemFrom(Uri uri) {
|
public bool IsReadOnly { get { return false; } }
|
||||||
|
public void RemoveAt(int index) {
|
||||||
|
_items[index].Delete(true);
|
||||||
|
}
|
||||||
|
public bool ImportFrom(Uri uri) {
|
||||||
if (!uri.IsFile) throw new NotSupportedException();
|
if (!uri.IsFile) throw new NotSupportedException();
|
||||||
var file = new FileInfo(uri.LocalPath);
|
var file = new FileInfo(uri.LocalPath);
|
||||||
IEnumerable<ResourceConverter> converters;
|
IEnumerable<ResourceConverter> converters;
|
||||||
@@ -198,48 +200,6 @@ namespace Cryville.Crtr.Browsing {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Enumerator GetEnumerator() { return new Enumerator(this); }
|
|
||||||
IEnumerator<ChartDetail> IEnumerable<ChartDetail>.GetEnumerator() { return GetEnumerator(); }
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
|
|
||||||
|
|
||||||
public struct Enumerator : IEnumerator<ChartDetail> {
|
|
||||||
readonly LegacyResourceManager _list;
|
|
||||||
int _index;
|
|
||||||
readonly int _version;
|
|
||||||
ChartDetail _current;
|
|
||||||
public ChartDetail Current => _current;
|
|
||||||
object IEnumerator.Current => Current;
|
|
||||||
|
|
||||||
public Enumerator(LegacyResourceManager list) {
|
|
||||||
_list = list;
|
|
||||||
_index = 0;
|
|
||||||
_version = list._version;
|
|
||||||
_current = default(ChartDetail);
|
|
||||||
}
|
|
||||||
public void Dispose() { }
|
|
||||||
|
|
||||||
public bool MoveNext() {
|
|
||||||
LegacyResourceManager list = _list;
|
|
||||||
if (_version != list._version)
|
|
||||||
throw new InvalidOperationException("Collection was changed.");
|
|
||||||
if (_index < list.Count) {
|
|
||||||
_current = list[_index++];
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
_current = default(ChartDetail);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IEnumerator.Reset() {
|
|
||||||
if (_version != _list._version)
|
|
||||||
throw new InvalidOperationException("Collection was changed.");
|
|
||||||
_index = 0;
|
|
||||||
_current = default(ChartDetail);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LogAndPopup(int level, string format, params object[] args) {
|
void LogAndPopup(int level, string format, params object[] args) {
|
||||||
var msg = string.Format(format, args);
|
var msg = string.Format(format, args);
|
||||||
Game.MainLogger.Log(level, "Resource", msg);
|
Game.MainLogger.Log(level, "Resource", msg);
|
||||||
|
|||||||
Reference in New Issue
Block a user