Example #1
0
        // This function is run before all mods are finished loading.
        protected override void OnPreInitialize()
        {
            DataTransferManager.onMessageCompleted += recieveData;



            config = configuration;
            Injections.InjectMethods();

            base.OnPreInitialize();
        }
        private static void ResultFetched(object value0, WorkshopQueryResult result)
        {
            if (result == null || result.details == null)
            {
                return;
            }
            WorkshopItem publishedFile = result.details.publishedFile;

            if (publishedFile == null)
            {
                return;
            }
            try
            {
                if ((publishedFile.stateFlags & WorkshopItemState.Installed) == WorkshopItemState.None || !Directory.Exists(publishedFile.path))
                {
                    return;
                }
                foreach (string folder in DuckFile.GetDirectoriesNoCloud(publishedFile.path))
                {
                    ModConfiguration modConfiguration = ModLoader.AttemptModLoad(folder);
                    if (modConfiguration != null)
                    {
                        try
                        {
                            modConfiguration.isWorkshop = true;
                            ModLoader.loadableMods.Add(modConfiguration.uniqueID, modConfiguration);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
        private static ModConfiguration AttemptModLoad(string folder)
        {
            ModConfiguration modConfiguration = new ModConfiguration()
            {
                directory = folder
            };

            modConfiguration.contentDirectory = modConfiguration.directory + "/content/";
            modConfiguration.name             = Path.GetFileNameWithoutExtension(folder);
            modConfiguration.content          = new ContentPack(modConfiguration);
            try
            {
                if (modConfiguration.name == "DuckGame")
                {
                    return((ModConfiguration)null);
                }
                ModLoader.currentModLoadString = modConfiguration.name;
                MonoMain.loadMessage           = "COMPILING MOD " + ModLoader.currentModLoadString;
                modConfiguration.LoadConfiguration();
                if (!modConfiguration.disabled)
                {
                    if (!File.Exists(modConfiguration.assemblyPath) && !ModLoader.AttemptCompile(modConfiguration))
                    {
                        return((ModConfiguration)null);
                    }
                    ++ModLoader._numModsEnabled;
                }
                ++ModLoader._numModsTotal;
                return(modConfiguration);
            }
            catch (Exception ex)
            {
                ModLoader._modLoadErrors.Add(Tuple.Create <string, Exception>(modConfiguration.uniqueID, ex));
            }
            return((ModConfiguration)null);
        }
        internal static void LoadMods(string dir)
        {
            ModLoader.modDirectory = dir;
            ModLoader.LoadConfig();
            Stack <string> modLoadStack = new Stack <string>();

            ModLoader.loadableMods = new Dictionary <string, ModConfiguration>();
            if (Directory.Exists(ModLoader.modDirectory))
            {
                if (Steam.IsInitialized())
                {
                    bool done = false;
                    WorkshopQueryUser queryUser = Steam.CreateQueryUser(Steam.user.id, WorkshopList.Subscribed, WorkshopType.UsableInGame, WorkshopSortOrder.TitleAsc);
                    queryUser.requiredTags.Add("Mod");
                    queryUser.onlyQueryIDs   = true;
                    queryUser.QueryFinished += (WorkshopQueryFinished)(sender => done = true);
                    queryUser.ResultFetched += new WorkshopQueryResultFetched(ModLoader.ResultFetched);
                    queryUser.Request();
                    while (!done)
                    {
                        Steam.Update();
                        Thread.Sleep(13);
                    }
                }
                List <string> directoriesNoCloud = DuckFile.GetDirectoriesNoCloud(ModLoader.modDirectory);
                MonoMain.totalLoadyBits += directoriesNoCloud.Count <string>() * 2;
                foreach (string folder in directoriesNoCloud)
                {
                    ModConfiguration modConfiguration = ModLoader.AttemptModLoad(folder);
                    MonoMain.loadyBits += 2;
                    if (modConfiguration != null)
                    {
                        if (ModLoader.loadableMods.ContainsKey(modConfiguration.uniqueID))
                        {
                            if (ModLoader.loadableMods[modConfiguration.uniqueID].disabled && !modConfiguration.disabled)
                            {
                                ModLoader.loadableMods.Remove(modConfiguration.uniqueID);
                            }
                            else
                            {
                                continue;
                            }
                        }
                        ModLoader.loadableMods.Add(modConfiguration.uniqueID, modConfiguration);
                    }
                }
            }
            MonoMain.totalLoadyBits += ModLoader.loadableMods.Count * 2;
            int num = 0;

            foreach (ModConfiguration modConfig in ModLoader.loadableMods.Values)
            {
                try
                {
                    ModLoader.currentModLoadString = modConfig.name;
                    MonoMain.loadMessage           = "LOADING MOD " + ModLoader.currentModLoadString;
                    ModLoader.GetOrLoad(modConfig, ref modLoadStack, ref ModLoader.loadableMods);
                    MonoMain.loadyBits += 2;
                    ++num;
                    if (num == 10)
                    {
                        num = 0;
                        Thread.Sleep(50);
                    }
                }
                catch (Exception ex)
                {
                    ModLoader._modLoadErrors.Add(Tuple.Create <string, Exception>(modConfig.uniqueID, ex));
                    MonoMain.loadyBits += 2;
                }
            }
            ModLoader._sortedMods           = (IList <Mod>)ModLoader._loadedMods.Values.OrderBy <Mod, Priority>((Func <Mod, Priority>)(mod => mod.priority)).ToList <Mod>();
            ModLoader._sortedAccessibleMods = (IList <Mod>)ModLoader._sortedMods.Where <Mod>((Func <Mod, bool>)(mod => !mod.configuration.disabled)).ToList <Mod>();
            foreach (Mod mod in ModLoader._sortedMods.Where <Mod>((Func <Mod, bool>)(a => a.configuration.disabled)))
            {
                if (mod != null && mod.configuration != null)
                {
                    ModLoader._loadedMods.Remove(mod.configuration.uniqueID);
                }
            }
            foreach (Mod sortedAccessibleMod in (IEnumerable <Mod>)ModLoader._sortedAccessibleMods)
            {
                sortedAccessibleMod.InvokeOnPreInitialize();
            }
            ModLoader.modHash = ModLoader.GetModHash();
            foreach (Mod sortedAccessibleMod in (IEnumerable <Mod>)ModLoader._sortedAccessibleMods)
            {
                foreach (System.Type type in sortedAccessibleMod.configuration.assembly.GetTypes())
                {
                    ModLoader._typesByName[ModLoader.SmallTypeName(type)] = type;
                }
            }
        }
        private static bool AttemptCompile(ModConfiguration config)
        {
            if (config.noCompilation)
            {
                return(false);
            }
            List <string> filesNoCloud = DuckFile.GetFilesNoCloud(config.directory, "*.cs", SearchOption.AllDirectories);

            if (filesNoCloud.Count == 0)
            {
                return(false);
            }
            config.isDynamic = true;
            CRC32 crC32 = new CRC32();

            byte[] numArray = new byte[2048];
            foreach (string path in filesNoCloud)
            {
                using (FileStream fileStream = File.Open(path, FileMode.Open))
                {
                    while (fileStream.Position != fileStream.Length)
                    {
                        int blockLen = fileStream.Read(numArray, 0, numArray.Length);
                        crC32.ProcessBlock(numArray, blockLen);
                    }
                }
            }
            uint num = crC32.Finalize();

            if (!ModLoader.forceRecompilation && File.Exists(config.hashPath))
            {
                if (File.Exists(config.tempAssemblyPath))
                {
                    try
                    {
                        if ((int)BitConverter.ToUInt32(File.ReadAllBytes(config.hashPath), 0) == (int)num)
                        {
                            return(true);
                        }
                    }
                    catch
                    {
                    }
                }
            }
            File.WriteAllBytes(config.hashPath, BitConverter.GetBytes(num));
            if (ModLoader._provider == null)
            {
                ModLoader._provider   = new CSharpCodeProvider();
                ModLoader._parameters = new CompilerParameters(((IEnumerable <Assembly>)AppDomain.CurrentDomain.GetAssemblies()).Select <Assembly, string>((Func <Assembly, string>)(assembly => assembly.Location)).ToArray <string>());
                ModLoader._parameters.GenerateExecutable = ModLoader._parameters.GenerateInMemory = false;
            }
            if (File.Exists(config.buildLogPath))
            {
                File.SetAttributes(config.buildLogPath, FileAttributes.Normal);
                File.Delete(config.buildLogPath);
            }
            ModLoader._parameters.OutputAssembly = config.tempAssemblyPath;
            CompilerResults compilerResults = ModLoader._provider.CompileAssemblyFromFile(ModLoader._parameters, filesNoCloud.ToArray());

            if (compilerResults.Errors.Count == 0)
            {
                return(true);
            }
            File.WriteAllLines(config.buildLogPath, compilerResults.Output.OfType <string>());
            return(false);
        }
 private static Mod GetOrLoad(
     ModConfiguration modConfig,
     ref Stack <string> modLoadStack,
     ref Dictionary <string, ModConfiguration> loadableMods)
 {
     if (modLoadStack.Contains(modConfig.uniqueID))
     {
         throw new ModCircularDependencyException(modLoadStack);
     }
     modLoadStack.Push(modConfig.uniqueID);
     try
     {
         Mod mod1;
         if (ModLoader._loadedMods.TryGetValue(modConfig.uniqueID, out mod1))
         {
             return(mod1);
         }
         Mod mod2;
         if (modConfig.disabled)
         {
             mod2 = (Mod) new DisabledMod();
         }
         else
         {
             foreach (string hardDependency in modConfig.hardDependencies)
             {
                 ModConfiguration modConfig1;
                 if (!loadableMods.TryGetValue(hardDependency, out modConfig1))
                 {
                     throw new ModDependencyNotFoundException(modConfig.uniqueID, hardDependency);
                 }
                 if (modConfig1.disabled)
                 {
                     throw new ModDependencyNotFoundException(modConfig.uniqueID, hardDependency);
                 }
                 ModLoader.GetOrLoad(modConfig1, ref modLoadStack, ref loadableMods);
             }
             foreach (string softDependency in modConfig.softDependencies)
             {
                 ModConfiguration modConfig1;
                 if (loadableMods.TryGetValue(softDependency, out modConfig1) && !modConfig1.disabled)
                 {
                     ModLoader.GetOrLoad(modConfig1, ref modLoadStack, ref loadableMods);
                 }
             }
             modConfig.assembly = Assembly.Load(File.ReadAllBytes(modConfig.isDynamic ? modConfig.tempAssemblyPath : modConfig.assemblyPath));
             System.Type[] array1 = ((IEnumerable <System.Type>)modConfig.assembly.GetExportedTypes()).Where <System.Type>((Func <System.Type, bool>)(type => type.IsSubclassOf(typeof(IManageContent)) && type.IsPublic && type.IsClass && !type.IsAbstract)).ToArray <System.Type>();
             modConfig.contentManager = array1.Length <= 1 ? ContentManagers.GetContentManager(array1.Length == 1 ? array1[0] : (System.Type)null) : throw new ModTypeMissingException(modConfig.uniqueID + " has more than one content manager class");
             System.Type[] array2 = ((IEnumerable <System.Type>)modConfig.assembly.GetExportedTypes()).Where <System.Type>((Func <System.Type, bool>)(type => type.IsSubclassOf(typeof(Mod)) && !type.IsAbstract)).ToArray <System.Type>();
             if (array2.Length != 1)
             {
                 throw new ModTypeMissingException(modConfig.uniqueID + " is missing or has more than one Mod subclass");
             }
             if (MonoMain.preloadModContent && modConfig.preloadContent)
             {
                 modConfig.content.PreloadContent();
             }
             else
             {
                 modConfig.content.PreloadContentPaths();
             }
             mod2 = (Mod)Activator.CreateInstance(array2[0]);
         }
         mod2.configuration = modConfig;
         ModLoader.AddMod(mod2);
         return(mod2);
     }
     finally
     {
         modLoadStack.Pop();
     }
 }
 public ContentPack(ModConfiguration modConfiguration) => this._modConfig = modConfiguration;