public SoundFit Clone(string newName) //creates and returns a clone of itself //only difference being a new name { SoundFit copy = new SoundFit(newName); for (int ii = 1; ii < lines.Count - 1; ii++) { copy.Add(this.lines[ii].Clone()); //actually inserts at next-to-last index } return(copy); }
public void ImportLayoutDotFitFile(int numWorkouts, string pathToLayoutFit) { char[] charBuf = new char[32]; lines.Clear(); sFs.Clear(); bFs.Clear(); System.IO.StreamReader objFile = null; objFile = new System.IO.StreamReader(pathToLayoutFit); for (int ii = 0; ii < numWorkouts; ii++) { objFile.ReadBlock(charBuf, 0, 32); lines.Add(new LayoutLine(charBuf)); if (ii == 0) { //determine workoutLength string layoutDirectory = pathToLayoutFit.Substring(0, pathToLayoutFit.Length - 10); workoutLength = getWorkoutLength(layoutDirectory + lines[ii].binaryFitFileName + ".fit"); } bFs.Add(new BinaryFit(lines[ii].binaryFitFileName, workoutLength)); sFs.Add(new SoundFit(lines[ii].soundFitFileName)); //okay, at this point we now have the LayoutFit object constructed //along with some empty BinaryFit and SoundFit objects //now we need to populate the BinaryFit and SoundFit objects with //valid information from the files on the disk //first we need to fill in the command blocks for this newly created //BinaryFit file BinaryFit bF = bFs[ii]; SoundFit sF = sFs[ii]; string bFName = new string(charBuf, 0, 8); string sFName = new string(charBuf, 9, 8); if (bFName.Equals("********")) //nothing to read from disk here { ASCIIEncoding AE = new ASCIIEncoding(); byte[] me = new byte[1]; //byte me :> :P :) ;) me[0] = lowByte((short)ii); me[0] += 65; //'A' string bFNamer = string.Format("W000000{0}", new String(AE.GetChars(me))); string sFNamer = string.Format("S000000{0}", new String(AE.GetChars(me))); bFs[ii] = new BinaryFit(bFNamer, workoutLength); sFs[ii] = new SoundFit(sFNamer); Replace(ii, new LayoutLine(bFNamer, sFNamer)); } else //each BinaryFit object can load itself from disk { string sFPath = pathToLayoutFit; sFPath = sFPath.Replace("layout", sFName); sF.LoadFromDisk(sFPath); string bFPath = pathToLayoutFit; bFPath = bFPath.Replace("layout", bFName); bF.LoadFromDisk(bFPath, sF); } } }
public void LoadFromDisk(string path, SoundFit sF) { blocks.Clear(); //clear out any pre-existing command blocks in this object blocks = new List <CommandBlock>(250); BinaryReader objFile = new BinaryReader(File.Open(path, FileMode.Open)); BinaryReader iniFile = null; string iniPath = path.Substring(0, path.Length - 4) + ".bin"; bool bIniExists = File.Exists(iniPath); if (bIniExists) { try { iniFile = new BinaryReader(File.Open(iniPath, FileMode.Open)); } catch (Exception e) { MessageBox.Show(e.ToString()); bIniExists = false; //couldn't open it, so ignore it } } // int ii = 0; byte[] byteBuf = new byte[9]; //9 bytes is the largest command block type byte[] iniBuf = new byte[1]; //only need a one-byte buffer since we'll only be reading one byte at a time byteBuf = objFile.ReadBytes(2);//first 2 bytes always command count blocks.Add(new CommandBlock(BlockType.CommandCount, byteBuf[0] * 256 + byteBuf[1])); //now we have to peek ahead to see how many bytes to read for each new block bool bContinue = true; while (bContinue)//we'll break out at the end of the file { byte commandByte; try { commandByte = objFile.ReadByte(); } catch (EndOfStreamException e) { // MessageBox.Show(e.ToString()); EndOfStreamException eos = e; objFile.Close(); bContinue = false; commandByte = 0xff; } int exp1, exp2, exp3, exp4; int timeParam, speedParam, inclineParam, wavParam; BlockType t; switch (commandByte) { case 0x04: t = BlockType.UnknownBlock02; //3 bytes in this block byteBuf = objFile.ReadBytes(2); //already read the first byte if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else //the checksum test passed, so we're good to go with this block { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x05: t = BlockType.ShowProgressGraphics; //3 bytes byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x0c: t = BlockType.ShowInitialTime; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { timeParam = byteBuf[0]; blocks.Add(new CommandBlock(t, timeParam)); } break; case 0x0f: t = BlockType.GenerateBarGraph; //2 bytes byteBuf = objFile.ReadBytes(1); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { blocks.Add(new CommandBlock(t)); } break; case 0x0d: t = BlockType.MaxRunTime; byteBuf = objFile.ReadBytes(4); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; timeParam = (byteBuf[1] * 256 + byteBuf[2]) / 60; //timeParam is converted from seconds to minutes blocks.Add(new CommandBlock(t, timeParam, exp1)); } break; case 0x06: t = BlockType.ShowInitialSpeed; byteBuf = objFile.ReadBytes(3); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { speedParam = byteBuf[0]; exp1 = byteBuf[1]; blocks.Add(new CommandBlock(t, speedParam, exp1)); } break; case 0x07: t = BlockType.UnknownBlock08; byteBuf = objFile.ReadBytes(3); //already read the first byte if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; exp2 = byteBuf[1]; blocks.Add(new CommandBlock(t, exp1, exp2)); } break; case 0x08: t = BlockType.ShowInitialIncline; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { inclineParam = byteBuf[0]; blocks.Add(new CommandBlock(t, inclineParam)); } break; case 0x1c: t = BlockType.UnknownBlock10; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x12: t = BlockType.UnknownBlock11; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x22: t = BlockType.UnknownBlock12; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x13: t = BlockType.UnknownBlock13; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x2d: t = BlockType.UnknownBlock14; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x14: t = BlockType.UnknownBlock15; byteBuf = objFile.ReadBytes(3); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; exp2 = byteBuf[1]; blocks.Add(new CommandBlock(t, exp1, exp2)); } break; case 0x23: t = BlockType.UnknownBlock16; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x24: t = BlockType.UnknownBlock17; byteBuf = objFile.ReadBytes(2); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; blocks.Add(new CommandBlock(t, exp1)); } break; case 0x51: t = BlockType.PausePriorToStart; byteBuf = objFile.ReadBytes(4); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; exp2 = byteBuf[1]; exp3 = byteBuf[2]; blocks.Add(new CommandBlock(t, exp1, exp2, exp3)); } break; case 0x15: t = BlockType.SetInitialSpeedAndIncline; byteBuf = objFile.ReadBytes(3); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { speedParam = byteBuf[0]; inclineParam = byteBuf[1]; blocks.Add(new CommandBlock(t, speedParam, inclineParam)); } break; case 0x5a: t = BlockType.FetchWaveFile; byteBuf = objFile.ReadBytes(5); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { wavParam = byteBuf[0]; exp1 = byteBuf[1]; exp2 = byteBuf[2]; exp3 = byteBuf[3]; CommandBlock block = new CommandBlock(t, wavParam, exp1, exp2, exp3); block.CurrentWaveFileName = sF.GetFileNameAtIndex(wavParam) + ".WAV"; blocks.Add(block); } break; case 0x5b: t = BlockType.PlayFetchedWaveFile; byteBuf = objFile.ReadBytes(4); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; exp2 = byteBuf[1]; exp3 = byteBuf[2]; blocks.Add(new CommandBlock(t, exp1, exp2, exp3)); } break; case 0x60: t = BlockType.PlayWaveFile; byteBuf = objFile.ReadBytes(8); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; timeParam = byteBuf[1] * 256 + byteBuf[2]; wavParam = byteBuf[3]; exp2 = byteBuf[4]; exp3 = byteBuf[5]; exp4 = byteBuf[6]; CommandBlock block = new CommandBlock(t, timeParam, wavParam, exp1, exp2, exp3, exp4); block.CurrentWaveFileName = sF.GetFileNameAtIndex(wavParam) + ".WAV"; blocks.Add(block); } break; case 0x5d: t = BlockType.AdjustSpeedAndIncline; byteBuf = objFile.ReadBytes(8); if (bIniExists) { try { iniBuf = iniFile.ReadBytes(1); } catch (Exception e) { MessageBox.Show(e.ToString()); bIniExists = false; //couldn't read ini file, so ignore it } } if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { exp1 = byteBuf[0]; int hiSeconds = byteBuf[1]; int loSeconds = byteBuf[2]; timeParam = (hiSeconds * 256 + loSeconds) / 60; speedParam = byteBuf[3]; exp2 = byteBuf[4]; inclineParam = byteBuf[5]; int x60s = x60sFromEnd(); int xParam = 0xf7 - 9 * x60s; CommandBlock block = new CommandBlock(t, timeParam, speedParam, inclineParam, xParam, exp1, exp2); if (bIniExists && iniBuf[0] == 0x00) { block.IsGraduatable = false; } else { block.IsGraduatable = true; } blocks.Add(block); } break; case 0x01: t = BlockType.EndProgram; byteBuf = objFile.ReadBytes(1); if (!isGoodChecksum(commandByte, byteBuf)) { diskError(); return; } else { blocks.Add(new CommandBlock(t)); } break; case 0xff: //end of file has been reached //0xff is not a real command, we just use it as a //signal that we reached the end of the file stream break; default: MessageBox.Show("Logical Error in BinaryFit.LoadFromDisk(): unknown command block type"); break; } } }