Skip to content

Commit 63e476c

Browse files
committed
Add magic decoder
1 parent 80a0b72 commit 63e476c

File tree

5 files changed

+49
-33
lines changed

5 files changed

+49
-33
lines changed

Ray2StringEd/FixManager.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.IO;
43
using System.Text;
4+
using R2MagicDecoder;
55

66
namespace Ray2StringEd
77
{
@@ -15,19 +15,21 @@ public FixManager(string path, int offset = 0x7051F4)
1515

1616
private string Path { get; }
1717
private int Offset { get; }
18+
private byte[] FixBytes { get; set; }
1819

1920
public IEnumerable<FixString> ReadFix()
2021
{
21-
using (FileStream file = new FileStream(Path, FileMode.Open, FileAccess.Read))
22+
FixBytes = MagicDecoder.DecodeBytes(File.ReadAllBytes(Path));
23+
using (MemoryStream fix = new MemoryStream(FixBytes))
2224
{
23-
file.Seek(Offset, SeekOrigin.Begin);
25+
fix.Seek(Offset, SeekOrigin.Begin);
2426

25-
using (BinaryReader reader = new BinaryReader(file))
27+
using (BinaryReader reader = new BinaryReader(fix))
2628
{
27-
long fileLength = file.Length;
29+
long fixLength = fix.Length;
2830
long position;
2931

30-
while ((position = reader.BaseStream.Position) != fileLength)
32+
while ((position = reader.BaseStream.Position) != fixLength)
3133
{
3234
int length = reader.ReadInt32();
3335

@@ -45,29 +47,37 @@ public IEnumerable<FixString> ReadFix()
4547

4648
public void WriteFix(IEnumerable<FixString> strings)
4749
{
48-
using (FileStream file = new FileStream(Path, FileMode.Open, FileAccess.Write))
50+
using (MemoryStream fix = new MemoryStream(FixBytes))
51+
using (BinaryWriter writer = new BinaryWriter(fix))
4952
{
50-
using (BinaryWriter writer = new BinaryWriter(file))
53+
foreach (FixString s in strings)
5154
{
52-
foreach (FixString s in strings)
53-
{
54-
file.Seek(s.Offset, SeekOrigin.Begin);
55+
fix.Seek(s.Offset, SeekOrigin.Begin);
5556

56-
string text = s.Text + '\0';
57-
byte[] bytes = Encoding.GetEncoding(1252).GetBytes(text);
57+
string text = s.Text + '\0';
58+
byte[] bytes = Encoding.GetEncoding(1252).GetBytes(text);
5859

59-
writer.Write(s.DwordLength);
60-
writer.Write(bytes);
60+
writer.Write(s.DwordLength);
61+
writer.Write(bytes);
6162

62-
int freeLength = s.ByteLength - bytes.Length - 4;
63-
if (freeLength > 0)
64-
{
65-
byte[] freeBytes = new byte[freeLength];
66-
writer.Write(freeBytes);
67-
}
63+
int freeLength = s.ByteLength - bytes.Length - 4;
64+
if (freeLength > 0)
65+
{
66+
byte[] freeBytes = new byte[freeLength];
67+
writer.Write(freeBytes);
6868
}
6969
}
7070
}
71+
72+
File.WriteAllBytes(Path, MagicDecoder.DecodeBytes(FixBytes));
73+
}
74+
75+
public void BackupFix()
76+
{
77+
string bakPath = Path + ".bak";
78+
79+
if (!File.Exists(bakPath))
80+
File.Copy(Path, bakPath);
7181
}
7282
}
7383
}

Ray2StringEd/MainViewModel.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public MainViewModel()
1717
WriteFixCommand = new RelayCommand(WriteFix);
1818
}
1919

20+
private FixManager Manager { get; set; }
21+
2022
public ObservableCollection<FixString> FixStrings { get; set; }
2123
public string FixPath { get; set; }
2224
public FixString SelectedItem { get; set; }
@@ -25,7 +27,7 @@ public MainViewModel()
2527
public ICommand ReadFixCommand { get; }
2628
public ICommand WriteFixCommand { get; }
2729

28-
public void ChooseFile()
30+
private void ChooseFile()
2931
{
3032
OpenFileDialog dialog = new OpenFileDialog();
3133
dialog.ShowDialog();
@@ -34,20 +36,20 @@ public void ChooseFile()
3436
FixPath = dialog.FileName;
3537
}
3638

37-
public void ReadFix()
39+
private void ReadFix()
3840
{
3941
if (!File.Exists(FixPath)) return;
4042

41-
FixManager fix = new FixManager(FixPath);
42-
FixStrings = new ObservableCollection<FixString>(fix.ReadFix());
43+
Manager = new FixManager(FixPath);
44+
FixStrings = new ObservableCollection<FixString>(Manager.ReadFix());
4345
}
4446

45-
public void WriteFix()
47+
private void WriteFix()
4648
{
4749
if (!File.Exists(FixPath)) return;
4850

49-
FixManager fix = new FixManager(FixPath);
50-
fix.WriteFix(FixStrings);
51+
Manager.BackupFix();
52+
Manager.WriteFix(FixStrings);
5153

5254
MessageBox.Show($"Written {FixStrings.Count} strings to {FixPath}.");
5355
}

Ray2StringEd/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Reflection;
2-
using System.Resources;
3-
using System.Runtime.CompilerServices;
42
using System.Runtime.InteropServices;
53
using System.Windows;
64

@@ -51,5 +49,4 @@
5149
// You can specify all the values or you can default the Build and Revision Numbers
5250
// by using the '*' as shown below:
5351
// [assembly: AssemblyVersion("1.0.*")]
54-
[assembly: AssemblyVersion("1.0.0.0")]
55-
[assembly: AssemblyFileVersion("1.0.0.0")]
52+
[assembly: AssemblyVersion("1.1.0.0")]

Ray2StringEd/R2MagicDecoder.dll

5 KB
Binary file not shown.

Ray2StringEd/Ray2StringEd.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
<Reference Include="PropertyChanged, Version=3.2.5.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
4949
<HintPath>..\packages\PropertyChanged.Fody.3.2.5\lib\net40\PropertyChanged.dll</HintPath>
5050
</Reference>
51+
<Reference Include="R2MagicDecoder, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
52+
<SpecificVersion>False</SpecificVersion>
53+
<HintPath>.\R2MagicDecoder.dll</HintPath>
54+
</Reference>
5155
<Reference Include="System" />
5256
<Reference Include="System.Data" />
5357
<Reference Include="System.Xml" />
@@ -128,6 +132,9 @@
128132
<ItemGroup>
129133
<Resource Include="RTI.ico" />
130134
</ItemGroup>
135+
<ItemGroup>
136+
<Content Include="R2MagicDecoder.dll" />
137+
</ItemGroup>
131138
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
132139
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
133140
<PropertyGroup>

0 commit comments

Comments
 (0)