-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnityWebToolUtils.cs
81 lines (71 loc) · 2.63 KB
/
UnityWebToolUtils.cs
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
using System.Text;
namespace Kiraio.UnityWebTools
{
/// <summary>
/// Utility class for `UnityWebTools`.
/// </summary>
public static class UnityWebToolUtils
{
/// <summary>
/// Check if a file is a valid `UnityWebData` file.
/// </summary>
/// <param name="filePath"></param>
/// <returns>Validity of the file.</returns>
public static bool IsUnityWebData(string filePath)
{
return IsFile(filePath, Encoding.UTF8.GetBytes(UnityWebTool.MAGIC_HEADER));
}
/// <summary>
/// Check the file signature if it's a valid file.
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileSignature"></param>
/// <returns>The file are valid or not.</returns>
internal static bool IsFile(string filePath, byte[] fileSignature)
{
try
{
byte[] sourceFileSignature = new byte[fileSignature.Length];
using FileStream file = File.OpenRead(filePath);
file.Read(sourceFileSignature, 0, fileSignature.Length);
for (int i = 0; i < fileSignature.Length; i++)
{
if (sourceFileSignature[i] != fileSignature[i])
return false;
}
return true;
}
catch (Exception ex)
{
Console.WriteLine(
$"Error when reading file signature on {Path.GetFileName(filePath)}. {ex.Message}"
);
return false;
}
}
/// <summary>
/// Get files path recursively.
/// </summary>
/// <param name="sourceFolder"></param>
/// <returns></returns>
internal static string[] GetFilesRecursive(string sourceFolder)
{
return Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories);
}
/// <summary>
/// Add null terminator at the end of bytes.
/// </summary>
/// <param name="originalArray"></param>
/// <returns>Modified array of bytes.</returns>
internal static byte[] AddNullTerminate(byte[] originalArray)
{
// Create a new array with one extra element to accommodate the null byte
byte[] newArray = new byte[originalArray.Length + 1];
// Copy the original array to the new array
Array.Copy(originalArray, newArray, originalArray.Length);
// Set the last element to be the null byte
newArray[^1] = 0;
return newArray;
}
}
}