forked from FosterFramework/Foster
-
Notifications
You must be signed in to change notification settings - Fork 0
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 FosterFramework#61 from kuujoo/content-improvements
Storage improvements
- Loading branch information
Showing
3 changed files
with
157 additions
and
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,114 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Foster.Framework.Storage | ||
using System.Collections; | ||
|
||
namespace Foster.Framework.Storage; | ||
|
||
/// <summary> | ||
/// Default Content implementation. | ||
/// Should work well for PC, etc, but can be overridden for custom handling. | ||
/// </summary> | ||
public class Content | ||
{ | ||
/// <summary> | ||
/// Default Content implementation. | ||
/// Should work well for PC, etc, but can be overridden for custom handling. | ||
/// </summary> | ||
public class Content | ||
public string CurrentDirectory { get; set; } = ""; | ||
|
||
private class ContentEnumerator : IEnumerator<string> | ||
{ | ||
public string CurrentDirectory { get; set; } = ""; | ||
public string[] Locations; | ||
public int Index = -1; | ||
|
||
private class ContentEnumerator : IEnumerator<string> | ||
public string Current | ||
{ | ||
public string[] Locations; | ||
public int Index = -1; | ||
|
||
public string Current | ||
get | ||
{ | ||
get | ||
try | ||
{ | ||
try | ||
{ | ||
return Locations[Index]; | ||
} | ||
catch (IndexOutOfRangeException) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
return Locations[Index]; | ||
} | ||
catch (IndexOutOfRangeException) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
|
||
object IEnumerator.Current => Current; | ||
|
||
public ContentEnumerator(string[] locations) | ||
{ | ||
Locations = locations; | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
Index++; | ||
if (Index >= Locations.Length) | ||
return false; | ||
return true; | ||
} | ||
|
||
public void Reset() => Index = -1; | ||
|
||
public void Dispose() { } | ||
} | ||
|
||
public Content() { } | ||
public Content(string content) : this() | ||
{ | ||
CurrentDirectory = content; | ||
} | ||
object IEnumerator.Current => Current; | ||
|
||
#region Directory | ||
public virtual bool FileExists(string relativePath) | ||
public ContentEnumerator(string[] locations) | ||
{ | ||
return File.Exists(CurrentDirectory + relativePath); | ||
} | ||
public virtual bool DirectoryExists(string relativePath) | ||
{ | ||
return Directory.Exists(CurrentDirectory + relativePath); | ||
} | ||
public virtual bool Exists(string name) | ||
{ | ||
return FileExists(name) || DirectoryExists(name); | ||
Locations = locations; | ||
} | ||
|
||
public virtual IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive) | ||
public bool MoveNext() | ||
{ | ||
return new ContentEnumerator( | ||
Directory.GetFiles( | ||
CurrentDirectory + path, | ||
searchPattern, | ||
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly | ||
)); | ||
Index++; | ||
if (Index >= Locations.Length) | ||
return false; | ||
return true; | ||
} | ||
|
||
public virtual IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive) | ||
{ | ||
public void Reset() => Index = -1; | ||
|
||
public void Dispose() { } | ||
} | ||
|
||
public Content() { } | ||
public Content(string content) : this() | ||
{ | ||
CurrentDirectory = content; | ||
} | ||
|
||
return new ContentEnumerator( | ||
Directory.GetDirectories( | ||
#region Directory | ||
public virtual bool FileExists(string relativePath) | ||
{ | ||
return File.Exists(Path.Combine(CurrentDirectory, relativePath)); | ||
} | ||
public virtual bool DirectoryExists(string relativePath) | ||
{ | ||
return Directory.Exists(Path.Combine(CurrentDirectory, relativePath)); | ||
} | ||
public virtual bool Exists(string name) | ||
{ | ||
return FileExists(name) || DirectoryExists(name); | ||
} | ||
|
||
public virtual IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive) | ||
{ | ||
return new ContentEnumerator( | ||
Directory.GetFiles( | ||
CurrentDirectory + path, | ||
searchPattern, | ||
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly | ||
)); | ||
} | ||
} | ||
|
||
public virtual IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive) | ||
{ | ||
|
||
#endregion | ||
return new ContentEnumerator( | ||
Directory.GetDirectories( | ||
CurrentDirectory + path, | ||
searchPattern, | ||
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly | ||
)); | ||
} | ||
|
||
#region File | ||
#endregion | ||
|
||
public virtual Stream OpenRead(string relativePath) | ||
{ | ||
return File.OpenRead(CurrentDirectory + relativePath); | ||
} | ||
#region File | ||
|
||
public virtual byte[] ReadAllBytes(string relativePath) | ||
{ | ||
return File.ReadAllBytes(CurrentDirectory + relativePath); | ||
} | ||
public virtual Stream OpenRead(string relativePath) | ||
{ | ||
return File.OpenRead(Path.Combine(CurrentDirectory, relativePath)); | ||
} | ||
|
||
public virtual string ReadAllText(string relativePath) | ||
{ | ||
return File.ReadAllText(CurrentDirectory + relativePath); | ||
} | ||
public virtual byte[] ReadAllBytes(string relativePath) | ||
{ | ||
return File.ReadAllBytes(Path.Combine(CurrentDirectory, relativePath)); | ||
} | ||
|
||
#endregion | ||
public virtual string ReadAllText(string relativePath) | ||
{ | ||
return File.ReadAllText(Path.Combine(CurrentDirectory, relativePath)); | ||
} | ||
|
||
#endregion | ||
} |
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 |
---|---|---|
@@ -1,61 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
namespace Foster.Framework.Storage; | ||
|
||
namespace Foster.Framework.Storage | ||
/// <summary> | ||
/// A storage location for user data (save files, etc) | ||
/// </summary> | ||
public static class UserStorage | ||
{ | ||
/// <summary> | ||
/// A storage location for user data (save files, etc) | ||
/// </summary> | ||
public static class UserStorage | ||
{ | ||
public static WritableContent Provider { get; set; } = new WritableContent(App.UserPath); | ||
public static WritableContent Provider { get; set; } = new WritableContent(App.UserPath); | ||
|
||
#region Directory | ||
#region Directory | ||
|
||
public static bool FileExists(string relativePath) | ||
=> Provider.FileExists(relativePath); | ||
public static bool DirectoryExists(string relativePath) | ||
=> Provider.DirectoryExists(relativePath); | ||
public static bool Exists(string name) | ||
=> Provider.Exists(name); | ||
public static bool FileExists(string relativePath) | ||
=> Provider.FileExists(relativePath); | ||
public static bool DirectoryExists(string relativePath) | ||
=> Provider.DirectoryExists(relativePath); | ||
public static bool Exists(string name) | ||
=> Provider.Exists(name); | ||
|
||
public static IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive) | ||
=> Provider.EnumerateFiles(path, searchPattern, recursive); | ||
public static IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive) | ||
=> Provider.EnumerateDirectories(path, searchPattern, recursive); | ||
public static IEnumerator<string> EnumerateFiles(string path, string searchPattern, bool recursive) | ||
=> Provider.EnumerateFiles(path, searchPattern, recursive); | ||
public static IEnumerator<string> EnumerateDirectories(string path, string searchPattern, bool recursive) | ||
=> Provider.EnumerateDirectories(path, searchPattern, recursive); | ||
|
||
public static void CreateDirectory(string path) | ||
=> Provider.CreateDirectory(path); | ||
public static void DeleteDirectory(string path, bool recursive) | ||
=> Provider.DeleteDirectory(path, recursive); | ||
public static void DeleteFile(string path) | ||
=> Provider.DeleteFile(path); | ||
public static void CreateDirectory(string path) | ||
=> Provider.CreateDirectory(path); | ||
public static void DeleteDirectory(string path, bool recursive) | ||
=> Provider.DeleteDirectory(path, recursive); | ||
public static void DeleteFile(string path) | ||
=> Provider.DeleteFile(path); | ||
|
||
#endregion | ||
#endregion | ||
|
||
#region File | ||
#region File | ||
|
||
public static Stream OpenRead(string relativePath) | ||
=> Provider.OpenRead(relativePath); | ||
public static Stream OpenRead(string relativePath) | ||
=> Provider.OpenRead(relativePath); | ||
|
||
public static byte[] ReadAllBytes(string relativePath) | ||
=> Provider.ReadAllBytes(relativePath); | ||
public static byte[] ReadAllBytes(string relativePath) | ||
=> Provider.ReadAllBytes(relativePath); | ||
|
||
public static string ReadAllText(string relativePath) | ||
=> Provider.ReadAllText(relativePath); | ||
public static string ReadAllText(string relativePath) | ||
=> Provider.ReadAllText(relativePath); | ||
|
||
|
||
public static Stream OpenWrite(string path) | ||
=> Provider.OpenWrite(path); | ||
public static void WriteAllBytes(string path, byte[] bytes) | ||
=> Provider.WriteAllBytes(path, bytes); | ||
public static void WriteAllText(string path, string text) | ||
=> Provider.WriteAllText(path, text); | ||
public static Stream OpenWrite(string path) | ||
=> Provider.OpenWrite(path); | ||
public static void WriteAllBytes(string path, byte[] bytes) | ||
=> Provider.WriteAllBytes(path, bytes); | ||
public static void WriteAllText(string path, string text) | ||
=> Provider.WriteAllText(path, text); | ||
|
||
#endregion | ||
#endregion | ||
|
||
} | ||
} |
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 |
---|---|---|
@@ -1,54 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
namespace Foster.Framework.Storage; | ||
|
||
namespace Foster.Framework.Storage | ||
/// <summary> | ||
/// A Content that may also be written to. | ||
/// </summary> | ||
public class WritableContent : Content | ||
{ | ||
/// <summary> | ||
/// A Content that may also be written to. | ||
/// </summary> | ||
public class WritableContent : Content | ||
{ | ||
public WritableContent() { } | ||
public WritableContent(string currentDirectory) : base(currentDirectory) { } | ||
|
||
#region Directory | ||
public virtual void CreateDirectory(string path) | ||
{ | ||
Directory.CreateDirectory(CurrentDirectory + path); | ||
} | ||
public WritableContent() { } | ||
public WritableContent(string currentDirectory) : base(currentDirectory) { } | ||
|
||
public virtual void DeleteDirectory(string path, bool recursive) | ||
{ | ||
Directory.Delete(CurrentDirectory + path, recursive); | ||
} | ||
#region Directory | ||
public virtual void CreateDirectory(string path) | ||
{ | ||
Directory.CreateDirectory(Path.Combine(CurrentDirectory, path)); | ||
} | ||
|
||
public virtual void DeleteFile(string path) | ||
{ | ||
File.Delete(CurrentDirectory + path); | ||
} | ||
public virtual void DeleteDirectory(string path, bool recursive) | ||
{ | ||
Directory.Delete(Path.Combine(CurrentDirectory, path), recursive); | ||
} | ||
|
||
#endregion | ||
public virtual void DeleteFile(string path) | ||
{ | ||
File.Delete(Path.Combine(CurrentDirectory, path)); | ||
} | ||
|
||
#region File | ||
#endregion | ||
|
||
public virtual Stream OpenWrite(string path) | ||
{ | ||
return File.OpenWrite(CurrentDirectory + path); | ||
} | ||
#region File | ||
|
||
public virtual void WriteAllBytes(string path, byte[] bytes) | ||
{ | ||
File.WriteAllBytes(CurrentDirectory + path, bytes); | ||
} | ||
public virtual Stream OpenWrite(string path) | ||
{ | ||
return File.OpenWrite(Path.Combine(CurrentDirectory, path)); | ||
} | ||
|
||
public virtual void WriteAllText(string path, string text) | ||
{ | ||
File.WriteAllText(CurrentDirectory + path, text); | ||
} | ||
public virtual void WriteAllBytes(string path, byte[] bytes) | ||
{ | ||
File.WriteAllBytes(Path.Combine(CurrentDirectory, path), bytes); | ||
} | ||
|
||
#endregion | ||
public virtual void WriteAllText(string path, string text) | ||
{ | ||
File.WriteAllText(Path.Combine(CurrentDirectory, path), text); | ||
} | ||
|
||
#endregion | ||
} |