-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunScanner.cs
More file actions
114 lines (97 loc) · 3.82 KB
/
RunScanner.cs
File metadata and controls
114 lines (97 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// RunScanner.cs - loader journal for run_journal.exe
using System;
using System.IO;
using System.Reflection;
using NXOpen;
public class RunScanner
{
public static void Main(string[] args)
{
Session s = Session.GetSession();
s.ListingWindow.Open();
try
{
// 1) Определяем baseDir:
// - если передан config=... -> папка конфига
// - иначе journalPath=... -> папка journal
// - иначе текущая директория
string baseDir = ResolveBaseDir(args);
// 2) Где лежит DLL сканера?
string dllPath = GetArgValue(args, "scannerDll");
if (string.IsNullOrWhiteSpace(dllPath))
{
dllPath = Path.Combine(baseDir, "NxPrtAttributeScanner.dll");
}
else
{
dllPath = Environment.ExpandEnvironmentVariables(dllPath);
if (!Path.IsPathRooted(dllPath))
dllPath = Path.GetFullPath(Path.Combine(baseDir, dllPath));
else
dllPath = Path.GetFullPath(dllPath);
}
if (!File.Exists(dllPath))
throw new FileNotFoundException("Scanner DLL not found: " + dllPath);
// 3) Подгружаем сборку и вызываем HeadlessEntryPoint.Main(args)
var asm = Assembly.LoadFrom(dllPath);
var t = asm.GetType("HeadlessEntryPoint", throwOnError: true);
var m = t.GetMethod("Main", BindingFlags.Public | BindingFlags.Static);
if (m == null)
throw new MissingMethodException("HeadlessEntryPoint.Main not found in " + dllPath);
m.Invoke(null, new object[] { args });
}
catch (TargetInvocationException tie)
{
var ex = tie.InnerException ?? tie;
s.ListingWindow.WriteLine("FATAL ERROR (inner):");
s.ListingWindow.WriteLine(ex.ToString());
}
catch (Exception ex)
{
s.ListingWindow.WriteLine("FATAL ERROR:");
s.ListingWindow.WriteLine(ex.ToString());
}
}
private static string ResolveBaseDir(string[] args)
{
// config=... (самый надёжный якорь)
string configPath = GetArgValue(args, "config");
if (!string.IsNullOrWhiteSpace(configPath))
{
configPath = Path.GetFullPath(Environment.ExpandEnvironmentVariables(configPath));
string dir = Path.GetDirectoryName(configPath);
if (!string.IsNullOrWhiteSpace(dir))
return dir;
}
// journalPath=... (опционально)
string journalPath = GetArgValue(args, "journalPath");
if (!string.IsNullOrWhiteSpace(journalPath))
{
journalPath = Path.GetFullPath(Environment.ExpandEnvironmentVariables(journalPath));
string dir = Path.GetDirectoryName(journalPath);
if (!string.IsNullOrWhiteSpace(dir))
return dir;
}
// fallback
return Directory.GetCurrentDirectory();
}
private static string GetArgValue(string[] args, string key)
{
if (args == null) return null;
for (int i = 0; i < args.Length; i++)
{
var a = args[i];
if (string.IsNullOrWhiteSpace(a)) continue;
int eq = a.IndexOf('=');
if (eq <= 0) continue;
string k = a.Substring(0, eq).Trim();
if (!k.Equals(key, StringComparison.OrdinalIgnoreCase)) continue;
return a.Substring(eq + 1).Trim().Trim('"');
}
return null;
}
public static int GetUnloadOption(string dummy)
{
return (int)NXOpen.Session.LibraryUnloadOption.Immediately;
}
}