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;
21 using System.Globalization;
23 namespace Tizen.Runtime
25 public class Preloader
27 const string preloadPath = "/usr/share/dotnet.tizen/preload/";
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()
35 _ = CultureInfo.CurrentCulture.CompareInfo.Compare("abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", CompareOptions.IgnoreCase);
38 public static void Preload()
42 if (!Directory.Exists(preloadPath))
45 string[] paths = Directory.GetFiles(preloadPath, "*.preload");
47 foreach (string path in paths)
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))
54 // ex) 000.Tizen.preload / 0.Tizen.preload
55 if (fileName.IndexOf('.') != 2)
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") ))
62 else if (value == "ElmSharp" && fileName.Contains("NUI"))
64 else if (value == "NUI" && (fileName.Contains("ElmSharp") || fileName.Contains("XSF")))
69 Console.WriteLine("Start preload : " + fileName);
70 BindingFlags bindingFlag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
71 foreach (string line in File.ReadLines(path))
73 if (line.StartsWith('#') || !line.Contains(".dll") || !line.Contains(' '))
76 string[] getWord = line.Split(' ');
77 if (getWord.Length != 2)
80 string assemblyStr = getWord[0].Replace(".dll", "");
81 string typenameStr = getWord[1];
82 string methodStr = "";
83 string parenthesis = "()";
85 if (line.Contains(parenthesis))
87 string[] getMethod = typenameStr.Split('.');
88 methodStr = getMethod[getMethod.Length - 1].Replace(parenthesis, "");
89 typenameStr = typenameStr.Replace("." + methodStr + parenthesis, "");
92 if (assemblyStr == "" || typenameStr == "" || typenameStr.Contains(parenthesis))
94 Console.WriteLine("[Warning] Skip the '" + line + "' in " + fileName);
100 Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyStr));
101 Type type = asm.GetType(typenameStr);
103 Console.WriteLine("[Warning] Check the '" + line + "' in " + fileName);
107 MethodInfo method = type.GetMethod(methodStr, bindingFlag);
108 method.Invoke(null, null);
113 Console.WriteLine("[Error] Fail the '" + line + "' in " + fileName);
117 catch (IOException e)
119 Console.WriteLine(e.ToString());
120 Console.WriteLine("[Error] Failed to open file : " + fileName);
125 GC.WaitForPendingFinalizers();