Beispiel #1
0
        public static void ReplaceWithAssembly()
        {
            Debug.Assert(Selection.activeObject != null);

            EditorUtility.DisplayProgressBar("Replacing source with assembly", "Getting things ready...", 0);

            var assetPath              = AssetDatabase.GetAssetPath(Selection.activeObject);
            var directoryPath          = new FileInfo(assetPath).Directory?.FullName;
            var assemblyDefinitionText = AssetDatabase.LoadAssetAtPath <AssemblyDefinitionAsset>(assetPath).text;
            var scriptAssemblyData     = CustomScriptAssemblyData.FromJson(assemblyDefinitionText);
            var fromAssemblyName       = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName(scriptAssemblyData.name);

            Debug.Assert(!string.IsNullOrEmpty(scriptAssemblyData.name));
            Debug.Assert(fromAssemblyName == assetPath, "Failed to get the proper assembly name!");

            if (CompilationPipeline.GetAssemblies(AssembliesType.Editor).ReplaceSourceWithAssembly(ref scriptAssemblyData, directoryPath) ||
                CompilationPipeline.GetAssemblies(AssembliesType.Player).ReplaceSourceWithAssembly(ref scriptAssemblyData, directoryPath))
            {
                EditorUtility.DisplayProgressBar("Replacing source with assembly", "Saving source meta data for later...", 0.95f);
                File.WriteAllText(assetPath, CustomScriptAssemblyData.ToJson(scriptAssemblyData));
                File.WriteAllText($"{Path.GetFullPath(assetPath).Hide()}{JSON}", JsonUtility.ToJson(scriptAssemblyData.Source, true));
            }
            else
            {
                Debug.LogError("Failed to replace source code with assembly!");
            }

            if (!EditorApplication.isUpdating)
            {
                AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
            }

            EditorUtility.ClearProgressBar();
        }
        static void SaveAssemblyDefinitionState(AssemblyDefintionState state)
        {
            var references             = state.references.Where(r => r.asset != null);
            var platforms              = Compilation.CompilationPipeline.GetAssemblyDefinitionPlatforms();
            var OptinalUnityAssemblies = CustomScriptAssembly.OptinalUnityAssemblies;

            CustomScriptAssemblyData data = new CustomScriptAssemblyData();

            data.name = state.name;

            data.references = references.Select(r => r.data.name).ToArray();

            List <string> optionalUnityReferences = new List <string>();

            for (int i = 0; i < OptinalUnityAssemblies.Length; i++)
            {
                if (state.optionalUnityReferences[i] == MixedBool.True)
                {
                    optionalUnityReferences.Add(OptinalUnityAssemblies[i].OptionalUnityReferences.ToString());
                }
            }
            data.optionalUnityReferences = optionalUnityReferences.ToArray();

            data.allowUnsafeCode = ToBool(state.allowUnsafeCode);

            List <string> dataPlatforms = new List <string>();

            for (int i = 0; i < platforms.Length; ++i)
            {
                if (state.platformCompatibility[i] == MixedBool.True)
                {
                    dataPlatforms.Add(platforms[i].Name);
                }
            }

            if (dataPlatforms.Any())
            {
                if (state.compatibleWithAnyPlatform == MixedBool.True)
                {
                    data.excludePlatforms = dataPlatforms.ToArray();
                }
                else
                {
                    data.includePlatforms = dataPlatforms.ToArray();
                }
            }

            var json = CustomScriptAssemblyData.ToJson(data);

            File.WriteAllText(state.path, json);
            state.modified = false;

            AssetDatabase.ImportAsset(state.path);
        }
        private static void SaveAssemblyDefinitionState(AssemblyDefinitionImporterInspector.AssemblyDefintionState state)
        {
            IEnumerable <AssemblyDefinitionImporterInspector.AssemblyDefinitionReference> source = from r in state.references
                                                                                                   where r.asset != null
                                                                                                   select r;

            AssemblyDefinitionPlatform[]       assemblyDefinitionPlatforms = CompilationPipeline.GetAssemblyDefinitionPlatforms();
            CustomScriptOptinalUnityAssembly[] optinalUnityAssemblies      = CustomScriptAssembly.OptinalUnityAssemblies;
            CustomScriptAssemblyData           customScriptAssemblyData    = new CustomScriptAssemblyData();

            customScriptAssemblyData.name       = state.name;
            customScriptAssemblyData.references = (from r in source
                                                   select r.data.name).ToArray <string>();
            List <string> list = new List <string>();

            for (int i = 0; i < optinalUnityAssemblies.Length; i++)
            {
                if (state.optionalUnityReferences[i] == AssemblyDefinitionImporterInspector.MixedBool.True)
                {
                    list.Add(optinalUnityAssemblies[i].OptionalUnityReferences.ToString());
                }
            }
            customScriptAssemblyData.optionalUnityReferences = list.ToArray();
            List <string> list2 = new List <string>();

            for (int j = 0; j < assemblyDefinitionPlatforms.Length; j++)
            {
                if (state.platformCompatibility[j] == AssemblyDefinitionImporterInspector.MixedBool.True)
                {
                    list2.Add(assemblyDefinitionPlatforms[j].Name);
                }
            }
            if (list2.Any <string>())
            {
                if (state.compatibleWithAnyPlatform == AssemblyDefinitionImporterInspector.MixedBool.True)
                {
                    customScriptAssemblyData.excludePlatforms = list2.ToArray();
                }
                else
                {
                    customScriptAssemblyData.includePlatforms = list2.ToArray();
                }
            }
            string contents = CustomScriptAssemblyData.ToJson(customScriptAssemblyData);

            File.WriteAllText(state.path, contents);
            state.modified = false;
            AssetDatabase.ImportAsset(state.path);
        }
        static void SaveAssemblyDefinitionState(AssemblyDefinitionState state)
        {
            var references = state.references;
            var platforms  = CompilationPipeline.GetAssemblyDefinitionPlatforms();

            CustomScriptAssemblyData data = new CustomScriptAssemblyData();

            data.name = state.assemblyName;

            if (state.useGUIDs)
            {
                data.references = references.Select(r =>
                {
                    var guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(r.asset));

                    if (string.IsNullOrEmpty(guid))
                    {
                        return(r.serializedReference);
                    }

                    return(CompilationPipeline.GUIDToAssemblyDefinitionReferenceGUID(guid));
                }).ToArray();
            }
            else
            {
                data.references = references.Select(r => r.name).ToArray();
            }

            data.defineConstraints = state.defineConstraints
                                     .Where(x => !string.IsNullOrEmpty(x.name))
                                     .Select(r => r.name)
                                     .ToArray();

            data.versionDefines = state.versionDefines.Select(x => new UnityEditor.Scripting.ScriptCompilation.VersionDefine
            {
                name       = x.name,
                expression = x.expression,
                define     = x.define,
            }).ToArray();

            data.autoReferenced     = state.autoReferenced;
            data.overrideReferences = state.overrideReferences;

            data.precompiledReferences = state.precompiledReferences
                                         .Select(r => r.name).ToArray();


            data.allowUnsafeCode = state.allowUnsafeCode;

            List <string> dataPlatforms = new List <string>();

            for (int i = 0; i < platforms.Length; ++i)
            {
                if (state.platformCompatibility[i])
                {
                    dataPlatforms.Add(platforms[i].Name);
                }
            }

            if (dataPlatforms.Any())
            {
                if (state.compatibleWithAnyPlatform)
                {
                    data.excludePlatforms = dataPlatforms.ToArray();
                }
                else
                {
                    data.includePlatforms = dataPlatforms.ToArray();
                }
            }

            var json = CustomScriptAssemblyData.ToJson(data);

            File.WriteAllText(state.path, json);

            AssetDatabase.ImportAsset(state.path);
        }