Skip to content

Instantly share code, notes, and snippets.

@jasonknight
Last active April 1, 2020 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonknight/1d2df9e270117c678ce56b9ecd12c319 to your computer and use it in GitHub Desktop.
Save jasonknight/1d2df9e270117c678ce56b9ecd12c319 to your computer and use it in GitHub Desktop.
C# Ini File Loader for Unity3d
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class IniLoader {
public string data;
public int position;
public void load(string path) {
if ( ! File.Exists(path) )
throw new System.ArgumentException("path:" + path + " does not exist");
data = File.ReadAllText(path,Encoding.UTF8);
}
public string get_key(string kname) {
position = 0;
string ckey = "";
while ( position < data.Length ) {
ckey = get_next_key();
if ( ckey == "" || ckey == kname )
break;
}
if ( ckey == "" )
throw new System.ArgumentException("key: " + kname + " could not be found");
Debug.Log("Found [" + ckey + "]");
string cvalue = get_next_value();
return cvalue;
}
public string get_next_value() {
string buffer = "";
while ( position < data.Length ) {
if ( data[position] == '\\') {
position = position + 2;
continue;
}
if ( data[position] == ';' ) {
position--;
break;
}
buffer = buffer + data[position];
position++;
}
return buffer.Trim();
}
public string get_next_key() {
advance_to_next_semicolon();
string buffer = "";
bool found = false;
while ( position < data.Length ) {
if ( data[position] == '=' ) {
found = true;
position++;
break;
}
buffer = buffer + data[position];
position++;
}
if ( found == false )
return "";
return buffer.Trim();
}
public void advance_to_next_semicolon() {
if ( position == 0 )
return; // we are at the very start
while ( position < data.Length ) {
if ( data[position] == '\\') {
position = position + 2;
continue;
}
if ( data[position] == ';' ) {
position++;
return;
}
position++;
}
}
}
@jasonknight
Copy link
Author

This obviously expects that you will be fastidious with your ini files as it has no error or optimization handling. It's dead simple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment