Esempio n. 1
0
 public static bool HasEntry(this BaseUnityPlugin plugin, string key)
 {
     return(HasEntry(key, TypeLoader.GetMetadata(plugin).GUID));
 }
Esempio n. 2
0
 public static string GetEntry(this BaseUnityPlugin plugin, string key, string defaultValue = "")
 {
     return(GetEntry(key, defaultValue, TypeLoader.GetMetadata(plugin).GUID));
 }
Esempio n. 3
0
 public static void SetEntry(this BaseUnityPlugin plugin, string key, string value)
 {
     SetEntry(key, value, TypeLoader.GetMetadata(plugin).GUID);
 }
Esempio n. 4
0
        /// <summary>
        /// The entry point for BepInEx, called on the very first LoadScene() from UnityEngine.
        /// </summary>
        public static void Initialize()
        {
            if (loaded)
            {
                return;
            }

            if (!Directory.Exists(Common.Utility.PluginsDirectory))
            {
                Directory.CreateDirectory(Utility.PluginsDirectory);
            }

            if (bool.Parse(Config.GetEntry("console", "false")) || bool.Parse(Config.GetEntry("console-shiftjis", "false")))
            {
                try
                {
                    UnityInjector.ConsoleUtil.ConsoleWindow.Attach();

                    if (bool.Parse(Config.GetEntry("console-shiftjis", "false")))
                    {
                        UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
                    }
                }
                catch
                {
                    BepInLogger.Log("Failed to allocate console!", true);
                }
            }

            try
            {
                BepInLogger.Log($"BepInEx {Assembly.GetExecutingAssembly().GetName().Version}");
                BepInLogger.Log("Chainloader started");

                UnityEngine.Object.DontDestroyOnLoad(ManagerObject);


                string currentProcess = Process.GetCurrentProcess().ProcessName.ToLower();

                var pluginTypes = TypeLoader.LoadTypes <BaseUnityPlugin>(Utility.PluginsDirectory)
                                  .Where(plugin =>
                {
                    //Perform a filter for currently running process
                    var filters = TypeLoader.GetAttributes <BepInProcess>(plugin);

                    if (!filters.Any())
                    {
                        return(true);
                    }

                    return(filters.Any(x => x.ProcessName.ToLower().Replace(".exe", "") == currentProcess));
                })
                                  .ToList();

                BepInLogger.Log($"{pluginTypes.Count} plugins selected");

                Dictionary <Type, IEnumerable <Type> > dependencyDict = new Dictionary <Type, IEnumerable <Type> >();

                foreach (Type t in pluginTypes)
                {
                    try
                    {
                        IEnumerable <Type> dependencies = TypeLoader.GetDependencies(t, pluginTypes);

                        dependencyDict[t] = dependencies;
                    }
                    catch (MissingDependencyException)
                    {
                        var metadata = TypeLoader.GetMetadata(t);

                        BepInLogger.Log($"Cannot load [{metadata.Name}] due to missing dependencies.");
                    }
                }

                pluginTypes = TopologicalSort(dependencyDict.Keys, x => dependencyDict[x]).ToList();

                foreach (Type t in pluginTypes)
                {
                    try
                    {
                        var metadata = TypeLoader.GetMetadata(t);

                        var plugin = (BaseUnityPlugin)ManagerObject.AddComponent(t);

                        Plugins.Add(plugin);
                        BepInLogger.Log($"Loaded [{metadata.Name} {metadata.Version}]");
                    }
                    catch (Exception ex)
                    {
                        BepInLogger.Log($"Error loading [{t.Name}] : {ex.Message}");
                    }
                }
            }
            catch (Exception ex)
            {
                UnityInjector.ConsoleUtil.ConsoleWindow.Attach();

                Console.WriteLine("Error occurred starting the game");
                Console.WriteLine(ex.ToString());
            }

            loaded = true;
        }
 public static void HandleReflectionTypeLoad(ref Exception __exception, ref Type[] __result)
 {
     if (__exception == null)
     {
         return;
     }
     if (__exception is ReflectionTypeLoadException re)
     {
         __exception = null;
         __result    = re.Types.Where(t => t != null).ToArray();
         UnityEngine.Debug.Log($"Encountered ReflectionTypeLoadException which was suppressed. Full error: \n${TypeLoader.TypeLoadExceptionToString(re)}");
     }
 }
Esempio n. 6
0
 public ConfigWrapper(string key, BaseUnityPlugin plugin, IConfigConverter <T> converter, T @default = default(T))
     : this(key, converter.ConvertFromString, converter.ConvertToString, @default)
 {
     Section = TypeLoader.GetMetadata(plugin).GUID;
 }
Esempio n. 7
0
 public ConfigWrapper(string key, BaseUnityPlugin plugin, Func <string, T> strToObj, Func <T, string> objToStr, T @default = default(T))
     : this(key, strToObj, objToStr, @default)
 {
     Section = TypeLoader.GetMetadata(plugin).GUID;
 }
Esempio n. 8
0
 public ConfigWrapper(string key, BaseUnityPlugin plugin, T @default = default(T))
     : this(key, @default)
 {
     Section = TypeLoader.GetMetadata(plugin).GUID;
 }