Unity(c#)讀取ini配置
來源:
未知 |
責(zé)任編輯:六月芳菲 |
發(fā)布時(shí)間: 2018-03-02 09:43 | 瀏覽量:
更多精彩unity教程:http://m.trusteddivorcelawyers.com/resource/
話不多說,直接上源碼,整個(gè)庫(kù)只有這一個(gè)文件
代碼基于.net2.0,不依賴任何庫(kù),所有c# 版本均可使用,包括 unity
支持注釋,// 和 /* */ 兩種均可使用
可以根據(jù)當(dāng)前的數(shù)據(jù)獲得ini的字符串
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
/// <summary> 單個(gè)模塊數(shù)據(jù) </summary>
public class ScorpioIniSection {
/// <summary> 所有數(shù)據(jù) </summary>
public Dictionary<string, ScorpioIniValue> datas = new Dictionary<string, ScorpioIniValue>();
/// <summary> 設(shè)置值 </summary>
public void Set(string key, string value, string comment) {
if (!datas.ContainsKey(key))
datas.Add(key, new ScorpioIniValue());
datas[key].Set(value, comment);
}
/// <summary> 獲得值 </summary>
public ScorpioIniValue Get(string key) {
if (datas.ContainsKey(key))
return datas[key];
return null;
}
/// <summary> 刪除值 </summary>
public void Remove(string key) {
if (datas.ContainsKey(key)) {
datas.Remove(key);
}
}
}
/// <summary> 具體數(shù)據(jù) </summary>
public class ScorpioIniValue {
/// <summary> 值 </summary>
public string value;
/// <summary> 注釋 </summary>
public string comment;
/// <summary> 設(shè)置值 </summary>
public void Set(string value, string comment) {
if (value != null)
this.value = value;
if (comment != null)
this.comment = comment;
}
}
/// <summary> 讀取ini文件 </summary>
public class ScorpioIni {
/// <summary> 所有數(shù)據(jù) </summary>
public Dictionary<string, ScorpioIniSection> m_ConfigData = new Dictionary<string, ScorpioIniSection>();
/// <summary> 構(gòu)造函數(shù) </summary>
public ScorpioIni() { }
/// <summary> 構(gòu)造函數(shù) </summary>
public ScorpioIni(byte[] bytes, Encoding encoding) {
InitFormBuffer(bytes, encoding);
}
public ScorpioIni(string file, Encoding encoding) {
InitFormFile(file, encoding);
}
/// <summary> 構(gòu)造函數(shù) </summary>
public ScorpioIni(string data) {
InitFormString(data);
}
/// <summary> 根據(jù)BYTE[]初始化數(shù)據(jù) </summary>
public void InitFormBuffer(byte[] buffer, Encoding encoding) {
InitFormString(encoding.GetString(buffer, 0, buffer.Length));
}
public void InitFormFile(string file, Encoding encoding) {
using (FileStream fs = new FileStream(file, FileMode.Open)) {
long length = fs.Length;
byte[] buffer = new byte[length];
fs.Read(buffer, 0, (int)length);
InitFormString(encoding.GetString(buffer));
}
}
/// <summary> 根據(jù)string初始化數(shù)據(jù) </summary>
public void InitFormString(string buffer) {
try {
m_ConfigData.Clear();
string[] datas = buffer.Split('\n');
string section = "";
bool startComment = false;
StringBuilder comment = new StringBuilder();
int count = datas.Length;
for (int i = 0; i < count; ++i) {
string data = datas[i].Trim();
if (!string.IsNullOrEmpty(data)) {
// /* */為區(qū)域注釋
if (data.StartsWith("/*")) {
comment.Append(data.Replace(@"/*", "").Replace(@"*/", ""));
if (data.EndsWith("*/")) {
startComment = false;
continue;
}
startComment = true;
continue;
}
// */ 為區(qū)域注釋結(jié)尾
else if (data.EndsWith("*/")) {
comment.Append(data.Replace(@"/*", "").Replace(@"*/", ""));
startComment = false;
continue;
}
// //為行注釋
else if (data.StartsWith("//")) {
comment.Append(data.Replace(@"//", ""));
continue;
}
if (startComment == true) {
continue;
}
if (data.StartsWith("[")) {
int indexLeft = data.IndexOf("[");
int indexRight = data.IndexOf("]");
if (indexLeft >= 0 && indexRight >= 0) {
section = data.Substring(indexLeft + 1, indexRight - indexLeft - 1);
}
} else {
int index = data.IndexOf("=");
if (index >= 0) {
string key = data.Substring(0, index).Trim();
string value = data.Substring(index + 1).Trim();
Set(section, key, value, comment.ToString());
comment = new StringBuilder();
} else {
throw new Exception((i + 1) + " 行填寫錯(cuò)誤, 正確格式為 key=value 不支持回車");
}
}
}
}
} catch (System.Exception e) {
throw new Exception("initialize is error : " + e.ToString());
}
}
/// <summary> 返回所有數(shù)據(jù) </summary>
public Dictionary<string, ScorpioIniSection> GetData() {
return m_ConfigData;
}
/// <summary> 返回單個(gè)模塊的數(shù)據(jù) </summary>
public ScorpioIniSection GetSection() {
return GetSection("");
}
/// <summary> 返回單個(gè)模塊的數(shù)據(jù) </summary>
public ScorpioIniSection GetSection(string section) {
if (!m_ConfigData.ContainsKey(section))
return null;
return m_ConfigData[section];
}
/// <summary> 獲得Value </summary>
public string Get(string key) {
return Get("", key);
}
/// <summary> 設(shè)置Value </summary>
public string Get(string section, string key) {
if (m_ConfigData.Count <= 0)
return "";
if (section == null) section = "";
if (!m_ConfigData.ContainsKey(section))
return "";
var configValue = m_ConfigData[section].Get(key);
return configValue != null ? configValue.value : "";
}
public ScorpioIniValue GetValue(string key) {
return GetValue("", key);
}
/// <summary> 設(shè)置Value </summary>
public ScorpioIniValue GetValue(string section, string key) {
if (m_ConfigData.Count <= 0)
return null;
if (section == null) section = "";
if (!m_ConfigData.ContainsKey(section))
return null;
return m_ConfigData[section].Get(key);
}
/// <summary> 設(shè)置Value </summary>
public void Set(string key, string value) {
Set("", key, value);
}
/// <summary> 設(shè)置Value </summary>
public void Set(string section, string key, string value) {
Set(section, key, value, null);
}
/// <summary> 設(shè)置Value </summary>
public void Set(string section, string key, string value, string comment) {
if (!m_ConfigData.ContainsKey(section))
m_ConfigData.Add(section, new ScorpioIniSection());
m_ConfigData[section].Set(key, value, comment);
}
/// <summary> 刪除Key </summary>
public void Remove(string key) {
Remove("", key);
}
/// <summary> 刪除Key </summary>
public void Remove(string section, string key) {
if (m_ConfigData.ContainsKey(section))
m_ConfigData[section].Remove(key);
}
/// <summary> 清空一個(gè)模塊 </summary>
public void ClearSection() {
ClearSection("");
}
/// <summary> 清空一個(gè)模塊 </summary>
public void ClearSection(string section) {
if (m_ConfigData.ContainsKey(section))
m_ConfigData.Remove(section);
}
/// <summary> 清空所有數(shù)據(jù) </summary>
public void ClearData() {
m_ConfigData.Clear();
}
/// <summary> 返回?cái)?shù)據(jù)字符串 </summary>
public string GetString() {
SortedDictionary<string, ScorpioIniSection> data = new SortedDictionary<string, ScorpioIniSection>(m_ConfigData);
StringBuilder builder = new StringBuilder();
if (data.ContainsKey("")) {
foreach (var val in data[""].datas) {
var configValue = val.Value;
if (!string.IsNullOrEmpty(configValue.comment))
builder.AppendLine(string.Format("/*{0}*/", configValue.comment));
builder.AppendLine(string.Format("{0}={1}", val.Key, configValue.value));
}
}
foreach (var pair in data) {
if (string.IsNullOrEmpty(pair.Key))
continue;
builder.AppendLine(string.Format("[{0}]", pair.Key));
foreach (var val in pair.Value.datas) {
var configValue = val.Value;
if (!string.IsNullOrEmpty(configValue.comment))
builder.AppendLine(string.Format("/*{0}*/", configValue.comment));
builder.AppendLine(string.Format("{0}={1}", val.Key, configValue.value));
}
}
return builder.ToString();
}
}
下面是示例ini
// // 和 /* */ 可以當(dāng)作注釋
/*這種是默認(rèn)命名空間*/
key1 = value1
[sec1]
//這種是sec1里的值
key1 = value1sec1
更多精彩unity教程:http://m.trusteddivorcelawyers.com/resource/
話不多說,直接上源碼,整個(gè)庫(kù)只有這一個(gè)文件
代碼基于.net2.0,不依賴任何庫(kù),所有c# 版本均可使用,包括 unity
支持注釋,// 和 /* */ 兩種均可使用
可以根據(jù)當(dāng)前的數(shù)據(jù)獲得ini的字符串
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
/// <summary> 單個(gè)模塊數(shù)據(jù) </summary>
public class ScorpioIniSection {
/// <summary> 所有數(shù)據(jù) </summary>
public Dictionary<string, ScorpioIniValue> datas = new Dictionary<string, ScorpioIniValue>();
/// <summary> 設(shè)置值 </summary>
public void Set(string key, string value, string comment) {
if (!datas.ContainsKey(key))
datas.Add(key, new ScorpioIniValue());
datas[key].Set(value, comment);
}
/// <summary> 獲得值 </summary>
public ScorpioIniValue Get(string key) {
if (datas.ContainsKey(key))
return datas[key];
return null;
}
/// <summary> 刪除值 </summary>
public void Remove(string key) {
if (datas.ContainsKey(key)) {
datas.Remove(key);
}
}
}
/// <summary> 具體數(shù)據(jù) </summary>
public class ScorpioIniValue {
/// <summary> 值 </summary>
public string value;
/// <summary> 注釋 </summary>
public string comment;
/// <summary> 設(shè)置值 </summary>
public void Set(string value, string comment) {
if (value != null)
this.value = value;
if (comment != null)
this.comment = comment;
}
}
/// <summary> 讀取ini文件 </summary>
public class ScorpioIni {
/// <summary> 所有數(shù)據(jù) </summary>
public Dictionary<string, ScorpioIniSection> m_ConfigData = new Dictionary<string, ScorpioIniSection>();
/// <summary> 構(gòu)造函數(shù) </summary>
public ScorpioIni() { }
/// <summary> 構(gòu)造函數(shù) </summary>
public ScorpioIni(byte[] bytes, Encoding encoding) {
InitFormBuffer(bytes, encoding);
}
public ScorpioIni(string file, Encoding encoding) {
InitFormFile(file, encoding);
}
/// <summary> 構(gòu)造函數(shù) </summary>
public ScorpioIni(string data) {
InitFormString(data);
}
/// <summary> 根據(jù)BYTE[]初始化數(shù)據(jù) </summary>
public void InitFormBuffer(byte[] buffer, Encoding encoding) {
InitFormString(encoding.GetString(buffer, 0, buffer.Length));
}
public void InitFormFile(string file, Encoding encoding) {
using (FileStream fs = new FileStream(file, FileMode.Open)) {
long length = fs.Length;
byte[] buffer = new byte[length];
fs.Read(buffer, 0, (int)length);
InitFormString(encoding.GetString(buffer));
}
}
/// <summary> 根據(jù)string初始化數(shù)據(jù) </summary>
public void InitFormString(string buffer) {
try {
m_ConfigData.Clear();
string[] datas = buffer.Split('\n');
string section = "";
bool startComment = false;
StringBuilder comment = new StringBuilder();
int count = datas.Length;
for (int i = 0; i < count; ++i) {
string data = datas[i].Trim();
if (!string.IsNullOrEmpty(data)) {
// /* */為區(qū)域注釋
if (data.StartsWith("/*")) {
comment.Append(data.Replace(@"/*", "").Replace(@"*/", ""));
if (data.EndsWith("*/")) {
startComment = false;
continue;
}
startComment = true;
continue;
}
// */ 為區(qū)域注釋結(jié)尾
else if (data.EndsWith("*/")) {
comment.Append(data.Replace(@"/*", "").Replace(@"*/", ""));
startComment = false;
continue;
}
// //為行注釋
else if (data.StartsWith("//")) {
comment.Append(data.Replace(@"//", ""));
continue;
}
if (startComment == true) {
continue;
}
if (data.StartsWith("[")) {
int indexLeft = data.IndexOf("[");
int indexRight = data.IndexOf("]");
if (indexLeft >= 0 && indexRight >= 0) {
section = data.Substring(indexLeft + 1, indexRight - indexLeft - 1);
}
} else {
int index = data.IndexOf("=");
if (index >= 0) {
string key = data.Substring(0, index).Trim();
string value = data.Substring(index + 1).Trim();
Set(section, key, value, comment.ToString());
comment = new StringBuilder();
} else {
throw new Exception((i + 1) + " 行填寫錯(cuò)誤, 正確格式為 key=value 不支持回車");
}
}
}
}
} catch (System.Exception e) {
throw new Exception("initialize is error : " + e.ToString());
}
}
/// <summary> 返回所有數(shù)據(jù) </summary>
public Dictionary<string, ScorpioIniSection> GetData() {
return m_ConfigData;
}
/// <summary> 返回單個(gè)模塊的數(shù)據(jù) </summary>
public ScorpioIniSection GetSection() {
return GetSection("");
}
/// <summary> 返回單個(gè)模塊的數(shù)據(jù) </summary>
public ScorpioIniSection GetSection(string section) {
if (!m_ConfigData.ContainsKey(section))
return null;
return m_ConfigData[section];
}
/// <summary> 獲得Value </summary>
public string Get(string key) {
return Get("", key);
}
/// <summary> 設(shè)置Value </summary>
public string Get(string section, string key) {
if (m_ConfigData.Count <= 0)
return "";
if (section == null) section = "";
if (!m_ConfigData.ContainsKey(section))
return "";
var configValue = m_ConfigData[section].Get(key);
return configValue != null ? configValue.value : "";
}
public ScorpioIniValue GetValue(string key) {
return GetValue("", key);
}
/// <summary> 設(shè)置Value </summary>
public ScorpioIniValue GetValue(string section, string key) {
if (m_ConfigData.Count <= 0)
return null;
if (section == null) section = "";
if (!m_ConfigData.ContainsKey(section))
return null;
return m_ConfigData[section].Get(key);
}
/// <summary> 設(shè)置Value </summary>
public void Set(string key, string value) {
Set("", key, value);
}
/// <summary> 設(shè)置Value </summary>
public void Set(string section, string key, string value) {
Set(section, key, value, null);
}
/// <summary> 設(shè)置Value </summary>
public void Set(string section, string key, string value, string comment) {
if (!m_ConfigData.ContainsKey(section))
m_ConfigData.Add(section, new ScorpioIniSection());
m_ConfigData[section].Set(key, value, comment);
}
/// <summary> 刪除Key </summary>
public void Remove(string key) {
Remove("", key);
}
/// <summary> 刪除Key </summary>
public void Remove(string section, string key) {
if (m_ConfigData.ContainsKey(section))
m_ConfigData[section].Remove(key);
}
/// <summary> 清空一個(gè)模塊 </summary>
public void ClearSection() {
ClearSection("");
}
/// <summary> 清空一個(gè)模塊 </summary>
public void ClearSection(string section) {
if (m_ConfigData.ContainsKey(section))
m_ConfigData.Remove(section);
}
/// <summary> 清空所有數(shù)據(jù) </summary>
public void ClearData() {
m_ConfigData.Clear();
}
/// <summary> 返回?cái)?shù)據(jù)字符串 </summary>
public string GetString() {
SortedDictionary<string, ScorpioIniSection> data = new SortedDictionary<string, ScorpioIniSection>(m_ConfigData);
StringBuilder builder = new StringBuilder();
if (data.ContainsKey("")) {
foreach (var val in data[""].datas) {
var configValue = val.Value;
if (!string.IsNullOrEmpty(configValue.comment))
builder.AppendLine(string.Format("/*{0}*/", configValue.comment));
builder.AppendLine(string.Format("{0}={1}", val.Key, configValue.value));
}
}
foreach (var pair in data) {
if (string.IsNullOrEmpty(pair.Key))
continue;
builder.AppendLine(string.Format("[{0}]", pair.Key));
foreach (var val in pair.Value.datas) {
var configValue = val.Value;
if (!string.IsNullOrEmpty(configValue.comment))
builder.AppendLine(string.Format("/*{0}*/", configValue.comment));
builder.AppendLine(string.Format("{0}={1}", val.Key, configValue.value));
}
}
return builder.ToString();
}
}
下面是示例ini
// // 和 /* */ 可以當(dāng)作注釋
/*這種是默認(rèn)命名空間*/
key1 = value1
[sec1]
//這種是sec1里的值
key1 = value1sec1
更多精彩unity教程:http://m.trusteddivorcelawyers.com/resource/
-
分享到:
相關(guān)文章
網(wǎng)友評(píng)論
您需要登錄后才可以發(fā)帖 登錄 | 立即注冊(cè)
關(guān)閉
- 用戶名:
- 密 碼:
- 驗(yàn)證碼: 看不清? 點(diǎn)擊更換
- 忘記密碼?
全部評(píng)論:0條
推薦
熱門