Exemple #1
0
        public static int ExecuteResource(IntPtr arg, int argLength)
        {
            if (argLength < Marshal.SizeOf(typeof(LibArgs)))
            {
                return(1);
            }

            var libArgs      = Marshal.PtrToStructure <LibArgs>(arg);
            var resourcePath = Marshal.PtrToStringUTF8(libArgs.ResourcePath);
            var resourceName = Marshal.PtrToStringUTF8(libArgs.ResourceName);
            var resourceMain = Marshal.PtrToStringUTF8(libArgs.ResourceMain);

            var resourceDllPath             = GetPath(resourcePath, resourceMain);
            var resourceAssemblyLoadContext =
                new ResourceAssemblyLoadContext(resourceDllPath, resourcePath, resourceName);

            _loadContexts[resourceDllPath] = resourceAssemblyLoadContext;

            Assembly resourceAssembly;

            try
            {
                resourceAssembly = resourceAssemblyLoadContext.LoadFromAssemblyPath(resourceDllPath);
            }
            catch (FileLoadException e)
            {
                Console.WriteLine($"Threw a exception while loading the assembly \"{resourceDllPath}\": {e}");
                return(1);
            }

            var altVNetAssembly = resourceAssemblyLoadContext.LoadFromAssemblyName(new AssemblyName("AltV.Net"));

            foreach (var type in altVNetAssembly.GetTypes())
            {
                if (type.Name != "ModuleWrapper")
                {
                    continue;
                }
                type.GetMethod("MainWithAssembly", BindingFlags.Public | BindingFlags.Static)?.Invoke(null,
                                                                                                      new object[] { libArgs.ServerPointer, libArgs.ResourcePointer, resourceAssemblyLoadContext });
                break;
            }

            return(0);
        }
Exemple #2
0
        public static int ExecuteResource(IntPtr arg, int argLength)
        {
            if (argLength < Marshal.SizeOf(typeof(LibArgs)))
            {
                return(1);
            }

            var libArgs      = Marshal.PtrToStructure <LibArgs>(arg);
            var resourcePath = Marshal.PtrToStringUTF8(libArgs.ResourcePath);
            var resourceName = Marshal.PtrToStringUTF8(libArgs.ResourceName);
            var resourceMain = Marshal.PtrToStringUTF8(libArgs.ResourceMain);

            string resourceDllPath;

            /*if (resourceMain.EndsWith(".csproj"))
             * {
             *  Func<AssemblyLoadContext, AssemblyName, Assembly> resolving = (context, assemblyName) =>
             *  {
             *      var dllPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "runtime" +
             *                    Path.DirectorySeparatorChar + assemblyName.Name + ".dll";
             *      if (File.Exists(dllPath))
             *      {
             *          try
             *          {
             *              return context.LoadFromAssemblyPath(dllPath);
             *          }
             *          catch (Exception exception)
             *          {
             *              Console.WriteLine(exception);
             *          }
             *      }
             *
             *      dllPath = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + assemblyName.Name +
             *                ".dll";
             *
             *
             *      if (File.Exists(dllPath))
             *      {
             *          try
             *          {
             *              return context.LoadFromAssemblyPath(dllPath);
             *          }
             *          catch (Exception exception)
             *          {
             *              Console.WriteLine(exception);
             *          }
             *      }
             *
             *      return null;
             *  };
             *  AssemblyLoadContext.Default.Resolving += resolving;
             *  var task = Task.Run(
             *      async () => await CompileResource(resourceName, GetPath(resourcePath, resourceMain)));
             *  var (result, dllPath) = task.GetAwaiter().GetResult();
             *  AssemblyLoadContext.Default.Resolving -= resolving;
             *  if (!result)
             *  {
             *      Console.WriteLine($"Compilation of resource {resourceName} wasn't successfully.");
             *  }
             *
             *  resourceDllPath = dllPath;
             * }
             * else
             * {*/
            resourceDllPath = GetPath(resourcePath, resourceMain);
            //}

            var resourceAssemblyLoadContext =
                new ResourceAssemblyLoadContext(resourceDllPath, resourcePath, resourceName);

            LoadContexts[resourceDllPath] = resourceAssemblyLoadContext;

            resourceAssemblyLoadContext.SharedAssemblyNames.UnionWith(GetResourceSharedAssemblies(resourceDllPath));

            try
            {
                resourceAssemblyLoadContext.LoadFromAssemblyPath(resourceDllPath);
                var newList = new HashSet <string>();
                foreach (var referencedAssembly in resourceAssemblyLoadContext.SharedAssemblyNames)
                {
                    var refAssembly =
                        AssemblyLoadContext.Default.LoadFromAssemblyPath(GetPath(resourcePath,
                                                                                 referencedAssembly + ".dll"));
                    foreach (var referencedAssembly2 in refAssembly.GetReferencedAssemblies())
                    {
                        newList.Add(referencedAssembly2.Name);
                    }
                }

                resourceAssemblyLoadContext.SharedAssemblyNames.UnionWith(newList);
            }
            catch (FileLoadException e)
            {
                Console.WriteLine($"Threw a exception while loading the assembly \"{resourceDllPath}\": {e}");
                return(1);
            }

            var isDefaultLoaded = false;
            var isShared        = resourceAssemblyLoadContext.SharedAssemblyNames.Contains("AltV.Net");

            foreach (var assembly in AssemblyLoadContext.Default.Assemblies)
            {
                if (assembly.GetName().Name != "AltV.Net")
                {
                    continue;
                }
                isDefaultLoaded = true;
                break;
            }

            if (!isDefaultLoaded && isShared)
            {
                var defaultAltVNetAssembly =
                    AssemblyLoadContext.Default.LoadFromAssemblyPath(GetPath(resourcePath, "AltV.Net.dll"));
                InitAltVAssembly(defaultAltVNetAssembly, libArgs, resourceAssemblyLoadContext, resourceName);
            }

            resourceAssemblyLoadContext.SharedAssemblyNames.Remove("AltV.Net");
            var altVNetAssembly = resourceAssemblyLoadContext.LoadFromAssemblyName(new AssemblyName("AltV.Net"));

            InitAltVAssembly(altVNetAssembly, libArgs, resourceAssemblyLoadContext, resourceName);
            if (isShared)
            {
                resourceAssemblyLoadContext.SharedAssemblyNames.Add("AltV.Net");
            }

            return(0);
        }
Exemple #3
0
        public static int ExecuteResource(IntPtr arg, int argLength)
        {
            if (argLength < Marshal.SizeOf(typeof(LibArgs)))
            {
                return(1);
            }

            var libArgs      = Marshal.PtrToStructure <LibArgs>(arg);
            var resourcePath = Marshal.PtrToStringUTF8(libArgs.ResourcePath);
            var resourceName = Marshal.PtrToStringUTF8(libArgs.ResourceName);
            var resourceMain = Marshal.PtrToStringUTF8(libArgs.ResourceMain);

            var isCollectible = Environment.GetEnvironmentVariable("CSHARP_MODULE_DISABLE_COLLECTIBLE") == null;

            var resourceDllPath             = GetPath(resourcePath, resourceMain);
            var resourceAssemblyLoadContext =
                new ResourceAssemblyLoadContext(resourceDllPath, resourcePath, resourceName, isCollectible);

            _loadContexts[resourceDllPath] = resourceAssemblyLoadContext;

            Assembly resourceAssembly;

            try
            {
                resourceAssembly = resourceAssemblyLoadContext.LoadFromAssemblyPath(resourceDllPath);
            }
            catch (FileLoadException e)
            {
                Console.WriteLine($"Threw a exception while loading the assembly \"{resourceDllPath}\": {e}");
                return(1);
            }

            var altVNetAssembly = resourceAssemblyLoadContext.LoadFromAssemblyName(new AssemblyName("AltV.Net"));

            foreach (var type in altVNetAssembly.GetTypes())
            {
                switch (type.Name)
                {
                case "ModuleWrapper":
                    type.GetMethod("MainWithAssembly", BindingFlags.Public | BindingFlags.Static)?.Invoke(null,
                                                                                                          new object[] { libArgs.ServerPointer, libArgs.ResourcePointer, resourceAssemblyLoadContext });
                    break;

                case "HostWrapper":
                    try
                    {
                        type.GetMethod("SetStartTracingDelegate", BindingFlags.Public | BindingFlags.Static)
                        ?.Invoke(
                            null,
                            new object[] { new Action <string>(StartTracing) });
                        type.GetMethod("SetStopTracingDelegate", BindingFlags.Public | BindingFlags.Static)?.Invoke(
                            null,
                            new object[] { new Action(StopTracing) });
                        type.GetMethod("SetImportDelegate", BindingFlags.Public | BindingFlags.Static)?.Invoke(
                            null,
                            new object[] { new ImportDelegate(Import) });
                        type.GetMethod("SetExportDelegate", BindingFlags.Public | BindingFlags.Static)?.Invoke(
                            null,
                            new object[]
                        {
                            new Action <string, object>((key, value) => { Export(resourceName, key, value); })
                        });
                        var traceSizeChangeDelegate = (Action <long>)type.GetMethod("GetTraceSizeChangeDelegate",
                                                                                    BindingFlags.Public | BindingFlags.Static)?.Invoke(
                            null,
                            new object[]
                        {
                        });
                        _traceSizeChangeDelegates[resourceName] = traceSizeChangeDelegate;
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine(
                            "Consider updating the AltV.Net nuget package and AltV.Net.Host.dll to be able to access all csharp-module features.");
                        Console.WriteLine(exception);
                    }

                    break;
                }
            }

            return(0);
        }