namespace UIWidgets { using System; using System.IO; using System.Security; using UIWidgets.Styles; using UnityEngine; /// /// IOExceptionsView. /// Handle IO exceptions - catch exception and display following errors. /// public class IOExceptionsView : MonoBehaviour, IStylable { /// /// Invalid argument error message. /// [SerializeField] public GameObject ErrorArgument; /// /// Long path error message. /// [SerializeField] public GameObject ErrorLongPath; /// /// Unauthorized access error message. /// [SerializeField] public GameObject ErrorUnauthorizedAccess; /// /// Security error message. /// [SerializeField] public GameObject ErrorSecurity; /// /// Directory not found error message. /// [SerializeField] public GameObject ErrorDirectoryNotFound; /// /// IO error message. /// [SerializeField] public GameObject ErrorIO; /// /// Current error message. /// protected GameObject currentError; /// /// Current error message. /// public virtual GameObject CurrentError { get { return currentError; } set { if (currentError != null) { currentError.gameObject.SetActive(false); } currentError = value; if (currentError != null) { currentError.gameObject.SetActive(true); } } } bool isInited; /// /// Start this instance. /// public virtual void Start() { Init(); } /// /// Init this instance. /// public virtual void Init() { if (isInited) { return; } isInited = true; ErrorArgument.SetActive(false); ErrorLongPath.SetActive(false); ErrorUnauthorizedAccess.SetActive(false); ErrorSecurity.SetActive(false); ErrorDirectoryNotFound.SetActive(false); ErrorIO.SetActive(false); } /// /// Execute action and handle exceptions if raised. /// /// Type of the result. /// Type of the function argument. /// Function. /// Function argument. /// Function result. public virtual TResult Execute(Action func, TArgument argument) where TResult : class, new() { Init(); CurrentError = null; var result = new TResult(); try { func(result, argument); } catch (UnauthorizedAccessException) { // The caller does not have the required permission. CurrentError = ErrorUnauthorizedAccess; } catch (SecurityException) { // The caller does not have the required permission. CurrentError = ErrorSecurity; } catch (DirectoryNotFoundException) { // The path encapsulated in the DirectoryInfo object is invalid, such as being on an unmapped drive. CurrentError = ErrorDirectoryNotFound; } catch (PathTooLongException) { // The specified path, file name, or both exceed the system-defined maximum length. // For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters. CurrentError = ErrorLongPath; } catch (IOException) { // Path is a file name -or- network error has occurred. CurrentError = ErrorIO; } catch (ArgumentNullException) { // Path is null. CurrentError = ErrorArgument; } catch (ArgumentException) { // Path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars method. CurrentError = ErrorArgument; } return result; } /// /// Execute action and handle exceptions if raised. /// /// Type of result. /// Function. /// Function result. public virtual TResult Execute(Action func) where TResult : class, new() { Init(); CurrentError = null; var result = new TResult(); try { func(result); } catch (UnauthorizedAccessException) { // The caller does not have the required permission. CurrentError = ErrorUnauthorizedAccess; } catch (SecurityException) { // The caller does not have the required permission. CurrentError = ErrorSecurity; } catch (DirectoryNotFoundException) { // The path encapsulated in the DirectoryInfo object is invalid, such as being on an unmapped drive. CurrentError = ErrorDirectoryNotFound; } catch (PathTooLongException) { // The specified path, file name, or both exceed the system-defined maximum length. // For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters. CurrentError = ErrorLongPath; } catch (IOException) { // Path is a file name -or- network error has occurred. CurrentError = ErrorIO; } catch (ArgumentNullException) { // Path is null. CurrentError = ErrorArgument; } catch (ArgumentException) { // Path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars method. CurrentError = ErrorArgument; } return result; } /// /// Execute action and handle exceptions if raised. /// /// Action. public virtual void Execute(Action action) { Init(); CurrentError = null; try { action(); } catch (UnauthorizedAccessException) { // The caller does not have the required permission. CurrentError = ErrorUnauthorizedAccess; } catch (SecurityException) { // The caller does not have the required permission. CurrentError = ErrorSecurity; } catch (DirectoryNotFoundException) { // The path encapsulated in the DirectoryInfo object is invalid, such as being on an unmapped drive. CurrentError = ErrorDirectoryNotFound; } catch (PathTooLongException) { // The specified path, file name, or both exceed the system-defined maximum length. // For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters. CurrentError = ErrorLongPath; } catch (IOException) { // Path is a file name -or- network error has occurred. CurrentError = ErrorIO; } catch (ArgumentNullException) { // Path is null. CurrentError = ErrorArgument; } catch (ArgumentException) { // Path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars method. CurrentError = ErrorArgument; } } #region IStylable implementation /// public virtual bool SetStyle(Style style) { style.IOCollectionsErrors.ApplyTo(ErrorArgument); style.IOCollectionsErrors.ApplyTo(ErrorLongPath); style.IOCollectionsErrors.ApplyTo(ErrorUnauthorizedAccess); style.IOCollectionsErrors.ApplyTo(ErrorSecurity); style.IOCollectionsErrors.ApplyTo(ErrorDirectoryNotFound); style.IOCollectionsErrors.ApplyTo(ErrorIO); return true; } /// public virtual bool GetStyle(Style style) { style.IOCollectionsErrors.GetFrom(ErrorArgument); style.IOCollectionsErrors.GetFrom(ErrorLongPath); style.IOCollectionsErrors.GetFrom(ErrorUnauthorizedAccess); style.IOCollectionsErrors.GetFrom(ErrorSecurity); style.IOCollectionsErrors.GetFrom(ErrorDirectoryNotFound); style.IOCollectionsErrors.GetFrom(ErrorIO); return true; } #endregion } }