Skip to content

Commit ceb4241

Browse files
committed
update(GameFileSystem): More API
1 parent b76b286 commit ceb4241

File tree

5 files changed

+317
-25
lines changed

5 files changed

+317
-25
lines changed

managed/src/SwiftlyS2.Core/Modules/FileSystem/GameFileSystem.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public bool FileExists( string filePath, string pathId )
1515
return NativeFileSystem.FileExists(filePath, pathId);
1616
}
1717

18+
public uint GetFileSize( string filePath, string pathId )
19+
{
20+
return NativeFileSystem.GetFileSize(filePath, pathId);
21+
}
22+
1823
public string GetSearchPath( string pathId, GetSearchPathTypes_t searchPathType, int searchPathsToGet )
1924
{
2025
return NativeFileSystem.GetSearchPath(pathId, (int)searchPathType, searchPathsToGet);
@@ -25,13 +30,38 @@ public bool IsDirectory( string path, string pathId )
2530
return NativeFileSystem.IsDirectory(path, pathId);
2631
}
2732

33+
public bool IsFileWritable( string filePath, string pathId )
34+
{
35+
return NativeFileSystem.IsFileWritable(filePath, pathId);
36+
}
37+
38+
public bool PrecacheFile( string filePath, string pathId )
39+
{
40+
return NativeFileSystem.PrecacheFile(filePath, pathId);
41+
}
42+
2843
public void PrintSearchPaths()
2944
{
3045
NativeFileSystem.PrintSearchPaths();
3146
}
3247

48+
public string ReadFile( string filePath, string pathId )
49+
{
50+
return NativeFileSystem.ReadFile(filePath, pathId);
51+
}
52+
3353
public bool RemoveSearchPath( string path, string pathId )
3454
{
3555
return NativeFileSystem.RemoveSearchPath(path, pathId);
3656
}
57+
58+
public bool SetFileWritable( string filePath, string pathId, bool writable )
59+
{
60+
return NativeFileSystem.SetFileWritable(filePath, pathId, writable);
61+
}
62+
63+
public bool WriteFile( string filePath, string pathId, string content )
64+
{
65+
return NativeFileSystem.WriteFile(filePath, pathId, content);
66+
}
3767
}

managed/src/SwiftlyS2.Generated/Natives/FileSystem.cs

Lines changed: 167 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,6 @@ public unsafe static string GetSearchPath(string pathId, int searchPathType, int
3232
}
3333
}
3434

35-
private unsafe static delegate* unmanaged<byte*, byte*, byte> _FileExists;
36-
37-
public unsafe static bool FileExists(string fileName, string pathId) {
38-
var pool = ArrayPool<byte>.Shared;
39-
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
40-
var fileNameBuffer = pool.Rent(fileNameLength + 1);
41-
Encoding.UTF8.GetBytes(fileName, fileNameBuffer);
42-
fileNameBuffer[fileNameLength] = 0;
43-
var pathIdLength = Encoding.UTF8.GetByteCount(pathId);
44-
var pathIdBuffer = pool.Rent(pathIdLength + 1);
45-
Encoding.UTF8.GetBytes(pathId, pathIdBuffer);
46-
pathIdBuffer[pathIdLength] = 0;
47-
fixed (byte* fileNameBufferPtr = fileNameBuffer) {
48-
fixed (byte* pathIdBufferPtr = pathIdBuffer) {
49-
var ret = _FileExists(fileNameBufferPtr, pathIdBufferPtr);
50-
pool.Return(fileNameBuffer);
51-
pool.Return(pathIdBuffer);
52-
return ret == 1;
53-
}
54-
}
55-
}
56-
5735
private unsafe static delegate* unmanaged<byte*, byte*, int, int, void> _AddSearchPath;
5836

5937
public unsafe static void AddSearchPath(string path, string pathId, int searchPathAdd, int searchPathPriority) {
@@ -97,6 +75,28 @@ public unsafe static bool RemoveSearchPath(string path, string pathId) {
9775
}
9876
}
9977

78+
private unsafe static delegate* unmanaged<byte*, byte*, byte> _FileExists;
79+
80+
public unsafe static bool FileExists(string fileName, string pathId) {
81+
var pool = ArrayPool<byte>.Shared;
82+
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
83+
var fileNameBuffer = pool.Rent(fileNameLength + 1);
84+
Encoding.UTF8.GetBytes(fileName, fileNameBuffer);
85+
fileNameBuffer[fileNameLength] = 0;
86+
var pathIdLength = Encoding.UTF8.GetByteCount(pathId);
87+
var pathIdBuffer = pool.Rent(pathIdLength + 1);
88+
Encoding.UTF8.GetBytes(pathId, pathIdBuffer);
89+
pathIdBuffer[pathIdLength] = 0;
90+
fixed (byte* fileNameBufferPtr = fileNameBuffer) {
91+
fixed (byte* pathIdBufferPtr = pathIdBuffer) {
92+
var ret = _FileExists(fileNameBufferPtr, pathIdBufferPtr);
93+
pool.Return(fileNameBuffer);
94+
pool.Return(pathIdBuffer);
95+
return ret == 1;
96+
}
97+
}
98+
}
99+
100100
private unsafe static delegate* unmanaged<byte*, byte*, byte> _IsDirectory;
101101

102102
public unsafe static bool IsDirectory(string path, string pathId) {
@@ -124,4 +124,149 @@ public unsafe static bool IsDirectory(string path, string pathId) {
124124
public unsafe static void PrintSearchPaths() {
125125
_PrintSearchPaths();
126126
}
127+
128+
private unsafe static delegate* unmanaged<byte*, byte*, byte*, int> _ReadFile;
129+
130+
public unsafe static string ReadFile(string fileName, string pathId) {
131+
var pool = ArrayPool<byte>.Shared;
132+
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
133+
var fileNameBuffer = pool.Rent(fileNameLength + 1);
134+
Encoding.UTF8.GetBytes(fileName, fileNameBuffer);
135+
fileNameBuffer[fileNameLength] = 0;
136+
var pathIdLength = Encoding.UTF8.GetByteCount(pathId);
137+
var pathIdBuffer = pool.Rent(pathIdLength + 1);
138+
Encoding.UTF8.GetBytes(pathId, pathIdBuffer);
139+
pathIdBuffer[pathIdLength] = 0;
140+
fixed (byte* fileNameBufferPtr = fileNameBuffer) {
141+
fixed (byte* pathIdBufferPtr = pathIdBuffer) {
142+
var ret = _ReadFile(null, fileNameBufferPtr, pathIdBufferPtr);
143+
var retBuffer = pool.Rent(ret + 1);
144+
fixed (byte* retBufferPtr = retBuffer) {
145+
ret = _ReadFile(retBufferPtr, fileNameBufferPtr, pathIdBufferPtr);
146+
var retString = Encoding.UTF8.GetString(retBufferPtr, ret);
147+
pool.Return(retBuffer);
148+
pool.Return(fileNameBuffer);
149+
pool.Return(pathIdBuffer);
150+
return retString;
151+
}
152+
}
153+
}
154+
}
155+
156+
private unsafe static delegate* unmanaged<byte*, byte*, byte*, byte> _WriteFile;
157+
158+
public unsafe static bool WriteFile(string fileName, string pathId, string content) {
159+
var pool = ArrayPool<byte>.Shared;
160+
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
161+
var fileNameBuffer = pool.Rent(fileNameLength + 1);
162+
Encoding.UTF8.GetBytes(fileName, fileNameBuffer);
163+
fileNameBuffer[fileNameLength] = 0;
164+
var pathIdLength = Encoding.UTF8.GetByteCount(pathId);
165+
var pathIdBuffer = pool.Rent(pathIdLength + 1);
166+
Encoding.UTF8.GetBytes(pathId, pathIdBuffer);
167+
pathIdBuffer[pathIdLength] = 0;
168+
var contentLength = Encoding.UTF8.GetByteCount(content);
169+
var contentBuffer = pool.Rent(contentLength + 1);
170+
Encoding.UTF8.GetBytes(content, contentBuffer);
171+
contentBuffer[contentLength] = 0;
172+
fixed (byte* fileNameBufferPtr = fileNameBuffer) {
173+
fixed (byte* pathIdBufferPtr = pathIdBuffer) {
174+
fixed (byte* contentBufferPtr = contentBuffer) {
175+
var ret = _WriteFile(fileNameBufferPtr, pathIdBufferPtr, contentBufferPtr);
176+
pool.Return(fileNameBuffer);
177+
pool.Return(pathIdBuffer);
178+
pool.Return(contentBuffer);
179+
return ret == 1;
180+
}
181+
}
182+
}
183+
}
184+
185+
private unsafe static delegate* unmanaged<byte*, byte*, uint> _GetFileSize;
186+
187+
public unsafe static uint GetFileSize(string fileName, string pathId) {
188+
var pool = ArrayPool<byte>.Shared;
189+
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
190+
var fileNameBuffer = pool.Rent(fileNameLength + 1);
191+
Encoding.UTF8.GetBytes(fileName, fileNameBuffer);
192+
fileNameBuffer[fileNameLength] = 0;
193+
var pathIdLength = Encoding.UTF8.GetByteCount(pathId);
194+
var pathIdBuffer = pool.Rent(pathIdLength + 1);
195+
Encoding.UTF8.GetBytes(pathId, pathIdBuffer);
196+
pathIdBuffer[pathIdLength] = 0;
197+
fixed (byte* fileNameBufferPtr = fileNameBuffer) {
198+
fixed (byte* pathIdBufferPtr = pathIdBuffer) {
199+
var ret = _GetFileSize(fileNameBufferPtr, pathIdBufferPtr);
200+
pool.Return(fileNameBuffer);
201+
pool.Return(pathIdBuffer);
202+
return ret;
203+
}
204+
}
205+
}
206+
207+
private unsafe static delegate* unmanaged<byte*, byte*, byte> _PrecacheFile;
208+
209+
public unsafe static bool PrecacheFile(string fileName, string pathId) {
210+
var pool = ArrayPool<byte>.Shared;
211+
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
212+
var fileNameBuffer = pool.Rent(fileNameLength + 1);
213+
Encoding.UTF8.GetBytes(fileName, fileNameBuffer);
214+
fileNameBuffer[fileNameLength] = 0;
215+
var pathIdLength = Encoding.UTF8.GetByteCount(pathId);
216+
var pathIdBuffer = pool.Rent(pathIdLength + 1);
217+
Encoding.UTF8.GetBytes(pathId, pathIdBuffer);
218+
pathIdBuffer[pathIdLength] = 0;
219+
fixed (byte* fileNameBufferPtr = fileNameBuffer) {
220+
fixed (byte* pathIdBufferPtr = pathIdBuffer) {
221+
var ret = _PrecacheFile(fileNameBufferPtr, pathIdBufferPtr);
222+
pool.Return(fileNameBuffer);
223+
pool.Return(pathIdBuffer);
224+
return ret == 1;
225+
}
226+
}
227+
}
228+
229+
private unsafe static delegate* unmanaged<byte*, byte*, byte> _IsFileWritable;
230+
231+
public unsafe static bool IsFileWritable(string fileName, string pathId) {
232+
var pool = ArrayPool<byte>.Shared;
233+
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
234+
var fileNameBuffer = pool.Rent(fileNameLength + 1);
235+
Encoding.UTF8.GetBytes(fileName, fileNameBuffer);
236+
fileNameBuffer[fileNameLength] = 0;
237+
var pathIdLength = Encoding.UTF8.GetByteCount(pathId);
238+
var pathIdBuffer = pool.Rent(pathIdLength + 1);
239+
Encoding.UTF8.GetBytes(pathId, pathIdBuffer);
240+
pathIdBuffer[pathIdLength] = 0;
241+
fixed (byte* fileNameBufferPtr = fileNameBuffer) {
242+
fixed (byte* pathIdBufferPtr = pathIdBuffer) {
243+
var ret = _IsFileWritable(fileNameBufferPtr, pathIdBufferPtr);
244+
pool.Return(fileNameBuffer);
245+
pool.Return(pathIdBuffer);
246+
return ret == 1;
247+
}
248+
}
249+
}
250+
251+
private unsafe static delegate* unmanaged<byte*, byte*, byte, byte> _SetFileWritable;
252+
253+
public unsafe static bool SetFileWritable(string fileName, string pathId, bool writable) {
254+
var pool = ArrayPool<byte>.Shared;
255+
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
256+
var fileNameBuffer = pool.Rent(fileNameLength + 1);
257+
Encoding.UTF8.GetBytes(fileName, fileNameBuffer);
258+
fileNameBuffer[fileNameLength] = 0;
259+
var pathIdLength = Encoding.UTF8.GetByteCount(pathId);
260+
var pathIdBuffer = pool.Rent(pathIdLength + 1);
261+
Encoding.UTF8.GetBytes(pathId, pathIdBuffer);
262+
pathIdBuffer[pathIdLength] = 0;
263+
fixed (byte* fileNameBufferPtr = fileNameBuffer) {
264+
fixed (byte* pathIdBufferPtr = pathIdBuffer) {
265+
var ret = _SetFileWritable(fileNameBufferPtr, pathIdBufferPtr, writable ? (byte)1 : (byte)0);
266+
pool.Return(fileNameBuffer);
267+
pool.Return(pathIdBuffer);
268+
return ret == 1;
269+
}
270+
}
271+
}
127272
}

managed/src/SwiftlyS2.Shared/Modules/FileSystem/IGameFileSystem.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,54 @@ public interface IGameFileSystem
4848
/// <param name="searchPathsToGet">The number of search paths to get.</param>
4949
/// <returns>The search path(s) for the given path ID and search path type.</returns>
5050
public string GetSearchPath( string pathId, GetSearchPathTypes_t searchPathType, int searchPathsToGet );
51+
52+
/// <summary>
53+
/// Reads the contents of a file at the given file path and path ID.
54+
/// </summary>
55+
/// <param name="filePath">The path to the file to read.</param>
56+
/// <param name="pathId">The ID of the path to read the file from.</param>
57+
/// <returns>The contents of the file as a string.</returns>
58+
public string ReadFile( string filePath, string pathId );
59+
60+
/// <summary>
61+
/// Writes content to a file at the given file path and path ID.
62+
/// </summary>
63+
/// <param name="filePath">The path to the file to write.</param>
64+
/// <param name="pathId">The ID of the path to write the file to.</param>
65+
/// <param name="content">The content to write to the file.</param>
66+
/// <returns>True if the file was written successfully, false otherwise.</returns>
67+
public bool WriteFile( string filePath, string pathId, string content );
68+
69+
/// <summary>
70+
/// Gets the size of a file at the given file path and path ID.
71+
/// </summary>
72+
/// <param name="filePath">The path to the file to get the size of.</param>
73+
/// <param name="pathId">The ID of the path to get the file size from.</param>
74+
/// <returns>The size of the file in bytes.</returns>
75+
public uint GetFileSize( string filePath, string pathId );
76+
77+
/// <summary>
78+
/// Precaches a file at the given file path and path ID.
79+
/// </summary>
80+
/// <param name="filePath">The path to the file to precache.</param>
81+
/// <param name="pathId">The ID of the path to precache the file in.</param>
82+
/// <returns>True if the file was precached successfully, false otherwise.</returns>
83+
public bool PrecacheFile( string filePath, string pathId );
84+
85+
/// <summary>
86+
/// Checks if a file is writable at the given file path and path ID.
87+
/// </summary>
88+
/// <param name="filePath">The path to the file to check.</param>
89+
/// <param name="pathId">The ID of the path to check in.</param>
90+
/// <returns>True if the file is writable, false otherwise.</returns>
91+
public bool IsFileWritable( string filePath, string pathId );
92+
93+
/// <summary>
94+
/// Sets the writable status of a file at the given file path and path ID.
95+
/// </summary>
96+
/// <param name="filePath">The path to the file to set the writable status for.</param>
97+
/// <param name="pathId">The ID of the path to set the writable status in.</param>
98+
/// <param name="writable">True to make the file writable, false to make it read-only.</param>
99+
/// <returns>True if the writable status was set successfully, false otherwise.</returns>
100+
public bool SetFileWritable( string filePath, string pathId, bool writable );
51101
}

natives/engine/filesystem.native

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
class FileSystem
22

33
string GetSearchPath = string pathId, int32 searchPathType, int32 searchPathsToGet
4-
bool FileExists = string fileName, string pathId
54
void AddSearchPath = string path, string pathId, int32 searchPathAdd, int32 searchPathPriority
65
bool RemoveSearchPath = string path, string pathId
6+
bool FileExists = string fileName, string pathId
77
bool IsDirectory = string path, string pathId
8-
void PrintSearchPaths = void
8+
void PrintSearchPaths = void
9+
string ReadFile = string fileName, string pathId
10+
bool WriteFile = string fileName, string pathId, string content
11+
uint32 GetFileSize = string fileName, string pathId
12+
bool PrecacheFile = string fileName, string pathId
13+
bool IsFileWritable = string fileName, string pathId
14+
bool SetFileWritable = string fileName, string pathId, bool writable

0 commit comments

Comments
 (0)