Some code cleanup.

lua-bytecode
Icedream 2014-10-22 19:48:30 +02:00
parent 5582debcee
commit f67b95007a
13 changed files with 266 additions and 251 deletions

View File

@ -1,5 +1,4 @@

using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@ -12,14 +11,64 @@ namespace GarrysMod.AddonCreator
{
public class Addon
{
private static readonly byte[] FormatIdent = Encoding.ASCII.GetBytes("GMAD");
private const byte FormatVersion = 3;
private const uint AppID = 4000;
private const uint CompressionSignature = 0xbeefcace;
private static readonly byte[] FormatIdent = Encoding.ASCII.GetBytes("GMAD");
/// <summary>
/// Initializes a new instance of <see cref="Addon" />
/// </summary>
public Addon()
{
Files = new Dictionary<string, AddonFileInfo>();
RequiredContent = new List<string>();
Version = 1;
}
/// <summary>
/// Returns the timestamp of when the addon was built. This data is retrieved from full imports and for new (unsaved)
/// addons this is 0.
/// </summary>
public ulong BuildTimestamp { get; private set; }
/// <summary>
/// The name of this addon.
/// </summary>
public string Title { get; set; }
/// <summary>
/// The author of this addon.
/// </summary>
public string Author { get; set; }
/// <summary>
/// A description of this addon.
/// </summary>
public string Description { get; set; }
/// <summary>
/// This addon's version.
/// </summary>
public int Version { get; set; }
/// <summary>
/// The files to include in the addon.
/// </summary>
public Dictionary<string, AddonFileInfo> Files { get; private set; }
/// <summary>
/// Currently unused.
/// </summary>
public ulong SteamID { get; set; }
/// <summary>
/// Content that needs to exist in order to run this addon.
/// </summary>
public List<string> RequiredContent { get; private set; }
public static void CreateFromFolder()
{
}
/// <summary>
@ -113,7 +162,8 @@ namespace GarrysMod.AddonCreator
// avoid duplicates
if (newFilesList.ContainsKey(filePath))
{
throw new IOException("Found duplicate file path in addon file. Contact the addon creator and tell him to build a new proper addon file.");
throw new IOException(
"Found duplicate file path in addon file. Contact the addon creator and tell him to build a new proper addon file.");
}
newFilesList.Add(filePath, new Tuple<long, int>(fileSize, fileHash));
@ -150,11 +200,6 @@ namespace GarrysMod.AddonCreator
}
}
/// <summary>
/// Returns the timestamp of when the addon was built. This data is retrieved from full imports and for new (unsaved) addons this is 0.
/// </summary>
public ulong BuildTimestamp { get; private set; }
/// <summary>
/// Exports this addon into a GMA file.
/// </summary>
@ -178,7 +223,8 @@ namespace GarrysMod.AddonCreator
var files = Files;
// Check for errors and ignores in addon.json
var addonJson = JsonConvert.DeserializeObject<AddonJson>(Encoding.UTF8.GetString(Files["addon.json"].GetContents()));
var addonJson =
JsonConvert.DeserializeObject<AddonJson>(Encoding.UTF8.GetString(Files["addon.json"].GetContents()));
addonJson.CheckForErrors();
addonJson.RemoveIgnoredFiles(ref files);
@ -228,7 +274,8 @@ namespace GarrysMod.AddonCreator
// Required content
if (RequiredContent.Count > byte.MaxValue)
{
throw new IndexOutOfRangeException("Required content count must not exceed " + byte.MaxValue + " entries.");
throw new IndexOutOfRangeException("Required content count must not exceed " + byte.MaxValue +
" entries.");
}
sw.Write((byte) RequiredContent.Count);
foreach (var content in RequiredContent)
@ -245,7 +292,8 @@ namespace GarrysMod.AddonCreator
// File list
if (Files.Count > uint.MaxValue)
{
throw new IndexOutOfRangeException("Number of addon files must not exceed " + uint.MaxValue + " elements.");
throw new IndexOutOfRangeException("Number of addon files must not exceed " + uint.MaxValue +
" elements.");
}
uint fileNum = 0;
foreach (var file in resultingFiles)
@ -278,50 +326,5 @@ namespace GarrysMod.AddonCreator
}
}
}
/// <summary>
/// The name of this addon.
/// </summary>
public string Title { get; set; }
/// <summary>
/// The author of this addon.
/// </summary>
public string Author { get; set; }
/// <summary>
/// A description of this addon.
/// </summary>
public string Description { get; set; }
/// <summary>
/// This addon's version.
/// </summary>
public int Version { get; set; }
/// <summary>
/// The files to include in the addon.
/// </summary>
public Dictionary<string, AddonFileInfo> Files { get; private set; }
/// <summary>
/// Currently unused.
/// </summary>
public ulong SteamID { get; set; }
/// <summary>
/// Content that needs to exist in order to run this addon.
/// </summary>
public List<string> RequiredContent { get; private set; }
/// <summary>
/// Initializes a new instance of <see cref="Addon"/>
/// </summary>
public Addon()
{
Files = new Dictionary<string, AddonFileInfo>();
RequiredContent = new List<string>();
Version = 1;
}
}
}

View File

@ -4,12 +4,18 @@ namespace GarrysMod.AddonCreator
{
public abstract class AddonFileInfo
{
private long? _size;
private int? _hash;
private long? _size;
public virtual long Size { get { return _size.HasValue ? _size.Value : (_size = GetContents().Length).Value; } }
public virtual long Size
{
get { return _size.HasValue ? _size.Value : (_size = GetContents().Length).Value; }
}
public virtual int Crc32Hash { get { return _hash.HasValue ? _hash.Value : (_hash = ParallelCRC.Compute(GetContents())).Value; } }
public virtual int Crc32Hash
{
get { return _hash.HasValue ? _hash.Value : (_hash = ParallelCRC.Compute(GetContents())).Value; }
}
public abstract byte[] GetContents();
}

View File

@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace GarrysMod.AddonCreator

View File

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace GarrysMod.AddonCreator
@ -72,7 +69,7 @@ namespace GarrysMod.AddonCreator
private static Regex[] _regularExpressions;
static void ConvertWhitelist()
private static void ConvertWhitelist()
{
if (_regularExpressions != null)
return;

View File

@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace GarrysMod.AddonCreator
{
static class Extensions
internal static class Extensions
{
public static Regex WildcardRegex(this string pattern)
{

View File

@ -1,5 +1,4 @@
using System;
using System.IO;
namespace CRC32
{
@ -7,8 +6,10 @@ namespace CRC32
{
private const uint kCrcPoly = 0xEDB88320;
private const uint kInitial = 0xFFFFFFFF;
private static readonly uint[] Table;
private const uint CRC_NUM_TABLES = 8;
private static readonly uint[] Table;
private uint value;
static OptimizedCRC()
{
@ -31,13 +32,16 @@ namespace CRC32
}
}
private uint value;
public OptimizedCRC()
{
Init();
}
public int Value
{
get { return (int) ~value; }
}
/// <summary>
/// Reset CRC
/// </summary>
@ -46,11 +50,6 @@ namespace CRC32
value = kInitial;
}
public int Value
{
get { return (int)~value; }
}
public void UpdateByte(byte b)
{
value = (value >> 8) ^ Table[(byte) value ^ b];
@ -61,7 +60,7 @@ namespace CRC32
new ArraySegment<byte>(data, offset, count); // check arguments
if (count == 0) return;
var table = OptimizedCRC.Table; // important for performance!
var table = Table; // important for performance!
uint crc = value;
@ -80,8 +79,14 @@ namespace CRC32
while (offset != to)
{
crc ^= (uint)(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) + (data[offset + 3] << 24));
uint high = (uint)(data[offset + 4] + (data[offset + 5] << 8) + (data[offset + 6] << 16) + (data[offset + 7] << 24));
crc ^=
(uint)
(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) +
(data[offset + 3] << 24));
var high =
(uint)
(data[offset + 4] + (data[offset + 5] << 8) + (data[offset + 6] << 16) +
(data[offset + 7] << 24));
offset += 8;
crc = table[(byte) crc + 0x700]
@ -101,19 +106,19 @@ namespace CRC32
value = crc;
}
static public int Compute(byte[] data, int offset, int size)
public static int Compute(byte[] data, int offset, int size)
{
var crc = new OptimizedCRC();
crc.Update(data, offset, size);
return crc.Value;
}
static public int Compute(byte[] data)
public static int Compute(byte[] data)
{
return Compute(data, 0, data.Length);
}
static public int Compute(ArraySegment<byte> block)
public static int Compute(ArraySegment<byte> block)
{
return Compute(block.Array, block.Offset, block.Count);
}

View File

@ -1,7 +1,5 @@
using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
namespace CRC32
{
@ -9,13 +7,15 @@ namespace CRC32
{
private const uint kCrcPoly = 0xEDB88320;
private const uint kInitial = 0xFFFFFFFF;
private static readonly uint[] Table;
private const int CRC_NUM_TABLES = 8;
private const int ThreadCost = 256 << 10;
private static int ThreadCount = Environment.ProcessorCount;
private static readonly uint[] Table;
private static readonly int ThreadCount = Environment.ProcessorCount;
private uint value;
static ParallelCRC()
{
unchecked
@ -24,7 +24,7 @@ namespace CRC32
int i;
for (i = 0; i < 256; i++)
{
uint r = (uint)i;
var r = (uint) i;
for (int j = 0; j < 8; j++)
r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
Table[i] = r;
@ -37,13 +37,16 @@ namespace CRC32
}
}
private uint value;
public ParallelCRC()
{
Init();
}
public int Value
{
get { return (int) ~value; }
}
/// <summary>
/// Reset CRC
/// </summary>
@ -52,11 +55,6 @@ namespace CRC32
value = kInitial;
}
public int Value
{
get { return (int)~value; }
}
public void UpdateByte(byte b)
{
value = (value >> 8) ^ Table[(byte) value ^ b];
@ -110,7 +108,7 @@ namespace CRC32
if (count < 0) throw new ArgumentOutOfRangeException("count");
if (count == 0) return crc;
var table = ParallelCRC.Table;
var table = Table;
for (; (offset & 7) != 0 && count != 0; count--)
crc = (crc >> 8) ^ table[(byte) crc ^ data[offset++]];
@ -123,8 +121,14 @@ namespace CRC32
while (offset != to)
{
crc ^= (uint)(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) + (data[offset + 3] << 24));
uint high = (uint)(data[offset + 4] + (data[offset + 5] << 8) + (data[offset + 6] << 16) + (data[offset + 7] << 24));
crc ^=
(uint)
(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) +
(data[offset + 3] << 24));
var high =
(uint)
(data[offset + 4] + (data[offset + 5] << 8) + (data[offset + 6] << 16) +
(data[offset + 7] << 24));
offset += 8;
crc = table[(byte) crc + 0x700]
@ -144,19 +148,19 @@ namespace CRC32
return crc;
}
static public int Compute(byte[] data, int offset, int count)
public static int Compute(byte[] data, int offset, int count)
{
var crc = new ParallelCRC();
crc.Update(data, offset, count);
return crc.Value;
}
static public int Compute(byte[] data)
public static int Compute(byte[] data)
{
return Compute(data, 0, data.Length);
}
static public int Compute(ArraySegment<byte> block)
public static int Compute(ArraySegment<byte> block)
{
return Compute(block.Array, block.Offset, block.Count);
}
@ -168,6 +172,9 @@ namespace CRC32
* Taken from DotNetZip project sources (http://dotnetzip.codeplex.com/)
*/
private static uint[] even_cache;
private static uint[] odd_cache;
/// <summary>
/// This function is thread-safe!
/// </summary>
@ -187,7 +194,7 @@ namespace CRC32
crc1 = ~crc1;
crc2 = ~crc2;
uint len2 = (uint)length2;
var len2 = (uint) length2;
// apply len2 zeros to crc1 (first square will put the operator for one
// zero byte, eight zero bits, in even)
@ -211,9 +218,6 @@ namespace CRC32
return ~crc1;
}
private static uint[] even_cache = null;
private static uint[] odd_cache;
private static void Prepare_even_odd_Cache()
{
var even = new uint[32]; // even-power-of-two zeros operator
@ -264,11 +268,11 @@ namespace CRC32
#endregion Combining
class Job
private class Job
{
private readonly ParallelCRC accumulator;
private readonly Job waitForJob;
private ArraySegment<byte> data;
private Job waitForJob;
private ParallelCRC accumulator;
private ManualResetEventSlim finished;
@ -277,7 +281,7 @@ namespace CRC32
this.data = data;
this.accumulator = accumulator;
this.waitForJob = waitForJob;
this.finished = new ManualResetEventSlim(false);
finished = new ManualResetEventSlim(false);
}
public void Do(object arg)
@ -301,5 +305,4 @@ namespace CRC32
}
}
}
}

View File

@ -4,13 +4,13 @@ namespace GarrysMod.AddonCreator
{
public class PhysicalAddonFileInfo : AddonFileInfo
{
private readonly FileInfo _fi;
public PhysicalAddonFileInfo(string path)
{
_fi = new FileInfo(path);
}
private FileInfo _fi;
public override long Size
{
get { return _fi.Length; }

View File

@ -19,7 +19,8 @@ namespace GarrysMod.AddonCreator
// recursively add files
foreach (var file in folder.EnumerateFiles("*", SearchOption.AllDirectories))
{
var relpath = MakeRelativePath(folder.FullName, file.FullName).Replace(Path.DirectorySeparatorChar, '/');
var relpath =
MakeRelativePath(folder.FullName, file.FullName).Replace(Path.DirectorySeparatorChar, '/');
Console.WriteLine("Adding: {0}", relpath);
addon.Files.Add(relpath, new PhysicalAddonFileInfo(file.FullName));
@ -45,7 +46,8 @@ namespace GarrysMod.AddonCreator
foreach (var file in addon.Files)
{
var relpath = file.Key;
var targetFile = new FileInfo(Path.Combine(folder.FullName, relpath.Replace('/', Path.DirectorySeparatorChar)));
var targetFile =
new FileInfo(Path.Combine(folder.FullName, relpath.Replace('/', Path.DirectorySeparatorChar)));
Console.WriteLine("Extracting: {0}", relpath);

View File

@ -1,10 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die mit einer Assembly verknüpft sind.
[assembly: AssemblyTitle("GarrysMod.AddonCreator")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
@ -17,9 +17,11 @@ using System.Runtime.InteropServices;
// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
[assembly: ComVisible(false)]
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("bb585862-950d-415b-b518-eb9c7e3d50f3")]
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
@ -32,5 +34,6 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -5,9 +5,9 @@ namespace GarrysMod.AddonCreator
{
public class SegmentedAddonFileInfo : AddonFileInfo
{
private Stream _stream;
private long _pos;
private long _len;
private readonly long _len;
private readonly long _pos;
private readonly Stream _stream;
private int _hash;
public SegmentedAddonFileInfo(Stream stream, long pos, long len, int fileHash)

View File

@ -1,5 +1,4 @@
using System;
using System.IO;
namespace CRC32
{
@ -9,6 +8,8 @@ namespace CRC32
private const uint kInitial = 0xFFFFFFFF;
private static readonly uint[] Table;
private uint value;
static TraditionalCRC()
{
unchecked
@ -24,13 +25,16 @@ namespace CRC32
}
}
private uint value;
public TraditionalCRC()
{
Init();
}
public int Value
{
get { return (int) ~value; }
}
/// <summary>
/// Reset CRC
/// </summary>
@ -39,11 +43,6 @@ namespace CRC32
value = kInitial;
}
public int Value
{
get { return (int)~value; }
}
public void UpdateByte(byte b)
{
value = (value >> 8) ^ Table[(byte) value ^ b];
@ -56,19 +55,19 @@ namespace CRC32
value = (value >> 8) ^ Table[(byte) value ^ data[offset++]];
}
static public int Compute(byte[] data, int offset, int count)
public static int Compute(byte[] data, int offset, int count)
{
var crc = new TraditionalCRC();
crc.Update(data, offset, count);
return crc.Value;
}
static public int Compute(byte[] data)
public static int Compute(byte[] data)
{
return Compute(data, 0, data.Length);
}
static public int Compute(ArraySegment<byte> block)
public static int Compute(ArraySegment<byte> block)
{
return Compute(block.Array, block.Offset, block.Count);
}

View File

@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net40" />
</packages>