예제 #1
0
 public ProfileViewData(SPProfile prof, ProfileType profType)
 {
     this.name         = prof.name;
     this.color        = Color.FromName("Blue");
     this.creatingDate = prof.creationDate;
     this.state        = profType;
 }
        /// <summary>
        /// Returns the application state, and returns by references (if it exists) the
        /// active profile (null otherwise); and the list of desactivated profiles
        /// (or a empty list otherwise).
        /// </summary>
        /// <param name="activeProfileOk"></param>
        /// <param name="desactivatedProfilesOk"></param>
        /// <returns></returns>
        private SPMState discoverApplicationState()
        {
            bool isInstalled  = true;
            int  configStatus = SettingsFactory.checkConfig(this.theGame);

            if (configStatus != Errors.SUCCESS)
            {
                // settings are not ok
                return(SPMState.NOT_CONFIGURED);
            }
            else
            {
                // load desactivated installations
                this.listDesactivated = SPProfile.loadDesactivatedProfiles(this.paths.steamBkp, this.paths.gameFolderName);
                // load active instalations
                if (!Directory.Exists(this.paths.steamGame))
                {
                    this.activeProfile = null;
                    isInstalled        = false;
                }
                else
                {
                    this.activeProfile = SPProfile.loadActivatedProfile(this.paths.steamGame);
                }
            }
            if (!isInstalled)
            {
                if (this.listDesactivated.Count == 0)
                {
                    return(SPMState.NO_PROFILE);
                }
                else
                {
                    return(SPMState.DESACTIVATED_ONLY);
                }
            }
            else
            {
                if (!this.activeProfile.isReady)
                {
                    return(SPMState.INACTIVE_PROFILE);
                }
                else if (this.listDesactivated.Count == 0)
                {
                    return(SPMState.ACTIVE_ONLY);
                }
                else // if(this.listDesactivated.Count > 0)
                {
                    return(SPMState.ACTIVE_AND_DESACTIVATED_PROFILES);
                }
            }
        }
예제 #3
0
        private void sample_entries()
        {
            SPProfile profAct = new SPProfile();
            SPProfile profD1  = new SPProfile();
            SPProfile profD2  = new SPProfile();
            SPProfile profD3  = new SPProfile();

            profAct.name = "Oldrim Vanilla";
            profD1.name  = "Skyrim Vanilla";
            profD2.name  = "Skyrim Dev";
            profD3.name  = "Skyrim Modded";
            List <SPProfile> listD = new List <SPProfile>();

            listD.Add(profD1);
            listD.Add(profD2);
            listD.Add(profD3);
            this.context.desactivatedProf = listD;
            this.context.activeProf       = profAct;
        }
        public bool updateDesactivatedIntegrityFile(SPProfile prof, out string errMsg, out string errPath)
        {
            string content  = prof.name + "," + prof.color + "," + prof.creationDate;
            string filePath = this.desactivatedIntegrityFilePath(prof.name);

            try
            {
                File.WriteAllText(filePath, content);
                errMsg  = "SUCCESS";
                errPath = "";
                return(true);
            }
            catch (Exception ex)
            {
                errMsg  = ex.Message;
                errPath = filePath;
                return(false);
            }
        }
        /// <summary>
        /// Creates an active profile from existing instalation. If there is already an active profile,
        /// or if there is no installation, this method does nothing adn returns an error code.
        /// </summary>
        /// <param name="profileName">Alpanumeric string</param>
        /// <returns></returns>
        public int activateInactiveProfile(string profileName, string color, string creatinDate)
        {
            log.Debug("## manager.activateInactiveProfile() ###########################################");
            log.Debug("# activateInactiveProfile() profileName:" + profileName);
            // update state
            this.updateManagerState();
            if (this.applicationState != SPMState.INACTIVE_PROFILE)
            {
                log.Debug("-- activateInactiveProfile() operation can only be executed if the application state is SPMState.INACTIVE_PROFILE");
                log.Error(" ** Errors.ERR_INVALID_STATE_FOR_REQUESTED_OPERATION_1");
                return(Errors.ERR_INVALID_STATE_FOR_REQUESTED_OPERATION_1);
            }

            // Create profile object
            if (profileName.Trim() == Consts.INACTIVE_NAME)
            {
                // name already in use
                log.Error(" ** Errors.ERR_PROFILE_NAME_ALREADY_EXISTS_1");
                return(Errors.ERR_PROFILE_NAME_ALREADY_EXISTS_1);
            }
            foreach (var item in this.listDesactivated)
            {
                if (profileName.Trim() == item.name.Trim())
                {
                    // name already in use
                    log.Error(" ** Errors.ERR_PROFILE_NAME_ALREADY_EXISTS_2");
                    return(Errors.ERR_PROFILE_NAME_ALREADY_EXISTS_2);
                }
            }
            if (this.activeProfile != null)
            {
                if (this.activeProfile.name.Trim() == profileName.Trim())
                {
                    // name already in use
                    log.Error(" ** Errors.ERR_PROFILE_NAME_ALREADY_EXISTS_3");
                    return(Errors.ERR_PROFILE_NAME_ALREADY_EXISTS_3);
                }
            }
            SPProfile newProfile = new SPProfile();

            newProfile.color        = color;
            newProfile.name         = profileName;
            newProfile.creationDate = creatinDate;
            newProfile.isReady      = true;

            // Create integrity file
            log.Debug("-- creating integrity file for profile " + newProfile.name);
            string errMsg  = "";
            string errPath = "";

            //if (!this.paths.updateActiveIntegrityFile(newProfile, out errMsg, out errPath))
            if (!this.integrityFile.updateActiveIntegrityFile(newProfile, out errMsg, out errPath))
            {
                log.Error(" ** Could not create intregrity file");
                log.Error(" ** ERROR errMsg:" + errMsg + ", errPath:" + errPath);
                log.Error(" ** Errors.ERR_CANNOT_CREATE_INTEGRITY_FILE_1");
                return(Errors.ERR_CANNOT_CREATE_INTEGRITY_FILE_1);
            }
            // update Manager state
            this.reloadState();
            return(Errors.SUCCESS);
        }
        /// <summary>
        /// Rename a ACTIVE or DESACTIVATED profile. Have no effect on INACTIVE profile.
        /// </summary>
        /// <param name="profNameOld"></param>
        /// <param name="profNameNew"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        public int editProfile(string profNameOld, string profNameNew, string colorNew, out string errMsgRet)
        {
            bool   ret     = false;
            string errMsg  = "";
            string errDir  = "";
            string errName = "";
            string errPath = "";

            log.Debug("## manager.editProfile() #######################################################");
            log.Debug("# editProfile() profNameOld:" + profNameOld + ", profNameNew:" + profNameNew);
            log.Debug("* profNameOld:" + profNameOld);
            log.Debug("* profNameNew:" + profNameNew);
            log.Debug("* colorNew:" + colorNew);

            // check state
            this.updateManagerState();
            if ((this.applicationState != SPMState.ACTIVE_AND_DESACTIVATED_PROFILES) &&
                (this.applicationState != SPMState.ACTIVE_ONLY) &&
                (this.applicationState != SPMState.DESACTIVATED_ONLY))
            {
                log.Warn("-- invalid state for requested operation editProfile. State:" + this.applicationState);
                errMsgRet = "Invalid state for Edit operation. State:" + this.applicationState;
                log.Error(" ** Errors.ERR_INVALID_STATE_FOR_REQUESTED_OPERATION_4");
                return(Errors.ERR_INVALID_STATE_FOR_REQUESTED_OPERATION_4);
            }

            profNameNew = this.safeNewName(profNameNew);
            int profileType = 0;

            profNameOld.Trim();
            SPProfile prof = this.searchProfile(profNameOld, 0, out profileType); // any profile

            log.Debug("- Profile-Old-Name:" + profNameOld +
                      ", profileType:" + profileType + "(1:Active/2:Desactivated/0:NULL)");
            if (prof != null)
            {
                prof.name  = profNameNew;
                prof.color = colorNew;
                // creatin date is not changed
                if (profileType == 1) // ACTIVE profile
                {
                    ret = this.integrityFile.updateActiveIntegrityFile(prof, out errMsg, out errPath);
                    if (ret)
                    {
                        log.Info("Success Updating integrity file! New Integrity File:{" + this.integrityFile.activeIntegrityFileContent() + "}");
                        errMsgRet = "SUCCESS";
                        return(Errors.SUCCESS);
                    }
                    else
                    {
                        log.Error("** CANNOT UPDATE ACTIVE INTEGRITY FILE");
                        log.Error("** errMsg:" + errMsg + ", errPath:" + errPath);
                        errMsgRet = "SUCCESS";
                        log.Error(" ** Errors.ERR_CANNOT_CREATE_INTEGRITY_FILE_3");
                        return(Errors.ERR_CANNOT_CREATE_INTEGRITY_FILE_3);
                    }
                }
                else if (profileType == 2) // DESACTIVATED profile
                {
                    List <string> dirsToCheck = this.paths.getAllPaths_BkpProf(profNameOld).listPaths;
                    List <string> names       = new List <string>();
                    int           nDirs       = dirsToCheck.Count;
                    for (int i = 0; i < nDirs; i++)
                    {
                        names.Add(profNameNew);
                    }
                    log.Debug(" -- editProfile for DESACTIVATED profile ");
                    log.Debug(" -- dirsToCheck:{" + CSharp.listToCsv(dirsToCheck) + "}");
                    log.Debug(" -- names:{" + CSharp.listToCsv(names) + "}");
                    errMsg  = "";
                    errDir  = "";
                    errName = "";
                    // OK! rename dirs
                    ret = CSharp.stackRename(dirsToCheck, names, out errMsg, out errDir, out errName);
                    if (ret)
                    {
                        // update integrity file
                        ret = this.integrityFile.updateDesactivatedIntegrityFile(prof, out errMsg, out errPath);
                        if (ret)
                        {
                            log.Info("Success Updating integrity file! New Integrity File:{" + this.integrityFile.activeIntegrityFileContent() + "}");
                        }
                        else
                        {
                            log.Error("** CANNOT UPDATE ACTIVE INTEGRITY FILE");
                            log.Error("** errMsg:" + errMsg + ", errPath:" + errPath);
                            errMsgRet = errMsg;
                            log.Error(" ** Errors.ERR_CANNOT_CREATE_INTEGRITY_FILE_4");
                            return(Errors.ERR_CANNOT_CREATE_INTEGRITY_FILE_4);
                        }
                    }
                    else
                    {
                        log.Error("** ERROR RENAMING DIRECTORIES errMsg:" + errMsg +
                                  ", errDir:" + errDir + ", errName:" + errName);
                        errMsgRet = errMsg;
                        log.Error(" ** Errors.ERR_MOVING_DIRECTORIES_3");
                        return(Errors.ERR_MOVING_DIRECTORIES_3);
                    }
                }
                this.reloadState();
                errMsgRet = "SUCCESS";
                return(Errors.SUCCESS);
            }
            log.Warn("-- invalid profile name profNameOld:" + profNameOld);
            errMsgRet = "SUCCESS";
            log.Error(" ** Errors.ERR_INVALID_PROFILE_NAME_3");
            return(Errors.ERR_INVALID_PROFILE_NAME_3);
        }
        /// <summary>
        /// If there is no profile installed, set a desactivated profile as active. Otherwise does nothing
        /// and returns an error code.
        /// </summary>
        /// <param name="profileName">Alpanumeric string</param>
        /// <returns></returns>
        public int activateDesactivatedProfile(string profileName, out string outErrMsg)
        {
            bool opWasCancelled = false;

            log.Debug("## manager.activateDesactivatedProfile() #######################################");
            log.Debug("# activateDesactivatedProfile() profileName:" + profileName);
            log.Debug("STEP 1: Check Settings...");
            this.updateManagerState();
            if (this.applicationState != SPMState.DESACTIVATED_ONLY)
            {
                outErrMsg = "Invalid state for requested operation. This operation may only be completed if the aplication state is SPMState.DESACTIVATED_ONLY.";
                log.Warn(" -- " + outErrMsg);
                log.Error(" ** Errors.ERR_INVALID_STATE_FOR_REQUESTED_OPERATION_5");
                return(Errors.ERR_INVALID_STATE_FOR_REQUESTED_OPERATION_5);
            }
            profileName = profileName.Trim();
            if (profileName == null || profileName.Equals(""))
            {
                outErrMsg = "profile name is empty";
                log.Warn(" -- " + outErrMsg);
                log.Warn(" -- Errors.ERR_INVALID_PROFILE_NAME_1");
                return(Errors.ERR_INVALID_PROFILE_NAME_1);
            }

            log.Debug("STEP 2: search profile to activate...");
            SPProfile profToActivate = null;

            foreach (var item in this.listDesactivated)
            {
                if (item.name.Trim() == profileName.Trim())
                {
                    profToActivate = item;
                    break;
                }
            }
            if (profToActivate == null)
            {
                outErrMsg = "Specified profile could not be found, ERR_INVALID_PROFILE_NAME";
                log.Warn(" -- " + outErrMsg);
                log.Error(" ** Errors.ERR_INVALID_PROFILE_NAME_2");
                return(Errors.ERR_INVALID_PROFILE_NAME_2);
            }

            log.Debug("STEP 3: moving folders from backup to root dir...");
            // check backup before moving
            string errPathOut = "";
            string errLabel   = "";
            int    ret        = this.paths.checkBackupInstallationPaths(profileName, out errPathOut, out errLabel);

            if (ret != Errors.SUCCESS)
            {
                outErrMsg = "Backup Path (" + profileName + ") " + errLabel + ": <" + errPathOut + ">";
                return(ret);
            }
            // load all paths
            ListPaths listPathsDst = this.paths.getAllPaths_App();
            ListPaths listPathsSrc = this.paths.getAllPaths_BkpProfGame(profileName);
            string    errLabel1    = "";
            string    errLabel2    = "";

            if (!listPathsSrc.checkLabels(listPathsDst, out errLabel1, out errLabel2))
            {
                outErrMsg = "sourceType: " + errLabel1 + ", destination:" + errLabel2;
                log.Error(" ** Source and destination folder are mislabeled!!");
                log.Error(" ** errLabel1:" + errLabel1);
                log.Error(" ** errLabel2:" + errLabel2);
                log.Error(" ** outErrMsg:" + outErrMsg);
                log.Error(" ** Source Paths:" + CSharp.listToCsv(listPathsSrc.listPaths));
                log.Error(" ** Destination Paths:" + CSharp.listToCsv(listPathsDst.listPaths));
                log.Error(" ** Errors.ERR_SOURCE_DESTINATION_DONT_MATCH_ACTIVATEDESACTIVATED");
                return(Errors.ERR_SOURCE_DESTINATION_DONT_MATCH_ACTIVATEDESACTIVATED);
            }
            string[] destinationDirs = listPathsDst.vecPaths;
            string[] sourceDirs      = listPathsSrc.vecPaths;
            string   errMsg          = "";
            string   errSrcDir       = "";
            string   errDstDir       = "";
            string   errPath         = "";
            bool     sucess          = CSharp.stackMv(sourceDirs, destinationDirs, true, LogMethod.LOGGER,
                                                      out errMsg, out errSrcDir, out errDstDir, out opWasCancelled);

            if (!sucess)
            {
                if (opWasCancelled)
                {
                    outErrMsg = "Operation cancelled by the User";
                    log.Warn(" ** outMsg: " + outErrMsg);
                    log.Warn(" ** errMsg: " + errMsg);
                    log.Warn(" ** errSrcDir:" + errSrcDir);
                    log.Warn(" ** errDstDir: " + errDstDir);
                    log.Warn(" ** Errors.ERR_MOVING_DIRECTORIES_2");
                    return(Errors.INFO_OPERATION_CANCELLED_BY_USER);
                }
                outErrMsg = "Error moving directories: SrcDir<" + errSrcDir + ">, DstDir<" + errDstDir + ">. Error Message:" + errMsg;
                log.Error(" ** " + outErrMsg);
                log.Error(" ** errMsg: " + errMsg);
                log.Error(" ** errSrcDir:" + errSrcDir);
                log.Error(" ** errDstDir: " + errDstDir);
                log.Error(" ** Errors.ERR_MOVING_DIRECTORIES_1");
                return(Errors.ERR_MOVING_DIRECTORIES_1);
            }

            log.Debug("STEP 4: create integrity file...");
            errMsg  = "";
            errPath = "";
            if (!this.integrityFile.updateActiveIntegrityFile(profToActivate, out errMsg, out errPath))
            {
                outErrMsg = "Could not create intregrity file. Message:" + errMsg + ". Error Path:" + errPath;
                log.Error(" ** outErrMsg:" + outErrMsg);
                log.Error(" ** ERROR errMsg:" + errMsg + ", errPath:" + errPath);
                log.Error(" ** Errors.ERR_CANNOT_CREATE_INTEGRITY_FILE_2");
                return(Errors.ERR_CANNOT_CREATE_INTEGRITY_FILE_2);
            }

            log.Debug("STEP 5: update manager state...");
            profToActivate.isReady = true;
            this.reloadState();

            outErrMsg = "";
            return(Errors.SUCCESS);
        }