Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Wincent01 committed Jan 21, 2020
2 parents b76324c + 1be7a45 commit a1a3e6b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 24 deletions.
27 changes: 4 additions & 23 deletions InfectedRose.Utilities/Checksum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@ namespace InfectedRose.Utilities
{
public static class Checksum
{
private struct ChecksumLayer
{
internal uint Id { get; set; }

internal uint Layer { get; set; }

internal uint Revision { get; set; }

public void Apply(ref uint value, ref uint total)
{
foreach (var reference in new[] {Id, Layer, Revision})
{
value += reference >> 16; // Apply reference

total += value; // Add to total

value += reference & ushort.MaxValue; // Make ushort

total += value; // Add to total
}
}
}

/// <summary>
/// Calculate the checksum for a LEGO Universe zone
/// </summary>
Expand Down Expand Up @@ -126,6 +103,10 @@ public static uint Generate(string zone)
var lower = (ushort) ((value & ushort.MaxValue) + (value >> 16));
var upper = (ushort) ((total & ushort.MaxValue) + (total >> 16));

//
// The checksum has two parts, one for the 'total', and one for the 'value', these combine to form a 32bit value
//

return (uint) (upper << 16 | lower);
}

Expand Down
25 changes: 25 additions & 0 deletions InfectedRose.Utilities/ChecksumLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace InfectedRose.Utilities
{
internal struct ChecksumLayer
{
internal uint Id { get; set; }

internal uint Layer { get; set; }

internal uint Revision { get; set; }

internal void Apply(ref uint value, ref uint total)
{
foreach (var reference in new[] {Id, Layer, Revision})
{
value += reference >> 16; // Apply reference

total += value; // Add to total

value += reference & ushort.MaxValue; // Make ushort

total += value; // Add to total
}
}
}
}
68 changes: 68 additions & 0 deletions InfectedRose.Utilities/Extensions/LuzFileExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using InfectedRose.Luz;
using InfectedRose.Lvl;

namespace InfectedRose.Utilities
{
public static class LuzFileExtensions
{
public static uint GenerateChecksum(this LuzFile @this, List<LvlFile> scenes)
{
if (@this.Scenes.Length != scenes.Count)
{
throw new ArgumentOutOfRangeException(nameof(scenes), "The count of scenes has to equal the count of scenes in this luz file.");
}

uint value = ushort.MaxValue; // For checksum calculations
uint total = ushort.MaxValue; // Sum of all changes applied to value

var zoneLayer = new ChecksumLayer
{
Id = uint.MaxValue,
Layer = default,
Revision = @this.RevisionNumber
};

zoneLayer.Apply(ref value, ref total);

for (var index = 0; index < scenes.Count; index++)
{
var scene = scenes[index];
var lvl = @this.Scenes[index];

//
// Get revision
//

var revision = scene.LevelInfo?.RevisionNumber ?? scene.OldLevelHeader.Revision;

//
// Get layer
//

var sceneLayer = new ChecksumLayer
{
Id = lvl.SceneId,
Layer = lvl.LayerId,
Revision = revision
};

sceneLayer.Apply(ref value, ref total);
}

//
// Get final checksum
//

var lower = (ushort) ((value & ushort.MaxValue) + (value >> 16));
var upper = (ushort) ((total & ushort.MaxValue) + (total >> 16));

//
// The checksum has two parts, one for the 'total', and one for the 'value', these combine to form a 32bit value
//

return (uint) (upper << 16 | lower);
}
}
}
2 changes: 1 addition & 1 deletion InfectedRose.Utilities/InfectedRose.Utilities.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

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

0 comments on commit a1a3e6b

Please sign in to comment.