Ejemplo n.º 1
0
        public MainForm()
        {
            InitializeComponent();


            handTextBoxes = new TextBox[9] {
                handTextBox1, handTextBox2, handTextBox3, handTextBox4, handTextBox5,
                handTextBox6, handTextBox7, handTextBox8, handTextBox9
            };

            loadedHistoryResult   = null;
            historyResults        = new ArrayList();
            savedHistoryFileNames = new ArrayList();
            savedHistoryResults   = new Dictionary <int, Results>();

            daysRemaining = 0;

            analyzeFromHHButton.Text = "Analyze" + Environment.NewLine + "(Shift+Enter)";

            //Validate Software Registration
            bool isRegistered = false;

            identificationCode = String.Empty;
            registrationKey    = String.Empty;

            String identifier = "QOCalc";

            ManagementClass mc = new System.Management.ManagementClass("Win32_Processor");

            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                identifier += mo["ProcessorID"].ToString();
            }

            ManagementObject disk =
                new ManagementObject("win32_logicaldisk.deviceid=\"C:\"");

            disk.Get();
            identifier += disk["VolumeSerialNumber"].ToString();

            mc  = new ManagementClass("Win32_NetworkAdapterConfiguration");
            moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    identifier += mo["MacAddress"].ToString().Replace(":", "");
                    mo.Dispose();
                    break;
                }
                mo.Dispose();
            }

            int identifierHash = identifier.GetHashCode();

            byte[] hashBytes = BitConverter.GetBytes(identifierHash);
            foreach (byte b in hashBytes)
            {
                identificationCode += String.Format("{0,2:X2}", b);
            }

            DESCryptoServiceProvider key = new DESCryptoServiceProvider();

            key.Key = ASCIIEncoding.ASCII.GetBytes("12664961");
            key.IV  = ASCIIEncoding.ASCII.GetBytes("QODDCALC");

            MemoryStream ms        = new MemoryStream();
            CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

            encStream.Write(hashBytes, 0, hashBytes.Length);
            encStream.FlushFinalBlock();

            byte[] encryptedBytes = ms.ToArray();

            encStream.Close();
            ms.Close();

            foreach (byte b in encryptedBytes)
            {
                registrationKey += String.Format("{0,2:X2}", b);
            }

            try
            {
                reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\QuickOdds", true);
                String registrationKeyInRegistry = reg.GetValue("RegistrationKey").ToString();

                if (registrationKeyInRegistry.Equals(registrationKey))
                {
                    isRegistered = true;
                }
            }
            catch (Exception)
            {
                //key does not exist, therefore create registry data
                reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\", true);
                reg = reg.CreateSubKey("QuickOdds\\");
                reg.SetValue("RegistrationKey", "", RegistryValueKind.String);
                reg.SetValue("FirstRunDate", BitConverter.GetBytes((DateTime.Now.Ticks)));
            }
            if (!isRegistered)
            {
                DateTime firstRunDate;
                try
                {
                    firstRunDate = new DateTime(BitConverter.ToInt64((byte[])reg.GetValue("FirstRunDate"), 0));
                }
                catch (Exception)
                {
                    firstRunDate = DateTime.MinValue;
                }
                if (firstRunDate < DateTime.Now.AddDays(-14) || firstRunDate > DateTime.Now)
                {
                    //disable
                    DisableButtons();
                    resultsRichTextBox.Text += "TRIAL EXPIRED. Please register to continue using QuickOdds Calculator.";
                }
                else
                {
                    TimeSpan timeRemaining = firstRunDate - DateTime.Now;
                    daysRemaining            = timeRemaining.Days + 14;
                    resultsRichTextBox.Text += "TRIAL VERSION. You have " + daysRemaining + " days remaining.";
                }
            }

            SetRegistrationValues(isRegistered);

            try
            {
                Consts.BuildDictionaries();
            }
            catch
            {
                DisableButtons();
                resultsRichTextBox.Text = "Data files are corrupted, please reinstall QuickOdds.\n";
            }

            String[] historyFilenames = Directory.GetFiles("History\\");
            Array.Sort(historyFilenames);
            Array.Reverse(historyFilenames);
            foreach (String filename in historyFilenames)
            {
                FileInfo fileInfo = new FileInfo(filename);
                if (fileInfo.Length < 2048) //if filesize is too small to be valid
                {
                    File.Delete(filename);
                }

                else
                {
                    DateTime filenameDate;
                    String   dateString = filename.Substring(8, 10);

                    if (DateTime.TryParse(dateString, out filenameDate))
                    {
                        if (filenameDate < DateTime.Today.AddDays(Properties.Settings.Default.SaveDays * -1.0))
                        {
                            File.Delete(filename);
                        }
                        else
                        {
                            savedHistoryFileNames.Add(filename);
                            previousSessionsTreeView.Nodes.Add(filenameDate.ToString("d"));
                        }
                    }
                    else
                    {
                        File.Delete(filename);
                    }
                }
            }

            showPlayerNamesCheckBox.Checked        = Properties.Settings.Default.ShowPlayerNames;
            saveSimulationsCheckBox.Checked        = Properties.Settings.Default.SaveSimulations;
            saveDaysTextBox.Text                   = Properties.Settings.Default.SaveDays.ToString();
            showTimeCheckBox.Checked               = Properties.Settings.Default.ShowTime;
            gameComboBox.SelectedIndex             = Properties.Settings.Default.GameType;
            historyShowPlayerNamesCheckBox.Checked = Properties.Settings.Default.HistoryShowPlayerNames;
        }