Skip to content

Commit

Permalink
Refactoring to comply with some CA warnings, suppressed others
Browse files Browse the repository at this point in the history
  • Loading branch information
z16 committed Feb 25, 2015
1 parent b43bb5d commit d0bb5fb
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 207 deletions.
61 changes: 35 additions & 26 deletions BitmapParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,50 @@ internal static class BitmapParser
internal static Bitmap Parse(BinaryReader reader, ImageHeader header, bool ignoreAlpha = false)
{
var Result = new Bitmap(header.Width, header.Height, PixelFormat.Format32bppArgb);
var Raw = Result.LockBits(new Rectangle(0, 0, Result.Width, Result.Height), ImageLockMode.WriteOnly, Result.PixelFormat);
try
{
var Raw = Result.LockBits(new Rectangle(0, 0, Result.Width, Result.Height), ImageLockMode.WriteOnly, Result.PixelFormat);

var PixelCount = header.Height * header.Width;
var PixelCount = header.Height * header.Width;

unsafe
{
var Buffer = (byte*)Raw.Scan0;
unsafe
{
var Buffer = (byte*)Raw.Scan0;

Color[] Palette = null;
byte[] BitFields = null;
bool EightCount = header.BitCount == 8;
if (EightCount)
{ // 8-bit, with palette
Palette = new Color[256];
for (var i = 0; i < 256; ++i)
{
Palette[i] = ReadColor(reader, 32);
Color[] Palette = null;
byte[] BitFields = null;
bool EightCount = header.BitCount == 8;
if (EightCount)
{ // 8-bit, with palette
Palette = new Color[256];
for (var i = 0; i < 256; ++i)
{
Palette[i] = ReadColor(reader, 32);
}

BitFields = reader.ReadBytes(PixelCount);
}

BitFields = reader.ReadBytes(PixelCount);
for (var Pixel = 0; Pixel < PixelCount; ++Pixel)
{
var Color = EightCount ? Palette[BitFields[Pixel]] : ReadColor(reader, header.BitCount);
Buffer[4 * Pixel + 0] = Color.B;
Buffer[4 * Pixel + 1] = Color.G;
Buffer[4 * Pixel + 2] = Color.R;
Buffer[4 * Pixel + 3] = ignoreAlpha ? (byte)255 : Color.A;
}
}

for (var Pixel = 0; Pixel < PixelCount; ++Pixel)
{
var Color = EightCount ? Palette[BitFields[Pixel]] : ReadColor(reader, header.BitCount);
Buffer[4 * Pixel + 0] = Color.B;
Buffer[4 * Pixel + 1] = Color.G;
Buffer[4 * Pixel + 2] = Color.R;
Buffer[4 * Pixel + 3] = ignoreAlpha ? (byte)255 : Color.A;
}
}
Result.UnlockBits(Raw);

Result.UnlockBits(Raw);
Result.RotateFlip(RotateFlipType.RotateNoneFlipY);
}
catch
{
Result.Dispose();
throw;
}

Result.RotateFlip(RotateFlipType.RotateNoneFlipY);
return Result;
}

Expand Down
2 changes: 2 additions & 0 deletions DatParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace ResourceExtractor

internal class DatParser
{
private DatParser() { }

public static dynamic[] Parse(Stream stream, Dictionary<int, string> fields)
{
var format = stream.Read<ulong>();
Expand Down
6 changes: 5 additions & 1 deletion DialogParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ internal static dynamic[] Parse(Stream stream, string key)
{
data[i] ^= 0x80;
}
var table = new MemoryStream(data).ReadArray<int>((int)header.TableSize);
int[] table;
using (var datastream = new MemoryStream(data))
{
table = datastream.ReadArray<int>((int)header.TableSize);
}

dynamic objects = new ModelObject[header.TableSize];

Expand Down
49 changes: 29 additions & 20 deletions DxtParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace ResourceExtractor
{
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;

internal static class DxtParser
Expand All @@ -32,35 +33,43 @@ internal static Bitmap Parse(BinaryReader reader, ImageHeader header, bool ignor
{
var Format = (header.Type == ImageType.DXT2 || header.Type == ImageType.DXT4) ? PixelFormat.Format32bppPArgb : PixelFormat.Format32bppArgb;
var Result = new Bitmap(header.Width, header.Height, Format);
var Raw = Result.LockBits(new Rectangle(0, 0, Result.Width, Result.Height), ImageLockMode.WriteOnly, Result.PixelFormat);

var TexelBlockCount = (header.Width * header.Height) / (4 * 4);
unsafe
try
{
var Buffer = (byte*)Raw.Scan0;
var Raw = Result.LockBits(new Rectangle(0, 0, Result.Width, Result.Height), ImageLockMode.WriteOnly, Result.PixelFormat);

for (var Texel = 0; Texel < TexelBlockCount; ++Texel)
var TexelBlockCount = (header.Width * header.Height) / (4 * 4);
unsafe
{
var TexelBlock = ReadTexelBlock(reader, header.Type);
var PixelOffsetX = 4 * (Texel % (header.Width / 4));
var PixelOffsetY = 4 * (Texel / (header.Width / 4)) * (Raw.Stride / 4);
var Buffer = (byte*)Raw.Scan0;

var Index = 4 * (PixelOffsetX + PixelOffsetY);
for (var Y = 0; Y < 4; ++Y, Index += Raw.Stride - 16)
for (var Texel = 0; Texel < TexelBlockCount; ++Texel)
{
for (var X = 0; X < 4; ++X, Index += 4)
var TexelBlock = ReadTexelBlock(reader, header.Type);
var PixelOffsetX = 4 * (Texel % (header.Width / 4));
var PixelOffsetY = 4 * (Texel / (header.Width / 4)) * (Raw.Stride / 4);

var Index = 4 * (PixelOffsetX + PixelOffsetY);
for (var Y = 0; Y < 4; ++Y, Index += Raw.Stride - 16)
{
var Lookup = X + 4 * Y;
Buffer[Index + 0] = TexelBlock[Lookup].B;
Buffer[Index + 1] = TexelBlock[Lookup].G;
Buffer[Index + 2] = TexelBlock[Lookup].R;
Buffer[Index + 3] = ignoreAlpha ? (byte)255 : TexelBlock[Lookup].A;
for (var X = 0; X < 4; ++X, Index += 4)
{
var Lookup = X + 4 * Y;
Buffer[Index + 0] = TexelBlock[Lookup].B;
Buffer[Index + 1] = TexelBlock[Lookup].G;
Buffer[Index + 2] = TexelBlock[Lookup].R;
Buffer[Index + 3] = ignoreAlpha ? (byte)255 : TexelBlock[Lookup].A;
}
}
}
}
}

Result.UnlockBits(Raw);
Result.UnlockBits(Raw);
}
catch
{
Result.Dispose();
throw;
}

return Result;
}
Expand All @@ -75,7 +84,7 @@ private static Color[] ReadTexelBlock(BinaryReader reader, ImageType type)
}
else if (type != ImageType.DXT1)
{
throw new InvalidDataException(string.Format("Format {0} not recognized.", type));
throw new InvalidDataException(string.Format(CultureInfo.InvariantCulture, "Format {0} not recognized.", type));
}

ushort C0 = reader.ReadUInt16();
Expand Down
1 change: 1 addition & 0 deletions Fixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private static void SetDynamic(object obj, string key, object value)
callsite.Target(callsite, obj, value);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
private static object Convert(string value, string type)
{
switch (type)
Expand Down
5 changes: 4 additions & 1 deletion ImageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ namespace ResourceExtractor
{
using System;
using System.Drawing;
using System.Globalization;
using System.IO;

internal class ImageParser
{
private ImageParser() { }

internal static Bitmap Parse(Stream stream, bool ignoreAlpha = false)
{
Bitmap Result = null;
Expand All @@ -53,7 +56,7 @@ internal static Bitmap Parse(Stream stream, bool ignoreAlpha = false)
Result = BitmapParser.Parse(Reader, Header, ignoreAlpha);
break;
default:
throw new InvalidDataException(string.Format("Image type {0} not supported: {1}", Header.Type, Flag));
throw new InvalidDataException(string.Format(CultureInfo.InvariantCulture, "Image type {0} not supported: {1}", Header.Type, Flag));
}
}

Expand Down
3 changes: 2 additions & 1 deletion MapParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace ResourceExtractor
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Web.Script.Serialization;

Expand Down Expand Up @@ -61,7 +62,7 @@ public static void Extract()
using (FileStream Stream = File.Open(Program.GetPath(MapPair.Value), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var Image = ImageParser.Parse(Stream, true);
using (FileStream OutFile = File.Open(string.Format("resources/maps/{0}_{1}.png", Zone, Map), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
using (FileStream OutFile = File.Open(string.Format(CultureInfo.InvariantCulture, "resources/maps/{0}_{1}.png", Zone, Map), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
Image.Save(OutFile, ImageFormat.Png);
}
Expand Down
11 changes: 11 additions & 0 deletions ModelObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ internal class ModelObject : DynamicObject, ISerializable, IEnumerable<KeyValueP
{
private IDictionary<string, object> map = new Dictionary<string, object>();

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return map.TryGetValue(binder.Name, out result);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (value != null)
Expand All @@ -51,6 +53,7 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
return true;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")]
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
result = null;
Expand All @@ -66,6 +69,7 @@ public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out ob
return false;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")]
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
{
if (indexes.Length == 1)
Expand All @@ -88,12 +92,14 @@ public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object
return false;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
public override bool TryDeleteMember(DeleteMemberBinder binder)
{
map.Remove(binder.Name);
return true;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1")]
public override bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes)
{
if (indexes.Length == 1)
Expand Down Expand Up @@ -144,6 +150,11 @@ public bool ContainsKey(string key)

void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException("info");
}

foreach (var pair in map)
{
info.AddValue(pair.Key, pair.Value);
Expand Down
14 changes: 5 additions & 9 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ namespace ResourceExtractor
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Win32;
using ResourceExtractor.Serializers.Lua;
using Serializers.Lua;

internal class Program
{
Expand Down Expand Up @@ -68,11 +67,6 @@ internal class Program
{0, "ja"},
}},
}},
//{"augments", new Dictionary<ushort, Dictionary<int, string>> {
// {0xD98C, new Dictionary<int, string> {
// {0, "en"},
// }},
//}},
{"auto_translates", new Dictionary<ushort, Dictionary<int, string>> {
{0xD971, new Dictionary<int, string> {
{0, "en"},
Expand Down Expand Up @@ -403,7 +397,6 @@ private static void PostProcess()
}
}


// Shift monster abilities up by 0x100
foreach (var monster_ability in model.monster_abilities)
{
Expand Down Expand Up @@ -644,7 +637,10 @@ private static void LoadMainData()
try
{
#endif
ResourceParser.ParseMainStream(File.OpenRead(GetPath(0x0051)));
using (var file = File.OpenRead(GetPath(0x0051)))
{
ResourceParser.ParseMainStream(file);
}
#if !DEBUG
}
catch
Expand Down
Loading

0 comments on commit d0bb5fb

Please sign in to comment.