Example #1
0
        public static ScriptDomain Load(string path)
        {
            int num;

            List <string> list;
            List <string> list2;

            path = Path.GetFullPath(path);
            AppDomainSetup info = new AppDomainSetup {
                ApplicationBase       = path,
                ShadowCopyFiles       = "true",
                ShadowCopyDirectories = path
            };

            StrongName[]     fullTrustAssemblies = new StrongName[0];
            System.AppDomain domain = System.AppDomain.CreateDomain("ScriptDomain_" + (path.GetHashCode() * Environment.TickCount).ToString("X"), null, info, new PermissionSet(PermissionState.Unrestricted), fullTrustAssemblies);
            domain.InitializeLifetimeService();
            ScriptDomain domain2 = null;

            try
            {
                domain2 = (ScriptDomain)domain.CreateInstanceFromAndUnwrap(typeof(ScriptDomain).Assembly.Location, typeof(ScriptDomain).FullName);
            }
            catch (Exception exception1)
            {
                string[] strArray2 = new string[] { "Failed to create script domain '", domain.FriendlyName, "':", Environment.NewLine, exception1.ToString() };
                GTA.Log("[ERROR]", strArray2);
                System.AppDomain.Unload(domain);
                return(null);
            }
            string[] message = new string[] { "Loading scripts from '", path, "' into script domain '", domain.FriendlyName, "' ..." };
            GTA.Log("[INFO]", message);
            if (!Directory.Exists(path))
            {
                string[] strArray7 = new string[] { "Failed to reload scripts because the directory is missing." };
                GTA.Log("[ERROR]", strArray7);
                return(domain2);
            }
            else
            {
                list2 = new List <string>();
                list  = new List <string>();
                try
                {
                    list2.AddRange(Directory.GetFiles(path, "*.vb", SearchOption.AllDirectories));
                    list2.AddRange(Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories));
                    list.AddRange(Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories));
                }
                catch (Exception exception5)
                {
                    string[] strArray6 = new string[] { "Failed to reload scripts:", Environment.NewLine, exception5.ToString() };
                    GTA.Log("[ERROR]", strArray6);
                    System.AppDomain.Unload(domain);
                    return(null);
                }
                num = 0;
            }
            while (true)
            {
                if (num >= list.Count)
                {
                    List <string> .Enumerator enumerator2 = list2.GetEnumerator();
                    if (enumerator2.MoveNext())
                    {
                        do
                        {
                            string current = enumerator2.Current;
                            domain2.LoadScript(current);
                        }while (enumerator2.MoveNext());
                    }
                    List <string> .Enumerator enumerator = list.GetEnumerator();
                    if (enumerator.MoveNext())
                    {
                        do
                        {
                            string current = enumerator.Current;
                            domain2.LoadAssembly(current);
                        }while (enumerator.MoveNext());
                    }
                    break;
                }
                try
                {
                    string assemblyFile = list[num];
                    if (AssemblyName.GetAssemblyName(assemblyFile).Name == "ScriptHookVDotNet")
                    {
                        string[] strArray5 = new string[] { "Removing assembly file '", Path.GetFileName(assemblyFile), "'." };
                        GTA.Log("[WARNING]", strArray5);
                        num += -1;
                        list.RemoveAt(num);
                        try
                        {
                            File.Delete(assemblyFile);
                        }
                        catch (Exception exception6)
                        {
                            string[] strArray4 = new string[] { "Failed to delete assembly file:", Environment.NewLine, exception6.ToString() };
                            GTA.Log("[ERROR]", strArray4);
                        }
                    }
                }
                catch (BadImageFormatException)
                {
                }
                catch (Exception exception7)
                {
                    string[] strArray3 = new string[] { "Failed to load assembly ", list[num], Environment.NewLine, exception7.ToString() };
                    GTA.Log("[ERROR]", strArray3);
                }
                num++;
            }
            return(domain2);
        }
Example #2
0
        public static ScriptDomain Load(string path)
        {
            if (!Path.IsPathRooted(path))
            {
                path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), path);
            }

            // Create AppDomain
            var setup = new AppDomainSetup();

            setup.ApplicationBase       = path;
            setup.ShadowCopyFiles       = "true";
            setup.ShadowCopyDirectories = path;

            var appdomain = AppDomain.CreateDomain("ScriptDomain_" + (path.GetHashCode() * Environment.TickCount).ToString("X"), null, setup, new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted));

            appdomain.InitializeLifetimeService();

            Log("[DEBUG]", "Path ", path);

            ScriptDomain scriptdomain = null;

            try
            {
                scriptdomain = (ScriptDomain)(appdomain.CreateInstanceFromAndUnwrap(typeof(ScriptDomain).Assembly.Location, typeof(ScriptDomain).FullName));
            }
            catch (Exception ex)
            {
                Log("[ERROR]", "Failed to create script domain':", Environment.NewLine, ex.ToString());

                AppDomain.Unload(appdomain);

                return(null);
            }

            Log("[INFO]", "Loading scripts from '", path, "' ...");

            if (Directory.Exists(path))
            {
                var filenameScripts    = new List <string>();
                var filenameAssemblies = new List <string>();

                try
                {
                    filenameScripts.AddRange(Directory.GetFiles(path, "*.vb", SearchOption.AllDirectories));
                    filenameScripts.AddRange(Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories));
                    filenameAssemblies.AddRange(Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories));
                }
                catch (Exception ex)
                {
                    Log("[ERROR]", "Failed to reload scripts:", Environment.NewLine, ex.ToString());

                    AppDomain.Unload(appdomain);

                    return(null);
                }

                for (int i = 0; i < filenameAssemblies.Count; i++)
                {
                    var filename     = filenameAssemblies[i];
                    var assemblyName = AssemblyName.GetAssemblyName(filename);

                    try
                    {
                        if (AssemblyName.GetAssemblyName(filename).Name.StartsWith("ScriptHookVDotNet", StringComparison.OrdinalIgnoreCase))
                        {
                            Log("[WARNING]", "Removing assembly file '", Path.GetFileName(filename), "'.");

                            filenameAssemblies.RemoveAt(i--);

                            try
                            {
                                File.Delete(filename);
                            }
                            catch (Exception ex)
                            {
                                Log("[ERROR]", "Failed to delete assembly file:", Environment.NewLine, ex.ToString());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log("[ERROR]", "Failed to load assembly file '", Path.GetFileName(filename), "':", Environment.NewLine, ex.ToString());
                    }
                }

                foreach (string filename in filenameScripts)
                {
                    scriptdomain.LoadScript(filename);
                }
                foreach (string filename in filenameAssemblies)
                {
                    scriptdomain.LoadAssembly(filename);
                }
            }
            else
            {
                Log("[ERROR]", "Failed to reload scripts because the directory is missing.");
            }

            return(scriptdomain);
        }