Esempio n. 1
0
        public static async Task<Rules> LoadFromAppDataAsync () {
            var d20Rules = new D20Rules() { Rules = new Rules() };
            var appPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var basePath = Path.Combine(appPath, "CBLoader");
            var rulesPath = Path.Combine(basePath, "combined.dnd40");

            if (!File.Exists(rulesPath)) {
                return d20Rules.Rules;
            }

            Stopwatch timer = Stopwatch.StartNew();
            try {
                d20Rules = await Task.Run<D20Rules>(() => {
                    XmlSerializer serializer = new XmlSerializer(typeof(D20Rules));
                    using (var file = new FileStream(rulesPath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, useAsync: true)) {
                        using (var reader = new XmlTextReader(file)) {
                            if (serializer.CanDeserialize(reader)) {
                                return serializer.Deserialize(reader) as D20Rules;
                            }
                        }
                    }
                    return d20Rules;
                }).ConfigureAwait(false);
            }
            finally {
                timer.Stop();
                Trace.TraceInformation("Deserializing D20Rules took {0}ms", timer.ElapsedMilliseconds);
            }

            return d20Rules.Rules;
        }
Esempio n. 2
0
        public static async Task <Rules> LoadFromAppDataAsync()
        {
            var d20Rules = new D20Rules()
            {
                Rules = new Rules()
            };
            var appPath   = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var basePath  = Path.Combine(appPath, "CBLoader");
            var rulesPath = Path.Combine(basePath, "combined.dnd40");

            if (!File.Exists(rulesPath))
            {
                return(d20Rules.Rules);
            }

            Stopwatch timer = Stopwatch.StartNew();

            try {
                d20Rules = await Task.Run <D20Rules>(() => {
                    XmlSerializer serializer = new XmlSerializer(typeof(D20Rules));
                    using (var file = new FileStream(rulesPath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, useAsync: true)) {
                        using (var reader = new XmlTextReader(file)) {
                            if (serializer.CanDeserialize(reader))
                            {
                                return(serializer.Deserialize(reader) as D20Rules);
                            }
                        }
                    }
                    return(d20Rules);
                }).ConfigureAwait(false);
            }
            finally {
                timer.Stop();
                Trace.TraceInformation("Deserializing D20Rules took {0}ms", timer.ElapsedMilliseconds);
            }

            return(d20Rules.Rules);
        }