-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptRes.cs
More file actions
119 lines (101 loc) · 3.41 KB
/
Copy pathEncryptRes.cs
File metadata and controls
119 lines (101 loc) · 3.41 KB
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
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class EncryptRes
{
static string resDir = string.Empty;
static string targetDir = string.Empty;
static string identifier = string.Empty;
static List<string> paths = new List<string>();
static List<string> files = new List<string>();
[MenuItem("BuildAppEditeTools/EncryptRes", false, 710)]
static public void EncryptStreamRes()
{
resDir = Application.dataPath + "/StreamingAssets";
if (!Directory.Exists(resDir))
{
Debug.LogError("资源目录不存在---");
return;
}
identifier = PlayerSettings.bundleIdentifier;
if (string.IsNullOrEmpty(identifier))
{
identifier = "sen";
}
targetDir = Directory.GetCurrentDirectory();
targetDir = targetDir.Replace('\\', '/');
targetDir = targetDir + "/EncryptRes/" + identifier;
DirectoryInfo dirOutDir = new DirectoryInfo(targetDir);
if (dirOutDir.Exists)
{
Directory.Delete(targetDir, true);
}
Directory.CreateDirectory(targetDir);
paths.Clear();
files.Clear();
Recursive(resDir);
int n = 0;
foreach (string f in files)
{
EncryptFile(f);
UpdateProgress(n++, files.Count, f);
}
EditorUtility.ClearProgressBar();
Debug.Log("资源加密完成---");
}
private static void EncryptFile(string file)
{
if (!File.Exists(file))
{
Debug.LogError("加密文件不存在---");
return;
}
string subPath = file.Replace(resDir, string.Empty);
string targetFile = targetDir + file.Replace(resDir, string.Empty);
if (File.Exists(targetFile)) File.Delete(targetFile);
string dir = Path.GetDirectoryName(targetFile);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buff = new byte[fs.Length];
fs.Read(buff, 0, (int)fs.Length);
fs.Close();
byte[] encryptBuff = AES.AESEncrypt(buff, identifier);
FileStream newFile = new FileStream(targetFile, FileMode.Create);
newFile.Write(encryptBuff, 0, encryptBuff.Length);
newFile.Close();
buff = null;
encryptBuff = null;
}
static void UpdateProgress(int progress, int progressMax, string desc)
{
string title = "Processing...[" + progress + " - " + progressMax + "]";
float value = (float)progress / (float)progressMax;
EditorUtility.DisplayProgressBar(title, desc, value);
}
/// <summary>
/// 遍历目录及其子目录
/// </summary>
static void Recursive(string path)
{
string[] names = Directory.GetFiles(path);
string[] dirs = Directory.GetDirectories(path);
string ext = string.Empty;
foreach (string filename in names)
{
ext = Path.GetExtension(filename);
if (ext.Equals(AppConst.ExtName) || string.IsNullOrEmpty(ext))
{
files.Add(filename.Replace('\\', '/'));
}
}
foreach (string dir in dirs)
{
paths.Add(dir.Replace('\\', '/'));
Recursive(dir.Replace('\\', '/'));
}
}
}