Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="?"></param>
        public void ExposeConfig(object targetObject, string niceName, string shortName)
        {
            if (game.IsInitialized)
            {
                throw new InvalidOperationException("Could not expose target object properties after game initialized");
            }

            targetObjects.Add(niceName, targetObject);

            var props = targetObject
                        .GetType()
                        .GetProperties()
                        .Where(p => p.HasAttribute <ConfigAttribute>())
                        .ToList();

            foreach (var prop in props)
            {
                var key  = shortName + "." + prop.Name;
                var cvar = new ConfigVariable(niceName, shortName, prop.Name, prop, targetObject);

                try {
                    configVariables.Add(key, cvar);
                } catch (ArgumentException) {
                    Log.Warning("Can not expose property {0}. Skipped.", key);
                }
            }
        }
		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <returns></returns>
		public static IEnumerable<ConfigVariable> GetConfigVariables ( IEnumerable<GameModule.ModuleBinding> bindings )
		{
			var list = new List<ConfigVariable>();

			foreach ( var bind in bindings ) {

				var prefix			=	bind.ShortName;
				var configObject	=	bind.Module;

				if (configObject==null) {
					continue;
				}

				foreach ( var prop in configObject.GetConfigurationProperties() ) {

					var cfgVar	=	new ConfigVariable( prefix, prop.Name, prop, configObject );
					
					list.Add( cfgVar ); 
				}
			}

			return list;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="input"></param>
		/// <param name="args"></param>
		/// <param name="variable"></param>
		/// <param name="suggestions"></param>
		/// <returns></returns>
		Suggestion AutoCompleteVariable ( string input, string[] args, ConfigVariable variable )
		{
			var suggestion = new Suggestion(input);

			var type = variable.Property.PropertyType;
			var candidates = new string[0];

			//
			//	Gather possible values :
			//
			if (type==typeof(bool)) {
				candidates = new string[]{"True", "False"};
			} else if (type.IsEnum) {
				candidates = Enum.GetNames(type);
			} else {
				candidates = new string[]{variable.Get()};
			}

			//
			//	Only name of the variables is entered.
			//	Just show possible values.
			//	
			if (args.Length==1) {	
				suggestion.Set( args[0] + " ");
				suggestion.AddRange( candidates.Select( c1 => args[0] + " " + c1 ) );
				return suggestion;
			}

			//
			//	Select candidates that starts with entered value.
			//
			candidates = candidates
				.Where( c => c.StartsWith( args[1], StringComparison.OrdinalIgnoreCase) )
				.ToArray();

			var longest = LongestCommon( candidates );


			suggestion.AddRange( candidates.Select( c1 => args[0] + " " + c1 ) );

			//	add quotes if longest common contains spaces :
			if (longest!=null && longest.Any( c => char.IsWhiteSpace(c) )) {
				longest = "\"" + longest;// + "\"";
				if (candidates.Length==1) {
					//	only on suggestion - close quotes.
					longest += "\"";
				}
			} else {
			}

			suggestion.Set( string.Format("{0} {1}", args[0], longest) );

			return suggestion;
		}