public bool SaveModState() { if (!IsReadAddonList) { Logging.Log("Try read addon list"); ReadAddonList(m_path); if (!IsReadAddonList) { Logging.Log("Some problem here: can not read the list"); return(false); } } try { if (!Backup) { FileInfo bak = new FileInfo(m_path + m_fileList); if (bak.Exists) { bak.CopyTo(m_path + m_fileList + ".bak", true); } Backup = true; } StringBuilder sb = new StringBuilder(); sb.Append("\"AddonList\"\n{\n"); L4D2TxtReader reader = new L4D2TxtReader(m_path + m_fileList, L4D2TxtReader.TxtType.AddonList); foreach (var v in reader.Values) { var value = v.Value; if (m_modStates.ContainsKey(v.Key)) { if (m_modStates[v.Key].ModState == ModState.On || m_modStates[v.Key].ModState == ModState.Off) { value = (m_modStates[v.Key].ModState == ModState.On ? "1" : "0"); } } sb.Append("\t\"" + v.Key + "\"\t\t\"" + value + "\"\n"); } foreach (var v in m_modStates) { if (reader.Values.FindIndex(match => match.Key.Equals(v.Key)) < 0) { if (v.Value.ModState == ModState.On || v.Value.ModState == ModState.Off) { sb.Append("\t\"" + v.Key + "\"\t\t\"" + (v.Value.ModState == ModState.On ? '1' : '0') + "\"\n"); } } } sb.Append("}\n"); File.WriteAllText(m_path + '\\' + m_fileList, sb.ToString()); Logging.Log("save mod state to file : " + m_path + '\\' + m_fileList); } catch (Exception e) { Logging.Log(e.Message, "CATCH"); return(false); } return(true); }
static void HandleAddonInfoTxt(L4D2Mod o, SharpVPK.VpkEntry entry) { bool AddDescription = o.Description.Length <= 0; MemoryStream ms = new MemoryStream(); byte[] data = entry.Data; ms.Write(data, 0, data.Length); ms.Seek(0, SeekOrigin.Begin); L4D2TxtReader reader = new L4D2TxtReader(ms, L4D2TxtReader.TxtType.AddonInfo); foreach (var item in reader.Values) { var key = item.Key.ToLower(); if (key.Contains("title") && o.Title.Length <= 0) { o.Title = item.Value.Replace('\r', ' ').Replace('\n', ' '); } else if (key.Contains("author") && o.Author.Length <= 0) { o.Author = item.Value; } else if (key.Contains("description") && AddDescription) { o.Description = o.Description + item.Value + "\r\n"; } else if (key.Contains("version") && o.Version.Length <= 0) { o.Version = item.Value; } else if (key.Contains("url") && o.URL.Length <= 0) { o.URL = item.Value; } else if (key.Contains("ownerid") && o.OwnerId <= 0) { try { o.OwnerId = Convert.ToUInt64(item.Value); } catch { } } } }
private void ReadAddonList(string path) { if (path != null && path != "") { L4D2TxtReader reader = new L4D2TxtReader(path + m_fileList, L4D2TxtReader.TxtType.AddonList); ModInfo nullModeInfo = new ModInfo(this, null, ModState.Miss, ModSource.Player, null); nullModeInfo.ModState = ModState.Miss; //update state of mods foreach (var v in reader.Values) { if (m_modStates.ContainsKey(v.Key)) { if (v.Value == "0") { //m_modStates[v.Key].ModState = (v.Value == "1" ? ModState.On : ModState.Off); m_modStates[v.Key].SetOff(); } } //else // m_modStates.Add(v.Key, nullModeInfo); } IsReadAddonList = true; } }