Jump to content
  • Advertisement
Sign in to follow this  
ScopeDynamo

[.net] BankStreamer

This topic is 4609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's a class for C# that lets you poke and peek bytes/int/short into a basic style bank at any byte index, and load/save them using threads so you can use it for streamable media etc. I can upload the .dll if any Visual Basic users want to use it. Use the functions IsSaved,IsLoaded to determine when the class is finished loading or saving a bank.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace BankThreader
{
   public class Bank
    {
        private byte[] data;
        private byte[][] sdata;
        private int size, chunksize;
        Thread SaveThread;
        Thread LoadThread;
        string filePath;
        public Bank()
        {
        }
        public Bank(int bytes)
        {
            Create(bytes);
        }
        ~Bank()
        {
        }
        public void Create(int bytes)
        {
            data = new byte[bytes];
      
        }
        public void Save(string filename)
        {
            filePath = filename;
            SaveThread = new Thread(new ThreadStart(WriteData));
            SaveThread.Start();
        }
        public void Load(string filename)
        {
            filePath = filename;
            LoadThread = new Thread(new ThreadStart(ReadData));
            LoadThread.Start();
        }
        public void Stream(string filename)
        {
            filePath = filename;
            LoadThread = new Thread(new ThreadStart(StreamData));
            LoadThread.Start();
        }

        public bool IsSaved()
        {
            ThreadState ts=SaveThread.ThreadState;
            if (ts == ThreadState.Stopped)
            {
                return true;
            };
            return false;
        }
        public bool IsLoaded()
        {
            ThreadState ts = LoadThread.ThreadState;
            if (ts == ThreadState.Stopped)
            {
                return true;
            }
            return false;
        }
        public void ReadData()
        {
            
            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            Create((int)file.Length);
            BinaryReader br = new BinaryReader(file);
            br.Read(data, 0,(int) file.Length);
            br.Close();
        }
        public void WriteData()
        {
            FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(file);
            bw.Write(data);
            bw.Close();
        }
        public void PokeByte(int index, byte val)
        {
            data[index] = val;
        }
        public byte PeekByte(int index)
        {
            return data[index];
        }
        
        public void PokeInt(int index, int val)
        {
            unsafe
            {
                fixed (byte* bytePtr = data)
                {
                    byte* byteIter = bytePtr + index;
                    *((int*)byteIter) = val;
                }
            }
        }
        public int PeekInt(int index)
        {
            unsafe
            {
                fixed (byte* bytePtr = data)
                {
                    byte* byteIter = bytePtr + index;
                    return *((int*)byteIter);
                }
            }
        }
        public void PokeShort(int index, short val)
        {
            unsafe
            {
                fixed (byte* bytePtr = data)
                {
                    byte* byteIter = bytePtr + index;
                    *((short*)byteIter) = val;
                }
            }
        }
        public int PeekShort(int index)
        {
            unsafe
            {
                fixed (byte* bytePtr = data)
                {
                    byte* byteIter = bytePtr + index;
                    return *((short*)byteIter);
                }
            }
        }
        
    }
}

Example
using System;
using System.Collections.Generic;
using System.Text;
using BankThreader;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Bank tst = new Bank(10024);
          
            tst.Load("Test.txt");
            Console.WriteLine("Saving Test Bank.");
            while (tst.IsLoaded() == false)
            {
                Console.Write(".");
            }
            Console.WriteLine("Fin.");
            Console.WriteLine("Bank Loaded.");
            byte t = tst.PeekByte(0);
            Console.WriteLine(t);
        }
    }
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!