Esempio n. 1
0
        public static Modpack CreateModpack()
        {
            var id      = GetNextModpackId();
            var name    = (App.Current?.Locales?.GetResource("DefaultModpackName") as string) ?? "New Modpack";
            var modpack = new Modpack {
                DisplayName = name
            };

            _modpacks.Add(id, modpack);
            return(modpack);
        }
Esempio n. 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);
        }