Пример #1
0
        public void ApplyProfile(ModProfile prof, bool disableAllMods = true)
        {
            if (prof == null)
            {
                return;
            }
            if (disableAllMods)
            {
                foreach (KeyValuePair <string, Mod> rawEntry in ModLookup)
                {
                    ModLookup[rawEntry.Key].Enabled = false;
                }
            }

            foreach (KeyValuePair <string, Mod> entry in prof.ProfileData)
            {
                if (ModLookup.ContainsKey(entry.Key))
                {
                    ModLookup[entry.Key].Enabled = entry.Value.Enabled;
                    if (entry.Value.InstalledVersion != null)
                    {
                        ModLookup[entry.Key].InstalledVersion = entry.Value.InstalledVersion;
                    }
                    ModLookup[entry.Key].Priority    = entry.Value.Priority;
                    ModLookup[entry.Key].IsOptional  = entry.Value.IsOptional;
                    ModLookup[entry.Key].ForceLatest = entry.Value.ForceLatest;
                    if (entry.Value.ForceLatest || !ModLookup[entry.Key].AllModData.ContainsKey(ModLookup[entry.Key].InstalledVersion))
                    {
                        ModLookup[entry.Key].InstalledVersion = ModLookup[entry.Key].AvailableVersions[0];
                    }
                }
            }
        }
Пример #2
0
        public ModProfile GenerateProfile()
        {
            var res = new ModProfile(new Dictionary <string, Mod>());

            foreach (Mod mod in Mods)
            {
                res.ProfileData.Add(mod.CurrentModData.ModID, (Mod)mod.Clone());
            }
            return(res);
        }
        private void ForceExportProfile()
        {
            if (listBox1.SelectedValue == null || SelectedProfile == null)
            {
                this.ShowBasicButton("Please select a profile to export it as a .zip file.", "OK", null, null);
                return;
            }

            var dialog = new SaveFileDialog();

            dialog.Filter           = "ZIP files (*.zip)|*.zip|All files (*.*)|*.*";
            dialog.Title            = "Export a profile";
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string targetFolderPath = Path.Combine(Path.GetTempPath(), "AstroModLoader", "export");
                Directory.CreateDirectory(targetFolderPath);

                ModProfile creatingProfile = new ModProfile();
                creatingProfile.ProfileData = new Dictionary <string, Mod>();
                creatingProfile.Name        = listBox1.SelectedValue as string;
                creatingProfile.Info        = "Exported by " + AMLUtils.UserAgent + " at " + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffK");

                List <KeyValuePair <string, Mod> > plannedOrdering = new List <KeyValuePair <string, Mod> >();
                foreach (KeyValuePair <string, Mod> entry in SelectedProfile.ProfileData)
                {
                    if (entry.Value.Enabled)
                    {
                        plannedOrdering.Add(entry);
                    }
                }
                plannedOrdering = new List <KeyValuePair <string, Mod> >(plannedOrdering.OrderBy(o => o.Value.Priority).ToList());

                for (int i = 0; i < plannedOrdering.Count; i++)
                {
                    plannedOrdering[i].Value.Priority = i + 1;
                    creatingProfile.ProfileData[plannedOrdering[i].Key] = plannedOrdering[i].Value;

                    // Copy mod pak to the zip as well
                    string onePathOnDisk = OurParentForm.ModManager.GetPathOnDisk(plannedOrdering[i].Value, plannedOrdering[i].Key);
                    if (!string.IsNullOrEmpty(onePathOnDisk))
                    {
                        File.Copy(onePathOnDisk, Path.Combine(targetFolderPath, Path.GetFileName(onePathOnDisk)));
                    }
                }

                File.WriteAllBytes(Path.Combine(targetFolderPath, "profile1.json"), Encoding.UTF8.GetBytes(AMLUtils.SerializeObject(creatingProfile)));

                ZipFile.CreateFromDirectory(targetFolderPath, dialog.FileName);
                Directory.Delete(targetFolderPath, true);

                RefreshBox();
                statusLabel.Text = "Successfully exported profile.";
            }
        }
        private void ForceRefreshSelectedProfile()
        {
            string kosherKey = listBox1.SelectedValue as string;

            if (string.IsNullOrEmpty(kosherKey) || !OurParentForm.ModManager.ProfileList.ContainsKey(kosherKey))
            {
                SelectedProfile = null;
                return;
            }
            SelectedProfile = OurParentForm.ModManager.ProfileList[kosherKey];
        }
 private void cancelButton_Click(object sender, EventArgs e)
 {
     SelectedProfile   = null;
     this.DialogResult = DialogResult.OK;
     this.Close();
 }
Пример #6
0
        private List <Mod> InstallModFromPath(string newInstallingMod, out int numClientOnly, out int numMalformatted, out int numNewProfiles)
        {
            numClientOnly   = 0;
            numMalformatted = 0;
            numNewProfiles  = 0;
            string ext = Path.GetExtension(newInstallingMod);

            if (!AllowedModExtensions.Contains(ext))
            {
                return(null);
            }

            List <string> newPaths = new List <string>();

            if (ext == ".zip")                                                                                                                    // If the mod we are trying to install is a zip, we go through and copy each pak file inside that zip
            {
                string targetFolderPath = Path.Combine(Path.GetTempPath(), "AstroModLoader", Path.GetFileNameWithoutExtension(newInstallingMod)); // Extract the zip file to the temporary data folder
                ZipFile.ExtractToDirectory(newInstallingMod, targetFolderPath);

                string[] allAccessiblePaks = Directory.GetFiles(targetFolderPath, "*.pak", SearchOption.AllDirectories); // Get all pak files that exist in the zip file
                foreach (string zippedPakPath in allAccessiblePaks)
                {
                    string newPath = AddModFromPakPath(zippedPakPath, out bool wasMalformatted);
                    if (wasMalformatted)
                    {
                        numMalformatted++;
                    }
                    if (newPath != null)
                    {
                        newPaths.Add(newPath);
                    }
                }

                // Any .json files included will be treated as mod profiles to add to our current list
                string[] allAccessibleJsonFiles = Directory.GetFiles(targetFolderPath, "*.json", SearchOption.AllDirectories);
                foreach (string jsonFilePath in allAccessibleJsonFiles)
                {
                    ModProfile parsingProfile = null;
                    try
                    {
                        parsingProfile = JsonConvert.DeserializeObject <ModProfile>(File.ReadAllText(jsonFilePath));
                    }
                    catch
                    {
                        continue;
                    }

                    parsingProfile.Name = string.IsNullOrWhiteSpace(parsingProfile.Name) ? "Unknown" : parsingProfile.Name;
                    while (ModManager.ProfileList.ContainsKey(parsingProfile.Name))
                    {
                        parsingProfile.Name = parsingProfile.Name + "*";
                    }

                    List <KeyValuePair <string, Mod> > plannedOrdering = new List <KeyValuePair <string, Mod> >();
                    foreach (KeyValuePair <string, Mod> entry in parsingProfile.ProfileData)
                    {
                        plannedOrdering.Add(entry);
                    }
                    plannedOrdering = new List <KeyValuePair <string, Mod> >(plannedOrdering.OrderBy(o => o.Value.Priority).ToList());

                    ModProfile currentProf = ModManager.GenerateProfile();
                    List <KeyValuePair <string, Mod> > plannedOrderingCurrent = new List <KeyValuePair <string, Mod> >();
                    string[] parsingProfileAllIDs = parsingProfile.ProfileData.Keys.ToArray();
                    foreach (KeyValuePair <string, Mod> entry in currentProf.ProfileData)
                    {
                        if (parsingProfileAllIDs.Contains(entry.Key))
                        {
                            continue;
                        }
                        entry.Value.Enabled = false;
                        plannedOrderingCurrent.Add(entry);
                    }

                    plannedOrdering.AddRange(plannedOrderingCurrent.OrderBy(o => o.Value.Priority));

                    ModProfile creatingProfile = new ModProfile();
                    creatingProfile.ProfileData = new Dictionary <string, Mod>();
                    for (int i = 0; i < plannedOrdering.Count; i++)
                    {
                        plannedOrdering[i].Value.Priority = i + 1;
                        creatingProfile.ProfileData[plannedOrdering[i].Key] = plannedOrdering[i].Value;
                    }

                    if (ModManager.ProfileList == null)
                    {
                        ModManager.ProfileList = new Dictionary <string, ModProfile>();
                    }
                    ModManager.ProfileList[parsingProfile.Name] = creatingProfile;
                    numNewProfiles++;
                }

                Directory.Delete(targetFolderPath, true); // Clean up the temporary data folder
            }
            else // Otherwise just copy the file itself
            {
                string newPath = AddModFromPakPath(newInstallingMod, out bool wasMalformatted);
                if (wasMalformatted)
                {
                    numMalformatted++;
                }
                if (newPath != null)
                {
                    newPaths.Add(newPath);
                }
            }

            List <Mod> outputs = new List <Mod>();

            foreach (string newPath in newPaths)
            {
                try
                {
                    if (!string.IsNullOrEmpty(newPath))
                    {
                        Mod nextMod = ModManager.SyncSingleModFromDisk(newPath, out bool wasClientOnly, false);
                        if (nextMod != null)
                        {
                            outputs.Add(nextMod);
                        }
                        if (wasClientOnly)
                        {
                            numClientOnly++;
                            File.Delete(newPath);
                        }
                    }
                }
                catch (IOException) { }
            }

            return(outputs);
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            BaseForm.CurrentlySyncing = true;
            try
            {
                SetDebugText("Finding server");
                AstroLauncherServerInfo serverInfo = PlayFabAPI.GetAstroLauncherData(OurIP);
                if (serverInfo == null)
                {
                    BaseForm.syncErrored      = true;
                    BaseForm.syncErrorMessage = "Failed to find an online AML-compatible server with the requested address!";
                    return;
                }

                if (PlayFabAPI.Dirty)
                {
                    BaseForm.ModManager.SyncConfigToDisk();
                    PlayFabAPI.Dirty = false;
                }

                List <Mod> allMods          = serverInfo.GetAllMods();
                string     kosherServerName = serverInfo.ServerName;
                if (string.IsNullOrWhiteSpace(kosherServerName) || kosherServerName == "Astroneer Dedicated Server")
                {
                    kosherServerName = OurIP;
                }

                ModProfile creatingProfile = new ModProfile();
                creatingProfile.ProfileData = new Dictionary <string, Mod>();
                List <string> failedDownloadMods = new List <string>();

                // Add our current mods into the brand new profile, and specify that they are disabled
                ModProfile currentProf     = BaseForm.ModManager.GenerateProfile();
                List <Mod> plannedOrdering = new List <Mod>();
                foreach (KeyValuePair <string, Mod> entry in currentProf.ProfileData)
                {
                    //entry.Value.Enabled = false;
                    entry.Value.Enabled = entry.Value.CurrentModData.Sync == SyncMode.ClientOnly;
                    creatingProfile.ProfileData[entry.Key] = entry.Value;
                    plannedOrdering.Add(entry.Value);
                }

                plannedOrdering = new List <Mod>(plannedOrdering.OrderBy(o => o.Priority).ToList());

                if (worker.CancellationPending == true)
                {
                    SetDebugText("Canceled!");
                    BaseForm.syncErrored      = true;
                    BaseForm.syncErrorMessage = "The syncing process was canceled.";
                    e.Cancel = true;
                    return;
                }

                // Incorporate newly synced index files into the global index
                SetDebugText("Fetching index files");
                List <string> DuplicateURLs = new List <string>();
                foreach (Mod mod in allMods)
                {
                    if (mod.CurrentModData.Sync == SyncMode.ServerAndClient || mod.CurrentModData.Sync == SyncMode.ClientOnly)
                    {
                        IndexFile thisIndexFile = mod.GetIndexFile(DuplicateURLs);
                        if (thisIndexFile != null)
                        {
                            thisIndexFile.Mods.ToList().ForEach(x => BaseForm.ModManager.GlobalIndexFile[x.Key] = x.Value);
                            DuplicateURLs.Add(thisIndexFile.OriginalURL);
                        }
                    }
                }

                // Download server mods from the newly incorporated index files
                int numMods = allMods.Count;
                for (int i = 0; i < allMods.Count; i++)
                {
                    if (worker.CancellationPending == true)
                    {
                        SetDebugText("Canceled!");
                        BaseForm.syncErrored      = true;
                        BaseForm.syncErrorMessage = "The syncing process was canceled.";
                        e.Cancel = true;
                        return;
                    }

                    if (i > 0)
                    {
                        worker.ReportProgress((int)((double)i / numMods * 100));
                    }

                    Mod mod = allMods[i];
                    if (mod.CurrentModData.Sync == SyncMode.ServerAndClient || mod.CurrentModData.Sync == SyncMode.ClientOnly)
                    {
                        Mod appliedMod = null;

                        string debugModName = mod.CurrentModData.ModID + " v" + mod.CurrentModData.ModVersion.ToString();

                        // If we already have this mod downloaded, no sense in downloading it again
                        if (BaseForm.ModManager.ModLookup.ContainsKey(mod.CurrentModData.ModID) && BaseForm.ModManager.ModLookup[mod.CurrentModData.ModID].AvailableVersions != null && BaseForm.ModManager.ModLookup[mod.CurrentModData.ModID].AllModData.Keys.Where(m => m.ToString() == mod.InstalledVersion.ToString()).Count() > 0)
                        {
                            SetDebugText("Applying " + debugModName);
                            appliedMod = (Mod)BaseForm.ModManager.ModLookup[mod.CurrentModData.ModID].Clone();
                            appliedMod.InstalledVersion = (Version)mod.InstalledVersion.Clone();
                            creatingProfile.ProfileData[mod.CurrentModData.ModID] = appliedMod;
                        }
                        else
                        {
                            // Otherwise, go ahead and download it
                            SetDebugText("Installing " + debugModName);
                            bool didDownloadMod = BaseForm.DownloadVersionSync(mod, mod.InstalledVersion);
                            if (didDownloadMod)
                            {
                                appliedMod = mod;
                                creatingProfile.ProfileData[mod.CurrentModData.ModID] = appliedMod;
                            }
                            else
                            {
                                failedDownloadMods.Add(debugModName);
                            }
                        }

                        if (appliedMod != null)
                        {
                            appliedMod.Enabled     = true;
                            appliedMod.ForceLatest = false;
                            plannedOrdering.Remove(appliedMod);
                            plannedOrdering.Add(appliedMod);
                        }
                    }
                }

                // Update available versions list to make the syncing seamless
                SetDebugText("Refreshing version display");
                BaseForm.ModManager.UpdateAvailableVersionsFromIndexFiles();

                // Enforce the planned ordering in our new profile
                SetDebugText("Reordering mods");
                for (int i = 0; i < plannedOrdering.Count; i++)
                {
                    string thisModID = plannedOrdering[i].CurrentModData.ModID;
                    if (creatingProfile.ProfileData.ContainsKey(thisModID))
                    {
                        creatingProfile.ProfileData[thisModID].Priority = i + 1;
                    }
                }
                BaseForm.ModManager.RefreshAllPriorites();

                // Add the new profile to the list
                SetDebugText("Adding profile");
                if (BaseForm.ModManager.ProfileList == null)
                {
                    BaseForm.ModManager.ProfileList = new Dictionary <string, ModProfile>();
                }
                string kosherProfileName = kosherServerName + " Synced Mods";
                BaseForm.ModManager.ProfileList[kosherProfileName] = creatingProfile;
                BaseForm.syncKosherProfileName  = kosherProfileName;
                BaseForm.syncFailedDownloadMods = failedDownloadMods.ToArray();
                SetDebugText("Done!");

                BaseForm.syncErrored = false;
            }
            catch (Exception ex)
            {
                if (ex is PlayFabException || ex is WebException)
                {
                    BaseForm.syncErrored      = true;
                    BaseForm.syncErrorMessage = "Failed to access PlayFab!";
                    return;
                }
                BaseForm.syncErrored      = true;
                BaseForm.syncErrorMessage = ex.ToString();
                return;
            }
        }