少年修仙传客户端基础资源
hch
2024-05-11 cc9183fd3d8888d533197e6351e99a2cca6b1ada
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (c) 2018-2023 Beebyte Limited. All rights reserved.
 */
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
#if UNITY_2017_3_OR_NEWER
using UnityEditor.Compilation;
#endif
using UnityEngine;
 
namespace Beebyte.Obfuscator.Assembly
{
    public class AssemblySelector
    {
        private readonly HashSet<string> _compiledAssemblyPaths = new HashSet<string>();
        private readonly HashSet<string> _assemblyPaths = new HashSet<string>();
 
        public AssemblySelector(Options options)
        {
            if (options == null) throw new ArgumentException("options must not be null", "options");
            if (options.compiledAssemblies == null)
                throw new ArgumentException(
                    "options.compiledAssemblies must not be null", "options");
            if (options.assemblies == null)
                throw new ArgumentException(
                    "options.assemblies must not be null", "options");
            if (Application.dataPath == null) throw new ArgumentException("Application.dataPath must not be null");
 
            foreach (string assemblyName in options.compiledAssemblies)
            {
                string location = FindDllLocation(assemblyName);
                if (location != null)
                {
                    _compiledAssemblyPaths.Add(location);
                }
            }
 
            foreach (string assemblyName in options.assemblies)
            {
                string location = FindDllLocation(assemblyName);
                if (location != null)
                {
                    _assemblyPaths.Add(location);
                }
            }
 
#if UNITY_2017_3_OR_NEWER
            if (!options.includeCompilationPipelineAssemblies) return;
 
            string projectDir = Path.GetDirectoryName(Application.dataPath);
            
#if UNITY_2021_3_OR_NEWER
            string[] nativeCompiledFileNames = GetNativeCompiledFileNames();
            string[] packageDllFileNames = GetPackageDllFileNames();
            string[] additionalAssembliesToObfuscate = Directory.GetFiles(
#if UNITY_2022_1_OR_NEWER
                    projectDir + "/Library/Bee/PlayerScriptAssemblies/")
#else
                    projectDir + "/Library/PlayerScriptAssemblies/")
#endif
                .Where(path => path.EndsWith(".dll"))
                .Where(path => !Path.GetFileName(path).Contains("-firstpass"))
                .Where(path => !Path.GetFileName(path).StartsWith("Unity."))
                .Where(path => !Path.GetFileName(path).StartsWith("UnityEngine."))
                .Where(path => !nativeCompiledFileNames.Contains(Path.GetFileName(path)))
                .Where(path => !packageDllFileNames.Contains(Path.GetFileName(path)))
                .Select(Path.GetFullPath)
                .ToArray();
            _assemblyPaths = Enumerable.ToHashSet(_assemblyPaths.Select(Path.GetFullPath));
            _assemblyPaths.UnionWith(additionalAssembliesToObfuscate);
#else
#if UNITY_2018_1_OR_NEWER
#if UNITY_2019_3_OR_NEWER
            foreach (UnityEditor.Compilation.Assembly assembly in CompilationPipeline.GetAssemblies(AssembliesType
                .PlayerWithoutTestAssemblies))
#else
            foreach (UnityEditor.Compilation.Assembly assembly in CompilationPipeline.GetAssemblies(AssembliesType.Player))
#endif
            {
#else
            foreach (UnityEditor.Compilation.Assembly assembly in CompilationPipeline.GetAssemblies())
            {
                if ((assembly.flags & AssemblyFlags.EditorAssembly) != 0)
                {
                    continue;
                }
#endif
                if (assembly.name.Contains("-firstpass"))
                {
                    continue;
                }
 
                if (assembly.sourceFiles.Length == 0)
                {
                    continue;
                }
 
                if (assembly.sourceFiles[0].StartsWith("Packages"))
                {
                    continue;
                }
 
        string scriptDllLocation = Path.Combine(projectDir, assembly.outputPath).Replace('\\', '/');
#if !UNITY_2019_2_OR_NEWER || UNITY_2019_2_0 || UNITY_2019_2_1 || UNITY_2019_2_2 || UNITY_2019_2_3 || UNITY_2019_2_4 || UNITY_2019_2_5 || UNITY_2019_2_6 || UNITY_2019_2_7
                string dllLocation = scriptDllLocation;
#else
                string dllLocation = ConvertToPlayerAssemblyLocationIfPresentOrElseNull(scriptDllLocation) ?? scriptDllLocation;
#endif
 
                // If the assembly is for a different build target platform, oddly it will still be in the compilation
                // pipeline, however the file won't actually exist.
                if (dllLocation != null && File.Exists(dllLocation))
                {
                    _assemblyPaths.Add(dllLocation);
                }
            }
#endif
#endif
        }
 
        private string[] GetNativeCompiledFileNames()
        {
            return CompilationPipeline.GetAssemblies()
                .Where(assembly => (assembly.sourceFiles.Length == 0))
                .Select(assembly => assembly.outputPath)
                .Select(Path.GetFileName)
                .ToArray();
        }
 
        private string[] GetPackageDllFileNames()
        {
            return CompilationPipeline.GetAssemblies()
                .Where(assembly => assembly.sourceFiles.Length != 0)
                .Where(assembly => assembly.sourceFiles[0].StartsWith("Packages"))
                .Select(assembly => assembly.outputPath)
                .Select(Path.GetFileName)
                .ToArray();
        }
 
        public ICollection<string> GetCompiledAssemblyPaths()
        {
            return _compiledAssemblyPaths;
        }
 
        public ICollection<string> GetAssemblyPaths()
        {
            return _assemblyPaths;
        }
 
        private static string FindDllLocation(string suffix)
        {
            if (string.IsNullOrEmpty(suffix))
            {
                throw new ArgumentException(
                    "Empty or null DLL names are forbidden (check Obfuscator Options assemblies / compiled assemblies list)");
            }
 
            string compiledAssemblyLocation = GetCompiledAssemblyLocation(suffix);
 
#if !UNITY_2019_2_OR_NEWER || UNITY_2019_2_0 || UNITY_2019_2_1 || UNITY_2019_2_2 || UNITY_2019_2_3 || UNITY_2019_2_4 || UNITY_2019_2_5 || UNITY_2019_2_6 || UNITY_2019_2_7
            return compiledAssemblyLocation;
#else
            return ConvertToPlayerAssemblyLocationIfPresentOrElseNull(compiledAssemblyLocation) ?? compiledAssemblyLocation;
#endif
        }
 
        private static string GetCompiledAssemblyLocation(string suffix)
        {
            if (IsAPath(suffix))
            {
                var path = GetFileOnKnownPath(suffix);
                if (path != null)
                {
                    return path;
                }
            }
 
            foreach (System.Reflection.Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    if (assembly.Location.Equals(string.Empty))
                    {
                        DisplayFailedAssemblyParseWarning(assembly);
                    }
                    else if (assembly.Location.EndsWith(suffix))
                    {
                        return assembly.Location.Replace('\\', '/');
                    }
                }
                catch (NotSupportedException)
                {
                    DisplayFailedAssemblyParseWarning(assembly);
                }
            }
 
            Debug.LogWarning(
                suffix + " was not found (check Obfuscator Options assemblies / compiled assemblies list)");
            return null;
        }
 
        private static bool IsAPath(string s)
        {
            return s.Contains("/") || s.Contains("\\");
        }
 
        private static string GetFileOnKnownPath(string path)
        {
            var fromAssetFolder = Application.dataPath + Path.DirectorySeparatorChar.ToString() + path;
            if (File.Exists(fromAssetFolder))
            {
                return fromAssetFolder;
            }
 
            if (File.Exists(path))
            {
                return path;
            }
 
            return null;
        }
 
        private static string ConvertToPlayerAssemblyLocationIfPresentOrElseNull(string location)
        {
            if (location == null)
            {
                return null;
            }
 
            string beeScriptAssemblyLocation =
                Regex.Replace(location, "/ScriptAssemblies/", "/Bee/PlayerScriptAssemblies/");
            string playerScriptAssemblyLocation =
                Regex.Replace(location, "/ScriptAssemblies/", "/PlayerScriptAssemblies/");
            return File.Exists(beeScriptAssemblyLocation) ? beeScriptAssemblyLocation :
                File.Exists(playerScriptAssemblyLocation) ? playerScriptAssemblyLocation : null;
        }
 
        private static void DisplayFailedAssemblyParseWarning(System.Reflection.Assembly assembly)
        {
            Debug.LogWarning("Could not parse dynamically created assembly (string.Empty location) " +
                             assembly.FullName +
                             ". If you extend classes from within this assembly that in turn extend from " +
                             "MonoBehaviour you will need to manually annotate these classes with [Skip]");
        }
    }
}