impr: Move script library to its own library

This commit is contained in:
WerWolv 2023-07-16 20:13:50 +02:00
parent 0a6815da8f
commit d7238a5f80
11 changed files with 46 additions and 13 deletions

View File

@ -16,7 +16,7 @@ namespace ImHex
}
catch (Exception e)
{
Console.WriteLine("[.NET Script] Exception in AssemblyLoader: " + e.Message);
Console.WriteLine("[.NET Script] Exception in AssemblyLoader: " + e.ToString());
return 1;
}
}
@ -24,10 +24,22 @@ namespace ImHex
private static bool ExecuteScript(string path)
{
AssemblyLoadContext? context = new("ScriptDomain_" + Path.GetFileNameWithoutExtension(path), true);
string? basePath = Path.GetDirectoryName(path);
if (basePath == null)
{
Console.WriteLine("[.NET Script] Failed to get base path");
return false;
}
AssemblyLoadContext? context = new("ScriptDomain_" + basePath, true);
try
{
foreach (var file in Directory.GetFiles(basePath, "*.dll"))
{
context.LoadFromStream(new MemoryStream(File.ReadAllBytes(file)));
}
var assembly = context.LoadFromStream(new MemoryStream(File.ReadAllBytes(path)));
var entryPointType = assembly.GetType("Script");
@ -48,7 +60,7 @@ namespace ImHex
}
catch (Exception e)
{
Console.WriteLine("[.NET Script] Exception in AssemblyLoader: " + e.Message);
Console.WriteLine("[.NET Script] Exception in AssemblyLoader: " + e.ToString());
return false;
}
finally

View File

@ -4,14 +4,14 @@ function(add_dotnet_assembly name)
set(OUTPUT_DLL ${CMAKE_CURRENT_BINARY_DIR}/../../${name}.dll)
set(OUTPUT_RUNTIMECONFIG ${CMAKE_CURRENT_BINARY_DIR}/../../${name}.runtimeconfig.json)
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/${name}/*.cs)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/../../${name}.dll
COMMAND ${DOTNET_EXECUTABLE} build ${CMAKE_CURRENT_SOURCE_DIR}/${name}/${name}.csproj --nologo -c Release -o ${CMAKE_CURRENT_BINARY_DIR}/../.. && ${CMAKE_COMMAND} -DOUTPUT_RUNTIMECONFIG="${OUTPUT_RUNTIMECONFIG}" -P ${CMAKE_CURRENT_SOURCE_DIR}/post_process_runtimeconfig.cmake
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}/${name}.csproj
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}/${name}.csproj ${sources}
COMMENT "Building ${name}.dll"
)
add_custom_target(${name} ALL DEPENDS ${OUTPUT_DLL})
install(FILES
@ -20,9 +20,6 @@ function(add_dotnet_assembly name)
DESTINATION
${PLUGINS_INSTALL_LOCATION}
)
file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/${name}/*.cs)
target_sources(${name} PRIVATE ${sources})
endfunction()
add_dotnet_assembly(AssemblyLoader)

View File

@ -1,3 +1,3 @@
.vs/
ImHexScript/bin/
ImHexScript/obj/
*/bin/
*/obj/

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
</Project>

View File

@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33801.468
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImHexScript", "ImHexScript\ImHexScript.csproj", "{F5DDEF0E-0CD2-4724-87A6-96FAF1FD64B0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImHexScript", "ImHexScript\ImHexScript.csproj", "{F5DDEF0E-0CD2-4724-87A6-96FAF1FD64B0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImHexLibrary", "ImHexLibrary\ImHexLibrary.csproj", "{B70BCC4E-7E54-45CD-AF85-9D56CA9E64B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +17,10 @@ Global
{F5DDEF0E-0CD2-4724-87A6-96FAF1FD64B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5DDEF0E-0CD2-4724-87A6-96FAF1FD64B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5DDEF0E-0CD2-4724-87A6-96FAF1FD64B0}.Release|Any CPU.Build.0 = Release|Any CPU
{B70BCC4E-7E54-45CD-AF85-9D56CA9E64B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B70BCC4E-7E54-45CD-AF85-9D56CA9E64B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B70BCC4E-7E54-45CD-AF85-9D56CA9E64B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B70BCC4E-7E54-45CD-AF85-9D56CA9E64B1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>Main</AssemblyName>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<SelfContained>true</SelfContained>
</PropertyGroup>
@ -14,4 +14,12 @@
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.89.7.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ImHexLibrary\ImHexLibrary.csproj" />
</ItemGroup>
</Project>

View File

@ -5,6 +5,6 @@ class Script
{
public static void Main()
{
UI.ShowMessageBox("Hello World!");
}
}