public static PBOList FindPBOinDirectory(string basePath) { // Recurse through the directory PBOList list = new PBOList(); try { string[] files = Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories); foreach (string file in files) { string fileName = Path.GetFileName(file); string filePath = file.TrimEnd(fileName.ToCharArray()); // for each file get the hash //string fileHash = HashGenerator.GetHash(file); string fileHash = ""; // store data in new pbo list.Add(new PBO(fileName, filePath, fileHash)); } return(list); } catch { return(list); }; }
public bool HaveFileNamesChanged(PBOList inputList) { PBOList diff = new PBOList(); diff.AddRange(this); foreach (PBO inputPBO in inputList) { foreach (PBO thisPBO in this) { if (inputPBO.fileName == thisPBO.fileName) { diff = DeleteFromArray(diff, thisPBO); } } } if (this.Count == 0) { return(true); } if (inputList.Count != this.Count) { return(true); } if (diff.Count > 0) { return(true); } return(false); }
public static void DownloadList(PBOList dlList) { foreach (PBO dlObject in dlList) { Download(dlObject.sdir + "\\" + dlObject.name, dlObject.sdir + "\\" + dlObject.name); } }
public static PBOList HashPBOs(PBOList list) { foreach (PBO pbo in list) { pbo.fileHash = HashGenerator.GetHash(pbo.filePath + pbo.fileName); } return(list); }
public static void DownloadList(PBOList dlList) { foreach (PBO dlObject in dlList) { Log.Info(dlObject.fileName); Download(Program.localSettings.server + dlObject.filePath.Replace(@"\", "/") + "/" + dlObject.fileName, Program.localSettings.modfolder + "\\" + dlObject.filePath + dlObject.fileName); } ; }
public PBOList DeleteFromArray(PBOList list, PBO pbo) { foreach (PBO p in list) { if ((p.fileHash == pbo.fileHash) && (p.fileName == pbo.fileName)) { list.Remove(p); return(list); } ; } return(list); }
public static void Reset() { Log.Info("deleting xml"); PBOList tempPBO = new PBOList(); for (int i = 0; i < remoteSettings.modsArray.Length; i++) { tempPBO.DeleteXML(Path.Combine(LOCAL_FOLDER, (remoteSettings.modsArray[i] + ".xml"))); } ; tempPBO.DeleteXML(LOCAL_SETTINGS); Log.Info("all xmls removed"); }
public static void WritePBOXML(string xmlName, PBOList list) { XDocument xmlFile = XDocument.Load(xmlName); foreach (PBO pbo in list) { var xmlElement = (new XElement("FileNode", new XElement("FileName", pbo.fileName), new XElement("FilePath", pbo.filePath), new XElement("FileHash", pbo.fileHash))); xmlFile.Element("SyncTool").Add(xmlElement); } ; xmlFile.Save(xmlName); }
//the return list contains a list of files not present in the remote repo (Deletion List) public PBOList DeleteList(PBOList remote) { PBOList diff = this; foreach (PBO diffPBO in diff) { foreach (PBO remotePBO in remote) { if (remotePBO.hash == diffPBO.hash) { diff.Remove(diffPBO); } } } return(diff); }
//the return list contains a list of files not present in the remote repo (Deletion List) //Path check is a little long for some extra checks and formatting of path string public PBOList GetDeleteList(PBOList remote) { PBOList diff = new PBOList(); diff.AddRange(this); foreach (PBO thisPBO in this) { foreach (PBO remotePBO in remote) { if ((remotePBO.fileHash == thisPBO.fileHash) && (remotePBO.fileName == thisPBO.fileName) && (Path.GetFullPath(Path.Combine(Program.localSettings.modfolder, remotePBO.filePath)) == Path.GetFullPath(thisPBO.filePath))) { diff = DeleteFromArray(diff, thisPBO); } } } return(diff); }
public static PBOList ReadRepoXML(string s) { IsSyntaxCorrect(s); var doc = XDocument.Load(s); var list = from x in doc.Descendants("FileNode") select new PBO ( (string)x.Element("FileName"), (string)x.Element("FilePath"), (string)x.Element("FileHash") ); PBOList p = new PBOList(); foreach (PBO x in list) { p.Add(x); } return(p); }
public static PBOList ReadXML(string s) { CheckSyntax(s); var doc = XDocument.Load(s); var list = from x in doc.Descendants("FILE") select new PBO ( (string)x.Element("NAME"), (string)x.Element("SDIR"), (string)x.Element("HASH") ); PBOList p = new PBOList(); foreach (PBO x in list) { p.Add(x); } return(p); }
//the return list contains a list of files not present in the local repo (Download List) public PBOList GetDownloadList(PBOList remote) { PBOList diff = new PBOList(); diff.AddRange(remote); foreach (PBO remotePBO in remote) { foreach (PBO thisPBO in this) { if ((remotePBO.fileHash == thisPBO.fileHash) && (remotePBO.fileName == thisPBO.fileName)) { diff = DeleteFromArray(diff, thisPBO); } } } if (this.Count == 0) { return(remote); } return(diff); }
public static void GenRepo() { ArrayList quickRepoList = new ArrayList(); //Get list of mods from server, pull the XML for each one Log.Info("building list of files"); foreach (string modlist in remoteSettings.modsArray) { //Quick PBO list PBOList tempQuickRepo = new PBOList(); tempQuickRepo.GeneratePBOListFromDir(modlist); quickRepoList.Add(tempQuickRepo); } ; //Add hashes to each quick repo Log.InfoStamp("hashing files stored locally"); for (int i = 0; i < remoteSettings.modsArray.Length; i++) { Log.Info(remoteSettings.modsArray[i]); PBOList tempQuickRepo = (PBOList)quickRepoList[i]; tempQuickRepo.AddHashesToList(); tempQuickRepo.RemoveModFolderForServerRepo(); quickRepoList.Add(tempQuickRepo); } ; Log.InfoStamp("saving xml"); for (int i = 0; i < remoteSettings.modsArray.Length; i++) { Log.Info(remoteSettings.modsArray[i]); PBOList tempQuickRepo = (PBOList)quickRepoList[i]; tempQuickRepo.DeleteXML(Path.Combine(LOCAL_FOLDER, (remoteSettings.modsArray[i] + ".xml"))); tempQuickRepo.AddModPathToList(); tempQuickRepo.WriteXMLToDisk(Path.Combine(LOCAL_FOLDER, (remoteSettings.modsArray[i] + ".xml"))); } ; }
static void Main(string[] args) { if (false) { Application.Run(new OptionsMenu()); return; } //load settings Log.Startup(); LocalSettings localSettings = XML.ReadLocalSettingsXML(LOCAL_SETTINGS); RemoteSettings remoteSettings = XML.ReadRemoteSettingsXML(localSettings.server + "settings.xml"); //load repo info PBOList localRepo = XML.ReadXML(LOCAL_REPO); PBOList remoteRepo = XML.ReadXML(localSettings.server + "repo.xml"); Console.WriteLine("DONE"); Console.ReadKey(); //generate object chain of loaded dirs/pbos //create list of pbos that have changed, hashes that have changed PBOList downloadList = localRepo.DownloadList(remoteRepo); PBOList deleteList = localRepo.DeleteList(remoteRepo); //cycle list of pbo downloads, store in temp location //hash downloaded pbos, compare to remote list of pbos //replace local pbos from temp location //update local xml as replace //compare two xml checksums for pbos, again //Run A3 Run.Execute(localSettings); }
public static bool Sync(bool forceSync) { ArrayList remoteRepoList = new ArrayList(); ArrayList localRepoList = new ArrayList(); ArrayList quickRepoList = new ArrayList(); //Get list of mods from server, pull the XML for each one Log.Info("building list of files"); foreach (string modlist in remoteSettings.modsArray) { //For remote XMLs PBOList tempServerRepo = new PBOList(); tempServerRepo = tempServerRepo.ReadFromDisk(Path.Combine(localSettings.server, (modlist + ".xml"))); remoteRepoList.Add(tempServerRepo); //For local XMLs PBOList tempLocalRepo = new PBOList(); tempLocalRepo = tempLocalRepo.ReadFromDisk(Path.Combine(LOCAL_FOLDER, (modlist + ".xml"))); localRepoList.Add(tempLocalRepo); //Quick PBO list PBOList tempQuickRepo = new PBOList(); tempQuickRepo.GeneratePBOListFromDir(modlist); quickRepoList.Add(tempQuickRepo); } ; //Check the server repo for mods, if the list is empty, something is wrong if (remoteRepoList.Count == 0) { Log.Info("something is wrong with the server files, the server may be down temporarily"); return(true); } ; //Check the local mod directory, if it's blank throw error, recreate settings if (localSettings.modfolder == "") { Log.Info("the mod folder appears to be blank, removing"); PBOList temp = new PBOList(); temp.DeleteXML(LOCAL_SETTINGS); return(false); } ; //Check each quick repo against it's corresponding local repo bool haveFilesChanged = false; ArrayList modsThatChanged = new ArrayList(); //Check quick vs local(xml) repo for (int i = 0; i < localRepoList.Count; i++) { PBOList tempLocalRepo = (PBOList)localRepoList[i]; PBOList tempQuickRepo = (PBOList)quickRepoList[i]; if (tempLocalRepo.HaveFileNamesChanged(tempQuickRepo) || forceSync) { haveFilesChanged = true; modsThatChanged.Add(true); } else { modsThatChanged.Add(false); } } ; //Check quick vs remote repo for (int i = 0; i < localRepoList.Count; i++) { PBOList tempRemoteRepo = (PBOList)remoteRepoList[i]; PBOList tempQuickRepo = (PBOList)quickRepoList[i]; if (tempRemoteRepo.HaveFileNamesChanged(tempQuickRepo) || forceSync) { haveFilesChanged = true; modsThatChanged[i] = true; //modifies the value from the first check } ; } ; //Run checks, downloads, and deletions if files have changed if (haveFilesChanged || forceSync || remoteSettings.forceHash) { Log.Info("changes detected or checking has been forced"); //Add hashes to each quick repo Log.InfoStamp("hashing files stored locally"); for (int i = 0; i < remoteSettings.modsArray.Length; i++) { //Only hash mod folders that have changed if ((bool)modsThatChanged[i]) { Log.Info(remoteSettings.modsArray[i]); PBOList tempQuickRepo = (PBOList)quickRepoList[i]; tempQuickRepo.AddHashesToList(); quickRepoList.RemoveAt(i); quickRepoList.Insert(i, tempQuickRepo); } ; } ; //Check each quick repo to each remote repo Log.InfoStamp("finding files to delete"); ArrayList deleteRepoList = new ArrayList(); for (int i = 0; i < remoteSettings.modsArray.Length; i++) { if ((bool)modsThatChanged[i]) { PBOList tempQuickRepo = (PBOList)quickRepoList[i]; PBOList tempRemoteRepo = (PBOList)remoteRepoList[i]; deleteRepoList.Add(tempQuickRepo.GetDeleteList(tempRemoteRepo)); } } ; //Get number of files going to be downloaded int tempCountDelete = 0; foreach (PBOList tempDeleteRepo in deleteRepoList) { tempCountDelete = tempCountDelete + tempDeleteRepo.Count; } if (tempCountDelete > 0) { Log.Info(tempCountDelete + " files will be deleted"); //Delete foreach (PBOList tempDeleteRepo in deleteRepoList) { tempDeleteRepo.DeleteFilesOnDisk(); } Log.Info("files deleted"); //Check for empty folders to delete Log.Info("deleting any empty folders"); foreach (string dir in remoteSettings.modsArray) { if (Directory.Exists(Path.Combine(localSettings.modfolder, dir))) { FileHandler.DeleteEmptyFolders(Path.Combine(localSettings.modfolder, dir)); } } } else { Log.Info("no files to delete"); }; //cycle list of pbo downloads Log.InfoStamp("finding files to download"); ArrayList downloadRepoList = new ArrayList(); for (int i = 0; i < remoteSettings.modsArray.Length; i++) { if ((bool)modsThatChanged[i]) { PBOList tempQuickRepo = (PBOList)quickRepoList[i]; PBOList tempRemoteRepo = (PBOList)remoteRepoList[i]; downloadRepoList.Add(tempQuickRepo.GetDownloadList(tempRemoteRepo)); } } ; //Get number of files going to be downloaded int tempCountDownload = 0; foreach (PBOList tempDownloadRepo in downloadRepoList) { tempCountDownload = +tempDownloadRepo.Count; } if (tempCountDownload > 0) { Log.Info(tempCountDownload + " files will be downloaded"); //Download foreach (PBOList tempDownloadRepo in downloadRepoList) { HTTP.DownloadList(tempDownloadRepo); } ; Log.Info("files downloaded"); } else { Log.Info("no files to download"); }; //save to xml, add the repo from the server after adding back our modfolder Log.InfoStamp("saving xml"); for (int i = 0; i < remoteSettings.modsArray.Length; i++) { Log.Info(remoteSettings.modsArray[i]); PBOList tempLocalRepo = (PBOList)localRepoList[i]; PBOList tempRemoteRepo = (PBOList)remoteRepoList[i]; tempLocalRepo.Clear(); tempLocalRepo.DeleteXML(Path.Combine(LOCAL_FOLDER, (remoteSettings.modsArray[i] + ".xml"))); tempLocalRepo.AddModPathToList(); tempLocalRepo.AddRange(tempRemoteRepo); tempLocalRepo.WriteXMLToDisk(Path.Combine(LOCAL_FOLDER, (remoteSettings.modsArray[i] + ".xml"))); } ; //Mods changed Log.InfoStamp("checking files for consistency"); return(false); } else { //Mods did not change Log.Info("all done, no changes detected"); return(true); }; }