C64Lib.Core.Job1541.OpenD64File C# (CSharp) Method

OpenD64File() private method

private OpenD64File ( string filepath, Stream stream ) : void
filepath string
stream Stream
return void
        void OpenD64File(string filepath, Stream stream)
        {
            long size;
            byte[] magic = new byte[4];
            byte[] bam = new byte[256];

            // Clear GCR buffer
            memset(gcr_data, 0x55);

            #if false
            //// Try opening the file for reading/writing first, then for reading only
            //write_protected = false;
            //try
            //{
            //    the_file = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite);
            //}
            //catch (UnauthorizedAccessException)
            //{
            //    write_protected = true;
            //}

            //if (the_file == null)
            //    the_file = new FileStream(filepath, FileMode.Open, FileAccess.Read);
            #endif

            write_protected = true;
            the_file = stream;

            size = the_file.Length;
            // Check length
            if (size < D64Drive.NUM_SECTORS * 256)
            {
                CloseD64File();
                return;
            }

            // x64 image?
            the_file.Read(magic, 0, 4);
            if (magic[0] == 0x43 && magic[1] == 0x15 && magic[2] == 0x41 && magic[3] == 0x64)
                image_header = 64;
            else
                image_header = 0;

            // Preset error info (all sectors no error)
            memset(error_info, 1);

            // Load sector error info from .d64 file, if present
            if (image_header == 0 && size == D64Drive.NUM_SECTORS * 257)
            {
                the_file.Seek(D64Drive.NUM_SECTORS * 256, SeekOrigin.Begin);
                the_file.Read(error_info, 0, D64Drive.NUM_SECTORS);
            };

            // Read BAM and get ID
            read_sector(18, 0, bam);
            id1 = bam[162];
            id2 = bam[163];

            // Create GCR encoded disk data from image
            disk2gcr();
        }