A game framework written with osu! in mind.
at master 2.4 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using System.Collections.Generic; 6using System.IO; 7using Newtonsoft.Json; 8using osu.Framework.Input.Handlers; 9using osu.Framework.Logging; 10using osu.Framework.Platform; 11 12#nullable enable 13 14namespace osu.Framework.Configuration 15{ 16 [Serializable] 17 public class InputConfigManager : ConfigManager 18 { 19 public const string FILENAME = "input.json"; 20 21 private readonly Storage storage; 22 23 [JsonConverter(typeof(TypedRepopulatingConverter<InputHandler>))] 24 public IReadOnlyList<InputHandler> InputHandlers { get; set; } 25 26 public InputConfigManager(Storage storage, IReadOnlyList<InputHandler> inputHandlers) 27 { 28 this.storage = storage; 29 InputHandlers = inputHandlers; 30 31 Load(); 32 } 33 34 protected override bool PerformSave() 35 { 36 try 37 { 38 using (var stream = storage.GetStream(FILENAME, FileAccess.Write, FileMode.Create)) 39 using (var sw = new StreamWriter(stream)) 40 { 41 sw.Write(JsonConvert.SerializeObject(this)); 42 return true; 43 } 44 } 45 catch (Exception e) 46 { 47 Logger.Error(e, "Error occurred when saving input configuration"); 48 } 49 50 return false; 51 } 52 53 protected override void PerformLoad() 54 { 55 if (storage.Exists(FILENAME)) 56 { 57 try 58 { 59 using (Stream stream = storage.GetStream(FILENAME, FileAccess.Read, FileMode.Open)) 60 using (var sr = new StreamReader(stream)) 61 { 62 JsonConvert.PopulateObject(sr.ReadToEnd(), this, new JsonSerializerSettings 63 { 64 ObjectCreationHandling = ObjectCreationHandling.Reuse, 65 DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate 66 }); 67 } 68 } 69 catch (Exception e) 70 { 71 Logger.Error(e, "Error occurred when parsing input configuration"); 72 } 73 } 74 } 75 } 76}