Ejemplo n.º 1
0
        void DoExportDLL(string dllPath)
        {
            var fileCollection  = Util.CreateGameCollection();
            var assemblyManager = (AssemblyManager)fileCollection.AssemblyManager;

            fileCollection.AssemblyManager.Load(dllPath);
            fileCollection.Exporter.Export(ExportPath, fileCollection, new SerializedFile[] { }, options);

            ScriptExportManager scriptManager = new ScriptExportManager(fileCollection.Layout, ExportPath);
            AssemblyDefinition  myLibrary     = AssemblyDefinition.ReadAssembly(dllPath);
            var refrences = myLibrary.MainModule.AssemblyReferences;

            foreach (TypeDefinition type in myLibrary.MainModule.Types)
            {
                //TODO: only export unity serializable classes
                if (!type.IsClass || type.Name == "<Module>")
                {
                    continue;
                }
                var libName    = myLibrary.Name.Name;
                var @namespace = type.Namespace;
                var className  = type.Name;
                var scriptID   = assemblyManager.GetScriptID(libName, @namespace, className);
                var exportType = assemblyManager.GetExportType(scriptManager, scriptID);
                scriptManager.Export(exportType);
            }
        }
        void DoExportDLL(string dllPath)
        {
            fileCollection = new FileCollection(new FileCollection.Parameters()
            {
                RequestAssemblyCallback = RequestAssembly,
                RequestResourceCallback = RequestResource
            });
            fileCollection.AssemblyManager.ScriptingBackEnd = ScriptingBackEnd.Mono;
            var assemblyManager = (AssemblyManager)fileCollection.AssemblyManager;

            fileCollection.AssemblyManager.Load(dllPath);
            fileCollection.Exporter.Export(ExportPath, fileCollection, new Object[] { }, options);
            ScriptExportManager scriptManager = new ScriptExportManager(ExportPath);
            AssemblyDefinition  myLibrary     = AssemblyDefinition.ReadAssembly(dllPath);
            var refrences = myLibrary.MainModule.AssemblyReferences;

            foreach (TypeDefinition type in myLibrary.MainModule.Types)
            {
                //TODO: only export unity serializable classes
                if (!type.IsClass || type.Name == "<Module>")
                {
                    continue;
                }
                var libName    = myLibrary.Name.Name;
                var @namespace = type.Namespace;
                var className  = type.Name;
                var scriptID   = assemblyManager.GetScriptID(libName, @namespace, className);
                var exportType = assemblyManager.GetExportType(scriptManager, scriptID);
                scriptManager.Export(exportType);
            }
        }
        //Refer MonoManager, ScriptAssetExporter, ScriptExportManager
        void DoExport(Func <MonoScript, bool> selector = null)
        {
            var managedPath            = Path.Combine(GameDir, "Managed");
            var globalgamemanagersPath = Path.Combine(GameDir, "globalgamemanagers.assets");
            var gameStructure          = GameStructure.Load(new string[]
            {
                globalgamemanagersPath,
                managedPath
            });

            fileCollection = gameStructure.FileCollection;
            if (selector == null)
            {
                selector = (o) => true;
            }
            var assets = fileCollection.FetchAssets().Where(o => o is MonoScript ms && selector(ms)).ToArray();
            ScriptExportManager scriptManager = new ScriptExportManager(ExportPath);
            Dictionary <Object, ScriptExportType> exportTypes = new Dictionary <Object, ScriptExportType>();

            foreach (Object asset in assets)
            {
                MonoScript       script     = (MonoScript)asset;
                ScriptExportType exportType = script.GetExportType(scriptManager);
                exportTypes.Add(asset, exportType);
            }
            foreach (KeyValuePair <Object, ScriptExportType> exportType in exportTypes)
            {
                string path = scriptManager.Export(exportType.Value);
            }
            //scriptManager.ExportRest();
        }
Ejemplo n.º 4
0
        public static string GetFullName(TypeReference type, string module)
        {
            string name     = GetNestedName(type);
            string fullName = $"{type.Namespace}.{name}";

            return(ScriptExportManager.ToFullName(module, fullName));
        }
Ejemplo n.º 5
0
 public ScriptExportType GetExportType(ScriptExportManager exportManager, ScriptIdentifier scriptID)
 {
     if (ScriptingBackend == ScriptingBackend.Unknown)
     {
         throw new Exception("You have to set backend first");
     }
     return(m_manager.GetExportType(exportManager, scriptID));
 }
Ejemplo n.º 6
0
 public ScriptExportType CreateExportType(ScriptExportManager exportManager, string assembly, string @namespace, string name)
 {
     if (!m_assemblyManager.IsAssemblyLoaded(assembly))
     {
         m_requestAssemblyCallback.Invoke(assembly);
     }
     return(m_assemblyManager.CreateExportType(exportManager, assembly, @namespace, name));
 }
Ejemplo n.º 7
0
        public ScriptExportType GetExportType(ScriptExportManager exportManager)
        {
            ScriptIdentifier scriptID = IsReadNamespace(File.Version) ?
                                        File.AssemblyManager.GetScriptID(AssemblyName, Namespace, ClassName) :
                                        File.AssemblyManager.GetScriptID(AssemblyName, ClassName);

            return(File.AssemblyManager.GetExportType(exportManager, scriptID));
        }
Ejemplo n.º 8
0
        public ScriptExportType GetExportType(ScriptExportManager exportManager, ScriptIdentifier scriptID)
        {
            TypeDefinition type = FindType(scriptID);

            if (type == null)
            {
                throw new ArgumentException($"Can't find type {scriptID.UniqueName}");
            }
            return(exportManager.RetrieveType(type));
        }
Ejemplo n.º 9
0
        public ScriptExportType CreateExportType(ScriptExportManager exportManager, string assembly, string @namespace, string name)
        {
            TypeDefinition type = FindType(assembly, @namespace, name);

            if (type == null)
            {
                throw new ArgumentException($"Can't find type {@namespace}.{name}[{assembly}]");
            }
            return(exportManager.CreateExportType(type));
        }
Ejemplo n.º 10
0
 public ScriptExportType CreateExportType(ScriptExportManager exportManager)
 {
     if (IsReadNamespace(File.Version))
     {
         return(File.AssemblyManager.CreateExportType(exportManager, AssemblyName, Namespace, ClassName));
     }
     else
     {
         return(File.AssemblyManager.CreateExportType(exportManager, AssemblyName, ClassName));
     }
 }
Ejemplo n.º 11
0
        public ScriptExportMonoAttribute(CustomAttribute attribute)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            Attribute = attribute;

            m_module   = ScriptExportMonoType.GetModule(Attribute.AttributeType);
            m_fullName = ScriptExportManager.ToFullName(Module, Attribute.AttributeType.FullName);
        }
Ejemplo n.º 12
0
        public ScriptExportMonoEnum(TypeReference @enum)
        {
            if (@enum == null)
            {
                throw new ArgumentNullException(nameof(@enum));
            }

            Type = @enum;
            if (@enum.Module != null)
            {
                Definition = @enum.Resolve();
            }

            m_module   = ScriptExportMonoType.GetModule(Type);
            m_fullName = ScriptExportManager.ToFullName(Module, Type.FullName);
        }
Ejemplo n.º 13
0
        public void Export(IExportContainer container, IEnumerable <Object> assets, string dirPath, Action <IExportContainer, Object, string> callback)
        {
            ScriptExportManager scriptManager = new ScriptExportManager(dirPath);

            foreach (Object asset in assets)
            {
                MonoScript       script     = (MonoScript)asset;
                ScriptExportType exportType = script.CreateExportType(scriptManager);
                string           path       = scriptManager.Export(exportType);
                if (path != null)
                {
                    callback?.Invoke(container, asset, path);
                }
            }
            scriptManager.ExportRest();
        }
        public ScriptExportMonoDelegate(TypeDefinition @delegate)
        {
            if (@delegate == null)
            {
                throw new ArgumentNullException(nameof(@delegate));
            }
            if (!IsDelegate(@delegate))
            {
                throw new Exception("Type isn't delegate");
            }

            Type = @delegate;

            m_module   = ScriptExportMonoType.GetModule(Type);
            m_fullName = ScriptExportManager.ToFullName(Module, Type.FullName);
        }
Ejemplo n.º 15
0
        public ScriptExportMonoArray(TypeReference type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (!type.IsArray)
            {
                throw new Exception("Type isn't an array");
            }

            Type = type;

            m_module   = ScriptExportMonoType.GetModule(Type);
            m_fullName = ScriptExportManager.ToFullName(Module, Type.FullName);
        }
Ejemplo n.º 16
0
        public ScriptExportMonoType(TypeReference type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            Type = type;
            if (type.Module != null)
            {
                Definition = type.Resolve();
            }

            m_name     = GetName();
            m_module   = GetModule(Type);
            m_fullName = ScriptExportManager.ToFullName(Module, Type.FullName);
        }
Ejemplo n.º 17
0
        public ScriptExportMonoGeneric(TypeReference type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (!type.IsGenericInstance)
            {
                throw new Exception("Type isn't generic");
            }

            Type = (GenericInstanceType)type;

            m_name     = GetName();
            m_module   = ScriptExportMonoType.GetModule(Type);
            m_fullName = ScriptExportManager.ToFullName(Module, Type.FullName);
        }
Ejemplo n.º 18
0
        public void Export(IExportContainer container, IEnumerable <Object> assets, string dirPath, Action <IExportContainer, Object, string> callback)
        {
            ScriptExportManager scriptManager = new ScriptExportManager(dirPath);
            Dictionary <Object, ScriptExportType> exportTypes = new Dictionary <Object, ScriptExportType>();

            foreach (Object asset in assets)
            {
                MonoScript       script     = (MonoScript)asset;
                ScriptExportType exportType = script.GetExportType(scriptManager);
                exportTypes.Add(asset, exportType);
            }
            foreach (KeyValuePair <Object, ScriptExportType> exportType in exportTypes)
            {
                string path = scriptManager.Export(exportType.Value);
                if (path != null)
                {
                    callback?.Invoke(container, exportType.Key, path);
                }
            }
            scriptManager.ExportRest();
        }
        //Refer MonoManager, ScriptAssetExporter, ScriptExportManager
        void DoExport(Func <MonoScript, bool> selector = null)
        {
            var managedPath            = Path.Combine(GameDir, "Managed");
            var globalgamemanagersPath = Path.Combine(GameDir, "globalgamemanagers.assets");
            var gameStructure          = GameStructure.Load(new string[]
            {
                globalgamemanagersPath,
                managedPath
            });

            fileCollection = gameStructure.FileCollection;
            if (selector == null)
            {
                selector = (o) => true;
            }
            var assets = fileCollection.FetchAssets().Where(o => o is MonoScript ms && selector(ms)).ToArray();
            ScriptExportManager scriptManager = new ScriptExportManager(ExportPath);
            Dictionary <Object, ScriptExportType> exportTypes = new Dictionary <Object, ScriptExportType>();

            foreach (Object asset in assets)
            {
                MonoScript script = (MonoScript)asset;
                if (ScriptByName)
                {
                    using (MD5 md5 = MD5.Create())
                    {
                        var data    = md5.ComputeHash(Encoding.UTF8.GetBytes($"{script.AssemblyName}.{script.Namespace}.{script.ClassName}"));
                        var newGuid = new Guid(data);
                        Util.SetGUID(script, newGuid);
                    }
                }
                ScriptExportType exportType = script.GetExportType(scriptManager);
                exportTypes.Add(asset, exportType);
            }
            foreach (KeyValuePair <Object, ScriptExportType> exportType in exportTypes)
            {
                string path = scriptManager.Export(exportType.Value);
            }
            //scriptManager.ExportRest();
        }
Ejemplo n.º 20
0
 public static string ToFullName(CustomAttribute attribute)
 {
     return(ScriptExportManager.ToFullName(GetModule(attribute.AttributeType), attribute.AttributeType.FullName));
 }
Ejemplo n.º 21
0
 public static string ToFullName(TypeReference type)
 {
     return(ScriptExportManager.ToFullName(GetModule(type), type.FullName));
 }
Ejemplo n.º 22
0
 public ScriptExportType CreateExportType(ScriptExportManager exportManager, string assembly, string @namespace, string name)
 {
     return(m_manager.CreateExportType(exportManager, assembly, @namespace, name));
 }
Ejemplo n.º 23
0
 public ScriptExportType GetExportType(ScriptExportManager exportManager, ScriptIdentifier scriptID)
 {
     return(m_manager.GetExportType(exportManager, scriptID));
 }
Ejemplo n.º 24
0
        private IReadOnlyList <ScriptExportMethod> CreateMethods(IScriptExportManager manager)
        {
            if (Definition == null || Definition.BaseType == null || Definition.BaseType.Module == null)
            {
                return(Array.Empty <ScriptExportMethod>());
            }

            // we need to export only such properties that are declared as asbtract inside builin assemblies
            // and not overridden anywhere except current type
            List <MethodDefinition> overrides = new List <MethodDefinition>();

            foreach (MethodDefinition method in Definition.Methods)
            {
                if (method.IsVirtual && method.IsReuseSlot && !method.IsGetter && !method.IsSetter)
                {
                    overrides.Add(method);
                }
            }

            List <ScriptExportMethod> methods    = new List <ScriptExportMethod>();
            MonoTypeContext           context    = new MonoTypeContext(Definition);
            TypeDefinition            definition = Definition;

            while (true)
            {
                if (overrides.Count == 0)
                {
                    break;
                }
                if (definition.BaseType == null || definition.BaseType.Module == null)
                {
                    break;
                }

                context    = context.GetBase();
                definition = context.Type.Resolve();
                if (definition == null)
                {
                    break;
                }

                string module    = GetModuleName(definition);
                bool   isBuiltIn = ScriptExportManager.IsBuiltinLibrary(module);
                IReadOnlyDictionary <GenericParameter, TypeReference> arguments = context.GetContextArguments();
                // definition is a Template for GenericInstance, so we must recreate context
                MonoTypeContext definitionContext = new MonoTypeContext(definition, arguments);
                foreach (MethodDefinition method in definition.Methods)
                {
                    if (method.IsVirtual && (method.IsNewSlot || method.IsReuseSlot))
                    {
                        for (int i = 0; i < overrides.Count; i++)
                        {
                            MethodDefinition @override = overrides[i];
                            if (MonoUtils.AreSame(@override, definitionContext, method))
                            {
                                if (isBuiltIn && method.IsAbstract)
                                {
                                    ScriptExportMethod exportMethod = manager.RetrieveMethod(@override);
                                    methods.Add(exportMethod);
                                }

                                overrides.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
            }
            return(methods.ToArray());
        }
Ejemplo n.º 25
0
        private IReadOnlyList <ScriptExportProperty> CreateProperties(IScriptExportManager manager)
        {
            if (Definition == null || Definition.BaseType == null || Definition.BaseType.Module == null)
            {
                return(Array.Empty <ScriptExportProperty>());
            }

            // we need to export only such properties that are declared as asbtract inside builin assemblies
            // and not overridden anywhere except current type
            List <PropertyDefinition> overrides = new List <PropertyDefinition>();

            foreach (PropertyDefinition property in Definition.Properties)
            {
                MethodDefinition method = property.GetMethod == null ? property.SetMethod : property.GetMethod;
                if (method.IsVirtual && method.IsReuseSlot)
                {
                    overrides.Add(property);
                }
            }

            List <ScriptExportProperty> properties = new List <ScriptExportProperty>();
            MonoTypeContext             context    = new MonoTypeContext(Definition);
            TypeDefinition definition = Definition;

            while (true)
            {
                if (overrides.Count == 0)
                {
                    break;
                }
                if (definition.BaseType == null || definition.BaseType.Module == null)
                {
                    break;
                }

                context    = context.GetBase();
                definition = context.Type.Resolve();
                if (definition == null)
                {
                    break;
                }

                string module    = GetModuleName(context.Type);
                bool   isBuiltIn = ScriptExportManager.IsBuiltinLibrary(module);
                foreach (PropertyDefinition property in definition.Properties)
                {
                    MethodDefinition method = property.GetMethod == null ? property.SetMethod : property.GetMethod;
                    if (method.IsVirtual && (method.IsNewSlot || method.IsReuseSlot))
                    {
                        for (int i = 0; i < overrides.Count; i++)
                        {
                            PropertyDefinition @override = overrides[i];
                            if (@override.Name == property.Name)
                            {
                                if (isBuiltIn && method.IsAbstract)
                                {
                                    ScriptExportProperty exportProperty = manager.RetrieveProperty(@override);
                                    properties.Add(exportProperty);
                                }

                                overrides.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
            }
            return(properties.ToArray());
        }