Register for your free account! | Forgot your password?

You last visited: Today at 08:35

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Release] IniFile Reader!

Discussion on [Release] IniFile Reader! within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

Reply
 
Old   #1
 
Mr_PoP's Avatar
 
elite*gold: 0
Join Date: Apr 2008
Posts: 759
Received Thanks: 285
[Release] IniFile Reader!

Greetings Guys,

I was bored so I made a dll to read your Ini files.
all you need is to add the dll as a reference to your project and start using it.

an example:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IniFile;

namespace Test {

    internal class Program {

        private static void Main(string[] args) {
            var ini = new CIniFile("test.ini", "AccountServer");
            int port = ini.GetInt("ACCOUNT_SERVER_PORT");
            ini.SetSection("Game_DataBase");
            string test = ini.GetString("DATABASE_HOST");
            Console.WriteLine("values are {0} , {1}", port, test);
            Console.ReadKey();
        }
    }
}
test.ini

Code:
[AccountServer]
ACCOUNT_SERVER_IP=5.230.189.19
ACCOUNT_SERVER_PORT=9958

[Account_DataBase]
DATABASE_HOST=127.0.0.1
DATABASE_USER=root
DATABASE_PASSWORD=
DATABASE_DBNAME=account_zf
DATABASE_PORT=3306

[GameServer]
GAME_SERVER_NAME=Unkown
GAME_SERVER_IP=5.230.189.19
GAME_SERVER_PORT=5816

[Game_DataBase]
DATABASE_HOST=127.0.0.1 // this is the db ip
DATABASE_USER=root
DATABASE_PASSWORD=
DATABASE_DBNAME=account_zf
DATABASE_PORT=3306
output:
Code:
values are 9958 , 127.0.0.1
Features:
-Managed (no native calls);
-You can comment your fields.
-Works on anyCPU maybe any platform :P.
-Reads Multiple sections.

okey test it , and if there is any issue please let me know so I can fix it.
Attached Files
File Type: zip IniFile.zip (2.9 KB, 26 views)
Mr_PoP is offline  
Thanks
2 Users
Old 02/25/2012, 11:12   #2
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,282
Received Thanks: 4,190
Code:
public class CIniFile
{
    // Fields
    private Dictionary<string, string> fields = new Dictionary<string, string>();
    private string[] lines;
    private string section;
    private List<string> temp = new List<string>();
    private string Wsection;

    // Methods
    public CIniFile(string filepath, string section)
    {
        try
        {
            this.lines = File.ReadAllLines(filepath);
            this.section = "[" + section + "]";
            this.GetFields();
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }

    private void GetFields()
    {
        try
        {
            bool flag = false;
            for (int i = 0; i < this.lines.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(this.lines[i]))
                {
                    continue;
                }
                char ch = this.lines[i][0];
                if (ch.Equals('[') && (this.lines[i] == this.section))
                {
                    for (int j = i + 1; j < this.lines.Length; j++)
                    {
                        if (!string.IsNullOrWhiteSpace(this.lines[j]))
                        {
                            ch = this.lines[j][0];
                            if (ch.Equals('['))
                            {
                                flag = true;
                                break;
                            }
                            this.temp.Add(this.lines[j]);
                        }
                    }
                }
                if (flag)
                {
                    break;
                }
            }
            foreach (string str in this.temp)
            {
                string[] strArray = str.Split("//".ToCharArray())[0].Replace(" ", "").Split(new char[] { '=' });
                this.fields.Add(strArray[0], strArray[1].Replace(";", ""));
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }

    public int GetInt(string key)
    {
        Func<KeyValuePair<string, string>, bool> predicate = null;
        try
        {
            if (predicate == null)
            {
                predicate = x => x.Key == key;
            }
            return int.Parse(this.fields.SingleOrDefault<KeyValuePair<string, string>>(predicate).Value);
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            return 0;
        }
    }

    public string GetString(string key)
    {
        Func<KeyValuePair<string, string>, bool> predicate = null;
        try
        {
            if (predicate == null)
            {
                predicate = x => x.Key == key;
            }
            return this.fields.SingleOrDefault<KeyValuePair<string, string>>(predicate).Value;
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            return "";
        }
    }

    public void SetSection(string section)
    {
        this.section = "[" + section + "]";
        this.fields.Clear();
        this.temp.Clear();
        this.GetFields();
    }
}
Why would you want to keep yourself away from Native calls? .-.
Spirited is offline  
Thanks
2 Users
Old 02/25/2012, 11:59   #3
 
JobvdH's Avatar
 
elite*gold: 0
Join Date: Nov 2010
Posts: 371
Received Thanks: 120
Great work Mr_PoP.
Fang`s method is better tho.
JobvdH is offline  
Old 02/25/2012, 12:04   #4
 
Mr_PoP's Avatar
 
elite*gold: 0
Join Date: Apr 2008
Posts: 759
Received Thanks: 285
Quote:
Originally Posted by Fаng View Post
Why would you want to keep yourself away from Native calls? .-.
the question is why do I need to use it?

Quote:
Originally Posted by JobvdH View Post
Great work Mr_PoP.
Fang`s method is better tho.
what method lol? if your talking about the code he posted, it's the reflection of my dll.
Mr_PoP is offline  
Old 02/25/2012, 12:23   #5
 
JobvdH's Avatar
 
elite*gold: 0
Join Date: Nov 2010
Posts: 371
Received Thanks: 120
Aah, well then great job
JobvdH is offline  
Old 02/25/2012, 16:23   #6


 
CptSky's Avatar
 
elite*gold: 0
Join Date: Jan 2008
Posts: 1,443
Received Thanks: 1,175
Quote:
Originally Posted by Fаng View Post
Why would you want to keep yourself away from Native calls? .-.
1. Native calls may be really slow...
2. Native calls aren't made for INI file, but for the Windows Registry.
3. The second point give another problem. Each time you call a function, the file is completely loaded for the call... Really not optimal.
CptSky is offline  
Thanks
3 Users
Old 02/25/2012, 16:44   #7


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Of course a better question is why the hell are you still using .ini files at all?
Korvacs is offline  
Old 02/25/2012, 16:45   #8
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by Korvacs View Post
Of course a better question is why the hell are you still using .ini files at all?
Config?
I don't have a username is offline  
Old 02/25/2012, 16:59   #9


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Quote:
Originally Posted by I don't have a username View Post
Config?
...xml? Basically the replacement for ini files?? ie. Welcome to the 21st century??
Korvacs is offline  
Old 02/25/2012, 17:31   #10
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by Korvacs View Post
...xml? Basically the replacement for ini files?? ie. Welcome to the 21st century??
Not necessary. Ini is far easier to read than reading a xml file, so if you have a lot of configurations, it would be much faster configuring an inifile than xml. However xml is faster parsing for programming and much easier to use at that part as well, but performance doesn't matter much at loading configs, unless you load configurations multiple times during your process.
I don't have a username is offline  
Old 02/25/2012, 17:40   #11


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
There's practically no difference in readability unless your a complete moron who doesn't understand simple syntax, if anything its clearer to read if your sensible about your syntax.

And as for performance its not even worth thinking about, at no point should you need to be worrying about how long it takes you to parse a .ini or .xml file, if your worrying about it then you need to rethink what your doing entirely.

Honestly do like the rest of the programmers in the world and let old technology die (surprisingly enough that's why the .net framework doesn't have any managed methods for ini files -.-")
Korvacs is offline  
Old 02/25/2012, 17:42   #12
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by Korvacs View Post
There's practically no difference in readability unless your a complete moron who doesn't understand simple syntax, if anything its clearer to read if your sensible about your syntax.

And as for performance its not even worth thinking about, at no point should you need to be worrying about how long it takes you to parse a .ini or .xml file, if your worrying about it then you need to rethink what your doing entirely.

Honestly do like the rest of the programmers in the world and let old technology die (surprisingly enough that's why the .net framework doesn't have any managed methods for ini files -.-")
I'm not saying I am doing it, was just saying why he could be doing it. I'm using xml at its best lmao.
I don't have a username is offline  
Old 02/25/2012, 21:00   #13
 
Mr_PoP's Avatar
 
elite*gold: 0
Join Date: Apr 2008
Posts: 759
Received Thanks: 285
Quote:
Originally Posted by Korvacs View Post
Of course a better question is why the hell are you still using .ini files at all?
ahaha am not using .ini am using xml , I was just bored so I made it really
Mr_PoP is offline  
Old 02/26/2012, 18:47   #14
 
elite*gold: 0
Join Date: Nov 2009
Posts: 342
Received Thanks: 17
Best Ya POP
|xabi| is offline  
Old 02/26/2012, 21:05   #15


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
Quote:
Originally Posted by Mr_PoP View Post
ahaha am not using .ini am using xml , I was just bored so I made it really
lol what a waste of time
Korvacs is offline  
Reply


Similar Threads Similar Threads
[Release]Metin2 Pong & Key Reader
05/01/2013 - Metin2 PServer Guides & Strategies - 611 Replies
Abend Elitepvpers, Wie zuvor erwähnt Release Ich mein Metin2 KeyManager der Pong & Key liest. http://img846.imageshack.us/img846/3704/keymanage r02.th.png Verwendung: Mit dem Pong könnt Ihr mit einem modifizierten Orginal DE Ordner auf einen Privat Server connecten, Ihr müsst jedoch selbstständig die serverinfo anpassen. Mit dem Key könnt Ihr einen Entpacker für einen Privat Server Client erstellen lassen, somit könnt ihr einfach an benötigte Dateien gelangen, selbst wenn dieser...
[RELEASE]Metin2 Soulstone Reader [ENG/GER]
09/03/2011 - Metin2 Hacks, Bots, Cheats, Exploits & Macros - 10 Replies
Hey Leute Ich hab einen Seelibot erstellt wo man afk gehen kann usw. da ist nch ein video guckt es euch an! Version 1.0: Schrott Version 2.0: 2x auf Zen-Bohnen (Auf RSMt2 verbessern die zenbohnen 5k) Version 3.0: max 10x auf Zen-Bohnen (Auf manchen Servern verbessern die Zen-Bohnen nur 1-2k) und ich Hoffe das es jez klappt! Diesen Bot hab ich erstellt damit man afk gehen kann! ihr sollt nicht vor dem Com oder Leppi sitzen und alles angucken bis die Fertigkeit P ist! Ich gehe immer AFK...
[Release] InI File Reader
06/28/2009 - CO2 PServer Guides & Releases - 2 Replies
Well This Should be All.



All times are GMT +1. The time now is 08:35.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.