2 * Copyright (c) 2019 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 namespace Tizen.Runtime
24 public class Preloader
26 const string preloadPath = "/usr/share/dotnet.tizen/preload/";
27 public static void Preload()
29 string[] paths = Directory.GetFiles(preloadPath, "*.preload");
31 foreach (string path in paths)
33 string fileName = Path.GetFileName(path);
34 // ex) Tizen.preload / 0A.Tizen.preload / A0.Tizen.preload / .0.Tizen.preload / .00.Tizen.preload
35 if (!char.IsNumber(fileName, 0) || !char.IsNumber(fileName, 1))
38 // ex) 000.Tizen.preload / 0.Tizen.preload
39 if (fileName.IndexOf('.') != 2)
44 BindingFlags bindingFlag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
45 foreach (string line in File.ReadLines(path))
47 if (line.StartsWith('#') || !line.Contains(".dll") || !line.Contains(' '))
50 string[] getWord = line.Split(' ');
51 if (getWord.Length != 2)
54 string assemblyStr = getWord[0].Replace(".dll", "");
55 string typenameStr = getWord[1];
56 string methodStr = "";
57 string parenthesis = "()";
59 if (line.Contains(parenthesis))
61 string[] getMethod = typenameStr.Split('.');
62 methodStr = getMethod[getMethod.Length - 1].Replace(parenthesis, "");
63 typenameStr = typenameStr.Replace("." + methodStr + parenthesis, "");
68 if (assemblyStr == "")
71 Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyStr));
72 if (asm == null || typenameStr == "")
75 Type type = asm.GetType(typenameStr);
76 if (type == null || methodStr == "")
79 MethodInfo method = type.GetMethod(methodStr, bindingFlag);
83 method.Invoke(null, null);
87 Console.WriteLine(e.ToString());
88 Console.WriteLine("[ERROR] Failed to '" + line + "' preload");
94 Console.WriteLine(e.ToString());
95 Console.WriteLine("[ERROR] Failed to " + path + " file open");
99 Console.WriteLine("Success to preload : " + fileName);
104 GC.WaitForPendingFinalizers();