public static void ReferencingAnAssemblyFromTheScriptFolder(string path1, string path2, Foo foo) { dynamic config = null; "Given a remote assembly" .f(c => File.Copy( "ConfigR.Tests.Support.SampleDependency.dll", path1 = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetTempFileName(), "dll")), true)); "And a remote config file in the same folder, which references the assembly" .f(c => { var code = $@"#r ""{Path.GetFileName(path1)}"" using ConfigR.Tests.Support.SampleDependency; Config.Foo = new Foo {{ Bar = ""baz"" }}; "; ConfigFile.Create(code, path2 = Path.Combine(Path.GetTempPath(), Path.GetTempFileName())).Using(c); }); "When I load the config file" .f(async() => config = await new Config().UseRoslynCSharpLoader(path2).LoadDynamic()); "And I get Foo" .f(() => foo = config.Foo <Foo>()); "Then Foo has a Bar of 'baz'" .f(() => foo.Bar.Should().Be("baz")); }
public static void ReferencingAnAssemblyFromTheApplicationFolder(string path, Foo foo) { dynamic config = null; "Given a remote config file which references a local assembly" .f(c => { var code = $@"#r ""ConfigR.Tests.Support.SampleDependency.dll"" using ConfigR.Tests.Support.SampleDependency; Config.Foo = new Foo {{ Bar = ""baz"" }}; "; var remoteAssemblyPath = Path.Combine(Path.GetTempPath(), "ConfigR.Tests.Support.SampleDependency.dll"); if (File.Exists(remoteAssemblyPath)) { File.Delete(remoteAssemblyPath); } ConfigFile.Create(code, path = Path.Combine(Path.GetTempPath(), Path.GetTempFileName())).Using(c); }); "When I load the config file" .f(async() => config = await new Config().UseRoslynCSharpLoader(path).LoadDynamic()); "And I get Foo" .f(() => foo = config.Foo <Foo>()); "Then Foo has a Bar of 'baz'" .f(() => foo.Bar.Should().Be("baz")); }
public static void RetrievingAnObject(int result) { dynamic config = null; "Given a local config file containing Foo of 42" .f(c => ConfigFile.Create("Config.Foo = 42;").Using(c)); "And we are using Visual Studio hosting" .x(() => { var tokens = Path.GetFileName(ConfigFile.GetDefaultPath()).Split('.'); var vsHostTokens = tokens .Reverse() .Skip(2) .Reverse() .Concat(new[] { "vshost" }) .Concat(tokens.Reverse().Take(2).Reverse()); AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", string.Join(".", vsHostTokens)); }) .Teardown(() => AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", Path.GetFileName(ConfigFile.GetDefaultPath()))); "When I load the config" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDynamic()); "And I get Foo" .f(() => result = config.Foo <int>()); "Then Foo is 42" .f(() => result.Should().Be(42)); }
public static void LoadingAScriptFromTheScriptFolder(string path1, string path2, int foo) { dynamic config = null; "Given a remote config file with a Foo of 123" .f(c => ConfigFile.Create( "Config.Foo = 123;", path1 = Path.Combine(Path.GetTempPath(), Path.GetTempFileName())) .Using(c)); "And another remote config file in the same folder, which loads the first file" .f(c => ConfigFile.Create( $@"#load ""{Path.GetFileName(path1)}""", path2 = Path.Combine(Path.GetTempPath(), Path.GetTempFileName())) .Using(c)); "When I load the second config file" .f(async() => config = await new Config().UseRoslynCSharpLoader(path2).LoadDynamic()); "And I get Foo" .f(() => foo = config.Foo <int>()); "Then Foo is 123" .f(() => foo.Should().Be(123)); }
public static void RetrievingAnAnonymousType() { dynamic config = null; dynamic result = null; "Given a local config file containing an anonymous type with a Bar of 'baz'" .f(c => { var code = @"using ConfigR.Tests.Acceptance.Roslyn.CSharp.Support; Config.Foo = new { Bar = ""baz"" }; "; ConfigFile.Create(code).Using(c); }); "When I load the config" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDynamic()); "When I get the anonymous type" .f(() => { result = config.Foo; }); "Then the anonymous type has a Bar of 'baz'" .f(() => ((string)result.Bar).Should().Be("baz")); }
public static void TryingToGetANullConfigurationItem(Exception ex) { dynamic config = null; "Given a config file with a Foo null" .f(c => ConfigFile.Create(@"Config.Foo = null;").Using(c)); "When I load the file" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDynamic()); "And I try to get an integer named 'foo'" .f(() => ex = Record.Exception(() => config.Foo <int>())); "Then an exception is thrown" .f(() => ex.Should().NotBeNull()); "And the exception message contains 'Foo'" .f(() => ex.Message.Should().Contain("Foo")); "And the exception message contains the full type name of int" .f(() => ex.Message.Should().Contain(typeof(int).FullName)); "And the exception message contains 'null'" .f(() => ex.Message.Should().Contain("null")); }
/// <summary> /// 将WriteString写入配置文件,并返回是否写入成功 /// </summary> /// <returns>bool</returns> public bool WriteToConfig() { var errFlag = true; if (ConfigFile.Exists) { ConfigFile.Delete(); } FileStream fs = ConfigFile.Create(); BinaryWriter bw = new BinaryWriter(fs); try { bw.Write(WriteString ?? ""); errFlag = true; } catch { errFlag = false; } finally { fs.Close(); bw.Close(); } return(errFlag); }
public GameProcess(string name) : base(name, Path.Combine(GameBankPath, name), "", ProcessSecurityAccess.Low) { config = ConfigFile.Create(Path.Combine(GameBankPath, "gconfig.ini")); }
static void Main(string[] args) { globalConfig = ConfigFile.Create("config.ini"); sharedConfig = ConfigFile.Create("shared.ini"); string systemLockKey = globalConfig.GetString("System", "MutexKey"); Mutex systemLock = new Mutex(false, systemLockKey); if (!systemLock.WaitOne(TimeSpan.Zero, true)) { return; } Log.ShowDate = true; //show the date of when logs happen Log.ShowThreadID = true; //show the thread id that this message is from RPCManager.Create("."); CommandExecuter commandExecuter = new CommandExecuter(); commandExecuter.Execute(args); Thread keyboardInterceptorThread = new Thread(SetupKeyboardInterceptor); keyboardInterceptorThread.Start(); scheduler = new Core.Tasks.TaskScheduler(); //Start the installation task. InstallationTask inst = new InstallationTask(); inst.Run(); //Schedule the frontend to launch in 30 seconds. scheduler.Schedule(DateTime.Now.AddSeconds(3), new FrontendLauncherTask()); Thread readInThread = new Thread(ReadIn); readInThread.Start(); while (bRunning) { Thread.Sleep(10000); } readInSafeAccess.WaitOne(Timeout.InfiniteTimeSpan, true); readInThread.Abort(); readInSafeAccess.ReleaseMutex(); scheduler.Shutdown(); scheduler.WaitForShutdownComplete(); globalConfig.Save(); systemLock.ReleaseMutex(); }
public static void EmptyConfig(string code, Exception exception) { "Given a config file which contains no executable code" .f(c => ConfigFile.Create(code).Using(c)); "When I load the config" .f(async() => exception = await Record.ExceptionAsync(async() => await new Config().UseRoslynCSharpLoader().LoadDynamic())); "Then no exception is thrown" .f(() => exception.Should().BeNull()); }
public static void UsingTypeSyntax(Foo foo) { "Given a local config file with a Bar of 'abc'" .f(c => ConfigFile.Create(@"Config.Bar = ""abc"";").Using(c)); "When I load the config as Foo" .f(async() => foo = await new Config().UseRoslynCSharpLoader().Load <Foo>()); "Then the Foo has a Bar of 'abc'" .f(() => foo.Bar.Should().Be("abc")); }
public static void UsingTypeSyntaxWithRedundantValues(Exception exception) { "Given a local config file with a Bazz of 'def'" .f(c => ConfigFile.Create(@"Config.Bazz = ""def""").Using(c)); "When I load the config as Foo" .f(async() => exception = await Record.ExceptionAsync(async() => await new Config().UseRoslynCSharpLoader().Load <Foo>())); "Then no exception is thrown" .f(() => exception.Should().BeNull()); }
public static void AddingADynamicReferenceToAnAssemblyOnDisk(Assembly reference, object result) { dynamic config = null; "Given an assembly on disk which defines a Foo type" .f(c => { var code = $@"namespace {c.Step.Scenario.ScenarioOutline.Method.Name} {{ public class Foo {{ public string Bar {{ get; set; }} }} }}"; var compilation = CSharpCompilation.Create( c.Step.Scenario.ScenarioOutline.Method.Name, new[] { CSharpSyntaxTree.ParseText(code) }, null, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location)); var emitResult = compilation.Emit(compilation.AssemblyName + ".dll"); emitResult.Success.Should().BeTrue( "The diagnostics should indicate no errors:{0}{1}", Environment.NewLine, string.Join(Environment.NewLine, emitResult.Diagnostics.Select(diagnostic => diagnostic.ToString()))); }); "And config file with Foo with a Bar of 'baz'" .f(c => { var code = $@"using {c.Step.Scenario.ScenarioOutline.Method.Name}; Config.Foo = new Foo {{ Bar = ""baz"" }}; "; ConfigFile.Create(code).Using(c); }); "When I load the assembly" .f(c => reference = Assembly.Load(c.Step.Scenario.ScenarioOutline.Method.Name)); "And I load the config using the assembly as a reference" .f(async() => config = await new Config() .UseRoslynCSharpLoader(options: ScriptOptions.Default.ForConfigScript().AddReferences(reference)).LoadDynamic()); "And I get the value" .f(() => result = config.Foo); "Then the Foo Bar should be 'baz'" .f(() => ((string)((dynamic)result).Bar).Should().Be("baz")); }
public static void ScriptFailsToCompile(Exception exception) { "Given a local config file which fails to compile" .f(c => ConfigFile.Create(@"This is not C#!").Using(c)); "When I load the config file" .f(async() => exception = await Record.ExceptionAsync(async() => await new Config().UseRoslynCSharpLoader().LoadDynamic())); "Then an exception is thrown" .f(() => exception.Should().NotBeNull()); "And the exception should be a compilation error exception" .f(() => exception.GetType().Name.Should().Be("CompilationErrorException")); }
public static void ScriptFailsToExecute(Exception exception) { "Given a local config file which throws an exception with the message 'Boo!'" .f(c => ConfigFile.Create(@"throw new System.Exception(""Boo!"");").Using(c)); "When I load the config file" .f(async() => exception = await Record.ExceptionAsync(async() => await new Config().UseRoslynCSharpLoader().LoadDynamic())); "Then an exception is thrown" .f(() => exception.Should().NotBeNull()); "And the exception message is 'Boo!'" .f(() => exception.Message.Should().Be("Boo!")); }
public static void UsingTypeSyntaxWithBadlyTypedValues(Exception exception) { "Given a local config file with a Bar of 42" .f(c => ConfigFile.Create(@"Config.Bar = 42;").Using(c)); "When I load the config as Foo" .f(async() => exception = await Record.ExceptionAsync(async() => await new Config().UseRoslynCSharpLoader().Load <Foo>())); "Then an exception is thrown" .f(() => exception.Should().NotBeNull()); "And the exception message contains 'Bar'" .f(() => exception.Message.Should().Contain("Bar")); }
public static void UsingTypeSyntaxWithAnObjectSeed(Foo foo) { "Given a local config file with a Bar of 'abc'" .f(c => ConfigFile.Create(@"Config.Bar = ""abc"";").Using(c)); "When I load the config as Foo with an object seed with a Baz of 'def'" .f(async() => foo = await new Config().UseRoslynCSharpLoader().Load <Foo>(new { Baz = "def" })); "Then the Foo has a Bar of 'abc'" .f(() => foo.Bar.Should().Be("abc")); "And the Foo has a Baz of 'def'" .f(() => foo.Baz.Should().Be("def")); }
public static void GettingANonexistentItem(int result) { dynamic config = null; "Given a config file with a Foo of 123" .f(c => ConfigFile.Create("Config.Foo = 123;").Using(c)); "When I load the file" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDynamic()); "And I get Bar with a default of 456" .f(() => result = config.Bar <int>(456)); "Then the result is 456" .f(() => result.Should().Be(456)); }
public static void GettingAnExistingItemWithTheWrongTypeOfDefault(Exception exception) { dynamic config = null; "Given a config file with a Foo of 123" .f(c => ConfigFile.Create("Config.Foo = 123;").Using(c)); "When I load the file" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDynamic()); "And I get Bar with a default of 'abc'" .f(() => exception = Record.Exception(() => config.Bar <int>("abc"))); "Then the exception indicates that 'abc' is the wrong type" .f(() => exception.Message.Should().Contain("default is not").And.Contain("System.Int32")); }
public static void UsingDictionarySyntaxWithADefaultValue(int result) { var config = default(IDictionary <string, object>); "Given a local config file with a Foo of 123" .f(c => ConfigFile.Create(@"ConfigDictionary[""Foo""] = 123;").Using(c)); "When I load the config" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDictionary()); "And I get Bar with a default of 456" .f(() => result = config.Get("Bar", 456)); "Then the result is 456" .f(() => result.Should().Be(456)); }
public static void SeedingAScriptWithAnObjectAndReceivingADictionary(string result) { IDictionary <string, object> config = null; "Given a local config file which sets Foo using the value of Bar" .f(c => ConfigFile.Create(@"Config.Foo = Config.Bar;").Using(c)); "And I load the config seeded with Bar set to 'baz'" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDictionary(new { Bar = "baz" })); "And I get Foo" .f(() => result = config.Get <string>("Foo")); "Then Foo is 'baz'" .f(() => result.Should().Be("baz")); }
public void Load() { userPath = Path.Combine(UserBankDirectory, userName);//get the users directory if (!Directory.Exists(userPath)) { return; } string configPath = Path.Combine(userPath, "UserConfig.ini"); config = ConfigFile.Create(configPath); if (config == null) { return; } }
public static void RetreivingANonExistentValue(Exception exception) { dynamic config = null; "Given a local config file containing Foo of 42" .f(c => ConfigFile.Create("Config.Foo = 42;").Using(c)); "When I load the config" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDynamic()); "And I get Bar" .f(() => exception = Record.Exception(() => config.Bar <int>())); "Then an exception is thrown" .f(() => exception.Should().NotBeNull()); "And the exception indicates that Bar does not exist" .f(() => exception.Message.Should().Contain("'Bar' does not exist")); }
static void Main() { sharedConfig = ConfigFile.Create("shared.ini");//get the config file string mutexGUID = sharedConfig.GetString("System", "MutexKey"); Mutex mutex = new Mutex(false, mutexGUID); if (mutex.WaitOne(TimeSpan.Zero, true)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); mutex.ReleaseMutex(); } else { //inform the running version to show its window NativeMethods.PostMessage((IntPtr)NativeMethods.WM_SHOWME, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); } }
public static void RetrievingAnObjectDefinedInTwoFiles(int foo) { dynamic config = null; "Given a config file with a Foo of 123" .f(c => ConfigFile.Create("Config.Foo = 123;", "foo.csx").Using(c)); "And another config file with a Foo of 456" .f(c => ConfigFile.Create("Config.Foo = 456;", "bar.csx").Using(c)); "When I load the first file followed by the second file" .f(async() => config = await new Config() .UseRoslynCSharpLoader("foo.csx").UseRoslynCSharpLoader("bar.csx").LoadDynamic()); "And I get Foo" .f(() => foo = config.Foo <int>()); "Then Foo is 456" .f(() => foo.Should().Be(456)); }
public static void UsingTypeSyntaxWithADictionarSeed(Foo foo) { "Given a local config file with a Bar of 'abc'" .f(c => ConfigFile.Create(@"Config.Bar = ""abc"";").Using(c)); "When I load the config as Foo with a dictionary seed with a Baz of 'def'" .f(async() => { var seed = new Dictionary <string, object>() { { "Baz", "def" } }; foo = await new Config().UseRoslynCSharpLoader().Load <Foo>(seed); }); "Then the Foo has a Bar of 'abc'" .f(() => foo.Bar.Should().Be("abc")); "And the Foo has a Baz of 'def'" .f(() => foo.Baz.Should().Be("def")); }
public static void RetrievingAnObject(Foo result) { dynamic config = null; "Given a local config file containing a Foo with a Bar of 'baz'" .f(c => { var code = @"using ConfigR.Tests.Acceptance.Roslyn.CSharp.Support; Config.Foo = new Foo { Bar = ""baz"" }; "; ConfigFile.Create(code).Using(c); }); "When I load the config" .f(async() => config = await new Config().UseRoslynCSharpLoader().LoadDynamic()); "And I get the Foo" .f(() => result = config.Foo <Foo>()); "Then the Foo has a Bar of 'baz'" .f(() => result.Bar.Should().Be("baz")); }
/// <summary> /// Installs the user on the machine /// <returns>True if the install worked, false otherwise</returns> /// </summary> public bool Install() { if (userTable.ContainsKey(userName)) { return(false); } Load(); //attempt a load before running the install procedure if (isValid) //if the load was valid do not install { return(true); //the install was valid, even though it didn't run } if (!Directory.Exists(userPath)) { return(false); } if (config == null) { string configPath = Path.Combine(userPath, "UserConfig.ini"); config = ConfigFile.Create(configPath); if (config == null) { return(false); } } try { systemUser = UserPrincipal.FindByIdentity(localContext, IdentityType.Name, userName); } catch { return(false); } if (systemUser == null) { systemUser = new UserPrincipal(localContext); } systemUser.SetPassword(key); systemUser.Name = userName; systemUser.DisplayName = displayName; systemUser.PasswordNeverExpires = true; systemUser.HomeDirectory = userPath; try { systemUser.Save(); } catch { return(false); } gamePath = config.GetPath("SystemInfo", "GameLocation").OriginalString; rootPath = config.GetPath("SystemInfo", "RootLocation").OriginalString; DirectoryInfo gameDirectoryInfo = Directory.CreateDirectory(gamePath); gameDirectoryInfo.Create(); string defaultUserPath = Program.GlobalConfig.GetPath("User", "DefaultUser").OriginalString; DirectoryInfo defaultUserPathInfo = new DirectoryInfo(defaultUserPath); DirectoryInfo rootDirectoryInfo = defaultUserPathInfo.Copy(rootPath); FileSystemAccessRule accessRule = new FileSystemAccessRule(userName, FileSystemRights.Modify, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow); FileSystemAccessRule denyRule = new FileSystemAccessRule(userName, FileSystemRights.TakeOwnership | FileSystemRights.ChangePermissions, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow); DirectorySecurity rootSecurity = rootDirectoryInfo.GetAccessControl(); rootSecurity.AddAccessRule(accessRule); rootSecurity.AddAccessRule(denyRule); rootDirectoryInfo.SetAccessControl(rootSecurity); DirectorySecurity gameSecurity = gameDirectoryInfo.GetAccessControl(); gameSecurity.AddAccessRule(accessRule); gameSecurity.AddAccessRule(denyRule); gameDirectoryInfo.SetAccessControl(gameSecurity); userTable.Add(userName, this); UpdateInstallFile(); return(true); }