Password.hh

Go to the documentation of this file.
00001 /*
00002  * Metroid Password Generator
00003  * Copyright (C) 2005,2007-2008 emuWorks
00004  * http://games.technoplaza.net/
00005  *
00006  * This file is part of Metroid Password Generator.
00007  *
00008  * Metroid Password Generator is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Metroid Password Generator is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Metroid Password Generator; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022  
00023 // $Id: Password.hh,v 1.12 2008/12/17 00:23:12 jdratlif Exp $
00024 
00025 #ifndef MPG_PASSWORD_HH_
00026 #define MPG_PASSWORD_HH_
00027 
00028 #include <wx/string.h>
00029 
00030 #include "exceptions/InvalidPasswordException.hh"
00031 
00032 namespace mpg {
00033     const int PASSWORD_BITS = 128;
00034     const unsigned int PASSWORD_LENGTH = 24;
00035     const int PASSWORD_LENGTH_BYTES = 18;
00036     
00037     enum {
00038         MARUMARI_TAKEN, MISSILE_BS1, REDDOOR_BS_LONGBEAM,
00039         REDDOOR_BS_TOURIANBRIDGE, ENERGYTANK_BS1, REDDOOR_BS_BOMBS, BOMBS_TAKEN,
00040         REDDOOR_BS_ICEBEAM, MISSILE_BS2, ENERGYTANK_BS2, REDDOOR_BS_VARIA,
00041         VARIA_TAKEN, ENERGYTANK_BS3, MISSILE_NF1, MISSILE_NF2,
00042         REDDOOR_NF_ICEBEAM, MISSILE_NF3, MISSILE_NF4, MISSILE_NF5, MISSILE_NF6,
00043         MISSILE_NF7, MISSILE_NF8, MISSILE_NF9, REDDOOR_NF_HIGHJUMPBOOTS,
00044         HIGHJUMPBOOTS_TAKEN, REDDOOR_NF_SCREWATTACK, SCREWATTACK_TAKEN,
00045         MISSILE_NF10, MISSILE_NF11, REDDOOR_NF_WAVEBEAM, ENERGYTANK_NF,
00046         MISSILE_NF12, REDDOOR_KL1, MISSILE_KL1, MISSILE_KL2, REDDOOR_KL2,
00047         ENERGYTANK_KL, REDDOOR_KL3, REDDOOR_KL4, MISSILE_KL3, MISSILE_KL4,
00048         REDDOOR_KL_KRAID, ENERGYTANK_KL_KRAID, MISSILE_RL1, REDDOOR_RL,
00049         ENERGYTANK_RL, MISSILE_RL2, REDDOOR_RL_RIDLEY, ENERGYTANK_RL_RIDLEY,
00050         MISSILE_RL3, REDDOOR_T1, REDDOOR_T2, REDDOOR_T3, ZEBETITE1, ZEBETITE2,
00051         ZEBETITE3, ZEBETITE4, ZEBETITE5, MOTHERBRAIN, START_NF = 64, START_KL,
00052         START_RL, RESET, SWIMSUIT = 71, BOMBS, HIGHJUMPBOOTS, LONGBEAM,
00053         SCREWATTACK, MARUMARI, VARIA, WAVEBEAM, ICEBEAM, MISSILES,
00054         GAMETIME = 88, RIDLEY = 124, RIDLEY_STATUE, KRAID, KRAID_STATUE
00055     };
00056     
00057     /// Class to encapsulate a Metroid Password
00058     class Password {
00059     private:
00060         wxString encoded;
00061         unsigned char data[18];
00062         
00063         static const int MISSILE_COUNT_BYTE = 10;
00064         static const int GAME_TIME_BYTE = 11;
00065         static const int SHIFT_BYTE = 16;
00066         static const int CHECKSUM_BYTE = 17;
00067         static const unsigned char SPACE_VALUE = 0xFF;
00068 
00069         /**
00070          * Calculates the checksum value for this Password.
00071          *
00072          * @return The checksum.
00073          */
00074         unsigned char calcChecksum() const;
00075         
00076         /**
00077          * Decodes a Metroid password into our raw format.
00078          *
00079          * @param password The password to decode.
00080          * @param fixChecksum true if the checksum should be fixed to force a
00081          *                    valid password; false to accept only valid
00082          *                    Metroid passwords.
00083          *
00084          * @throw InvalidPasswordException if the given password is invalid.
00085          */
00086         void decode(const wxString &password, bool fixChecksum) 
00087             throw(InvalidPasswordException);
00088         
00089         /**
00090          * Encodes the raw password data into a Metroid password.
00091          */
00092         void encode();
00093 
00094     public:
00095         static const wxString ALPHABET;
00096     
00097         /**
00098          * Creates a new blank password.
00099          */
00100         Password();
00101         
00102         /**
00103          * Creates a password from a Metroid password.
00104          *
00105          * @param password The Metroid password.
00106          * @param fixChecksum true if the checksum should be fixed to force a
00107          *                    valid password; false to accept only valid
00108          *                    Metroid passwords.
00109          *
00110          * @throw InvalidPasswordException if an invalid password is supplied.
00111          */
00112         Password(const wxString &password, bool fixChecksum = false)
00113             throw(InvalidPasswordException);
00114         
00115         /**
00116          * Gets a bit from the raw password data.
00117          *
00118          * @param bit The bit to get (1-128).
00119          *
00120          * @return true if the bit is set; false otherwise.
00121          */
00122         bool getBit(int bit) const;
00123         
00124         /**
00125          * Sets a bit in the raw password data.
00126          *
00127          * @param bit the bit to set (1-128).
00128          * @param value true to set the bit; false to clear it.
00129          */
00130         void setBit(int bit, bool value = true);
00131         
00132         /**
00133          * Gets the encoded Metroid password encapsulated by this Password.
00134          *
00135          * @return The Metroid password.
00136          */
00137         const wxString &getEncoded() const;
00138         
00139         /**
00140          * Gets the game time in ticks (1 tick = ~4 seconds).
00141          *
00142          * @return The game time in ticks.
00143          */
00144         wxUint32 getGameTime() const;
00145         
00146         /**
00147          * Sets the game time.
00148          *
00149          * @param time The game time in ticks (1 tick = ~4 seconds).
00150          */
00151         void setGameTime(wxUint32 time);
00152         
00153         /**
00154          * Gets the number of missiles held by Samus.
00155          *
00156          * @return The number of missiles.
00157          */
00158         unsigned char getMissiles() const;
00159         
00160         /**
00161          * Sets the number of missiles held by Samus.
00162          *
00163          * @param missiles The number of missiles.
00164          */
00165         void setMissiles(unsigned char missiles);
00166         
00167         /**
00168          * Gets the shift byte used by this Password.
00169          *
00170          * @return The shift byte.
00171          */
00172         unsigned char getShift() const;
00173         
00174         /**
00175          * Sets the shift byte used by this Password.
00176          *
00177          * @param shift The shift byte.
00178          */
00179         void setShift(unsigned char shift);
00180     };
00181     
00182     inline const wxString &Password::getEncoded() const { return encoded; }
00183     inline unsigned char Password::getMissiles() const
00184         { return data[MISSILE_COUNT_BYTE]; }
00185     inline unsigned char Password::getShift() const { return data[SHIFT_BYTE]; }
00186 }
00187 
00188 #endif
00189 

Generated on Tue Dec 16 20:16:56 2008 for Metroid Password Generator by  doxygen 1.5.4