com.beanbag.tiktokminigames 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/DefaultTemplate/backgroundlandscape.png +0 -0
  2. package/DefaultTemplate/backgroundlandscape.png.meta +159 -0
  3. package/DefaultTemplate/backgroundportrait.png +0 -0
  4. package/DefaultTemplate/backgroundportrait.png.meta +159 -0
  5. package/DefaultTemplate/game.js +151 -0
  6. package/DefaultTemplate/game.js.meta +7 -0
  7. package/DefaultTemplate/images/unity_logo.png +0 -0
  8. package/DefaultTemplate/images/unity_logo.png.meta +159 -0
  9. package/DefaultTemplate/images.meta +8 -0
  10. package/DefaultTemplate/plugin-config.js +23 -0
  11. package/DefaultTemplate/plugin-config.js.meta +7 -0
  12. package/DefaultTemplate/plugin.js +1 -0
  13. package/DefaultTemplate/plugin.js.meta +7 -0
  14. package/DefaultTemplate/unity-namespace.js +11 -0
  15. package/DefaultTemplate/unity-namespace.js.meta +7 -0
  16. package/DefaultTemplate/webgl-wasm-split.js +17 -0
  17. package/DefaultTemplate/webgl-wasm-split.js.meta +7 -0
  18. package/DefaultTemplate.meta +8 -0
  19. package/Editor/Extras/Integration/DouYinMiniGameSettings.cs +30 -0
  20. package/Editor/Extras/Integration/DouYinMiniGameSettings.cs.meta +11 -0
  21. package/Editor/Extras/Integration/DouYinMiniGameSettingsEditor.cs +354 -0
  22. package/Editor/Extras/Integration/DouYinMiniGameSettingsEditor.cs.meta +3 -0
  23. package/Editor/Extras/Integration/DouYinSubplatformInterface.cs +153 -0
  24. package/Editor/Extras/Integration/DouYinSubplatformInterface.cs.meta +3 -0
  25. package/Editor/Extras/Integration.meta +8 -0
  26. package/Editor/Extras/Providers/TTMiniGameSupportProvider.cs +148 -0
  27. package/Editor/Extras/Providers/TTMiniGameSupportProvider.cs.meta +3 -0
  28. package/Editor/Extras/Providers.meta +3 -0
  29. package/Editor/Extras/TTEditorSupportProviderRegister.cs +15 -0
  30. package/Editor/Extras/TTEditorSupportProviderRegister.cs.meta +3 -0
  31. package/Editor/Extras.meta +3 -0
  32. package/Editor/ttsdk_tools.dll +0 -0
  33. package/Editor/ttsdk_tools.dll.meta +33 -0
  34. package/Editor/ttsdk_tools.pdb +0 -0
  35. package/Editor/ttsdk_tools.pdb.meta +7 -0
  36. package/Editor.meta +8 -0
  37. package/LitJson/TTLitJson.dll +0 -0
  38. package/LitJson/TTLitJson.dll.meta +33 -0
  39. package/LitJson/TTLitJson.pdb +0 -0
  40. package/LitJson/TTLitJson.pdb.meta +7 -0
  41. package/LitJson.meta +8 -0
  42. package/TTSDK/ttsdk.dll +0 -0
  43. package/TTSDK/ttsdk.dll.meta +33 -0
  44. package/TTSDK/ttsdk.pdb +0 -0
  45. package/TTSDK/ttsdk.pdb.meta +7 -0
  46. package/TTSDK.meta +8 -0
  47. package/WebGL/AssetBundle/DownloadHandlerTTAssetBundle.cs +85 -0
  48. package/WebGL/AssetBundle/DownloadHandlerTTAssetBundle.cs.meta +3 -0
  49. package/WebGL/AssetBundle/TTAssetBundle.cs +72 -0
  50. package/WebGL/AssetBundle/TTAssetBundle.cs.meta +3 -0
  51. package/WebGL/AssetBundle/TTAssetBundleExtensions.cs +25 -0
  52. package/WebGL/AssetBundle/TTAssetBundleExtensions.cs.meta +3 -0
  53. package/WebGL/AssetBundle/TTAssetBundleProviders.cs.txt +1301 -0
  54. package/WebGL/AssetBundle/TTAssetBundleProviders.cs.txt.meta +3 -0
  55. package/WebGL/AssetBundle/TTAssetBundleRequest.cs +96 -0
  56. package/WebGL/AssetBundle/TTAssetBundleRequest.cs.meta +3 -0
  57. package/WebGL/AssetBundle.meta +8 -0
  58. package/WebGL/Compatibility/UnityCacheCompatibility.cs +49 -0
  59. package/WebGL/Compatibility/UnityCacheCompatibility.cs.meta +3 -0
  60. package/WebGL/Compatibility/UnityWebRequestCompatibility.cs +18 -0
  61. package/WebGL/Compatibility/UnityWebRequestCompatibility.cs.meta +3 -0
  62. package/WebGL/Compatibility.meta +3 -0
  63. package/WebGL/FileSystem/TTFileSystemManager.cs +460 -0
  64. package/WebGL/FileSystem/TTFileSystemManager.cs.meta +11 -0
  65. package/WebGL/FileSystem/TTFileSystemManagerDefault.cs +1039 -0
  66. package/WebGL/FileSystem/TTFileSystemManagerDefault.cs.meta +11 -0
  67. package/WebGL/FileSystem/TTFileSystemManagerWebGL.cs +1278 -0
  68. package/WebGL/FileSystem/TTFileSystemManagerWebGL.cs.meta +11 -0
  69. package/WebGL/FileSystem.meta +3 -0
  70. package/WebGL/Input/Input.cs +230 -0
  71. package/WebGL/Input/Input.cs.meta +3 -0
  72. package/WebGL/Input/TTPCInputHandler.cs +199 -0
  73. package/WebGL/Input/TTPCInputHandler.cs.meta +3 -0
  74. package/WebGL/Input.meta +3 -0
  75. package/WebGL/Network/TTUDPSocketHandler.cs +130 -0
  76. package/WebGL/Network/TTUDPSocketHandler.cs.meta +11 -0
  77. package/WebGL/Network.meta +3 -0
  78. package/WebGL/TTAssetBundle.jslib +33 -0
  79. package/WebGL/TTAssetBundle.jslib.meta +32 -0
  80. package/WebGL/TTCallbackHandler.cs +139 -0
  81. package/WebGL/TTCallbackHandler.cs.meta +3 -0
  82. package/WebGL/TTModel.cs +582 -0
  83. package/WebGL/TTModel.cs.meta +11 -0
  84. package/WebGL/TTSDK.jslib +592 -0
  85. package/WebGL/TTSDK.jslib.meta +32 -0
  86. package/WebGL/TTSDKType.cs +10 -0
  87. package/WebGL/TTSDKType.cs.meta +3 -0
  88. package/WebGL/TTStorageManager.cs +155 -0
  89. package/WebGL/TTStorageManager.cs.meta +11 -0
  90. package/WebGL/TTUNBridge.jslib +117 -0
  91. package/WebGL/TTUNBridge.jslib.meta +32 -0
  92. package/WebGL/TTVersionAttribute.cs +21 -0
  93. package/WebGL/TTVersionAttribute.cs.meta +11 -0
  94. package/WebGL/TTWebGL.asmdef +18 -0
  95. package/WebGL/TTWebGL.asmdef.meta +7 -0
  96. package/WebGL/TTWebGLInterface.cs +63 -0
  97. package/WebGL/TTWebGLInterface.cs.meta +11 -0
  98. package/WebGL.meta +8 -0
  99. package/package.json +13 -0
  100. package/package.json.meta +7 -0
@@ -0,0 +1,1278 @@
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Runtime.InteropServices;
4
+ using TTSDK.UNBridgeLib.LitJson;
5
+ using UnityEngine;
6
+ using Object = UnityEngine.Object;
7
+
8
+ namespace TTSDK
9
+ {
10
+ public class TTFileSystemManagerWebGL : TTFileSystemManager
11
+ {
12
+ #region 非流式读写
13
+ #if UNITY_WEBGL && !UNITY_EDITOR
14
+ [DllImport("__Internal")]
15
+ private static extern string StarkWriteStringFileSync(string filePath, string data, string encoding);
16
+ #else
17
+ private static string StarkWriteStringFileSync(string filePath, string data, string encoding)
18
+ {
19
+ return TTFileSystemManagerDefault.Instance.WriteFileSync(filePath, data, encoding);
20
+ }
21
+ #endif
22
+
23
+ #if UNITY_WEBGL && !UNITY_EDITOR
24
+ [DllImport("__Internal")]
25
+ private static extern string StarkWriteBinFileSync(string filePath, byte[] data, int dataLength);
26
+ #else
27
+ private static string StarkWriteBinFileSync(string filePath, byte[] data, int dataLength)
28
+ {
29
+ return TTFileSystemManagerDefault.Instance.WriteFileSync(filePath, data);
30
+ }
31
+ #endif
32
+
33
+ #if UNITY_WEBGL && !UNITY_EDITOR
34
+ [DllImport("__Internal")]
35
+ private static extern void StarkWriteBinFile(string filePath, byte[] data, int dataLength, string s, string f);
36
+ #else
37
+ private static void StarkWriteBinFile(string filePath, byte[] data, int dataLength, string s, string f)
38
+ {
39
+ TTFileSystemManagerDefault.Instance.WriteFileSync(filePath, data);
40
+ }
41
+ #endif
42
+
43
+ #if UNITY_WEBGL && !UNITY_EDITOR
44
+ [DllImport("__Internal")]
45
+ private static extern void StarkWriteStringFile(string filePath, string data, string encoding, string s,
46
+ string f);
47
+ #else
48
+ private static void StarkWriteStringFile(string filePath, string data, string encoding, string s,
49
+ string f)
50
+ {
51
+ TTFileSystemManagerDefault.Instance.WriteFileSync(filePath, data, encoding);
52
+ }
53
+ #endif
54
+
55
+ #if UNITY_WEBGL && !UNITY_EDITOR
56
+ [DllImport("__Internal")]
57
+ private static extern void StarkReadFile(string filePath, string encoding, string callbackId);
58
+ #else
59
+ private static void StarkReadFile(string filePath, string encoding, string callbackId)
60
+ {
61
+ }
62
+ #endif
63
+
64
+ #if UNITY_WEBGL && !UNITY_EDITOR
65
+ [DllImport("__Internal")]
66
+ private static extern string StarkReadStringFileSync(string filePath, string encoding);
67
+ #else
68
+ private static string StarkReadStringFileSync(string filePath, string encoding)
69
+ {
70
+ return TTFileSystemManagerDefault.Instance.ReadFileSync(filePath, encoding);
71
+ }
72
+ #endif
73
+
74
+ #if UNITY_WEBGL && !UNITY_EDITOR
75
+ [DllImport("__Internal")]
76
+ private static extern int StarkReadBinFileSync(string filePath);
77
+ #else
78
+ private static int StarkReadBinFileSync(string filePath)
79
+ {
80
+ return 0;
81
+ }
82
+ #endif
83
+
84
+ #if UNITY_WEBGL && !UNITY_EDITOR
85
+ [DllImport("__Internal")]
86
+ private static extern void StarkShareFileBuffer(byte[] data, string callbackId);
87
+ #else
88
+ private static void StarkShareFileBuffer(byte[] data, string callbackId)
89
+ {
90
+ }
91
+ #endif
92
+
93
+ #if UNITY_WEBGL && !UNITY_EDITOR
94
+ [DllImport("__Internal")]
95
+ private static extern bool StarkAccessFileSync(string filePath);
96
+ #else
97
+ private static bool StarkAccessFileSync(string filePath)
98
+ {
99
+ return TTFileSystemManagerDefault.Instance.AccessSync(filePath);
100
+ }
101
+ #endif
102
+
103
+ #if UNITY_WEBGL && !UNITY_EDITOR
104
+ [DllImport("__Internal")]
105
+ private static extern void StarkAccessFile(string filePath, string s, string f);
106
+ #else
107
+ private static void StarkAccessFile(string filePath, string s, string f)
108
+ {
109
+ }
110
+ #endif
111
+
112
+ #if UNITY_WEBGL && !UNITY_EDITOR
113
+ [DllImport("__Internal")]
114
+ private static extern string StarkCopyFileSync(string srcPath, string destPath);
115
+ #else
116
+ private static string StarkCopyFileSync(string srcPath, string destPath)
117
+ {
118
+ return TTFileSystemManagerDefault.Instance.CopyFileSync(srcPath, destPath);
119
+ }
120
+ #endif
121
+
122
+ #if UNITY_WEBGL && !UNITY_EDITOR
123
+ [DllImport("__Internal")]
124
+ private static extern string StarkRenameFileSync(string srcPath, string destPath);
125
+ #else
126
+ private static string StarkRenameFileSync(string srcPath, string destPath)
127
+ {
128
+ return TTFileSystemManagerDefault.Instance.RenameFileSync(srcPath, destPath);
129
+ }
130
+ #endif
131
+
132
+ #if UNITY_WEBGL && !UNITY_EDITOR
133
+ [DllImport("__Internal")]
134
+ private static extern void StarkCopyFile(string srcPath, string destPath, string s, string f);
135
+ #else
136
+ private static void StarkCopyFile(string srcPath, string destPath, string s, string f)
137
+ {
138
+ TTFileSystemManagerDefault.Instance.CopyFileSync(srcPath, destPath);
139
+ }
140
+ #endif
141
+
142
+ #if UNITY_WEBGL && !UNITY_EDITOR
143
+ [DllImport("__Internal")]
144
+ private static extern void StarkRenameFile(string srcPath, string destPath, string s, string f);
145
+ #else
146
+ private static void StarkRenameFile(string srcPath, string destPath, string s, string f)
147
+ {
148
+ TTFileSystemManagerDefault.Instance.RenameFileSync(srcPath, destPath);
149
+ }
150
+ #endif
151
+
152
+ #if UNITY_WEBGL && !UNITY_EDITOR
153
+ [DllImport("__Internal")]
154
+ private static extern string StarkUnlinkSync(string filePath);
155
+ #else
156
+ private static string StarkUnlinkSync(string filePath)
157
+ {
158
+ return TTFileSystemManagerDefault.Instance.UnlinkSync(filePath);
159
+ }
160
+ #endif
161
+
162
+ #if UNITY_WEBGL && !UNITY_EDITOR
163
+ [DllImport("__Internal")]
164
+ private static extern void StarkUnlink(string filePath, string s, string f);
165
+ #else
166
+ private static void StarkUnlink(string filePath, string s, string f)
167
+ {
168
+ TTFileSystemManagerDefault.Instance.UnlinkSync(filePath);
169
+ }
170
+ #endif
171
+
172
+ #if UNITY_WEBGL && !UNITY_EDITOR
173
+ [DllImport("__Internal")]
174
+ private static extern void StarkMkdir(string dirPath, bool recursive, string s, string f);
175
+ #else
176
+ private static void StarkMkdir(string dirPath, bool recursive, string s, string f)
177
+ {
178
+ TTFileSystemManagerDefault.Instance.MkdirSync(dirPath);
179
+ }
180
+ #endif
181
+
182
+ #if UNITY_WEBGL && !UNITY_EDITOR
183
+ [DllImport("__Internal")]
184
+ private static extern string StarkMkdirSync(string dirPath, bool recursive);
185
+ #else
186
+ private static string StarkMkdirSync(string dirPath, bool recursive)
187
+ {
188
+ return TTFileSystemManagerDefault.Instance.MkdirSync(dirPath);
189
+ }
190
+ #endif
191
+
192
+ #if UNITY_WEBGL && !UNITY_EDITOR
193
+ [DllImport("__Internal")]
194
+ private static extern void StarkRmdir(string dirPath, bool recursive, string s, string f);
195
+ #else
196
+ private static void StarkRmdir(string dirPath, bool recursive, string s, string f)
197
+ {
198
+ TTFileSystemManagerDefault.Instance.RmdirSync(dirPath);
199
+ }
200
+ #endif
201
+
202
+ #if UNITY_WEBGL && !UNITY_EDITOR
203
+ [DllImport("__Internal")]
204
+ private static extern string StarkRmdirSync(string dirPath, bool recursive);
205
+ #else
206
+ private static string StarkRmdirSync(string dirPath, bool recursive)
207
+ {
208
+ return TTFileSystemManagerDefault.Instance.RmdirSync(dirPath);
209
+ }
210
+ #endif
211
+
212
+ #if UNITY_WEBGL && !UNITY_EDITOR
213
+ [DllImport("__Internal")]
214
+ private static extern string StarkStatSync(string path);
215
+ #else
216
+ private static string StarkStatSync(string path)
217
+ {
218
+ return "";
219
+ }
220
+ #endif
221
+
222
+ #if UNITY_WEBGL && !UNITY_EDITOR
223
+ [DllImport("__Internal")]
224
+ private static extern void StarkStat(string path, string callbackId);
225
+ #else
226
+ private static void StarkStat(string path, string callbackId)
227
+ {
228
+ }
229
+ #endif
230
+
231
+ #if UNITY_WEBGL && !UNITY_EDITOR
232
+ [DllImport("__Internal")]
233
+ private static extern void StarkGetSavedFileList(string callbackId);
234
+ #else
235
+ private static void StarkGetSavedFileList(string callbackId)
236
+ {
237
+ }
238
+ #endif
239
+
240
+ #if UNITY_WEBGL && !UNITY_EDITOR
241
+ [DllImport("__Internal")]
242
+ private static extern string StarkGetCachedPathForUrl(string url);
243
+ #else
244
+ private static string StarkGetCachedPathForUrl(string url)
245
+ {
246
+ return "";
247
+ }
248
+ #endif
249
+
250
+ #if UNITY_WEBGL && !UNITY_EDITOR
251
+ [DllImport("__Internal")]
252
+ private static extern string StarkAppendStringFileSync(string filePath, string data, string encoding);
253
+ #else
254
+ private static string StarkAppendStringFileSync(string filePath, string data, string encoding)
255
+ {
256
+ return TTFileSystemManagerDefault.Instance.AppendFileSync(filePath, data, encoding);
257
+ }
258
+ #endif
259
+
260
+ #if UNITY_WEBGL && !UNITY_EDITOR
261
+ [DllImport("__Internal")]
262
+ private static extern string StarkAppendBinFileSync(string filePath, byte[] data, int dataLength);
263
+ #else
264
+ private static string StarkAppendBinFileSync(string filePath, byte[] data, int dataLength)
265
+ {
266
+ return TTFileSystemManagerDefault.Instance.AppendFileSync(filePath, data);
267
+ }
268
+ #endif
269
+
270
+ #if UNITY_WEBGL && !UNITY_EDITOR
271
+ [DllImport("__Internal")]
272
+ private static extern void StarkAppendStringFile(string filePath, string data, string encoding, string s, string f);
273
+ #else
274
+ private static void StarkAppendStringFile(string filePath, string data, string encoding, string s, string f)
275
+ {
276
+ TTFileSystemManagerDefault.Instance.AppendFileSync(filePath, data, encoding);
277
+ }
278
+ #endif
279
+
280
+ #if UNITY_WEBGL && !UNITY_EDITOR
281
+ [DllImport("__Internal")]
282
+ private static extern void StarkAppendBinFile(string filePath, byte[] data, int dataLength, string s, string f);
283
+ #else
284
+ private static void StarkAppendBinFile(string filePath, byte[] data, int dataLength, string s, string f)
285
+ {
286
+ TTFileSystemManagerDefault.Instance.AppendFileSync(filePath, data);
287
+ }
288
+ #endif
289
+
290
+ #if UNITY_WEBGL && !UNITY_EDITOR
291
+ [DllImport("__Internal")]
292
+ private static extern void StarkTruncate(string filePath, int length, string s, string f);
293
+ #else
294
+ private static void StarkTruncate(string filePath, int length, string s, string f)
295
+ {
296
+ TTFileSystemManagerDefault.Instance.TruncateSync(filePath, length);
297
+ }
298
+ #endif
299
+
300
+ #if UNITY_WEBGL && !UNITY_EDITOR
301
+ [DllImport("__Internal")]
302
+ private static extern string StarkTruncateSync(string filePath, int length);
303
+ #else
304
+ private static string StarkTruncateSync(string filePath, int length)
305
+ {
306
+ return TTFileSystemManagerDefault.Instance.TruncateSync(filePath, length);
307
+ }
308
+ #endif
309
+
310
+ #if UNITY_WEBGL && !UNITY_EDITOR
311
+ [DllImport("__Internal")]
312
+ private static extern void StarkReadDir(string filePath, string callbackId);
313
+ #else
314
+ private static void StarkReadDir(string filePath, string callbackId)
315
+ {
316
+ TTFileSystemManagerDefault.Instance.ReadDirSync(filePath);
317
+ }
318
+ #endif
319
+
320
+ #if UNITY_WEBGL && !UNITY_EDITOR
321
+ [DllImport("__Internal")]
322
+ private static extern string StarkReadDirSync(string filePath);
323
+ #else
324
+ private static string StarkReadDirSync(string filePath)
325
+ {
326
+ return TTFileSystemManagerDefault.Instance.ReadDirSync(filePath)[0];
327
+ }
328
+ #endif
329
+ #endregion
330
+
331
+ #region 流式读写
332
+ #if UNITY_WEBGL && !UNITY_EDITOR
333
+ //webgl
334
+ [DllImport("__Internal")]
335
+ private static extern void TT_FOpenFile(string filePath, string flag, string callbackId);
336
+ [DllImport("__Internal")]
337
+ private static extern string TT_FOpenFileSync(string filePath, string flag);
338
+ [DllImport("__Internal")]
339
+ private static extern void TT_FCloseFile(string fd, string s, string f);
340
+ [DllImport("__Internal")]
341
+ private static extern string TT_FCloseFileSync(string fd);
342
+ [DllImport("__Internal")]
343
+ private static extern void TT_FWriteBinFile(string fd, byte[] data, int dataLength, int offset, int length, string callbackId, int position);
344
+ [DllImport("__Internal")]
345
+ private static extern void TT_FWriteStringFile(string fd, string data, string encoding, string callbackId);
346
+ [DllImport("__Internal")]
347
+ private static extern string TT_FWriteBinFileSync(string fd, byte[] data, int dataLength, int offset, int length, int position);
348
+ [DllImport("__Internal")]
349
+ private static extern string TT_FWriteStringFileSync(string fd, string data, string encoding);
350
+ [DllImport("__Internal")]
351
+ private static extern void TT_FReadFile(string fd, int arrayBufferLength, int offset, int length, int position, string callbackId);
352
+ [DllImport("__Internal")]
353
+ private static extern string TT_FReadFileSync(string fd, int arrayBufferLength, int offset, int length, int position);
354
+ [DllImport("__Internal")]
355
+ private static extern void TT_FReadCompressedFile(string filePath, string compressionAlgorithm, string callbackId);
356
+ [DllImport("__Internal")]
357
+ private static extern string TT_FReadCompressedFileSync(string filePath, string compressionAlgorithm);
358
+ [DllImport("__Internal")]
359
+ private static extern void TT_Fstat(string fd, string callbackId);
360
+ [DllImport("__Internal")]
361
+ private static extern string TT_FstatSync(string fd);
362
+ [DllImport("__Internal")]
363
+ private static extern void TT_Ftruncate(string fd, int length, string s, string f);
364
+ [DllImport("__Internal")]
365
+ private static extern string TT_FtruncateSync(string fd, int length);
366
+ #else
367
+ //native
368
+ private static void TT_FOpenFile(string filePath, string flag, string callbackId) {}
369
+ private static string TT_FOpenFileSync(string filePath, string flag) { return null; }
370
+ private static void TT_FCloseFile(string fd, string s, string f) {}
371
+ private static string TT_FCloseFileSync(string fd) { return ""; }
372
+ private static void TT_FWriteBinFile(string fd, byte[] data, int dataLength, int offset, int length, string callbackId, int position) {}
373
+ private static void TT_FWriteStringFile(string fd, string data, string encoding, string callbackId) {}
374
+ private static string TT_FWriteBinFileSync(string fd, byte[] data, int dataLength, int offset, int length, int position) {return ""; }
375
+ private static string TT_FWriteStringFileSync(string fd, string data, string encoding) {return "";}
376
+ private static void TT_FReadFile(string fd, int arrayBufferLength, int offset, int length, int position, string callbackId) {}
377
+ private static string TT_FReadFileSync(string fd, int arrayBufferLength, int offset, int length, int position) { return ""; }
378
+ private static void TT_FReadCompressedFile(string filePath, string compressionAlgorithm, string callbackId) {}
379
+ private static string TT_FReadCompressedFileSync(string filePath, string compressionAlgorithm) { return ""; }
380
+ private static void TT_Fstat(string fd, string callbackId) {}
381
+ private static string TT_FstatSync(string fd) { return null; }
382
+ private static void TT_Ftruncate(string fd, int length, string s, string f) {}
383
+ private static string TT_FtruncateSync(string fd, int length) { return ""; }
384
+ #endif
385
+ #endregion
386
+
387
+
388
+ public static readonly TTFileSystemManagerWebGL Instance = new TTFileSystemManagerWebGL();
389
+
390
+ private static Dictionary<string, ReadFileParam> s_readFileParams = new Dictionary<string, ReadFileParam>();
391
+ private static Dictionary<string, StatParam> s_statParams = new Dictionary<string, StatParam>();
392
+
393
+ private static Dictionary<string, GetSavedFileListParam> s_getSavedFileListParams =
394
+ new Dictionary<string, GetSavedFileListParam>();
395
+ private static Dictionary<string, ReadDirParam> s_readDirParams = new Dictionary<string, ReadDirParam>();
396
+
397
+ #region 流式读写
398
+ private static Dictionary<string, OpenParam> s_openParams = new();
399
+ private static Dictionary<string, WriteParam> s_writeParams = new();
400
+ private static Dictionary<string, ReadParam> s_readParams = new();
401
+ private static Dictionary<string, ReadCompressedFileParam> s_readCompressedFileParams = new();
402
+ private static Dictionary<string, FstatParam> s_fstatParams = new();
403
+ #endregion
404
+
405
+ private static bool _initialized;
406
+
407
+ public TTFileSystemManagerWebGL()
408
+ {
409
+ MigratingData();
410
+ CreateStarkFileSystemHandler();
411
+ }
412
+
413
+ private void CreateStarkFileSystemHandler()
414
+ {
415
+ if (!_initialized)
416
+ {
417
+ _initialized = true;
418
+ GameObject obj = new GameObject();
419
+ Object.DontDestroyOnLoad(obj);
420
+ obj.name = "StarkFileSystemManager";
421
+ obj.AddComponent<TTFileSystemHandler>();
422
+ }
423
+ }
424
+
425
+ public class TTFileSystemHandler : MonoBehaviour
426
+ {
427
+ private void SafeHandle(string handlerName, string msg, Action<string> action)
428
+ {
429
+ try
430
+ {
431
+ Debug.Log($"{handlerName} - {msg}");
432
+ action(msg);
433
+ }
434
+ catch (Exception e)
435
+ {
436
+ Debug.LogError($"{handlerName} exception: {e}");
437
+ }
438
+ }
439
+
440
+ public void HandleNativeCallback(string msg) => SafeHandle(nameof(HandleNativeCallback), msg, m =>
441
+ {
442
+ TTCallbackHandler.InvokeResponseCallback<TTBaseResponse>(m);
443
+ });
444
+
445
+ public void HandleReadFileCallback(string msg) => SafeHandle(nameof(HandleReadFileCallback), msg, m =>
446
+ {
447
+ var res = JsonUtility.FromJson<TTReadFileCallback>(m);
448
+ var conf = s_readFileParams[res.callbackId];
449
+ if (conf == null)
450
+ {
451
+ Debug.LogWarning($"HandleReadFileCallback - no callback for callbackId: {res.callbackId}");
452
+ return;
453
+ }
454
+
455
+ s_readFileParams.Remove(res.callbackId);
456
+
457
+ if (res.errCode == 0)
458
+ {
459
+ if (string.IsNullOrEmpty(conf.encoding) || conf.encoding.Equals("binary"))
460
+ {
461
+ var sharedBuffer = new byte[res.byteLength];
462
+ StarkShareFileBuffer(sharedBuffer, res.callbackId);
463
+ var obj = new TTReadFileResponse()
464
+ {
465
+ binData = sharedBuffer,
466
+ };
467
+ conf.success?.Invoke(obj);
468
+ }
469
+ else
470
+ {
471
+ var obj = new TTReadFileResponse()
472
+ {
473
+ stringData = res.data
474
+ };
475
+ conf.success?.Invoke(obj);
476
+ }
477
+ }
478
+ else
479
+ {
480
+ var obj = new TTReadFileResponse()
481
+ {
482
+ errCode = res.errCode,
483
+ errMsg = res.errMsg
484
+ };
485
+ conf.fail?.Invoke(obj);
486
+ }
487
+ });
488
+
489
+ public void HandleStatCallback(string msg) => SafeHandle(nameof(HandleStatCallback), msg, m =>
490
+ {
491
+ var res = JsonMapper.ToObject<TTStatResponse>(m);
492
+ if (res == null)
493
+ {
494
+ Debug.LogError("empty response");
495
+ return;
496
+ }
497
+ var conf = s_statParams[res.callbackId];
498
+ if (conf == null)
499
+ {
500
+ Debug.LogWarning($"HandleStatCallback - no callback for callbackId: {res.callbackId}");
501
+ return;
502
+ }
503
+
504
+ s_statParams.Remove(res.callbackId);
505
+
506
+ if (res.errCode == 0)
507
+ {
508
+ if (res.stat == null)
509
+ {
510
+ Debug.LogWarning("empty stat info");
511
+ res.stat = new TTStatInfo();
512
+ }
513
+ conf.success?.Invoke(res);
514
+ }
515
+ else
516
+ {
517
+ res.stat = new TTStatInfo();
518
+ conf.fail?.Invoke(res);
519
+ }
520
+ });
521
+
522
+ public void HandleGetSavedFileListCallback(string msg) => SafeHandle(nameof(HandleGetSavedFileListCallback), msg, m =>
523
+ {
524
+ var res = JsonMapper.ToObject<TTGetSavedFileListResponse>(m);
525
+ if (res == null)
526
+ {
527
+ Debug.LogError("empty response");
528
+ return;
529
+ }
530
+ var conf = s_getSavedFileListParams[res.callbackId];
531
+ if (conf == null)
532
+ {
533
+ Debug.LogWarning($"HandleGetSavedFileListCallback - no callback for callbackId: {res.callbackId}");
534
+ return;
535
+ }
536
+
537
+ s_getSavedFileListParams.Remove(res.callbackId);
538
+
539
+ if (res.errCode == 0)
540
+ {
541
+ if (res.fileList == null)
542
+ {
543
+ res.fileList = new TTFileInfo[0];
544
+ }
545
+
546
+ conf.success?.Invoke(res);
547
+ }
548
+ else
549
+ {
550
+ res.fileList = new TTFileInfo[0];
551
+ conf.fail?.Invoke(res);
552
+ }
553
+ });
554
+
555
+ public void HandleReadDirCallback(string msg) => SafeHandle(nameof(HandleReadDirCallback), msg, m =>
556
+ {
557
+ var res = JsonMapper.ToObject<TTReadDirResponse>(m);
558
+ if (res == null)
559
+ {
560
+ Debug.LogError("empty response");
561
+ return;
562
+ }
563
+ var conf = s_readDirParams[res.callbackId];
564
+ if (conf == null)
565
+ {
566
+ Debug.LogWarning($"HandleReadDirCallback - no callback for callbackId: {res.callbackId}");
567
+ return;
568
+ }
569
+ s_readDirParams.Remove(res.callbackId);
570
+ if (res.errCode == 0)
571
+ {
572
+ if (res.files == null)
573
+ {
574
+ res.files = Array.Empty<string>();
575
+ }
576
+ conf.success?.Invoke(res);
577
+ }
578
+ else
579
+ {
580
+ res.files = Array.Empty<string>();
581
+ conf.fail?.Invoke(res);
582
+ }
583
+ });
584
+
585
+ #region 流式读写
586
+ public void HandleFOpenCallback(string msg) => SafeHandle(nameof(HandleFOpenCallback), msg, m =>
587
+ {
588
+ var res = JsonUtility.FromJson<TTOpenResponse>(m);
589
+ var conf = s_openParams[res.callbackId];
590
+ if (conf == null)
591
+ {
592
+ Debug.LogWarning($"HandleFOpenCallback - no callback for callbackId: {res.callbackId}");
593
+ return;
594
+ }
595
+
596
+ s_openParams.Remove(res.callbackId);
597
+
598
+ if (res.errCode == 0)
599
+ {
600
+ conf.success?.Invoke(res);
601
+ }
602
+ else
603
+ {
604
+ conf.fail?.Invoke(res);
605
+ }
606
+ });
607
+
608
+ public void HandleFWriteCallback(string msg) => SafeHandle(nameof(HandleFWriteCallback), msg, m =>
609
+ {
610
+ var res = JsonUtility.FromJson<TTWriteResponse>(m);
611
+ var conf = s_writeParams[res.callbackId];
612
+ if (conf == null)
613
+ {
614
+ Debug.LogWarning($"HandleFWriteCallback - no callback for callbackId: {res.callbackId}");
615
+ return;
616
+ }
617
+
618
+ s_writeParams.Remove(res.callbackId);
619
+
620
+ if (res.errCode == 0)
621
+ {
622
+ conf.success?.Invoke(res);
623
+ }
624
+ else
625
+ {
626
+ conf.fail?.Invoke(res);
627
+ }
628
+ });
629
+
630
+ public void HandleFReadCallback(string msg) => SafeHandle(nameof(HandleFReadCallback), msg, m =>
631
+ {
632
+ var res = JsonUtility.FromJson<TTReadResponse>(m);
633
+ var conf = s_readParams[res.callbackId];
634
+ if (conf == null)
635
+ {
636
+ Debug.LogWarning($"HandleFReadCallback - no callback for callbackId: {res.callbackId}");
637
+ return;
638
+ }
639
+
640
+ s_readParams.Remove(res.callbackId);
641
+ res.bytesRead = Math.Max(res.bytesRead, 0);
642
+
643
+ if (res.errCode == 0)
644
+ {
645
+ res.arrayBuffer = conf.arrayBuffer;
646
+ StarkShareFileBuffer(res.arrayBuffer, res.callbackId);
647
+ conf.success?.Invoke(res);
648
+ }
649
+ else
650
+ {
651
+ conf.fail?.Invoke(res);
652
+ }
653
+ });
654
+
655
+ public void HandleFReadCompressedFileCallback(string msg) => SafeHandle(nameof(HandleFReadCompressedFileCallback), msg, m =>
656
+ {
657
+ var res = JsonUtility.FromJson<TTReadCompressedCallback>(m);
658
+ var conf = s_readCompressedFileParams[res.callbackId];
659
+ if (conf == null)
660
+ {
661
+ Debug.LogWarning($"HandleFReadCompressedFileCallback - no callback for callbackId: {res.callbackId}");
662
+ return;
663
+ }
664
+
665
+ s_readCompressedFileParams.Remove(res.callbackId);
666
+
667
+ if (res.errCode == 0)
668
+ {
669
+ var sharedBuffer = new byte[res.byteLength];
670
+ StarkShareFileBuffer(sharedBuffer, res.callbackId);
671
+ var obj = new TTReadCompressedFileResponse()
672
+ {
673
+ arrayBuffer = sharedBuffer,
674
+ };
675
+ conf.success?.Invoke(obj);
676
+ }
677
+ else
678
+ {
679
+ var obj = new TTReadCompressedFileResponse()
680
+ {
681
+ errCode = res.errCode,
682
+ errMsg = res.errMsg
683
+ };
684
+ conf.fail?.Invoke(obj);
685
+ }
686
+ });
687
+
688
+ public void HandleFStatCallback(string msg) => SafeHandle(nameof(HandleFStatCallback), msg, m =>
689
+ {
690
+ var res = JsonMapper.ToObject<FstatResponse>(m);
691
+ var conf = s_fstatParams[res.callbackId];
692
+ if (conf == null)
693
+ {
694
+ Debug.LogWarning($"HandleFStatCallback - no callback for callbackId: {res.callbackId}");
695
+ return;
696
+ }
697
+
698
+ s_fstatParams.Remove(res.callbackId);
699
+
700
+ if (res.errCode == 0)
701
+ {
702
+ conf.success?.Invoke(res);
703
+ }
704
+ else
705
+ {
706
+ conf.fail?.Invoke(res);
707
+ }
708
+ });
709
+ #endregion
710
+
711
+ }
712
+
713
+ /// <summary>
714
+ /// 将字符串写入文件(同步)
715
+ /// </summary>
716
+ /// <param name="filePath">要写入的文件路径</param>
717
+ /// <param name="data">要写入的文本</param>
718
+ /// <param name="encoding">指定写入文件的字符编码</param>
719
+ /// <returns>成功返回空字符串,失败返回错误信息</returns>
720
+ public override string WriteFileSync(string filePath, string data, string encoding = "utf8")
721
+ {
722
+ if (string.IsNullOrEmpty(encoding))
723
+ {
724
+ encoding = "utf8";
725
+ }
726
+
727
+ return StarkWriteStringFileSync(FixFilePath(filePath), data, encoding);
728
+ }
729
+
730
+ /// <summary>
731
+ /// 将二进制写入文件(同步)
732
+ /// </summary>
733
+ /// <param name="filePath">要写入的文件路径</param>
734
+ /// <param name="data">要写入的二进制数据</param>
735
+ /// <returns>成功返回空字符串,失败返回错误信息</returns>
736
+ public override string WriteFileSync(string filePath, byte[] data)
737
+ {
738
+ return StarkWriteBinFileSync(FixFilePath(filePath), data, data.Length);
739
+ }
740
+
741
+ /// <summary>
742
+ /// 将二进制写入文件(异步)
743
+ /// </summary>
744
+ /// <param name="param"></param>
745
+ public override void WriteFile(WriteFileParam param)
746
+ {
747
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
748
+ StarkWriteBinFile(
749
+ FixFilePath(param.filePath),
750
+ param.data,
751
+ param.data.Length,
752
+ pair.success,
753
+ pair.fail
754
+ );
755
+ }
756
+
757
+ /// <summary>
758
+ /// 将字符串写入文件(异步)
759
+ /// </summary>
760
+ /// <param name="param"></param>
761
+ public override void WriteFile(WriteFileStringParam param)
762
+ {
763
+ if (string.IsNullOrEmpty(param.encoding))
764
+ {
765
+ param.encoding = "utf8";
766
+ }
767
+
768
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
769
+ StarkWriteStringFile(
770
+ FixFilePath(param.filePath),
771
+ param.data,
772
+ param.encoding,
773
+ pair.success,
774
+ pair.fail
775
+ );
776
+ }
777
+
778
+ /// <summary>
779
+ /// 读取本地文件内容(异步)
780
+ /// </summary>
781
+ /// <param name="param"></param>
782
+ public override void ReadFile(ReadFileParam param)
783
+ {
784
+ var key = TTCallbackHandler.MakeKey();
785
+ s_readFileParams.Add(key, param);
786
+ StarkReadFile(FixFilePath(param.filePath), param.encoding, key);
787
+ }
788
+
789
+ /// <summary>
790
+ /// 从本地文件读取二进制数据数据(同步)
791
+ /// </summary>
792
+ /// <param name="filePath"></param>
793
+ /// <returns>字节数据,读取失败返回null</returns>
794
+ public override byte[] ReadFileSync(string filePath)
795
+ {
796
+ if (Application.platform == RuntimePlatform.WebGLPlayer ||
797
+ (Application.unityVersion.Contains("t") && (int)Application.platform == 0x00000032))
798
+ {
799
+ filePath = FixFilePath(filePath);
800
+ var length = StarkReadBinFileSync(filePath);
801
+ if (length == 0)
802
+ {
803
+ return null;
804
+ }
805
+
806
+ var sharedBuffer = new byte[length];
807
+ StarkShareFileBuffer(sharedBuffer, filePath);
808
+ return sharedBuffer;
809
+ }
810
+ else
811
+ {
812
+ return System.IO.File.ReadAllBytes(filePath);
813
+ }
814
+ }
815
+
816
+ /// <summary>
817
+ /// 从本地文件读取字符串数据(同步)
818
+ /// </summary>
819
+ /// <param name="filePath">要读取的文件的路径</param>
820
+ /// <param name="encoding">指定读取文件的字符编码, 不能为空</param>
821
+ /// <returns>字符串数据,读取失败返回null</returns>
822
+ public override string ReadFileSync(string filePath, string encoding)
823
+ {
824
+ return StarkReadStringFileSync(FixFilePath(filePath), encoding);
825
+ }
826
+
827
+ /// <summary>
828
+ /// 判断文件/目录是否存在(同步)
829
+ /// </summary>
830
+ /// <param name="path">要判断是否存在的文件/目录路径</param>
831
+ /// <returns>成功返回 true, 失败返回 false</returns>
832
+ public override bool AccessSync(string path)
833
+ {
834
+ return StarkAccessFileSync(FixFilePath(path));
835
+ }
836
+
837
+ /// <summary>
838
+ /// 判断文件/目录是否存在(异步)
839
+ /// </summary>
840
+ /// <param name="param"></param>
841
+ public override void Access(AccessParam param)
842
+ {
843
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
844
+ StarkAccessFile(FixFilePath(param.path), pair.success, pair.fail);
845
+ }
846
+
847
+ /// <summary>
848
+ /// 复制文件(同步)
849
+ /// </summary>
850
+ /// <param name="srcPath">源文件路径</param>
851
+ /// <param name="destPath">目标文件路径</param>
852
+ /// <returns>成功返回空字符串,失败返回错误信息</returns>
853
+ public override string CopyFileSync(string srcPath, string destPath)
854
+ {
855
+ return StarkCopyFileSync(FixFilePath(srcPath), FixFilePath(destPath));
856
+ }
857
+
858
+ /// <summary>
859
+ /// 复制文件(异步)
860
+ /// </summary>
861
+ /// <param name="param"></param>
862
+ public override void CopyFile(CopyFileParam param)
863
+ {
864
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
865
+ StarkCopyFile(FixFilePath(param.srcPath), FixFilePath(param.destPath), pair.success, pair.fail);
866
+ }
867
+
868
+ /// <summary>
869
+ /// 重命名文件(异步)
870
+ /// </summary>
871
+ /// <param name="param"></param>
872
+ public override void RenameFile(RenameFileParam param)
873
+ {
874
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
875
+ StarkRenameFile(FixFilePath(param.srcPath), FixFilePath(param.destPath), pair.success, pair.fail);
876
+ }
877
+
878
+ /// <summary>
879
+ /// 重命名文件(同步)
880
+ /// </summary>
881
+ /// <param name="srcPath">源文件路径</param>
882
+ /// <param name="destPath">目标文件路径</param>
883
+ /// <returns>成功返回空字符串,失败返回错误信息</returns>
884
+ public override string RenameFileSync(string srcPath, string destPath)
885
+ {
886
+ return StarkRenameFileSync(FixFilePath(srcPath), FixFilePath(destPath));
887
+ }
888
+
889
+ /// <summary>
890
+ /// 删除文件(同步)
891
+ /// </summary>
892
+ /// <param name="filePath">源文件路径,支持本地路径</param>
893
+ /// <returns>成功返回空字符串,失败返回错误信息</returns>
894
+ public override string UnlinkSync(string filePath)
895
+ {
896
+ return StarkUnlinkSync(FixFilePath(filePath));
897
+ }
898
+
899
+ /// <summary>
900
+ /// 删除文件(异步)
901
+ /// </summary>
902
+ /// <param name="param"></param>
903
+ public override void Unlink(UnlinkParam param)
904
+ {
905
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
906
+ StarkUnlink(FixFilePath(param.filePath), pair.success, pair.fail);
907
+ }
908
+
909
+ /// <summary>
910
+ /// 创建目录(异步)
911
+ /// </summary>
912
+ /// <param name="param"></param>
913
+ public override void Mkdir(MkdirParam param)
914
+ {
915
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
916
+ StarkMkdir(FixFilePath(param.dirPath), param.recursive, pair.success, pair.fail);
917
+ }
918
+
919
+ /// <summary>
920
+ /// 创建目录(同步)
921
+ /// </summary>
922
+ /// <param name="dirPath">创建的目录路径</param>
923
+ /// <param name="recursive">是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。</param>
924
+ /// <returns>成功返回空字符串,失败返回错误信息</returns>
925
+ public override string MkdirSync(string dirPath, bool recursive = false)
926
+ {
927
+ return StarkMkdirSync(FixFilePath(dirPath), recursive);
928
+ }
929
+
930
+ /// <summary>
931
+ /// 删除目录(异步)
932
+ /// </summary>
933
+ /// <param name="param"></param>
934
+ public override void Rmdir(RmdirParam param)
935
+ {
936
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
937
+ StarkRmdir(FixFilePath(param.dirPath), param.recursive, pair.success, pair.fail);
938
+ }
939
+
940
+ /// <summary>
941
+ /// 删除目录(同步)
942
+ /// </summary>
943
+ /// <param name="dirPath">创建的目录路径</param>
944
+ /// <param name="recursive">是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件 。</param>
945
+ /// <returns>成功返回空字符串,失败返回错误信息</returns>
946
+ public override string RmdirSync(string dirPath, bool recursive = false)
947
+ {
948
+ return StarkRmdirSync(FixFilePath(dirPath), recursive);
949
+ }
950
+
951
+ /// <summary>
952
+ /// 读取文件描述信息(同步)
953
+ /// </summary>
954
+ /// <param name="path">文件/目录路径</param>
955
+ /// <param name="recursive">是否递归获取目录下的每个文件的 Stat 信息 </param>
956
+ /// <param name="throwException">是否抛出错误信息,如果抛出错误信息,当文件不存在时则会抛出异常,错误信息从异常中获取。</param>
957
+ /// <returns>返回文件信息,如果访问失败则返回null</returns>
958
+ public override TTStatInfo StatSync(string path, bool throwException = false)
959
+ {
960
+ var info = StarkStatSync(FixFilePath(path));
961
+ try
962
+ {
963
+ return JsonUtility.FromJson<TTStatInfo>(info);
964
+ }
965
+ catch (Exception exception)
966
+ {
967
+ if (throwException)
968
+ {
969
+ if (string.IsNullOrEmpty(info))
970
+ {
971
+ info = "stat failed";
972
+ }
973
+
974
+ throw new Exception(info);
975
+ }
976
+
977
+ return null;
978
+ }
979
+ }
980
+
981
+ /// <summary>
982
+ /// 读取文件描述信息(异步)
983
+ /// </summary>
984
+ /// <param name="param"></param>
985
+ public override void Stat(StatParam param)
986
+ {
987
+ var key = TTCallbackHandler.MakeKey();
988
+ s_statParams.Add(key, param);
989
+ StarkStat(FixFilePath(param.path), key);
990
+ }
991
+
992
+ /// <summary>
993
+ /// 获取保存的用户目录文件列表
994
+ /// </summary>
995
+ /// <param name="param"></param>
996
+ public override void GetSavedFileList(GetSavedFileListParam param)
997
+ {
998
+ var key = TTCallbackHandler.MakeKey();
999
+ s_getSavedFileListParams.Add(key, param);
1000
+ StarkGetSavedFileList(key);
1001
+ }
1002
+
1003
+ /// <summary>
1004
+ /// 根据url链接获取本地缓存文件路径
1005
+ /// </summary>
1006
+ /// <param name="url">输入文件下载链接url</param>
1007
+ /// <returns>返回本地缓存文件路径,以scfile://user开头的路径,可以直接用这个路径访问该文件</returns>
1008
+ public override string GetLocalCachedPathForUrl(string url)
1009
+ {
1010
+ return StarkGetCachedPathForUrl(url);
1011
+ }
1012
+
1013
+ /// <summary>
1014
+ /// 判断该url是否有本地缓存文件
1015
+ /// </summary>
1016
+ /// <param name="url">输入文件下载链接url</param>
1017
+ /// <returns>如果存在缓存文件则返回true,不存在缓存文件则返回false</returns>
1018
+ public override bool IsUrlCached(string url)
1019
+ {
1020
+ var path = GetLocalCachedPathForUrl(url);
1021
+ return !string.IsNullOrEmpty(path) && AccessSync(path);
1022
+ }
1023
+
1024
+ public override string AppendFileSync(string filePath, string data, string encoding = "utf8")
1025
+ {
1026
+ Debug.Log($"AppendFileSync filePath:{filePath},data:{data},encoding:{encoding}");
1027
+ if (string.IsNullOrEmpty(encoding))
1028
+ {
1029
+ encoding = "utf8";
1030
+ }
1031
+
1032
+ return StarkAppendStringFileSync(FixFilePath(filePath), data, encoding);
1033
+ }
1034
+
1035
+ public override string AppendFileSync(string filePath, byte[] data)
1036
+ {
1037
+ Debug.Log($"AppendFileSync filePath:{filePath},data:{data}");
1038
+ return StarkAppendBinFileSync(FixFilePath(filePath), data, data.Length);
1039
+ }
1040
+
1041
+ public override void AppendFile(AppendFileStringParam param)
1042
+ {
1043
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
1044
+ StarkAppendStringFile(
1045
+ FixFilePath(param.FilePath),
1046
+ param.Data,
1047
+ param.Encoding,
1048
+ pair.success,
1049
+ pair.fail);
1050
+ }
1051
+
1052
+ public override void AppendFile(AppendFileParam param)
1053
+ {
1054
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
1055
+ StarkAppendBinFile(
1056
+ FixFilePath(param.FilePath),
1057
+ param.Data,
1058
+ param.Data.Length,
1059
+ pair.success,
1060
+ pair.fail);
1061
+ }
1062
+
1063
+
1064
+ public override void ReadDir(ReadDirParam param)
1065
+ {
1066
+ var key = TTCallbackHandler.MakeKey();
1067
+ s_readDirParams.Add(key, param);
1068
+ StarkReadDir(
1069
+ FixFilePath(param.DirPath),
1070
+ key);
1071
+ }
1072
+
1073
+ public override string[] ReadDirSync(string dirPath)
1074
+ {
1075
+ var msg = StarkReadDirSync(FixFilePath(dirPath));
1076
+ Debug.Log($"ReadDirSync callback -->{msg}");
1077
+ string[] res;
1078
+ try
1079
+ {
1080
+ res = JsonMapper.ToObject<string[]>(msg);
1081
+ }
1082
+ catch (Exception e)
1083
+ {
1084
+ Debug.LogError($"ReadDirSync ->{msg},{e}");
1085
+ res = Array.Empty<string>();
1086
+ }
1087
+ return res;
1088
+ }
1089
+
1090
+ public override void Truncate(TruncateParam param)
1091
+ {
1092
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
1093
+ StarkTruncate(
1094
+ FixFilePath(param.FilePath),
1095
+ param.Length,
1096
+ pair.success,
1097
+ pair.fail);
1098
+ }
1099
+
1100
+ public override string TruncateSync(string filePath, int length)
1101
+ {
1102
+ return StarkTruncateSync(
1103
+ FixFilePath(filePath),
1104
+ length);
1105
+ }
1106
+
1107
+ #region 流式读写
1108
+ public override void Open(OpenParam param)
1109
+ {
1110
+ var key = TTCallbackHandler.MakeKey();
1111
+ s_openParams.Add(key, param);
1112
+ TT_FOpenFile(FixFilePath(param.filePath), param.flag, key);
1113
+ }
1114
+
1115
+ public override string OpenSync(OpenSyncParam param)
1116
+ {
1117
+ var fd = TT_FOpenFileSync(FixFilePath(param.filePath), param.flag);
1118
+ if (fd.Contains("ERROR"))
1119
+ {
1120
+ throw new Exception(fd);
1121
+ }
1122
+ return fd;
1123
+ }
1124
+
1125
+ public override void Close(CloseParam param)
1126
+ {
1127
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
1128
+ TT_FCloseFile(param.fd, pair.success, pair.fail);
1129
+ }
1130
+
1131
+ public override void CloseSync(CloseSyncParam param)
1132
+ {
1133
+ string info = TT_FCloseFileSync(param.fd);
1134
+ if (!string.IsNullOrEmpty(info))
1135
+ {
1136
+ throw new Exception(info);
1137
+ }
1138
+ }
1139
+
1140
+ public override void Write(WriteBinParam param)
1141
+ {
1142
+ var key = TTCallbackHandler.MakeKey();
1143
+ s_writeParams.Add(key, param);
1144
+ int length = param.length == null ? -1 : Math.Max(param.length.Value, 0);
1145
+ int position = param.position == null ? -1 : Math.Max(param.position.Value, -1);
1146
+ TT_FWriteBinFile(param.fd, param.data, param.data.Length, param.offset, length, key, position);
1147
+ }
1148
+
1149
+ public override void Write(WriteStringParam param)
1150
+ {
1151
+ var key = TTCallbackHandler.MakeKey();
1152
+ s_writeParams.Add(key, param);
1153
+ TT_FWriteStringFile(param.fd, param.data, param.encoding, key);
1154
+ }
1155
+
1156
+ public override WriteResult WriteSync(WriteBinSyncParam param)
1157
+ {
1158
+ int length = param.length == null ? -1 : Math.Max(param.length.Value, 0);
1159
+ int position = param.position == null ? -1 : Math.Max(param.position.Value, -1);
1160
+ var result = TT_FWriteBinFileSync(param.fd, param.data, param.data.Length, param.offset, length, position);
1161
+ if (int.TryParse(result, out int bytesWritten))
1162
+ {
1163
+ return new()
1164
+ {
1165
+ bytesWritten = bytesWritten
1166
+ };
1167
+ }
1168
+ else
1169
+ {
1170
+ throw new Exception(result);
1171
+ }
1172
+ }
1173
+
1174
+ public override WriteResult WriteSync(WriteStringSyncParam param)
1175
+ {
1176
+ var result = TT_FWriteStringFileSync(param.fd, param.data, param.encoding);
1177
+ if (int.TryParse(result, out int bytesWritten))
1178
+ {
1179
+ return new()
1180
+ {
1181
+ bytesWritten = bytesWritten
1182
+ };
1183
+ }
1184
+ else
1185
+ {
1186
+ throw new Exception(result);
1187
+ }
1188
+ }
1189
+
1190
+ public override void Read(ReadParam param)
1191
+ {
1192
+ var key = TTCallbackHandler.MakeKey();
1193
+ s_readParams.Add(key, param);
1194
+ int position = param.position == null ? -1 : Math.Max(param.position.Value, -1);
1195
+ TT_FReadFile(param.fd, param.arrayBuffer.Length, param.offset, param.length, position, key);
1196
+ }
1197
+
1198
+ public override ReadResult ReadSync(ReadSyncParam param)
1199
+ {
1200
+ int position = param.position == null ? -1 : Math.Max(param.position.Value, -1);
1201
+ var result = TT_FReadFileSync(param.fd, param.arrayBuffer.Length, param.offset, param.length, position);
1202
+ if (int.TryParse(result, out int bytesRead))
1203
+ {
1204
+ bytesRead = Math.Max(bytesRead, 0);//根据平台不同,有的会返回-1有的是0
1205
+ var sharedBuffer = param.arrayBuffer;
1206
+ StarkShareFileBuffer(sharedBuffer, param.fd);
1207
+ return new()
1208
+ {
1209
+ bytesRead = bytesRead,
1210
+ arrayBuffer = sharedBuffer
1211
+ };
1212
+ }
1213
+ else
1214
+ {
1215
+ throw new Exception(result);
1216
+ }
1217
+ }
1218
+
1219
+ public override void ReadCompressedFile(ReadCompressedFileParam param)
1220
+ {
1221
+ var key = TTCallbackHandler.MakeKey();
1222
+ s_readCompressedFileParams.Add(key, param);
1223
+ TT_FReadCompressedFile(FixFilePath(param.filePath), param.compressionAlgorithm, key);
1224
+ }
1225
+
1226
+ public override byte[] ReadCompressedFileSync(ReadCompressedFileSyncParam param)
1227
+ {
1228
+ var result = TT_FReadCompressedFileSync(FixFilePath(param.filePath), param.compressionAlgorithm);
1229
+ if (int.TryParse(result, out int length))
1230
+ {
1231
+ var sharedBuffer = new byte[length];
1232
+ StarkShareFileBuffer(sharedBuffer, FixFilePath(param.filePath));
1233
+ return sharedBuffer;
1234
+ }
1235
+ else
1236
+ {
1237
+ throw new Exception(result);
1238
+ }
1239
+ }
1240
+
1241
+ public override void Fstat(FstatParam param)
1242
+ {
1243
+ var key = TTCallbackHandler.MakeKey();
1244
+ s_fstatParams.Add(key, param);
1245
+ TT_Fstat(param.fd, key);
1246
+ }
1247
+
1248
+ public override TTStatInfo FstatSync(FstatSyncParam param)
1249
+ {
1250
+ var info = TT_FstatSync(param.fd);
1251
+ try
1252
+ {
1253
+ return JsonUtility.FromJson<TTStatInfo>(info);
1254
+ }
1255
+ catch (Exception exception)
1256
+ {
1257
+ throw new Exception(info);
1258
+ }
1259
+ }
1260
+
1261
+ public override void Ftruncate(FtruncateParam param)
1262
+ {
1263
+ var pair = TTCallbackHandler.AddPair(param.success, param.fail);
1264
+ TT_Ftruncate(param.fd, param.length, pair.success, pair.fail);
1265
+ }
1266
+
1267
+ public override void FtruncateSync(FtruncateSyncParam param)
1268
+ {
1269
+ string info = TT_FtruncateSync(param.fd, param.length);
1270
+ if (!string.IsNullOrEmpty(info))
1271
+ {
1272
+ throw new Exception(info);
1273
+ }
1274
+ }
1275
+ #endregion
1276
+
1277
+ }
1278
+ }