Check ascii casing in the candidate process (#254)
[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             string[] paths = Directory.GetFiles(preloadPath, "*.preload");
43             Array.Sort(paths);
44             foreach (string path in paths)
45             {
46                 string fileName = Path.GetFileName(path);
47                 // ex) Tizen.preload / 0A.Tizen.preload / A0.Tizen.preload / .0.Tizen.preload / .00.Tizen.preload
48                 if (!char.IsNumber(fileName, 0) || !char.IsNumber(fileName, 1))
49                     continue;
50
51                 // ex) 000.Tizen.preload / 0.Tizen.preload
52                 if (fileName.IndexOf('.') != 2)
53                     continue;
54
55                 try
56                 {
57                     Console.WriteLine("Start preload : " + fileName);
58                     BindingFlags bindingFlag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
59                     foreach (string line in File.ReadLines(path))
60                     {
61                         if (line.StartsWith('#') || !line.Contains(".dll") || !line.Contains(' '))
62                             continue;
63
64                         string[] getWord = line.Split(' ');
65                         if (getWord.Length != 2)
66                             continue;
67
68                         string assemblyStr = getWord[0].Replace(".dll", "");
69                         string typenameStr = getWord[1];
70                         string methodStr = "";
71                         string parenthesis = "()";
72
73                         if (line.Contains(parenthesis))
74                         {
75                             string[] getMethod = typenameStr.Split('.');
76                             methodStr = getMethod[getMethod.Length - 1].Replace(parenthesis, "");
77                             typenameStr = typenameStr.Replace("." + methodStr + parenthesis, "");
78                         }
79
80                         if (assemblyStr == "" || typenameStr == "" || typenameStr.Contains(parenthesis))
81                         {
82                             Console.WriteLine("[Warning] Skip the '" + line + "' in " + fileName);
83                             continue;
84                         }
85
86                         try
87                         {
88                             Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyStr));
89                             Type type = asm.GetType(typenameStr);
90                             if (type == null)
91                                 Console.WriteLine("[Warning] Check the '" + line + "' in " + fileName);
92
93                             if (methodStr != "")
94                             {
95                                 MethodInfo method = type.GetMethod(methodStr, bindingFlag);
96                                 method.Invoke(null, null);
97                             }
98                         }
99                         catch
100                         {
101                             Console.WriteLine("[Error] Fail the '" + line + "' in " + fileName);
102                         }
103                     }
104                 }
105                 catch (IOException e)
106                 {
107                     Console.WriteLine(e.ToString());
108                     Console.WriteLine("[Error] Failed to open file : " + fileName);
109                 }
110             }
111
112             GC.Collect();
113             GC.WaitForPendingFinalizers();
114         }
115     }
116 }