Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make INI Files Look Appealing Again...
#1
INI Optimization
Made by pingu

Difficulty: 1 / 5 (do not do this tutorial if you cannot use the copy and paste tool one time!)


You don't need to read this if you are one of those people who just want it to work and don't want to know how or why it does (or what effect it has even). That's right, I give you permission to skip the boring parts and get right to the code.

I made this nifty little optimization for Elysium Reborn, which ironically was so bugged that it died. This feature was the only feature to be used in the later versions while everything else died with the version.

This uses the basic principle of "if it doesn't exist, it must be zero". "Null" is our way of saying something is nothing, which can be translated to 0. It's really simple how it works. If GetVar is called, it just returns what is can find. In this case, we are lucky enough that "Val(vbNullString)" returns as a "0". If PutVar is called, it first checks to see if we are trying to save a null (or 0) piece of data. If so, we check to see if that slot for the data already exists. If it does, we delete it and save a bunch of ini space. This new space can save significant amounts of room and makes INI files smaller than binary ones in most cases! It even makes it go faster too, because there is less data to sort through.

I'm not sure what would be faster, checking each time and then deleting if true or deleting no matter what. I tried doing a speed test, but the stupid thing must be messed up because it keeps creating infinate loops (I did a simple one with a message box and it would never end), so I'm going to have to redownload. Until then, use the current method of checking every time because I'm assuming GetVar is faster than PutVar (ExistVar and DelVar are simple variations of each).

Anyway, it does do something dramatic. I saved a backup of the old account files for Elysium (which are larger than Mirage's ones, so keep that in mind, but accounts are the only data files still ini for Elysium) so that I could compare them in the future. The size of a new account file with one char created is 299 bytes. The old size was 5.24 KB (5240 bytes). That's a pretty dramatic difference, especially because you get the small size without losing performance (well, you do lose some when you use PutVar with a null value, but you make up for it when you use GetVar at all).

Here we are:

----------

SERVER SIDE

----------

Find:
Code:
Public Function GetVar(File As String, Header As String, Var As String) As String
Dim sSpaces As String   ' Max string length
Dim szReturn As String  ' Return default value if not found
  
    szReturn = ""
  
    sSpaces = Space(5000)
  
    Call GetPrivateProfileString(Header, Var, szReturn, sSpaces, Len(sSpaces), File)
  
    GetVar = RTrim(sSpaces)
    GetVar = Left(GetVar, Len(GetVar) - 1)
End Function

Public Sub PutVar(File As String, Header As String, Var As String, Value As String)
    Call WritePrivateProfileString(Header, Var, Value, File)
End Sub

Replace with:
Code:
Public Function GetVar(File As String, Header As String, Var As String) As String
Dim sSpaces As String

    sSpaces = Space$(5000)
    File = App.Path & "\" & File
    GetPrivateProfileString Header, Var, vbNullString, sSpaces, Len(sSpaces), File
    GetVar = RTrim$(sSpaces)
    GetVar = Left$(GetVar, Len(GetVar) - 1)

End Function

Public Function ExistVar(ByVal File As String, ByVal Header As String, ByVal Var As String) As Boolean
Dim sSpaces  As String

    sSpaces = Space$(5000)
    GetPrivateProfileString Header, Var, "somethingwierdheresothatitcouldntbeguessed", sSpaces, Len(sSpaces), File
    ExistVar = RTrim$(sSpaces) = "somethingwierdheresothatitcouldntbeguessed"

End Function

Public Sub PutVar(File As String, Header As String, Var As String, Value As String)

    If Trim$(Value) = "0" Or Trim$(Value) = vbNullString Then
        If ExistVar(File, Header, Var) Then
            DelVar File, Header, Var
        End If
    Else
        WritePrivateProfileString Header, Var, Value, File
    End If

End Sub

Public Sub DelVar(sFileName As String, sSection As String, sKey As String)

    WritePrivateProfileString sSection, sKey, vbNullString, sFileName

End Sub

----------

Whew! You must of almost broken into a sweat!
Reply
#2
Code:
sSpaces = Space$(5000)

that alone makes it horrible. max needed is 255.
Reply
#3
For this to work correctly on Window ME systems, it shoudl be this:

Code:
Public Function GetVar(File As String, Header As String, Var As String) As String
Dim sSpaces As String   ' Max string length

    sSpaces = Space(5000)

    Call GetPrivateProfileString(Header, Var, "", sSpaces, Len(sSpaces), File)

    GetVar = RTrim(sSpaces)
    GetVar = Left(GetVar, Len(GetVar) - 1)
End Function

vbNullString causes an error on this line:
Code:
GetVar = Left$(GetVar, Len(GetVar) - 1)

The only way it work was for me to add the empty string quotes.

Also thought I'd mention that you left out the "Dim sSpaces As String" code, but anyone could figure that out... I'd hope :o .
Reply
#4
Leighland Wrote:Also thought I'd mention that you left out the "Dim sSpaces As String" code, but anyone could figure that out... I'd hope :o .

You would be surprised Sad
Reply
#5
I'm not saying INIs are better, I'm just saying there is a way to make them look better.

As for "sSpaces = Space(5000)", that just defines 5000 as the maximum length for a string to return. The only reason I can see it being that high is for the MOTD or descriptions. You could set it to 500 or something, but 255 may be cutting it too close where you start to lose functionability.

Didn't notice I missed the Dim, thanks for pointing that out.

I haven't actually tried, but I'd like to think that binary delivers the same or similar file size reduction. Somebody will have to get me a number, but the Elysium account file example reduced the size by almost 18 times. The only real differences between binary and ini files is that one uses more text than it needs to make identifying possible ("Name=" is just extra space used up, but it is only 4 times larger than Binary) and that binary files need to be loaded all at once and saved into memory.

Once again, I'm not telling you to run out and change the binary formats back to INI, I'm just saying that some simple changes can make it much better than it is right now.

Heck, if you want to try it out, get me the size of the binary version of the default items file and I'll see what I can with INI. Eh?
Reply
#6
You could make inis a tad faster by putting all items in their own separate ini file.. You could also do this with spells, etc.. But yeah.. Dave is right.. There is no way in hell inis holding the same amount of data as a binary file be faster than a binary file.. I dont want to go through it, but Dave and loads of other people have gone through the whole text being converted to binary, etc..

Although, this isnt a bad idea.. I mean everything and anything to do with making ms faster is good..
Reply
#7
You'll notice I never said INI will be faster than binary, just the possibility of it being smaller.

This tutorial is more focused on being integrated into an engine with INIs like Mirage, and not really games made from that engine with the possibility of binary still around.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)