public async System.Threading.Tasks.Task <DynValue> ExecuteAsync(params object[] args) { if (IsCoroutine) { var co = Coroutine.Coroutine; if (co.State == CoroutineState.Dead || !CheckYieldStatus()) //Doesn't run check yield if coroutine is dead { return(DynValue.Nil); } DynValue ret; if (co.State == CoroutineState.NotStarted) { ret = await co.ResumeAsync(args); } else { ret = await co.ResumeAsync(); } switch (co.State) { case CoroutineState.Suspended: if (ret.IsNotNil()) { try { CurYielder = ret.ToObject <Yielder>(); } catch { //TODO: throw error? } } break; case CoroutineState.Dead: CurYielder = null; if (AutoResetCoroutine) { //Create new coroutine, assign it to our dynvalue Coroutine = ScriptRef.CreateCoroutine(LuaFunc); } break; default: break; } return(ret); } else { //Not coroutine, just call the function var ret = await ScriptRef.CallAsync(LuaFunc, args); return(ret); } }
/// <summary> /// Adds the script reference. /// </summary> /// /// <param name="html">The HTML.</param> /// <param name="reference">The reference.</param> public static void AddScriptReference(this HtmlHelper html, ScriptRef reference) { Page page = _httpContext.GetCurrentHandler(); if (page != null) { PageManager.ConfigureScriptManager(page, reference); } }
public virtual void ResetCoroutine() { if (IsCoroutine) { CurYielder = null; //It is expensive to create coroutines, also DynVal.Assign() function is more expensive than just assigning in C# if (CoroutineState != CoroutineState.NotStarted) { Coroutine = ScriptRef.CreateCoroutine(LuaFunc); } } }
#pragma warning disable 56507 // check for null or empty strings private SrgsRule[] RunOnInit(bool stg) { SrgsRule[] extraRules = null; bool onInitInvoked = false; // Get the name of the onInit method to run string methodName = ScriptRef.OnInitMethod(_scripts, _ruleName); if (methodName != null) { if (_proxy != null) { Exception appDomainException; extraRules = _proxy.OnInit(methodName, _parameters, _onInitParameters, out appDomainException); onInitInvoked = true; if (appDomainException != null) { ExceptionDispatchInfo.Throw(appDomainException); } } else { // call OnInit if any - should be based on Rule Type[] types = new Type[_parameters.Length]; for (int i = 0; i < _parameters.Length; i++) { types[i] = _parameters[i].GetType(); } MethodInfo onInit = GetType().GetMethod(methodName, types); // If somehow we failed to find a constructor, let the system handle it if (onInit != null) { System.Diagnostics.Debug.Assert(_parameters != null); extraRules = (SrgsRule[])onInit.Invoke(this, _parameters); onInitInvoked = true; } else { throw new ArgumentException(SR.Get(SRID.RuleScriptInvalidParameters, _ruleName, _ruleName)); } } } // Cannot have onInit parameters if onInit has not been invoked. if (!stg && !onInitInvoked && _parameters != null) { throw new ArgumentException(SR.Get(SRID.RuleScriptInvalidParameters, _ruleName, _ruleName)); } return(extraRules); }
/// <summary> /// Clone the other scripthook. Also creates a new coroutine with the same settings as the original /// </summary> /// <param name="other"></param> public ScriptFunction(ScriptFunction other) { this.ScriptRef = other.ScriptRef; LuaFunc = other.LuaFunc; IsCoroutine = other.IsCoroutine; AutoResetCoroutine = other.AutoResetCoroutine; Coroutine = IsCoroutine ? ScriptRef.CreateCoroutine(LuaFunc) : null; if (IsCoroutine) { Coroutine.Coroutine.AutoYieldCounter = other.Coroutine.Coroutine.AutoYieldCounter; } FuncType = other.FuncType; }
public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, bool throwException = false) { if (ResourceHelper.TryConfigureScriptManager(scriptReference)) return MvcHtmlString.Empty; var resourceKey = scriptReference.ToString(); var context = helper.ViewContext.HttpContext; var resourceUrl = ResourceHelper.GetWebResourceUrl(scriptReference); if (string.IsNullOrEmpty(resourceUrl)) throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Script reference for {0} is not found.", resourceKey)); return ResourceHelper.RegisterResource(context, resourceKey, resourceUrl, throwException); }
public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, bool throwException = false) { if (ResourceHelper.TryConfigureScriptManager(scriptReference)) return MvcHtmlString.Empty; var resourceKey = scriptReference.ToString(); var context = helper.ViewContext.HttpContext; var resourceUrl = ResourceHelper.GetWebResourceUrl(scriptReference); if (string.IsNullOrEmpty(resourceUrl)) return MvcHtmlString.Empty; return ResourceHelper.RegisterResource(context, resourceKey, resourceUrl, throwException); }
/// <summary> /// Tries the configure script manager. /// </summary> /// <param name="scriptReference">The script reference.</param> /// <returns></returns> private static bool TryConfigureScriptManager(ScriptRef scriptReference) { var page = HttpContext.Current.Handler as Page; ScriptManager scriptManager = null; if (page != null) { scriptManager = PageManager.ConfigureScriptManager(page, scriptReference, false); } if (scriptManager == null) return false; else return true; }
/// <summary> /// Tries the configure script manager. /// </summary> /// <param name="scriptReference">The script reference.</param> /// <param name="httpHandler">The httpHandler.</param> /// <returns></returns> private static bool TryConfigureScriptManager(ScriptRef scriptReference, IHttpHandler httpHandler) { var page = ResourceHelper.GetTopMostPageFromHandler(httpHandler); if (page != null) { var scriptManager = PageManager.ConfigureScriptManager(page, scriptReference, false); if (scriptManager != null) { return(true); } } return(false); }
public static System.Web.Mvc.MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, bool throwException = false) { if (ResourceHelper.TryConfigureScriptManager(scriptReference)) { return(System.Web.Mvc.MvcHtmlString.Empty); } var resourceKey = scriptReference.ToString(); var context = helper.ViewContext.HttpContext; var resourceUrl = ResourceHelper.GetWebResourceUrl(scriptReference); if (string.IsNullOrEmpty(resourceUrl)) { return(System.Web.Mvc.MvcHtmlString.Empty); } return(ResourceHelper.RegisterResource(context, resourceKey, resourceUrl, throwException)); }
public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, bool throwException = false) { if (ResourceHelper.TryConfigureScriptManager(scriptReference)) { return(MvcHtmlString.Empty); } var resourceKey = scriptReference.ToString(); var context = helper.ViewContext.HttpContext; var resourceUrl = ResourceHelper.GetWebResourceUrl(scriptReference); if (string.IsNullOrEmpty(resourceUrl)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Script reference for {0} is not found.", resourceKey)); } return(ResourceHelper.RegisterResource(context, resourceKey, resourceUrl, throwException)); }
/// <summary> /// Executes this function with a callback. The callback will be called when the coroutine dies, or every time if it is a normal function /// </summary> /// <param name="callback"></param> /// <param name="args"></param> /// <returns></returns> public DynValue ExecuteWithCallback(Action callback, params object[] args) { if (IsCoroutine) { if (Coroutine.Coroutine.State == CoroutineState.Dead || !CheckYieldStatus()) //Doesn't run check yield if coroutine is dead { return(null); } DynValue ret = Coroutine.Coroutine.Resume(args); switch (Coroutine.Coroutine.State) { case CoroutineState.Suspended: if (ret.IsNotNil()) { CurYielder = ret.ToObject <Yielder>(); } else { CurYielder = null; } break; case CoroutineState.Dead: CurYielder = null; callback?.Invoke(); if (AutoResetCoroutine) { Coroutine = ScriptRef.CreateCoroutine(LuaFunc); } break; default: break; } return(ret); } else { var ret = ScriptRef.Call(LuaFunc, args); callback?.Invoke(); return(ret); } }
/// <summary> /// Tries the configure script manager. /// </summary> /// <param name="scriptReference">The script reference.</param> /// <returns></returns> private static bool TryConfigureScriptManager(ScriptRef scriptReference) { var page = HttpContext.Current.Handler as Page; ScriptManager scriptManager = null; if (page != null) { scriptManager = PageManager.ConfigureScriptManager(page, scriptReference, false); } if (scriptManager == null) { return(false); } else { return(true); } }
/// <summary> /// Registers JavaScript reference and ensures that it loads maximum once for a page. /// </summary> /// <remarks> /// This helper references the same resource existing in Sitefinity. /// </remarks> /// <param name="helper">The helper.</param> /// <param name="scriptReference">The script reference.</param> /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param> /// <param name="throwException">Indicates whether to throw an exception if the specified section does not exist.</param> /// <param name="tryUseScriptManager">Indicates whether to use script manager (if exists) when register JavaScript reference. /// If it is used the script will always be loaded on the top section of the page.</param> /// <returns> /// MvcHtmlString /// </returns> public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, string sectionName, bool throwException, bool tryUseScriptManager) { if (tryUseScriptManager && ResourceHelper.TryConfigureScriptManager(scriptReference, helper.ViewContext.HttpContext.CurrentHandler)) { return(System.Web.Mvc.MvcHtmlString.Empty); } var references = PageManager.GetScriptReferences(scriptReference).Select(r => new MvcScriptReference(r)); StringBuilder outputMarkup = new StringBuilder(); foreach (var script in references) { var resourceUrl = script.GetResourceUrl(); outputMarkup.Append(ResourceHelper.RegisterResource(helper.ViewContext.HttpContext, resourceUrl, ResourceType.Js, sectionName, throwException)); } return(MvcHtmlString.Create(outputMarkup.ToString())); }
public static string GetWebResourceUrl(ScriptRef scriptReference) { var config = Config.Get <PagesConfig>().ScriptManager; var scriptConfig = config.ScriptReferences[scriptReference.ToString()]; string resourceUrl = string.Empty; if (config.EnableCdn || (scriptConfig.EnableCdn.HasValue && scriptConfig.EnableCdn.Value)) { resourceUrl = scriptConfig.Path; } else { var page = HttpContext.Current.Handler.GetPageHandler() ?? new PageProxy(null); resourceUrl = page.ClientScript.GetWebResourceUrl( TypeResolutionService.ResolveType("Telerik.Sitefinity.Resources.Reference"), scriptConfig.Name); } return(resourceUrl); }
public static string GetWebResourceUrl(ScriptRef scriptReference) { var config = Config.Get<PagesConfig>().ScriptManager; var scriptConfig = config.ScriptReferences[scriptReference.ToString()]; string resourceUrl = string.Empty; if (config.EnableCdn || (scriptConfig.EnableCdn.HasValue && scriptConfig.EnableCdn.Value)) { resourceUrl = scriptConfig.Path; } else { var page = HttpContext.Current.Handler.GetPageHandler() ?? new PageProxy(null); resourceUrl = page.ClientScript.GetWebResourceUrl( TypeResolutionService.ResolveType("Telerik.Sitefinity.Resources.Reference"), scriptConfig.Name); } return resourceUrl; }
private SrgsRule[] RunOnInit(bool stg) { SrgsRule[] result = null; bool flag = false; string text = ScriptRef.OnInitMethod(_scripts, _ruleName); if (text != null) { if (_proxy != null) { Exception exceptionThrown; result = _proxy.OnInit(text, _parameters, _onInitParameters, out exceptionThrown); flag = true; if (exceptionThrown != null) { throw exceptionThrown; } } else { Type[] array = new Type[_parameters.Length]; for (int i = 0; i < _parameters.Length; i++) { array[i] = _parameters[i].GetType(); } MethodInfo method = GetType().GetMethod(text, array); if (!(method != null)) { throw new ArgumentException(SR.Get(SRID.RuleScriptInvalidParameters, _ruleName, _ruleName)); } result = (SrgsRule[])method.Invoke(this, _parameters); flag = true; } } if (!stg && !flag && _parameters != null) { throw new ArgumentException(SR.Get(SRID.RuleScriptInvalidParameters, _ruleName, _ruleName)); } return(result); }
public OptimizedUnityCoroutine(ScriptRunnerBase runner, string functionBody, params string[] parameters) { UnityCoroutineState = $"{ID}_UNITY_COROUTINE_STATE"; this.ScriptRef = runner.Lua; ScriptRef.Globals[UnityCoroutineState] = true; string nxtV = $"{ID}_NextVals"; StringBuilder sb = new StringBuilder(); //1 based lua indices, ugh for (int i = 1; i < parameters.Length + 1; i++) { sb.Append($"{parameters[i - 1]} = {nxtV}[{i}] "); } string NextParamsStr = sb.ToString(); string funcString = $"function({string.Join(",", parameters)}) while (true) do{Environment.NewLine}{functionBody}{Environment.NewLine}{UnityCoroutineState} = false " + $"local {nxtV} = coroutine.yield() " + $"{NextParamsStr} " + $"end end"; LuaFunc = ScriptRef.LoadFunction(funcString); Coroutine = ScriptRef.CreateCoroutine(LuaFunc); }
/// <summary> /// Registers JavaScript reference and ensures that it loads maximum once for a page. /// </summary> /// <remarks> /// This helper references the same resource existing in Sitefinity. /// </remarks> /// <param name="helper">The helper.</param> /// <param name="scriptReference">The script reference.</param> /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param> /// <returns> /// MvcHtmlString /// </returns> public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, string sectionName) { return ResourceHelper.Script(helper, scriptReference, sectionName, true); }
/// <summary> /// Registers JavaScript reference and ensures that it loads maximum once for a page. /// </summary> /// <remarks> /// This helper references the same resource existing in Sitefinity. /// </remarks> /// <param name="helper">The helper.</param> /// <param name="scriptReference">The script reference.</param> /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param> /// <param name="throwException">Indicates whether to throw an exception if the specified section does not exist.</param> /// If it is used the script will always be loaded on the top section of the page.</param> /// <returns> /// MvcHtmlString /// </returns> public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, string sectionName, bool throwException) { return(ResourceHelper.Script(helper, scriptReference, sectionName, throwException, true)); }
/// <summary> /// Registers JavaScript reference and ensures that it loads maximum once for a page. /// </summary> /// <remarks> /// This helper references the same resource existing in Sitefinity. /// </remarks> /// <param name="helper">The helper.</param> /// <param name="scriptReference">The script reference.</param> /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param> /// <param name="attributes">The path to the JavaScript file.</param> /// If it is used the script will always be loaded on the top section of the page.</param> /// <returns> /// MvcHtmlString /// </returns> public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, string sectionName, List <KeyValuePair <string, string> > attributes) { return(ResourceHelper.Script(helper, scriptReference, sectionName, true, true, attributes)); }
/// <summary> /// Registers JavaScript reference and ensures that it loads maximum once for a page. /// </summary> /// <remarks> /// This helper references the same resource existing in Sitefinity. /// </remarks> /// <param name="helper">The helper.</param> /// <param name="scriptReference">The script reference.</param> /// If it is used the script will always be loaded on the top section of the page.</param> /// <returns> /// MvcHtmlString /// </returns> public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference) { return(ResourceHelper.Script(helper, scriptReference, null, false, true)); }
/// <summary> /// Registers JavaScript reference and ensures that it loads maximum once for a page. /// </summary> /// <remarks> /// This helper references the same resource existing in Sitefinity. /// </remarks> /// <param name="helper">The helper.</param> /// <param name="scriptReference">The script reference.</param> /// <param name="tryUseScriptManager">Indicates whether to use script manager(if exists) when register JavaScript reference. /// If it is used the script will always be loaded on the top section of the page.</param> /// <returns> /// MvcHtmlString /// </returns> public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, bool tryUseScriptManager) { return(ResourceHelper.Script(helper, scriptReference, null, false, tryUseScriptManager)); }
/// <summary> /// Executes this function whether it is a normal function or a coroutine. Dead coroutines do not run and return DynValue.Nil /// </summary> /// <param name="args"></param> /// <returns></returns> public DynValue Execute(params object[] args) { try { if (IsCoroutine) { var co = Coroutine.Coroutine; if (co.State == CoroutineState.Dead || !CheckYieldStatus()) //Doesn't run check yield if coroutine is dead { return(DynValue.Nil); } DynValue ret = co.Resume(args); //if (co.State == CoroutineState.NotStarted) //{ // ret = co.Resume(args); //} //else //{ // ret = co.Resume(); //} switch (co.State) { case CoroutineState.Suspended: if (ret.IsNotNil()) { try { CurYielder = ret.ToObject <Yielder>(); } catch //Todo: catch specific exception { //Moonsharp does not have a good way of checking the userdata type //The way to check just throws an error anyways, so this is more efficient } } break; case CoroutineState.Dead: CurYielder = null; if (AutoResetCoroutine) { //Create new coroutine, assign it to our dynvalue Coroutine = ScriptRef.CreateCoroutine(LuaFunc); } break; default: break; } return(ret); } else { //Not coroutine, just call the function var ret = ScriptRef.Call(LuaFunc, args); return(ret); } } catch (Exception ex) { if (ex is InterpreterException ex2) { //For unity throw new Exception(ex2.DecoratedMessage, ex); } else { throw ex; } } }
public void ForceFullReset() { Coroutine = ScriptRef.CreateCoroutine(LuaFunc); started = false; ScriptRef.Globals[$"{ID}_UNITY_COROUTINE_STATE"] = true; }
/// <summary> /// Tries the configure script manager. /// </summary> /// <param name="scriptReference">The script reference.</param> /// <param name="httpHandler">The httpHandler.</param> /// <returns></returns> private static bool TryConfigureScriptManager(ScriptRef scriptReference, IHttpHandler httpHandler) { var page = ResourceHelper.GetTopMostPageFromHandler(httpHandler); if (page != null) { var scriptManager = PageManager.ConfigureScriptManager(page, scriptReference, false); if (scriptManager != null) { return true; } } return false; }
/// <summary> /// Registers JavaScript reference and ensures that it loads maximum once for a page. /// </summary> /// <remarks> /// This helper references the same resource existing in Sitefinity. /// </remarks> /// <param name="helper">The helper.</param> /// <param name="scriptReference">The script reference.</param> /// <returns> /// MvcHtmlString /// </returns> public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference) { return ResourceHelper.Script(helper, scriptReference, null, false); }
/// <summary> /// Registers JavaScript reference and ensures that it loads maximum once for a page. /// </summary> /// <remarks> /// This helper references the same resource existing in Sitefinity. /// </remarks> /// <param name="helper">The helper.</param> /// <param name="scriptReference">The script reference.</param> /// <param name="sectionName">The name of the section that will render this script. If null it will render on the same place of the page</param> /// <param name="throwException">Indicates whether to throw an exception if the specified section does not exist.</param> /// <returns> /// MvcHtmlString /// </returns> public static MvcHtmlString Script(this HtmlHelper helper, ScriptRef scriptReference, string sectionName, bool throwException) { if (ResourceHelper.TryConfigureScriptManager(scriptReference, helper.ViewContext.HttpContext.CurrentHandler)) return System.Web.Mvc.MvcHtmlString.Empty; var references = PageManager.GetScriptReferences(scriptReference).Select(r => new MvcScriptReference(r)); StringBuilder outputMarkup = new StringBuilder(); foreach (var script in references) { var resourceUrl = script.GetResourceUrl(); outputMarkup.Append(ResourceHelper.RegisterResource(helper.ViewContext.HttpContext, resourceUrl, ResourceType.Js, sectionName, throwException)); } return MvcHtmlString.Create(outputMarkup.ToString()); }