public _7thWrapperLib.RuntimeMod GetRuntime(_7thWrapperLib.LoaderContext context) { var mod = Sys.Library.GetItem(ModID); if (mod == null) { return(null); } string location = System.IO.Path.Combine(Sys.Settings.LibraryLocation, mod.LatestInstalled.InstalledLocation); _7thWrapperLib.ModInfo modinfo = null; if (mod.LatestInstalled.InstalledLocation.EndsWith(".iro")) { using (var arc = new _7thWrapperLib.IrosArc(location)) { if (arc.HasFile("mod.xml")) { var doc = new System.Xml.XmlDocument(); doc.Load(arc.GetData("mod.xml")); modinfo = new _7thWrapperLib.ModInfo(doc, context); } } } else { string mfile = System.IO.Path.Combine(location, "mod.xml"); if (System.IO.File.Exists(mfile)) { modinfo = new _7thWrapperLib.ModInfo(mfile, context); } } modinfo = modinfo ?? new _7thWrapperLib.ModInfo(); foreach (var opt in modinfo.Options) { if (!Settings.Any(s => s.ID.Equals(opt.ID, StringComparison.InvariantCultureIgnoreCase))) { Settings.Add(new ProfileSetting() { ID = opt.ID, Value = opt.Default }); } } return(new _7thWrapperLib.RuntimeMod( location, modinfo.Conditionals.Where(f => IsActive(f.ActiveWhen)), modinfo.ModFolders.Where(f => IsActive(f.ActiveWhen)).Select(f => f.Folder), modinfo )); }
public bool HasFile(string name) { string path = Path.Combine(Sys.Settings.LibraryLocation, InstalledLocation); if (InstalledLocation.EndsWith(".iro", StringComparison.InvariantCultureIgnoreCase) && File.Exists(path)) { using (var arc = new _7thWrapperLib.IrosArc(path)) return(arc.HasFile(name)); } else { path = Path.Combine(path, name); return(File.Exists(path)); } }
public Stream GetData(string name) { string path = Path.Combine(Sys.Settings.LibraryLocation, InstalledLocation); if (InstalledLocation.EndsWith(".iro", StringComparison.InvariantCultureIgnoreCase) && File.Exists(path)) { using (var arc = new _7thWrapperLib.IrosArc(path)) return(arc.GetData(name)); } else { path = Path.Combine(path, name); if (File.Exists(path)) { return(new FileStream(path, FileMode.Open, FileAccess.Read)); } } return(null); }
public void ApplyPatch(IrosArc patch, Action <double, string> onProgress) { int currentDirSize = _entries.Sum(e => e.GetSize()); byte[] deldata = patch.GetBytes("%IrosPatch:Deleted"); if (deldata != null) { string[] delfile = System.Text.Encoding.Unicode.GetString(deldata).Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string del in delfile) { RuntimeLog.Write("Removing file {0} from archive", del); _entries.RemoveAll(e => e.Filename.Equals(del, StringComparison.InvariantCultureIgnoreCase)); } onProgress(0, "Removed " + delfile.Length + " deleted files"); } int count = 0; var files = patch.AllFileNames().Where(s => !s.StartsWith("%")).ToArray(); foreach (string file in files) { var patchEntry = patch._lookup[file]; byte[] data = new byte[patchEntry.Length]; patch._data.Position = patchEntry.Offset; patch._data.Read(data, 0, data.Length); if (HasFile(file)) //update existing { RuntimeLog.Write("File {0} is already in archive...", file); DirectoryEntry exist = _lookup[file]; if (exist.Length >= data.Length) //put data in same position, woo { RuntimeLog.Write("...updating in place"); _data.Position = exist.Offset; } else //stick at end of file { _data.Position = _data.Length; exist.Offset = _data.Position; RuntimeLog.Write("...size increase: writing to end of file"); } _data.Write(data, 0, data.Length); exist.Length = data.Length; exist.Flags = patchEntry.Flags; } else //new file, just append { RuntimeLog.Write("File {0} is new, appending", file); DirectoryEntry de = new DirectoryEntry() { Filename = file, Flags = patchEntry.Flags, Length = patchEntry.Length, Offset = _data.Length }; _data.Position = de.Offset; _data.Write(data, 0, data.Length); _entries.Add(de); _lookup[file] = de; } count++; onProgress(1.0 * count / files.Length, "Processed " + file); } int newDirSize = _entries.Sum(e => e.GetSize()); if (newDirSize <= currentDirSize) { RuntimeLog.Write("Directory will fit in existing location"); _data.Position = _header.Directory; } else { RuntimeLog.Write("Directory size increase, appending"); if (_data.Length >= int.MaxValue) //write forwarder { _data.Position = _header.Directory; _data.WriteInt(-1); _data.WriteLong(_data.Length); _data.Position = _data.Length; } else //write direct location { _header.Directory = (int)_data.Length; _data.Position = _header.Directory; } } _data.WriteInt(_entries.Count); foreach (var e in _entries) { e.Save(_data); } _header.Version = MAX_VERSION; _data.Position = 0; _header.Save(_data); onProgress(1.0, "Wrote directory"); } //TODO: track blank spaces in file and reuse where possible...