少年修仙传客户端基础资源
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
/*
 * Copyright (c) 2015-2020 Beebyte Limited. All rights reserved.
 */
#if !BEEBYTE_OBFUSCATOR_DISABLE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Beebyte.Obfuscator.Assembly;
using UnityEditor;
using UnityEngine;
 
namespace Beebyte.Obfuscator
{
    /**
     * Handles obfuscation calls for a Unity project and controls restoration of backed up files.
     */
    public class Project
    {
        private readonly bool _developmentBuild;
        private Options _options;
 
        private bool _monoBehaviourAssetsNeedReverting = false;
        private bool _hasError;
        private bool _hasObfuscated;
        private bool _noCSharpScripts;
 
        public Project(bool developmentBuild)
        {
            _developmentBuild = developmentBuild;
        }
        
        public bool ShouldObfuscate()
        {
            if (_options == null) _options = OptionsManager.LoadOptions();
            return _options.enabled && (_options.obfuscateReleaseOnly == false || !_developmentBuild);
        }
 
        public bool IsSuccess()
        {
            return _hasObfuscated || !ShouldObfuscate();
        }
 
        public bool HasCSharpScripts()
        {
            return !_noCSharpScripts;
        }
 
        public bool HasMonoBehaviourAssetsThatNeedReverting()
        {
            return _monoBehaviourAssetsNeedReverting;
        }
 
        public void ObfuscateIfNeeded()
        {
#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 && !_hasObfuscated && _hasError == false)
#else
            if (!EditorApplication.isPlayingOrWillChangePlaymode && !_hasObfuscated && _hasError == false && BuildPipeline.isBuildingPlayer)
#endif
            {
#if !UNITY_5_6_OR_NEWER
                EditorApplication.update += PipelineHook.ClearProjectViaUpdate;
#endif
                try
                {
                    EditorApplication.LockReloadAssemblies();
                    ObfuscateWhileLocked();
                }
                catch (Exception e)
                {
                    Debug.LogError("Obfuscation Failed: " + e);
                    _hasError = true;
                    throw new OperationCanceledException("Obfuscation failed", e);
                }
                finally
                {
                    EditorApplication.UnlockReloadAssemblies();
                }
            }
        }
 
        private void ObfuscateWhileLocked()
        {
            if (_options == null) _options = OptionsManager.LoadOptions();
 
            if (ShouldObfuscate() == false) return;
 
            AssemblySelector selector = new AssemblySelector(_options);
 
            ICollection<string> compiledDlls = selector.GetCompiledAssemblyPaths();
 
            if (compiledDlls.Count > 0)
            {
                EditorApplication.update += RestoreUtils.RestoreOriginalDlls;
            }
            
            IDictionary<string, string> backupMap = FileBackup.GetBackupMap(compiledDlls);
            FileBackup.Backup(backupMap);
 
            ICollection<string> dlls = selector.GetAssemblyPaths();
 
            if (dlls.Count == 0 && compiledDlls.Count == 0)
            {
                _noCSharpScripts = true;
                return;
            }
 
#if UNITY_2017_3_OR_NEWER
            Obfuscator.AppendReferenceAssemblies(AssemblyReferenceLocator.GetAssemblyReferenceDirectories().ToArray());
#endif
                
#if UNITY_2018_2_OR_NEWER
            Obfuscator.ObfuscateMonoBehavioursByAssetDatabase(false);
            var obfuscateMonoBehaviourNames = _options.obfuscateMonoBehaviourClassNames;
            try
            {
                if (IsXCodeProject() && _options.obfuscateMonoBehaviourClassNames)
                {
                    Debug.LogWarning("MonoBehaviour class names will not be obfuscated when creating Xcode projects");
                    _options.obfuscateMonoBehaviourClassNames = false;
                }
#endif
 
                Obfuscator.Obfuscate(dlls, compiledDlls, _options, EditorUserBuildSettings.activeBuildTarget);
 
#if !UNITY_2018_2_OR_NEWER
            if (_options.obfuscateMonoBehaviourClassNames)
            {
                /*
                 * RestoreAssets must be registered via the update delegate because [PostProcessBuild] is not guaranteed to be called
                 */
                EditorApplication.update += RestoreUtils.RestoreMonobehaviourSourceFiles;
                _monoBehaviourAssetsNeedReverting = true;
            }
#else
            }
            finally
            {
                _options.obfuscateMonoBehaviourClassNames = obfuscateMonoBehaviourNames;
            }
#endif
            _hasObfuscated = true;
        }
 
#if UNITY_2018_2_OR_NEWER
        private bool IsXCodeProject()
        {
            return EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneOSX &&
                   EditorUserBuildSettings.GetPlatformSettings("OSXUniversal", "CreateXcodeProject").Equals("true");
        }
#endif
 
        public void ObfuscateAssets(BuildTarget buildTarget, string pathToBuildProject)
        {
#if UNITY_2018_2_OR_NEWER
            if (IsXCodeProject()) return;
            if (_options == null) _options = OptionsManager.LoadOptions();
            if (_options.obfuscateMonoBehaviourClassNames && File.Exists("_AssetTranslations"))
            {
                string pathToGlobalGameManagersAsset = GlobalGameManagersPath.GetPathToGlobalGameManagersAsset(buildTarget, pathToBuildProject);
                Obfuscator.RenameScriptableAssets("_AssetTranslations", pathToGlobalGameManagersAsset);
            }
#endif
        }
    }
}
#endif