Пример #1
0
 public UnrealModuleInfo(UPackage package, string name, string path)
 {
     Package = package;
     Name    = name;
     Path    = path;
     Type    = GetModuleType(path);
 }
Пример #2
0
 public UnrealModuleInfo(UPackage package, string name, string path, UnrealModuleType type)
 {
     Package = package;
     Name    = name;
     Path    = path;
     Type    = type;
 }
Пример #3
0
 public UnrealModuleInfo(UPackage package, string name, string path)
     : this(package, name, path, UnrealModuleType.Unknown)
 {
     IPlugin[] plugins = IPluginManager.Instance.GetDiscoveredPlugins();
     Type = GetModuleType(name, path, plugins);
     IPlugin.Dispose(plugins);
 }
Пример #4
0
 /// <summary>
 /// Loads a package and all contained objects that match context flags.
 /// </summary>
 /// <param name="outer">Package to load new package into (usually NULL or ULevel->GetOuter())</param>
 /// <param name="longPackageName">Long package name to load</param>
 /// <param name="loadFlags">Flags controlling loading behavior</param>
 /// <returns>Loaded package if successful, NULL otherwise</returns>
 public static UPackage LoadPackage(UPackage outer, string longPackageName, ELoadFlags loadFlags)
 {
     using (FStringUnsafe longPackageNameUnsafe = new FStringUnsafe(longPackageName))
     {
         return(GCHelper.Find <UPackage>(Native_UObjectGlobals.LoadPackage(
                                             outer == null ? IntPtr.Zero : outer.Address, ref longPackageNameUnsafe.Array, loadFlags)));
     }
 }
Пример #5
0
 /// <summary>
 /// Converts package name to short name.
 /// </summary>
 /// <param name="package">Package which name to convert.</param>
 /// <returns>Short package name.</returns>
 public static string GetShortName(UPackage package)
 {
     using (FStringUnsafe resultUnsafe = new FStringUnsafe())
     {
         Native_FPackageName.GetShortNameFromPackage(package == null ? IntPtr.Zero : package.Address, ref resultUnsafe.Array);
         return(resultUnsafe.Value);
     }
 }
Пример #6
0
        public UPackage GetPackage()
        {
            if (PackageName == FName.None)
            {
                return(null);
            }

            UPackage package = UObject.FindPackage(null, PackageName.ToString());

            if (package != null)
            {
                package.FullyLoad();
            }
            else
            {
                package = UObject.LoadPackage(null, PackageName.ToString(), ELoadFlags.None);
            }
            return(package);
        }
Пример #7
0
        public void GenerateCodeForModule(string moduleName, bool loadModule)
        {
            string   longPackageName = FPackageName.ConvertToLongScriptPackageName(moduleName);
            UPackage package         = UObject.FindObjectFast <UPackage>(null, new FName(longPackageName), false, false);

            if (package == null && !FModuleManager.Instance.IsModuleLoaded(new FName(moduleName)))
            {
                // package is almost always non-null even when IsModuleLoaded returns false - if the package is non-null it
                // seems the module types are available so we don't have to call LoadModule (check if this is always true)

                if (!loadModule)
                {
                    return;
                }

                EModuleLoadResult loadResult;
                FModuleManager.Instance.LoadModuleWithFailureReason(new FName(moduleName), out loadResult);
                if (loadResult != EModuleLoadResult.Success)
                {
                    FMessage.Log("Failed to load module '" + moduleName + "'. Reason: " + loadResult);
                    return;
                }

                package = UObject.FindObjectFast <UPackage>(null, new FName(longPackageName), false, false);
            }

            if (package != null)
            {
                Dictionary <FName, string> modulePaths = FModulesPaths.FindModulePaths("*");
                string modulePath;
                if (!modulePaths.TryGetValue(new FName(moduleName), out modulePath))
                {
                    return;
                }

                BeginGenerateModules();
                GenerateCodeForModule(new UnrealModuleInfo(package, moduleName, modulePath));
                EndGenerateModules();
            }
        }
Пример #8
0
        private string GetModuleName(UField field, out UnrealModuleType moduleType, out UnrealModuleType moduleAssetType)
        {
            moduleType      = UnrealModuleType.Unknown;
            moduleAssetType = UnrealModuleType.Unknown;
            UPackage package = field.GetOutermost();

            if (package != null)
            {
                CachedNamespace cachedNamespace;
                if (!namespaceCache.TryGetValue(package, out cachedNamespace))
                {
                    GetModuleNamespace(field);
                    namespaceCache.TryGetValue(package, out cachedNamespace);
                }

                if (cachedNamespace != null)
                {
                    moduleType      = cachedNamespace.ModuleType;
                    moduleAssetType = cachedNamespace.ModuleAssetType;
                    return(cachedNamespace.ModuleName);
                }
            }
            return(null);
        }
Пример #9
0
        private static UPackage FindOrLoadPackage(string pathName)
        {
            // If there is a dot, remove it.
            int packageDelimPos = pathName.IndexOf('.');

            if (packageDelimPos != -1)
            {
                pathName = pathName.Remove(packageDelimPos, 1);
            }

            // Find the package in memory.
            UPackage package = UObject.FindPackage(null, pathName);

            if (package == null)
            {
                // If it is not in memory, try to load it.
                package = UObject.LoadPackage(null, pathName, ELoadFlags.None);
            }
            if (package != null)
            {
                package.AddToRoot();
            }
            return(package);
        }
Пример #10
0
        public void GenerateCodeForModules(UnrealModuleType[] moduleTypes)
        {
            BeginGenerateModules();

            HashSet <string> moduleNamesWhitelistToLower = new HashSet <string>();

            foreach (string moduleName in ModulesNamesWhitelist)
            {
                moduleNamesWhitelistToLower.Add(moduleName.ToLower());
            }

            HashSet <string> moduleNamesBlacklistToLower = new HashSet <string>();

            foreach (string moduleName in ModulesNamesBlacklist)
            {
                moduleNamesBlacklistToLower.Add(moduleName.ToLower());
            }

            Dictionary <UPackage, List <UStruct> >   structsByPackage         = new Dictionary <UPackage, List <UStruct> >();
            Dictionary <UPackage, List <UEnum> >     enumsByPackage           = new Dictionary <UPackage, List <UEnum> >();
            Dictionary <UPackage, List <UFunction> > globalFunctionsByPackage = new Dictionary <UPackage, List <UFunction> >();

            foreach (UStruct unrealStruct in new TObjectIterator <UStruct>())
            {
                if (!unrealStruct.IsA <UFunction>() && CanExportStruct(unrealStruct) && IsAvailableType(unrealStruct))
                {
                    UPackage package = unrealStruct.GetOutermost();
                    if (package != null)
                    {
                        List <UStruct> structs;
                        if (!structsByPackage.TryGetValue(package, out structs))
                        {
                            structsByPackage.Add(package, structs = new List <UStruct>());
                        }
                        structs.Add(unrealStruct);
                    }
                }
            }

            foreach (UEnum unrealEnum in new TObjectIterator <UEnum>())
            {
                if (CanExportEnum(unrealEnum) && IsAvailableType(unrealEnum))
                {
                    UPackage package = unrealEnum.GetOutermost();
                    if (package != null)
                    {
                        List <UEnum> enums;
                        if (!enumsByPackage.TryGetValue(package, out enums))
                        {
                            enumsByPackage.Add(package, enums = new List <UEnum>());
                        }
                        enums.Add(unrealEnum);
                    }
                }
            }

            UClass packageClass = UClass.GetClass <UPackage>();

            foreach (UFunction function in new TObjectIterator <UFunction>())
            {
                UObject outer = function.GetOuter();
                if (outer == null || outer.GetClass() != packageClass)
                {
                    continue;
                }

                if (function.HasAnyFunctionFlags(EFunctionFlags.Delegate | EFunctionFlags.MulticastDelegate))
                {
                    UPackage package = function.GetOutermost();
                    if (package != null)
                    {
                        List <UFunction> functions;
                        if (!globalFunctionsByPackage.TryGetValue(package, out functions))
                        {
                            globalFunctionsByPackage.Add(package, functions = new List <UFunction>());
                        }
                        functions.Add(function);
                    }
                }
                else
                {
                    FMessage.Log(ELogVerbosity.Error, string.Format("Global function which isn't a delegate '{0}'", function.GetName()));
                }
            }

            Dictionary <FName, string> modulePaths = FModulesPaths.FindModulePaths("*");

            // Make sure ModulePaths and ModuleNames are the same. Based on comments FindModules may be changed/removed in the
            // future so we will want to just use FindModulePaths. For now make sure they are synced up.
            FName[] moduleNames = FModuleManager.Get().FindModules("*");
            if (moduleNames.Length != modulePaths.Count)
            {
                FMessage.Log(ELogVerbosity.Warning, string.Format("Module count invalid (update FModulePaths). FindModules:{0} FindModulePaths:{1}",
                                                                  moduleNames.Length, modulePaths.Count));

                List <FName> additionalNames = new List <FName>();
                foreach (KeyValuePair <FName, string> module in modulePaths)
                {
                    if (moduleNames.Contains(module.Key))
                    {
                        FMessage.Log(ELogVerbosity.Warning, "Module: " + module.Key + " - " + module.Value);
                    }
                    else
                    {
                        FMessage.Log(ELogVerbosity.Warning, "Additional module: " + module.Key + " - " + module.Value);
                    }
                }

                List <FName> missingNames = new List <FName>();
                foreach (FName moduleName in moduleNames)
                {
                    if (!modulePaths.ContainsKey(moduleName))
                    {
                        FMessage.Log(ELogVerbosity.Warning, "Missing module: " + moduleName);
                    }
                }
            }


            IPlugin[] plugins = IPluginManager.Instance.GetDiscoveredPlugins();

            SlowTaskSetModuleCount(modulePaths.Count);

            foreach (KeyValuePair <FName, string> modulePath in modulePaths)
            {
                SlowTaskBeginModule(modulePath.Key.PlainName);

                string   moduleName      = modulePath.Key.PlainName;
                string   longPackageName = FPackageName.ConvertToLongScriptPackageName(moduleName);
                UPackage package         = UObject.FindObjectFast <UPackage>(null, new FName(longPackageName), false, false);
                if (package != null)
                {
                    UnrealModuleInfo moduleInfo = new UnrealModuleInfo(package, moduleName, modulePath.Value,
                                                                       UnrealModuleInfo.GetModuleType(moduleName, modulePath.Value, plugins));

                    if (moduleInfo.Type == UnrealModuleType.Unknown)
                    {
                        FMessage.Log(ELogVerbosity.Error, string.Format("Unknown module type on module '{0}' '{1}'",
                                                                        moduleInfo.Name, moduleInfo.Package));
                    }
                    else if (!moduleTypes.Contains(moduleInfo.Type) || moduleNamesBlacklistToLower.Contains(moduleInfo.Name.ToLower()) ||
                             (moduleNamesWhitelistToLower.Count > 0 && !moduleNamesWhitelistToLower.Contains(moduleInfo.Name.ToLower())))
                    {
                        continue;
                    }

                    List <UStruct> structs;
                    if (!structsByPackage.TryGetValue(package, out structs))
                    {
                        structs = new List <UStruct>();
                    }

                    List <UEnum> enums;
                    if (!enumsByPackage.TryGetValue(package, out enums))
                    {
                        enums = new List <UEnum>();
                    }

                    List <UFunction> globalFunctions;
                    if (!globalFunctionsByPackage.TryGetValue(package, out globalFunctions))
                    {
                        globalFunctions = new List <UFunction>();
                    }

                    SlowTaskUpdateTarget(structs.Count + enums.Count + globalFunctions.Count);

                    GenerateCodeForModule(moduleInfo, structs.ToArray(), enums.ToArray(), globalFunctions.ToArray());
                }
            }

            IPlugin.Dispose(plugins);

            EndGenerateModules();
        }
Пример #11
0
        private string GetModuleNamespace(UField field, out UnrealModuleType moduleAssetType, bool allowFoldersAsNamespace = true)
        {
            moduleAssetType = UnrealModuleType.Unknown;
            UPackage package = field.GetOutermost();

            if (package != null)
            {
                CachedNamespace cachedNamespace;
                if (namespaceCache.TryGetValue(package, out cachedNamespace))
                {
                    moduleAssetType = cachedNamespace.ModuleAssetType;
                    return(cachedNamespace.Namespace);
                }

                UnrealModuleType moduleType = UnrealModuleType.Unknown;
                moduleAssetType = UnrealModuleType.Unknown;

                string packageFilename = package.FileName.ToString();
                if (string.IsNullOrEmpty(packageFilename.ToString()) || packageFilename == FName.None.ToString())
                {
                    packageFilename = field.GetPathName();
                }

                string moduleName = FPackageName.GetShortName(package.GetName());
                if (packageFilename.StartsWith("/Script"))
                {
                    if (!modulesByName.TryGetValue(new FName(moduleName), out moduleType))
                    {
                        moduleType = UnrealModuleType.Unknown;
                        FMessage.Log(ELogVerbosity.Error, string.Format("Failed to find module for module '{0}'", moduleName));
                    }
                }
                else if (packageFilename.StartsWith("/Game/"))
                {
                    moduleType      = UnrealModuleType.Game;
                    moduleAssetType = UnrealModuleType.Game;
                    moduleName      = FPaths.GetBaseFilename(FPaths.ProjectFilePath);// {Module} same as {Game}
                }
                else if (packageFilename.StartsWith("/Engine/"))
                {
                    moduleType      = UnrealModuleType.Game;
                    moduleAssetType = UnrealModuleType.Engine;
                    moduleName      = Settings.Namespaces.Default;
                }
                else
                {
                    string rootName = null;
                    if (packageFilename.Length > 1 && packageFilename[0] == '/')
                    {
                        int slashIndex = packageFilename.IndexOf('/', 1);
                        if (slashIndex >= 0)
                        {
                            rootName   = packageFilename.Substring(1, slashIndex - 1);
                            moduleName = rootName;// Update ModuleName for {Module}

                            if (!modulesByName.TryGetValue(new FName(rootName), out moduleAssetType))
                            {
                                moduleAssetType = UnrealModuleType.Unknown;
                            }
                        }
                    }

                    if (moduleAssetType == UnrealModuleType.Unknown)
                    {
                        FMessage.Log(ELogVerbosity.Error, string.Format("Unknown module asset type root:'{0}' path:'{1}' name:'{2}' path2:'{3}'",
                                                                        rootName, packageFilename, field.GetName(), field.GetPathName()));
                    }
                    moduleType = UnrealModuleType.Game;
                }

                if (moduleType != UnrealModuleType.Unknown)
                {
                    string namespaceName = GetModuleNamespace(moduleType, moduleName, moduleAssetType, allowFoldersAsNamespace, packageFilename);
                    namespaceCache[package] = new CachedNamespace(namespaceName, moduleName, moduleType, moduleAssetType);
                    return(namespaceName);
                }
                else
                {
                    FMessage.Log(ELogVerbosity.Error, string.Format("Unknown module type {0} {1}", packageFilename, moduleName));
                }
            }
            return(null);
        }