Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Game Attack Combos : WPF Hybrid Smart Client for Combo Calculations

, 23 May 2009
A WPF hybrid smart client for calculating attack combos in the Prince of Persia game.
GameAttackCombos_Src.zip
ComboServices
App_Data
ComboPackagesService.svc
GameAttackCombos.snk
Properties
GameAttackCombos.mdf
GameAttackCombos_log.LDF
Packages
PrinceOfPersia2008.gcp
PrinceOfPersia2010.gcp
PackageComboFiles
Properties
ServiceData
GameAttackCombosModel.edmx
Properties
GameAttackCombos.snk
Assets
Button Icons.design
Game Attack Combos Icon.design
Game Attack Combos Icon.ico
Prince of Persia 2008
Prince of Persia 2010
PrinceOfPersia2010Background.png
PrinceOfPersia2010Icon.png
PrinceOfPersia2008Background.png
PrinceOfPersia2008Icon.png
ComboClient
Game Attack Combos Icon.ico
Properties
Resources
Service References
Supporting
Themes
ComboServices
ComboPackagesService.disco
ComboPackagesService.wsdl
ComboPackagesService1.wsdl
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
app.manifest
ComboLogic
Properties
GameAttackCombos_Standalone.zip
Packages
PrinceOfPersia2008.gcp
PrinceOfPersia2010.gcp
Resources
PresentationFramework.Aero.dll
GameAttackCombos.exe
GameAttackCombos.Logic.dll
using System;
using System.IO;

namespace GG.GameAttackCombos.Logic {

	/// <summary>
	/// A helper class for stream-related tasks.
	/// </summary>
	public static class StreamHelper {

		/// <summary>
		/// Copies the specified Stream to a byte array.
		/// </summary>
		/// <param name="sourceData">The source byte array to copy from.</param>
		/// <param name="targetStream">The target Stream to copy to.</param>
		public static void CopyArrayToStream(byte[] sourceData, Stream targetStream) {
			const int BufferSize = 1 << 16;

			if (sourceData == null) {
				throw new ArgumentNullException("sourceData");
			}
			if (targetStream == null) {
				throw new ArgumentNullException("targetSource");
			}

			// Create a buffer for copying in chunks.
			byte[] Buffer = new byte[BufferSize];

			// Read bytes from the source data and write them to the target stream until all are read.
			int i = 0;
			int BytesToRead = Math.Min(BufferSize, sourceData.Length);
			while (BytesToRead > 0) {
				targetStream.Write(sourceData, i, BytesToRead);
				i += BytesToRead;

				// Calculate the number of bytes to read next.
				BytesToRead = Math.Min(BufferSize, sourceData.Length - i);
			}
		}

		/// <summary>
		/// Copies the data from the source file to the target stream.
		/// </summary>
		/// <param name="sourceFilePath">The path to the source file to copy from.</param>
		/// <param name="targetStream">The Stream to copy to.</param>
		public static void CopyFileToStream(string sourceFilePath, Stream targetStream) {
			if (!string.IsNullOrEmpty(sourceFilePath)) {
				if (File.Exists(sourceFilePath)) {
					// Open the source file and copy it to the target stream.
					using (FileStream Source = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
						CopyStream(Source, targetStream);
					}
				} else {
					throw new FileNotFoundException("The source file cannot be found.", sourceFilePath);
				}
			} else {
				throw new ArgumentNullException("sourceFilePath");
			}
		}

		/// <summary>
		/// Copies the data from the source stream to the target stream.
		/// </summary>
		/// <remarks>
		/// Both streams must already be opened.
		/// </remarks>
		/// <param name="source">The Stream to copy from.</param>
		/// <param name="target">The Stream to copy to.</param>
		public static void CopyStream(Stream source, Stream target) {
			const int BufferSize = 1 << 16;

			if (source == null) {
				throw new ArgumentNullException("source");
			}
			if (target == null) {
				throw new ArgumentNullException("target");
			}

			// Create a buffer for copying in chunks.
			byte[] Buffer = new byte[BufferSize];

			// Read bytes from the source stream until all are read.
			source.Position = 0;
			int BytesRead = 0;
			try {
				while ((BytesRead = source.Read(Buffer, 0, BufferSize)) > 0) {
					// Write the exact number of bytes read from source to target.
					target.Write(Buffer, 0, BytesRead);
				}
			} finally {
				// Flush the target stream when done copying.
				target.Flush();
			}
		}

		/// <summary>
		/// Copies the specified Stream to a byte array.
		/// </summary>
		/// <param name="sourceStream">The source Stream to copy from.</param>
		/// <returns>A byte array containing a copy of the source stream data.</returns>
		public static byte[] CopyStreamToArray(Stream sourceStream) {
			const int BufferSize = 1 << 16;

			if (sourceStream == null) {
				throw new ArgumentNullException("sourceStream");
			}

			// Initialize the target array.
			byte[] Result =  new byte[sourceStream.Length];

			// Read bytes from the source stream and write them to the target array until all are copied.
			sourceStream.Position = 0;
			int i = 0;
			int BytesRead = 0;
			do {
				BytesRead = sourceStream.Read(Result, i, Math.Min(BufferSize, Result.Length - i));
				i += BytesRead;
			} while (BytesRead > 0);

			return Result;
		}

	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Matt Sollars
Web Developer
United States United States
I began programming on my Commodore 64 at around the age of 12. After migrating to DOS and then Windows, I decided to take on the Web. Several languages and platforms later, I have settled in with .NET nicely. I am currently the owner of a software consulting company and lead application developer for a learning-based technology consultation company.
 
The love of a finished application is usually at war with the desire to improve it as soon as it's released (they're never really finished).

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 23 May 2009
Article Copyright 2009 by Matt Sollars
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid