public override int GetHashCode() { #pragma warning disable RECS0025 // Non-readonly field referenced in 'GetHashCode()' var intPtr = ICVar.GetIntPtr(m_icVar); #pragma warning restore RECS0025 // Non-readonly field referenced in 'GetHashCode()' return(intPtr.GetHashCode()); }
private static void RegisterSubAttribute <TAttribute, TAttributeProperty>(Assembly targetAssembly) where TAttribute : ConsoleVariableAttribute where TAttributeProperty : class, new() { Assembly assembly = targetAssembly ?? Assembly.GetExecutingAssembly(); var processedTypes = assembly.GetTypes().SelectMany(x => x.GetFields(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)) .Where(y => y.GetCustomAttributes().OfType <TAttribute>().Any()) .ToList(); foreach (FieldInfo fieldInfo in processedTypes) { Type fieldType = fieldInfo.FieldType; var attributeProperty = fieldInfo.GetValue(null) as TAttributeProperty; var consoleVariableAttribute = fieldInfo.GetCustomAttribute <ConsoleVariableAttribute>() as ConsoleVariableAttribute; if (fieldType == typeof(TAttributeProperty)) { if (attributeProperty != null) { ICVar newVar = null; ProcessConsoleVariableProperty(consoleVariableAttribute, attributeProperty, out newVar); } else { throw new ConsoleVariableConfigurationException("Console Variable Attribute " + consoleVariableAttribute.Name + " cannot be applied to static variable " + fieldType.FullName + " which is null!"); } } else // attribute declared does not match property value { throw new ConsoleVariableConfigurationException("Attribute [" + consoleVariableAttribute.Name + "] type does not match Property type " + fieldType); } } }
/// <summary> /// Unregistered an existing control variable, This unregisters from both control variables that are both registered natively and via managed /// </summary> /// <param name="consoleVariable">Will be set to null if unregistered successfully</param> /// <returns>true if control variable is unregistered successfully</returns> public static bool UnRegister(ref ICVar consoleVariable) { if (consoleVariable == null) { return(false); } bool unregistered = false; //check if registered via C# var consoleVariableName = consoleVariable.GetName(); if (s_variablesDelegates.ContainsKey(consoleVariableName)) { ConsoleVariableItem consoleVariableItem = null; s_variablesDelegates.TryGetValue(consoleVariableName, out consoleVariableItem); unregistered = s_variablesDelegates.Remove(consoleVariableName); if (unregistered) { consoleVariableItem.m_icVar = null; consoleVariableItem.m_managedConsoleVariableDelegate = null; } } // likelihood that console variable is preregistered via C++ and not C# Global.gEnv.pConsole.UnregisterVariable(consoleVariableName); unregistered |= (Global.gEnv.pConsole.GetCVar(consoleVariableName) == null); if (unregistered) { consoleVariable = null; } return(unregistered); }
private void ExecuteCommand(string rawCmd) { string[] parts = rawCmd.Split(' '); string cvarOrCmd = parts[0].ToLower(); if (Array.IndexOf(CVars.GetNames(), cvarOrCmd) > -1) { string value = rawCmd.Substring(cvarOrCmd.Length).Trim(); if (value.Length > 0) { string oldValue = CVars.RawGet(cvarOrCmd).Serialize(); CVars.RawGet(cvarOrCmd).Deserialize(value); Console.WriteLine("Updated `{0}` from '{1}' to '{2}'.", cvarOrCmd, oldValue, value); } else { CVar <bool> boolCVar = CVars.RawGet(cvarOrCmd) as CVar <bool>; if (boolCVar != null) { bool oldValue = boolCVar.Value; boolCVar.Value = !oldValue; Console.WriteLine("Updated `{0}` from '{1}' to '{2}'.", cvarOrCmd, oldValue, boolCVar.Value); } } } else if (cvarOrCmd == "clear") { _consoleItems.Clear(); } else if (cvarOrCmd == "help") { if (parts.Length > 1) { if (Array.IndexOf(CVars.GetNames(), parts[1].ToLower()) > -1) { ICVar rawCVar = CVars.RawGet(parts[1].ToLower()); Console.WriteLine("`{0}`: {1}", parts[1].ToLower(), rawCVar.GetDescription()); Console.WriteLine("\tDefault value: {0}", rawCVar.SerializeDefault()); Console.WriteLine("\tCurrent value: {0}", rawCVar.Serialize()); } else { Console.WriteLine("`{0}` is not a valid CVar.", parts[1]); } } else { Console.WriteLine("Prints the description of a CVar."); Console.WriteLine("Format: `help <cvar name>`"); } } else { Console.WriteLine("CVar `{0}` not found.", cvarOrCmd); } }
internal ConsoleVariableAttributeStringProperty(string name, string value, string comments, uint flags) { ICVar icVar = null; var registered = ConsoleVariable.Register(name, value, flags, comments, OnValueChanged, out icVar); if (!registered) { throw new ConsoleVariableConfigurationException(GetType() + " cannot be created for attribute name " + name); } SetInternalContent(value, icVar); }
/// <summary> /// Retrieves the managed control variable callback delegate. Only for control variables that are registered via managed /// </summary> /// <param name="icVar">Any control variable that is registered via managed</param> /// <param name="managedConsoleVariableDelegate">the managed control variable delegate if function returns true</param> /// <returns>true if the control variable exists</returns> public static bool GetManagedConsoleVariableFunctionDelegate(ICVar icVar, out ManagedConsoleVariableFunctionDelegate managedConsoleVariableDelegate) { var consoleVariableName = icVar.GetName(); managedConsoleVariableDelegate = null; if (!s_variablesDelegates.ContainsKey(consoleVariableName)) { return(false); } managedConsoleVariableDelegate = s_variablesDelegates[consoleVariableName].m_managedConsoleVariableDelegate; return(true); }
/// <summary> /// Changes the control variable callback to the specified delegate. /// Note: Only control variables that are registered via managed class CryEngine.ConsoleVariable can be changed. Control Variables that are predefined in system cannot be changed. /// </summary> /// <param name="icVar"></param> /// <param name="managedConsoleVariableDelegate"></param> /// <returns></returns> public static bool SetManagedConsoleVariableFunctionDelegate(ICVar icVar, ManagedConsoleVariableFunctionDelegate managedConsoleVariableDelegate) { var consoleVariableName = icVar.GetName(); if (!s_variablesDelegates.ContainsKey(consoleVariableName)) { return(false); } //change the console variable item to map to new delegate, the c++ callback "OnConsoleVariableChanged(IntPtr consoleVariableArg)" remains unchanged ConsoleVariableItem consoleVariableItem = null; s_variablesDelegates.TryGetValue(consoleVariableName, out consoleVariableItem); consoleVariableItem.m_managedConsoleVariableDelegate = managedConsoleVariableDelegate; return(true); }
public bool Equals(ConsoleVariableItem item) { if (item == null) { return(false); } var intPtr = ICVar.GetIntPtr(item.m_icVar); var myIntPtr = ICVar.GetIntPtr(m_icVar); if (intPtr.Equals(myIntPtr)) { return(true); } return(false); }
public static bool Register(string consoleVariableName, float consoleVariableValue, uint nFlags, string commandHelp, ManagedConsoleVariableFunctionDelegate managedConsoleVariableDelegate, out ICVar newICVar) { //check if console variable with same name exists ICVar existingICVar = Global.gEnv.pConsole.GetCVar(consoleVariableName); if (existingICVar != null) { newICVar = null; return(false); } CryEngine.NativeInternals.IConsole.AddConsoleVariableFloat(System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(s_myDelegate), consoleVariableName, consoleVariableValue, nFlags, commandHelp); newICVar = Global.gEnv.pConsole.GetCVar(consoleVariableName); if (newICVar == null) { return(false); } ConsoleVariableItem newItem = new ConsoleVariableItem { m_icVar = newICVar, m_managedConsoleVariableDelegate = managedConsoleVariableDelegate }; s_variablesDelegates.Add(consoleVariableName, newItem); return(true); }
internal void OnValueChanged(ICVar var) { m_value = var.GetIVal(); }
// Does not activate managed console variable listener. Called when console variable has been successfully registered natively internal void SetInternalContent(float newValue, ICVar icVar) { m_value = newValue; m_icVar = icVar; }
private static void ProcessConsoleVariableProperty(ConsoleVariableAttribute consoleVariableAttribute, object property, out ICVar icvar) { icvar = null; var stringProperty = property as ConsoleVariableAttributeStringProperty; if (stringProperty != null) { var stringAttribute = consoleVariableAttribute as ConsoleVariableStringAttribute; string value = stringProperty.Content ?? stringAttribute.Content; var success = ConsoleVariable.Register(consoleVariableAttribute.Name, value, consoleVariableAttribute.Flags, consoleVariableAttribute.Comments, stringProperty.OnValueChanged, out icvar); if (success) { stringProperty.SetInternalContent(value, icvar); } return; } var integerProperty = property as ConsoleVariableAttributeIntegerProperty; if (integerProperty != null) { var integerAttribute = consoleVariableAttribute as ConsoleVariableIntegerAttribute; int value = integerAttribute.Content.HasValue ? integerAttribute.Content.Value : integerProperty.Content; var success = ConsoleVariable.Register(consoleVariableAttribute.Name, value, consoleVariableAttribute.Flags, consoleVariableAttribute.Comments, integerProperty.OnValueChanged, out icvar); if (success) { integerProperty.SetInternalContent(value, icvar); } return; } var floatProperty = property as ConsoleVariableAttributeFloatProperty; if (floatProperty != null) { var floatAttribute = consoleVariableAttribute as ConsoleVariableFloatAttribute; float value = floatAttribute.Content.HasValue ? floatAttribute.Content.Value : floatProperty.Content; var success = ConsoleVariable.Register(consoleVariableAttribute.Name, value, consoleVariableAttribute.Flags, consoleVariableAttribute.Comments, floatProperty.OnValueChanged, out icvar); if (success) { floatProperty.SetInternalContent(value, icvar); } return; } var integer64Property = property as ConsoleVariableAttributeInteger64Property; if (integer64Property != null) { var integer64Attribute = consoleVariableAttribute as ConsoleVariableInteger64Attribute; long value = integer64Attribute.Content.HasValue ? integer64Attribute.Content.Value : integer64Property.Content; var success = ConsoleVariable.Register(consoleVariableAttribute.Name, value, consoleVariableAttribute.Flags, consoleVariableAttribute.Comments, integer64Property.OnValueChanged, out icvar); if (success) { integer64Property.SetInternalContent(value, icvar); } } }
public override int GetHashCode() { IntPtr intPtr = ICVar.GetIntPtr(m_icVar); return(intPtr.GetHashCode()); }
internal void OnValueChanged(ICVar var) { m_value = var.GetString(); }
private static void OnConsoleVariableChanged(IntPtr consoleVariableArg) { var foundValue = s_variablesDelegates.First(item => ICVar.GetIntPtr(item.Value.m_icVar) == consoleVariableArg); foundValue.Value.m_managedConsoleVariableDelegate?.Invoke(foundValue.Value.m_icVar); }
// does not activate managed console variable listener. Called when console variable has been successfully registered natively internal void SetInternalContent(string newValue, ICVar icVar) { m_value = newValue; m_icVar = icVar; }
private void DrawCVarWindow() { if (CVars.Get <bool>("debug_show_cvar_viewer")) { ImGui.SetNextWindowSize(new System.Numerics.Vector2(500, 400), ImGuiCond.FirstUseEver); ImGui.Begin("CVar Viewer", ref CVars.Get <bool>("debug_show_cvar_viewer")); if (ImGui.Button("Save##Control")) { CVars.Save(); } ImGui.SameLine(); if (ImGui.Button("Load##Control")) { CVars.SynchronizeFromFile(); } string[] names = CVars.GetNames(); Array.Sort(names); ImGui.Columns(4); ImGui.Text("CVar"); ImGui.NextColumn(); ImGui.Text("Value"); ImGui.NextColumn(); ImGui.Text("Default"); ImGui.NextColumn(); ImGui.Text("Description"); ImGui.NextColumn(); ImGui.Separator(); foreach (string name in names) { if (ImGui.Button(name)) { _cvarEditing = name; } ImGui.NextColumn(); ICVar cvar = CVars.RawGet(name); if (_cvarEditing == name) { switch (cvar) { case CVar <byte> numCVar: { int num = (int)numCVar.Value; ImGui.InputInt("##" + name, ref num); numCVar.Value = (byte)num; } break; case CVar <short> numCVar: { int num = (int)numCVar.Value; ImGui.InputInt("##" + name, ref num); numCVar.Value = (short)num; } break; case CVar <int> numCVar: ImGui.InputInt("##" + name, ref numCVar.Value); break; case CVar <float> numCVar: ImGui.InputFloat("##" + name, ref numCVar.Value); break; case CVar <double> numCVar: ImGui.InputDouble("##" + name, ref numCVar.Value); break; case CVar <bool> boolCVar: ImGui.Checkbox("##" + name, ref boolCVar.Value); break; case CVar <Color> colorCVar: { System.Numerics.Vector3 color = new System.Numerics.Vector3(colorCVar.Value.R / 255.0f, colorCVar.Value.G / 255.0f, colorCVar.Value.B / 255.0f); ImGui.ColorEdit3("##" + name, ref color); colorCVar.Value.R = (byte)(color.X * 255); colorCVar.Value.G = (byte)(color.Y * 255); colorCVar.Value.B = (byte)(color.Z * 255); } break; default: { string strValue = cvar.Serialize(); byte[] buff = new byte[strValue.Length + 500]; Array.Copy(Encoding.UTF8.GetBytes(strValue), buff, strValue.Length); ImGui.InputText("##" + name, buff, (uint)buff.Length); cvar.Deserialize(Encoding.UTF8.GetString(buff, 0, buff.Length)); } break; } } else { ImGui.Text(cvar.Serialize()); } ImGui.NextColumn(); if (ImGui.Button(cvar.SerializeDefault() + "##" + name)) { cvar.Reset(); } ImGui.NextColumn(); ImGui.TextWrapped(cvar.GetDescription()); ImGui.NextColumn(); } ImGui.End(); } }