25-09-2006, 02:31 AM
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:
Replace with:
----------
Whew! You must of almost broken into a sweat!
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!