protected override void Initialize() { _graphicsDeviceManager.PreferredBackBufferWidth = 960; _graphicsDeviceManager.PreferredBackBufferHeight = 720; _graphicsDeviceManager.ApplyChanges(); _engine = new Jint.Engine(cfg => cfg.Strict(false).AllowClr(typeof(GameInstance).Assembly)); _resourceManager = new Services.ResourceManager(GraphicsDevice); _audioContext = new AudioContext(_resourceManager); _backgroundRenderTarget = new RenderTarget2D(GraphicsDevice, 320, 240); _graphics3dTarget = new RenderTarget2D(GraphicsDevice, 320, 240, false, SurfaceFormat.Color, DepthFormat.Depth24); _foregroundRenderTarget = new RenderTarget2D(GraphicsDevice, 320, 240); _spriteBatch = new SpriteBatch(GraphicsDevice); _mainEffect = new BasicEffect(GraphicsDevice); _flatShadedEffect = new BasicEffect(GraphicsDevice) { FogEnabled = true, FogColor = new Vector3(), FogStart = 90, FogEnd = 100, LightingEnabled = false, TextureEnabled = false, VertexColorEnabled = true }; _texturedEffect = new BasicEffect(GraphicsDevice) { FogEnabled = true, FogColor = new Vector3(), FogStart = 90, FogEnd = 100, LightingEnabled = false, TextureEnabled = true }; _scriptingContext = new ScriptingContext(_resourceManager); _graphicsContext3d = new GraphicsContext3d(_resourceManager); _backgroundGraphicsContext = new GraphicsContext2d(_resourceManager); _foregroundGraphicsContext = new GraphicsContext2d(_resourceManager); _inputContext = new InputContext(); _engine.SetValue("engine", _scriptingContext); string pathToIndex = ""; string cwd = Environment.CurrentDirectory; // was a path explicitly stated? if (Environment.GetCommandLineArgs().Length > 1) { pathToIndex = Environment.GetCommandLineArgs()[1]; } // is index in root? else if (File.Exists("index.js")) { pathToIndex = "index.js"; } else { // Where is it? var dirs = Directory.EnumerateDirectories(".."); pathToIndex = dirs.FirstOrDefault(x => File.Exists(Path.Combine(x, "index.js"))); } if (string.IsNullOrEmpty(pathToIndex)) { // can we still not find anything? Console.WriteLine("Can not find index.js... Exiting"); return; } _engine.CommonJS().RunMain(pathToIndex); _resourceManager.CreateTexture("__dither", 320, 240); var r = new Random(4206932); for (int y = 0; y < 240; y++) { for (int x = 0; x < 320; x++) { var c = Color.Transparent; if ((x + y) % 2 == 0) { c = new Color(0, 0, 0, (byte)r.Next() % 16); } _resourceManager.SetPixel("__dither", x, y, c); } } _resourceManager.UpdateTextures(); base.Initialize(); }
public async Task <string> RenderAsync(HttpContext context) { var re = ""; var pathValue = context.Request.Path.Value !.ToString(); if (string.IsNullOrEmpty(pathValue) || pathValue.Equals("/")) { return(re); } var path = pathValue.Substring(1, pathValue.Length - 1); var nomarlPath = context.Request.GetDisplayUrl(); try { //只拿第一层路径 var entryPointName = path.Split('/').FirstOrDefault(); if (string.IsNullOrEmpty(entryPointName)) { return(re); } entryPointName = entryPointName.ToLowerInvariant(); var indexHtml = new FileModel(_hostingEnvironment, entryPointName, "index.html"); if (!indexHtml.IsExist) { return(re); } // 有 但是是文件的话 if (_contentTypeProvider.TryGetContentType(pathValue.ToLower(), out _)) { return(re); } CheckConfigRefresh(); CheckConfigRefresh(entryPointName); var html = indexHtml.GetContent(); re = html; var cacheKey = entryPointName + "_" + indexHtml.LastModifyTime.ToString("yyyMMddHHmmss"); var jsFileContent = new FileModel(_hostingEnvironment, entryPointName, "_server_.js"); dynamic serverJsResult = null; if (jsFileContent.IsExist) { var exports = engine .CommonJS() .RegisterInternalModule("server", typeof(PluginFactory)) .RunMain(jsFileContent.FilePath); try { var jsValue = exports.AsObject().Get("main").Invoke(new JsValue(nomarlPath)).ToString(); if (!string.IsNullOrEmpty(jsValue) && jsValue != "null" && jsValue != "undefined") { serverJsResult = JObject.Parse(jsValue); } } catch (Exception e) { logger.Error($"excute {ConfigHelper.ServerJsFile} fail:" + e.Message); } } if (serverJsResult == null) { serverJsResult = new JObject(); } serverJsResult.GlobalEnv = new JObject(); if (_appsettingsJson != null) { foreach (var jsonItem in _appsettingsJson) { serverJsResult.GlobalEnv[jsonItem.Key] = jsonItem.Value; } } serverJsResult.Env = new JObject(); if (_currentAppsettingsJson != null) { foreach (var jsonItem in _currentAppsettingsJson) { serverJsResult.Env[jsonItem.Key] = jsonItem.Value; } } try { var cacheResult = _engine.Handler.Cache.RetrieveTemplate(cacheKey); if (cacheResult.Success) { var itemple = cacheResult.Template.TemplatePageFactory(); itemple.DisableEncoding = true; string result2 = await _engine.RenderTemplateAsync(itemple, serverJsResult); re = result2; return(re); } string result = await _engine.CompileRenderStringAsync(cacheKey, html, serverJsResult); if (!cacheList.TryGetValue(entryPointName, out var oldCache)) { cacheList.TryAdd(entryPointName, cacheKey); } else { //之前有缓存了 就清掉 _engine.Handler.Cache.Remove(oldCache); cacheList[entryPointName] = cacheKey; } re = result; context.Response.Cookies.Append("spa_project", entryPointName); } catch (Exception e) { logger.Error(e.ToString()); } } catch (Exception e1) { //ignore logger.Error(e1.ToString()); } return(re); }