C# .dll with code for type preloading (#57)
[platform/core/dotnet/launcher.git] / Init / Init.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.Reflection;
19 using System.Runtime.Loader;
20 using System.IO;
21
22 namespace Tizen.Init {
23
24     class TypeLoader
25     {
26         static string[,] assem_type = new string[,] 
27         {
28             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Box"},
29             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Button"},
30             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Check"},
31             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Color"},
32             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Conformant"},
33             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.DateTimeSelector"},
34             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.DisplayRotation"},
35             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.EdjeObject"},
36             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.EdjeObject+SignalData"},
37             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.EdjeTextPartObject"},
38             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Elementary"},
39             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Entry"},
40             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.EvasKeyEventArgs"},
41             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.EvasMap"},
42             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.EvasObject"},
43             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.FocusDirection"},
44             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.GenList"},
45             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.GestureLayer+GestureType"},
46             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.IInvalidatable"},
47             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Image"},
48             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.ItemObject+SignalData"},
49             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Label"},
50             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Layout"},
51             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Naviframe"},
52             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.NaviItem"},
53             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Point3D"},
54             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.ProgressBar"},
55             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Radio"},
56             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Rect"},
57             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Rectangle"},
58             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Scroller"},
59             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Size"},
60             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Slider"},
61             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Spinner"},
62             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Widget"},
63             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.Window"},
64             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "ElmSharp.WrapType"},
65             {"/usr/share/dotnet.tizen/framework/ElmSharp.dll", "Interop"},
66             {"/usr/share/dotnet.tizen/framework/ElmSharp.Wearable.dll", "ElmSharp.Wearable.CircleDateTimeSelector"},
67             {"/usr/share/dotnet.tizen/framework/ElmSharp.Wearable.dll", "ElmSharp.Wearable.CircleGenList"},
68             {"/usr/share/dotnet.tizen/framework/ElmSharp.Wearable.dll", "ElmSharp.Wearable.CircleScroller"},
69             {"/usr/share/dotnet.tizen/framework/ElmSharp.Wearable.dll", "ElmSharp.Wearable.CircleSpinner"},
70             {"/usr/share/dotnet.tizen/framework/ElmSharp.Wearable.dll", "ElmSharp.Wearable.ICircleWidget"},
71             {"/usr/share/dotnet.tizen/framework/ElmSharp.Wearable.dll", "ElmSharp.Wearable.IRotaryActionWidget"},
72             {"/usr/share/dotnet.tizen/framework/ElmSharp.Wearable.dll", "Interop"},
73             {"/usr/share/dotnet.tizen/netcoreapp/System.IO.FileSystem.dll", "Interop"},
74             {"/usr/share/dotnet.tizen/netcoreapp/System.IO.FileSystem.dll", "<PrivateImplementationDetails>"},
75             {"/usr/share/dotnet.tizen/netcoreapp/System.IO.FileSystem.dll", "System.IO.File"},
76             {"/usr/share/dotnet.tizen/netcoreapp/System.Security.Cryptography.Algorithms.dll", "Interop"},
77             {"/usr/share/dotnet.tizen/netcoreapp/System.Security.Cryptography.Algorithms.dll", "System.Security.Cryptography.MD5"},
78             {"/usr/share/dotnet.tizen/netcoreapp/System.Collections.dll", "<PrivateImplementationDetails>"},
79             {"/usr/share/dotnet.tizen/netcoreapp/System.Collections.dll", "System.Collections.Generic.HashSet`1"},
80             {"/usr/share/dotnet.tizen/netcoreapp/System.Collections.dll", "System.Collections.Generic.Queue`1"},
81             {"/usr/share/dotnet.tizen/netcoreapp/System.Private.Uri.dll", "<PrivateImplementationDetails>"},
82             {"/usr/share/dotnet.tizen/netcoreapp/System.Private.Uri.dll", "System.Uri"},
83             {"/usr/share/dotnet.tizen/netcoreapp/System.Private.Xml.dll", "<PrivateImplementationDetails>"},
84             {"/usr/share/dotnet.tizen/netcoreapp/System.Private.Xml.dll", "System.Xml.IXmlLineInfo"},
85             {"/usr/share/dotnet.tizen/netcoreapp/System.Private.Xml.dll", "System.Xml.IXmlNamespaceResolver"},
86             {"/usr/share/dotnet.tizen/netcoreapp/System.Runtime.dll", "System.Collections.Generic.ISet`1"},
87             {"/usr/share/dotnet.tizen/netcoreapp/System.Runtime.dll", "System.Reflection.RuntimeReflectionExtensions"},
88             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.Collections.ObjectModel.ObservableCollection`1"},
89             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.Collections.Specialized.INotifyCollectionChanged"},
90             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.Collections.Specialized.NotifyCollectionChangedAction"},
91             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.Collections.Specialized.NotifyCollectionChangedEventArgs"},
92             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.Collections.Specialized.NotifyCollectionChangedEventHandler"},
93             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.ComponentModel.INotifyPropertyChanged"},
94             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.ComponentModel.PropertyChangedEventArgs"},
95             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.ComponentModel.PropertyChangedEventHandler"},
96             {"/usr/share/dotnet.tizen/netcoreapp/System.ObjectModel.dll", "System.Windows.Input.ICommand"},
97             {"/usr/share/dotnet.tizen/netcoreapp/System.ComponentModel.dll", "System.IServiceProvider"},
98             {"/usr/share/dotnet.tizen/netcoreapp/System.Linq.dll", "System.Linq.Enumerable"},
99             {"/usr/share/dotnet.tizen/netcoreapp/System.Linq.dll", "System.Linq.EnumerableSorter`1"},
100             {"/usr/share/dotnet.tizen/netcoreapp/System.Linq.dll", "System.Linq.IIListProvider`1"},
101             {"/usr/share/dotnet.tizen/netcoreapp/System.Linq.dll", "System.Linq.IOrderedEnumerable`1"},
102             {"/usr/share/dotnet.tizen/netcoreapp/System.Linq.dll", "System.Linq.IPartition`1"},
103             {"/usr/share/dotnet.tizen/netcoreapp/System.Linq.dll", "System.Linq.OrderedEnumerable`1"},
104             {"/usr/share/dotnet.tizen/netcoreapp/System.Linq.Expressions.dll", "System.Linq.Expressions.ExpressionVisitor"},
105             {"/usr/share/dotnet.tizen/netcoreapp/System.Security.Cryptography.Primitives.dll", "System.Security.Cryptography.HashAlgorithm"},
106             {"/usr/share/dotnet.tizen/framework/Tizen.Applications.Common.dll", "Interop"},
107             {"/usr/share/dotnet.tizen/framework/Tizen.Applications.Common.dll", "Tizen.Applications.Application"},
108             {"/usr/share/dotnet.tizen/framework/Tizen.Applications.Common.dll", "Tizen.Applications.CoreApplication"},
109             {"/usr/share/dotnet.tizen/framework/Tizen.Applications.Common.dll", "Tizen.Applications.CoreBackend.EventType"},
110             {"/usr/share/dotnet.tizen/framework/Tizen.Applications.Common.dll", "Tizen.Applications.TizenSynchronizationContext"},
111             {"/usr/share/dotnet.tizen/framework/Tizen.Applications.UI.dll", "Tizen.Applications.CoreUIApplication"},
112             {"/usr/share/dotnet.tizen/framework/Tizen.Applications.UI.dll", "Interop"},
113             {"/usr/share/dotnet.tizen/framework/Tizen.dll", "Interop"},
114             {"/usr/share/dotnet.tizen/framework/Tizen.Log.dll", "Interop"},
115             {"/usr/share/dotnet.tizen/framework/Tizen.Log.dll", "Tizen.Log"},
116             {"/usr/share/dotnet.tizen/framework/Tizen.System.Information.dll", "Tizen.System.Information"}
117         };
118
119         static void PreloadEnd()
120         {
121                 GC.Collect();
122                 GC.WaitForPendingFinalizers();
123         }
124
125         static int PreloadTypes()
126         {
127                 string loaded_assem_name = new string("");
128                 Assembly loaded_assem = null;
129                 for (int i = 0; i < assem_type.GetLength(0); i++)
130                 {
131                         if (assem_type[i,0] != loaded_assem_name)
132                         {
133                                 loaded_assem_name = assem_type[i,0];
134                                 loaded_assem = LoadAssembly(loaded_assem_name);
135                         }
136                         if (loaded_assem != null)
137                         {
138                                 LoadType(loaded_assem, assem_type[i,1]);
139                         }
140                 }
141                 PreloadEnd();
142
143                 return 0;
144         }
145         static Assembly LoadAssembly(string path)
146         {
147                 var context = AssemblyLoadContext.Default;
148                 Assembly ret = null;
149
150                 try
151                 {
152                         if (File.Exists(path.Replace(".dll", ".ni.dll")))
153                         {
154                                 ret = context.LoadFromNativeImagePath(path.Replace(".dll", ".ni.dll"), path);
155                         } else
156                         {//by concept LoadFromNativeImagePath has to do fallbacks to
157                          //properly passed IL asms, but it does not
158                                 ret = context.LoadFromAssemblyPath(path);
159                         }
160                 }
161                 catch (Exception e) 
162                 {
163                         Console.WriteLine(e.ToString());
164
165                         return null;
166                 }
167
168                 return ret;
169         }
170         static int LoadType(Assembly assem, string typeName)
171         {
172                 try
173                 {
174                         var tp = assem.GetType(typeName);
175                 }
176                 catch
177                 {
178                         Console.WriteLine("Tizen.Init.TypeLoader.PreloadTypes can't load type " + typeName + " from assembly " + assem.ToString());
179                         return 1;
180                 }
181
182                 return 0;
183         }
184     }
185 }