2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Reflection;
20 using System.Runtime.Loader;
22 using System.Runtime.InteropServices;
24 namespace Tizen.Runtime.Coreclr
26 public static class AssemblyManager
28 public static bool Launch(
32 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)]
35 ALog.Debug($"Application Launch path : {path}");
38 DirectoryInfo bindir = new DirectoryInfo(Path.Combine(rootPath, "bin"));
39 DirectoryInfo libdir = new DirectoryInfo(Path.Combine(rootPath, "lib"));
40 if (Directory.Exists(bindir.FullName))
42 CurrentAssemblyLoaderContext.AddSearchableDirectory(bindir.FullName);
44 if (Directory.Exists(libdir.FullName))
46 CurrentAssemblyLoaderContext.AddSearchableDirectory(libdir.FullName);
52 ALog.Debug("Exception in Launch()");
60 public static void Prepared()
64 string preloadPath = "";
65 ICustomAttributeProvider assembly = typeof(AssemblyManager).GetTypeInfo().Assembly;
66 var attributes = assembly.GetCustomAttributes(typeof(DefaultConfigAttribute), false);
67 foreach (DefaultConfigAttribute dca in attributes)
69 ALog.Debug($"{dca.Key} = {dca.Value}");
70 if (dca.Key == "PreloadPath")
72 preloadPath = dca.Value;
76 if (!Initialize(preloadPath))
78 ALog.Debug($"Failed to Initialized with {preloadPath}");
83 ALog.Debug("Exception at Preparing");
88 private static void PrintException(Exception exception)
90 while (exception != null)
92 ALog.Debug(exception.ToString());
93 exception = exception.InnerException;
97 public static void UnhandledExceptionHandler(object sender, object args)
99 TypeInfo unhandledExceptionEventArgsType =
100 Type.GetType("UnhandledExceptionEventArgs").GetTypeInfo();
102 PropertyInfo exception = unhandledExceptionEventArgsType.GetProperty("ExceptionObject");
103 Exception e = (Exception)exception.GetValue(args);
108 public static bool Initialize(string preloadDirectory)
112 // Set UnhandledException handler with reflection
113 // we must replace this to no reflection method after AppDomain is comming in used net standard
114 TypeInfo appdomainType = Type.GetType("System.AppDomain").GetTypeInfo();
115 PropertyInfo currentDomain = appdomainType.GetProperty("CurrentDomain",
116 BindingFlags.Public | BindingFlags.Static);
117 EventInfo unhandledException = appdomainType.GetDeclaredEvent("UnhandledException");
118 object appdomain = currentDomain.GetValue(null, null);
119 MethodInfo handlerInfo = typeof(AssemblyManager).GetTypeInfo().GetDeclaredMethod("UnhandledExceptionHandler");
120 Delegate handler = handlerInfo.CreateDelegate(unhandledException.EventHandlerType);
121 var addMethod = unhandledException.GetAddMethod(true);
122 addMethod.Invoke(appdomain, new[] {handler});
127 ALog.Debug("Exception on set handler for unhandled exception");
133 CurrentAssemblyLoaderContext = new AssemblyLoader();
135 if (!string.IsNullOrEmpty(preloadDirectory))
137 ALog.Debug($"Load from [{preloadDirectory}]");
138 DirectoryInfo d = new DirectoryInfo(preloadDirectory);
139 if (Directory.Exists(d.FullName))
141 CurrentAssemblyLoaderContext.AddSearchableDirectory(d.FullName);
142 string[] dlls = Directory.GetFiles(d.FullName, "*.dll");
144 foreach (string dll in dlls)
146 ALog.Debug($"preload dll : {dll}");
147 CurrentAssemblyLoaderContext.LoadFromAssemblyPath(dll);
154 ALog.Debug("Exception on Initialized");
161 public static void Execute(string dllPath, string[] argv)
165 FileInfo f = new FileInfo(dllPath);
166 if (File.Exists(f.FullName))
169 if (0 == string.Compare(f.FullName, f.FullName.Length - 7, ".ni", 0, 3, StringComparison.OrdinalIgnoreCase))
171 asm = CurrentAssemblyLoaderContext.LoadFromNativeImagePath(f.FullName, null);
175 asm = CurrentAssemblyLoaderContext.LoadFromAssemblyPath(f.FullName);
178 if (asm == null) throw new FileNotFoundException($"{f.FullName} is not found");
179 if (asm.EntryPoint == null) throw new ArgumentException($"{f.FullName} did not have EntryPoint");
180 asm.EntryPoint.Invoke(null, new object[]{argv});
185 ALog.Debug("Exception on Execute");
190 public static AssemblyLoader CurrentAssemblyLoaderContext