protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents) { ResourceSet resourceSet = (ResourceSet)ResourceSets[culture]; if (resourceSet == null) { // Lazy-load default language (without caring about duplicate assignment in race conditions, no harm done); if (neutralResourcesCulture == null) { neutralResourcesCulture = GetNeutralResourcesLanguage(MainAssembly); } // If we're asking for the default language, then ask for the invariant (non-specific) resources. if (neutralResourcesCulture.Equals(culture)) { culture = CultureInfo.InvariantCulture; } string resourceFileName = GetResourceFileName(culture); Stream store = MainAssembly.GetManifestResourceStream(contextTypeInfo, resourceFileName); // If we found the appropriate resources in the local assembly... if (store != null) { resourceSet = new ResourceSet(store); // Save for later. AddResourceSet(ResourceSets, culture, ref resourceSet); } else { resourceSet = base.InternalGetResourceSet(culture, createIfNotExists, tryParents); } } return(resourceSet); }
public void FilterOutIfNeeded() { var throwableClass = MainAssembly.GetClass("java.lang.Throwable"); if (throwableClass != null && throwableClass.Assembly == MainAssembly) { var list2 = new string[] { "java.lang.*", "java.lang.reflect.*", "java.lang.ref.*", "java.lang.annotation.*", "java.util.*", "java.util.concurrent.*", "java.util.jar.*", "java.util.regex.*", "java.math.*", "java.io.*", "java.nio.*", "java.net.*", }; var list = list2.Select(FindClasses).SelectMany(t => t).ToList(); var deps = DetectDependencies(list); var deps2 = deps.OrderBy(t => t.FullName).ToList(); MainAssembly.Classes.RemoveAll(t => !deps.Contains(t)); } }
private void ReplaceDollarToUnderscoreInAllElements() { foreach (var me in MainAssembly.Descendants().Where(t => t.Name.Contains("$"))) { me.Name = me.Name.Replace("$", "_"); } }
IEnumerable <Class> FindClasses(string q) { if (q.EndsWith(".*")) { return(MainAssembly.Classes.Where(t => t.Namespace == q.Substring(0, q.Length - 2))); } return(new Class[] { MainAssembly.GetClass(q) }); }
private void RemoveCtorsWithLastPrmNamedOne() { foreach (var me in MainAssembly.Descendants().OfType <Method>().ToList()) { if (me.Parameters.Where(t => !t.Type.IsNullOrVoid() && t.Type.Name != null && t.Type.Name.Contains(".1")).FirstOrDefault() != null) { me.Remove(); } } }
private static Screen LoadScreen(string screen, Layer layerToLoadScreenOn, bool addToManagers, bool makeCurrentScreen) { mNextScreenLayer = layerToLoadScreenOn; Screen newScreen = null; Type typeOfScreen = MainAssembly.GetType(screen); if (typeOfScreen == null) { throw new System.ArgumentException("There is no " + screen + " class defined in your project or linked assemblies."); } if (screen != null && screen != "") { #if XBOX360 newScreen = (Screen)Activator.CreateInstance(typeOfScreen); #else newScreen = (Screen)Activator.CreateInstance(typeOfScreen, new object[0]); #endif } if (newScreen != null) { FlatRedBall.Input.InputManager.CurrentFrameInputSuspended = true; if (addToManagers) { // We do this so that new Screens are the CurrentScreen in Activity. // This is useful in custom logic. if (makeCurrentScreen) { mCurrentScreen = newScreen; } newScreen.Initialize(addToManagers); newScreen.ApplyRestartVariables(); } mSuppressStatePush = false; nextCallback?.Invoke(newScreen); nextCallback = null; if (addToManagers) { newScreen.Activity(true); newScreen.ActivityCallCount++; } } return(newScreen); }
/// <summary> /// /// </summary> /// <param name="cancellationToken"></param> /// <returns></returns> public Task <IList <string> > GetTypesAsync(CancellationToken cancellationToken = default) { AssemblyLoadContext = AssemblyLoadContext ?? throw new InvalidOperationException("Container is not started."); MainAssembly = MainAssembly ?? throw new InvalidOperationException("Assembly is not loaded."); var types = MainAssembly.GetTypes() .Select(type => type.FullName ?? string.Empty) .ToArray(); return(Task.FromResult <IList <string> >(types)); }
public IEnumerable <PluginAssembly> UnfilteredAssemblies(MainAssembly mainAssembly) { var allAssemblies = PluginSelector.AllPlugins; if (mainAssembly == MainAssembly.Include) { allAssemblies = allAssemblies.Concat(new PluginAssembly(Assembly.GetExecutingAssembly()).Only()); allAssemblies = allAssemblies.Concat(new PluginAssembly(typeof(EditableUI).Assembly).Only()); } return(allAssemblies.Except(FilteredAssemblies)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="typeName"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public Task <T> CreateObjectAsync <T>(string typeName, CancellationToken cancellationToken = default) where T : class { AssemblyLoadContext = AssemblyLoadContext ?? throw new InvalidOperationException("AssemblyLoadContext is not loaded."); MainAssembly = MainAssembly ?? throw new InvalidOperationException("Assembly is not loaded."); var obj = MainAssembly.CreateInstance(typeName, true) ?? throw new InvalidOperationException("Object is null"); return(Task.FromResult((T)obj)); }
protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents) { ResourceSet rs; if (!Resources.TryGetValue(culture, out rs)) { if (m_neutralResourcesCulture == null) { m_neutralResourcesCulture = GetNeutralResourcesLanguage(MainAssembly); } if (m_neutralResourcesCulture.Equals(culture)) { culture = CultureInfo.InvariantCulture; } string resourceFileName = GetResourceFileName(culture); Stream resStream = MainAssembly.GetManifestResourceStream(resourceFileName); if (resStream == null && !culture.IsNeutralCulture) { // try two-letter culture resourceFileName = GetResourceFileName(culture.Parent); resStream = MainAssembly.GetManifestResourceStream(resourceFileName); } if (resStream == null) { rs = base.InternalGetResourceSet(culture, createIfNotExists, tryParents); } else { using (resStream) { rs = new ResourceSet(resStream); } } if (rs != null) { lock (Resources) { ResourceSet ex; if (!Resources.TryGetValue(culture, out ex)) { Resources.Add(culture, rs); } else if (!object.ReferenceEquals(ex, rs)) { rs.Dispose(); rs = ex; } } } } return(rs); }
protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents) { var rs = (ResourceSet)ResourceSets[culture]; if (rs == null) { Stream store = null; string resourceFileName = null; //lazy-load default language (without caring about duplicate assignment in race conditions, no harm done); if (_neutralResourcesCulture == null) { _neutralResourcesCulture = GetNeutralResourcesLanguage(MainAssembly); } // if we're asking for the default language, then ask for the // invariant (non-specific) resources. if (_neutralResourcesCulture.Equals(culture)) { culture = CultureInfo.InvariantCulture; } resourceFileName = GetResourceFileName(culture); store = MainAssembly.GetManifestResourceStream( _contextTypeInfo, resourceFileName); // Try looking for the neutral culture if the specific culture was not found if (store == null && !culture.IsNeutralCulture) { resourceFileName = GetResourceFileName(culture.Parent); store = MainAssembly.GetManifestResourceStream( _contextTypeInfo, resourceFileName); } // If we found the appropriate resources in the local assembly if (store != null) { rs = new ResourceSet(store); // save for later. AddResourceSet(ResourceSets, culture, ref rs); } else { rs = base.InternalGetResourceSet(culture, createIfNotExists, tryParents); } } return(rs); }
private void AutoNameMethodParameters() { foreach (var me in MainAssembly.Descendants().OfType <Method>()) { var index = 0; foreach (var prm in me.Parameters) { if (prm.Name.IsNullOrEmpty()) { index++; prm.Name = "prm" + index; } } } }
public IntegratedMod(string name, string assemblyName, string integratorName, Version versionExpected = null) : base(name, assemblyName, versionExpected) { if (!IsActive) { return; } Integrator = MainAssembly.GetType(assemblyName + "." + integratorName); if (Integrator == null) { Mod.Warning($"Integrator '{integratorName}' not found for '{Name}'"); return; } Mod.Log($"Integrated with {Name}"); }
protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents) { // CultureInfo.InvariantCulture.Name is empty if (culture.Name.IsEmpty() && MainAssembly.TryGetCustomAttribute(out NeutralResourcesLanguageAttribute attribute)) { culture = new CultureInfo(attribute.CultureName); } else { attribute = new NeutralResourcesLanguageAttribute(string.Empty); } var resourceSetKey = $"{nameof(BaseName)}={BaseName},{nameof(CultureInfo)}={culture.Name}"; if (!_resourceSets.TryGetValue(resourceSetKey, out ResourceSet resourceSet)) { try { // 提供对内置资源的支持 if (IsInternalResourceBaseName() && culture.Name.Equals(attribute.CultureName, StringComparison.OrdinalIgnoreCase)) { resourceSet = GetAssemblyResourceSet(MainAssembly); } else { resourceSet = GetAssemblyResourceSet(MainAssembly.GetSatelliteAssembly(culture)); } } catch (Exception ex) { Logger.LogError(ex.AsInnerMessage()); } if (resourceSet.IsNotNull()) { _resourceSets.GetOrAdd(resourceSetKey, resourceSet); } } return(resourceSet); }
/// <summary> /// Finalizes the assembly. /// </summary> private void finalizeAssembly() { foreach (var curr in _DefinedTypes) { if (!curr.Value.IsImported) { curr.Value.TypeBuilder.CreateType(); } } if (Options.AllowSave) { if (Options.SaveAsExe) { var ep = ResolveMethod(ResolveType(EntityNames.MainTypeName), EntityNames.EntryPointMethodName); MainAssembly.SetEntryPoint(ep.MethodInfo, PEFileKinds.ConsoleApplication); } MainAssembly.Save(Options.FileName); } }
public override bool OnStartup() { if (!IsActive) { return(false); } var type = MainAssembly?.GetType("ZoneInspectData.MainTabWindow_InspectZone_Stockpile"); var method = type?.GetMethod("ExtraOnGUI"); if (method == null) { return(false); } if (Mod.Harmony.GetPatchedMethods().All(patch => patch != method)) { Mod.Harmony.Patch(method, new HarmonyMethod(typeof(ExtendedInspectData).GetMethod(nameof(Prefix_ExtraOnGui), BindingFlags.NonPublic | BindingFlags.Static))); } return(true); }
protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents) { ResourceSet resource; // search for specific resource file in the assembly resources string cultureName = culture.Name; if (cultureName.IsEmpty()) { resource = EmptyResourceSet.Instance; } else { string resourceFileName = BaseName + "." + cultureName + ".resources"; if (!resources.TryGetValue(resourceFileName, out resource)) { lock (lockingobject) { if (!resources.TryGetValue(resourceFileName, out resource)) { Stream s = MainAssembly.GetManifestResourceStream(resourceFileName); if (s != null) { resource = new ResourceSet(s); } else { // delegate in parent resource = base.InternalGetResourceSet(culture, createIfNotExists, tryParents); } resources[resourceFileName] = resource; } } } } return(resource); }
public Context(LensCompilerOptions options = null) { Options = options ?? new LensCompilerOptions(); _definedTypes = new Dictionary <string, TypeEntity>(); _definedProperties = new Dictionary <string, GlobalPropertyInfo>(); Unique = new UniqueNameGenerator(); if (Options.UseDefaultNamespaces) { Namespaces.Add("System", true); Namespaces.Add("System.Linq", true); Namespaces.Add("System.Text.RegularExpressions", true); } AssemblyCache = new ReferencedAssemblyCache(Options.UseDefaultAssemblies); _extensionResolver = new ExtensionMethodResolver(Namespaces, AssemblyCache); _typeResolver = new TypeResolver(Namespaces, AssemblyCache) { ExternalLookup = name => { _definedTypes.TryGetValue(name, out var ent); return(ent?.TypeBuilder); } }; AssemblyName an; lock (typeof(Context)) an = new AssemblyName(Unique.AssemblyName()); #if NET_CLASSIC if (Options.AllowSave) { if (string.IsNullOrEmpty(Options.FileName)) { Options.FileName = an.Name + (Options.SaveAsExe ? ".exe" : ".dll"); } MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave); MainModule = MainAssembly.DefineDynamicModule(an.Name, Options.FileName); } else { MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); MainModule = MainAssembly.DefineDynamicModule(an.Name); } #else MainAssembly = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); MainModule = MainAssembly.DefineDynamicModule(an.Name); #endif ContextId = GlobalPropertyHelper.RegisterContext(); MainType = CreateType(EntityNames.MainTypeName, prepare: false); MainType.Kind = TypeEntityKind.Main; MainType.Interfaces = new[] { typeof(IScript) }; MainMethod = MainType.CreateMethod(EntityNames.RunMethodName, typeof(object), Type.EmptyTypes, false, true, false); if (Options.LoadStandardLibrary) { InitStdlib(); } InitSafeMode(); }
public string GetAttributeProperty <T>(Func <T, string> getProperty) where T : Attribute { var attribute = (T)MainAssembly.GetCustomAttributes(typeof(T), false).FirstOrDefault(); return(attribute != null?getProperty.Invoke(attribute) : string.Empty); }
private void ProcessInputFileList( ITaskItem[] inputFiles, List <ITaskItem> imageCompilationList, List <ITaskItem> symbolsCompilationList, List <ITaskItem> r2rFilesPublishList, List <ITaskItem> r2rReferenceList, List <ITaskItem> r2rCompositeReferenceList, List <ITaskItem> r2rCompositeInputList, bool hasValidDiaSymReaderLib) { if (inputFiles == null) { return; } var exclusionSet = ExcludeList == null || Crossgen2Composite ? null : new HashSet <string>(ExcludeList, StringComparer.OrdinalIgnoreCase); var compositeExclusionSet = PublishReadyToRunCompositeExclusions == null || !Crossgen2Composite ? null : new HashSet <string>(PublishReadyToRunCompositeExclusions, StringComparer.OrdinalIgnoreCase); foreach (var file in inputFiles) { var eligibility = GetInputFileEligibility(file, Crossgen2Composite, exclusionSet, compositeExclusionSet); if (eligibility.NoEligibility) { continue; } if (eligibility.IsReference) { r2rReferenceList.Add(file); } if (eligibility.IsReference && !eligibility.ReferenceHiddenFromCompositeBuild && !eligibility.Compile) { r2rCompositeReferenceList.Add(file); } if (!eligibility.Compile) { continue; } var outputR2RImageRelativePath = file.GetMetadata(MetadataKeys.RelativePath); var outputR2RImage = Path.Combine(OutputPath, outputR2RImageRelativePath); string outputPDBImage = null; string outputPDBImageRelativePath = null; string crossgen1CreatePDBCommand = null; if (EmitSymbols) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && hasValidDiaSymReaderLib) { outputPDBImage = Path.ChangeExtension(outputR2RImage, "ni.pdb"); outputPDBImageRelativePath = Path.ChangeExtension(outputR2RImageRelativePath, "ni.pdb"); crossgen1CreatePDBCommand = $"/CreatePDB \"{Path.GetDirectoryName(outputPDBImage)}\""; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { string perfmapExtension; if (ReadyToRunUseCrossgen2 && !_crossgen2IsVersion5 && _perfmapFormatVersion >= 1) { perfmapExtension = ".ni.r2rmap"; } else { using (FileStream fs = new FileStream(file.ItemSpec, FileMode.Open, FileAccess.Read)) { PEReader pereader = new PEReader(fs); MetadataReader mdReader = pereader.GetMetadataReader(); Guid mvid = mdReader.GetGuid(mdReader.GetModuleDefinition().Mvid); perfmapExtension = ".ni.{" + mvid + "}.map"; } } outputPDBImage = Path.ChangeExtension(outputR2RImage, perfmapExtension); outputPDBImageRelativePath = Path.ChangeExtension(outputR2RImageRelativePath, perfmapExtension); crossgen1CreatePDBCommand = $"/CreatePerfMap \"{Path.GetDirectoryName(outputPDBImage)}\""; } } if (eligibility.CompileSeparately) { // This TaskItem is the IL->R2R entry, for an input assembly that needs to be compiled into a R2R image. This will be used as // an input to the ReadyToRunCompiler task TaskItem r2rCompilationEntry = new TaskItem(file); r2rCompilationEntry.SetMetadata(MetadataKeys.OutputR2RImage, outputR2RImage); if (outputPDBImage != null && ReadyToRunUseCrossgen2 && !_crossgen2IsVersion5) { r2rCompilationEntry.SetMetadata(MetadataKeys.EmitSymbols, "true"); r2rCompilationEntry.SetMetadata(MetadataKeys.OutputPDBImage, outputPDBImage); } r2rCompilationEntry.RemoveMetadata(MetadataKeys.OriginalItemSpec); imageCompilationList.Add(r2rCompilationEntry); } else if (eligibility.CompileIntoCompositeImage) { r2rCompositeInputList.Add(file); } // This TaskItem corresponds to the output R2R image. It is equivalent to the input TaskItem, only the ItemSpec for it points to the new path // for the newly created R2R image TaskItem r2rFileToPublish = new TaskItem(file); r2rFileToPublish.ItemSpec = outputR2RImage; r2rFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec); r2rFilesPublishList.Add(r2rFileToPublish); // Note: ReadyToRun PDB/Map files are not needed for debugging. They are only used for profiling, therefore the default behavior is to not generate them // unless an explicit PublishReadyToRunEmitSymbols flag is enabled by the app developer. There is also another way to profile that the runtime supports, which does // not rely on the native PDBs/Map files, so creating them is really an opt-in option, typically used by advanced users. // For debugging, only the IL PDBs are required. if (eligibility.CompileSeparately && outputPDBImage != null) { if (!ReadyToRunUseCrossgen2 || _crossgen2IsVersion5) { // This TaskItem is the R2R->R2RPDB entry, for a R2R image that was just created, and for which we need to create native PDBs. This will be used as // an input to the ReadyToRunCompiler task TaskItem pdbCompilationEntry = new TaskItem(file); pdbCompilationEntry.ItemSpec = outputR2RImage; pdbCompilationEntry.SetMetadata(MetadataKeys.OutputPDBImage, outputPDBImage); pdbCompilationEntry.SetMetadata(MetadataKeys.CreatePDBCommand, crossgen1CreatePDBCommand); symbolsCompilationList.Add(pdbCompilationEntry); } // This TaskItem corresponds to the output PDB image. It is equivalent to the input TaskItem, only the ItemSpec for it points to the new path // for the newly created PDB image. TaskItem r2rSymbolsFileToPublish = new TaskItem(file); r2rSymbolsFileToPublish.ItemSpec = outputPDBImage; r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.RelativePath, outputPDBImageRelativePath); r2rSymbolsFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec); if (!IncludeSymbolsInSingleFile) { r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.ExcludeFromSingleFile, "true"); } r2rFilesPublishList.Add(r2rSymbolsFileToPublish); } } if (Crossgen2Composite) { MainAssembly.SetMetadata(MetadataKeys.RelativePath, Path.GetFileName(MainAssembly.ItemSpec)); var compositeR2RImageRelativePath = MainAssembly.GetMetadata(MetadataKeys.RelativePath); compositeR2RImageRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, "r2r" + Path.GetExtension(compositeR2RImageRelativePath)); var compositeR2RImage = Path.Combine(OutputPath, compositeR2RImageRelativePath); TaskItem r2rCompilationEntry = new TaskItem(MainAssembly); r2rCompilationEntry.ItemSpec = r2rCompositeInputList[0].ItemSpec; r2rCompilationEntry.SetMetadata(MetadataKeys.OutputR2RImage, compositeR2RImage); r2rCompilationEntry.SetMetadata(MetadataKeys.CreateCompositeImage, "true"); r2rCompilationEntry.RemoveMetadata(MetadataKeys.OriginalItemSpec); if (EmitSymbols) { string compositePDBImage = null; string compositePDBRelativePath = null; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && hasValidDiaSymReaderLib) { compositePDBImage = Path.ChangeExtension(compositeR2RImage, ".ni.pdb"); compositePDBRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, ".ni.pdb"); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { string perfmapExtension = (_perfmapFormatVersion >= 1 ? ".ni.r2rmap" : ".ni.{composite}.map"); compositePDBImage = Path.ChangeExtension(compositeR2RImage, perfmapExtension); compositePDBRelativePath = Path.ChangeExtension(compositeR2RImageRelativePath, perfmapExtension); } if (compositePDBImage != null && ReadyToRunUseCrossgen2 && !_crossgen2IsVersion5) { r2rCompilationEntry.SetMetadata(MetadataKeys.EmitSymbols, "true"); r2rCompilationEntry.SetMetadata(MetadataKeys.OutputPDBImage, compositePDBImage); // Publish composite PDB file TaskItem r2rSymbolsFileToPublish = new TaskItem(MainAssembly); r2rSymbolsFileToPublish.ItemSpec = compositePDBImage; r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.RelativePath, compositePDBRelativePath); r2rSymbolsFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec); if (!IncludeSymbolsInSingleFile) { r2rSymbolsFileToPublish.SetMetadata(MetadataKeys.ExcludeFromSingleFile, "true"); } r2rFilesPublishList.Add(r2rSymbolsFileToPublish); } } imageCompilationList.Add(r2rCompilationEntry); // Publish it TaskItem compositeR2RFileToPublish = new TaskItem(MainAssembly); compositeR2RFileToPublish.ItemSpec = compositeR2RImage; compositeR2RFileToPublish.RemoveMetadata(MetadataKeys.OriginalItemSpec); compositeR2RFileToPublish.SetMetadata(MetadataKeys.RelativePath, compositeR2RImageRelativePath); r2rFilesPublishList.Add(compositeR2RFileToPublish); } }