public T Get <T>(string section, string key, T defaultValue, int index = 0) { T newValue = default(T); try { newValue = ConvertUtil.ConvertFromStr <T>(getSection(section).Get(key, index)); } catch { newValue = defaultValue; } return(newValue); }
public object Get(Type type, string section, string key, object defaultValue, int index = 0) { object newValue = null; try { newValue = ConvertUtil.ConvertFromStr(type, getSection(section).Get(key, index)); } catch { newValue = defaultValue; } return(newValue); }
/// <summary> /// Runs a command. Returns true if a valid command was found, false if not. If there are invalid parameters or other errors but the command /// is a registered command, will return true. /// </summary> /// <param name="commandString"></param> /// <returns></returns> public static bool RunCom(string commandString) { string[] vars = commandString.Split(' '); string command = vars[0]; if (!registeredCommands.ContainsKey(command)) { Debug.LogError($"Command {command} is not registered!"); return(false); } Com c = registeredCommands[command]; if (c.parameters.Length == 1 && c.parameters[0].ParameterType == typeof(string)) { if (vars.Length == 1) { Debug.LogError("Needs to have a string!"); return(false); } Debug.Log($"runnning com: {commandString}"); try { c.method.Invoke(c.sourceObject, new object[] { commandString.MinusFirst(command.Length + 1) }); } catch (Exception ex) { Debug.LogException(ex); } return(true); } if (vars.Length - 1 != c.parameters.Length) { Debug.LogError($"Parameter mismatch, params should be: {c.GetParamsTypeStr()}"); return(false); } Debug.Log($"runnning com: {commandString}"); object[] objParams = new object[c.parameters.Length]; for (int i = 0; i < objParams.Length; i++) { try { objParams[i] = ConvertUtil.ConvertFromStr(c.parameters[i].ParameterType, vars[i + 1]); } catch (Exception ex) { Debug.LogException(ex); return(true); } } try { c.method.Invoke(c.sourceObject, objParams); } catch (Exception ex) { Debug.LogException(ex); } return(true); }