1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/*
* MIT License
*
* Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using static ShiftOS.Objects.ShiftFS.Utils;
namespace ShiftOS.Engine
{
/// <summary>
/// Provides basic high-level access to the ShiftOS filesystem engine (ShiftFS) and File Skimmer.
/// </summary>
public static class FileSkimmerBackend
{
private static IFileSkimmer _fs = null;
/// <summary>
/// Opens a file from the specified ShiftFS path.
/// </summary>
/// <param name="path">The path to open.</param>
/// <returns>Whether or not the file could be opened.</returns>
public static bool OpenFile(string path)
{
if (!Objects.ShiftFS.Utils.FileExists(path))
throw new System.IO.FileNotFoundException("ShiftFS could not find the file specified.", path);
foreach (var type in ReflectMan.Types.Where(x => x.GetInterfaces().Contains(typeof(IFileHandler)) && Shiftorium.UpgradeAttributesUnlocked(x)))
{
foreach(FileHandlerAttribute attrib in type.GetCustomAttributes(false).Where(x => x is FileHandlerAttribute))
{
if (path.ToLower().EndsWith(attrib.Extension))
{
var obj = (IFileHandler)Activator.CreateInstance(type);
obj.OpenFile(path);
return true;
}
}
}
return false;
}
public static FileType GetFileType(string path)
{
if (path == "__upone")
return FileType.UpOne;
if (DirectoryExists(path))
{
if (Mounts.Contains(GetDirectoryInfo(path)))
return FileType.Mount;
else
return FileType.Directory;
}
string ext = path.Split('.')[path.Split('.').Length - 1];
switch (ext)
{
case "txt":
return FileType.TextFile;
case "pic":
case "png":
case "jpg":
case "bmp":
case "gif":
return FileType.Image;
case "py":
return FileType.Python;
case "mfs":
return FileType.Filesystem;
case "lua":
return FileType.Lua;
case "skn":
return FileType.Skin;
case "json":
return FileType.JSON;
case "sft":
//No, not "sex" - ShiftOS EXecutable. xD
case "sex":
return FileType.Executable;
case "cf":
return FileType.CommandFormat;
default:
return FileType.Unknown;
}
}
/// <summary>
/// Opens the specified directory path inside a new File Skimmer frontend.
/// </summary>
/// <param name="path">The path to open</param>
public static void OpenDirectory(string path)
{
_fs.OpenDirectory(path);
}
/// <summary>
/// Allows you to prompt the user to select a file, either to open or save, and filter the types of files they can select.
/// </summary>
/// <param name="types">An array of file extensions that the user may select.</param>
/// <param name="style">The UI style of the new file select frontend.</param>
/// <param name="callback">The Action that is called when the user selects a file. The string argument provided by this call is the path of the file they selected.</param>
public static void GetFile(string[] types, FileOpenerStyle style, Action<string> callback)
{
_fs.GetPath(types, style, callback);
}
/// <summary>
/// Initiates the file skimmer backend with a new middle-end layer.
/// </summary>
/// <param name="fs">The middle-end IFileSkimmer that'll do all the work.</param>
/// <remarks>Without a middle-end, the File Skimmer will not function properly.</remarks>
public static void Init(IFileSkimmer fs)
{
_fs = fs;
}
public static string GetFileExtension(FileType fileType)
{
return _fs.GetFileExtension(fileType);
}
}
/// <summary>
/// Provides primary middle-end functions allowing the File Skimmer API to talk with your frontend.
/// </summary>
public interface IFileSkimmer
{
void GetPath(string[] filetypes, FileOpenerStyle style, Action<string> callback);
void OpenDirectory(string path);
string GetFileExtension(FileType fileType);
}
/// <summary>
/// Different types of UI styles for File Openers.
/// </summary>
public enum FileOpenerStyle
{
Open,
Save
}
/// <summary>
/// Recognized file types within the ShiftFS engine.
/// </summary>
public enum FileType
{
TextFile,
Directory,
Mount,
UpOne,
Image,
Skin,
JSON,
Executable,
Lua,
Python,
Filesystem,
CommandFormat,
Unknown
}
}
|