Пример #1
0
        private static void _Start()
        {
            if (!Initialize())
            {
                Logger.Log("初始化数据出错,启用已被取消!");
                OpenUnityFileLog();
                return;
            }

            if (started)
            {
                Logger.Log("MOD已经启用,无需再次启用!");
                return;
            }

            started = true;
            if (!string.IsNullOrEmpty(Config.GameVersionPoint))
            {
                try
                {
                    Logger.Log("正在解析游戏版本……");
                    if (Injector.TryParseEntryPoint(Config.GameVersionPoint, out var assembly, out var className,
                                                    out var methodName, out _))
                    {
                        var asm = Assembly.Load(assembly);
                        if (asm == null)
                        {
                            Logger.Error($"找不到Assembly文件“{assembly}”!");
                            goto Next;
                        }

                        var foundClass = asm.GetType(className);
                        if (foundClass == null)
                        {
                            Logger.Error($"找不到类名称“{className}”!");
                            goto Next;
                        }

                        var foundMethod = foundClass.GetMethod(methodName,
                                                               BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                        if (foundMethod == null)
                        {
                            var foundField = foundClass.GetField(methodName,
                                                                 BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                            if (foundField != null)
                            {
                                gameVersion = ParseVersion(foundField.GetValue(null).ToString());
                                Logger.Log($"已检测游戏版本“{gameVersion}”!");
                            }
                            else
                            {
                                Logger.Error($"找不到方法名称“{methodName}”!");
                            }

                            goto Next;
                        }

                        gameVersion = ParseVersion(foundMethod.Invoke(null, null).ToString());
                        Logger.Log($"已检测游戏版本“{gameVersion}”!");
                    }
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    OpenUnityFileLog();
                }
            }

Next:
            GameScripts.Init(ModEntries);
            GameScripts.OnBeforeLoadMods();
            if (Directory.Exists(modsPath))
            {
                Logger.Log("正在解析Mods……");
                var mods      = new Dictionary <string, ModEntry>();
                var countMods = 0;
                foreach (var dir in Directory.GetDirectories(modsPath))
                {
                    var jsonPath = Path.Combine(dir, Config.ModInfo);
                    if (!File.Exists(Path.Combine(dir, Config.ModInfo)))
                    {
                        jsonPath = Path.Combine(dir, Config.ModInfo.ToLower());
                    }

                    if (!File.Exists(jsonPath))
                    {
                        continue;
                    }
                    countMods++;
                    Logger.Log($"正在解析文件“{jsonPath}”……");
                    try
                    {
                        var modInfo = File.ReadAllText(jsonPath).FromJson <ModInfo>();
                        if (string.IsNullOrEmpty(modInfo.Id))
                        {
                            Logger.Error("Id为空!");
                            continue;
                        }
                        if (mods.ContainsKey(modInfo.Id))
                        {
                            Logger.Error($"Id“{modInfo.Id}”已经被另一个MOD占用!");
                            continue;
                        }
                        if (string.IsNullOrEmpty(modInfo.AssemblyName))
                        {
                            modInfo.AssemblyName = modInfo.Id + ".dll";
                        }
                        var modEntry = new ModEntry(modInfo, dir + Path.DirectorySeparatorChar);
                        mods.Add(modInfo.Id, modEntry);
                    }
                    catch (Exception exception)
                    {
                        Logger.Error($"解析文件“{jsonPath}”失败!");
                        Debug.LogException(exception);
                    }
                }
                if (mods.Count > 0)
                {
                    Logger.Log("正在排序Mods……");
                    TopoSort(mods);
                    Params.ReadModParams();
                    Logger.Log("正在加载Mods……");
                    foreach (var mod in ModEntries)
                    {
                        if (!mod.Enabled)
                        {
                            mod.Logger.Log($"MOD“{mod.Info.Id}”已被禁用!");
                        }
                        else
                        {
                            mod.Active = true;
                        }
                    }
                }
                Logger.Log(
                    $"Mods解析完成!成功加载了{ModEntries.Count(x => !x.ErrorOnLoading)}/{countMods}的MOD!{Environment.NewLine}");
                var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.ManifestModule.Name == "UnityModManager.dll");
                if (assemblies.Count() > 1)
                {
                    Logger.Error($"检测到UnityModManager.dll的额外副本!");
                    foreach (var ass in assemblies)
                    {
                        Logger.Log($"- {ass.CodeBase}");
                    }
                    Console.WriteLine();
                }
            }
            else
            {
                Logger.Log($"目录“{modsPath}”不存在!");
            }
            GameScripts.OnAfterLoadMods();
            if (!UI.Load())
            {
                Logger.Error("不能加载UI!");
            }
        }
Пример #2
0
        //private static void ParseGameVersion()
        //{
        //	if (string.IsNullOrEmpty(Config.GameVersionPoint))
        //		return;
        //	try
        //	{
        //		Logger.Log("Start parsing game version.");
        //		if (!Injector.TryParseEntryPoint(Config.GameVersionPoint, out string assembly, out string className, out string methodName, out _))
        //			return;
        //		var asm = Assembly.Load(assembly);
        //		if (asm == null)
        //		{
        //			Logger.Error($"File '{assembly}' not found.");
        //			return;
        //		}

        //		var foundClass = asm.GetType(className);
        //		if (foundClass == null)
        //		{
        //			Logger.Error($"Class '{className}' not found.");
        //			return;
        //		}

        //		var foundMethod = foundClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
        //		if (foundMethod == null)
        //		{
        //			var foundField = foundClass.GetField(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
        //			if (foundField != null)
        //			{
        //				gameVersion = ParseVersion(foundField.GetValue(null).ToString());
        //				Logger.Log($"Game version detected as '{gameVersion}'.");
        //				return;
        //			}

        //			Logger.Error($"Method '{methodName}' not found.");
        //			return;
        //		}

        //		gameVersion = ParseVersion(foundMethod.Invoke(null, null).ToString());
        //		Logger.Log($"Game version detected as '{gameVersion}'.");
        //	}
        //	catch (Exception e)
        //	{
        //		Debug.LogException(e);
        //		OpenUnityFileLog();
        //	}
        //}

        private static void _Start()
        {
            if (!Initialize())
            {
                Logger.Log($"Cancel start due to an error.");
                OpenUnityFileLog();
                return;
            }
            if (started)
            {
                Logger.Log($"Cancel start. Already started.");
                return;
            }

            started = true;

            if (!string.IsNullOrEmpty(Config.GameVersionPoint))
            {
                try
                {
                    Logger.Log($"Start parsing game version.");
                    if (Injector.TryParseEntryPoint(Config.GameVersionPoint, out var assembly, out var className, out var methodName, out _))
                    {
                        var asm = Assembly.Load(assembly);
                        if (asm == null)
                        {
                            Logger.Error($"File '{assembly}' not found.");
                            goto Next;
                        }
                        var foundClass = asm.GetType(className);
                        if (foundClass == null)
                        {
                            Logger.Error($"Class '{className}' not found.");
                            goto Next;
                        }
                        var foundMethod = foundClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                        if (foundMethod == null)
                        {
                            var foundField = foundClass.GetField(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                            if (foundField != null)
                            {
                                gameVersion = ParseVersion(foundField.GetValue(null).ToString());
                                Logger.Log($"Game version detected as '{gameVersion}'.");
                                goto Next;
                            }

                            UnityModManager.Logger.Error($"Method '{methodName}' not found.");
                            goto Next;
                        }

                        gameVersion = ParseVersion(foundMethod.Invoke(null, null).ToString());
                        Logger.Log($"Game version detected as '{gameVersion}'.");
                    }
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    OpenUnityFileLog();
                }
            }

Next:

            GameScripts.Init();

            GameScripts.OnBeforeLoadMods();

            if (Directory.Exists(modsPath))
            {
                Logger.Log($"Parsing mods.");

                Dictionary <string, ModEntry> mods = new Dictionary <string, ModEntry>();

                int countMods = 0;

                foreach (string dir in Directory.GetDirectories(modsPath))
                {
                    string jsonPath = Path.Combine(dir, Config.ModInfo);
                    if (!File.Exists(Path.Combine(dir, Config.ModInfo)))
                    {
                        jsonPath = Path.Combine(dir, Config.ModInfo.ToLower());
                    }
                    if (File.Exists(jsonPath))
                    {
                        countMods++;
                        Logger.Log($"Reading file '{jsonPath}'.");
                        try
                        {
                            //ModInfo modInfo = JsonUtility.FromJson<ModInfo>(File.ReadAllText(jsonPath));
                            ModInfo modInfo = TinyJson.JSONParser.FromJson <ModInfo>(File.ReadAllText(jsonPath));
                            if (string.IsNullOrEmpty(modInfo.Id))
                            {
                                Logger.Error($"Id is null.");
                                continue;
                            }
                            if (mods.ContainsKey(modInfo.Id))
                            {
                                Logger.Error($"Id '{modInfo.Id}' already uses another mod.");
                                continue;
                            }
                            if (string.IsNullOrEmpty(modInfo.AssemblyName))
                            {
                                modInfo.AssemblyName = modInfo.Id + ".dll";
                            }

                            ModEntry modEntry = new ModEntry(modInfo, dir + Path.DirectorySeparatorChar);
                            mods.Add(modInfo.Id, modEntry);
                        }
                        catch (Exception exception)
                        {
                            Logger.Error($"Error parsing file '{jsonPath}'.");
                            Debug.LogException(exception);
                        }
                    }
                    else
                    {
                        //Logger.Log($"File not found '{jsonPath}'.");
                    }
                }

                if (mods.Count > 0)
                {
                    Logger.Log($"Sorting mods.");
                    TopoSort(mods);

                    Params.ReadModParams();

                    Logger.Log($"Loading mods.");
                    foreach (var mod in modEntries)
                    {
                        if (!mod.Enabled)
                        {
                            mod.Logger.Log("To skip (disabled).");
                        }
                        else
                        {
                            mod.Active = true;
                        }
                    }
                }

                Logger.Log($"Finish. Successful loaded {modEntries.Count(x => !x.ErrorOnLoading)}/{countMods} mods.".ToUpper());
                Console.WriteLine();
                Console.WriteLine();
            }

            GameScripts.OnAfterLoadMods();

            if (!UI.Load())
            {
                Logger.Error($"Can't load UI.");
            }
        }