Esempio n. 1
0
 static void Main(string[] args)
 {
     try
     {
         //var engine = new V8Engine();
         var engine  = new V8Engine(false);
         var context = engine.CreateContext();
         engine.SetContext(context);
         using (var result = engine.Execute("'Hello World!'"))
         {
             Console.WriteLine(result.AsString);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.GetFullErrorMessage());
     }
     Console.ReadKey(true);
 }
        protected override async Task OnLoadAsync()
        {
            var scriptsDir = Path.Combine(m_Runtime.WorkingDirectory, "scripts");
            var nativeDir  = Path.Combine(m_Runtime.WorkingDirectory, "native");

            if (!s_Initialized)
            {
                if (!Directory.Exists(nativeDir))
                {
                    Directory.CreateDirectory(nativeDir);
                }

                var latestPackageId = await m_NuGetPackageManager.GetLatestPackageIdentityAsync("V8.NET");

                var nupkgFile     = m_NuGetPackageManager.GetNugetPackageFile(latestPackageId);
                var packageReader = new PackageArchiveReader(nupkgFile);
                foreach (var file in packageReader.GetFiles("contentFiles"))
                {
                    if (!file.Contains("netstandard2.0"))
                    {
                        continue;
                    }

                    var fileName = Path.GetFileName(file);
                    var entry    = packageReader.GetEntry(file);
                    using var stream = entry.Open();
                    var ms = new FileStream(Path.Combine(nativeDir, fileName), FileMode.Create);
                    await stream.CopyToAsync(ms);

                    ms.Close();
                    stream.Close();
                }

                Loader.AlternateRootSubPath = nativeDir;
                s_Initialized = true;
            }

            if (!Directory.Exists(scriptsDir))
            {
                Directory.CreateDirectory(scriptsDir);
            }
            else
            {
                foreach (var directory in Directory.GetDirectories(scriptsDir))
                {
                    m_Logger.LogInformation($"[loading] Script: {directory}");

                    var filePath = Path.Combine(directory, "startup.js");
                    if (!File.Exists(filePath))
                    {
                        continue;
                    }

                    var scriptId = new DirectoryInfo(directory).Name;

                    m_Engine = new V8Engine(true);
                    var globalContext = m_Engine.GetContext();
                    var v8Context     = m_Engine.CreateContext();

                    var scriptLifeTime = m_LifetimeScope.BeginLifetimeScope(builder =>
                    {
                        builder.Register(ctx => m_Engine)
                        .As <V8Engine>()
                        .SingleInstance()
                        .OwnedByLifetimeScope();

                        builder.Register(ctx => v8Context)
                        .As <Context>()
                        .SingleInstance()
                        .OwnedByLifetimeScope();
                    });

                    try
                    {
                        var serviceProvider = scriptLifeTime.Resolve <IServiceProvider>();
                        m_Engine.SetContext(v8Context);
                        m_Engine.GlobalObject.SetProperty("logger", ActivatorUtilities.CreateInstance <ScriptLogger>(serviceProvider, scriptId), memberSecurity: ScriptMemberSecurity.Locked);
                        m_Engine.GlobalObject.SetProperty("openmod", ActivatorUtilities.CreateInstance <OpenModFunctions>(serviceProvider), memberSecurity: ScriptMemberSecurity.Locked);
                        var script = m_Engine.LoadScript(filePath, throwExceptionOnError: true);
                        m_Engine.Execute(script, throwExceptionOnError: true, trackReturn: false);
                        m_Engine.SetContext(globalContext);
                        GC.KeepAlive(script);
                    }
                    catch (Exception ex)
                    {
                        m_Logger.LogError(ex.GetBaseException(), $"Script error in script \"{scriptId}\"");
                    }
                }
            }
        }