Hide the preload path from the dlog (#229)
[platform/core/dotnet/launcher.git] / Managed / Tizen.Runtime / Preloader.cs
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.IO;
19 using System.Reflection;
20 using System.Runtime.Loader;
21
22 namespace Tizen.Runtime
23 {
24     public class Preloader
25     {
26         const string preloadPath = "/usr/share/dotnet.tizen/preload/";
27         public static void Preload()
28         {
29             string[] paths = Directory.GetFiles(preloadPath, "*.preload");
30             Array.Sort(paths);
31             foreach (string path in paths)
32             {
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))
36                     continue;
37
38                 // ex) 000.Tizen.preload / 0.Tizen.preload
39                 if (fileName.IndexOf('.') != 2)
40                     continue;
41
42                 try
43                 {
44                     BindingFlags bindingFlag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
45                     foreach (string line in File.ReadLines(path))
46                     {
47                         if (line.StartsWith('#') || !line.Contains(".dll") || !line.Contains(' '))
48                             continue;
49
50                         string[] getWord = line.Split(' ');
51                         if (getWord.Length != 2)
52                             continue;
53
54                         string assemblyStr = getWord[0].Replace(".dll", "");
55                         string typenameStr = getWord[1];
56                         string methodStr = "";
57                         string parenthesis = "()";
58
59                         if (line.Contains(parenthesis))
60                         {
61                             string[] getMethod = typenameStr.Split('.');
62                             methodStr = getMethod[getMethod.Length - 1].Replace(parenthesis, "");
63                             typenameStr = typenameStr.Replace("." + methodStr + parenthesis, "");
64                         }
65
66                         try
67                         {
68                             if (assemblyStr == "")
69                                 continue;
70
71                             Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyStr));
72                             if (asm == null || typenameStr == "")
73                                 continue;
74
75                             Type type = asm.GetType(typenameStr);
76                             if (type == null || methodStr == "")
77                                 continue;
78
79                             MethodInfo method = type.GetMethod(methodStr, bindingFlag);
80                             if (method == null)
81                                 continue;
82
83                             method.Invoke(null, null);
84                         }
85                         catch (Exception e)
86                         {
87                             Console.WriteLine(e.ToString());
88                             Console.WriteLine("[ERROR] Failed to '" + line + "' preload");
89                         }
90                     }
91                 }
92                 catch (IOException e)
93                 {
94                     Console.WriteLine(e.ToString());
95                     Console.WriteLine("[ERROR] Failed to " + path + " file open");
96                 }
97                 finally
98                 {
99                     Console.WriteLine("Success to preload : " + fileName);
100                 }
101             }
102
103             GC.Collect();
104             GC.WaitForPendingFinalizers();
105         }
106     }
107 }