Exemplo n.º 1
0
 /// <summary>
 /// Stores a config setting.  This is meant to be open and editable by the user by the config file.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public object SetConfig(string key, object value)
 {
     if (ConfigFile.ContainsKey(key))
     {
         ConfigFile[key] = value.ToString();
         return(value);
     }
     ConfigFile.Add(key, value.ToString());
     return(value);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Gets a value from the config settings.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="onNull"></param>
 /// <returns></returns>
 public string GetConfig(string key, string onNull = "")
 {
     if (ConfigFile.ContainsKey(key))
     {
         return(ConfigFile[key]);
     }
     else
     {
         ConfigFile.Add(key, onNull);
         return(onNull);
     }
 }
Exemplo n.º 3
0
        internal static void UpdateCustomDataFile(ConfigFile file)
        {
            if (!file.ContainsKey(new ConfigDefinition("General", "Version")))
            {
                File.Copy(file.ConfigFilePath, file.ConfigFilePath + ".bak", true);
                file.Clear();
                ((Dictionary <ConfigDefinition, string>)OrphanedEntriesProp.GetValue(file)).Clear();
            }

            ConfigEntry <string> version = file.Bind("General", "Version", LDBToolPlugin.VERSION, "Version of LDB Tool which created this file. DO NOT EDIT THIS MANUALLY!");

            version.Value = LDBToolPlugin.VERSION;
            file.Save();
        }
Exemplo n.º 4
0
        protected void DownloadJars()
        {
            ConfigFile md5s = new ConfigFile();

            if (File.Exists(Path.Combine(Inst.BinDir, "md5s")))
            {
                md5s.Load(Path.Combine(Inst.BinDir, "md5s"));
            }
            State = EnumState.DOWNLOADING;

            int[]  fileSizes = new int[this.uriList.Length];
            bool[] skip      = new bool[this.uriList.Length];

            // Get the headers and decide what files to skip downloading
            for (int i = 0; i < uriList.Length; i++)
            {
                Console.WriteLine("Getting header " + uriList[i].ToString());

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uriList[i]);
                request.Timeout = 1000 * 15;                 // Set a 15 second timeout
                request.Method  = "HEAD";

                string etagOnDisk = null;
                if (md5s.ContainsKey(GetFileName(uriList[i])))
                {
                    etagOnDisk = md5s[GetFileName(uriList[i])];
                }

                if (!forceUpdate && !string.IsNullOrEmpty(etagOnDisk))
                {
                    request.Headers[HttpRequestHeader.IfNoneMatch] = etagOnDisk;
                }

                using (HttpWebResponse response = ((HttpWebResponse)request.GetResponse()))
                {
                    int code = (int)response.StatusCode;
                    if (code == 300)
                    {
                        skip[i] = true;
                    }

                    fileSizes[i]            = (int)response.ContentLength;
                    this.totalDownloadSize += fileSizes[i];
                    Console.WriteLine("Got response: " + code + " and file size of " +
                                      fileSizes[i] + " bytes");
                }
            }

            int initialPercentage = Progress;

            byte[] buffer = new byte[1024 * 10];
            for (int i = 0; i < this.uriList.Length; i++)
            {
                if (skip[i])
                {
                    Progress = (initialPercentage + fileSizes[i] *
                                (100 - initialPercentage) / this.totalDownloadSize);
                }
                else
                {
                    string currentFile = GetFileName(uriList[i]);

                    if (currentFile == "minecraft.jar" && File.Exists("mcbackup.jar"))
                    {
                        File.Delete("mcbackup.jar");
                    }

                    md5s.Remove(currentFile);
                    md5s.Save(Path.Combine(Inst.BinDir, "md5s"));

                    int       failedAttempts = 0;
                    const int MAX_FAILS      = 3;
                    bool      downloadFile   = true;

                    // Download the files
                    while (downloadFile)
                    {
                        downloadFile = false;

                        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uriList[i]);
                        request.Headers[HttpRequestHeader.CacheControl] = "no-cache";

                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                        string etag = "";
                        // If downloading from Mojang, use ETag.
                        if (uriList[i].ToString().StartsWith(Properties.Resources.MojangMCDownloadUrl))
                        {
                            etag = response.Headers[HttpResponseHeader.ETag];
                            etag = etag.TrimEnd('"').TrimStart('"');
                        }
                        // If downloading from dropbox, ignore MD5s
                        else
                        {
                            // TODO add a way to verify integrity of files downloaded from dropbox
                        }

                        Stream dlStream = response.GetResponseStream();
                        using (FileStream fos =
                                   new FileStream(Path.Combine(Inst.BinDir, currentFile), FileMode.Create))
                        {
                            int fileSize = 0;

                            using (MD5 digest = MD5.Create())
                            {
                                digest.Initialize();
                                int readSize;
                                while ((readSize = dlStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    //							Console.WriteLine("Read " + readSize + " bytes");
                                    fos.Write(buffer, 0, readSize);

                                    this.currentDownloadSize += readSize;
                                    fileSize += readSize;

                                    digest.TransformBlock(buffer, 0, readSize, null, 0);

                                    //							Progress = fileSize / fileSizes[i];

                                    Progress = (initialPercentage + this.currentDownloadSize *
                                                (100 - initialPercentage) / this.totalDownloadSize);
                                }
                                digest.TransformFinalBlock(new byte[] { }, 0, 0);

                                dlStream.Close();

                                string md5 = DataUtils.HexEncode(digest.Hash).Trim();
                                etag = etag.Trim();

                                bool md5Matches = true;
                                if (!string.IsNullOrEmpty(etag) && !string.IsNullOrEmpty(md5))
                                {
                                    // This is temporarily disabled since dropbox doesn't use MD5s as etags
                                    md5Matches = md5.Equals(etag);
                                    //							Console.WriteLine(md5 + "\n" + etag + "\n");
                                }

                                if (md5Matches && fileSize == fileSizes[i] || fileSizes[i] <= 0)
                                {
                                    md5s[(currentFile.Contains("natives") ?
                                          currentFile : currentFile)] = etag;
                                    md5s.Save(Path.Combine(Inst.BinDir, "md5s"));
                                }
                                else
                                {
                                    failedAttempts++;
                                    if (failedAttempts < MAX_FAILS)
                                    {
                                        downloadFile              = true;
                                        this.currentDownloadSize -= fileSize;
                                    }
                                    else
                                    {
                                        OnErrorMessage("Failed to download " + currentFile +
                                                       " MD5 sums did not match.");
                                        Cancel();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }