Skip to content

Commit

Permalink
Merge pull request #5 from ix-ax/4-Discover_and_copy_Openness_dll
Browse files Browse the repository at this point in the history
Discover and copy Openness dll
  • Loading branch information
TomKovac committed Jun 28, 2023
2 parents c939a23 + 28c1e19 commit 00eaf16
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 73 deletions.
51 changes: 16 additions & 35 deletions src/tia2ax/V18_0/ApiResolver/ApiResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Security.AccessControl;

namespace Tia2Ax.Utils
{
Expand All @@ -15,16 +13,13 @@ namespace Tia2Ax.Utils
/// </summary>
public static class ApiResolver
{

/// <summary>
/// Required min version of engineering dll
/// </summary>
public const string StrRequiredVersion = "V18.0";
private const string BasePath = "SOFTWARE\\Siemens\\Automation\\Openness\\";
private const string ReferencedAssembly = "Siemens.Engineering";
private const string ReferencedHmiAssembly = "Siemens.Engineering.Hmi";
private static string _assemblyPath = "";
private static string _assemblyPathHmi = "";
private const string LibraryKey = "SOFTWARE\\Siemens\\Automation\\Openness\\18.0\\PublicAPI\\18.0.0.0";
private const string LibraryName = "Siemens.Engineering";

/// <summary>
/// Get version info from registry key
Expand Down Expand Up @@ -57,25 +52,6 @@ where Convert.ToDecimal(item.Substring(0, 4)) >= Convert.ToDecimal(StrRequiredVe
return new List<string>();
}


/// <summary>
/// Retrieve the path from assembly by version
/// </summary>
/// <param name="version"></param>
/// <param name="assembly"></param>
/// <returns></returns>
public static string GetAssemblyPath(string version, string assembly)
{
var libraries = OpennessLibraries.GetOpennessLibraries();
var portalVersion = new Version(version);
var apiVersion = new Version(assembly);
_assemblyPath = libraries.Where(e => e.TiaPortalVersion.Major == portalVersion.Major &&
e.TiaPortalVersion.Minor == portalVersion.Minor).SingleOrDefault(e => e.PublicApiVersion.Major == apiVersion.Major &&
e.PublicApiVersion.Minor == apiVersion.Minor)?.LibraryFilePath;

return null;
}

private static RegistryKey GetRegistryKey(string keyName)
{
RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
Expand Down Expand Up @@ -109,13 +85,18 @@ public static bool IsOpennessInstalled()

private static string GetLibraryFilePath()
{
var engineeringVersion = GetEngineeringVersions();

var requiredVersion = (from version in engineeringVersion
where Convert.ToDecimal(version) >= Convert.ToDecimal(StrRequiredVersion.Substring(1, 4))
select version).FirstOrDefault();

return requiredVersion;
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var registryKey = baseKey.OpenSubKey(LibraryKey, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
{
var libraryFilePath = registryKey?.GetValue(LibraryName) as string;
if (!string.IsNullOrWhiteSpace(libraryFilePath) && File.Exists(libraryFilePath))
{
return libraryFilePath;
}
}
}
return null;
}


Expand All @@ -132,7 +113,7 @@ public static bool IsTiaInstalled()
{
foreach (var version in EngineeringVersions)
{
if(version == StrRequiredVersion)
if(version == StrRequiredVersion.Substring(1, 4))
{
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/tia2ax/V18_0/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Resources;
using Tia2Ax.Utils;
using System.Reflection;

namespace tia2ax
{
Expand Down
52 changes: 26 additions & 26 deletions src/tia2ax/V18_0/Utils/TiaOpeness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@ public static class TiaOpeness
{
public static bool CheckPrerequisities()
{
//try
//{
// if (!ApiResolver.IsOpennessInstalled())
// {
// throw new Exception(
// $"The TIA Portal Openness version required {ApiResolver.StrRequiredVersion}{Environment.NewLine} is not installed.");
// }
//}
//catch (Exception e)
//{
// Console.WriteLine(e);
// throw;
//}
//try
//{
// if (!ApiResolver.IsTiaInstalled())
// {
// throw new Exception(
// $"The TIA Portal version required {ApiResolver.StrRequiredVersion}{Environment.NewLine} is not installed.");
// }
//}
//catch (Exception e)
//{
// Console.WriteLine(e);
// throw;
//}
try
{
if (!ApiResolver.IsOpennessInstalled())
{
throw new Exception(
$"The TIA Portal Openness version required {ApiResolver.StrRequiredVersion}{Environment.NewLine} is not installed.");
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
try
{
if (!ApiResolver.IsTiaInstalled())
{
throw new Exception(
$"The TIA Portal version required {ApiResolver.StrRequiredVersion}{Environment.NewLine} is not installed.");
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return true;
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/tia2ax/V18_0/V18_0_tia2ax.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net4.8</TargetFramework>
<!--<PackAsTool>True</PackAsTool>
<ToolCommandName>tiax</ToolCommandName>-->
</PropertyGroup>

<ItemGroup>
Expand All @@ -24,8 +21,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Content Include="..\..\..\third\V18\Siemens.Engineering.dll" IncludeInPackage="true" Pack="true" PackagePath="lib\net48"/>
<Content Include="..\..\..\third\V18\Siemens.Engineering.dll" IncludeInPackage="true" Pack="true" PackagePath="lib\net48" />
</ItemGroup>
</Project>
41 changes: 35 additions & 6 deletions src/tia2axtool/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
using System.Diagnostics;
using Microsoft.Win32;
using System.Diagnostics;
using System.Reflection;
using System.Security.AccessControl;

namespace tia2axtool
{
internal class Program
{
private const string LibraryKey = "SOFTWARE\\Siemens\\Automation\\Openness\\18.0\\PublicAPI\\18.0.0.0";
private const string LibraryName = "Siemens.Engineering";

static void Main(string[] args)
{
// This is a workaround to make a .net48 assembly work as dotnet tool
var entry = new FileInfo(Assembly.GetEntryAssembly().Location);
var folder = entry.Directory.FullName;
string libraryPath = GetLibraryFilePath();
if (!string.IsNullOrEmpty(libraryPath))
{
// This is a workaround to get the local dll for the Openness
var entry = new FileInfo(Assembly.GetEntryAssembly().Location);
var folder = entry.Directory.FullName;
File.Copy(libraryPath, Path.Combine(folder, LibraryName + ".dll"), true);
File.Copy(libraryPath.Replace("dll", "xml"), Path.Combine(folder, LibraryName + ".xml"), true);

// This is a workaround to make a .net48 assembly work as dotnet tool

var exePath = Path.Combine(folder, "V18_0_tia2ax.exe");
Process.Start(new ProcessStartInfo(exePath) { Arguments = string.Join(" ", args) });
var exePath = Path.Combine(folder, "V18_0_tia2ax.exe");
Process.Start(new ProcessStartInfo(exePath) { Arguments = string.Join(" ", args) });
}
}

private static string GetLibraryFilePath()
{
using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var registryKey = baseKey.OpenSubKey(LibraryKey, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey))
{
var libraryFilePath = registryKey?.GetValue(LibraryName) as string;
if (!string.IsNullOrWhiteSpace(libraryFilePath) && File.Exists(libraryFilePath))
{
return libraryFilePath;
}
}
}
return null;
}
}
}
3 changes: 2 additions & 1 deletion src/tia2axtool/tia2axtool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<ItemGroup>
<ProjectReference Include="..\tia2ax\V18_0\V18_0_tia2ax.csproj" />
</ItemGroup>

</Project>


0 comments on commit 00eaf16

Please sign in to comment.