Skip to content

Commit

Permalink
Merge pull request #4 from xparadoxical/master
Browse files Browse the repository at this point in the history
Fix a NRE and add pvz installation path detection
  • Loading branch information
IntelOrca authored Sep 21, 2020
2 parents 5ecd56a + a056ab1 commit 4329080
Show file tree
Hide file tree
Showing 8 changed files with 414 additions and 374 deletions.
614 changes: 307 additions & 307 deletions IntelOrca.PvZTools/MainForm.Designer.cs

Large diffs are not rendered by default.

32 changes: 23 additions & 9 deletions IntelOrca.PvZTools/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using IntelOrca.PvZTools.Properties;
#nullable enable
using IntelOrca.PvZTools.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -9,14 +10,24 @@ namespace IntelOrca.PvZTools
{
public partial class MainForm : Form
{
PvZProcess mProcess = new PvZProcess();
ZombieSpawner mSpawner;
PvZProcess mProcess = null!;
ZombieSpawner? mSpawner;
ZombieProbabilityForm mZPF = new ZombieProbabilityForm();

Random mRand = new Random();

public MainForm()
{
try
{
mProcess = new PvZProcess();
}
catch (NullReferenceException e)
{
MessageBox.Show(e.Message + " If you have one, please report this as a bug at https://github.com/IntelOrca/PVZTools/issues.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}

InitializeComponent();

this.Icon = Resources.orca_icon;
Expand Down Expand Up @@ -44,14 +55,19 @@ private void SpawnZombie()
else
zombieType = GetZombieType();

mSpawner.Spawn(zombieType, GetRandomRowFromSelection(zombieType));
mSpawner!.Spawn(zombieType, GetRandomRowFromSelection(zombieType));
}

private void btnSpawnZombie_Click(object sender, EventArgs e)
{
SpawnZombie();
}

private void chkActive_CheckedChanged(object sender, EventArgs e)
{
tmrSpawn.Enabled = chkActive.Checked;
}

private int GetZombieType()
{
return cmbZombieType.SelectedIndex;
Expand Down Expand Up @@ -79,9 +95,6 @@ private int GetRandomRowFromSelection(int type = -1)

private void tmrSpawn_Tick(object sender, EventArgs e)
{
if (!chkActive.Checked)
return;

SpawnZombie();
}

Expand Down Expand Up @@ -131,11 +144,12 @@ private void UpdateStatus()

if (!mProcess.OpenProcess()) {
lblStatus.Text = "Status: Unable to connect...";
btnSpawnZombie.Enabled = chkActive.Enabled = chkActive.Checked = false;
} else {
lblStatus.Text = "Status: Running...";
btnSpawnZombie.Enabled = chkActive.Enabled = true;
mSpawner = new ZombieSpawner(mProcess);
mSpawner.Activate();
//Cheats.NoSunDecrease(mMemory);
}
}

Expand All @@ -160,7 +174,7 @@ private void lblAuthor_MouseLeave(object sender, EventArgs e)

private void lblAuthor_MouseDown(object sender, MouseEventArgs e)
{
Process.Start("http://tedtycoon.co.uk");
Process.Start("http://intelorca.co.uk");
}
}
}
4 changes: 2 additions & 2 deletions IntelOrca.PvZTools/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions IntelOrca.PvZTools/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 42 additions & 14 deletions IntelOrca.PvZTools/PvZProcess.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
using System;
using System.Collections.Generic;
#nullable enable
using System;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using IntelOrca.MemPatch;
using System.IO;

namespace IntelOrca.PvZTools
{
class PvZProcess : AppProcess
{
public override string StartupPath
{
get { return @"C:\Program Files (x86)\PopCap Games\Plants vs. Zombies"; }
}
internal PvZProcess()
{
(string? path, bool goty) = GetInstallLocation(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", wow6432node: true);
if (path is null)
{
(path, goty) = GetInstallLocation(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", wow6432node: false);
if (path is null) throw new NullReferenceException("Could not find a Plants vs. Zombies installation.");
}

public override string ExecutablePath
{
get { return @"C:\Program Files (x86)\PopCap Games\Plants vs. Zombies\PlantsVsZombies.exe"; }
GOTY = goty;
StartupPath = path;
}

public override string ProcessName
{
get { return @"popcapgame1"; }
}
readonly string[] displayNames = new string[] { "Plants vs. Zombies", "Plants vs. Zombies SDR" };
readonly string gotyName = "Plants vs. Zombies: Game of the Year";

private (string? path, bool goty) GetInstallLocation(string regkey, bool wow6432node)
{
using RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, wow6432node ? RegistryView.Registry32 : RegistryView.Registry64);
using RegistryKey installed = hklm.OpenSubKey(regkey);
foreach (string id in installed.GetSubKeyNames())
{
using RegistryKey program = installed.OpenSubKey(id);
string displayName = (string)program.GetValue("DisplayName");

if (displayNames.Contains(displayName))
return ((string)program.GetValue("InstallLocation") ?? Path.GetDirectoryName((string)program.GetValue("DisplayIcon")), false);
else if (gotyName == (string)program.GetValue("DisplayName"))
return ((string)program.GetValue("InstallLocation") ?? Path.GetDirectoryName((string)program.GetValue("DisplayIcon")), true);
}

return (null, false);
}

public override string StartupPath { get; }

public override string ExecutablePath => StartupPath + '\\' + ProcessName;

public override string ProcessName => GOTY ? "popcapgame1" : "PlantsVsZombies";

public bool GOTY { get; private set; }
}
}
42 changes: 24 additions & 18 deletions IntelOrca.PvZTools/PvZTools.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
Expand All @@ -10,31 +11,35 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>IntelOrca.PvZTools</RootNamespace>
<AssemblyName>PvZTools</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<PropertyGroup>
<ApplicationIcon>orca_icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject>IntelOrca.PvZTools.Program</StartupObject>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>orca_icon.ico</ApplicationIcon>
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -89,6 +94,7 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
33 changes: 11 additions & 22 deletions IntelOrca.PvZTools/PvZTools.sln
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PvZTools", "PvZTools.csproj", "{5E62F60C-F31E-44D3-82BF-8256F0297011}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntelOrca.MemPatch", "..\IntelOrca.MemPatch\IntelOrca.MemPatch.csproj", "{28741DD2-F467-446E-A363-6814DAEA23F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Debug|Any CPU.ActiveCfg = Debug|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Debug|Mixed Platforms.Build.0 = Debug|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Debug|x86.ActiveCfg = Debug|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Debug|x86.Build.0 = Debug|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Release|Any CPU.ActiveCfg = Release|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Release|Mixed Platforms.ActiveCfg = Release|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Release|Mixed Platforms.Build.0 = Release|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Release|x86.ActiveCfg = Release|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Release|x86.Build.0 = Release|x86
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E62F60C-F31E-44D3-82BF-8256F0297011}.Release|Any CPU.Build.0 = Release|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Debug|x86.ActiveCfg = Debug|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Release|Any CPU.Build.0 = Release|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{28741DD2-F467-446E-A363-6814DAEA23F6}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A546983C-15DA-41C6-BB8F-6B179E44471D}
EndGlobalSection
EndGlobal
3 changes: 3 additions & 0 deletions IntelOrca.PvZTools/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup></configuration>

0 comments on commit 4329080

Please sign in to comment.