少年修仙传客户端基础资源
dabaoji
2023-11-13 0a23e1b83f8fda8ab9f9e32fe71c26c431faf63a
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
/*
 * Copyright (c) 2015-2018 Beebyte Limited. All rights reserved.
 */
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections.Generic;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using Beebyte.Obfuscator.Assembly;
 
namespace Beebyte.Obfuscator
{
    public class Postbuild
    {
        private static Options _options;
        private static readonly IDictionary<string, string> DllsToRevert = new Dictionary<string, string>();
 
        private static bool _obfuscatedAfterScene;
        private static bool _noCSharpScripts;
        private static bool _monoBehaviourAssetsNeedReverting;
        private static bool _dllsNeedRestoring;
        private static bool _hasError;
 
        [InitializeOnLoad]
        public static class PostbuildStatic
        {
            /*
             * Often Unity's EditorApplication.update delegate is reset. Because it's so important to restore
             * renamed MonoBehaviour assets we assign here where it will be called after scripts are compiled.
             */ 
            static PostbuildStatic()
            {
                EditorApplication.update += RestoreAssets;
                EditorApplication.update += RestoreOriginalDlls;
                _hasError = false;
            }
        }
 
        [PostProcessBuild(1)]
        private static void PostBuildHook(BuildTarget buildTarget, string pathToBuildProject)
        {
            if (!_options || (_options.enabled && _obfuscatedAfterScene == false))
            {
                if (_noCSharpScripts) Debug.LogWarning("No obfuscation required because no C# scripts were found");
                else Debug.LogError("Failed to obfuscate");
            }
            else
            {
                if (_monoBehaviourAssetsNeedReverting) RestoreAssets();
                if (_dllsNeedRestoring) RestoreOriginalDlls();
            }
            Clear();
        }
 
        private static void Clear()
        {
            _obfuscatedAfterScene = false;
            _noCSharpScripts = false;
            _hasError = false;
 
            if (_options != null && _options.obfuscateMonoBehaviourClassNames == false) Obfuscator.Clear();
        }
 
        /**
         * When multiple DLLs are obfuscated, usually the extra DLLs need to be reverted back to their original state.
         * This method backs up the DLLs to be reverted after obfuscation is complete (or failed).
         */
        private static void BackupDlls(ICollection<string> locations)
        {
            if (locations.Count > 0)
            {
                EditorApplication.update += RestoreOriginalDlls;
                _dllsNeedRestoring = true;
            }
 
            foreach (string location in locations)
            {
                string backupLocation = location + ".pre";
 
                //This throws an exception if the backup already exists - we want this to happen
                File.Copy(location, backupLocation);
 
                DllsToRevert.Add(backupLocation, location);
            }
        }
 
        [PostProcessScene(1)]
        public static void Obfuscate()
        {
#if UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
            if (!EditorApplication.isPlayingOrWillChangePlaymode && !_obfuscatedAfterScene && _hasError == false)
#else
            if (!EditorApplication.isPlayingOrWillChangePlaymode && !_obfuscatedAfterScene && _hasError == false && BuildPipeline.isBuildingPlayer)
#endif
            {
                try
                {
                    EditorApplication.LockReloadAssemblies();
                    ObfuscateWhileLocked();
                }
                catch (Exception e)
                {
                    Debug.LogError("Obfuscation Failed: " + e);
                    _hasError = true;
                }
                finally
                {
                    EditorApplication.UnlockReloadAssemblies();
                }
            }
        }
 
        private static void ObfuscateWhileLocked()
        {
            if (_options == null) _options = OptionsManager.LoadOptions();
            Obfuscator.FixHexBug(_options);
 
            if (_options.enabled == false) return;
            
            AssemblySelector selector = new AssemblySelector(_options);
 
            ICollection<string> compiledDlls = selector.GetCompiledAssemblyPaths();
            BackupDlls(compiledDlls);
            ICollection<string> dlls = selector.GetAssemblyPaths();
 
            if (dlls.Count == 0 && compiledDlls.Count == 0) _noCSharpScripts = true;
            else
            {
                HashSet<string> extraAssemblyReferenceDirectories = new HashSet<string>(_options.extraAssemblyDirectories);
                
#if UNITY_2017_3_OR_NEWER
                extraAssemblyReferenceDirectories.UnionWith(AssemblyReferenceLocator.GetAssemblyReferenceDirectories());
#endif
                
                Obfuscator.SetExtraAssemblyDirectories(extraAssemblyReferenceDirectories.ToArray());
                Obfuscator.Obfuscate(dlls, compiledDlls, _options, EditorUserBuildSettings.activeBuildTarget);
 
                if (_options.obfuscateMonoBehaviourClassNames)
                {
                    /*
                     * RestoreAssets must be called via the update delegate because [PostProcessBuild] is not guaranteed to be called
                     */
                    EditorApplication.update += RestoreAssets;
                    _monoBehaviourAssetsNeedReverting = true;
                }
                _obfuscatedAfterScene = true;
            }
        }
 
        /**
         * This method restores obfuscated MonoBehaviour cs files to their original names.
         */
        private static void RestoreAssets()
        {
#if UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
#else
            if (BuildPipeline.isBuildingPlayer == false)
            {
#endif
                try
                {
                    EditorApplication.LockReloadAssemblies();
                    Obfuscator.RevertAssetObfuscation();
                    _monoBehaviourAssetsNeedReverting = false;
                    EditorApplication.update -= RestoreAssets;
                }
                finally
                {
                    EditorApplication.UnlockReloadAssemblies();
                }
#if UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
#else
            }
#endif
            Obfuscator.Clear();
        }
 
        private static void DeleteObfuscatedDll(string target)
        {
            int attempts = 60;
            while (attempts > 0)
            {
                try
                {
                    AttemptToDeleteObfuscatedDll(target);
                    if (attempts < 60) Debug.LogWarning("Successfully accessed " + target);
                    return;
                }
                catch (Exception)
                {
                    Debug.LogWarning("Failed to access " + target + " - Retrying...");
                    Thread.Sleep(500);
                    if (--attempts <= 0) throw;
                }
            }
        }
 
        private static void AttemptToDeleteObfuscatedDll(string target)
        {
            if (File.Exists(target)) File.Delete(target);
        }
        /**
         * This method restores original Dlls back into the project.
         * DLLs declared within permanentDLLs will be restored from this method.
         */
        private static void RestoreOriginalDlls()
        {
#if UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
#else
            if (BuildPipeline.isBuildingPlayer) return;
#endif
            foreach (string location in DllsToRevert.Keys)
            {
                try
                {
                    if (!File.Exists(location)) continue;
                        
                    string target = DllsToRevert[location];
 
                    DeleteObfuscatedDll(target);
 
                    File.Move(location, DllsToRevert[location]);
                }
                catch (Exception e)
                {
                    Debug.LogError("Could not restore original DLL to " + DllsToRevert[location] + "\n" + e);
                }
            }
            DllsToRevert.Clear();
            EditorApplication.update -= RestoreOriginalDlls;
            _dllsNeedRestoring = false;
        }
    }
}