Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
- Upgraded to C# 7.3
- Upgraded to .NET 4.7
- Fix bug with New NPC code type generation
- Add win rate to magazine code generator
  • Loading branch information
Cuyler36 committed Sep 1, 2018
1 parent ff5f2fd commit 5e3c394
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 215 deletions.
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Animal_Forest_e__Password_Library</RootNamespace>
<AssemblyName>Animal Forest e+ Password Library</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -20,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -28,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
56 changes: 31 additions & 25 deletions Animal Forest e+ Password Library/Common.cs
@@ -1,7 +1,21 @@
using System;
using System.Linq;

// ReSharper disable once CheckNamespace
namespace PasswordLibrary
{
public enum CodeType
{
Famicom = 0, // NES
NPC = 1, // Original NPC Code
Card_E = 2, // NOTE: This is a stubbed method (just returns 4)
Magazine = 3, // Contest?
User = 4, // Player-to-Player
Card_E_Mini = 5, // Only one data strip?
New_NPC = 6, // Using the new password system?
Monument = 7 // Town Decorations (from Object Delivery Service, see: https://www.nintendo.co.jp/ngc/gaej/obje/)
}

public static class Common
{
// Character List
Expand Down Expand Up @@ -377,24 +391,25 @@ public static void mMpswd_bit_shift(ref byte[] Data, int Shift)
mMpswd_prime_number[Data[0xC]], mMpswd_select_idx_table[ByteTable]);
}

public static bool mMpswd_new_password_zuru_check(int SavedZuru, int CodeType, string Reciepiant, string TownName, string Sender, ushort ItemId, int NpcCode, int Unknown)
public static bool mMpswd_new_password_zuru_check(int checksum, int CodeType, string Reciepiant, string TownName, string Sender, ushort ItemId, int NpcCode, int Unknown)
{
bool Invalid = true;
if (CodeType != 2 && CodeType < 8)
{
int Zuru = 0;
Zuru += GetStringByteValue(Reciepiant);
Zuru += GetStringByteValue(TownName);
Zuru += GetStringByteValue(Sender);
Zuru += ItemId;
if (CodeType == 2 || CodeType >= 8) return true;

if ((Zuru & 0xF) == SavedZuru && mMpswd_check_default_hit_rate(CodeType, NpcCode) && mMpswd_check_default_npc_code(CodeType, NpcCode, Unknown))
{
Invalid = false;
}
var invalid = true;

var calculatedChecksum = 0;
calculatedChecksum += GetStringByteValue(Reciepiant);
calculatedChecksum += GetStringByteValue(TownName);
calculatedChecksum += GetStringByteValue(Sender);
calculatedChecksum += ItemId;

if ((calculatedChecksum & 0xF) == checksum && mMpswd_check_default_hit_rate(CodeType, NpcCode) &&
mMpswd_check_default_npc_code(CodeType, NpcCode, Unknown))
{
invalid = false;
}

return Invalid;
return invalid;
}

private static bool mMpswd_check_default_hit_rate(int CodeType, int CodeCheck)
Expand Down Expand Up @@ -490,16 +505,7 @@ public static byte[] StringToAFByteArray(string Input)
return Output;
}

private static int GetStringByteValue(string Input)
{
byte[] Data = StringToAFByteArray(Input);
int Value = 0;
for (int i = 0; i < Data.Length; i++)
{
Value += Data[i];
}

return Value;
}
private static int GetStringByteValue(string input) =>
StringToAFByteArray(input).Aggregate(0, (current, t) => current + t);
}
}
1 change: 1 addition & 0 deletions Animal Forest e+ Password Library/Decoder.cs
@@ -1,6 +1,7 @@
using System;
using System.Linq;

// ReSharper disable once CheckNamespace
namespace PasswordLibrary.Decoder
{
public class Decoder
Expand Down
62 changes: 38 additions & 24 deletions Animal Forest e+ Password Library/Encoder.cs
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;

// ReSharper disable once CheckNamespace
namespace PasswordLibrary.Encoder
{
public static class Encoder
{
public static byte[] mMpswd_make_passcode(int CodeType, int Unknown1, string RecipientTown, string Recipient, string Sender, ushort ItemId, int Unknown2)
public static byte[] mMpswd_make_passcode(CodeType CodeType, int HitRateIndex, string RecipientTown, string Recipient, string Sender, ushort ItemId, int ExtraData)
{
byte[] Output = new byte[24];

Expand All @@ -15,37 +15,39 @@ public static byte[] mMpswd_make_passcode(int CodeType, int Unknown1, string Rec

switch (CodeType)
{
case 0:
case 4:
case 5:
case CodeType.Famicom:
case CodeType.User:
case CodeType.Card_E_Mini:
r0 = 4;
Unknown2 = 0;
ExtraData = 0;
r31 = 0xFF;
break;
case 1:
case 6:
Unknown2 &= 3;
case CodeType.NPC:
case CodeType.New_NPC:
ExtraData &= 3;
r0 = 4;
break;
case 3:
r0 = Unknown1 & 7;
Unknown2 = 0;
case CodeType.Magazine:
// Valid indices are 0 - 4. Hit rates are: { 80.0f, 60.0f, 30.0f, 0.0f, 100.0f }. The hit is RNG based and the player "wins" if hit < hitRate.
r0 = HitRateIndex & 7;
ExtraData = 0;
r31 = 0xFF;
break;
case 7:
Unknown2 &= 0xFF;
case CodeType.Monument:
ExtraData &= 0xFF;
r0 = 4;
r31 = 0xFF;
break;
default:
r0 = 4;
CodeType = 4;
CodeType = CodeType.User;
break;
}

int Byte0 = (CodeType << 5) & 0xE0;
int Byte0 = ((int) CodeType << 5) & 0xE0;
Byte0 |= (r0 << 2);
Output[0] = (byte)Byte0;
Output[1] = (byte)Unknown2;
Output[1] = (byte)ExtraData;
Output[2] = (byte)r31;

// Copy Recipient Name
Expand Down Expand Up @@ -297,9 +299,9 @@ public static void mMpswd_chg_common_font_code(ref byte[] Password)
}

#if DEBUG
public static string Encode(int CodeType, int Unknown1, string RecipientTown, string Recipient, string Sender, ushort ItemId, int Unknown2)
public static string Encode(CodeType CodeType, int HitRateIndex, string RecipientTown, string Recipient, string Sender, ushort ItemId, int ExtraData)
{
byte[] PasswordData = mMpswd_make_passcode(CodeType, Unknown1, RecipientTown, Recipient, Sender, ItemId, Unknown2);
byte[] PasswordData = mMpswd_make_passcode(CodeType, HitRateIndex, RecipientTown, Recipient, Sender, ItemId, ExtraData);
PrintByteBuffer("mMpswd_make_passcode", PasswordData);
mMpswd_substitution_cipher(ref PasswordData);
PrintByteBuffer("mMpswd_substitution_cipher", PasswordData);
Expand All @@ -318,7 +320,7 @@ public static string Encode(int CodeType, int Unknown1, string RecipientTown, st
byte[] Password = mMpswd_chg_6bits_code(PasswordData);
PrintByteBuffer("mMpswd_chg_6bits_code", Password);
mMpswd_chg_common_font_code(ref Password);
PrintByteBuffer("mMpswd_chg_common_font_code", PasswordData);
PrintByteBuffer("mMpswd_chg_common_font_code", Password);

// Construct password string
string PasswordString = "";
Expand All @@ -334,12 +336,24 @@ public static string Encode(int CodeType, int Unknown1, string RecipientTown, st
return PasswordString;
}

private static void PrintByteBuffer(string stage, IEnumerable<byte> buffer) =>
Console.WriteLine((stage + ":").PadRight(32) + buffer.Aggregate("", (current, b) => current + b.ToString("X2") + " "));
private static void PrintByteBuffer(string stage, byte[] buffer)
{
Console.Write((stage + ":").PadRight(32));
for (var i = 0; i < buffer.Length; i++)
{
if (i > 0 && i % 8 == 0)
{
Console.Write(("\n").PadRight(32) + " ");
}
Console.Write(buffer[i].ToString("X2"));
}
Console.Write("\n\n");
}
#else
public static string Encode(int CodeType, int Unknown1, string RecipientTown, string Recipient, string Sender, ushort ItemId, int Unknown2)
public static string Encode(CodeType CodeType, int HitRateIndex, string RecipientTown, string Recipient, string Sender, ushort ItemId, int ExtraData)
{
byte[] PasswordData = mMpswd_make_passcode(CodeType, Unknown1, RecipientTown, Recipient, Sender, ItemId, Unknown2);
byte[] PasswordData =
mMpswd_make_passcode(CodeType, HitRateIndex, RecipientTown, Recipient, Sender, ItemId, ExtraData);
mMpswd_substitution_cipher(ref PasswordData);
Common.mMpswd_transposition_cipher(ref PasswordData, true, 0);
mMpswd_bit_shuffle(ref PasswordData, 0);
Expand Down
Expand Up @@ -8,13 +8,14 @@
<OutputType>WinExe</OutputType>
<RootNamespace>Animal_Forest_e__Password_Tool</RootNamespace>
<AssemblyName>Animal Forest e+ Password Tool</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -25,6 +26,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -34,6 +36,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>AFe+ e+.ico</ApplicationIcon>
Expand Down
6 changes: 3 additions & 3 deletions Animal Forest e+ Password Tool/App.config
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/>
</startup>
</configuration>
</configuration>
8 changes: 8 additions & 0 deletions Animal Forest e+ Password Tool/MainWindow.xaml
Expand Up @@ -54,6 +54,14 @@
<TextBox x:Name="YAcreTextBox" MaxLength="1" HorizontalAlignment="Left" Height="22" Margin="87,213,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="20" FontSize="14" Visibility="Hidden"/>
<Label x:Name="Label7" Content="X-Acre:" HorizontalAlignment="Left" Margin="128,205,0,0" VerticalAlignment="Top" FontSize="18" Visibility="Hidden"/>
<TextBox x:Name="XAcreTextBox" MaxLength="1" HorizontalAlignment="Left" Height="22" Margin="205,213,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="20" FontSize="14" Visibility="Hidden"/>
<Label x:Name="HitRateLabel" Content="Win Percentage:" HorizontalAlignment="Left" Margin="10,205,0,0" VerticalAlignment="Top" FontSize="18" Visibility="Hidden"/>
<ComboBox x:Name="HitRateComboBox" Visibility="Hidden" FontSize="14" HorizontalAlignment="Left" Margin="156,210,0,0" VerticalAlignment="Top" Width="70" IsEditable="False" SelectedIndex="0">
<ComboBoxItem Content="0%"/>
<ComboBoxItem Content="30%"/>
<ComboBoxItem Content="60%"/>
<ComboBoxItem Content="80%"/>
<ComboBoxItem Content="100%"/>
</ComboBox>
</Grid>
</TabItem>
<TabItem Header="Decoder">
Expand Down

0 comments on commit 5e3c394

Please sign in to comment.