public ModViewModel(ModData model) { this.model = model; }
ModData LoadModFromZip(string zipFileFullName) { log.Debug("Loading '{0}'", Path.GetFileName(zipFileFullName)); ModData detail = new ModData(); string tempDir = Path.Combine(Path.GetTempPath(), "mc", Guid.NewGuid().ToString()); detail.FullPath = zipFileFullName; detail.FileSize = new FileInfo(zipFileFullName).Length; detail.Hash = new FileInfo(zipFileFullName).CalcMD5(); // Fallback title detail.Title = Path.GetFileNameWithoutExtension(detail.FileName).ToLowerInvariant(); detail.Title = detail.Title.ReplaceEx(new string[] { "aaa_", "zzz_", "aaa", "zzz" }, string.Empty); // Make any word begin with upercase letter detail.Title = Regex.Replace(detail.Title, @"((?<=\p{Ll})\p{Lu})|((?!\A)\p{Lu}(?>\p{Ll}))", " $0"); // Remove duplicate spaces detail.Title.Normalize(); try { Directory.CreateDirectory(tempDir); if (Unzip(zipFileFullName, tempDir, new string[] { MOD_DESC_FILENAME }, true, true)) { StringBuilder sb = new StringBuilder(); // Clear comments, cause they are often not wellformed foreach (string line in File.ReadAllLines(Path.Combine(tempDir, MOD_DESC_FILENAME))) { string currentLine = new Regex("(?s)<!--.*?-->", RegexOptions.Compiled).Replace(line, ""); currentLine = new Regex("[&]+", RegexOptions.Compiled).Replace(line, ""); if (string.IsNullOrEmpty(currentLine)) continue; sb.AppendLine(currentLine.Trim()); } try { XmlReaderSettings settings = new XmlReaderSettings(); using (TextReader txtReader = new StringReader(sb.ToString())) using (XmlReader reader = XmlReader.Create(txtReader, settings)) { XmlDocument doc = new XmlDocument(); doc.Load(reader); detail.Title = doc.GetInnerTextNormalized(string.Format("/modDesc/title/{0}", "de")); if (string.IsNullOrEmpty(detail.Title)) detail.Title = doc.GetInnerTextNormalized("modDesc/title/en"); detail.Description = doc.GetInnerTextNormalized(string.Format("/modDesc/description/{0}", "de")); if (string.IsNullOrEmpty(detail.Description)) detail.Description = doc.GetInnerTextNormalized("modDesc/description/en"); detail.Author = doc.GetInnerTextNormalized("/modDesc/author"); detail.Version = doc.GetInnerTextNormalized("/modDesc/version"); detail.IsMultiplayerReady = Convert.ToBoolean(doc.GetInnerTextSave("/modDesc/multiplayer/@supported")); //lock (locker) //{ //detail.Image = LoadBitmap(zipFileFullName, tempDir, doc.GetInnerTextSave("/modDesc/iconFilename")); //} foreach (XmlNode node in doc.SelectNodes("/modDesc/storeItems/storeItem")) { StoreItemData shopItem = new StoreItemData(); shopItem.Name = node.GetInnerTextSave(string.Format("{0}/name", "de")); if (string.IsNullOrEmpty(shopItem.Name)) shopItem.Name = node.GetInnerTextSave("en/name"); shopItem.Price = Convert.ToDouble(node.GetInnerTextSave("price")); shopItem.Upkeep = Convert.ToDouble(node.GetInnerTextSave("dailyUpkeep")); shopItem.MachineType = node.GetInnerTextSave("machineType"); shopItem.Brand = node.GetInnerTextSave("brand"); shopItem.Description = node.GetInnerTextSave(string.Format("{0}/description", "de")); if (string.IsNullOrEmpty(shopItem.Description)) shopItem.Description = node.GetInnerTextSave("en/description"); shopItem.Description = shopItem.Description.Replace("\r\n", " "); //shopItem.IsMultiplayerReady = Convert.ToBoolean(node.GetInnerTextSave("/modDesc/multiplayer/@supported")); shopItem.XmlFileName = node.GetInnerTextSave("xmlFilename"); shopItem.ImagePath = node.GetInnerTextSave("image/@active"); shopItem.BrandImagePath = node.GetInnerTextSave("image/@brand"); lock (locker) { if (detail.Image == null) { detail.Image = LoadBitmap(zipFileFullName, tempDir, shopItem.ImagePath); shopItem.Image = detail.Image; } else { shopItem.Image = LoadBitmap(zipFileFullName, tempDir, shopItem.ImagePath); } shopItem.BrandImage = LoadBitmap(zipFileFullName, tempDir, shopItem.BrandImagePath); } detail.StoreItems.Add(shopItem); } doc = null; } } catch (Exception ex) { log.Error("Error while loading {0}".Args(zipFileFullName), ex); } if (detail.Image == null) { detail.Image = null;//Resources.NoPicture; } } } catch (Exception ex) { log.Error("Error while loadings mod: " + zipFileFullName, ex); } finally { Directory.Delete(tempDir, true); } return detail; }