예제 #1
0
        public bool ReadAsBinary(Stream input)
        {
            var dummyChild = new SteamKeyValue();

            this.Children.Add(dummyChild);
            return(dummyChild.TryReadAsBinary(input));
        }
예제 #2
0
        public SteamApp(SteamLibrary library, string manifestFile)
        {
            OriginalLibrary = library;
            TargetLibrary   = library;

            string        manifestPath = Path.Combine(library.Path, manifestFile);
            SteamKeyValue manifest     = SteamKeyValue.LoadFromFile(manifestPath);

            // Read base settings.
            Id         = manifest["AppID"].AsString();
            Name       = manifest["name"].AsString();
            InstallDir = manifest["installdir"].AsString();

            // Override with user settings.
            string userSpecificName = manifest["UserConfig"]["name"].Value;

            if (!string.IsNullOrWhiteSpace(userSpecificName))
            {
                Name = userSpecificName;
            }

            // Calculate disk size.
            string        installPath      = Path.Combine(library.Path, "common", InstallDir);
            DirectoryInfo installDirectory = new DirectoryInfo(installPath);

            Size = installDirectory.EnumerateFiles("*.*", SearchOption.AllDirectories).Aggregate(0L, (s, f) => s += f.Length);
        }
예제 #3
0
        public SteamKeyValueReader(SteamKeyValue kv, Stream input)
            : base(input)
        {
            bool wasQuoted;
            bool wasConditional;

            SteamKeyValue currentKey = kv;

            do
            {
                // bool bAccepted = true;

                string s = ReadToken(out wasQuoted, out wasConditional);

                if (string.IsNullOrEmpty(s))
                {
                    break;
                }

                if (currentKey == null)
                {
                    currentKey = new SteamKeyValue(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);
        }
예제 #4
0
        public static SteamKeyValue LoadAsBinary(string path)
        {
            var kv = LoadFromFile(path, true);

            if (kv == null)
            {
                return(null);
            }

            var parent = new SteamKeyValue();

            parent.Children.Add(kv);
            return(parent);
        }
예제 #5
0
        /// <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 SteamKeyValue LoadFromString(string input)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(input);

            using (MemoryStream stream = new MemoryStream(bytes))
            {
                var kv = new SteamKeyValue();

                try
                {
                    if (kv.ReadAsText(stream) == false)
                    {
                        return(null);
                    }

                    return(kv);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
예제 #6
0
        public static SteamKeyValue LoadFromFile(string path, bool asBinary = false)
        {
            if (File.Exists(path) == false)
            {
                return(null);
            }

            try
            {
                using (var input = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var kv = new SteamKeyValue();

                    if (asBinary)
                    {
                        if (kv.TryReadAsBinary(input) == false)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        if (kv.ReadAsText(input) == false)
                        {
                            return(null);
                        }
                    }

                    return(kv);
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
예제 #7
0
        static bool TryReadAsBinaryCore(Stream input, SteamKeyValue current, SteamKeyValue parent)
        {
            current.Children = new List <SteamKeyValue>();

            while (true)
            {
                var type = (Type)input.ReadByte();

                if (type == Type.End)
                {
                    break;
                }

                current.Name = input.ReadNullTermString(Encoding.UTF8);

                switch (type)
                {
                case Type.None:
                {
                    var child        = new SteamKeyValue();
                    var didReadChild = TryReadAsBinaryCore(input, child, current);
                    if (!didReadChild)
                    {
                        return(false);
                    }
                    break;
                }

                case Type.String:
                {
                    current.Value = input.ReadNullTermString(Encoding.UTF8);
                    break;
                }

                case Type.WideString:
                {
                    //DebugLog.WriteLine("KeyValue", "Encountered WideString type when parsing binary KeyValue, which is unsupported. Returning false.");
                    return(false);
                }

                case Type.Int32:
                case Type.Color:
                case Type.Pointer:
                {
                    current.Value = Convert.ToString(input.ReadInt32());
                    break;
                }

                case Type.UInt64:
                {
                    current.Value = Convert.ToString(input.ReadUInt64());
                    break;
                }

                case Type.Float32:
                {
                    current.Value = Convert.ToString(input.ReadFloat());
                    break;
                }

                default:
                {
                    return(false);
                }
                }

                if (parent != null)
                {
                    parent.Children.Add(current);
                }
                current = new SteamKeyValue();
            }

            return(true);
        }
예제 #8
0
        internal void RecursiveLoadFromBuffer(SteamKeyValueReader 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;
                }

                SteamKeyValue dat = new SteamKeyValue(name);
                dat.Children = new List <SteamKeyValue>();
                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
                }
            }
        }
예제 #9
0
 /// <summary>
 /// Attempts to load the given filename as a binary <see cref="KeyValue"/>.
 /// </summary>
 /// <param name="path">The path to the file to load.</param>
 /// <param name="keyValue">The resulting <see cref="KeyValue"/> object if the load was successful, or <c>null</c> if unsuccessful.</param>
 /// <returns><c>true</c> if the load was successful, or <c>false</c> on failure.</returns>
 public static bool TryLoadAsBinary(string path, out SteamKeyValue keyValue)
 {
     keyValue = LoadFromFile(path, true);
     return(keyValue != null);
 }
예제 #10
0
        public SteamData(string installDir, ProgressChangedEventHandler onLoadProgerss = null)
        {
            this.installDir = installDir;

            // Init the main library.
            Libraries = new List <SteamLibrary>();
            Libraries.Add(new SteamLibrary(installDir));

            // Init additional libraries.
            try
            {
                SteamKeyValue libraryConfig = SteamKeyValue.LoadFromFile(Path.Combine(installDir, "steamapps\\libraryfolders.vdf"));

                for (int i = 1; ; ++i)
                {
                    SteamKeyValue libraryData = libraryConfig[i.ToString()];
                    if (libraryData != SteamKeyValue.Invalid)
                    {
                        Libraries.Add(new SteamLibrary(libraryData.Value));
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch
            {
                // TODO: Process exception.
            }

            // Read applications information.
            int appCount  = Libraries.Aggregate(0, (count, lib) => count + Directory.GetFiles(lib.Path, "appmanifest_*.acf").Count());
            int appLoaded = 0;

            Apps = new List <SteamApp>();
            if (onLoadProgerss != null)
            {
                onLoadProgerss(0);
            }

            foreach (SteamLibrary library in Libraries)
            {
                foreach (string manifestPath in Directory.GetFiles(library.Path, "appmanifest_*.acf"))
                {
                    SteamApp app = new SteamApp(library, System.IO.Path.GetFileName(manifestPath));
                    app.TargetLibraryChanged += a => { if (AppTargetLibraryChanged != null)
                                                       {
                                                           AppTargetLibraryChanged(app);
                                                       }
                    };
                    Apps.Add(app);

                    appLoaded += 1;
                    if (onLoadProgerss != null)
                    {
                        onLoadProgerss((int)((float)appLoaded / (float)appCount * 100f));
                    }
                }
            }
        }