Moved to settings file and enum for mode setting rather than a <string,object> dictionary for compile time type checking.
parent
dee7592ae6
commit
f45be2407b
|
@ -1,6 +1,21 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<configSections>
|
||||
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
|
||||
<section name="WifiSitter.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
|
||||
</sectionGroup>
|
||||
</configSections>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
|
||||
</startup>
|
||||
<userSettings>
|
||||
<WifiSitter.Settings>
|
||||
<setting name="enable_ipc" serializeAs="String">
|
||||
<value>False</value>
|
||||
</setting>
|
||||
<setting name="operating_mode" serializeAs="String">
|
||||
<value>0</value>
|
||||
</setting>
|
||||
</WifiSitter.Settings>
|
||||
</userSettings>
|
||||
</configuration>
|
||||
|
|
|
@ -12,13 +12,12 @@ namespace WifiSitter
|
|||
public static class Configuration
|
||||
{
|
||||
private static Dictionary<string, object> _options;
|
||||
|
||||
public static void SetOptions(string[] args) {
|
||||
_options = new Dictionary<string, object>();
|
||||
private static bool _optionsSet = false;
|
||||
|
||||
public static void SetOptions(string[] args) {
|
||||
bool showHelp = false;
|
||||
bool enableIPC = false;
|
||||
string mode = String.Empty;
|
||||
var mode = OperatingMode.none;
|
||||
|
||||
var opts = new OptionSet() {
|
||||
{"h|?|help", "Show this help and exit.",
|
||||
|
@ -26,9 +25,41 @@ namespace WifiSitter
|
|||
{"i|ipc", "Option to enable IPC communication for GUI.",
|
||||
v => enableIPC = v != null},
|
||||
{"console|service", "Direct wifisitter mode of operation.",
|
||||
v => mode = v.ToLower() },
|
||||
v => {
|
||||
switch (v.ToLower()) {
|
||||
case "console":
|
||||
mode = OperatingMode.console;
|
||||
break;
|
||||
case "service":
|
||||
mode = OperatingMode.service;
|
||||
break;
|
||||
default:
|
||||
mode = OperatingMode.none;
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
{"setupservice|install|uninstall|uninstallprompt", "Select wifisitter install/setup operation.",
|
||||
v => mode = v.ToLower() }
|
||||
v => {
|
||||
switch (v.ToLower()) {
|
||||
case "setupservice":
|
||||
mode = OperatingMode.setupservice;
|
||||
break;
|
||||
case "install":
|
||||
mode = OperatingMode.install;
|
||||
break;
|
||||
case "uninstall":
|
||||
mode = OperatingMode.uninstall;
|
||||
break;
|
||||
case "uninstallprompt":
|
||||
mode = OperatingMode.uninstallprompt;
|
||||
break;
|
||||
default:
|
||||
mode = OperatingMode.none;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
try {
|
||||
opts.Parse(args);
|
||||
|
@ -40,19 +71,22 @@ namespace WifiSitter
|
|||
|
||||
if (showHelp) ShowHelp(opts);
|
||||
|
||||
_options.Add("enable_ipc", enableIPC);
|
||||
_options.Add("operating_mode", mode);
|
||||
Properties.Settings.Default.enable_ipc = enableIPC;
|
||||
Properties.Settings.Default.operating_mode = (int)mode;
|
||||
_optionsSet = true;
|
||||
}
|
||||
|
||||
public static bool IsOptionsSet { get { return _optionsSet; } }
|
||||
|
||||
public static object GetOption(string key) {
|
||||
if (!_options.ContainsKey(key)) return null;
|
||||
return _options[key];
|
||||
public static bool IsModeSet {
|
||||
get {
|
||||
if (IsOptionsSet) {
|
||||
return (OperatingMode)Properties.Settings.Default.operating_mode != OperatingMode.none;
|
||||
};
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsOptionsSet { get { return _options != null; } }
|
||||
|
||||
public static bool IsModeSet { get { if (IsOptionsSet) { return !String.IsNullOrEmpty((string)_options["operating_mode"]); }; return false; } }
|
||||
|
||||
public static void ShowHelp(OptionSet opts, int exitCode = 0) {
|
||||
Console.WriteLine("Usage: wifisitter.exe [option] [directive]");
|
||||
Console.WriteLine();
|
||||
|
@ -65,4 +99,15 @@ namespace WifiSitter
|
|||
Environment.Exit(exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
public enum OperatingMode
|
||||
{
|
||||
none = 0,
|
||||
console,
|
||||
service,
|
||||
setupservice,
|
||||
install,
|
||||
uninstall,
|
||||
uninstallprompt
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,21 +79,22 @@ USAGE
|
|||
else if (!Configuration.IsModeSet) {
|
||||
Configuration.SetOptions(new[] { "-h" });
|
||||
}
|
||||
|
||||
|
||||
switch ((string)Configuration.GetOption("operating_mode"))
|
||||
|
||||
var mode = (OperatingMode)Properties.Settings.Default.operating_mode;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case "service":
|
||||
case OperatingMode.service:
|
||||
ServiceExecutionMode = ServiceExecutionMode.Service;
|
||||
Run(new[] { this });
|
||||
break;
|
||||
|
||||
case "setupservice":
|
||||
case OperatingMode.setupservice:
|
||||
ServiceExecutionMode = ServiceExecutionMode.Install;
|
||||
SetupService();
|
||||
break;
|
||||
|
||||
case "console":
|
||||
case OperatingMode.console:
|
||||
ServiceExecutionMode = ServiceExecutionMode.Console;
|
||||
Console.WriteLine("Starting Service...");
|
||||
OnStart(args);
|
||||
|
@ -101,17 +102,17 @@ USAGE
|
|||
OnStop();
|
||||
break;
|
||||
|
||||
case "install":
|
||||
case OperatingMode.install:
|
||||
ServiceExecutionMode = ServiceExecutionMode.Install;
|
||||
InstallService();
|
||||
break;
|
||||
|
||||
case "uninstall":
|
||||
case OperatingMode.uninstall:
|
||||
ServiceExecutionMode = ServiceExecutionMode.Uninstall;
|
||||
UninstallService();
|
||||
break;
|
||||
|
||||
case "uninstallprompt":
|
||||
case OperatingMode.uninstallprompt:
|
||||
ServiceExecutionMode = ServiceExecutionMode.Uninstall;
|
||||
if (ConfirmUninstall())
|
||||
{
|
||||
|
@ -119,7 +120,6 @@ USAGE
|
|||
InformUninstalled();
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
Configuration.SetOptions(new[] { "-h" });
|
||||
break;
|
||||
|
@ -201,7 +201,7 @@ USAGE
|
|||
}
|
||||
|
||||
var opt = " /service";
|
||||
if ((bool)Configuration.GetOption("enable_ipc")) opt += " /i";
|
||||
if (Properties.Settings.Default.enable_ipc) opt += " /i";
|
||||
key.SetValue(VALUE_NAME, origPath.Replace("\"\"", "\"") + opt);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,14 +12,14 @@ namespace WifiSitter
|
|||
Configuration.SetOptions(args);
|
||||
|
||||
var isRunning = Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1;
|
||||
string mode = (string)Configuration.GetOption("operating_mode");
|
||||
if (isRunning && (mode == "console" || mode == "service")) {
|
||||
OperatingMode mode = (OperatingMode)Properties.Settings.Default.operating_mode;
|
||||
if (isRunning && (mode == OperatingMode.console || mode == OperatingMode.service)) {
|
||||
Console.WriteLine("WifiSitter already running...\nQuiting in 10 seconds.");
|
||||
System.Threading.Thread.Sleep(10 * 1000);
|
||||
Environment.Exit(7);
|
||||
}
|
||||
else {
|
||||
(new WifiSitter()).Run(args);
|
||||
new WifiSitter().Run(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.42000
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace WifiSitter.Properties {
|
||||
|
||||
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
|
||||
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
|
||||
|
||||
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
|
||||
|
||||
public static Settings Default {
|
||||
get {
|
||||
return defaultInstance;
|
||||
}
|
||||
}
|
||||
|
||||
[global::System.Configuration.UserScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("False")]
|
||||
public bool enable_ipc {
|
||||
get {
|
||||
return ((bool)(this["enable_ipc"]));
|
||||
}
|
||||
set {
|
||||
this["enable_ipc"] = value;
|
||||
}
|
||||
}
|
||||
|
||||
[global::System.Configuration.UserScopedSettingAttribute()]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Configuration.DefaultSettingValueAttribute("0")]
|
||||
public int operating_mode {
|
||||
get {
|
||||
return ((int)(this["operating_mode"]));
|
||||
}
|
||||
set {
|
||||
this["operating_mode"] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WifiSitter" GeneratedClassName="Settings">
|
||||
<Profiles />
|
||||
<Settings>
|
||||
<Setting Name="enable_ipc" Type="System.Boolean" Scope="User">
|
||||
<Value Profile="(Default)">False</Value>
|
||||
</Setting>
|
||||
<Setting Name="operating_mode" Type="System.Int32" Scope="User">
|
||||
<Value Profile="(Default)">0</Value>
|
||||
</Setting>
|
||||
</Settings>
|
||||
</SettingsFile>
|
|
@ -544,7 +544,7 @@ namespace WifiSitter
|
|||
_mainLoopThread.Start();
|
||||
|
||||
|
||||
if ((bool)Configuration.GetOption("enable_ipc")) {
|
||||
if (Properties.Settings.Default.enable_ipc) {
|
||||
LogLine(LogType.warn, "Initializing IPC...");
|
||||
|
||||
_mqServerThread = new Thread(ZeroMQRouterRun);
|
||||
|
@ -557,7 +557,6 @@ namespace WifiSitter
|
|||
catch (Exception e) {
|
||||
WriteLog(LogType.error, e.Source + " {0}", e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override void OnStopImpl() {
|
||||
|
|
|
@ -118,6 +118,11 @@
|
|||
<Compile Include="Model\NetworkState.cs" />
|
||||
<Compile Include="Model\SimpleNetworkState.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\Settings.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="WifiSitter.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
|
@ -130,6 +135,10 @@
|
|||
<None Include="app.manifest" />
|
||||
<None Include="packages.config" />
|
||||
<None Include="Properties\app.manifest" />
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
|
||||
|
@ -165,7 +174,7 @@
|
|||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<UserProperties BuildVersion_UpdateFileVersion="False" BuildVersion_BuildVersioningStyle="None.None.None.Increment" BuildVersion_StartDate="2000/1/1" />
|
||||
<UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_BuildVersioningStyle="None.None.None.Increment" BuildVersion_UpdateFileVersion="False" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
<Import Project="..\packages\Fody.1.29.4\build\dotnet\Fody.targets" Condition="Exists('..\packages\Fody.1.29.4\build\dotnet\Fody.targets')" />
|
||||
|
|
Loading…
Reference in New Issue