【问题标题】:XNA - How to load the entire content of a .ini fileXNA - 如何加载 .ini 文件的全部内容
【发布时间】:2012-11-27 08:42:25
【问题描述】:

我设法从 .ini 文件中写入和读取特定参数。
我想知道是否有办法加载 .ini 文件的全部内容并将其存储在一个特殊的类中。这样,我只需加载 .ini 文件一次。这样可以减少游戏的加载量。

我知道在小型游戏中,这可能无关紧要,但如果有人能指出我正确的方向,我仍然会很感激。

【问题讨论】:

    标签: c# xna ini


    【解决方案1】:

    我相信 C# 的创建者倾向于将人们推向基于 XML 的配置文件而不是 INI 文件的方向 - 所以没有内置任何东西。我在 CodeProject 上找到了这篇文章,它把东西包装在一个很好的类中。这会有帮助吗?

    http://www.codeproject.com/Articles/1966/An-INI-file-handling-class-using-C

    这不是我写的 - 也不是我的功劳,但它可能就是你要找的东西 :)

    【讨论】:

    • 是的,我已经在使用它了。 :) 但是你只能加载特定的键而不是整个文件。当我的游戏增加大小时,我想读出整个内容并存储在一个类中以节省加载时间。
    • 对,那么安迪在下面发布的答案是一种更好的做事方式。请问您为什么选择ini文件而不是XML? C# 已经支持将对象序列化/反序列化为 XML - 类似以下内容:switchonthecode.com/tutorials/csharp-tutorial-xml-serialization
    • 好吧,我在游戏开发方面不是那么好,所以我想为不同类型的数据使用不同类型的文件来习惯使用不同的文件类型。我将使用 .ini 文件进行设置,使用 .scv 文件进行本地化,使用 .xml 文件进行大量文本。我知道这可能不是最好的做事方式,但我认为这是学习或了解不同文件类型如何工作的好方法。
    • 啊,我明白了,我认为这实际上是个好主意,没有什么比学习新东西更好的了! :)
    【解决方案2】:

    假设 INI 是一个用换行符拆分的简单键/值对,您是否可以使用类似的方法将整个 INI 文件作为字典或强类型对象提供。

    该方法允许您将 ini 文件加载到这样的对象中。

    class IniStructure
    {
        public short Field1;
        public int Property1 { get; set; }
        public string Property2 { get; set; }
    }
    
    IniStructure ini = IniLoader.Load<IniStructure>(<fileName>);
    

    或者干脆用非 T 方法进入字典。

    public static class IniLoader
        {
            public static T Load<T>(string fileName)
            {
                T results = (T)Activator.CreateInstance(typeof(T));
    
                PropertyInfo[] tProperties = typeof(T).GetProperties();
                FieldInfo[] tFields = typeof(T).GetFields();
    
                var iniFile = Load(fileName);
    
                foreach (var property in tProperties)
                    if (iniFile.ContainsKey(property.Name))
                    {
                        object s = System.Convert.ChangeType(iniFile[property.Name].ToString(), property.PropertyType);
                        property.SetValue(results, s, null);
                    }
                foreach (var field in tFields)
                    if (iniFile.ContainsKey(field.Name))
                    {
                        object s = System.Convert.ChangeType(iniFile[field.Name].ToString(), field.FieldType);
                        field.SetValue(results, s);
                    }
    
                return results;
            }
    
            public static Dictionary<string, object> Load(string fileName)
            {
                Dictionary<string, object> results = new Dictionary<string, object>();
    
                string fileText = File.ReadAllText(fileName);
                string[] fileLines = fileText.Split('\r');
                if (fileLines.Length > 0)
                    for (int i = 0; i < fileLines.Length; i++)
                    {
                        string line = fileLines[i].Trim();
                        if (!string.IsNullOrEmpty(line))
                        {
                            int equalsLocation = line.IndexOf('=');
                            if (equalsLocation > 0)
                            {
                                string key = line.Substring(0, equalsLocation).Trim();
                                string value = line.Substring(equalsLocation + 1, line.Length - equalsLocation - 1);
    
                                results.Add(key, value);
                            }
                        }
                    }
    
                return results;
            }
        }
    

    【讨论】:

    • 谢谢,通过一些编辑,我让它按我想要的方式工作。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多