示例#1
0
        /// <summary>
        /// Locates all the .dfmod files in the mod path
        /// </summary>
        /// <param name="refresh">Checks for mods to unload.</param>
        private void FindModsFromDirectory(bool refresh = false)
        {
            if (!Directory.Exists(ModDirectory))
            {
                Debug.Log("invalid mod directory: " + ModDirectory);
                return;
            }

            var modFiles       = Directory.GetFiles(ModDirectory, "*" + MODEXTENSION, SearchOption.AllDirectories);
            var modFileNames   = new string[modFiles.Length];
            var loadedModNames = GetAllModFileNames();

            for (int i = 0; i < modFiles.Length; i++)
            {
                string modFilePath = modFiles[i];

                string DirPath = modFilePath.Substring(0, modFilePath.LastIndexOf(Path.DirectorySeparatorChar));
                modFileNames[i] = GetModNameFromPath(modFilePath);

                if (string.IsNullOrEmpty(modFileNames[i]))
                {
                    Debug.Log("failed to get name of mod");
                    continue;
                }

                //prevent trying to re-load same asset bundles on refresh
                if (loadedModNames.Length > 0)
                {
                    if (loadedModNames.Contains(modFileNames[i]))
                    {
                        continue;
                    }
                }

                AssetBundle ab;
                if (!LoadModAssetBundle(modFilePath, out ab))
                {
                    continue;
                }
                Mod mod = new Mod(modFileNames[i], DirPath, ab);

                mod.LoadPriority = i;
                int index = GetModIndex(mod.Title);
                if (index < 0)
                {
                    mods.Add(mod);
                }
            }

            if (refresh)
            {
                for (int j = 0; j < loadedModNames.Length; j++)
                {
                    if (!modFileNames.Contains(loadedModNames[j]))
                    {
                        Debug.Log(string.Format("mod {0} no longer loaded", loadedModNames[j]));
                        UnloadMod(loadedModNames[j], true);
                    }
                }
            }

#if UNITY_EDITOR
            if (LoadVirtualMods)
            {
                foreach (string manifestPath in Directory.GetFiles(Application.dataPath + "/Game/Mods", "*" + MODINFOEXTENSION, SearchOption.AllDirectories))
                {
                    var modInfo = JsonUtility.FromJson <ModInfo>(File.ReadAllText(manifestPath));
                    if (mods.Any(x => x.ModInfo.GUID == modInfo.GUID))
                    {
                        Debug.LogWarningFormat("Ignoring virtual mod {0} because release mod is already loaded.", modInfo.ModTitle);
                        continue;
                    }

                    mods.Add(new Mod(manifestPath, modInfo));
                }
            }
#endif
        }
示例#2
0
 public SetupOptions(int priority, Mod mod, System.Reflection.MethodInfo mi)
 {
     this.priority = priority;
     this.mod      = mod;
     this.mi       = mi;
 }
示例#3
0
        /// <summary>
        /// Locates all the .dfmod files in the mod path
        /// </summary>
        /// <param name="refresh"></param>
        private void FindModsFromDirectory(bool refresh = false)
        {
            if (!Directory.Exists(ModDirectory))
            {
                Debug.Log("invalid mod directory: " + ModDirectory);
                return;
            }

            var modFiles       = Directory.GetFiles(ModDirectory, "*" + MODEXTENSION, SearchOption.AllDirectories);
            var modFileNames   = new string[modFiles.Length];
            var loadedModNames = GetAllModNames();

            for (int i = 0; i < modFiles.Length; i++)
            {
                string modFilePath = modFiles[i];

                string DirPath = modFilePath.Substring(0, modFilePath.LastIndexOf(Path.DirectorySeparatorChar));
                modFileNames[i] = GetModNameFromPath(modFilePath);

                if (string.IsNullOrEmpty(modFileNames[i]))
                {
                    Debug.Log("failed to get mod name of mod");
                    continue;
                }

                //prevent trying to re-load same asset bundles on refresh
                if (loadedModNames.Length > 0)
                {
                    if (loadedModNames.Contains(modFileNames[i]))
                    {
                        continue;
                    }
                }

                AssetBundle ab;
                if (!LoadModAssetBundle(modFilePath, out ab))
                {
                    continue;
                }
                Mod mod = new Mod(modFileNames[i], DirPath, ab);

                mod.LoadPriority = i;
                int index = GetModIndex(mod.Title);
                if (index < 0)
                {
                    Mods.Add(mod);
                }
            }

            if (refresh)
            {
                for (int j = 0; j < loadedModNames.Length; j++)
                {
                    if (!modFileNames.Contains(loadedModNames[j]))
                    {
                        Debug.Log(string.Format("mod {0} no longer loaded", loadedModNames[j]));
                        UnloadMod(loadedModNames[j], true);
                    }
                }
            }
        }
示例#4
0
            private fsResult DeserializeImportedComponents(fsData fsData, GameObject gameObject, Mod mod)
            {
                fsResult fsResult = fsResult.Success;

                if ((fsResult += CheckType(fsData, fsDataType.Object)).Failed)
                {
                    return(fsResult);
                }
                Dictionary <string, fsData> dict = fsData.AsDictionary;

                // Restore components on this gameobject
                fsData components;

                if ((fsResult += CheckKey(dict, "Components", out components)).Failed)
                {
                    return(fsResult);
                }
                if (!components.IsNull)
                {
                    if ((fsResult += CheckType(components, fsDataType.Array)).Failed)
                    {
                        return(fsResult);
                    }
                    foreach (fsData componentData in components.AsList)
                    {
                        // Get type name
                        string typeName;
                        if ((fsResult += DeserializeMember(componentData.AsDictionary, null, "$type", out typeName)).Failed)
                        {
                            return(fsResult);
                        }

                        // Add component and deserialize
                        Type type = FindType(mod, typeName);
                        if (type == null)
                        {
                            return(fsResult += fsResult.Fail(string.Format("Failed to find type {0}.", typeName)));
                        }
                        object instance = gameObject.AddComponent(type);
                        if ((fsResult += fsSerializer.TryDeserialize(componentData, type, ref instance)).Failed)
                        {
                            return(fsResult);
                        }
                    }
                }

                // Restore components on children
                fsData children;

                if ((fsResult += CheckKey(dict, "Children", out children)).Failed)
                {
                    return(fsResult);
                }
                if (!children.IsNull)
                {
                    if ((fsResult += CheckType(children, fsDataType.Object)).Failed)
                    {
                        return(fsResult);
                    }
                    foreach (KeyValuePair <string, fsData> childData in children.AsDictionary)
                    {
                        Transform child = gameObject.transform.Find(childData.Key);
                        if (child == null)
                        {
                            return(fsResult += fsResult.Fail(string.Format("{0} not found on {1}", childData.Key, gameObject.name)));
                        }
                        if ((fsResult += DeserializeImportedComponents(childData.Value, child.gameObject, mod)).Failed)
                        {
                            return(fsResult);
                        }
                    }
                }

                return(fsResult);
            }
示例#5
0
        /// <summary>
        /// Locates all the .dfmod files in the mod path
        /// </summary>
        /// <param name="refresh">Checks for mods to unload.</param>
        private void FindModsFromDirectory(bool refresh = false)
        {
            if (!Directory.Exists(ModDirectory))
            {
                Debug.Log("invalid mod directory: " + ModDirectory);
                return;
            }

            var modFiles       = Directory.GetFiles(ModDirectory, "*" + MODEXTENSION, SearchOption.AllDirectories);
            var modFileNames   = new string[modFiles.Length];
            var loadedModNames = GetAllModFileNames();

            for (int i = 0; i < modFiles.Length; i++)
            {
                string modFilePath = modFiles[i];

                string DirPath = modFilePath.Substring(0, modFilePath.LastIndexOf(Path.DirectorySeparatorChar));
                modFileNames[i] = GetModNameFromPath(modFilePath);

                if (string.IsNullOrEmpty(modFileNames[i]))
                {
                    Debug.Log("failed to get name of mod");
                    continue;
                }

                //prevent trying to re-load same asset bundles on refresh
                if (loadedModNames.Length > 0)
                {
                    if (loadedModNames.Contains(modFileNames[i]))
                    {
                        continue;
                    }
                }

                AssetBundle ab;
                if (!LoadModAssetBundle(modFilePath, out ab))
                {
                    continue;
                }
                Mod mod = new Mod(modFileNames[i], DirPath, ab);

                mod.LoadPriority = i;
                int index = GetModIndex(mod.Title);
                if (index < 0)
                {
                    mods.Add(mod);
                }
            }

            if (refresh)
            {
                for (int j = 0; j < loadedModNames.Length; j++)
                {
                    if (!modFileNames.Contains(loadedModNames[j]))
                    {
                        Debug.Log(string.Format("mod {0} no longer loaded", loadedModNames[j]));
                        UnloadMod(loadedModNames[j], true);
                    }
                }
            }

#if UNITY_EDITOR
            if (LoadVirtualMods)
            {
                foreach (string manifestPath in Directory.GetFiles(EditorModsDirectory, "*" + MODINFOEXTENSION, SearchOption.AllDirectories))
                {
                    ModInfo modInfo = null;
                    if (ModManager._serializer.TryDeserialize(fsJsonParser.Parse(File.ReadAllText(manifestPath)), ref modInfo).Failed)
                    {
                        Debug.LogErrorFormat("Failed to deserialize manifest file {0}", manifestPath);
                        continue;
                    }

                    if (string.IsNullOrWhiteSpace(modInfo.ModTitle))
                    {
                        Debug.LogError($"Discarded {manifestPath} because it doesn't have a valid title.");
                        continue;
                    }

                    if (mods.Any(x => x.ModInfo.GUID == modInfo.GUID))
                    {
                        Debug.LogWarningFormat("Ignoring virtual mod {0} because release mod is already loaded.", modInfo.ModTitle);
                        continue;
                    }

                    mods.Add(new Mod(manifestPath, modInfo));
                }
            }
#endif
        }