public void Write(List <Pair <string, string> > dirPaths, List <Pair <FilterType, Regex> > filterRules) { if (this.m_IsAdfWritten) { throw new InvalidOperationException("RomFs Adf can be written only once."); } MemoryStream memoryStream = new MemoryStream(); using (StreamWriter streamWriter = new StreamWriter((Stream)memoryStream, Encoding.UTF8)) { streamWriter.WriteLine("formatType : RomFs"); streamWriter.WriteLine("version : 0"); streamWriter.WriteLine("entries :"); List <Pair <Pair <string, string>, string> > pairList = new List <Pair <Pair <string, string>, string> >(); foreach (Pair <string, string> dirPath in dirPaths) { foreach (string enumerateFile in Directory.EnumerateFiles(dirPath.first, "*", SearchOption.AllDirectories)) { if (filterRules == null || !FilterDescription.IsEntryInFilterList(enumerateFile, dirPath.first, filterRules)) { pairList.Add(new Pair <Pair <string, string>, string>(dirPath, enumerateFile)); } } } Func <Pair <Pair <string, string>, string>, string> functionGetPathName = (Func <Pair <Pair <string, string>, string>, string>)(file => { Pair <string, string> first = file.first; string second = file.second; string str = ""; if (first.second != null) { str = first.second.Replace("\\", "/") + "/"; } return(str + second.Replace("\\", "/").Replace(first.first + "/", string.Empty)); }); pairList.Sort((Comparison <Pair <Pair <string, string>, string> >)((fileLeft, fileRight) => string.CompareOrdinal(functionGetPathName(fileLeft), functionGetPathName(fileRight)))); long num = 0; foreach (Pair <Pair <string, string>, string> pair in pairList) { Pair <string, string> first = pair.first; string second = pair.second; streamWriter.WriteLine(" - type : file"); streamWriter.WriteLine(" name : {0}", (object)functionGetPathName(pair)); streamWriter.WriteLine(" offset : {0}", (object)num); FileInfo fileInfo = new FileInfo(second); num = num + fileInfo.Length + 15L & -16L; streamWriter.WriteLine(" path : {0}", (object)Path.GetFullPath(second.Replace("\\", "/"))); } } using (FileStream fileStream = File.Open(this.m_adfPath, !File.Exists(this.m_adfPath) ? FileMode.CreateNew : FileMode.Truncate, FileAccess.Write, FileShare.None)) { byte[] array = memoryStream.ToArray(); fileStream.Write(array, 0, array.Length); this.m_IsAdfWritten = true; } }
public static List <Pair <FilterType, Regex> > ParseFdf(string fdfPath) { List <string> fdfPathList = new List <string>(); if (fdfPath == null) { return((List <Pair <FilterType, Regex> >)null); } return(FilterDescription.ParseFdfRecursively(fdfPath, ref fdfPathList)); }
private static List <Pair <FilterType, Regex> > ParseFdfRecursively(string fdfPath, ref List <string> fdfPathList) { fdfPath = FilterDescription.FormatFilePathDelimiterForFilter(fdfPath); if (!Path.IsPathRooted(fdfPath) && fdfPathList.Count > 0) { fdfPath = Path.Combine(Path.GetDirectoryName(fdfPathList[fdfPathList.Count - 1]), fdfPath); } fdfPath = Path.GetFullPath(fdfPath).TrimEnd('\\'); if (0 <= fdfPathList.FindIndex((Predicate <string>)(path => string.Compare(path, fdfPath, StringComparison.InvariantCultureIgnoreCase) == 0))) { return(new List <Pair <FilterType, Regex> >()); } fdfPathList.Add(fdfPath); List <Pair <FilterType, Regex> > pairList = new List <Pair <FilterType, Regex> >(); using (StreamReader streamReader = new StreamReader(fdfPath)) { while (!streamReader.EndOfStream) { string path = streamReader.ReadLine(); if (!string.IsNullOrEmpty(path) && !path.StartsWith(";")) { string input = FilterDescription.ReplaceEnvironmentVariableKeyword(path); Match match = FilterDescription.RegexKeywordInclude.Match(input); if (match.Success) { string fdfPath1 = match.Groups["path"].Value; pairList.AddRange((IEnumerable <Pair <FilterType, Regex> >)FilterDescription.ParseFdfRecursively(fdfPath1, ref fdfPathList)); } FilterType a; switch (input[0]) { case '+': a = FilterType.Exception; break; case '-': a = FilterType.Remove; break; default: continue; } Regex b = new Regex(FilterDescription.FormatFilePathDelimiterForFilterRegex(input.Substring(1).Trim().Trim('"')), RegexOptions.Compiled); pairList.Add(new Pair <FilterType, Regex>(a, b)); } } return(pairList); } }
public static string FormatFilePathForFilter(string target, string rootDir) { string target1 = target; if (!string.IsNullOrEmpty(rootDir)) { Uri uri1 = new Uri(FilterDescription.NormalizePathForUri(rootDir)); Uri uri2 = new Uri(FilterDescription.NormalizePathForUri(target)); if (uri1.Scheme == uri2.Scheme) { target1 = Uri.UnescapeDataString(uri1.MakeRelativeUri(uri2).ToString()); } } return(FilterDescription.FormatFilePathDelimiterForFilter(target1).TrimEnd('\\')); }
public static bool IsEntryInFilterList(string target, string rootDir, List <Pair <FilterType, Regex> > filterRuleList) { return(FilterDescription.IsEntryInFilterList(FilterDescription.FormatFilePathForFilter(target, rootDir), filterRuleList)); }