public static void Run(string url, string targetVersion, string currentVersion, string destinationFolder)
        {
            if (targetVersion == currentVersion)
            {
                Logger.Log($"{destinationFolder} already contains required version, skipping download");
                return;
            }

            Logger.Log($"Cleaning {destinationFolder}");
            foreach (var entry in Directory.EnumerateFileSystemEntries(destinationFolder))
            {
                if (Directory.Exists(entry))
                {
                    Directory.Delete(entry, true);
                }
                else
                {
                    File.Delete(entry);
                }
            }

            var tempFile = TempFileCache.CreateFile();

            Logger.Log($"Downloading {url} to {tempFile}");
            Program.webClient.DownloadFile(url, tempFile);
            Logger.Log($"Extracting {tempFile} to {destinationFolder}");

            using var stream = new FileStream(tempFile, FileMode.Open, FileAccess.Read);
            using var zip    = new ZipArchive(stream);

            foreach (var zipArchiveEntry in zip.Entries)
            {
                Logger.Log($"Extracting {zipArchiveEntry.FullName}");
                using var entryStream  = zipArchiveEntry.Open();
                using var targetStream = new FileStream(Path.Combine(destinationFolder, zipArchiveEntry.FullName), FileMode.OpenOrCreate, FileAccess.Write);
                entryStream.CopyTo(targetStream);
            }
        }
Ejemplo n.º 2
0
        public static int Main(string[] args)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol |= (SecurityProtocolType)3072;
            webClient.Headers.Add("User-Agent", "Unity web player");

            if (args.Length < 4)
            {
                Logger.LogError("Bad arguments for generator process; expected arguments: <unityVersion> <gameRoot> <gameData> <regenerate> <force_version_unhollower>");
                return(-1);
            }

            if ((args.Length >= 4) && !string.IsNullOrEmpty(args[3]) && args[3].Equals("true"))
            {
                Force_Regenerate = true;
            }

            if (args.Length >= 5)
            {
                try
                {
                    string Force_Version_Unhollower = args[4];
                    if (!string.IsNullOrEmpty(Force_Version_Unhollower))
                    {
                        if (!Force_Version_Unhollower.Equals("0.2.0.0") && !Force_Version_Unhollower.Equals("0.1.1.0") && !Force_Version_Unhollower.Equals("0.1.0.0"))
                        {
                            JsonArray data = (JsonArray)JsonValue.Parse(webClient.DownloadString("https://api.github.com/repos/knah/Il2CppAssemblyUnhollower/releases")).AsJsonArray;
                            if (data.Count > 0)
                            {
                                bool found_version = false;
                                foreach (var x in data)
                                {
                                    string version = x["tag_name"].AsString;
                                    if (!string.IsNullOrEmpty(version) && version.Equals("v" + Force_Version_Unhollower))
                                    {
                                        found_version = true;
                                        ExternalToolVersions.Il2CppAssemblyUnhollowerVersion = Force_Version_Unhollower;
                                        ExternalToolVersions.Il2CppAssemblyUnhollowerUrl     = "https://github.com/knah/Il2CppAssemblyUnhollower/releases/download/v" + Force_Version_Unhollower + "/Il2CppAssemblyUnhollower." + Force_Version_Unhollower + ".zip";
                                        break;
                                    }
                                }
                                if (!found_version)
                                {
                                    throw new Exception("Invalid Version Specified!");
                                }
                            }
                            else
                            {
                                throw new Exception("Unable to Verify Version!");
                            }
                        }
                        else
                        {
                            throw new Exception("Invalid Version! Only Supports v0.3.0.0 and up!");
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.LogError("Failed to Force Unhollower Version, Defaulting to " + ExternalToolVersions.Il2CppAssemblyUnhollowerVersion + "  |  " + e.Message);
                }
            }

            try
            {
                int returnval = (AssemblyGenerator.Main.Initialize(args[0], args[1], args[2]) ? 0 : -2);
                TempFileCache.ClearCache();
                return(returnval);
            }
            catch (Exception ex)
            {
                TempFileCache.ClearCache();
                Logger.LogError("Failed to generate assemblies;");
                Logger.LogError(ex.ToString());

                return(-3);
            }
        }
Ejemplo n.º 3
0
        private static void DownloadDependencies(string unityVersion)
        {
            Logger.Log("Downloading Il2CppDumper");
            DownloaderAndUnpacker.Run(ExternalToolVersions.Il2CppDumperUrl, ExternalToolVersions.Il2CppDumperVersion, localConfig.DumperVersion, Il2CppDumper.BaseFolder, TempFileCache.CreateFile());
            localConfig.DumperVersion = ExternalToolVersions.Il2CppDumperVersion;
            localConfig.Save(localConfigPath);

            Logger.Log("Downloading Il2CppAssemblyUnhollower");
            DownloaderAndUnpacker.Run(ExternalToolVersions.Il2CppAssemblyUnhollowerUrl, ExternalToolVersions.Il2CppAssemblyUnhollowerVersion, localConfig.UnhollowerVersion, Il2CppAssemblyUnhollower.BaseFolder, TempFileCache.CreateFile());
            localConfig.UnhollowerVersion = ExternalToolVersions.Il2CppAssemblyUnhollowerVersion;
            localConfig.Save(localConfigPath);

            Logger.Log("Downloading Unity Dependencies");
            string tempfile     = TempFileCache.CreateFile();
            bool   run_fallback = false;

            try
            {
                DownloaderAndUnpacker.Run($"{ExternalToolVersions.UnityDependenciesBaseUrl}{unityVersion}.zip", unityVersion, localConfig.UnityVersion, UnityDependencies.BaseFolder, tempfile);
                localConfig.UnityVersion = unityVersion;
                localConfig.Save(localConfigPath);
            }
            catch (Exception ex)
            {
                run_fallback = true;
                Logger.LogError(ex.ToString());
                Logger.Log("Can't download Unity Dependencies for " + unityVersion + ", downloading Fallback...");
            }
            if (run_fallback)
            {
                string subver = unityVersion.Substring(0, unityVersion.LastIndexOf("."));
                try
                {
                    JsonArray data = (JsonArray)JsonValue.Parse(Program.webClient.DownloadString("https://api.github.com/repos/HerpDerpinstine/MelonLoader/contents/BaseLibs/UnityDependencies")).AsJsonArray;
                    if (data.Count > 0)
                    {
                        List <string> versionlist = new List <string>();
                        foreach (var x in data)
                        {
                            string version = Path.GetFileNameWithoutExtension(x["name"].AsString);
                            if (version.StartsWith(subver))
                            {
                                versionlist.Add(version);
                                string[] semvertbl = version.Split(new char[] { '.' });
                            }
                        }
                        if (versionlist.Count > 0)
                        {
                            versionlist = versionlist.OrderBy(x => int.Parse(x.Split(new char[] { '.' })[2])).ToList();
                            string latest_version = versionlist.Last();
                            Logger.Log("Fallback Unity Version: " + latest_version);

                            DownloaderAndUnpacker.Run($"{ExternalToolVersions.UnityDependenciesBaseUrl}{latest_version}.zip", latest_version, localConfig.UnityVersion, UnityDependencies.BaseFolder, tempfile);
                            localConfig.UnityVersion = unityVersion;
                            localConfig.Save(localConfigPath);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError("Can't download Unity Dependencies, Unstripping will NOT be done!");
                    Logger.LogError(ex.ToString());
                }
            }
        }