-
-
Notifications
You must be signed in to change notification settings - Fork 365
Rewrite Loader into C# #2647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
romangolev
wants to merge
45
commits into
pyrevitlabs:develop
Choose a base branch
from
romangolev:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Rewrite Loader into C# #2647
Changes from 5 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
0ecc3de
feat: add pyRevitAssemblyBuilder project to the Loader solution
romangolev 723821c
feat: make assembly put dlls in a correct folder, scanning all availa…
romangolev 355f92e
feat: implement recursive UI builder
romangolev 847c246
fix: UIManagerServite to handle stacked pulldowns correctly
romangolev fc104aa
feat: update build dlls
romangolev f659f49
feat: replace DI architecture with plain intantiating of services. re…
romangolev c916a4a
feat: implement pyRevitExtensionParser csproject
romangolev aa4fc9d
feat: update dlls
romangolev aea0787
feat: change unique id to mimic pythonic one, add logic to CommandTyp…
romangolev 41a5955
feat: change assembly generation module to Roslyn
romangolev 2fc4643
feat: mvp c# loader
romangolev 81efe9a
fix: move enums to parser, simplify code by removing wraps
romangolev 3886911
undate binaries
romangolev 038fd01
Switch for .net core Desktop connector API
jmcouffin e93db34
fix: is_schedule
leyarx ae30736
Update pyrevit-hosts.json 2026.0.1
jmcouffin bb06cdc
Update Directory.Build.targets
jmcouffin 987570d
upd
nodatasheet 4ead9b0
add Associated to Others to select_family_parameters
nodatasheet b6dda98
cleanup
nodatasheet 2972ffc
Update __init__.py
jmcouffin 0ba9785
Adding exitscript option to SelectFromList from pyrevit.forms
iorhanV 52b9424
Update __init__.py - exitscritp handeled in show method
jmcouffin 7f5eadb
Bump golang.org/x/crypto
dependabot[bot] 3fb7b20
fixes commit f81f483
jmcouffin db6510d
Update __init__.py
jmcouffin 736cfed
Update go.mod mongodb driver
jmcouffin 6ccc8c1
Update go.mod
jmcouffin 8ae818e
missing comma in the docs for CommandSwitchWindow
jonatanjacobsson 4e98317
feat: add pyRevitAssemblyBuilder project to the Loader solution
romangolev 80f6ccf
feat: make assembly put dlls in a correct folder, scanning all availa…
romangolev 1d6da91
feat: implement recursive UI builder
romangolev fa8fc2b
fix: UIManagerServite to handle stacked pulldowns correctly
romangolev ba7d564
feat: update build dlls
romangolev 6964633
feat: replace DI architecture with plain intantiating of services. re…
romangolev 46bdaa9
feat: implement pyRevitExtensionParser csproject
romangolev 0546001
feat: update dlls
romangolev cffdb8a
feat: change unique id to mimic pythonic one, add logic to CommandTyp…
romangolev b686515
feat: change assembly generation module to Roslyn
romangolev 33b3d75
feat: mvp c# loader
romangolev 18ecf76
fix: move enums to parser, simplify code by removing wraps
romangolev e8e445a
undate binaries
romangolev 4acf0a9
feat: add a switcher in settings to be able to toglle between python …
romangolev 84ee64b
update dlls
romangolev 1c90cac
merge conflict
romangolev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file added
BIN
+62.8 KB
bin/netcore/engines/IPY342/Microsoft.Extensions.DependencyInjection.Abstractions.dll
Binary file not shown.
Binary file added
BIN
+90.2 KB
bin/netcore/engines/IPY342/Microsoft.Extensions.DependencyInjection.dll
Binary file not shown.
Binary file added
BIN
+63.7 KB
bin/netcore/engines/IPY342/Microsoft.Extensions.Logging.Abstractions.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+62.8 KB
bin/netfx/engines/IPY342/Microsoft.Extensions.DependencyInjection.Abstractions.dll
Binary file not shown.
Binary file added
BIN
+94.7 KB
bin/netfx/engines/IPY342/Microsoft.Extensions.DependencyInjection.dll
Binary file not shown.
Binary file added
BIN
+66.3 KB
bin/netfx/engines/IPY342/Microsoft.Extensions.Logging.Abstractions.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+1.23 KB
(110%)
bin/netfx/engines/IPY342/System.Runtime.CompilerServices.Unsafe.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
dev/pyRevitLoader/pyRevitAssemblyBuilder/AssemblyMaker/AssemblyBuilderService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using pyRevitAssemblyBuilder.Shared; | ||
|
||
namespace pyRevitAssemblyBuilder.AssemblyMaker | ||
{ | ||
public class AssemblyBuilderService | ||
{ | ||
private readonly ICommandTypeGenerator _typeGenerator; | ||
private readonly string _revitVersion; | ||
|
||
public AssemblyBuilderService(ICommandTypeGenerator typeGenerator, string revitVersion) | ||
{ | ||
_typeGenerator = typeGenerator; | ||
_revitVersion = revitVersion ?? throw new ArgumentNullException(nameof(revitVersion)); | ||
} | ||
|
||
public ExtensionAssemblyInfo BuildExtensionAssembly(IExtension extension) | ||
{ | ||
string extensionHash = GetStableHash(extension.GetHash() + _revitVersion).Substring(0, 16); | ||
string fileName = $"pyRevit_{_revitVersion}_{extensionHash}_{extension.Name}.dll"; | ||
|
||
string outputDir = Path.Combine( | ||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), | ||
"pyRevit", | ||
_revitVersion | ||
); | ||
Directory.CreateDirectory(outputDir); | ||
|
||
string outputPath = Path.Combine(outputDir, fileName); | ||
|
||
var asmName = new AssemblyName(extension.Name) | ||
{ | ||
Version = new Version(1, 0, 0, 0) | ||
}; | ||
|
||
string fileNameWithoutExt = Path.GetFileNameWithoutExtension(outputPath); | ||
|
||
#if NETFRAMEWORK | ||
var domain = AppDomain.CurrentDomain; | ||
var asmBuilder = domain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave, outputDir); | ||
var moduleBuilder = asmBuilder.DefineDynamicModule(fileNameWithoutExt, fileName); | ||
#else | ||
var asmBuilder = AssemblyBuilder.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run); | ||
var moduleBuilder = asmBuilder.DefineDynamicModule(fileNameWithoutExt); | ||
#endif | ||
|
||
foreach (var cmd in extension.GetAllCommands()) | ||
{ | ||
_typeGenerator.DefineCommandType(extension, cmd, moduleBuilder); | ||
} | ||
|
||
#if NETFRAMEWORK | ||
asmBuilder.Save(fileName); | ||
#else | ||
var generator = new Lokad.ILPack.AssemblyGenerator(); | ||
generator.GenerateAssembly(asmBuilder, outputPath); | ||
#endif | ||
|
||
return new ExtensionAssemblyInfo( | ||
name: extension.Name, | ||
location: outputPath, | ||
isReloading: CheckIfExtensionAlreadyLoaded(extension.Name) | ||
); | ||
} | ||
|
||
private bool CheckIfExtensionAlreadyLoaded(string extensionName) | ||
{ | ||
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) | ||
{ | ||
if (asm.GetName().Name == extensionName) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static string GetStableHash(string input) | ||
{ | ||
using (var sha1 = System.Security.Cryptography.SHA1.Create()) | ||
{ | ||
var hash = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)); | ||
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
dev/pyRevitLoader/pyRevitAssemblyBuilder/AssemblyMaker/DefaultCommandTypeGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Reflection.Emit; | ||
using System.Reflection; | ||
using pyRevitAssemblyBuilder.Shared; | ||
|
||
namespace pyRevitAssemblyBuilder.AssemblyMaker | ||
{ | ||
public class DefaultCommandTypeGenerator : ICommandTypeGenerator | ||
{ | ||
public void DefineCommandType(IExtension extension, ICommandComponent command, ModuleBuilder moduleBuilder) | ||
{ | ||
var typeBuilder = moduleBuilder.DefineType( | ||
name: command.UniqueId, | ||
attr: TypeAttributes.Public | TypeAttributes.Class | ||
); | ||
|
||
var attrBuilder = new CustomAttributeBuilder( | ||
typeof(System.ComponentModel.DescriptionAttribute).GetConstructor(new[] { typeof(string) }), | ||
new object[] { command.Tooltip } | ||
); | ||
typeBuilder.SetCustomAttribute(attrBuilder); | ||
|
||
typeBuilder.CreateType(); | ||
jmcouffin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
dev/pyRevitLoader/pyRevitAssemblyBuilder/AssemblyMaker/ExtensionAssemblyInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace pyRevitAssemblyBuilder.AssemblyMaker | ||
{ | ||
public class ExtensionAssemblyInfo | ||
{ | ||
public string Name { get; } | ||
public string Location { get; } | ||
public bool IsReloading { get; } | ||
|
||
public ExtensionAssemblyInfo(string name, string location, bool isReloading) | ||
{ | ||
Name = name; | ||
Location = location; | ||
IsReloading = isReloading; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"{Name} ({(IsReloading ? "Reloaded" : "Fresh")}) -> {Location}"; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
dev/pyRevitLoader/pyRevitAssemblyBuilder/AssemblyMaker/ICommandTypeGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using pyRevitAssemblyBuilder.Shared; | ||
using System.Reflection.Emit; | ||
|
||
namespace pyRevitAssemblyBuilder.AssemblyMaker | ||
{ | ||
public interface ICommandTypeGenerator | ||
{ | ||
void DefineCommandType(IExtension extension, ICommandComponent command, ModuleBuilder moduleBuilder); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
dev/pyRevitLoader/pyRevitAssemblyBuilder/AssemblyMaker/ServiceRegistration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using pyRevitAssemblyBuilder.AssemblyMaker; | ||
|
||
namespace pyRevitAssemblyBuilder.Startup | ||
{ | ||
public static class ServiceRegistration | ||
{ | ||
public static IServiceCollection AddAssemblyBuilder(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<ICommandTypeGenerator, DefaultCommandTypeGenerator>(); | ||
services.AddSingleton<AssemblyBuilderService>(); | ||
return services; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
dev/pyRevitLoader/pyRevitAssemblyBuilder/AssemblyMaker/TypeMakerService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using pyRevitAssemblyBuilder.Shared; | ||
|
||
namespace pyRevitAssemblyBuilder.AssemblyMaker | ||
{ | ||
public class TypeMakerService : ICommandTypeGenerator | ||
{ | ||
public void DefineCommandType(IExtension extension, ICommandComponent command, ModuleBuilder moduleBuilder) | ||
{ | ||
var typeBuilder = moduleBuilder.DefineType( | ||
name: command.UniqueId, | ||
attr: TypeAttributes.Public | TypeAttributes.Class | ||
); | ||
|
||
if (!string.IsNullOrEmpty(command.Tooltip)) | ||
{ | ||
var attrBuilder = new CustomAttributeBuilder( | ||
typeof(System.ComponentModel.DescriptionAttribute).GetConstructor(new[] { typeof(string) }), | ||
new object[] { command.Tooltip } | ||
); | ||
typeBuilder.SetCustomAttribute(attrBuilder); | ||
} | ||
|
||
// Emit a default constructor | ||
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public); | ||
|
||
// Create the type (works in .NET Standard 2.0 via CreateTypeInfo) | ||
_ = typeBuilder.CreateTypeInfo(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commented code indicates a switcher to toggle between Python and C# loaders, but the implementation currently just calls the C# version. Consider implementing this switch with a configuration option so users can choose which loader to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think @sanzoghenzo @dosymep @dosymep, should we keep 2 loaders and some sort of a transition period while migrating? I can add a key with a true/false value to the pyRevitConfig.ini in order to switch between them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that a feature flag is a good idea, it allows is to keep things working the old way while letting beta testers in and ironing out bugs that may (will) pop up.