Skip to content

Commit

Permalink
Fixed a nasty bug with JSON deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
sas41 committed Dec 25, 2019
1 parent ce2a342 commit e89b877
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
32 changes: 23 additions & 9 deletions PACTConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,40 @@
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using Newtonsoft.Json;

namespace ProcessAffinityControlTool
{
class PACTConfig
{
// Process name -> Affinity Mask
[JsonProperty]
public Dictionary<string, ProcessConfig> ProcessConfigs { get; set; }

public ProcessConfig Default { get; set; }
[JsonProperty]
public ProcessConfig DefaultConfig { get; set; }
[JsonProperty]
public int ScanInterval { get; set; }
[JsonProperty]
public int AggressiveScanInterval { get; set; }
[JsonProperty]
public bool ForceAggressiveScan { get; set; }

public PACTConfig()
public PACTConfig(Dictionary<string, ProcessConfig> processConfigs = null, ProcessConfig defaultConfig = null, int scanInterval = 3000, int aggressiveScanInterval = 3, bool forceAggressiveScan = false)
{
ProcessConfigs = new Dictionary<string, ProcessConfig>();
Default = new ProcessConfig(Enumerable.Range(0, Environment.ProcessorCount).ToList(), 2);
ScanInterval = 3000;
AggressiveScanInterval = 3;
ForceAggressiveScan = false;
ProcessConfigs = processConfigs;
if (processConfigs == null)
{
ProcessConfigs = new Dictionary<string, ProcessConfig>();
}

DefaultConfig = defaultConfig;
if (defaultConfig == null)
{
DefaultConfig = new ProcessConfig(Enumerable.Range(0, Environment.ProcessorCount).ToList(), 2);
}

ScanInterval = scanInterval;
AggressiveScanInterval = aggressiveScanInterval;
ForceAggressiveScan = forceAggressiveScan;
}

}
Expand Down
7 changes: 6 additions & 1 deletion ProcessConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand All @@ -8,10 +9,14 @@ namespace ProcessAffinityControlTool
{
class ProcessConfig
{
[JsonProperty]
public ProcessPriorityClass Priority { get; set; }
[JsonProperty]
public long AffinityMask { get; set; }

[JsonProperty]
public List<int> CoreList { get; private set; }
[JsonProperty]
public int PriorityNumber { get; private set; }

public ProcessConfig()
Expand Down
4 changes: 2 additions & 2 deletions ProcessOverwatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private void HandleProcess(Process process)
}
else
{
mask = (IntPtr)Config.Default.AffinityMask;
priority = Config.Default.Priority;
mask = (IntPtr)Config.DefaultConfig.AffinityMask;
priority = Config.DefaultConfig.Priority;
}

// Set Process Affinity
Expand Down
23 changes: 16 additions & 7 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static void Main(string[] args)
conf = ReadConfig();
pausedConf = new PACTConfig();

pow.Config = conf;
pow.SetTimer();


Expand Down Expand Up @@ -70,11 +71,11 @@ static void Main(string[] args)
else if (arguments[0] == "default_cores" || arguments[0] == "dc")
{
List<int> cores = arguments.Skip(1).Select(x => int.Parse(x)).ToList();
conf.Default = new ProcessConfig(cores, conf.Default.PriorityNumber);
conf.DefaultConfig = new ProcessConfig(cores, conf.DefaultConfig.PriorityNumber);
}
else if (arguments[0] == "default_priority" || arguments[0] == "dp")
{
conf.Default = new ProcessConfig(conf.Default.CoreList, int.Parse(arguments[1]));
conf.DefaultConfig = new ProcessConfig(conf.DefaultConfig.CoreList, int.Parse(arguments[1]));
}
else if (arguments[0] == "scan_interval" || arguments[0] == "si")
{
Expand Down Expand Up @@ -106,7 +107,7 @@ static void Main(string[] args)
else if (arguments[0] == "show_defaults" || arguments[0] == "sd")
{
Console.WriteLine("Defaults:");
Console.WriteLine(conf.Default);
Console.WriteLine(conf.DefaultConfig);
}
else if (arguments[0] == "show_exceptions" || arguments[0] == "se")
{
Expand Down Expand Up @@ -178,11 +179,19 @@ static void ApplyDefaultConfig()
static PACTConfig ReadConfig()
{
string path = AppDomain.CurrentDomain.BaseDirectory;
string configPath = path + "/config.json";
string configPath = path + "config.json";
Console.WriteLine();
Console.WriteLine($"Looking for config at: [{configPath}]...");
if (File.Exists(configPath))
{
string json = File.ReadAllText(configPath);
return JsonConvert.DeserializeObject<PACTConfig>(json);
Console.WriteLine("Config found!");
PACTConfig tconf = JsonConvert.DeserializeObject<PACTConfig>(json);
return tconf;
}
else
{
Console.WriteLine("Config not found, using initial defaults...");
}

return new PACTConfig();
Expand All @@ -192,14 +201,14 @@ static void SaveConfig(PACTConfig conf)
{
string json = JsonConvert.SerializeObject(conf, Formatting.Indented);
string path = AppDomain.CurrentDomain.BaseDirectory;
string configPath = path + "/config.json";
string configPath = path + "config.json";
File.WriteAllText(configPath, json);
}

static void ShowHelp()
{
string path = AppDomain.CurrentDomain.BaseDirectory;
string textPath = path + "/help.txt";
string textPath = path + "help.txt";
if (File.Exists(textPath))
{
string text = File.ReadAllText(textPath);
Expand Down

0 comments on commit e89b877

Please sign in to comment.