Skip to content

Commit

Permalink
.NET: Keep ref assemblies in their own generated folder to be replaced
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Apr 11, 2024
1 parent e010796 commit 922e2fb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
8 changes: 4 additions & 4 deletions csharp-api/AssemblyGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ static void FillValidEntries(REFrameworkNET.TDB context) {

static CompilationUnitSyntax MakeFromTypeEntry(REFrameworkNET.TDB context, string typeName, REFrameworkNET.TypeDefinition? t) {
var compilationUnit = SyntaxFactory.CompilationUnit();
FillValidEntries(context);

if (!validTypes.Contains(typeName)) {
return compilationUnit;
Expand Down Expand Up @@ -669,6 +668,7 @@ static CompilationUnitSyntax MakeFromTypeEntry(REFrameworkNET.TDB context, strin
List<CompilationUnitSyntax> compilationUnits = [];
var tdb = REFrameworkNET.API.GetTDB();

// Is this parallelizable?
foreach (dynamic reEngineT in assembly.GetTypes()) {
var th = reEngineT.get_TypeHandle();

Expand All @@ -694,7 +694,7 @@ static CompilationUnitSyntax MakeFromTypeEntry(REFrameworkNET.TDB context, strin
var syntaxTreeParseOption = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp12);

foreach (var cu in compilationUnits) {
syntaxTrees.Add(SyntaxFactory.SyntaxTree(cu.NormalizeWhitespace(), syntaxTreeParseOption));
syntaxTrees.Add(SyntaxFactory.SyntaxTree(cu/*.NormalizeWhitespace()*/, syntaxTreeParseOption));
}

string? assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
Expand Down Expand Up @@ -769,9 +769,9 @@ static CompilationUnitSyntax MakeFromTypeEntry(REFrameworkNET.TDB context, strin
}

public static List<REFrameworkNET.Compiler.DynamicAssemblyBytecode> MainImpl() {
Il2CppDump.FillTypeExtensions(REFrameworkNET.API.GetTDB());

var tdb = REFrameworkNET.API.GetTDB();
Il2CppDump.FillTypeExtensions(tdb);
FillValidEntries(tdb);

dynamic appdomainT = tdb.GetType("System.AppDomain");
dynamic appdomain = appdomainT.get_CurrentDomain();
Expand Down
45 changes: 18 additions & 27 deletions csharp-api/REFrameworkNET/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@ namespace REFrameworkNET {
return false;
}

System::Collections::Generic::List<System::Reflection::Assembly^>^ PluginManager::LoadDependencies() {
REFrameworkNET::API::LogInfo("Loading managed dependencies...");
List<System::Reflection::Assembly^>^ PluginManager::LoadAssemblies(System::String^ dependencies_dir) {
REFrameworkNET::API::LogInfo("Loading Assemblies from " + dependencies_dir + "...");

const auto dependencies_path = std::filesystem::current_path() / "reframework" / "plugins" / "managed" / "dependencies";
//const auto dependencies_path = std::filesystem::current_path() / "reframework" / "plugins" / "managed" / "dependencies";

std::filesystem::create_directories(dependencies_path);
std::filesystem::create_directories(msclr::interop::marshal_as<std::string>(dependencies_dir));

auto files = System::IO::Directory::GetFiles(gcnew System::String(dependencies_path.wstring().c_str()), "*.dll");
auto files = System::IO::Directory::GetFiles(dependencies_dir, "*.dll");

auto dependencies_dir = gcnew System::String(dependencies_path.wstring().c_str());
auto assemblies = gcnew System::Collections::Generic::List<System::Reflection::Assembly^>();
auto assemblies = gcnew List<System::Reflection::Assembly^>();

if (files->Length == 0) {
REFrameworkNET::API::LogInfo("No dependencies found in " + dependencies_dir);
Expand Down Expand Up @@ -123,9 +122,17 @@ namespace REFrameworkNET {
return assemblies;
}

void PluginManager::GenerateReferenceAssemblies(System::Collections::Generic::List<System::Reflection::Assembly^>^ deps) {
List<System::Reflection::Assembly^>^ PluginManager::LoadDependencies() {
const auto dependencies_path = std::filesystem::current_path() / "reframework" / "plugins" / "managed" / "dependencies";
return LoadAssemblies(gcnew System::String(dependencies_path.wstring().c_str()));
}

void PluginManager::GenerateReferenceAssemblies(List<System::Reflection::Assembly^>^ deps) {
REFrameworkNET::API::LogInfo("Generating reference assemblies...");

auto generatedFolder = std::filesystem::current_path() / "reframework" / "plugins" / "managed" / "generated";
std::filesystem::create_directories(generatedFolder);

// Look for AssemblyGenerator class in the loaded deps
for each (System::Reflection::Assembly^ a in deps) {
if (auto generator = a->GetType("REFrameworkNET.AssemblyGenerator"); generator != nullptr) {
Expand All @@ -143,10 +150,10 @@ namespace REFrameworkNET {
for each (Compiler::DynamicAssemblyBytecode^ bytes in result) {
REFrameworkNET::API::LogInfo("Adding generated assembly to deps...");

std::string assembly_name = msclr::interop::marshal_as<std::string>(bytes->AssemblyName);
assembly_name = "REFramework.NET." + assembly_name + ".dll";
std::string assemblyName = msclr::interop::marshal_as<std::string>(bytes->AssemblyName);
assemblyName = "REFramework.NET." + assemblyName + ".dll";

auto path = std::filesystem::current_path() / "reframework" / "plugins" / "managed" / "dependencies" / assembly_name;
auto path = generatedFolder / assemblyName;
System::IO::File::WriteAllBytes(gcnew System::String(path.wstring().c_str()), bytes->Bytecode);
REFrameworkNET::API::LogInfo("Wrote generated assembly to " + gcnew System::String(path.wstring().c_str()));

Expand Down Expand Up @@ -395,8 +402,6 @@ namespace REFrameworkNET {
PluginManager::s_api_instance = gcnew REFrameworkNET::API(param_raw);
}


System::Console::WriteLine("Test");
REFrameworkNET::API::LogInfo("Attempting to load plugins from source code...");

const auto plugins_path = std::filesystem::current_path() / "reframework" / "plugins";
Expand Down Expand Up @@ -429,20 +434,6 @@ namespace REFrameworkNET {
// Compile the C# file, and then call a function in it (REFrameworkPlugin.Main)
// This is useful for loading C# plugins that don't want to be compiled into a DLL
auto bytecode = REFrameworkNET::Compiler::Compile(file, self, deps);
// Dynamically look for DynamicRun.Builder.Compiler.Compile
/*auto type = intermediary->GetType("DynamicRun.Builder.Compiler");
if (type == nullptr) {
REFrameworkNET::API::LogError("Failed to get type DynamicRun.Builder.Compiler");
continue;
}
auto method = type->GetMethod("Compile", System::Reflection::BindingFlags::Static | System::Reflection::BindingFlags::Public);
if (method == nullptr) {
REFrameworkNET::API::LogError("Failed to get method DynamicRun.Builder.Compiler.Compile");
continue;
}
auto bytecode = (array<System::Byte>^)method->Invoke(nullptr, gcnew array<Object^>{file, self->Location});*/

if (bytecode == nullptr) {
REFrameworkNET::API::LogError("Failed to compile " + file);
Expand Down
1 change: 1 addition & 0 deletions csharp-api/REFrameworkNET/PluginManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private ref class PluginManager

// meant to be executed in the correct context
// after loading "ourselves" via System::Reflection::Assembly::LoadFrom
static System::Collections::Generic::List<System::Reflection::Assembly^>^ LoadAssemblies(System::String^ path);
static System::Collections::Generic::List<System::Reflection::Assembly^>^ LoadDependencies();
static void GenerateReferenceAssemblies(System::Collections::Generic::List<System::Reflection::Assembly^>^ deps);
static bool LoadPlugins(uintptr_t param_raw);
Expand Down

0 comments on commit 922e2fb

Please sign in to comment.