Example #1
0
        private T ProcessSetting <T>(RawSettings settings, string settingsKey, T defaultValue, Func <string, T> parser)
        {
            T   t;
            var defaultValueString = defaultValue.ToString();

            if (!settings.ContainsKey(settingsKey))
            {
                settings.Add(settingsKey, defaultValueString);
                t = defaultValue;
            }
            else
            {
                try
                {
                    t = parser(settings.GetItem <string>(settingsKey));
                }
                catch (Exception)
                {
                    _logger.Warning(
                        $"[Heat] Invalid '{settingsKey}' setting specified; defaulting to '{defaultValueString}'");
                    settings.Remove(settingsKey);
                    settings.Add(settingsKey, defaultValueString);
                    t = defaultValue;
                }
            }

            return(t);
        }
Example #2
0
        public void Initialize(IManager manager, string ipcIdentifier)
        {
            POVs = new Dictionary <string, Texture2D>();

            FileSystem = new FileSystem();
            Settings   = new Settings("povstatus");
            Logger     = new Logger("pov")
            {
                WriteToConsole = true
            };

            var currentPov = Settings.GetOrCreate("CurrentPOV", string.Empty);

            foreach (var pngFile in FileSystem.GetFiles("."))
            {
                if (!pngFile.EndsWith(".png"))
                {
                    continue;
                }

                var tex = new Texture2D(256, 256);

                using (var fs = FileSystem.OpenFile(pngFile))
                {
                    byte[] b = new byte[0];

                    using (BinaryReader br = new BinaryReader(fs))
                        b = br.ReadBytes((int)fs.Length);

                    tex.LoadImage(b);
                }
                POVs.Add(Path.GetFileNameWithoutExtension(pngFile), tex);
                Logger.Info($"Loaded POV decal '{pngFile}'");
            }

            if (POVs.Count == 0)
            {
                Logger.Warning("No POVs found. Stopping.");
                return;
            }

            if (string.IsNullOrEmpty(currentPov) || !POVs.ContainsKey(currentPov))
            {
                currentPov = POVs.First(x => true).Key;

                Settings["CurrentPOV"] = currentPov;
                Settings.Save();
            }

            var menuTree = new MenuTree("it.32-b.vdd.CustomPOVImageMenu", "Customizable wheel POV")
            {
                new ListBox <Texture2D>(MenuDisplayMode.Both, "it.32-b.vdd.CustomPOVImageMenu.POVs", "Available POVs")
                .WithEntries(POVs)
                .WithGetter(() => POVs[Settings.GetItem <string>("CurrentPOV")])
                .WithSetter((tex) =>
                {
                    foreach (var kvp in POVs)
                    {
                        if (tex == kvp.Value)
                        {
                            Settings["CurrentPOV"] = kvp.Key;
                            break;
                        }
                    }

                    POVChangeTriggered = true;
                })
                .WithDescription("Select one of available wheel POVs.")
            };

            manager.Menus.AddMenu(MenuDisplayMode.Both, menuTree, "Change how your wheels bling.");

            HarmonyInstance = HarmonyInstance.Create("it.32-b.vdd.CustomPOVImage");
            HarmonyInstance.PatchAll(Assembly.GetExecutingAssembly());
        }