示例#1
0
        /// <summary>
        /// Check if version file exists and if it is a valid version file
        /// </summary>
        /// <param name="path"></param>
        /// <returns>Version string in form x.y.z.w, kill, or null</returns>
        public static string ExtractVersionStringFile(string path, Configuration.VersionMode vmode)
        {
            string[] lines = null;
            if (string.IsNullOrEmpty(path) || InvalidPathCharacters(path) || !new System.IO.FileInfo(path).Exists)
            {
                return(null);
            }

            else if (new System.IO.FileInfo(path).Exists)
            {
                try
                {
                    lines = File.ReadAllLines(path);
                }
                catch (Exception e)
                {
                    PrintInformation(Configuration.InformationStrings.NONE, "Error while reading file, " + e.Message, Util.PrintMode.ERROR, false);
                    return(null);
                }

                foreach (string s in lines)
                {
                    PrintInformation(Configuration.InformationStrings.NONE, "Contents of file: " + s, Util.PrintMode.NORMAL, false);
                }

                string stringPattern = "";
                switch (vmode)
                {
                case Configuration.VersionMode.BINARY:
                    stringPattern = "bin ";
                    return(ParseVersionString(lines, stringPattern));

                case Configuration.VersionMode.CONFIGURATION:
                    stringPattern = "conf ";
                    return(ParseVersionString(lines, stringPattern));

                case Configuration.VersionMode.KILL:
                    return(ParseKillString(lines));

                default:
                    return(null);
                }
            }

            PrintInformation(Configuration.InformationStrings.NONE, "Parsing. Invalid version file, a general error occurred", PrintMode.ERROR, false);
            return(null);
        }
示例#2
0
        /// <summary>
        /// Installs the new version of the binary and update tool, together with libraries, in parent folder with corresponding version signature.
        /// If C:\Program Files (x86)\TARGET\Target Project is the current directory, the new version will be installed to
        /// C:\Programs (x86)\TARGET\x.y.z.w\
        ///
        /// In case of a binary update InstallUpdate compares the new binary first, before install
        /// Then the old program will be closed and the new one will be started.
        ///
        /// If it doesn't succeed don't invoke any error handling
        /// </summary>
        /// <param name="fileName">Filename to be downloaded</param>
        /// <param name="fileExtension">Different downloads have different file extensions, some have none ""</param>
        /// <param name="remoteVersionString">Version string that specifies file to download</param>
        /// <param name="path">Path to the current working directory</param>
        /// <param name="vmode">Configuration or Binary update</param>
        /// <returns>null on error otherwise new executable path</returns>
        public static string InstallUpdate(string fileName, string fileExtension, string remoteVersionString, string path, Configuration.VersionMode vmode)
        {
            if (String.IsNullOrEmpty(fileName) || String.IsNullOrEmpty(remoteVersionString) || String.IsNullOrEmpty(path))
            {
                Util.PrintInformation(Configuration.InformationStrings.NONE, "Variables not set", Util.PrintMode.ERROR, false);
                return(null);
            }

            fileName += remoteVersionString + fileExtension;
            string downloadLocation = Path.Combine(path, fileName);
            string downloadLink     = SERVER + fileName;
            string returnPath       = path;

            Util.PrintInformation(Configuration.InformationStrings.NONE, "Download Location:" + downloadLocation, Util.PrintMode.NORMAL, false);
            Util.PrintInformation(Configuration.InformationStrings.NONE, "Download Link:" + downloadLink, Util.PrintMode.NORMAL, false);

            // Download installation file (TARGET_x.y.z.w.zip or client_check_x.y.z.w)
            if (!Util.DownloadFile(downloadLink, downloadLocation))
            {
                Util.PrintInformation(Configuration.InformationStrings.NONE, "The file: " + downloadLink + " could not be downloaded", Util.PrintMode.ERROR, false);
                return(null);
            }
            switch (vmode)
            {
            // If a binary is installed we check the binary file version to see if it is indeed newer than the current one
            case Configuration.VersionMode.BINARY:
                string unpackLocation  = Path.Combine(EXECUTABLE_DIRECTORY_PARENT, remoteVersionString);
                string tmpFolder       = Path.GetTempPath();
                string tmpTargetFolder = Path.Combine(tmpFolder, remoteVersionString);
                Util.PrintInformation(Configuration.InformationStrings.NONE, "Trying to extract to temp folder: " + tmpTargetFolder, Util.PrintMode.NORMAL, false);

                bool ret = Util.ExtractFiles(downloadLocation, tmpTargetFolder);

                // If we were able to extract the files ..
                if (ret)
                {
                    Util.PrintInformation(Configuration.InformationStrings.NONE, "Set unpack location: " + unpackLocation, Util.PrintMode.NORMAL, false);
                    returnPath = unpackLocation;

                    // .. create path to binary
                    string PathToBinary = Path.Combine(tmpTargetFolder, TARGET_BINARY_NAME);
                    int    comp         = Util.CompareBinaryFileVersion(PathToBinary, BINARY_LOCAL_VERSION_STRING);

                    // .. and test whether the new version is really newer
                    if (comp == 1)
                    {
                        Util.PrintInformation(Configuration.InformationStrings.NONE, "Binary pointed by path is newer than " + BINARY_LOCAL_VERSION_STRING, Util.PrintMode.NORMAL, false);
                    }
                    else
                    {
                        Util.PrintInformation(Configuration.InformationStrings.NONE, "Binary pointed by path is older/same than " + BINARY_LOCAL_VERSION_STRING, Util.PrintMode.NORMAL, false);
                        return(null);
                    }
                }

                Util.ExtractFiles(downloadLocation, unpackLocation);
                Util.PrintInformation(Configuration.InformationStrings.INFORMATION_000_006, unpackLocation, Util.PrintMode.NORMAL, false);
                break;

            case Configuration.VersionMode.CONFIGURATION:
                Util.PrintInformation(Configuration.InformationStrings.NONE, "Nothing to do in CONFIGURATION mode", Util.PrintMode.NORMAL, false);
                break;

            default:
                return(null);
            }


#if __DEBUG
            Util.PrintFileSystemEntries(returnPath);
#endif

            return(returnPath);
        }
示例#3
0
        /// <summary>
        /// Compare various kinds of version strings, or find kill string.
        /// </summary>
        /// <param name="tmpFileVersion">Path to file that needs to be parsed for a version string</param>
        /// <param name="vmode">What kind of update, config files or binary, or killswitch</param>
        /// <returns></returns>
        public static string FindNewOnlineVersion(string tmpFileVersion, Configuration.VersionMode vmode)
        {
            Util.PrintInformation(Configuration.InformationStrings.NONE, "\n" + LINE, Util.PrintMode.NORMAL, false);

            if (String.IsNullOrEmpty(tmpFileVersion))
            {
                Util.PrintInformation(Configuration.InformationStrings.NONE, "tmpFileVersion not set", Util.PrintMode.ERROR, false);
                return(null);
            }

            string remoteVersionString = Util.ExtractVersionStringFile(tmpFileVersion, vmode);

            if (String.IsNullOrEmpty(remoteVersionString))
            {
                Util.PrintInformation(Configuration.InformationStrings.NONE, "Remote Version String could not be extracted", Util.PrintMode.ERROR, false);
                return(null);
            }

            Util.PrintInformation(Configuration.InformationStrings.NONE, "REMOTEVERSION:" + remoteVersionString, Util.PrintMode.NORMAL, false);


            int comp = -1;

            switch (vmode)
            {
            case Configuration.VersionMode.BINARY:
                Util.PrintInformation(Configuration.InformationStrings.NONE, "BINARY_LOCAL_VERSION_STRING:" + BINARY_LOCAL_VERSION_STRING, Util.PrintMode.NORMAL, false);
                comp = Util.CompareVersion(remoteVersionString, BINARY_LOCAL_VERSION_STRING);
                break;

            case Configuration.VersionMode.CONFIGURATION:
                Util.PrintInformation(Configuration.InformationStrings.NONE, "CONFIG_LOCAL_VERSION_STRING:" + CONFIG_LOCAL_VERSION_STRING, Util.PrintMode.NORMAL, false);
                comp = Util.CompareVersion(remoteVersionString, CONFIG_LOCAL_VERSION_STRING);
                break;

            case Configuration.VersionMode.KILL:
                Util.PrintInformation(Configuration.InformationStrings.NONE, "KILL_MODE", Util.PrintMode.NORMAL, false);
                return(remoteVersionString);

            default:
                Util.PrintInformation(Configuration.InformationStrings.NONE, "Unsupported vmode", Util.PrintMode.ERROR, false);
                return(null);
            }

            // Assuming the remote version string is newer than the other, we return that version string, e.g.
            // 1.2.3.4 > 0.0.2.1
            switch (comp)
            {
            case 0:
                Util.PrintInformation(Configuration.InformationStrings.NONE, "Same version", Util.PrintMode.NORMAL, false);
                return(null);

            case 1:
                Util.PrintInformation(Configuration.InformationStrings.NONE, "Remote version is newer", Util.PrintMode.NORMAL, false);
                return(remoteVersionString);

            case 2:
                Util.PrintInformation(Configuration.InformationStrings.NONE, "Local version is newer", Util.PrintMode.NORMAL, false);
                return(null);

            default:
                Util.PrintInformation(Configuration.InformationStrings.INFORMATION_000_002, "", Util.PrintMode.ERROR, false);
                return(null);
            }
        }