Integrate managed code into Tizen.Runtime (#228)
[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                 // ex) Tizen.preload / 0A.Tizen.preload / A0.Tizen.preload / .0.Tizen.preload / .00.Tizen.preload
34                 if (!char.IsNumber(Path.GetFileName(path), 0) || !char.IsNumber(Path.GetFileName(path), 1))
35                     continue;
36
37                 // ex) 000.Tizen.preload / 0.Tizen.preload
38                 if (Path.GetFileName(path).IndexOf('.') != 2)
39                     continue;
40
41                 try
42                 {
43                     BindingFlags bindingFlag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
44                     foreach (string line in File.ReadLines(path))
45                     {
46                         if (line.StartsWith('#') || !line.Contains(".dll") || !line.Contains(' '))
47                             continue;
48
49                         string[] getWord = line.Split(' ');
50                         if (getWord.Length != 2)
51                             continue;
52
53                         string assemblyStr = getWord[0].Replace(".dll", "");
54                         string typenameStr = getWord[1];
55                         string methodStr = "";
56                         string parenthesis = "()";
57
58                         if (line.Contains(parenthesis))
59                         {
60                             string[] getMethod = typenameStr.Split('.');
61                             methodStr = getMethod[getMethod.Length - 1].Replace(parenthesis, "");
62                             typenameStr = typenameStr.Replace("." + methodStr + parenthesis, "");
63                         }
64
65                         try
66                         {
67                             if (assemblyStr == "")
68                                 continue;
69
70                             Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyStr));
71                             if (asm == null || typenameStr == "")
72                                 continue;
73
74                             Type type = asm.GetType(typenameStr);
75                             if (type == null || methodStr == "")
76                                 continue;
77
78                             MethodInfo method = type.GetMethod(methodStr, bindingFlag);
79                             if (method == null)
80                                 continue;
81
82                             method.Invoke(null, null);
83                         }
84                         catch (Exception e)
85                         {
86                             Console.WriteLine(e.ToString());
87                             Console.WriteLine("[ERROR] Failed to '" + line + "' preload");
88                         }
89                     }
90                 }
91                 catch (IOException e)
92                 {
93                     Console.WriteLine(e.ToString());
94                     Console.WriteLine("[ERROR] Failed to " + path + " file open");
95                 }
96                 finally
97                 {
98                     Console.WriteLine("Success to preload : " + path);
99                 }
100             }
101
102             GC.Collect();
103             GC.WaitForPendingFinalizers();
104         }
105     }
106 }