-
Notifications
You must be signed in to change notification settings - Fork 1
/
BuildScriptEncryptMode.cs
213 lines (185 loc) · 7.99 KB
/
BuildScriptEncryptMode.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Extreal.Integration.AssetWorkflow.Addressables.Custom.ResourceProviders;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Build.DataBuilders;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
using UnityEngine;
using UnityEngine.Build.Pipeline;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.ResourceManagement.Util;
namespace Extreal.Integration.AssetWorkflow.Addressables.Editor.Custom
{
/// <summary>
/// Build scripts used for player builds and running with bundles in the editor.
/// </summary>
[CreateAssetMenu(fileName = "BuildScriptEncryptMode.asset", menuName = "Extreal/Integration.AssetWorkflow.Addressables.Editor/Encrypt Build Script")]
public class BuildScriptEncryptMode : BuildScriptPackedMode
{
private AddressableAssetSettings addressableSettings;
private readonly List<BundleResult> bundleResults = new List<BundleResult>();
private readonly HashSet<string> doneAssetGroups = new HashSet<string>();
/// <inheritdoc />
public override string Name => "Encrypt Build Script";
/// <inheritdoc />
protected override TResult BuildDataImplementation<TResult>(AddressablesDataBuilderInput builderInput)
{
addressableSettings = builderInput.AddressableSettings;
bundleResults.Clear();
doneAssetGroups.Clear();
return base.BuildDataImplementation<TResult>(builderInput);
}
/// <inheritdoc />
protected override TResult DoBuild<TResult>(
AddressablesDataBuilderInput builderInput, AddressableAssetsBuildContext aaContext)
{
var result = base.DoBuild<TResult>(builderInput, aaContext);
try
{
Encrypt();
}
catch (Exception e)
{
Debug.LogException(e);
throw;
}
return result;
}
/// <inheritdoc />
protected override string ConstructAssetBundleName(
AddressableAssetGroup assetGroup, BundledAssetGroupSchema schema,
BundleDetails info, string assetBundleName)
{
var outputBundleName = base.ConstructAssetBundleName(assetGroup, schema, info, assetBundleName);
assetGroup ??= addressableSettings.DefaultGroup;
bundleResults.Add(new BundleResult(assetGroup, schema, info, outputBundleName));
return outputBundleName;
}
private void Encrypt()
{
foreach (var bundleResult in bundleResults)
{
var isEncrypt = IsEncrypt(bundleResult) && IsUseWebRequest(bundleResult);
LogEncrypt(bundleResult, isEncrypt);
if (isEncrypt)
{
Encrypt(bundleResult);
}
}
}
private void LogEncrypt(BundleResult bundleResult, bool encrypted)
{
var assetGroupName = bundleResult.AssetGroup.Name;
if (doneAssetGroups.Contains(assetGroupName))
{
return;
}
doneAssetGroups.Add(assetGroupName);
var message = encrypted ? "ENCRYPTED" : "NOT ENCRYPTED";
Debug.Log($"<color=cyan>{message} '{assetGroupName}' {bundleResult.OutputBundleName}</color>");
}
private bool IsEncrypt(BundleResult bundleResult)
=> typeof(CryptoAssetBundleProviderBase)
.IsAssignableFrom(bundleResult.Schema.AssetBundleProviderType.Value);
private bool IsUseWebRequest(BundleResult bundleResult)
{
var loadPath = bundleResult.Schema.LoadPath.GetValue(addressableSettings);
return ResourceManagerConfig.ShouldPathUseWebRequest(loadPath)
|| bundleResult.Schema.UseUnityWebRequestForLocalBundles;
}
[SuppressMessage("CodeCracker", "CC0022")]
private void Encrypt(BundleResult bundleResult)
{
var builtBundlePath = ToBuiltBundlePath(bundleResult);
var srcPath = builtBundlePath + ".src";
CreateSrcFile(builtBundlePath, srcPath);
var destPath = builtBundlePath;
var dirName = Path.GetDirectoryName(destPath);
if (!Directory.Exists(dirName))
{
_ = Directory.CreateDirectory(dirName);
}
if (File.Exists(destPath))
{
File.Delete(destPath);
}
{
using var srcStream = new FileStream(srcPath, FileMode.Open, FileAccess.Read);
using var destStream = new FileStream(destPath, FileMode.OpenOrCreate, FileAccess.Write);
using var encryptor = ToFactory(bundleResult).CreateEncryptStream(destStream, ToOptions(bundleResult));
srcStream.CopyTo(encryptor);
}
File.Delete(srcPath);
}
private static void CreateSrcFile(string builtBundlePath, string srcPath)
{
if (File.Exists(srcPath))
{
File.Delete(srcPath);
}
File.Move(builtBundlePath, srcPath);
}
private string ToBuiltBundlePath(BundleResult bundleResult)
{
var buildPath = bundleResult.Schema.BuildPath.GetValue(addressableSettings);
return Path.Combine(buildPath, bundleResult.OutputBundleName);
}
private static ICryptoStreamFactory ToFactory(BundleResult bundleResult) =>
(bundleResult.Schema.AssetBundleProviderType.Value
.GetConstructor(Array.Empty<Type>())
.Invoke(default) as CryptoAssetBundleProviderBase).CryptoStreamFactory;
private class BundleResult
{
public AddressableAssetGroup AssetGroup { get; }
public BundledAssetGroupSchema Schema { get; }
public BundleDetails Info { get; }
public string OutputBundleName { get; }
public BundleResult(
AddressableAssetGroup assetGroup, BundledAssetGroupSchema schema,
BundleDetails info, string outputBundleName)
{
AssetGroup = assetGroup;
Schema = schema;
Info = info;
OutputBundleName = outputBundleName;
}
}
private AssetBundleRequestOptions ToOptions(BundleResult bundleResult)
{
var schema = bundleResult.Schema;
var info = bundleResult.Info;
return new AssetBundleRequestOptions
{
Crc = schema.UseAssetBundleCrc ? info.Crc : 0,
UseCrcForCachedBundle = schema.UseAssetBundleCrcForCachedBundles,
UseUnityWebRequestForLocalBundles = schema.UseUnityWebRequestForLocalBundles,
Hash = schema.UseAssetBundleCache ? info.Hash.ToString() : "",
ChunkedTransfer = schema.ChunkedTransfer,
RedirectLimit = schema.RedirectLimit,
RetryCount = schema.RetryCount,
Timeout = schema.Timeout,
BundleName = Path.GetFileNameWithoutExtension(info.FileName),
AssetLoadMode = schema.AssetLoadMode,
BundleSize = GetFileSize(ToBuiltBundlePath(bundleResult)),
ClearOtherCachedVersionsWhenLoaded = schema.AssetBundledCacheClearBehavior ==
BundledAssetGroupSchema.CacheClearBehavior
.ClearWhenWhenNewVersionLoaded
};
}
private static long GetFileSize(string fileName)
{
try
{
return new FileInfo(fileName).Length;
}
catch (Exception e)
{
Debug.LogException(e);
return 0;
}
}
}
}