Skip to content

Commit

Permalink
1. Better bug check
Browse files Browse the repository at this point in the history
2. Fixed a mistyped ID-Item mapping
  • Loading branch information
secXsQuared1995 authored and secXsQuared1995 committed Oct 19, 2016
1 parent ea35663 commit a684fea
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 84 deletions.
8 changes: 4 additions & 4 deletions MHSEC-G/MHSEC-G.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Global
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{80372081-2D34-424B-93E2-89D5815BF3E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80372081-2D34-424B-93E2-89D5815BF3E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80372081-2D34-424B-93E2-89D5815BF3E8}.Debug|x86.ActiveCfg = Debug|x86
{80372081-2D34-424B-93E2-89D5815BF3E8}.Debug|x86.Build.0 = Debug|x86
{80372081-2D34-424B-93E2-89D5815BF3E8}.Debug|x86.ActiveCfg = Debug|Any CPU
{80372081-2D34-424B-93E2-89D5815BF3E8}.Debug|x86.Build.0 = Debug|Any CPU
{80372081-2D34-424B-93E2-89D5815BF3E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80372081-2D34-424B-93E2-89D5815BF3E8}.Release|Any CPU.Build.0 = Release|Any CPU
{80372081-2D34-424B-93E2-89D5815BF3E8}.Release|x86.ActiveCfg = Release|x86
{80372081-2D34-424B-93E2-89D5815BF3E8}.Release|x86.Build.0 = Release|x86
{80372081-2D34-424B-93E2-89D5815BF3E8}.Release|x86.ActiveCfg = Release|Any CPU
{80372081-2D34-424B-93E2-89D5815BF3E8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
40 changes: 40 additions & 0 deletions MHSEC-G/MHSEC-G/BugCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Windows;

namespace MHSEC_G
{
public class BugCheck
{
public enum ErrorCode
{
// Facility Model
MODEL_INVALID_FILE_SIZE = 0x1,
MODEL_READ_BYTE_OVERFLOW = 0x2,
MODEL_WRITE_BYTE_OVERFLOW = 0x3,
MODEL_READ_UINT16_OVERFLOW = 0x4,
MODEL_WRITE_UINT16_OVERFLOW = 0x5,
MODEL_READ_UINT32_OVERFLOW = 0x6,
MODEL_WRITE_UINT32_OVERFLOW = 0x7,
MODEL_WRITE_UNICODE_OVERFLOW = 0x8,

// Faciltiy ViewModel
VIEWMODEL_NULL_SAVE = 0x11,

// Faciltiy Item
ITEM_NO_CORRESPONDENCE = 0x21,
ITEM_MAPPING_CORRUPTED = 0x22,
ITEM_MAPPING_DNE = 0x23,

}
public static void bug_check(ErrorCode error_code, string error_message)
{
MessageBoxResult result = MessageBox.Show("Error code: 0x" + ((int)error_code).ToString("X4") + "\nMessage: " + error_message, "MHSEC-G Bug Check",
MessageBoxButton.OK, MessageBoxImage.Stop);
if (result == MessageBoxResult.OK)
{
System.Windows.Forms.Application.Exit();
}
Environment.Exit((int)error_code);
}
}
}
25 changes: 22 additions & 3 deletions MHSEC-G/MHSEC-G/Item.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using MHSEC_G.Annotations;
Expand All @@ -14,12 +15,18 @@ public class Item : INotifyPropertyChanged
private static readonly uint OFFSETR_ITEM_ID = 0x0;
private static readonly uint OFFSETR_ITEM_COUNT = 0x2;
private static readonly uint OFFSETA_ITEM_BOX_END = 0x2EE7;
public static readonly uint OFFSETA_FIRST_KEY_ITEM = 0x17B0;

private static readonly string ID_MAPPING_FILE_NAME = "idmap.txt";
private static readonly Dictionary<uint, uint> OFFSET_ID_MAPPING = new Dictionary<uint, uint>();
private static readonly Dictionary<uint, string> OFFSET_NAME_MAPPING = new Dictionary<uint, string>();

private readonly uint _offset;

public uint offset
{
get { return _offset; }
}
private readonly Model _model;

public string name
Expand Down Expand Up @@ -91,7 +98,9 @@ public static List<Item> read_all_items(Model model)
{
if (item_id != OFFSET_ID_MAPPING[offset])
{
throw new SystemException("Item offset and ID do not correspond.");
// correct to the correct id
// BugCheck.bug_check(BugCheck.ErrorCode.ITEM_NO_CORRESPONDENCE, "Item offset " + offset.ToString("X") + " and item ID " + item_id.ToString("X") + " do not correspond.");
Model.write_uint16_le(model.save_file, offset + OFFSETR_ITEM_ID, OFFSET_ID_MAPPING[offset]);
}
}
}
Expand All @@ -104,15 +113,25 @@ public static List<Item> read_all_items(Model model)
public static void read_item_mappings()
{
string line;
System.IO.StreamReader file = new System.IO.StreamReader(ID_MAPPING_FILE_NAME);
System.IO.StreamReader file = null;

if (!File.Exists(ID_MAPPING_FILE_NAME))
{
BugCheck.bug_check(BugCheck.ErrorCode.ITEM_MAPPING_DNE,
"Cannot find mapping file " + ID_MAPPING_FILE_NAME);
}

file = new System.IO.StreamReader(ID_MAPPING_FILE_NAME);
while ((line = file.ReadLine()) != null)
{
if (line.Length == 0)
continue;

string[] eachline = line.Split('\t');
if (eachline.Length != 3)
throw new SystemException("Item mapping file is corrupted.");
{
BugCheck.bug_check(BugCheck.ErrorCode.ITEM_MAPPING_CORRUPTED, "Invalid mapping file line:\n" + line);
}

OFFSET_ID_MAPPING.Add(UInt32.Parse(eachline[0], System.Globalization.NumberStyles.HexNumber), UInt32.Parse(eachline[1], System.Globalization.NumberStyles.HexNumber));
OFFSET_NAME_MAPPING.Add(UInt32.Parse(eachline[0], System.Globalization.NumberStyles.HexNumber), eachline[2]);
Expand Down
56 changes: 38 additions & 18 deletions MHSEC-G/MHSEC-G/MHSEC-G.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -23,6 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -32,24 +49,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand All @@ -72,6 +72,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="BugCheck.cs" />
<Compile Include="Character.cs" />
<Compile Include="EggFragment.cs" />
<Compile Include="Item.cs" />
Expand Down Expand Up @@ -109,7 +110,9 @@
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand All @@ -119,6 +122,23 @@
<ItemGroup>
<Resource Include="idmap.txt" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.4.5">
<Visible>False</Visible>
<ProductName>Windows Installer 4.5</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
10 changes: 5 additions & 5 deletions MHSEC-G/MHSEC-G/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class MainWindow : Window
{
private ViewModel view_model;
private readonly byte[] dummy_data = new byte[Model.SAVE_FILE_SIZE];
private const string Version = "0.1";
private const string Version = "0.11";
public MainWindow()
{
InitializeComponent();
Expand All @@ -35,10 +35,10 @@ private void button_load_click(object sender, RoutedEventArgs e)
if (buffer.Length != Model.SAVE_FILE_SIZE)
{
System.Windows.Forms.MessageBox.Show(
"Wrong save file size! Expected: " + Model.SAVE_FILE_SIZE + ".",
"Wrong save file size! Expected: " + Model.SAVE_FILE_SIZE + " Got: " + buffer.Length,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
MessageBoxIcon.Error);
}
else
{
Expand All @@ -64,7 +64,7 @@ private void button_item_all_Click(object sender, RoutedEventArgs e)
List<Item> items = view_model.items;
for (uint i = 0; i < items.Count; i++)
{
if (items.ElementAt((int)i).id == 1227)
if (items.ElementAt((int)i).offset >= Item.OFFSETA_FIRST_KEY_ITEM)
break;

items.ElementAt((int)i).count = 986;
Expand All @@ -76,7 +76,7 @@ private void button_item_existing_Click(object sender, RoutedEventArgs e)
List<Item> items = view_model.items;
for (uint i = 0; i < items.Count; i++)
{
if (items.ElementAt((int) i).id == 1227)
if (items.ElementAt((int)i).offset >= Item.OFFSETA_FIRST_KEY_ITEM)
break;

if (items.ElementAt((int)i).count != 0)
Expand Down
28 changes: 20 additions & 8 deletions MHSEC-G/MHSEC-G/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public byte[] save_file

public Model(byte[] save_file)
{
if(save_file.Length != SAVE_FILE_SIZE)
throw new SystemException("Invalid save file size.");
if (save_file.Length != SAVE_FILE_SIZE)
{
BugCheck.bug_check(BugCheck.ErrorCode.MODEL_INVALID_FILE_SIZE, "Invalid file size.\nExpected: " + SAVE_FILE_SIZE + " Got: " + save_file.Length);
}
_save_file = save_file;
}

Expand All @@ -33,37 +35,45 @@ public static void write_byte(byte[]arr, uint offset, uint val)
arr[offset] = (byte)(val&0xFF);
else
{
throw new SystemException("Buffer overflowed - Offset " + offset);
BugCheck.bug_check(BugCheck.ErrorCode.MODEL_WRITE_BYTE_OVERFLOW, "Buffer overflowed.\nBound " + arr.Length + " Offset " + offset);
}
}

public static uint byte_to_uint16_le(byte[] arr, uint offset)
{
if (arr.Length < offset + 2)
throw new SystemException("Buffer overflowed - Offset " + offset);
{
BugCheck.bug_check(BugCheck.ErrorCode.MODEL_READ_UINT16_OVERFLOW, "Buffer overflowed.\nBound " + arr.Length + " Offset " + offset);
}
return byte_to_uint(arr[offset]) | (byte_to_uint(arr[offset + 1]) << 8);
}

public static void write_uint16_le(byte[] arr, uint offset, uint val)
{
if (arr.Length < offset + 2)
throw new SystemException("Buffer overflowed - Offset " + offset);
{
BugCheck.bug_check(BugCheck.ErrorCode.MODEL_WRITE_UINT16_OVERFLOW, "Buffer overflowed.\nBound " + arr.Length + " Offset " + offset);
}
arr[offset] = (byte) (val & 0xFF);
arr[offset + 1] = (byte) ((val >> 8) & 0xFF);
}

public static uint byte_to_uint32_le(byte[] arr, uint offset)
{
if (arr.Length < offset + 4)
throw new SystemException("Buffer overflowed - Offset " + offset);
{
BugCheck.bug_check(BugCheck.ErrorCode.MODEL_READ_UINT32_OVERFLOW, "Buffer overflowed.\nBound " + arr.Length + " Offset " + offset);
}
return byte_to_uint(arr[offset]) | (byte_to_uint(arr[offset + 1]) << 8) |
(byte_to_uint(arr[offset + 2]) << 16) | (byte_to_uint(arr[offset + 3]) << 24);
}

public static void write_uint32_le(byte[] arr, uint offset, uint val)
{
if (arr.Length < offset + 4)
throw new SystemException("Buffer overflowed - Offset " + offset);
{
BugCheck.bug_check(BugCheck.ErrorCode.MODEL_WRITE_UINT32_OVERFLOW, "Buffer overflowed.\nBound " + arr.Length + " Offset " + offset);
}
arr[offset] = (byte) (val & 0xFF);
arr[offset + 1] = (byte) ((val >> 8) & 0xFF);
arr[offset + 2] = (byte) ((val >> 16) & 0xFF);
Expand Down Expand Up @@ -95,7 +105,9 @@ public static string read_unicode_string(byte[] arr, uint offset, uint max_char)
public static void write_unicode_string(byte[] arr, uint offset, string str, uint length)
{
if (length < str.Length || arr.Length < offset + length)
throw new SystemException("Unicode string write - potential buffer overflow.");
{
BugCheck.bug_check(BugCheck.ErrorCode.MODEL_WRITE_UNICODE_OVERFLOW, "Buffer overflowed.\nBound " + arr.Length + " Offset " + offset);
}

Array.Clear(arr, (int) offset, (int)length*2);
for (uint i = 0; i < str.Length; i ++)
Expand Down
4 changes: 2 additions & 2 deletions MHSEC-G/MHSEC-G/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ public string full_name

public string name
{
get { return Model.read_unicode_string(_model.save_file, _offset + OFFSETR_MONSTER_NAME, 10); }
get { return Model.read_unicode_string(_model.save_file, _offset + OFFSETR_MONSTER_NAME, LIMIT_MONSTER_NAME); }
set
{
if (value.Length <= 10 && value.Length > 0)
{
Model.write_unicode_string(_model.save_file, _offset + OFFSETR_MONSTER_NAME, value, 10);
Model.write_unicode_string(_model.save_file, _offset + OFFSETR_MONSTER_NAME, value, LIMIT_MONSTER_NAME);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions MHSEC-G/MHSEC-G/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MHSEC-G")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("Monster Hunter Stories Save Editor")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MHSEC-G")]
Expand Down Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("0.1.1.0")]
[assembly: AssemblyFileVersion("0.1.1.0")]
Loading

0 comments on commit a684fea

Please sign in to comment.