merge .FileFormats and .DataStructures
This commit is contained in:
33
OpenRa.FileFormats/AssetInfo.cs
Normal file
33
OpenRa.FileFormats/AssetInfo.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace OpenRa.FileFormats
|
||||
{
|
||||
public class AssetInfo
|
||||
{
|
||||
public readonly string Filename;
|
||||
public readonly string Hash;
|
||||
|
||||
public AssetInfo(string filename, string hash)
|
||||
{
|
||||
Filename = filename;
|
||||
Hash = hash;
|
||||
}
|
||||
|
||||
static string GetHash(string filename)
|
||||
{
|
||||
using (var csp = SHA1.Create())
|
||||
return new string(csp.ComputeHash(File.ReadAllBytes(filename))
|
||||
.SelectMany(a => a.ToString("x2")).ToArray());
|
||||
}
|
||||
|
||||
public static AssetInfo FromLocalFile(string filename)
|
||||
{
|
||||
return new AssetInfo(filename, GetHash(filename));
|
||||
}
|
||||
|
||||
/* todo: perf: cache this */
|
||||
public bool IsPresent() { return File.Exists(Filename) && (Hash == GetHash(Filename)); }
|
||||
}
|
||||
}
|
||||
28
OpenRa.FileFormats/DisposableAction.cs
Normal file
28
OpenRa.FileFormats/DisposableAction.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenRa
|
||||
{
|
||||
public class DisposableAction : IDisposable
|
||||
{
|
||||
public DisposableAction(Action a) { this.a = a; }
|
||||
|
||||
Action a;
|
||||
bool disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed) return;
|
||||
disposed = true;
|
||||
a();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~DisposableAction()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,10 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="IjwFramework, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\Ijw.DirectX\Ijw.Framework\IjwFramework\bin\Release\IjwFramework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
@@ -44,19 +48,23 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AssetInfo.cs" />
|
||||
<Compile Include="AudLoader.cs" />
|
||||
<Compile Include="Blowfish.cs" />
|
||||
<Compile Include="BlowfishKeyProvider.cs" />
|
||||
<Compile Include="DisposableAction.cs" />
|
||||
<Compile Include="Dune2ShpReader.cs" />
|
||||
<Compile Include="Exts.cs" />
|
||||
<Compile Include="FieldLoader.cs" />
|
||||
<Compile Include="FileSystem.cs" />
|
||||
<Compile Include="float2.cs" />
|
||||
<Compile Include="Folder.cs" />
|
||||
<Compile Include="Format2.cs" />
|
||||
<Compile Include="Format40.cs" />
|
||||
<Compile Include="Format80.cs" />
|
||||
<Compile Include="IniFile.cs" />
|
||||
<Compile Include="IniWriter.cs" />
|
||||
<Compile Include="int2.cs" />
|
||||
<Compile Include="IPaletteRemap.cs" />
|
||||
<Compile Include="Map.cs" />
|
||||
<Compile Include="MiniYaml.cs" />
|
||||
@@ -73,14 +81,10 @@
|
||||
<Compile Include="TileReference.cs" />
|
||||
<Compile Include="TileSet.cs" />
|
||||
<Compile Include="TreeReference.cs" />
|
||||
<Compile Include="Tuple.cs" />
|
||||
<Compile Include="TypeDictionary.cs" />
|
||||
<Compile Include="Walkability.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OpenRa.DataStructures\OpenRa.DataStructures.csproj">
|
||||
<Project>{2F9E7A23-56C0-4286-9C8E-1060A9B2F073}</Project>
|
||||
<Name>OpenRa.DataStructures</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
48
OpenRa.FileFormats/Tuple.cs
Normal file
48
OpenRa.FileFormats/Tuple.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenRa.FileFormats
|
||||
{
|
||||
public class Tuple<A>
|
||||
{
|
||||
public A a;
|
||||
|
||||
public Tuple(A a) { this.a = a; }
|
||||
}
|
||||
|
||||
public class Tuple<A, B>
|
||||
{
|
||||
public A a;
|
||||
public B b;
|
||||
|
||||
public Tuple(A a, B b) { this.a = a; this.b = b; }
|
||||
}
|
||||
|
||||
public class Tuple<A, B, C>
|
||||
{
|
||||
public A a;
|
||||
public B b;
|
||||
public C c;
|
||||
|
||||
public Tuple(A a, B b, C c) { this.a = a; this.b = b; this.c = c; }
|
||||
}
|
||||
|
||||
public class Tuple<A, B, C, D>
|
||||
{
|
||||
public A a;
|
||||
public B b;
|
||||
public C c;
|
||||
public D d;
|
||||
|
||||
public Tuple(A a, B b, C c, D d) { this.a = a; this.b = b; this.c = c; this.d = d; }
|
||||
}
|
||||
|
||||
public static class Tuple
|
||||
{
|
||||
public static Tuple<A, B, C> New<A, B, C>(A a, B b, C c)
|
||||
{
|
||||
return new Tuple<A, B, C>(a, b, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
73
OpenRa.FileFormats/TypeDictionary.cs
Normal file
73
OpenRa.FileFormats/TypeDictionary.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using IjwFramework.Collections;
|
||||
|
||||
namespace OpenRa.FileFormats
|
||||
{
|
||||
public class TypeDictionary
|
||||
{
|
||||
Cache<Type, List<object>> innerInherit = new Cache<Type, List<object>>( _ => new List<object>() );
|
||||
|
||||
public void Add( object val )
|
||||
{
|
||||
var t = val.GetType();
|
||||
|
||||
foreach( var i in t.GetInterfaces() )
|
||||
innerInherit[ i ].Add( val );
|
||||
foreach( var tt in t.BaseTypes() )
|
||||
innerInherit[ tt ].Add( val );
|
||||
}
|
||||
|
||||
public bool Contains<T>()
|
||||
{
|
||||
return innerInherit[ typeof( T ) ].Count != 0;
|
||||
}
|
||||
|
||||
public T Get<T>()
|
||||
{
|
||||
var l = innerInherit[ typeof( T ) ];
|
||||
if( l.Count == 1 )
|
||||
return (T)l[ 0 ];
|
||||
else if( l.Count == 0 )
|
||||
throw new InvalidOperationException( string.Format( "TypeDictionary does not contain instance of type `{0}`", typeof( T ) ) );
|
||||
else
|
||||
throw new InvalidOperationException( string.Format( "TypeDictionary contains multiple instance of type `{0}`", typeof( T ) ) );
|
||||
}
|
||||
|
||||
public T GetOrDefault<T>()
|
||||
{
|
||||
var l = innerInherit[ typeof( T ) ];
|
||||
if( l.Count == 1 )
|
||||
return (T)l[ 0 ];
|
||||
else if( l.Count == 0 )
|
||||
return default( T );
|
||||
else
|
||||
throw new InvalidOperationException( string.Format( "TypeDictionary contains multiple instance of type `{0}`", typeof( T ) ) );
|
||||
}
|
||||
|
||||
public IEnumerable<T> WithInterface<T>()
|
||||
{
|
||||
foreach( var i in innerInherit[ typeof( T ) ] )
|
||||
yield return (T)i;
|
||||
}
|
||||
|
||||
public IEnumerator<object> GetEnumerator()
|
||||
{
|
||||
return WithInterface<object>().GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
static class TypeExts
|
||||
{
|
||||
public static IEnumerable<Type> BaseTypes( this Type t )
|
||||
{
|
||||
while( t != null )
|
||||
{
|
||||
yield return t;
|
||||
t = t.BaseType;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
93
OpenRa.FileFormats/float2.cs
Normal file
93
OpenRa.FileFormats/float2.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace OpenRa
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct float2
|
||||
{
|
||||
public float X, Y;
|
||||
|
||||
public float2(float x, float y) { X = x; Y = y; }
|
||||
public float2(PointF p) { X = p.X; Y = p.Y; }
|
||||
public float2(Point p) { X = p.X; Y = p.Y; }
|
||||
public float2(Size p) { X = p.Width; Y = p.Height; }
|
||||
public float2(SizeF p) { X = p.Width; Y = p.Height; }
|
||||
|
||||
public PointF ToPointF() { return new PointF(X, Y); }
|
||||
public SizeF ToSizeF() { return new SizeF(X, Y); }
|
||||
|
||||
public static implicit operator float2(int2 src) { return new float2(src.X, src.Y); }
|
||||
|
||||
public static float2 operator +(float2 a, float2 b) { return new float2(a.X + b.X, a.Y + b.Y); }
|
||||
public static float2 operator -(float2 a, float2 b) { return new float2(a.X - b.X, a.Y - b.Y); }
|
||||
|
||||
public static float2 operator -(float2 a) { return new float2(-a.X, -a.Y); }
|
||||
|
||||
public static float Lerp(float a, float b, float t) { return a + t * (b - a); }
|
||||
|
||||
public static float2 Lerp(float2 a, float2 b, float t)
|
||||
{
|
||||
return new float2(
|
||||
Lerp(a.X, b.X, t),
|
||||
Lerp(a.Y, b.Y, t));
|
||||
}
|
||||
|
||||
public static float2 Lerp(float2 a, float2 b, float2 t)
|
||||
{
|
||||
return new float2(
|
||||
Lerp(a.X, b.X, t.X),
|
||||
Lerp(a.Y, b.Y, t.Y));
|
||||
}
|
||||
|
||||
public static float2 FromAngle(float a) { return new float2((float)Math.Sin(a), (float)Math.Cos(a)); }
|
||||
|
||||
static float Constrain(float x, float a, float b) { return x < a ? a : x > b ? b : x; }
|
||||
|
||||
public float2 Constrain(float2 min, float2 max)
|
||||
{
|
||||
return new float2(
|
||||
Constrain(X, min.X, max.X),
|
||||
Constrain(Y, min.Y, max.Y));
|
||||
}
|
||||
|
||||
public static float2 operator *(float a, float2 b) { return new float2(a * b.X, a * b.Y); }
|
||||
public static float2 operator *( float2 a, float2 b ) { return new float2( a.X * b.X, a.Y * b.Y ); }
|
||||
public static float2 operator /( float2 a, float2 b ) { return new float2( a.X / b.X, a.Y / b.Y ); }
|
||||
|
||||
public static bool operator ==(float2 me, float2 other) { return (me.X == other.X && me.Y == other.Y); }
|
||||
public static bool operator !=(float2 me, float2 other) { return !(me == other); }
|
||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return false;
|
||||
|
||||
float2 o = (float2)obj;
|
||||
return o == this;
|
||||
}
|
||||
|
||||
public static readonly float2 Zero = new float2(0, 0);
|
||||
|
||||
public static bool WithinEpsilon(float2 a, float2 b, float e)
|
||||
{
|
||||
float2 d = a - b;
|
||||
return Math.Abs(d.X) < e && Math.Abs(d.Y) < e;
|
||||
}
|
||||
|
||||
public float2 Sign() { return new float2(Math.Sign(X), Math.Sign(Y)); }
|
||||
public static float Dot(float2 a, float2 b) { return a.X * b.X + a.Y * b.Y; }
|
||||
public float2 Round() { return new float2((float)Math.Round(X), (float)Math.Round(Y)); }
|
||||
|
||||
public override string ToString() { return string.Format("({0},{1})", X, Y); }
|
||||
public int2 ToInt2() { return new int2((int)X, (int)Y); }
|
||||
|
||||
public static float2 Max(float2 a, float2 b) { return new float2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); }
|
||||
public static float2 Min(float2 a, float2 b) { return new float2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); }
|
||||
|
||||
public float LengthSquared { get { return X * X + Y * Y; } }
|
||||
public float Length { get { return (float)Math.Sqrt(LengthSquared); } }
|
||||
}
|
||||
}
|
||||
49
OpenRa.FileFormats/int2.cs
Normal file
49
OpenRa.FileFormats/int2.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Drawing;
|
||||
|
||||
namespace OpenRa
|
||||
{
|
||||
public struct int2
|
||||
{
|
||||
public int X,Y;
|
||||
|
||||
public int2( int x, int y ) { this.X = x; this.Y = y; }
|
||||
public int2( Point p ) { X = p.X; Y = p.Y; }
|
||||
public int2( Size p ) { X = p.Width; Y = p.Height; }
|
||||
|
||||
public static int2 operator +(int2 a, int2 b) { return new int2(a.X + b.X, a.Y + b.Y); }
|
||||
public static int2 operator -(int2 a, int2 b) { return new int2(a.X - b.X, a.Y - b.Y); }
|
||||
public static int2 operator *(int a, int2 b) { return new int2(a * b.X, a * b.Y); }
|
||||
public static int2 operator *(int2 b, int a) { return new int2(a * b.X, a * b.Y); }
|
||||
|
||||
public static bool operator ==(int2 me, int2 other) { return (me.X == other.X && me.Y == other.Y); }
|
||||
public static bool operator !=(int2 me, int2 other) { return !(me == other); }
|
||||
|
||||
public int2 Sign() { return new int2(Math.Sign(X), Math.Sign(Y)); }
|
||||
public int2 Abs() { return new int2( Math.Abs( X ), Math.Abs( Y ) ); }
|
||||
public int LengthSquared { get { return X * X + Y * Y; } }
|
||||
public int Length { get { return (int)Math.Sqrt(LengthSquared); } }
|
||||
public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
|
||||
|
||||
public static int2 Max(int2 a, int2 b) { return new int2(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y)); }
|
||||
public static int2 Min(int2 a, int2 b) { return new int2(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y)); }
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return false;
|
||||
|
||||
int2 o = (int2)obj;
|
||||
return o == this;
|
||||
}
|
||||
|
||||
public static readonly int2 Zero = new int2(0, 0);
|
||||
public Point ToPoint() { return new Point(X, Y); }
|
||||
public PointF ToPointF() { return new PointF(X, Y); }
|
||||
public float2 ToFloat2() { return new float2(X, Y); }
|
||||
|
||||
public override string ToString() { return string.Format("({0},{1})", X, Y); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user