Last active
April 1, 2020 21:57
-
-
Save jasonknight/1d2df9e270117c678ce56b9ecd12c319 to your computer and use it in GitHub Desktop.
C# Ini File Loader for Unity3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This obviously expects that you will be fastidious with your ini files as it has no error or optimization handling. It's dead simple.