| Mirage Source http://www.miragesource.net/forums/ |
|
| Make INI Files Look Appealing Again... http://www.miragesource.net/forums/viewtopic.php?f=210&t=553 |
Page 1 of 1 |
| Author: | pingu [ Mon Sep 25, 2006 2:31 am ] |
| Post subject: | Make INI Files Look Appealing Again... |
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! |
|
| Author: | Lea [ Mon Sep 25, 2006 2:50 am ] |
| Post subject: | |
Quote: and makes INI files smaller than binary ones in most cases!
Using INI your computer is still crunching more data per number than it would with a binary system. The files may be smaller, but you're still dealing with text, and overall it would (should?) be slower. Also, how could the file be smaller than replacing a byte with a 40 byte string? o.O? |
|
| Author: | grimsk8ter11 [ Mon Sep 25, 2006 10:36 am ] |
| Post subject: | |
Code: sSpaces = Space$(5000)
that alone makes it horrible. max needed is 255. |
|
| Author: | Verrigan [ Mon Sep 25, 2006 1:58 pm ] |
| Post subject: | |
Microsoft Platform SDK wrote: WritePrivateProfileString
The WritePrivateProfileString function copies a string into the specified section in an initialization file. Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry. I agree whole-heartedly. |
|
| Author: | Leighland [ Mon Sep 25, 2006 5:12 pm ] |
| Post subject: | |
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 |
|
| Author: | Misunderstood [ Mon Sep 25, 2006 7:03 pm ] |
| Post subject: | |
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
You would be surprised |
|
| Author: | pingu [ Mon Sep 25, 2006 10:10 pm ] |
| Post subject: | |
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? |
|
| Author: | Lea [ Mon Sep 25, 2006 10:53 pm ] |
| Post subject: | |
Right, we know that your file will be a lot smaller where all the values are 0, because you simply dont save them and assume 0. However, your file also changes size. Binary files stay the same size no matter what. Also, your claim that you cant load a data member when needed is false. You can open the file and load any one part at any time you feel necessary. It's a downside of the Mirage Source code itself that has the server load all the information at startup. |
|
| Author: | Dark Echo [ Tue Sep 26, 2006 1:21 am ] |
| Post subject: | |
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.. |
|
| Author: | pingu [ Tue Sep 26, 2006 11:32 pm ] |
| Post subject: | |
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. |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |
|