示例#1
0
 internal void ReloadButton()
 {
     if (gadgetIndex >= 0)
     {
         GadgetCoreAPI.DisplayYesNoDialog("Are you sure you want to perform a Gadget reload?\nThe following Gadgets will be reloaded:\n\n" + modEntries[modIndex].Gadgets[gadgetIndex].Dependents.Union(Gadgets.LoadOrderTree.Find(modEntries[modIndex].Gadgets[gadgetIndex]).FlattenUniqueByBreadth()).Select(x => x.Attribute.Name).Concat("\n"), () => {
             Gadgets.ReloadGadget(modEntries[modIndex].Gadgets[gadgetIndex]);
         });
     }
     else
     {
         if (modEntries[modIndex].Type == ModMenuEntryType.GADGET)
         {
             GadgetCoreAPI.DisplayYesNoDialog("<color=red>WARNING: This feature does not work! Unexpected consequences may occur from attempting to use it!</color>\n\nAre you sure you want to perform a mod reload?\nThe following mods will be reloaded:\n\n" + modEntries[modIndex].Gadgets.SelectMany(x => Gadgets.LoadOrderTree.Find(x).FlattenUniqueByBreadth()).Where(x => x != null).Select(x => x.Mod).Distinct().Select(x => x.Name).Concat("\n"), () => {
                 GadgetLoader.ReloadMod(modEntries[modIndex].Gadgets[0].Mod);
             });
         }
         else
         {
             GadgetCoreAPI.DisplayYesNoDialog("UMF mods can only have their configs reloaded at runtime.\nWould you like to reload " + modEntries[modIndex].Name + "'s config?", () => {
                 GadgetCore.UMFAPI.SendCommand("cfgReload " + modEntries[modIndex].Name);
             });
         }
     }
     Rebuild();
     UpdateRestartNeeded();
 }
示例#2
0
 private static bool SetEnabledInternal(GadgetInfo gadget, bool enabled)
 {
     try
     {
         if (gadget.Gadget.Enabled == enabled)
         {
             return(false);
         }
         bool wasBatchLoading = GadgetLoader.BatchLoading;
         GadgetLoader.BatchLoading = true;
         if (enabled)
         {
             if (gadget.Attribute.AllowRuntimeReloading)
             {
                 EnableDependencies(gadget);
                 RefreshAfters(gadget);
             }
         }
         else
         {
             DisableDependents(gadget);
         }
         GadgetCoreConfig.enabledGadgets[gadget.Attribute.Name] = enabled;
         if (gadget.Attribute.AllowRuntimeReloading || !enabled)
         {
             gadget.Gadget.Enabled = enabled;
         }
         GadgetLoader.BatchLoading = wasBatchLoading;
         GadgetLoader.QueuedGadgets.Add(gadget);
         if (!GadgetLoader.BatchLoading)
         {
             if (enabled)
             {
                 if (gadget.Attribute.AllowRuntimeReloading)
                 {
                     GadgetLoader.EnableQueuedGadgets();
                 }
             }
             else
             {
                 GadgetLoader.DisableQueuedGadgets();
             }
             GadgetCoreConfig.Update();
         }
         return(true);
     }
     catch (Exception e)
     {
         GadgetLoader.Logger.LogError("Error " + (enabled ? "enabling" : "disabling") + " Gadget `" + gadget.Attribute.Name + "`: " + e);
         return(false);
     }
 }
示例#3
0
        internal static void Initialize()
        {
            if (Initialized)
            {
                return;
            }
            Initialized = true;
            try
            {
                CoreLogger = new GadgetLogger("GadgetCore", "Core");
                CoreLogger.Log("GadgetCore v" + GadgetCoreAPI.FULL_VERSION + " Initializing!");
                Debug.Log("GadgetCore v" + GadgetCoreAPI.FULL_VERSION);
            }
            catch (Exception e)
            {
                Debug.Log(e);
                GadgetCoreAPI.Quit();
                return;
            }
            try
            {
                if (File.Exists(Application.persistentDataPath + "/PlayerPrefs.txt") && VerifySaveFile())
                {
                    if (GadgetCoreConfig.MaxBackups > 0)
                    {
                        File.Copy(Application.persistentDataPath + "/PlayerPrefs.txt", Path.Combine(GadgetPaths.SaveBackupsPath, "Save Backup - " + DateTime.Now.ToString("yyyy-dd-M_HH-mm-ss") + ".txt"));
                        FileInfo[] backups = new DirectoryInfo(GadgetPaths.SaveBackupsPath).GetFiles().OrderByDescending(x => x.LastWriteTime.Year <= 1601 ? x.CreationTime : x.LastWriteTime).ToArray();
                        if (backups.Length > GadgetCoreConfig.MaxBackups)
                        {
                            for (int i = GadgetCoreConfig.MaxBackups; i < backups.Length; i++)
                            {
                                backups[i].Delete();
                            }
                        }
                    }
                }
                else
                {
                    GadgetCoreAPI.Quit();
                    return;
                }
                HarmonyInstance = new Harmony("GadgetCore.core");
                Type[] types;
                try
                {
                    types = Assembly.GetExecutingAssembly().GetTypes();
                }
                catch (ReflectionTypeLoadException e)
                {
                    types = e.Types.Where(t => t != null).ToArray();
                }
                types.Do(delegate(Type type)
                {
                    object[] attributes = type.GetCustomAttributes(true);
                    if (!attributes.Any(x => x.GetType() == typeof(HarmonyGadgetAttribute)))
                    {
                        HarmonyInstance.CreateClassProcessor(type).Patch();
                    }
                });
                new Thread(new ThreadStart(() => {
                    Thread.CurrentThread.Name         = "GadgetCore Unity Engine Log Cloner";
                    Thread.CurrentThread.Priority     = System.Threading.ThreadPriority.Lowest;
                    Thread.CurrentThread.IsBackground = true;
                    string logPath = Application.dataPath + "\\output_log.txt";
                    if (!File.Exists(logPath))
                    {
                        logPath = Application.persistentDataPath + "\\output_log.txt";
                    }
                    if (!File.Exists(logPath))
                    {
                        logPath = "~/Library/Logs/Unity/Player.log";
                    }
                    if (!File.Exists(logPath))
                    {
                        logPath = "~/.config/unity3d/DefaultCompany/Roguelands/Player.log";
                    }
                    if (!File.Exists(logPath))
                    {
                        CoreLogger.LogWarning("Unable to find Unity log file!");
                        return;
                    }
                    string targetPath = Path.Combine(GadgetPaths.LogsPath, "Unity Output.log");
                    DateTime t        = default;
                    while (!Quitting)
                    {
                        if (File.Exists(logPath) && File.GetLastWriteTime(logPath) > t)
                        {
                            File.Copy(logPath, targetPath, true);
                            t = DateTime.Now;
                        }
                        Thread.Sleep(100);
                    }
                })).Start();
                AppDomain.CurrentDomain.AssemblyResolve += (object sender, ResolveEventArgs args) =>
                {
                    string name = new AssemblyName(args.Name).Name;
                    if (LoadedAssemblies.ContainsKey(name))
                    {
                        return(LoadedAssemblies[name]);
                    }
                    foreach (string file in Directory.GetFiles(GadgetPaths.LibsPath))
                    {
                        if (AssemblyName.GetAssemblyName(file).Name == name)
                        {
                            Assembly assembly = Assembly.LoadFrom(file);
                            LoadedAssemblies[name] = assembly;
                            return(assembly);
                        }
                    }
                    return(null);
                };
                AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += (object sender, ResolveEventArgs args) =>
                {
                    string name = new AssemblyName(args.Name).Name;
                    if (LoadedAssemblies.ContainsKey("ReflectionOnly: " + name))
                    {
                        return(LoadedAssemblies["ReflectionOnly: " + name]);
                    }
                    foreach (string file in Directory.GetFiles(GadgetPaths.LibsPath))
                    {
                        if (AssemblyName.GetAssemblyName(file).Name == name)
                        {
                            Assembly assembly = Assembly.ReflectionOnlyLoadFrom(file);
                            LoadedAssemblies["ReflectionOnly: " + name] = assembly;
                            return(assembly);
                        }
                    }
                    return(null);
                };
                LoadMainMenu();
                try
                {
                    UMFAPI = new UMFAPI();
                    UMFAPI.GetModNames();
                    CoreLogger.Log("Enabling UMF API as UMF is installed.");
                }
                catch (Exception)
                {
                    UMFAPI = null;
                    CoreLogger.Log("Disabling UMF API as UMF is not installed.");
                }
                CoreLib = Activator.CreateInstance(Assembly.LoadFile(Path.Combine(Path.Combine(GadgetPaths.GadgetCorePath, "DependentLibs"), "GadgetCoreLib.dll")).GetTypes().First(x => typeof(IGadgetCoreLib).IsAssignableFrom(x))) as IGadgetCoreLib;
                CoreLib.ProvideLogger(CoreLogger);
                GadgetCoreConfig.Load();
                CoreLogger.Log("Finished loading config.");
                RegisterKeys();
                IniData coreManifest = new IniData();
                coreManifest["Metadata"]["Name"]     = "GadgetCore";
                coreManifest["Metadata"]["Assembly"] = Path.Combine(GadgetPaths.ManagedPath, "GadgetCore.dll");
                GadgetMod coreMod = new GadgetMod(GadgetPaths.GadgetCorePath, coreManifest, Assembly.GetExecutingAssembly());
                GadgetMods.RegisterMod(coreMod);
                VanillaRegistration();
                SceneManager.sceneLoaded += OnSceneLoaded;
                SceneInjector.InjectMainMenu();
                GadgetLoader.LoadAllMods();
                DontDestroyOnLoad(new GameObject("GadgetCore", typeof(GadgetCore)));
                CoreLogger.LogConsole("GadgetCore v" + GadgetCoreAPI.FULL_VERSION + " Initialized!");
#pragma warning disable CS0162 // Unreachable code detected
                if (GadgetCoreAPI.IS_BETA)
                {
                    CoreLogger.LogWarning("You are currently running a beta version of GadgetCore! Be prepared for bugs!");
                }
#pragma warning restore CS0162 // Unreachable code detected
            }
            catch (Exception e)
            {
                CoreLogger.LogError("There was a fatal error loading GadgetCore: " + e);
            }
        }