-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for white/blacklist modes, add configuration loader, upda…
…te UI
- Loading branch information
Showing
4 changed files
with
157 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace StayFocused | ||
{ | ||
enum Modes | ||
{ | ||
Blacklist, | ||
Whitelist | ||
} | ||
|
||
class Config | ||
{ | ||
private static readonly string[] DEFAULT_CONFIG = { | ||
"# valid modes: blacklist, whitelist", | ||
"[config]", | ||
"mode = whitelist", | ||
"", | ||
"# hook ONLY these programs, when mode = whitelist", | ||
"[whitelist]", | ||
"Photoshop.exe", | ||
"", | ||
"# hook ALL programs except the following, when mode = blacklist", | ||
"[blacklist]", | ||
"explorer.exe", | ||
"svchost.exe", | ||
"taskhostw.exe", | ||
"RuntimeBroker.exe", | ||
"SearchUI.exe", | ||
"ShellExperienceHost.exe", | ||
"chrome.exe", | ||
"conhost.exe", | ||
}; | ||
|
||
public Modes mode = Modes.Whitelist; | ||
public HashSet<string> whitelist; | ||
public HashSet<string> blacklist; | ||
|
||
public Config() { | ||
var filename = GetConfigPath(); | ||
if (!File.Exists(filename)) { | ||
Directory.CreateDirectory(Path.GetDirectoryName(filename)); | ||
File.WriteAllLines(filename, DEFAULT_CONFIG); | ||
} | ||
|
||
var config = File.ReadAllLines(filename); | ||
whitelist = new HashSet<string>(); | ||
blacklist = new HashSet<string>(); | ||
ParseConfig(config); | ||
} | ||
|
||
public string GetConfigPath() { | ||
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StayFocused.ini"); | ||
} | ||
|
||
void ParseConfig(string[] lines) { | ||
string section = "config"; | ||
|
||
foreach (var lineRaw in lines) { | ||
string line = lineRaw.Trim().ToLowerInvariant(); | ||
|
||
if (line.StartsWith("#") || line.Length == 0) { | ||
// comment line - skip | ||
continue; | ||
} | ||
|
||
if (line.StartsWith("[")) { | ||
// section line - change section | ||
section = line.Substring(1, line.Length - 2); | ||
continue; | ||
} | ||
|
||
if (section == "config") { | ||
if (line.StartsWith("mode = ")) { | ||
var parsedMode = line.Substring(7); | ||
if (parsedMode == "blacklist") mode = Modes.Blacklist; | ||
} | ||
continue; | ||
} | ||
|
||
if (section == "whitelist") { | ||
whitelist.Add(line); | ||
continue; | ||
} | ||
|
||
if (section == "blacklist") { | ||
blacklist.Add(line); | ||
continue; | ||
} | ||
} | ||
|
||
if (mode == Modes.Blacklist) { | ||
Form1.Log("Config loaded • mode = blacklist • blacklisted exes:"); | ||
Form1.Log(String.Join(", ", blacklist)); | ||
} else { | ||
Form1.Log("Config loaded • mode = whitelist • whitelisted exes:"); | ||
Form1.Log(String.Join(", ", whitelist)); | ||
} | ||
Form1.Log("---"); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters