Exemple #1
0
        private static bool TryLoadV1Mod(FileInfo fileInfo, ModPathInfo pathInfo, out BaseModConfiguration modData)
        {
            modData = null;

            if (fileInfo.Extension == ".zip")
            {
                var extractFolder = Path.Combine(pathInfo.extractBasePath, fileInfo.Name);
                System.IO.Compression.ZipFile.ExtractToDirectory(fileInfo.FullName, extractFolder);

                var configPath = Path.Combine(extractFolder, "config.json");

                if (!File.Exists(configPath))
                {
                    return(false);
                }

                var fileData = File.ReadAllText(configPath);
                modData = JsonMapper.ToObject <BaseModConfiguration>(fileData);

                if (string.IsNullOrEmpty(modData.ModID) || modData.Version > 1)
                {
                    return(false);
                }

                var destinationPath = Path.Combine(pathInfo.modCachePath, modData.ModID);

                // Delete the directory and re-save cache the mod data
                if (Directory.Exists(destinationPath))
                {
                    Directory.Delete(destinationPath, true);
                }

                modData = LoadModData(modData, extractFolder);
                Directory.Move(extractFolder, destinationPath);

                modData.ModCachePath = destinationPath;

                // Temporary work around since we can't appear to load images from %APPDATA%
                var imageBytes = File.ReadAllBytes(Path.Join(modData.ModCachePath, modData.DisplayImage));
                modData.DisplayImageBase64 = "data:image/png;base64," + Convert.ToBase64String(imageBytes);

                return(true);
            }

            return(false);
        }
        public static BaseModConfiguration[] LoadMods(List <string> modsToLoad, List <ModLoadResult> allResults)
        {
            var modPath         = Path.Combine(Global.APP_DATA_FOLDER, "Mods");
            var extractBasePath = Path.Combine(Global.APP_DATA_FOLDER, "Temp");

            var pathInfo = new ModPathInfo()
            {
                modPath         = modPath,
                extractBasePath = extractBasePath
            };

            if (!Directory.Exists(modPath))
            {
                Directory.CreateDirectory(modPath);
            }

            DeleteAndRecreateFolder(extractBasePath);

            var mods = new List <BaseModConfiguration>();

            allResults.Clear();

            foreach (var modName in modsToLoad)
            {
                var errors   = new List <string>();
                var fileInfo = new FileInfo(Path.Combine(modPath, modName));
                var result   = TryLoadV1Mod(fileInfo, pathInfo, out var modData, errors);

                if (result == ModLoadStatus.Success)
                {
                    if (mods.Any(m => m.ModID == modData.ModID))
                    {
                        result = ModLoadStatus.DuplicateID;
                    }
                    else
                    {
                        mods.Add(modData);
                    }
                }

                allResults.Add(new ModLoadResult(fileInfo.Name, result == ModLoadStatus.Success ? modData?.ModID : null, result, errors));
            }

            return(mods.ToArray());
        }
Exemple #3
0
        public static BaseModConfiguration[] LoadMods(double currentVersion)
        {
            var executionPath   = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var modPath         = Path.Combine(executionPath, MOD_PATH);
            var extractBasePath = Path.Combine(Global.APP_DATA_FOLDER, "Temp");
            var modCachePath    = Path.Combine(Global.APP_DATA_FOLDER, "ModCache");

            var pathInfo = new ModPathInfo()
            {
                executionPath   = executionPath,
                modPath         = modPath,
                extractBasePath = extractBasePath,
                modCachePath    = modCachePath
            };

            if (!Directory.Exists(modPath))
            {
                Directory.CreateDirectory(modPath);
            }

            DeleteAndRecreateFolder(extractBasePath);
            DeleteAndRecreateFolder(modCachePath);

            var mods = new List <BaseModConfiguration>();

            foreach (var file in Directory.GetFiles(modPath))
            {
                var fileInfo = new FileInfo(file);

                if (TryLoadV1Mod(fileInfo, pathInfo, out var modData))
                {
                    mods.Add(modData);
                }
            }

            return(mods.ToArray());
        }
        private static ModLoadStatus TryLoadV1Mod(FileInfo fileInfo, ModPathInfo pathInfo, out BaseModConfiguration modData, List <string> loadErrors)
        {
            modData = null;
            string zipFile = null;

            try
            {
                zipFile = Path.ChangeExtension(fileInfo.FullName, ".zip");
                File.Copy(fileInfo.FullName, zipFile);

                if (fileInfo.Extension == ".mod")
                {
                    var extractFolder = Path.Combine(pathInfo.extractBasePath, fileInfo.Name);

                    System.IO.Compression.ZipFile.ExtractToDirectory(zipFile, extractFolder);

                    var configPath = Path.Combine(extractFolder, "config.json");

                    if (!File.Exists(configPath))
                    {
                        return(ModLoadStatus.NoConfig);
                    }

                    var fileData = File.ReadAllText(configPath);
                    //JsonMapper.ToObject<BaseModConfiguration>(fileData);
                    modData = Newtonsoft.Json.JsonConvert.DeserializeObject <BaseModConfiguration>(fileData, new ModInstallActionConverter());

                    if (string.IsNullOrEmpty(modData.ModID))
                    {
                        return(ModLoadStatus.ConfigInvalid);
                    }

                    if (modData.Version < 2)
                    {
                        return(ModLoadStatus.UnsupportedVersion);
                    }

                    if (modData.Version > 2)
                    {
                        return(ModLoadStatus.UnsupportedVersion);
                    }

                    modData = LoadModData(modData, extractFolder);
                    modData.ModCachePath = extractFolder;

                    // Temporary work around since we can't appear to load images from %APPDATA%
                    var imageFileInfo = new FileInfo(Path.Join(modData.ModCachePath, modData.DisplayImage));
                    var imageBytes    = File.ReadAllBytes(imageFileInfo.FullName);
                    modData.DisplayImageBase64 = $"data:image/{imageFileInfo.Extension};base64," + Convert.ToBase64String(imageBytes);

                    var configValid = CheckModConfigurationIsValid(modData, loadErrors);

                    return(configValid ? ModLoadStatus.Success : ModLoadStatus.ConfigInvalid);
                }
                else
                {
                    return(ModLoadStatus.ExtensionInvalid);
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessage(ex.ToString(), Logging.LogSeverity.Error);
                return(ModLoadStatus.UnspecifiedFailure);
            }
            finally
            {
                if (!string.IsNullOrWhiteSpace(zipFile))
                {
                    File.Delete(zipFile);
                }
            }
        }