System.IO Extension

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
User avatar
xzodia




Translator Connoisseur Coagulator

Posts: 1981
Joined: Sun May 15, 2005 10:31 am
Location: UK
Contact:

System.IO Extension

Post by xzodia »

This is a little something I whipped up, since I've never been satisfied with existing forms of reading/writing data to a file...

The main class in this library is the DataStream which is a wrapper which combines the binary reader and binary writer with a few extra features:
  • Bookmark Stack -
    • IMO this is one of the best ideas I've ever come up with, its also very simple. If you are at a point in a file but you want to go to another part of the file and then return (eg for a reflexive), simply ".PushBookmark" to add a return point and ".PopBookmark" to return. An understanding of the stack structure is good to understand how useful this is.
    3 Different String Read/Write Methods -
    • When reading or writing a string you can specify how to do so. There are 3 options, Characters Only (When reading this requires the user to specify the character count), Null Terminated and System IO Standard.
      Reverse bool: If true the string is automatically reversed after read / before write.
    Memory Stream Read/Write -
    • Just a simple wrapper that returns a MemoryStream instead of a byte array.
    Flags Read/Write - Ability to read/write the included Flags class.
    IDatatizable Read/Write - Ability to read/write any user defined class/struct via the included IDatatizable interface.
    Extendible Design - The Binary Reader and Binary Writer are exposed to inherited class to allow you to easily add your own read/write functions.
The IDatatizable interface adds a Write(DataStream) void to your class/struct where you can write whatever info you like to write your class. To read your class/struct from a DataStream add a constructor with just a DataStream variable and read whatever info you like.

The Flags class is an abstact class with 4 ready made inherited classes (8,16,32,64 bits). To see how to expand on these look at the source. The Flags class uses a compact algorithm which I came up with to read/write bits, people who hate messy code should like it :P

Bare in mind I haven't tested any of this, so if you find any bugs let me know (its possible the flags write in reverse for halo standards but its easy to fix if this is the case)

Source Code Here
Attachments
System.IO Extension.zip
Updated
(4.13 KiB) Downloaded 7 times
Last edited by xzodia on Thu Sep 04, 2008 5:25 pm, edited 2 times in total.
Image
Halo 2 Plugins | Lock-on To Just About Anything | My Sites | Snow Hog
Old Plugins you have, upgrade you must...
Always Maintain a High Quality-To-Crap Ratio.
User avatar
kornman00




ONI New Age

Posts: 146
Joined: Fri Dec 12, 2003 6:30 pm
Contact:

Post by kornman00 »

Just some two cents...

Flags:
* FillBoolArray should be declared as static as it doesn't interface with any instance data
* FillBoolArray's 'flags' argument doesn't need a reference modifier, you're not modifying the actual object, only it's data (you'd want to ref it had you said 'flags = new bool[]' in the function
* Overall, I would suggest sticking with System.Collections.BitArray. Its not limited to 8,16,32, or 64 bits. You could easily create a new class which inherits from BitArray but also supports your data stream interface.

IDatatizable:
* You should allow objects to also sport a 'Read' function so as to not force them to perform the 'datatizing' upon the object's creation.

DataStream:
* For your string functions you'd be better off creating private functions which only do code like say, reading a null terminated string. This not only avoids source code duplication (which requires double maintenance and testing) but also makes JIT code creation more efficient.
User avatar
xzodia




Translator Connoisseur Coagulator

Posts: 1981
Joined: Sun May 15, 2005 10:31 am
Location: UK
Contact:

Post by xzodia »

Thanks for the tips, I was unaware of the BitArray class. I tried to inherit from it but its a sealed class so its not possible :? , also it doesn't have any way of getting a numerical value from it so I prefer mine tbh.

Don't quite understand what your saying about the read function...

String methods changed.

Edit: New Source and Binaries uploaded.
Image
Halo 2 Plugins | Lock-on To Just About Anything | My Sites | Snow Hog
Old Plugins you have, upgrade you must...
Always Maintain a High Quality-To-Crap Ratio.
Post Reply