-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
4 changed files
with
98 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters