private void copyTheMod(ModObject addedMod) { string logfile = PathCombine(_AbsModFolder, _YameConst.Modlogsdir, addedMod.ModName + ".log"); try { /* Show ProgressBar */ progressBar1.Maximum += addedMod.MoveFileToBackup.Count() + addedMod.CopyFileToGame.Count(); // AlertDependancies foreach (string alert in addedMod.AlertDependancies) { var alertSplit = alert.Split('|'); ModObject dependantMod = _ActivatedModsList.First(mod => mod.ModName == alertSplit[0]); // If already disabled by this mod, break if (dependantMod.DependanciesList.Contains(addedMod.ModName)) break; dependantMod.IsDisabled = true; dependantMod.DependanciesList.Add(addedMod.ModName); // update dependances in ini file ModIniFile["DEPENDANCIES", dependantMod.ModName] = String.Join("|", dependantMod.DependanciesList.ToArray()); } // CreateFolderInBackup foreach (string BackupFolder in addedMod.CreateFolderInBackup) { Directory.CreateDirectory(BackupFolder); } // CreateFolderInGame foreach (string GameFolder in addedMod.CreateFolderInGame) { Directory.CreateDirectory(GameFolder); } // MoveFileToBackup foreach (string mvFile in addedMod.MoveFileToBackup) { if (progressBar1.Value < progressBar1.Maximum) progressBar1.Value++; var mv = mvFile.Split('>'); /* try to make file rw */ FileInfo fileInfo = new FileInfo(mv[0]); fileInfo.IsReadOnly = false; MoveFile(mv[0], mv[1]); } // CopyFileToGame foreach (string cpFile in addedMod.CopyFileToGame) { if (progressBar1.Value < progressBar1.Maximum) progressBar1.Value++; var cp = cpFile.Split('>'); CopyFile(cp[0], cp[1]); /* try to make file rw */ FileInfo fileInfo = new FileInfo(cp[1]); fileInfo.IsReadOnly = false; } // RemoveFileFromGame foreach (string remFile in addedMod.RemoveFileFromGame) { if (progressBar1.Value < progressBar1.Maximum) progressBar1.Value++; var rm = remFile.Split('>'); MoveFile(rm[0], rm[1]); } } catch (Exception) { progressBar1.Value = progressBar1.Minimum; progressBar1.Visible = false; } _ActivatedModsList.Add(addedMod); // LogOutput File.WriteAllText(logfile, addedMod.LogOutput); }
private void loadModsFromIni() { ModIniFile = new INI.Ini(PathCombine(_AbsModFolder, _YameConst.Ininame), true); List<string> ListMods = ModIniFile["MODS", true]; foreach (string mod in ListMods) { var num = mod.Split('='); string splitModName = num[0]; int splitModNum = Int32.Parse(num[1]); // add objectmod string modlogpath = PathCombine(_AbsModFolder, _YameConst.Modlogsdir, splitModName + ".log"); // add mod to activatedModsList List ModObject ModObject thismod = new ModObject(splitModName); thismod.ModNumber = splitModNum; // find files in mod.log if (File.Exists(modlogpath)) { var logFile = File.ReadAllLines(modlogpath); thismod.FilesList = new List<string>(logFile); } ModIniFile = new INI.Ini(PathCombine(_AbsModFolder, _YameConst.Ininame), true); string dependancies = ModIniFile["DEPENDANCIES", splitModName]; if (dependancies != String.Empty) { thismod.IsDisabled = true; thismod.DependanciesList = dependancies.Split('|').ToList(); if (thismod.DependanciesList[thismod.DependanciesList.Count - 1] == String.Empty) thismod.DependanciesList.RemoveAt(thismod.DependanciesList.Count - 1); } _ActivatedModsList.Add(thismod); } _ActivatedModsList = _ActivatedModsList.OrderBy(o => o.ModNumber).ToList(); _TotalModActivated = _ActivatedModsList.Count(); if (_TotalModActivated < 1) { buttonModAllMoins.Enabled = false; buttonModAllMoins.BackgroundImage = Properties.Resources.ButtonCroix_d; } }
private ModObject prepareTheMod(string mod) { List<string> CreateFolderInBackup = new List<string>(); List<string> CreateFolderInGame = new List<string>(); List<string> MoveFileToBackup = new List<string>(); List<string> CopyFileToGame = new List<string>(); List<string> RemoveFileFromGame = new List<string>(); List<string> AlertDependancies = new List<string>(); string LogOutput = ""; bool isREMFILE = false; string backupdir = PathCombine(_AbsModFolder, _YameConst.Backupdir); string moddir = PathCombine(_AbsModFolder, mod); ModObject addedMod = new ModObject(mod); addedMod.ModNumber = _TotalModActivated; string absolutePath = PathCombine(_AbsModFolder, addedMod.ModName); try { var listOfFilesInDir = Directory .EnumerateFiles(moddir, "*", SearchOption.AllDirectories) .Select(Path.GetFullPath); /* Show ProgressBar */ progressBar1.Maximum = listOfFilesInDir.Count(); progressBar1.Visible = true; foreach (string f in listOfFilesInDir) { if (progressBar1.Value < progressBar1.Maximum) progressBar1.Value++; /* if folder is Documentation do next folder */ if (f.Contains(_YameConst.Infomodfldr)) continue; /* if ext file is .yame do next file */ if (Path.GetExtension(f) == _YameConst.Infomodext) continue; var fileRelativePath = f.Substring(absolutePath.Length); // \Media\classes\trucks\SID-75-K5-Blazer.xml List<string> listRelativePath = fileRelativePath.Split('\\').ToList(); // {"", Media, classes, trucks, SID-75-K5-Blazer.xml} var fileNameExt = listRelativePath[listRelativePath.Count - 1]; // SID-75-K5-Blazer.xml listRelativePath.RemoveAt(listRelativePath.Count - 1); // {"", Media, classes, trucks} listRelativePath.RemoveAt(0); // {Media, classes, trucks} var FolderRelativePath = listRelativePath.Count == 0 ? "" : String.Join("\\", listRelativePath); string Fpath = ""; foreach (string folder in listRelativePath) // stocker les nouveaux répertoires parents {Media, classes, trucks} ==> Media s'il est nouveau { Fpath += "\\" + folder; if (addedMod.FilesList.Contains("NEWFLDR|" + Fpath + "\\")) // /!\ Avec cette méthode tous les mods utilisant le repertoire racine seront désactivés ! break; if (!Directory.Exists(_StartPath + Fpath)) { // write NEWFLD in log LogOutput += "NEWFLDR|" + Fpath + "\\" + "\r\n"; // ModObjectList add new folder addedMod.FilesList.Add("NEWFLDR|" + Fpath + "\\"); break; } } // Here not PathCombine cause of absoluteGameFolderToModify doesn't exist at the moment var absoluteGameFolderToModify = _StartPath + "\\" + FolderRelativePath; // if not newfolder, and not folder create by other mods, create folder in backup dir if (!Directory.Exists(PathCombine(backupdir, FolderRelativePath)) && Directory.Exists(absoluteGameFolderToModify)) { // Modifie le 28/01/2016 - Si Folder créé par Mod il faut creer le backup dans le cas ou dependance. Ecrasement de mod //if ( !( addedMod.FilesList.Any(path => path.Contains(FolderRelativePath)) ) && !_ActivatedModsList.Any(aMod => aMod.FilesList.Any(path => path.Contains(FolderRelativePath))) ) CreateFolderInBackup.Add(PathCombine(backupdir, FolderRelativePath)); } // Test if FolderRelativePath exists in Game directory if (!Directory.Exists(absoluteGameFolderToModify)) { // create new folder in game dir CreateFolderInGame.Add(absoluteGameFolderToModify); } // If file mod ends with "-remove", move original file in backup if(Path.GetExtension(fileNameExt).Contains(_YameConst.Removetickfile)) /*_YameConst.Removetickfile = "-remove" */ { isREMFILE = true; string gameFileNameExt = fileNameExt.Replace(_YameConst.Removetickfile,String.Empty); if (File.Exists(PathCombine(absoluteGameFolderToModify, gameFileNameExt))) { string fromGame = PathCombine(absoluteGameFolderToModify, gameFileNameExt); string toBackup = PathCombine(backupdir, FolderRelativePath) + "\\" + gameFileNameExt + "." + addedMod.ModName; // move original file to backup RemoveFileFromGame.Add(fromGame + ">" + toBackup); // write file in log LogOutput += "REMFILE|" + FolderRelativePath + "\\" + gameFileNameExt + "\r\n"; // ModObjectList add new file addedMod.FilesList.Add("REMFILE|" + FolderRelativePath + "\\" + gameFileNameExt); } } else { isREMFILE = false; // write file in log LogOutput += FolderRelativePath + "\\" + fileNameExt + "\r\n"; // ModObjectList add new file addedMod.FilesList.Add(PathCombine(FolderRelativePath, fileNameExt)); } /* Find dependancies */ foreach (ModObject installedMod in _ActivatedModsList) { // if file in root game, FolderRelativePath is empty string relativePathFileName = (FolderRelativePath == String.Empty ? fileNameExt : FolderRelativePath + "\\" + fileNameExt); if (installedMod.FilesList.Contains(relativePathFileName)) { AlertDependancies.Add(installedMod.ModName + "|" + fileNameExt); } string wPath = "\\"; foreach(string folder in listRelativePath) { wPath += folder + "\\"; string wString = "NEWFLDR|" + wPath; if (AlertDependancies.Contains(installedMod.ModName + "|FLDR|" + wPath)) break; if (installedMod.FilesList.Contains(wString)) { AlertDependancies.Add(installedMod.ModName + "|FLDR|" + wPath); } } } // Test if fileNameExt exists in Game directory if (File.Exists(PathCombine(absoluteGameFolderToModify, fileNameExt))) { string fromGame = PathCombine(absoluteGameFolderToModify, fileNameExt); string toBackup = PathCombine(backupdir, FolderRelativePath) + "\\" + fileNameExt + "." + addedMod.ModName; // move original file to backup MoveFileToBackup.Add(fromGame + ">" + toBackup); } // copy mod file to game string fromMod = f; string toGame = absoluteGameFolderToModify + "\\" + fileNameExt; if(!isREMFILE) /* File not ends with -remove, copy file to game */ CopyFileToGame.Add(fromMod + ">" + toGame); } } catch (Exception) { progressBar1.Value = progressBar1.Minimum; progressBar1.Visible = false; return null; } addedMod.CreateFolderInBackup = CreateFolderInBackup; addedMod.CreateFolderInGame = CreateFolderInGame; addedMod.MoveFileToBackup = MoveFileToBackup; addedMod.CopyFileToGame = CopyFileToGame; addedMod.RemoveFileFromGame = RemoveFileFromGame; addedMod.AlertDependancies = AlertDependancies; addedMod.LogOutput = LogOutput; return addedMod; }