Example #1
0
        private static bool TryGetModpack(StartGameOptions options, [NotNullWhen(true)] out Modpack?modpack)
        {
            if (options.ModpackId.HasValue)
            {
                if (_modpacks.TryGetValue(options.ModpackId.Value, out modpack))
                {
                    return(true);
                }
                else
                {
                    Log.Warning($"Modpack with ID {options.ModpackId.Value} does not exist, no modpack enabled");
                    return(false);
                }
            }

            if (!string.IsNullOrEmpty(options.ModpackName))
            {
                foreach (var pack in Modpacks)
                {
                    if (pack.DisplayName == options.ModpackName)
                    {
                        modpack = pack;
                        return(true);
                    }
                }

                Log.Warning($"Modpack with name {options.ModpackName} does not exist, no modpack enabled");
                modpack = null;
                return(false);
            }

            modpack = null;
            return(false);
        }
Example #2
0
        private static async Task <ObservableDictionary <int, Modpack> > LoadModpacksAsync()
        {
            var result = new ObservableDictionary <int, Modpack>();

            string path = Path.Combine(ApplicationDataDirectory.FullName, "modpacks.json");
            var    file = new FileInfo(path);

            if (!file.Exists)
            {
                Log.Verbose("No mopack file found, skipping import");
                return(result);
            }

            var imported = await Importer.ImportAsync(file, TemporaryDirectory.FullName, false);

            var package = imported.Package; // Disregard extracted files since there aren't any

            if ((package is null) || (package.Mods is null))
            {
                Log.Verbose("No mopacks to import");
                return(result);
            }

            // We have to build a lookup table of mods before we can load any modpacks
            var modMappings = new Dictionary <int, Mod>();

            foreach (var modDef in package.Mods)
            {
                // Usually we'd have to take a whole lot of options into account.
                // However since the package we are loading is created very specifically
                // we can skip most of it and read name and version straight away.
                if (Manager.ContainsMod(modDef.Name, modDef.Version, out Mod? mod))
                {
                    modMappings.Add(modDef.Uid, mod);
                }
            }

            foreach (var modpackDef in package.Modpacks)
            {
                var modpack = new Modpack {
                    DisplayName = modpackDef.Name
                };

                // Instead of throwing an error when a mod or modpack is not found we just ignore it.
                // This way people are free to delete mods from within Factorio or even manually without causing a crash.
                foreach (int modId in modpackDef.ModIds)
                {
                    if (modMappings.TryGetValue(modId, out Mod? mod))
                    {
                        modpack.Add(mod);
                    }
                }
                foreach (int modpackId in modpackDef.ModpackIds)
                {
                    if (result.TryGetValue(modpackId, out var subPack))
                    {
                        modpack.Add(subPack);
                    }
                }

                result.Add(modpackDef.Uid, modpack);
                Log.Verbose($"Successfully loaded modpack '{modpack.DisplayName}' with ID {modpackDef.Uid}");
            }

            return(result);
        }