public CsgoItemsGameFileParser(string filePath) { _csgoKvFile = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); _itemGamesKv = new KeyValue(String.Empty, String.Empty); IsValid = _itemGamesKv.ReadAsText(_csgoKvFile); }
public CsgoItemsGameFileParser(string itemsGameFilePath, string localizationTextFilePath) { _csgoKvFile = new FileStream(itemsGameFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); _locKvFile = new FileStream(localizationTextFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); _itemGamesKv = new KeyValue(string.Empty, string.Empty); _locFile = new KeyValue(string.Empty, string.Empty); IsValid = _itemGamesKv.ReadAsText(_csgoKvFile); _locFile.ReadAsText(_locKvFile); }
public KVTextReader(KeyValue kv, Stream input) : base(input) { bool wasQuoted; bool wasConditional; KeyValue currentKey = kv; do { // bool bAccepted = true; string s = ReadToken(out wasQuoted, out wasConditional); if (string.IsNullOrEmpty(s)) break; if (currentKey == null) { currentKey = new KeyValue(s); } else { currentKey.Name = s; } s = ReadToken(out wasQuoted, out wasConditional); if (wasConditional) { // bAccepted = ( s == "[$WIN32]" ); // Now get the '{' s = ReadToken(out wasQuoted, out wasConditional); } if (s.StartsWith("{") && !wasQuoted) { // header is valid so load the file currentKey.RecursiveLoadFromBuffer(this); } else { throw new Exception("LoadFromBuffer: missing {"); } currentKey = null; } while (!EndOfStream); }
static KeyValue LoadFromFile(string path, bool asBinary) { if (File.Exists(path) == false) { return null; } try { using (var input = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var kv = new KeyValue(); if (asBinary) { if (kv.ReadAsBinary(input) == false) { return null; } } else { if (kv.ReadAsText(input) == false) { return null; } } return kv; } } catch (Exception) { return null; } }
internal void RecursiveLoadFromBuffer(KVTextReader kvr) { bool wasQuoted; bool wasConditional; while (true) { // bool bAccepted = true; // get the key name string name = kvr.ReadToken(out wasQuoted, out wasConditional); if (string.IsNullOrEmpty(name)) { throw new Exception("RecursiveLoadFromBuffer: got EOF or empty keyname"); } if (name.StartsWith("}") && !wasQuoted) // top level closed, stop reading break; KeyValue dat = new KeyValue(name); dat.Children = new List<KeyValue>(); this.Children.Add(dat); // get the value string value = kvr.ReadToken(out wasQuoted, out wasConditional); if (wasConditional && value != null) { // bAccepted = ( value == "[$WIN32]" ); value = kvr.ReadToken(out wasQuoted, out wasConditional); } if (value == null) throw new Exception("RecursiveLoadFromBuffer: got NULL key"); if (value.StartsWith("}") && !wasQuoted) throw new Exception("RecursiveLoadFromBuffer: got } in key"); if (value.StartsWith("{") && !wasQuoted) { dat.RecursiveLoadFromBuffer(kvr); } else { if (wasConditional) { throw new Exception("RecursiveLoadFromBuffer: got conditional between key and value"); } dat.Value = value; // blahconditionalsdontcare } } }
/// <summary> /// Populate this instance from the given <see cref="Stream"/> as a binary <see cref="KeyValue"/>. /// </summary> /// <param name="input">The input <see cref="Stream"/> to read from.</param> /// <returns><c>true</c> if the read was successful; otherwise, <c>false</c>.</returns> public bool ReadAsBinary(Stream input) { this.Children = new List<KeyValue>(); DataStream inputEnhanced = input as DataStream; while (true) { var type = (Type)input.ReadByte(); if (type == Type.End) { break; } var current = new KeyValue(); current.Name = inputEnhanced.ReadNullTermString(Encoding.UTF8); try { switch (type) { case Type.None: { current.ReadAsBinary(input); break; } case Type.String: { current.Value = inputEnhanced.ReadNullTermString(Encoding.UTF8); break; } case Type.WideString: { throw new InvalidDataException("wstring is unsupported"); } case Type.Int32: case Type.Color: case Type.Pointer: { current.Value = Convert.ToString(inputEnhanced.ReadInt32()); break; } case Type.UInt64: { current.Value = Convert.ToString(inputEnhanced.ReadUInt64()); break; } case Type.Float32: { current.Value = Convert.ToString(inputEnhanced.ReadFloat()); break; } default: { throw new InvalidDataException("Unknown KV type encountered."); } } } catch (InvalidDataException ex) { throw new InvalidDataException(string.Format("An exception ocurred while reading KV '{0}'", current.Name), ex); } this.Children.Add(current); } return input.Position == input.Length; }
/// <summary> /// Attempts to create an instance of <see cref="KeyValue"/> from the given input text. /// </summary> /// <param name="input">The input text to load.</param> /// <returns>a <see cref="KeyValue"/> instance if the load was successful, or <c>null</c> on failure.</returns> /// <remarks> /// This method will swallow any exceptions that occur when reading, use <see cref="ReadAsText"/> if you wish to handle exceptions. /// </remarks> public static KeyValue LoadFromString(string input) { byte[] bytes = Encoding.UTF8.GetBytes(input); using (MemoryStream stream = new MemoryStream(bytes)) { var kv = new KeyValue(); try { if (kv.ReadAsText(stream) == false) return null; return kv; } catch (Exception) { return null; } } }