示例#1
0
 public static void LoadConfig()
 {
     try
     {
         if (firstLoad)
         {
             firstLoad  = false;
             IsUnpacked = File.Exists(Path.Combine(UMFData.ModsPath, "GadgetCore.dll")) && File.Exists(Path.Combine(Path.Combine(UMFData.ModsPath, "DependentLibs"), "GadgetCoreLib.dll")) && !(Directory.GetFiles(UMFData.ModsPath, "GadgetCore*.zip").Length > 0);
             LoadMainMenu();
             if (IsUnpacked)
             {
                 CoreLib = Activator.CreateInstance(Assembly.LoadFile(Path.Combine(Path.Combine(UMFData.ModsPath, "DependentLibs"), "GadgetCoreLib.dll")).GetTypes().First(x => typeof(IGadgetCoreLib).IsAssignableFrom(x))) as IGadgetCoreLib;
                 CoreLib.ProvideLogger(Logger);
                 //CoreLib.InitializeSteamAPI();
                 LoadModAssemblies();
                 GadgetCoreConfig.Load();
                 InitializeRegistries();
                 GadgetCoreConfig.LoadRegistries();
                 VanillaRegistration();
                 InitializeMods();
             }
             SceneInjector.InjectMainMenu();
             SceneManager.sceneLoaded += OnSceneLoaded;
         }
         else if (IsUnpacked)
         {
             GadgetCoreConfig.Load();
         }
     }
     catch (Exception e)
     {
         Log("Exception during GadgetCore load: " + e);
         throw;
     }
 }
示例#2
0
 private static void InitializeMods()
 {
     Log("Beginning Gadget Mod Initialization");
     foreach (GadgetModInfo mod in GadgetMods.ListAllModInfos())
     {
         if (!mod.Mod.Enabled)
         {
             continue;
         }
         if (mod.Attribute.Dependencies.All(x => GadgetMods.ListAllModInfos().Where(y => y.Mod.Enabled).Select(y => y.Attribute.Name).Contains(x) || GadgetMods.ListAllModInfos().Where(y => y.Mod.Enabled).Select(y => y.Mod.GetPreviousModNames()).Any(y => y.Contains(x))))
         {
             bool            compatible        = true;
             string[][]      splitDependencies = mod.Attribute.Dependencies.Select(x => x.Split(' ')).Where(x => x.Length == 2).ToArray();
             GadgetModInfo[] dependencies      = splitDependencies.Select(x => GadgetMods.ListAllModInfos().Where(y => y.Mod.Enabled).FirstOrDefault(y => y.Attribute.Name.Equals(x[0])) ?? GadgetMods.ListAllModInfos().Where(y => y.Mod.Enabled).First(y => y.Mod.GetPreviousModNames().Contains(x[0]))).ToArray();
             for (int i = 0; i < dependencies.Length; i++)
             {
                 int[] currentVersionNums = dependencies[i].Mod.GetModVersionString().Split('.').Select(x => int.Parse(x)).ToArray();
                 int[] targetVersionNums  = splitDependencies[i][1].TrimStart('v').Split('.').Select(x => int.Parse(x)).ToArray();
                 VersionSpecificity versionSpecificity = (VersionSpecificity)targetVersionNums.Length;
                 if (!((versionSpecificity == VersionSpecificity.MAJOR && currentVersionNums[0] == targetVersionNums[0] && (currentVersionNums[1] > targetVersionNums[1] || (currentVersionNums[1] == targetVersionNums[1] && (currentVersionNums[2] > targetVersionNums[2] || (currentVersionNums[2] == targetVersionNums[2] && currentVersionNums[3] >= targetVersionNums[3]))))) ||
                       (versionSpecificity == VersionSpecificity.MINOR && currentVersionNums[0] == targetVersionNums[0] && currentVersionNums[1] == targetVersionNums[1] && (currentVersionNums[2] > targetVersionNums[2] || (currentVersionNums[2] == targetVersionNums[2] && currentVersionNums[3] >= targetVersionNums[3]))) ||
                       (versionSpecificity == VersionSpecificity.NONBREAKING && currentVersionNums[0] == targetVersionNums[0] && currentVersionNums[1] == targetVersionNums[1] && currentVersionNums[2] == targetVersionNums[2] && currentVersionNums[3] >= targetVersionNums[3]) ||
                       (versionSpecificity == VersionSpecificity.BUGFIX && currentVersionNums[0] == targetVersionNums[0] && currentVersionNums[1] == targetVersionNums[1] && currentVersionNums[2] == targetVersionNums[2] && currentVersionNums[3] == targetVersionNums[3])))
                 {
                     compatible = false;
                     break;
                 }
             }
             if (compatible)
             {
                 Log("Initializing Gadget Mod '" + mod.Attribute.Name + "'");
                 try
                 {
                     Registry.modRegistering = mod.Mod.ModID;
                     mod.Mod.Initialize();
                 }
                 catch (Exception e)
                 {
                     Log("Exception Loading Gadget Mod '" + mod.Attribute.Name + "':" + Environment.NewLine + e.ToString());
                 }
                 finally
                 {
                     Registry.modRegistering = -1;
                 }
             }
             else
             {
                 GadgetMods.SetEnabled(mod, false);
                 Log("Aborted loading Gadget Mod '" + mod.Attribute.Name + "' because although all required dependencies are available, they are of incompatible versions. " + mod.Attribute.Name + "'s dependencies are as follows: {" + mod.Attribute.Dependencies.Aggregate(string.Empty, (x, y) => x + ", " + y) + "}");
             }
         }
         else
         {
             GadgetMods.SetEnabled(mod, false);
             Log("Aborted loading Gadget Mod '" + mod.Attribute.Name + "' because it is missing required dependencies. " + mod.Attribute.Name + "'s dependencies are as follows: {" + mod.Attribute.Dependencies.Aggregate(string.Empty, (x, y) => x + ", " + y) + "}");
         }
     }
     GadgetCoreConfig.Update();
     Log("Gadget Mod Initialization Complete");
 }
示例#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);
            }
        }