Restructure resource managers. Add remove resource interface and directory changed event.

This commit is contained in:
2023-11-23 00:01:29 +08:00
parent 2d7a204a30
commit 33c0826f3b
2 changed files with 39 additions and 70 deletions

View File

@@ -2,10 +2,17 @@ using System;
using System.Collections.Generic;
namespace Cryville.Crtr.Browsing {
public interface IResourceManager<T> : IReadOnlyList<T> {
Uri GetItemUri(int id);
public interface IResourceManager {
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]
string[] GetSupportedFormats();
[Obsolete]
@@ -13,13 +20,15 @@ namespace Cryville.Crtr.Browsing {
void Activate();
void Deactivate();
event Action ItemChanged;
}
public interface IResourceManager<T> : IResourceManager {
new T this[int index] { get; }
}
public interface IPathedResourceManager<T> : IResourceManager<T> {
string[] CurrentDirectory { get; }
void ChangeDirectory(string[] dir);
void OpenDirectory(int id);
event Action DirectoryChanged;
IList<string> CurrentDirectory { get; }
void ChangeDirectory(IEnumerable<string> dir);
void OpenDirectory(int index);
void ReturnToDirectory(int index);
}
}

View File

@@ -6,7 +6,6 @@ using Cryville.Crtr.Extensions.Umg;
using Cryville.Crtr.UI;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -18,13 +17,14 @@ namespace Cryville.Crtr.Browsing {
DirectoryInfo _cd;
readonly FileSystemWatcher _watcher = new FileSystemWatcher();
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; } }
static bool _init;
public event Action ItemChanged;
int _version;
public event Action DirectoryChanged;
public LegacyResourceManager(string rootPath) {
_rootPath = rootPath;
@@ -37,12 +37,12 @@ namespace Cryville.Crtr.Browsing {
_watcher.Deleted += OnFileChanged;
_watcher.Renamed += OnFileChanged;
_watcher.Error += OnFileWatcherError;
OnDirectoryChange();
}
void OnFileWatcherError(object sender, ErrorEventArgs e) {
Game.MainLogger.Log(4, "Data", "An error occurred while watching file changes: {0}", e.GetException());
}
void OnFileChanged(object sender, FileSystemEventArgs e) {
ReloadFiles();
}
@@ -50,37 +50,34 @@ namespace Cryville.Crtr.Browsing {
public void Activate() {
_watcher.EnableRaisingEvents = !string.IsNullOrEmpty(_watcher.Path);
}
public void Deactivate() {
_watcher.EnableRaisingEvents = false;
}
void ReloadFiles() {
_items = _cd.GetDirectories();
_version++;
ItemChanged?.Invoke();
}
public void ChangeDirectory(string[] dir) {
CurrentDirectory = dir;
string path = Path.Combine(_rootPath, "charts", Path.Combine(dir));
void OnDirectoryChange() {
string path = Path.Combine(_rootPath, "charts", string.Join(Path.DirectorySeparatorChar, _dirParts));
_cd = new DirectoryInfo(path);
_watcher.Path = path;
_watcher.EnableRaisingEvents = true;
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) {
string[] nd = new string[CurrentDirectory.Length + 1];
Array.Copy(CurrentDirectory, nd, CurrentDirectory.Length);
nd[CurrentDirectory.Length] = _items[id].Name;
ChangeDirectory(nd);
_dirParts.Add(_items[id].Name);
OnDirectoryChange();
}
public void ReturnToDirectory(int index) {
string[] nd = new string[index + 1];
Array.Copy(CurrentDirectory, nd, index + 1);
ChangeDirectory(nd);
_dirParts.RemoveRange(index + 1, _dirParts.Count - index - 1);
OnDirectoryChange();
}
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) {
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)));
}
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();
var file = new FileInfo(uri.LocalPath);
IEnumerable<ResourceConverter> converters;
@@ -198,48 +200,6 @@ namespace Cryville.Crtr.Browsing {
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) {
var msg = string.Format(format, args);
Game.MainLogger.Log(level, "Resource", msg);