summaryrefslogtreecommitdiff
path: root/Tizen.Runtime/Tizen.Runtime.Coreclr/AssemblyManager.cs
blob: ec342d5978f9d3484cf8e88c9884a73affbd0d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using System.Linq;
using System.Runtime.InteropServices;

namespace Tizen.Runtime.Coreclr
{
    public static class AssemblyManager
    {
        public static bool Launch(
                [In] string rootPath,
                [In] string path,
                [In] int argc,
                [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)]
                [In] string[] argv)
        {
            ALog.Debug($"Application Launch path : {path}");
            try
            {
                DirectoryInfo bindir = new DirectoryInfo(Path.Combine(rootPath, "bin"));
                DirectoryInfo libdir = new DirectoryInfo(Path.Combine(rootPath, "lib"));
                if (Directory.Exists(bindir.FullName))
                {
                    CurrentAssemblyLoaderContext.AddSearchableDirectory(bindir.FullName);
                }
                if (Directory.Exists(libdir.FullName))
                {
                    CurrentAssemblyLoaderContext.AddSearchableDirectory(libdir.FullName);
                }
                Execute(path, argv);
            }
            catch(Exception e)
            {
                ALog.Debug("Exception in Launch()");
                PrintException(e);
                return false;
            }

            return true;
        }

        public static void Prepared()
        {
            try
            {
                string preloadPath = "";
                ICustomAttributeProvider assembly = typeof(AssemblyManager).GetTypeInfo().Assembly;
                var attributes = assembly.GetCustomAttributes(typeof(DefaultConfigAttribute), false);
                foreach (DefaultConfigAttribute dca in attributes)
                {
                    ALog.Debug($"{dca.Key} = {dca.Value}");
                    if (dca.Key == "PreloadPath")
                    {
                        preloadPath = dca.Value;
                    }
                }

                if (!Initialize(preloadPath))
                {
                    ALog.Debug($"Failed to Initialized with {preloadPath}");
                }
            }
            catch(Exception e)
            {
                ALog.Debug("Exception at Preparing");
                PrintException(e);
            }
        }

        private static void PrintException(Exception exception)
        {
            while (exception != null)
            {
                ALog.Debug(exception.ToString());
                exception = exception.InnerException;
            }
        }

        public static bool Initialize(string preloadDirectory)
        {
            try
            {
                CurrentAssemblyLoaderContext = new AssemblyLoader();

                if (!string.IsNullOrEmpty(preloadDirectory))
                {
                    Console.WriteLine($"[{preloadDirectory}]");
                    DirectoryInfo d = new DirectoryInfo(preloadDirectory);
                    if (Directory.Exists(d.FullName))
                    {
                        CurrentAssemblyLoaderContext.AddSearchableDirectory(d.FullName);
                        string[] dlls = Directory.GetFiles(d.FullName, "*.dll");

                        foreach (string dll in dlls)
                        {
                            ALog.Debug($"preload dll : {dll}");
                            CurrentAssemblyLoaderContext.LoadFromAssemblyPath(dll);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ALog.Debug("Exception on Initialized");
                PrintException(e);
                return false;
            }
            return true;
        }

        public static void Execute(string dllPath, string[] argv)
        {
            try
            {
                FileInfo f = new FileInfo(dllPath);
                if (File.Exists(f.FullName))
                {
                    Assembly asm = CurrentAssemblyLoaderContext.LoadFromAssemblyPath(f.FullName);
                    if (asm == null) throw new FileNotFoundException($"{f.FullName} is not found");
                    if (asm.EntryPoint == null) throw new ArgumentException($"{f.FullName} did not have EntryPoint");
                    asm.EntryPoint.Invoke(null, new object[]{argv});
                }
            }
            catch (Exception e)
            {
                ALog.Debug("Exception on Execute");
                PrintException(e);
            }
        }

        public static AssemblyLoader CurrentAssemblyLoaderContext
        {
            get;
            private set;
        }

    }
}