ini (ini-parser)配置文件解析 for donet

介绍

此ini解析库适用于mono(unity3d),donet,大小在30kb左右。

开源免费:https://github.com/rickyah/ini-parser

 

使用示例

engine_config.ini  配置文件内容如下

[Engine]
: product real path
ProductRelPath    = ../Product    
AssetBundleBuildRelPath    = ../Product/Bundles    
StreamingBundlesFolderName = Bundles    
AssetBundleExt = .bytes    
IsLoadAssetBundle = 1

使用方法如下

using System;
using IniParser.Model;
using IniParser.Model.Formatting;
using IniParser.Parser;

public class EngineConfigs
{
    private readonly IniData _iniData;

    public EngineConfigs(string iniconfig)
    {
        var parser = new IniDataParser();
        _iniData = parser.Parse(iniconfig);
    }

    /// <summary>
    /// GetConfig from section
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="throwError">whether or not throw error when get no config</param>
    /// <returns></returns>
    public string GetConfig(string section, string key, bool throwError = true)
    {
        var sectionData = _iniData[section];
        if (sectionData == null)
        {
            if (throwError)
                throw new Exception("Not found section from ini config: " + section);
            return null;
        }
        var value = sectionData[key];
        if (value == null)
        {
            if (throwError)
                throw new Exception(string.Format("Not found section:`{0}`, key:`{1}` config", section, key));
        }
        return value;
    }
}


public class IniParseDemo
{
    public void Main()
    {
        EngineConfigs engineConfigs = new EngineConfigs("xxx");
        //此处返回 ../Product    
        var productRelPath = engineConfigs.GetConfig("Engine", "ProductRelPath");
    }
}

注意事项

IniDataParser.Parse(iniconfig);

iniconfig是具体的内容,而不是某个ini文件

 

其它

示例:https://github.com/zhaoqingqing/blog_samplecode/tree/master/unity_helper/iniparser

或者参照 KEngine中的AppEngine使用:https://github.com/mr-kelly/KEngine/blob/3351991a9eb7593a53a6070eddff5dd912fc6d7a/KEngine.UnityProject/Assets/KEngine/AppEngine.cs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值