Exemplo n.º 1
0
        static void OnConsoleConfigCommand(string arguments, object userData)
        {
            EngineConfig.Parameter parameter = (EngineConfig.Parameter)userData;

            if (string.IsNullOrEmpty(arguments))
            {
                object value = parameter.GetValue();
                Print(string.Format("Value: \"{0}\", Default value: \"{1}\"",
                                    value != null ? value : "(null)", parameter.DefaultValue));
                return;
            }

            try
            {
                if (parameter.Field != null)
                {
                    object value = SimpleTypes.ParseValue(parameter.Field.FieldType,
                                                          arguments);
                    if (value == null)
                    {
                        throw new Exception("Not simple type");
                    }
                    parameter.Field.SetValue(null, value);
                }
                else if (parameter.Property != null)
                {
                    object value = SimpleTypes.ParseValue(parameter.Property.PropertyType,
                                                          arguments);
                    if (value == null)
                    {
                        throw new Exception("Not simple type");
                    }
                    parameter.Property.SetValue(null, value, null);
                }
            }
            catch (FormatException e)
            {
                string s = "";
                if (parameter.Field != null)
                {
                    s = parameter.Field.FieldType.ToString();
                }
                else if (parameter.Property != null)
                {
                    s = parameter.Property.PropertyType.ToString();
                }
                Print(string.Format("Config : Invalid parameter format \"{0}\" {1}", s, e.Message), new ColorValue(1, 0, 0));
            }
        }
Exemplo n.º 2
0
        static void ParseSettingsFromDefaultSettingsConfig()
        {
            var v = DefaultSettingsConfig.GetAttribute("RendererBackend");

            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.RendererBackend = (RendererBackend)Enum.Parse(typeof(RendererBackend), v);
            }

            v = DefaultSettingsConfig.GetAttribute("SimulationVSync");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.SimulationVSync = bool.Parse(v);
            }

            v = DefaultSettingsConfig.GetAttribute("SimulationTripleBuffering");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.SimulationTripleBuffering = bool.Parse(v);
            }

            v = DefaultSettingsConfig.GetAttribute("RendererReportDebugToLog");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.RendererReportDebugToLog = (bool)SimpleTypes.ParseValue(typeof(bool), v);
            }

            v = DefaultSettingsConfig.GetAttribute("UseShaderCache");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.UseShaderCache = (bool)SimpleTypes.ParseValue(typeof(bool), v);
            }

            v = DefaultSettingsConfig.GetAttribute("AnisotropicFiltering");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.AnisotropicFiltering = (bool)SimpleTypes.ParseValue(typeof(bool), v);
            }

            v = DefaultSettingsConfig.GetAttribute("SoundSystemDLL");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.SoundSystemDLL = v;
            }

            v = DefaultSettingsConfig.GetAttribute("SoundMaxReal2DChannels");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.SoundMaxReal2DChannels = int.Parse(v);
            }

            v = DefaultSettingsConfig.GetAttribute("SoundMaxReal3DChannels");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.SoundMaxReal3DChannels = int.Parse(v);
            }

            v = DefaultSettingsConfig.GetAttribute("ScriptingCompileProjectSolutionAtStartup");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.ScriptingCompileProjectSolutionAtStartup = bool.Parse(v);
            }

            v = DefaultSettingsConfig.GetAttribute("AutoUnloadTexturesNotUsedForLongTimeInSecondsInEditor");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.AutoUnloadTexturesNotUsedForLongTimeInSecondsInEditor = double.Parse(v);
            }

            v = DefaultSettingsConfig.GetAttribute("AutoUnloadTexturesNotUsedForLongTimeInSecondsInSimulation");
            if (!string.IsNullOrEmpty(v))
            {
                EngineSettings.Init.AutoUnloadTexturesNotUsedForLongTimeInSecondsInSimulation = double.Parse(v);
            }
        }
Exemplo n.º 3
0
            object LoadValue(TextBlock block, Type valueType)
            {
                if (valueType.IsGenericType && valueType.Name == typeof(List <>).Name)
                {
                    //List<>
                    Type[] genericArguments = valueType.GetGenericArguments();
                    Trace.Assert(genericArguments.Length == 1);
                    Type itemType = genericArguments[0];

                    MethodInfo methodAdd = valueType.GetMethod("Add");

                    TextBlock listBlock = block.FindChild(name);

                    if (listBlock == null)
                    {
                        return(null);
                    }

                    //create list
                    object value;
                    {
                        value = valueType.InvokeMember("", BindingFlags.Public |
                                                       BindingFlags.NonPublic | BindingFlags.CreateInstance |
                                                       BindingFlags.Instance, null, null, null);
                        //ConstructorInfo constructor = valueType.GetConstructor( new Type[ 0 ] );
                        //value = constructor.Invoke( null );
                    }

                    if (itemType == typeof(string))
                    {
                        for (int n = 0; ; n++)
                        {
                            string attributeName = string.Format("item{0}", n);

                            if (!listBlock.AttributeExists(attributeName))
                            {
                                break;
                            }

                            string itemValue = listBlock.GetAttribute(attributeName);
                            methodAdd.Invoke(value, new object[] { itemValue });
                        }
                    }
                    else
                    {
                        List <FieldInfo> itemFields = new List <FieldInfo>();
                        {
                            foreach (FieldInfo field in itemType.GetFields(BindingFlags.Instance |
                                                                           BindingFlags.NonPublic | BindingFlags.Public))
                            {
                                if (field.GetCustomAttributes(typeof(EngineConfigAttribute), true).Length != 0)
                                {
                                    itemFields.Add(field);
                                }
                            }
                        }
                        List <PropertyInfo> itemProperties = new List <PropertyInfo>();
                        {
                            foreach (PropertyInfo property in itemType.GetProperties(BindingFlags.Instance |
                                                                                     BindingFlags.NonPublic | BindingFlags.Public))
                            {
                                if (property.GetCustomAttributes(typeof(EngineConfigAttribute), true).Length != 0)
                                {
                                    itemProperties.Add(property);
                                }
                            }
                        }

                        foreach (TextBlock itemBlock in listBlock.Children)
                        {
                            object itemValue = itemType.InvokeMember("", BindingFlags.Public |
                                                                     BindingFlags.NonPublic | BindingFlags.CreateInstance |
                                                                     BindingFlags.Instance, null, null, null);
                            //ConstructorInfo constructor = itemType.GetConstructor( new Type[ 0 ] );
                            //object itemValue = constructor.Invoke( null );

                            //item fields
                            foreach (FieldInfo itemField in itemFields)
                            {
                                string itemName = ((EngineConfigAttribute)itemField.GetCustomAttributes(typeof(EngineConfigAttribute), true)[0]).GetName(itemField);

                                if (!itemBlock.AttributeExists(itemName))
                                {
                                    continue;
                                }

                                string strValue = itemBlock.GetAttribute(itemName);

                                try
                                {
                                    object v = SimpleTypes.ParseValue(
                                        itemField.FieldType, strValue);

                                    if (v == null)
                                    {
                                        Log.Error("Config: Not implemented parameter type \"{0}\"", itemField.FieldType.ToString());
                                    }
                                    if (v != null)
                                    {
                                        itemField.SetValue(itemValue, v);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Log.Warning("Config: {0} ({1})", itemName, e.Message);
                                }
                            }

                            //property fields
                            foreach (PropertyInfo itemProperty in itemProperties)
                            {
                                string itemName = ((EngineConfigAttribute)itemProperty.GetCustomAttributes(typeof(EngineConfigAttribute), true)[0]).GetName(itemProperty);

                                if (!itemBlock.AttributeExists(itemName))
                                {
                                    continue;
                                }

                                string strValue = itemBlock.GetAttribute(itemName);

                                try
                                {
                                    object v = SimpleTypes.ParseValue(
                                        itemProperty.PropertyType, strValue);

                                    if (v == null)
                                    {
                                        Log.Error("Config: Not implemented parameter type \"{0}\"", itemProperty.PropertyType.ToString());
                                    }
                                    if (v != null)
                                    {
                                        itemProperty.SetValue(itemValue, v, null);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Log.Warning("Config: {0} ({1})", itemName, e.Message);
                                }
                            }

                            methodAdd.Invoke(value, new object[] { itemValue });
                        }
                    }
                    return(value);
                }

                // default load
                if (block.AttributeExists(name))
                {
                    string strValue = block.GetAttribute(name);

                    try
                    {
                        object value = SimpleTypes.ParseValue(valueType, strValue);

                        if (value == null)
                        {
                            Log.Error("Config: Not implemented parameter type \"{0}\"", valueType.ToString());
                        }

                        return(value);
                    }
                    catch (Exception e)
                    {
                        Log.Warning("Config: {0} ({1})", name, e.Message);
                        return(null);
                    }
                }
                return(null);
            }
Exemplo n.º 4
0
		public static bool ParseFile( string fileName, out EDictionary<string, string> resultData, out Encoding encoding, out bool wide )
		{
			resultData = null;
			encoding = null;
			wide = false;

			var fileBase = Path.Combine( VirtualFileSystem.Directories.EngineInternal, "Localization", fileName );
			var pathInfo = fileBase + ".info";
			var pathTxt = fileBase + ".txt";

			if( File.Exists( pathTxt ) )
			{
				try
				{
					string encodingName = null;
					int? encodingCodepage = null;

					if( File.Exists( pathInfo ) )
					{
						var block = TextBlockUtility.LoadFromRealFile( pathInfo );
						if( block != null )
						{
							//Encoding
							{
								var value = block.GetAttribute( "Encoding" );
								if( int.TryParse( value, out var codepage ) )
									encodingCodepage = codepage;
								else
									encodingName = value;
							}

							//WideLanguage
							{
								var value = block.GetAttribute( "WideLanguage" );
								if( !string.IsNullOrEmpty( value ) )
									wide = (bool)SimpleTypes.ParseValue( typeof( bool ), value );
							}
						}
					}

#if !DEPLOY
					if( encodingCodepage.HasValue )
						encoding = CodePagesEncodingProvider.Instance.GetEncoding( encodingCodepage.Value );
					else if( !string.IsNullOrEmpty( encodingName ) )
						encoding = CodePagesEncodingProvider.Instance.GetEncoding( encodingName );
#endif
					//if( encodingCodepage.HasValue )
					//	encoding = Encoding.GetEncoding( encodingCodepage.Value );
					//else if( !string.IsNullOrEmpty( encodingName ) )
					//	encoding = Encoding.GetEncoding( encodingName );

					string[] lines = null;
					if( encoding != null )
						lines = File.ReadAllLines( pathTxt, encoding );
					else
						lines = File.ReadAllLines( pathTxt );

					resultData = new EDictionary<string, string>();

					foreach( var line in lines )
					{
						if( !string.IsNullOrEmpty( line ) )
						{
							var strs = line.Split( new char[] { '|' } );
							if( strs.Length != 3 )
								throw new Exception( string.Format( "Invalid format for line \'{0}\'.", line ) );
							resultData[ strs[ 0 ] + "|" + strs[ 1 ] ] = strs[ 2 ];
						}
					}

					return true;
				}
				catch( Exception e )
				{
					Log.Warning( e.Message );
				}
			}

			return false;
		}