8dd07b415880c21c6ed2a7a2349a71d38f690766
[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 using System.Globalization;
22
23 namespace Tizen.Runtime
24 {
25     public class Preloader
26     {
27         const string preloadPath = "/usr/share/dotnet.tizen/preload/";
28
29         // If current culture's casing for ASCII is the same as invariant, it can take a fast path
30         // than calling out to the OS for culture-aware casing.
31         // However, in certain languages, the following function may be significantly slowed down.
32         // To avoid that kind situation, call it in advance on the candidate process.
33         private static void CheckAsciiCasing()
34         {
35             _ = CultureInfo.CurrentCulture.CompareInfo.Compare("abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", CompareOptions.IgnoreCase);
36         }
37
38         public static void Preload()
39         {
40             CheckAsciiCasing();
41
42             if (!Directory.Exists(preloadPath))
43                 return;
44
45             string[] paths = Directory.GetFiles(preloadPath, "*.preload");
46             Array.Sort(paths);
47             foreach (string path in paths)
48             {
49                 string fileName = Path.GetFileName(path);
50                 // ex) Tizen.preload / 0A.Tizen.preload / A0.Tizen.preload / .0.Tizen.preload / .00.Tizen.preload
51                 if (!char.IsNumber(fileName, 0) || !char.IsNumber(fileName, 1))
52                     continue;
53
54                 // ex) 000.Tizen.preload / 0.Tizen.preload
55                 if (fileName.IndexOf('.') != 2)
56                     continue;
57
58                 string value = System.Environment.GetEnvironmentVariable("TIZEN_UIFW");
59                 // if TIZEN_UIFW is not set, do not preload UI related dll
60                 if (value == null && (fileName.Contains("NUI") || fileName.Contains("ElmSharp") || fileName.Contains("XSF") ))
61                     continue;
62                 else if (value == "ElmSharp" && fileName.Contains("NUI"))
63                     continue;
64                 else if (value == "NUI" && (fileName.Contains("ElmSharp") || fileName.Contains("XSF")))
65                     continue;
66
67                 try
68                 {
69                     Console.WriteLine("Start preload : " + fileName);
70                     BindingFlags bindingFlag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
71                     foreach (string line in File.ReadLines(path))
72                     {
73                         if (line.StartsWith('#') || !line.Contains(".dll") || !line.Contains(' '))
74                             continue;
75
76                         string[] getWord = line.Split(' ');
77                         if (getWord.Length != 2)
78                             continue;
79
80                         string assemblyStr = getWord[0].Replace(".dll", "");
81                         string typenameStr = getWord[1];
82                         string methodStr = "";
83                         string parenthesis = "()";
84
85                         if (line.Contains(parenthesis))
86                         {
87                             string[] getMethod = typenameStr.Split('.');
88                             methodStr = getMethod[getMethod.Length - 1].Replace(parenthesis, "");
89                             typenameStr = typenameStr.Replace("." + methodStr + parenthesis, "");
90                         }
91
92                         if (assemblyStr == "" || typenameStr == "" || typenameStr.Contains(parenthesis))
93                         {
94                             Console.WriteLine("[Warning] Skip the '" + line + "' in " + fileName);
95                             continue;
96                         }
97
98                         try
99                         {
100                             Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyStr));
101                             Type type = asm.GetType(typenameStr);
102                             if (type == null)
103                             {
104                                 Console.WriteLine("[Warning] Check the '" + line + "' in " + fileName);
105                                 continue;
106                             }
107
108                             if (methodStr != "")
109                             {
110                                 MethodInfo method = type.GetMethod(methodStr, bindingFlag);
111                                 if (method == null)
112                                 {
113                                     Console.WriteLine("[Warning] Check the '" + line + "' in " + fileName);
114                                     continue;
115                                 }
116                                 method.Invoke(null, null);
117                             }
118                         }
119                         catch
120                         {
121                             Console.WriteLine("[Error] Fail the '" + line + "' in " + fileName);
122                         }
123                     }
124                 }
125                 catch (IOException e)
126                 {
127                     Console.WriteLine(e.ToString());
128                     Console.WriteLine("[Error] Failed to open file : " + fileName);
129                 }
130             }
131
132             GC.Collect();
133             GC.WaitForPendingFinalizers();
134         }
135     }
136 }