Skip to content

Commit

Permalink
synchronize rhino dotnetsdk
Browse files Browse the repository at this point in the history
  • Loading branch information
fraguada committed Dec 12, 2023
1 parent 4a59401 commit a3bbbff
Show file tree
Hide file tree
Showing 37 changed files with 1,369 additions and 235 deletions.
7 changes: 7 additions & 0 deletions src/dotnet/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ class Import
// DO NOT add the ".dll, .dynlib, .so, ..." extension.
// Each platform should be smart enough to figure out how
// to append an extension to find the dynamic library
// #if ON_RUNTIME_IOS

// public const string lib = "RhCore.framework/RhCore";
// // public const string librdk = "RhCommonRDK.framework/RhCommonRDK";

// #else
public const string lib = "rhcommon_c";
// #endif
public const string librdk = "rhcommonrdk_c";
#endif
private Import() { }
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet/MonoHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ static void Application_ThreadException(object sender, System.Threading.ThreadEx
partial class UnsafeNativeMethods
{
// These functions must never be called unless RhinoCommon is being run from Mono
[DllImport("__Internal", CallingConvention=CallingConvention.Cdecl)]
internal static extern void RhMono_SetPlugInLoadString(int which, [MarshalAs(UnmanagedType.LPWStr)]string str);
// [DllImport("__Internal", CallingConvention=CallingConvention.Cdecl)]
// internal static extern void RhMono_SetPlugInLoadString(int which, [MarshalAs(UnmanagedType.LPWStr)]string str);
}

#endif
2 changes: 1 addition & 1 deletion src/dotnet/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ internal static extern void CRhinoObject_SetCallbacks(Rhino.DocObjects.RhinoObje
Rhino.DocObjects.RhinoObject.RhinoObjectDeletedCallback deleted);

[DllImport(Import.lib, CallingConvention = CallingConvention.Cdecl)]
internal static extern void CRhinoObject_SetPerObjectCallbacks(IntPtr ptrObject, IntPtr drawCallback);
internal static extern void CRhinoObject_SetPerObjectCallbacks(IntPtr ptrObject, IntPtr drawCallback, IntPtr boundingBoxCallback, IntPtr tightBoundingBoxCallback);

[DllImport(Import.lib, CallingConvention = CallingConvention.Cdecl)]
internal static extern void CRhinoObject_SetPickCallbacks(Rhino.DocObjects.RhinoObject.RhinoObjectPickCallback pick,
Expand Down
173 changes: 161 additions & 12 deletions src/dotnet/hostutils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Reflection.Metadata;
using System.Reflection;
using Rhino.FileIO;
using System.Text.RegularExpressions;
#endif

namespace Rhino.Runtime
Expand Down Expand Up @@ -888,6 +889,21 @@ public static Skin ActiveSkin
get { return m_theSingleSkin; }
}

internal static string SkinDllPath
{
get
{
using (var sh = new Rhino.Runtime.InteropWrappers.StringHolder())
{
var pointer = sh.NonConstPointer();
var path = string.Empty;
if (UnsafeNativeMethods.CRhSkin_SkinDllPath(pointer))
path = sh.ToString();
return string.IsNullOrWhiteSpace(path) ? null : path;
}
}
}

internal static void DeletePointer()
{
if( m_theSingleSkin!=null )
Expand Down Expand Up @@ -1442,11 +1458,23 @@ public static System.Reflection.Assembly LoadAssemblyFrom(string path)
}
}

// LoadFromAssemblyPath can generate an error so let's check for previous errors here.
UnsafeNativeMethods.RHC_GetLastWindowsError();

// Curtis:
// Load in the same context as Rhino vs. always the Default context.
// this should be the default context when running Rhino normally,
// however this will be a separate context when running Rhino.Inside on Mac.
return RhinoLoadContext.LoadFromAssemblyPath(fullPath);
Assembly assembly = RhinoLoadContext.LoadFromAssemblyPath(fullPath);

// Discard error produced by LoadFromAssemblyPath.
// Error: "The system cannot find the file specified."
// The error doesn't seem to be causing any problems so let's discard it.
// NOTE: Investigation into why this error happens in RH-78218.
UnsafeNativeMethods.RHC_MaskLastWindowsError(2); // ERROR_FILE_NOT_FOUND
UnsafeNativeMethods.RHC_GetLastWindowsError();

return assembly;
#endif
}

Expand Down Expand Up @@ -1715,6 +1743,19 @@ public static bool GetPrinterFormSize(string printerName, string formName, out d
return widthMillimeters > 0.0;
}

/// <summary>
/// Get the output resolution for a given printer.
/// </summary>
/// <param name="printerName"></param>
/// <param name="horizontal">get the horizontal or vertical resolution</param>
/// <returns>
/// Dot per inch resolution for a given printer on success. 0 if an error occurred
/// </returns>
public static double GetPrinterDPI(string printerName, bool horizontal)
{
return UnsafeNativeMethods.RHC_GetPrinterDPI(printerName, horizontal);
}

#endif

static Dictionary<string, IPlatformServiceLocator> g_platform_locator = new Dictionary<string, IPlatformServiceLocator>();
Expand Down Expand Up @@ -1836,9 +1877,11 @@ static int ExecuteNamedCallbackHelper(IntPtr name, IntPtr ptrNamedParams)
EventHandler<NamedParametersEventArgs> callback = null;
if( _namedCallbacks.TryGetValue(_name, out callback) && callback != null)
{
NamedParametersEventArgs e = new NamedParametersEventArgs(ptrNamedParams);
callback(null, e);
e.m_pNamedParams = IntPtr.Zero;
using (var e = new NamedParametersEventArgs(ptrNamedParams))
{
callback(null, e);
e.m_pNamedParams = IntPtr.Zero;
}
return 1;
}
}
Expand Down Expand Up @@ -3056,28 +3099,36 @@ internal static object ParseFieldExpression(string expression, RhinoDoc doc,



//v7 requires proper casing of functions. Repair the casing of older files to ensure compatibility.
//v8 and newer requires proper casing of functions. Please don't add new functions to this list
//Repair the casing of older files to ensure compatibility.
#region Repair Function Casing
var function_name_list = new List<string>()
{
"Area",
"AttributeUserText",
"BlockAttributeText",
"BlockInstanceCount",
"BlockInstanceName",
"CurveLength",
"Date",
"DateModified",
"DocumentText",
"DetailScale",
"DocumentUserText",
"FileName",
"LayerName",
"LayoutUserText",
"ModelUnits",
"Notes",
"NumPages",
"ObjectLayer",
"ObjectName",
"ObjectPage",
"PageHeight",
"PageName",
"PageNumber",
"PageHeight",
"PageWidth",
"PaperName",
"PointCoordinate",
"UserText",
"Volume"
};
Expand Down Expand Up @@ -3385,6 +3436,28 @@ static void BuildRegisteredPlugInList()
/// <since>7.17</since>
public static IEnumerable<System.IO.DirectoryInfo> GetActivePlugInVersionFolders()
{
// GetEnvironmentVariable can generate an error so let's check for previous errors here.
UnsafeNativeMethods.RHC_GetLastWindowsError();

var developmentDirs = System.Environment.GetEnvironmentVariable("RHINO_PACKAGE_DIRS")?.Split(';');

// Discard error produced by GetEnvironmentVariable.
// Error: "The system could not find the environment option that was entered."
// The error doesn't seem to be causing any problems so let's discard it.
UnsafeNativeMethods.RHC_MaskLastWindowsError(203); // ERROR_ENVVAR_NOT_FOUND
UnsafeNativeMethods.RHC_GetLastWindowsError();

if (developmentDirs?.Length > 0)
{
foreach (var developmentDir in developmentDirs)
{
if (Directory.Exists(developmentDir))
{
yield return GetRuntimeSpecificFolder(new DirectoryInfo(developmentDir));
}
}
}

var userDirs = GetActivePlugInVersionFolders(true).ToList();
var machineDirs = GetActivePlugInVersionFolders(false).ToList();

Expand All @@ -3410,11 +3483,87 @@ static void BuildRegisteredPlugInList()
break;
}
if (pick)
yield return userDir;
yield return GetRuntimeSpecificFolder(userDir);
}

foreach (var machineDir in machineDirs)
yield return machineDir;
yield return GetRuntimeSpecificFolder(machineDir);
}

internal static DirectoryInfo GetRuntimeSpecificFolder(DirectoryInfo root, bool useRootFiles = true)
{
// if there are .rhp or .gha's in the top folder, use default behaviour
if (useRootFiles &&
(root.GetFiles("*.rhp").Length > 0
|| root.GetFiles("*.gha").Length > 0)
)
return root;

// no plugins to load in this package, scan for framework-specific subfolders if they exist

if (RunningInNetCore)
{
// search from current .NET runtime down to 7 (for future proofing when we support .NET 8, 9, etc.)
const int minimumNetVersion = 7;
var currentRuntimeVersion = System.Environment.Version.Major;
for (int i = currentRuntimeVersion; i >= minimumNetVersion; i--)
{
var suffix = RunningOnWindows ? "-windows" :
RunningOnOSX ? "-macos" :
null; // -linux

Version GetOSVersion(string name)
{
var idx = name.IndexOf(suffix);
if (idx < 0)
return null;

var versionString = name.Substring(idx + suffix.Length);

return Version.TryParse(versionString, out var version) ? version : null;
}

// search for platform-specific directories first, ordered by os version if specified
var platformDirs = root
.GetDirectories($"net{i}.0{suffix}*")
.OrderByDescending(d => GetOSVersion(d.Name) ?? new Version());

var currentOSVersion = System.Environment.OSVersion.Version;

foreach (var platformDir in platformDirs)
{
var osVersion = GetOSVersion(platformDir.Name);

// no os version, just use it
if (osVersion == null)
return platformDir;

// and ensure the OS version, if specified, is greater or equal to package version
if (currentOSVersion >= osVersion)
return platformDir;
}

// search for platform-agnostic target
var targetDir = root.GetDirectories($"net{i}.0")?.FirstOrDefault();
if (targetDir != null)
return targetDir;
}
}

// fall back to the latest net4x folder
foreach (var net4xdir in root.EnumerateDirectories("net4*").OrderByDescending(r => r.Name))
{
var match = Regex.Match(net4xdir.Name, @"net(?<ver>4\d+)");
if (!match.Success)
continue;

// just return it.
return net4xdir;

}

// no match, return root folder
return root;
}

/// <summary>
Expand Down Expand Up @@ -3498,10 +3647,10 @@ private static Guid GetGuidAttributeValue(string str_path)
#endif
}

static int LoadPlugInHelper(IntPtr path, IntPtr pluginInfo, IntPtr errorMessage, int displayDebugInfo)
static int LoadPlugInHelper(IntPtr path, IntPtr pluginInfo, IntPtr errorMessage, int displayDebugInfo, bool bIsDirectoryInstall)
{
string str_path = StringWrapper.GetStringFromPointer (path);
int rc = (int)PlugIn.LoadPlugInHelper(str_path, pluginInfo, errorMessage, displayDebugInfo != 0);
int rc = (int)PlugIn.LoadPlugInHelper(str_path, pluginInfo, errorMessage, displayDebugInfo != 0, bIsDirectoryInstall);
return rc;
}

Expand Down Expand Up @@ -3560,7 +3709,7 @@ static int LoadSkinHelper(IntPtr path, int displayDebugInfo)
internal delegate void ShutdownRDKCallback();
static readonly ShutdownRDKCallback m_rdk_shutdown_callback = ShutDownRhinoCommon_RDK;

internal delegate int LoadPluginCallback(IntPtr path, IntPtr pluginInfo, IntPtr errorMessage, int displayDebugInfo);
internal delegate int LoadPluginCallback(IntPtr path, IntPtr pluginInfo, IntPtr errorMessage, int displayDebugInfo, bool bIsDirectoryInstall);
static readonly LoadPluginCallback m_loadplugin_callback = LoadPlugInHelper;
internal delegate int LoadSkinCallback(IntPtr path, int displayDebugInfo);
static readonly LoadSkinCallback m_loadskin_callback = LoadSkinHelper;
Expand Down
35 changes: 12 additions & 23 deletions src/dotnet/opennurbs/opennurbs_3dm_settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,16 @@ public EarthAnchorPoint()
m_ptr = UnsafeNativeMethods.ON_EarthAnchorPoint_New();
}

internal EarthAnchorPoint(IntPtr ptr)
{
m_ptr = ptr;
}

internal EarthAnchorPoint(Rhino.FileIO.File3dm file3dm)
{
m_ptr = UnsafeNativeMethods.ONX_Model_GetEarthAnchorPoint(file3dm.ConstPointer());
}

#if RHINO_SDK
internal EarthAnchorPoint(RhinoDoc doc)
{
Expand Down Expand Up @@ -2863,12 +2873,12 @@ internal File3dmSettings(File3dm parent)
m_parent = parent;
}

IntPtr ConstPointer()
internal IntPtr ConstPointer()
{
IntPtr ptr_const_parent = m_parent.ConstPointer();
return UnsafeNativeMethods.ONX_Model_3dmSettingsPointer(ptr_const_parent);
}
IntPtr NonConstPointer()
internal IntPtr NonConstPointer()
{
IntPtr ptr_parent = m_parent.NonConstPointer();
return UnsafeNativeMethods.ONX_Model_3dmSettingsPointer(ptr_parent);
Expand Down Expand Up @@ -2918,27 +2928,6 @@ public Point3d ModelBasepoint
}
}

/*
Rhino.DocObjects.EarthAnchorPoint m_earth_anchor;
/// <summary>
/// If set, this is the model's location on the earth. This information is
/// used when the model is used with GIS information.
/// </summary>
Rhino.DocObjects.EarthAnchorPoint EarthAnchorPoint
{
get
{
return m_earth_anchor ?? (m_earth_anchor = new DocObjects.EarthAnchorPoint(this));
}
set
{
if (m_earth_anchor == null)
m_earth_anchor = new DocObjects.EarthAnchorPoint(this);
m_earth_anchor.CopyFrom(value);
}
}
*/

double GetDouble(UnsafeNativeMethods.UnitsTolerancesSettingsDouble which)
{
IntPtr ptr_const_this = ConstPointer();
Expand Down
Loading

0 comments on commit a3bbbff

Please sign in to comment.