/// <summary> /// Loads in data from the raw secret data provided /// </summary> /// <param name="secret">The raw secret data</param> /// <example> /// This example demonstrates loading a <see cref="MemorySecret"/> from a /// a byte array containing an encoded secret. /// <code language="C#"> /// // 6●sW↑ /// byte[] rawSecret = new byte[] /// { /// 55, 21, 41, 18, 59 /// }; /// Secret secret = new MemorySecret(); /// secret.Load(rawSecret); /// </code> /// </example> public override void Load(byte[] secret) { if (secret == null || secret.Length != Length) { throw new InvalidSecretException("Secret must contatin exactly 5 bytes"); } byte[] decodedBytes = DecodeBytes(secret); string decodedSecret = ByteArrayToBinaryString(decodedBytes); if (decodedSecret[3] != '1' && decodedSecret[4] != '1') { throw new ArgumentException("The specified data is not a memory code", "secret"); } GameID = Convert.ToInt16(decodedSecret.ReversedSubstring(5, 15), 2); Memory = (Memory)Convert.ToByte(decodedSecret.ReversedSubstring(20, 4), 2); // Because the game and return type are stored in the cipher and checksum // we compare it against all four possible values. It's ugly, but it works. string desiredString = SecretParser.CreateString(secret); var memories = new[] { new MemorySecret(Game.Ages, GameID, Memory, true), new MemorySecret(Game.Ages, GameID, Memory, false), new MemorySecret(Game.Seasons, GameID, Memory, true), new MemorySecret(Game.Seasons, GameID, Memory, false) }; bool found = false; foreach (var memSecret in memories) { if (desiredString == memSecret.ToString()) { TargetGame = memSecret.TargetGame; IsReturnSecret = memSecret.IsReturnSecret; found = true; break; } } if (!found) { throw new InvalidSecretException("Cound not determine all properties of this secret"); } }
/// <summary> /// Returns a string that represents the current secret. /// </summary> /// <returns>A string that represents the current secret.</returns> /// <seealso cref="SecretParser.CreateString"/> /// <remarks> /// ToString will format the secret as it would be represented in the games. /// Internally, this method calls <see cref="ToBytes"/> and passes the result /// to <see cref="SecretParser.CreateString"/>. /// </remarks> /// <example> /// This example demonstrates how to get secret string from a <see cref="GameSecret"/>. /// <code language="C#"> /// GameSecret secret = new GameSecret() /// { /// TargetGame = Game.Ages, /// GameID = 14129, /// Hero = "Link", /// Child = "Pip", /// Animal = Animal.Dimitri, /// Behavior = ChildBehavior.BouncyD, /// IsLinkedGame = true, /// IsHeroQuest = false /// }; /// string secretString = secret.ToString(secret); /// // H~2:@ ←2♦yq GB3●( 6♥?↑6 /// </code> /// </example> public override string ToString() { return(SecretParser.CreateString(ToBytes())); }