Skip to content

Commit

Permalink
ignore empty groups, v2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeG621 committed Jun 6, 2021
1 parent 0d47198 commit 79834d1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
33 changes: 20 additions & 13 deletions DatFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Idmr.ImageFormat.Dat, Allows editing capability of LA *.DAT Image files
* Copyright (C) 2009-2019 Michael Gaisser (mjgaisser@gmail.com)
* Copyright (C) 2009-2021 Michael Gaisser (mjgaisser@gmail.com)
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the Mozilla Public License; either version 2.0 of the
Expand All @@ -13,10 +13,12 @@
* If a copy of the MPL (MPL.txt) was not distributed with this file,
* you can obtain one at http://mozilla.org/MPL/2.0/.
*
* VERSION: 2.2
* VERSION: 2.3
*/

/* CHANGE LOG
* v2.3, 210606
* [UPD] Ignores empty Groups
* v2.2, 190922
* [UPD] tweaked some comments
* [NEW] UsedHeight, UsedWidth
Expand Down Expand Up @@ -82,7 +84,7 @@ public void Save()
{
if (NumberOfGroups < 1) throw new InvalidDataException("No Groups defined");
for (int g = 0; g < NumberOfGroups; g++) if (Groups[g].ID < 0) throw new InvalidDataException("Not all Groups have been initialized");
_updateGroupHeaders();
updateGroupHeaders();
for (int g = 0; g < NumberOfGroups; g++)
for (int s = 0; s < Groups[g].NumberOfSubs; s++) Groups[g].Subs[s]._headerUpdate();
if (File.Exists(_filePath)) File.Copy(_filePath, tempFile); // create backup
Expand Down Expand Up @@ -154,20 +156,25 @@ public void DecodeFile(byte[] rawData)
for (int i = 0; i < numberOfGroups; i++)
{
ArrayFunctions.TrimArray(rawData, offset, header);
Groups[i] = new Group(header);
if (BitConverter.ToInt16(header, 2) > 0) Groups[i] = new Group(header); // only read if there's Subs
offset += Group._headerLength;
}
// Dat.Groups
for (int i = 0; i < numberOfGroups; i++)
{ // Group.Subs
for (int j = 0; j < Groups[i].NumberOfSubs; j++)
for (int i = 0; i < Groups.Count;)
{ // Group.Subs
if (Groups[i].ID > 0)
{
int length = BitConverter.ToInt32(rawData, offset + 0xE);
byte[] sub = new byte[length + Sub._subHeaderLength];
ArrayFunctions.TrimArray(rawData, offset, sub);
Groups[i].Subs[j] = new Sub(sub);
offset += sub.Length;
for (int j = 0; j < Groups[i].NumberOfSubs; j++)
{
int length = BitConverter.ToInt32(rawData, offset + 0xE);
byte[] sub = new byte[length + Sub._subHeaderLength];
ArrayFunctions.TrimArray(rawData, offset, sub);
Groups[i].Subs[j] = new Sub(sub);
offset += sub.Length;
}
i++;
}
else Groups.Remove(i);
}
}
#endregion public methods
Expand Down Expand Up @@ -213,7 +220,7 @@ public short UsedWidth
}
#endregion public properties

void _updateGroupHeaders()
void updateGroupHeaders()
{
for (int i = 0; i < Groups.Count; i++)
{
Expand Down
8 changes: 5 additions & 3 deletions GroupCollection.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
* Idmr.ImageFormat.Dat, Allows editing capability of LucasArts *.DAT Image files
* Copyright (C) 2009-2019 Michael Gaisser (mjgaisser@gmail.com)
* Copyright (C) 2009-2021 Michael Gaisser (mjgaisser@gmail.com)
* Licensed under the MPL v2.0 or later
*
* Full notice in DatFile.cs
* VERSION: 2.2
* VERSION: 2.3
*/

/* CHANGE LOG
* v2.3, 210606
* [UPD] changed the increment on intial IDs
* v2.2, 190922
* [UPD] added a quantity check to ctor
* [UPD] tweaked comments
Expand Down Expand Up @@ -52,7 +54,7 @@ public GroupCollection(int quantity)
_itemLimit = 256;
if (quantity < 1 || quantity > _itemLimit) throw new ArgumentOutOfRangeException("DAT.Group quantity must be positive and less than " + _itemLimit);
_items = new List<Group>(_itemLimit);
for (int i = 0, id = -2000; i < quantity; i++, id += 100) Add(new Group((short)id));
for (int i = 0, id = -2000; i < quantity; i++, id += 1) Add(new Group((short)id));
AutoSort = true;
}
/// <summary>Creates a Collection and populates it with the provided <see cref="Group">Groups</see></summary>
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
[assembly: AssemblyCulture("")]

// Major.Minor.Build.Revision
[assembly: AssemblyVersion("2.2.0.*")]
[assembly: AssemblyVersion("2.3.0.*")]
11 changes: 8 additions & 3 deletions Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ Idmr.ImageFormat.Dat.dll
========================

Author: Michael Gaisser (mjgaisser@gmail.com)
Version: 2.2
Date: 2019.09.22
Version: 2.3
Date: 2021.06.06

Library for reading LucasArts *.DAT backdrop files

==========
Version History

v2.3 - 06 Jun 2021
- Empty Groups are now ignored during read
- (GroupCollection) Changed the increment on intial ID creation so it's always negative
- (SubCollection) Added more details to exception message

v2.2 - 22 Sep 2019
- Added Format 25 capability
- Removed size limits from images. Masks still need to match the image size.
Expand Down Expand Up @@ -54,7 +59,7 @@ Programmer's reference can be found in help/Idmr.ImageFormat.Dat.chm
==========
Copyright Information

Copyright (C) Michael Gaisser, 2009-2014
Copyright (C) Michael Gaisser, 2009-2021
This library file and related files are licensed under the Mozilla Public License
v2.0 or later. See MPL.txt for further details.

Expand Down
12 changes: 7 additions & 5 deletions SubCollection.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
* Idmr.ImageFormat.Dat, Allows editing capability of LucasArts *.DAT Image files
* Copyright (C) 2009-2019 Michael Gaisser (mjgaisser@gmail.com)
* Copyright (C) 2009-2021 Michael Gaisser (mjgaisser@gmail.com)
* Licensed under the MPL v2.0 or later
*
* Full notice in DatFile.cs
* VERSION: 2.2
* VERSION: 2.3
*/

/* CHANGE LOG
* v2.3, 210606
* [UPD] Added more details to exception message
* v2.2, 190922
* [UPD] tweaked some of the comments
* [UPD] added some quantity checks
Expand Down Expand Up @@ -58,7 +60,7 @@ public SubCollection(short groupID)
public SubCollection(int quantity, short groupID)
{
_itemLimit = 256;
if (quantity < 1 || quantity > _itemLimit) throw new ArgumentOutOfRangeException("DAT.Sub quantity must be positive and less than or equal to" + _itemLimit);
if (quantity < 1 || quantity > _itemLimit) throw new ArgumentOutOfRangeException("DAT.Sub quantity must be positive and less than or equal to " + _itemLimit + ". Group ID: " + groupID);
_items = new List<Sub>(_itemLimit);
_groupID = groupID;
for (short i = 0; i < quantity; i++) Add(new Sub(_groupID, i));
Expand All @@ -72,7 +74,7 @@ public SubCollection(Sub[] subs)
{
_itemLimit = 256;
if (subs.Length > _itemLimit)
throw new ArgumentOutOfRangeException("DAT.Sub quantity must be less than or equal to" + _itemLimit);
throw new ArgumentOutOfRangeException("DAT.Sub quantity must be less than or equal to " + _itemLimit + ". Group ID: " + subs[0].GroupID);
_items = new List<Sub>(_itemLimit);
for (int i = 0; i < subs.Length; i++) Add(subs[i]);
GroupID = subs[0].GroupID;
Expand All @@ -87,7 +89,7 @@ public SubCollection(short groupID, System.Drawing.Bitmap[] images)
{
_itemLimit = 256;
if (images.Length > _itemLimit)
throw new ArgumentOutOfRangeException("DAT.Sub quantity must be less than or equal to" + _itemLimit);
throw new ArgumentOutOfRangeException("DAT.Sub quantity must be less than or equal to " + _itemLimit + ". Group ID: " + groupID);
_items = new List<Sub>(_itemLimit);
_groupID = groupID;
for(short i = 0; i < images.Length; i++) Add(new Sub(groupID, i, images[i]));
Expand Down

0 comments on commit 79834d1

Please sign in to comment.