-
Notifications
You must be signed in to change notification settings - Fork 35
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 #60 from Techiesplash/main
Framework and Platform improvements (App, Math, Graphical, Storage)
- Loading branch information
Showing
12 changed files
with
541 additions
and
222 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,121 @@ | ||
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 | ||
{ | ||
/// <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[] Locations; | ||
public int Index = -1; | ||
|
||
public string Current | ||
{ | ||
get | ||
{ | ||
try | ||
{ | ||
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; | ||
} | ||
|
||
#region Directory | ||
public virtual bool FileExists(string relativePath) | ||
{ | ||
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); | ||
} | ||
|
||
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) | ||
{ | ||
|
||
return new ContentEnumerator( | ||
Directory.GetDirectories( | ||
CurrentDirectory + path, | ||
searchPattern, | ||
recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly | ||
)); | ||
} | ||
|
||
#endregion | ||
|
||
#region File | ||
|
||
public virtual Stream OpenRead(string relativePath) | ||
{ | ||
return File.OpenRead(CurrentDirectory + relativePath); | ||
} | ||
|
||
public virtual byte[] ReadAllBytes(string relativePath) | ||
{ | ||
return File.ReadAllBytes(CurrentDirectory + relativePath); | ||
} | ||
|
||
public virtual string ReadAllText(string relativePath) | ||
{ | ||
return File.ReadAllText(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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Foster.Framework.Storage | ||
{ | ||
/// <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); | ||
|
||
#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 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); | ||
|
||
#endregion | ||
|
||
#region File | ||
|
||
public static Stream OpenRead(string relativePath) | ||
=> Provider.OpenRead(relativePath); | ||
|
||
public static byte[] ReadAllBytes(string relativePath) | ||
=> Provider.ReadAllBytes(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); | ||
|
||
#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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Foster.Framework.Storage | ||
{ | ||
/// <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 virtual void DeleteDirectory(string path, bool recursive) | ||
{ | ||
Directory.Delete(CurrentDirectory + path, recursive); | ||
} | ||
|
||
public virtual void DeleteFile(string path) | ||
{ | ||
File.Delete(CurrentDirectory + path); | ||
} | ||
|
||
#endregion | ||
|
||
#region File | ||
|
||
public virtual Stream OpenWrite(string path) | ||
{ | ||
return File.OpenWrite(CurrentDirectory + path); | ||
} | ||
|
||
public virtual void WriteAllBytes(string path, byte[] bytes) | ||
{ | ||
File.WriteAllBytes(CurrentDirectory + path, bytes); | ||
} | ||
|
||
public virtual void WriteAllText(string path, string text) | ||
{ | ||
File.WriteAllText(CurrentDirectory + path, text); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
Oops, something went wrong.