Skip to content

Instantly share code, notes, and snippets.

@HolyFot
Created July 15, 2020 05:32
Show Gist options
  • Save HolyFot/4a41f07001ccc13843a5e6cfc5b1c6d5 to your computer and use it in GitHub Desktop.
Save HolyFot/4a41f07001ccc13843a5e6cfc5b1c6d5 to your computer and use it in GitHub Desktop.
Simple INI Writer/Reader C# Unity
// Made by: HolyFot
// Very Simple INI Parser/Writer.
// Ignores Comments and is Case Sensitive.
using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
public class SimpleIni
{
public string CommentChar = "#";
public List<string> categories;
public List<VarData> variablez;
public void Initialize()
{
//Initialize
variablez = new List<VarData>();
categories = new List<string>();
}
public void ClearData()
{
variablez = new List<VarData>();
categories = new List<string>();
}
public string GetStringValue(string category, string variable)
{
for (var i = 0; i < variablez.Count; i++)
{
if (variablez[i].category == category && variablez[i].variable == variable)
{
return variablez[i].value;
}
}
return "";
}
public void AddSetStringValue(string category1, string variable1, string value1)
{
//If Exists: Set Value
bool found = false;
for (var i = 0; i < variablez.Count; i++)
{
if (variablez[i].category == category1 && variablez[i].variable == variable1)
{
variablez[i].value = value1;
found = true;
}
}
//If Doesnt Exist: Add
if (!found)
{
VarData temp = new VarData();
temp.category = category1;
temp.variable = variable1;
temp.value = value1;
variablez.Add(temp);
}
}
public void AddCategory(string category)
{
//Add if it doesn't exist
if (categories != null)
if (!categories.Contains(category))
categories.Add(category);
}
public void LoadIniFile(string file, bool clearData)
{
try
{
//Clear Data before Parsing if Requested
if (clearData)
ClearData();
//Parse Lines Into List
var RawIniData = File.ReadAllLines(file, Encoding.UTF8);
List<string> parseData = new List<string>(RawIniData);
//Fill In Categories/Variables/Values
string currentCategory = "";
for (var i = 0; i < parseData.Count; i++)
{
string cleanData = CleanStr(parseData[i]);
if (cleanData.StartsWith("[")) //Category
{
string resultCat = "";
int index1 = cleanData.IndexOf(']'); //Ignores Comment if after
resultCat = cleanData.Substring(1, index1 - 1);
AddCategory(resultCat);
currentCategory = resultCat; //Set to Current Category
}
else
{
if (cleanData.Contains("=") && !cleanData.StartsWith(CommentChar)) //Variable/Value
{
int index1 = cleanData.IndexOf('=');
if (index1 < 0)
continue;
//Ignore Comment inside
if (cleanData.Contains(CommentChar))
{
int commentIndex = cleanData.IndexOf(CommentChar);
if (commentIndex > index1)
{
string variableName = cleanData.Substring(0, index1);
string value = cleanData.Substring(index1 + 1, commentIndex - index1 - 1);
AddSetStringValue(currentCategory, variableName, value);
}
else
Debug.Log("[SimpleIni] Couldnt Parse Line due to bad comment on line: " + i + ". Text: " + cleanData);
}
else //No Comment
{
string variableName = cleanData.Substring(0, index1);
string value = cleanData.Substring(index1 + 1, cleanData.Length - index1 - 1);
AddSetStringValue(currentCategory, variableName, value);
}
}
}
}
}
catch (Exception ex)
{
Debug.Log("[SimpleIni] Read INI File Error: " + ex.Message);
}
}
public string CleanStr(string input)
{
return input.TrimEnd().Replace("\r", "").Replace("\n", "");
}
public void WriteIniFile(string file)
{
try
{
using (StreamWriter sw1 = new StreamWriter(file))
{
for (var i = 0; i < categories.Count; i++)
{
sw1.WriteLine("["+categories[i]+"]"); //Write Category Header
for (var x = 0; x < variablez.Count; x++)
{
if (variablez[x].category == categories[i])
{
sw1.WriteLine(variablez[x].variable + "=" + variablez[x].value); //Write Variable & Value
}
}
sw1.WriteLine(""); //Write Line Gap
}
}
}
catch (Exception ex)
{
Debug.Log("[SimpleIni] Write INI File Error: " + ex.Message);
}
}
}
public class VarData
{
public string variable = "";
public string category = "";
public string value = "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment