public string DownloadImage(string folder) { string previewPath = null; if (ImageFullPath != null && !ImageFullPath.EndsWith("modconstruction.jpg")) { try { previewPath = Path.Combine(folder, string.Format("{0}{1}", SettingsDataProvider.GetSafeFilename(Name), Path.GetExtension(ImageFullPath))); TrovesaurusApi.DownloadFile(ImageFullPath, previewPath); } catch (Exception ex) { log.Warn(string.Format("Error downloading image {0}, retrying with new filename", Path.GetFileName(previewPath)), ex); int i = 1; while (File.Exists(previewPath)) { previewPath = Path.Combine(folder, string.Format("{0} {1}{2}", SettingsDataProvider.GetSafeFilename(Name), i++, Path.GetExtension(ImageFullPath))); } try { TrovesaurusApi.DownloadFile(ImageFullPath, previewPath); } catch (Exception e) { log.Error(string.Format("Error downloading image {0}", Path.GetFileName(previewPath)), e); previewPath = null; } } } return(previewPath); }
private string GetTmodFilePath(TroveLocation loc) { string modsFolder = SettingsDataProvider.ResolveFolder(Path.Combine(loc.LocationPath, ModsFolder)); return(Path.Combine(modsFolder, string.Format("{0}.tmod", SettingsDataProvider.GetSafeFilename(ModTitle)))); }
public static void ExtractTmod(string file, string folder, bool createOverrideFolders, bool createYaml, Action <double> updateProgress) { var buffer = new byte[1048576]; var properties = new Dictionary <string, string>(); var archiveEntries = new List <ArchiveIndexEntry>(); ulong headerSize = 0; using (var stream = File.OpenRead(file)) { using (var reader = new MyBinaryReader(stream)) { headerSize = ReadProperties(properties, stream, reader); // Read archive index entries (remainder of header) while ((ulong)stream.Position < headerSize) { var entry = new ArchiveIndexEntry(); entry.file = reader.ReadString(); entry.archiveIndex = reader.Read7BitEncodedInt(); entry.byteOffset = reader.Read7BitEncodedInt(); entry.size = reader.Read7BitEncodedInt(); entry.hash = reader.Read7BitEncodedInt(); archiveEntries.Add(entry); } int offset = 0, byteRead = 0; double count = 0; if (stream.Position != (long)headerSize) { stream.Position = (long)headerSize; } using (InflaterInputStream decompressionStream = new InflaterInputStream(stream)) { foreach (var entry in archiveEntries.OrderBy(e => e.byteOffset)) { updateProgress(count / archiveEntries.Count * 100d); log.InfoFormat("Extracting {0}", entry.file); string extractPath = Path.Combine(folder, entry.file.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)); if (createOverrideFolders) { extractPath = Path.Combine(Path.GetDirectoryName(extractPath), TroveMod.OverrideFolder, Path.GetFileName(extractPath)); } SettingsDataProvider.ResolveFolder(Path.GetDirectoryName(extractPath)); // Advance data position to the next entry offset if needed while (offset < entry.byteOffset && byteRead != -1) { byteRead = decompressionStream.ReadByte(); offset++; } offset += SaveBytes(extractPath, stream, decompressionStream, Convert.ToInt64(headerSize) + Convert.ToInt64(entry.byteOffset), entry.size, buffer); } } } } try { if (createYaml) { string title = properties.ContainsKey(TitleValue) ? properties[TitleValue] : Path.GetFileNameWithoutExtension(file); string yamlPath = Path.Combine(folder, SettingsDataProvider.GetSafeFilename(title) + ".yaml"); log.InfoFormat("Generating YAML file: {0}", yamlPath); ModDetails details = new ModDetails() { Author = properties[AuthorValue], Title = properties[TitleValue], Notes = properties[NotesValue], PreviewPath = properties[PreviewPathValue], Files = archiveEntries.Select(e => e.file).ToList() }; if (properties.ContainsKey(TagsValue)) { string tags = properties[TagsValue]; if (!string.IsNullOrWhiteSpace(tags)) { details.Tags = tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); } } details.SaveYamlFile(yamlPath); } } catch (Exception ex) { log.Error("Error generating YAML file", ex); } log.InfoFormat("Completed extracting files from {0}", file); }