Skip to content

Commit

Permalink
Rename working, unit test added, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hostar committed Feb 6, 2021
1 parent d5bdd5e commit 9749e71
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/MafiaSceneEditor/obj/*
/Scene2Parser/bin/*
/Scene2Parser/obj/*
/Tests/bin/*
/Tests/obj/*
39 changes: 13 additions & 26 deletions MafiaSceneEditor/MainForm2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ComponentFactory.Krypton.Ribbon;
using ComponentFactory.Krypton.Toolkit;
using ComponentFactory.Krypton.Workspace;
using Scene2Parser.DataLayer;
using ScintillaNET;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -125,7 +124,7 @@ public MainForm2()
exportContextMenuItem.Click += ExportDnc;
options.Items.Add(exportContextMenuItem);

KryptonContextMenuItem renameContextMenuItem = new KryptonContextMenuItem("Rename (not implemented yet)") { Enabled = true };
KryptonContextMenuItem renameContextMenuItem = new KryptonContextMenuItem("Rename") { Enabled = true };
renameContextMenuItem.Click += RenameDnc;
options.Items.Add(renameContextMenuItem);

Expand Down Expand Up @@ -1159,34 +1158,14 @@ internal void ExportDnc(object sender, EventArgs e)
}
}

internal void RenameDnc(object sender, EventArgs e)
private void RenameDnc(object sender, EventArgs e)
{
var dnc = currTreeNode.Tag as Dnc;

var newName = KryptonInputBox.Show("Enter new name", "Rename", dnc.Name);
if (newName != dnc.Name)
{
var newnameBytes = Encoding.UTF8.GetBytes(newName);
var pos = Scene2Parser.GetPositionOfNameByID(dnc);

var nameLenDiff = dnc.Name.Length - newName.Length;
byte[] rawdata = new byte[dnc.RawData.Length + nameLenDiff];

//Array.Copy(dnc.RawData, rawdata, dnc.RawData.Length);

//Todo: put name to new array and move the data so they are not rewritten
rawdata = dnc.RawData.Take(pos).Concat(newnameBytes).ToArray();
//File.WriteAllBytes(@"d:\tc\0", dnc.RawData);
//File.WriteAllBytes(@"d:\tc\1", rawdata);
Scene2Parser.RenameDnc(dnc, newName);

rawdata = rawdata.Concat(dnc.RawData.Skip(pos).Skip(dnc.Name.Length).Take(dnc.RawData.Length - dnc.Name.Length - pos)).ToArray();
//File.WriteAllBytes(@"d:\tc\2", rawdata);

rawdata[16] = (byte)(newName.Length + 7);
//File.WriteAllBytes(@"d:\tc\3", rawdata);

dnc.RawData = rawdata;
}
currTreeNode.Text = newName;
}

private void AddRecentFile(string filename)
Expand Down Expand Up @@ -1263,7 +1242,15 @@ private void Scene2LoadInternal(MemoryStream memoryStream)
{
scene2Data = new Scene2Data();

Scene2Parser.LoadScene(memoryStream, ref scene2Data, listBoxOutput.Items);
try
{
Scene2Parser.LoadScene(memoryStream, ref scene2Data, listBoxOutput.Items);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// put into treeview
treeViewMain.Nodes.Clear();
Expand Down
2 changes: 1 addition & 1 deletion Scene2Parser/DataLayer/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;

namespace Scene2Parser.DataLayer
namespace YAMSE.DataLayer
{
public class Header
{
Expand Down
3 changes: 1 addition & 2 deletions Scene2Parser/DataLayer/Scene2Data.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Scene2Parser.DataLayer;
using System;
using System;
using System.Collections.Generic;
using System.Text;

Expand Down
32 changes: 32 additions & 0 deletions Scene2Parser/Scene2Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ public static void LoadScene(MemoryStream inputStream, ref Scene2Data scene2Data

int positionIterator = 0;

int antiInfiniteLoopLastValue = 0;

while (i < tmpBuff.Length)
{
if ((antiInfiniteLoopLastValue == i) && (i != 0))
{
throw new InvalidOperationException($"File corruped near 0x{i:X} position. Last object loaded: {currSection.Dncs.Last().Name}");
}
antiInfiniteLoopLastValue = i;

if (!headerParsed)
{
// parse header
Expand Down Expand Up @@ -496,6 +504,7 @@ public static int GetPositionOfNameByID(Dnc dnc)
case DncType.Wagon:
case DncType.Route:
case DncType.Clock:
case DncType.GhostObject:
return 10;

case DncType.Standard:
Expand Down Expand Up @@ -719,6 +728,29 @@ public static void WriteArrayToDnc(byte[] inputArray, int destinationIndex, Dnc
Array.Copy(inputArray, 0, dnc.RawData, destinationIndex, inputArray.Length);
}

public static void RenameDnc(Dnc dnc, string newName)
{
if (newName != dnc.Name)
{
var newNameBytes = Encoding.UTF8.GetBytes(newName);
var pos = GetPositionOfNameByID(dnc);

var lenDiff = dnc.Name.Length > newName.Length ? dnc.Name.Length - newName.Length : newName.Length - dnc.Name.Length;

// replace binary data
byte[] rawData = dnc.RawData.Take(pos).Concat(newNameBytes).ToArray();

// put new data to object
dnc.RawData = rawData.Concat(dnc.RawData.Skip(pos).Skip(dnc.Name.Length).Take(dnc.RawData.Length - dnc.Name.Length - pos)).ToArray();

// update length data
dnc.RawData[pos - 4] = (byte)(dnc.RawData[pos - 4] + lenDiff);
dnc.RawData[0] = (byte)(dnc.RawData[0] + lenDiff);

dnc.Name = newName;
}
}

public static void CutZerosAtEndOfArray(Dnc dnc)
{
int notZeroIndex = 0;
Expand Down
24 changes: 24 additions & 0 deletions Tests/ParserTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;
using Xunit;
using YAMSE.DataLayer;

namespace Tests
{
public class ParserTest
{
[Fact]
public void RenameTest()
{
string newNameStr = "renamedAAA";
var expectedBytesAfterRename = new byte[] { 0x27, 0x00, 0x00, 0x00, 0x23, 0xAE, 0x11, 0x00, 0x00, 0x00, 0x72, 0x65, 0x6E, 0x61, 0x6D, 0x65, 0x64, 0x41, 0x41, 0x41, 0x00, 0x22, 0xAE, 0x0A, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0xAE, 0x06, 0x00, 0x00, 0x00 };

Dnc testDnc = new Dnc() { Name = "9balik2", dncKind = YAMSE.NodeType.Definition, dncType = DncType.GhostObject };
testDnc.RawData = new byte[] { 0x24,0x00,0x00,0x00,0x23,0xAE,0x0E,0x00,0x00,0x00,0x39,0x62,0x61,0x6C,0x69,0x6B,0x32,0x00,0x22,0xAE,0x0A,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x24,0xAE,0x06,0x00,0x00,0x00 };

YAMSE.Scene2Parser.RenameDnc(testDnc, newNameStr);

Assert.True(testDnc.RawData.SequenceEqual(expectedBytesAfterRename));
}
}
}
26 changes: 26 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MafiaSceneEditor\YAMSE.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions YAMSE.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YAMSE", "MafiaSceneEditor\Y
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Scene2Parser", "Scene2Parser\Scene2Parser.csproj", "{F8BC1077-0434-47C0-B603-A924E8E8D983}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{02401F49-9CD6-458C-B322-3AA323190543}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{F8BC1077-0434-47C0-B603-A924E8E8D983}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8BC1077-0434-47C0-B603-A924E8E8D983}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8BC1077-0434-47C0-B603-A924E8E8D983}.Release|Any CPU.Build.0 = Release|Any CPU
{02401F49-9CD6-458C-B322-3AA323190543}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02401F49-9CD6-458C-B322-3AA323190543}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02401F49-9CD6-458C-B322-3AA323190543}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02401F49-9CD6-458C-B322-3AA323190543}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 9749e71

Please sign in to comment.