Пример #1
0
        public static async Task <bool> AreFilesValid(string ai, string model)
        {
            string mdlDir = GetLocalPath(ai, model);

            if (!Directory.Exists(mdlDir))
            {
                Logger.Log($"Files for model {model} not valid: {mdlDir} does not exist.", true);
                return(false);
            }

            string md5FilePath = Path.Combine(mdlDir, "files.json");

            if (!File.Exists(md5FilePath) || IoUtils.GetFilesize(md5FilePath) < 32)
            {
                Logger.Log($"Files for model {model} not valid: {mdlDir} does not exist or is incomplete.", true);
                return(false);
            }

            List <ModelFile> modelFiles = GetModelFilesFromJson(File.ReadAllText(Path.Combine(mdlDir, "files.json")));

            if (modelFiles.Count < 1)
            {
                Logger.Log($"Files for model {model} not valid: JSON contains {modelFiles.Count} entries.", true);
                return(false);
            }

            foreach (ModelFile mf in modelFiles)
            {
                string filePath = Path.Combine(mdlDir, mf.dir, mf.filename);
                long   fileSize = IoUtils.GetFilesize(filePath);

                if (fileSize != mf.size)
                {
                    Logger.Log($"Files for model {model} not valid: Filesize of {mf.filename} ({fileSize}) does not equal validation size ({mf.size}).", true);
                    return(false);
                }

                if (fileSize > 100 * 1024 * 1024)    // Skip hash calculation if file is very large, filesize check should be enough anyway
                {
                    Logger.Log($"Skipped CRC32 check for {mf.filename} because it's too big ({fileSize / 1024 / 1024} MB)", true);
                    return(true);
                }

                string crc = await IoUtils.GetHashAsync(Path.Combine(mdlDir, mf.dir, mf.filename), IoUtils.Hash.CRC32);

                if (crc.Trim() != mf.crc32.Trim())
                {
                    Logger.Log($"Files for model {model} not valid: CRC32 of {mf.filename} ({crc.Trim()}) does not equal validation CRC32 ({mf.crc32.Trim()}).", true);
                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
 public static void DeleteAllModels()
 {
     foreach (string modelFolder in GetAllModelFolders())
     {
         string size = FormatUtils.Bytes(IoUtils.GetDirSize(modelFolder, true));
         if (IoUtils.TryDeleteIfExists(modelFolder))
         {
             Logger.Log($"Deleted cached model '{Path.GetFileName(modelFolder.GetParentDir())}/{Path.GetFileName(modelFolder)}' ({size})");
         }
     }
 }
Пример #3
0
        public static async Task DownloadModelFiles(AI ai, string modelDir, bool log = true)
        {
            string aiDir = ai.pkgDir;

            Logger.Log($"DownloadModelFiles(string ai = {ai.aiName}, string model = {modelDir}, bool log = {log})", true);

            try
            {
                string mdlDir = GetLocalPath(aiDir, modelDir);

                if (modelDir.EndsWith("_custom") || await AreFilesValid(aiDir, modelDir))
                {
                    return;
                }

                Logger.Log($"Downloading '{modelDir}' model files...", !log);
                Directory.CreateDirectory(mdlDir);

                await DownloadTo(GetMdlFileUrl(aiDir, modelDir, "files.json"), mdlDir, false);

                string           jsonPath   = Path.Combine(mdlDir, "files.json");
                List <ModelFile> modelFiles = GetModelFilesFromJson(File.ReadAllText(jsonPath));

                if (!File.Exists(jsonPath) || IoUtils.GetFilesize(jsonPath) < 32)
                {
                    Interpolate.Cancel($"Error: Failed to download index file. Please try again.");
                    return;
                }

                if (modelFiles.Count < 1)
                {
                    Interpolate.Cancel($"Error: Can't download model files because no entries were loaded from the index file. Please try again.");
                    return;
                }

                foreach (ModelFile mf in modelFiles)
                {
                    string relPath = Path.Combine(mf.dir, mf.filename).Replace("\\", "/");
                    await DownloadTo(GetMdlFileUrl(aiDir, modelDir, relPath), Path.Combine(mdlDir, relPath), log);
                }

                Logger.Log($"Downloaded \"{modelDir}\" model files.", !log, true);

                if (!(await AreFilesValid(aiDir, modelDir)))
                {
                    Interpolate.Cancel($"Model files are invalid! Please try again.");
                }
            }
            catch (Exception e)
            {
                Logger.Log($"DownloadModelFiles Error: {e.Message}\nStack Trace:\n{e.StackTrace}", !log);
                Interpolate.Cancel($"Error downloading model files: {e.Message}");
            }
        }
Пример #4
0
        public static async Task <bool> MakeSymlinksForEncode(string framesFile, string linksDir, int zPad = 8)
        {
            try
            {
                IoUtils.DeleteIfExists(linksDir);
                Directory.CreateDirectory(linksDir);
                Stopwatch sw = new Stopwatch();
                sw.Restart();
                Logger.Log($"Creating symlinks for '{framesFile}' in '{linksDir} with zPadding {zPad}'", true);

                int counter = 0;

                Dictionary <string, string> pathsLinkTarget = new Dictionary <string, string>();

                foreach (string line in File.ReadAllLines(framesFile))
                {
                    string relTargetPath = line.Remove("file '").Split('\'').FirstOrDefault();     // Relative path in frames file
                    string absTargetPath = Path.Combine(framesFile.GetParentDir(), relTargetPath); // Full path to frame
                    string linkPath      = Path.Combine(linksDir, counter.ToString().PadLeft(zPad, '0') + Path.GetExtension(relTargetPath));
                    pathsLinkTarget.Add(linkPath, absTargetPath);
                    counter++;
                }

                await CreateSymlinksParallel(pathsLinkTarget);

                if (IoUtils.GetAmountOfFiles(linksDir, false) > 1)
                {
                    return(true);
                }
                else
                {
                    Logger.Log("Symlink creation seems to have failed even though SymlinksAllowed was true! Encoding ini with concat demuxer instead.", true);
                    return(false);
                }
            }
            catch (Exception e)
            {
                Logger.Log("MakeSymlinks Exception: " + e.Message);
            }

            return(false);
        }
Пример #5
0
 public static void Init()
 {
     configPath = Path.Combine(Paths.GetDataPath(), "config.json");
     IoUtils.CreateFileIfNotExists(configPath);
     Reload();
 }
Пример #6
0
        static async Task DownloadTo(string url, string saveDirOrPath, bool log = true, int retries = 3)
        {
            canceled = false;
            string savePath = saveDirOrPath;

            if (IoUtils.IsPathDirectory(saveDirOrPath))
            {
                savePath = Path.Combine(saveDirOrPath, Path.GetFileName(url));
            }

            IoUtils.TryDeleteIfExists(savePath);
            Directory.CreateDirectory(Path.GetDirectoryName(savePath));
            Logger.Log($"Downloading '{url}' to '{savePath}'", true);
            Stopwatch sw = new Stopwatch();

            sw.Restart();
            bool completed          = false;
            int  lastProgPercentage = -1;
            var  client             = new WebClient();

            client.DownloadProgressChanged += (sender, args) =>
            {
                if (sw.ElapsedMilliseconds > 200 && args.ProgressPercentage != lastProgPercentage)
                {
                    sw.Restart();
                    lastProgPercentage = args.ProgressPercentage;
                    Logger.Log($"Downloading model file '{Path.GetFileName(url)}'... {args.ProgressPercentage}%", !log, true);
                }
            };
            client.DownloadFileCompleted += (sender, args) =>
            {
                if (args.Error != null)
                {
                    Logger.Log("Download failed: " + args.Error.Message, !log);
                }
                completed = true;
            };

            client.DownloadFileTaskAsync(url, savePath).ConfigureAwait(false);

            while (!completed)
            {
                if (canceled || Interpolate.canceled)
                {
                    client.CancelAsync();
                    client.Dispose();
                    return;
                }

                if (sw.ElapsedMilliseconds > 6000)
                {
                    client.CancelAsync();
                    if (retries > 0)
                    {
                        await DownloadTo(url, saveDirOrPath, log, retries --);
                    }
                    else
                    {
                        Interpolate.Cancel("Model download failed.");
                        return;
                    }
                }

                await Task.Delay(500);
            }

            Logger.Log($"Downloaded '{Path.GetFileName(url)}' ({IoUtils.GetFilesize(savePath) / 1024} KB)", true);
        }