diff --git a/OpenRa.FileFormats/IniFile.cs b/OpenRa.FileFormats/IniFile.cs index e4d931a1e3..97ec1a957f 100644 --- a/OpenRa.FileFormats/IniFile.cs +++ b/OpenRa.FileFormats/IniFile.cs @@ -47,18 +47,19 @@ namespace OpenRa.FileFormats bool ProcessEntry( string line ) { - Match m = entryPattern.Match( line ); - if( m == null || !m.Success ) - return false; + int comment = line.IndexOf(';'); + if (comment >= 0) + line = line.Substring(0, comment); - if( currentSection == null ) - throw new InvalidOperationException( "No current INI section" ); + int eq = line.IndexOf('='); + if (eq < 0) + return false; - string keyName = m.Groups[ 1 ].Value; - string keyValue = m.Groups[ 2 ].Value; - - currentSection.Add( keyName, keyValue ); + if (currentSection == null) + throw new InvalidOperationException("No current INI section"); + currentSection.Add(line.Substring(0, eq), + line.Substring(eq + 1, line.Length - eq - 1)); return true; } diff --git a/OpenRa.FileFormats/OpenRa.FileFormats.csproj b/OpenRa.FileFormats/OpenRa.FileFormats.csproj index f95421aee1..ebf9aefc2e 100644 --- a/OpenRa.FileFormats/OpenRa.FileFormats.csproj +++ b/OpenRa.FileFormats/OpenRa.FileFormats.csproj @@ -2,7 +2,7 @@ Debug AnyCPU - 8.0.50727 + 9.0.21022 2.0 {BDAEAB25-991E-46A7-AF1E-4F0E03358DAA} Library @@ -14,6 +14,7 @@ 2.0 + v2.0 true @@ -23,6 +24,7 @@ DEBUG;TRACE prompt 4 + true pdbonly @@ -31,6 +33,7 @@ TRACE prompt 4 + true diff --git a/OpenRa.FileFormats/Package.cs b/OpenRa.FileFormats/Package.cs index 2131e9b407..ebefa30998 100644 --- a/OpenRa.FileFormats/Package.cs +++ b/OpenRa.FileFormats/Package.cs @@ -13,39 +13,48 @@ namespace OpenRa.FileFormats public class Package : IFolder { readonly string filename; - readonly List index; + readonly Dictionary index; readonly bool isRmix, isEncrypted; readonly long dataStart; + readonly Stream s; - public ICollection Content - { - get { return index.AsReadOnly(); } - } + //public ICollection Content + //{ + // get { return index.AsReadOnly(); } + //} + + public static Dictionary MakeDict(IEnumerable values, Converter keyFunc) + { + var dict = new Dictionary(); + foreach (var v in values) + dict.Add(keyFunc(v), v); + + return dict; + } public Package(string filename) { this.filename = filename; - using (Stream s = FileSystem.Open(filename)) + s = FileSystem.Open(filename); + + BinaryReader reader = new BinaryReader(s); + uint signature = reader.ReadUInt32(); + + isRmix = 0 == (signature & ~(uint)(MixFileFlags.Checksum | MixFileFlags.Encrypted)); + + if (isRmix) { - BinaryReader reader = new BinaryReader(s); - uint signature = reader.ReadUInt32(); - - isRmix = 0 == (signature & ~(uint)(MixFileFlags.Checksum | MixFileFlags.Encrypted)); - - if (isRmix) + isEncrypted = 0 != (signature & (uint)MixFileFlags.Encrypted); + if( isEncrypted ) { - isEncrypted = 0 != (signature & (uint)MixFileFlags.Encrypted); - if( isEncrypted ) - { - index = ParseRaHeader( s, out dataStart ); - return; - } + index = MakeDict(ParseRaHeader( s, out dataStart ), x => x.Hash ); + return; } - - isEncrypted = false; - s.Seek(0, SeekOrigin.Begin); - index = ParseTdHeader(s, out dataStart); } + + isEncrypted = false; + s.Seek(0, SeekOrigin.Begin); + index = MakeDict(ParseTdHeader(s, out dataStart), x => x.Hash ); } const long headerStart = 84; @@ -121,19 +130,14 @@ namespace OpenRa.FileFormats public Stream GetContent(uint hash) { - foreach( PackageEntry e in index ) - if (e.Hash == hash) - { - using (Stream s = FileSystem.Open(filename)) - { - s.Seek( dataStart + e.Offset, SeekOrigin.Begin ); - byte[] data = new byte[ e.Length ]; - s.Read( data, 0, (int)e.Length ); - return new MemoryStream(data); - } - } + PackageEntry e; + if (!index.TryGetValue(hash, out e)) + return null; - return null; + s.Seek( dataStart + e.Offset, SeekOrigin.Begin ); + byte[] data = new byte[ e.Length ]; + s.Read( data, 0, (int)e.Length ); + return new MemoryStream(data); } public Stream GetContent(string filename) diff --git a/OpenRa.FileFormats/PackageEntry.cs b/OpenRa.FileFormats/PackageEntry.cs index b70e3fc1f9..99871c059c 100644 --- a/OpenRa.FileFormats/PackageEntry.cs +++ b/OpenRa.FileFormats/PackageEntry.cs @@ -39,13 +39,11 @@ namespace OpenRa.FileFormats MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(name)); BinaryReader reader = new BinaryReader(ms); + int len = name.Length >> 2; uint result = 0; - try - { - while(true) - result = ((result << 1) | (result >> 31)) + reader.ReadUInt32(); - } - catch (EndOfStreamException) { } + + while (len-- != 0) + result = ((result << 1) | (result >> 31)) + reader.ReadUInt32(); return result; } diff --git a/OpenRa.TechTree/Item.cs b/OpenRa.Game/Item.cs similarity index 100% rename from OpenRa.TechTree/Item.cs rename to OpenRa.Game/Item.cs diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 4dd9ff031d..0080dcc292 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -71,10 +71,13 @@ + + + @@ -99,6 +102,7 @@ + @@ -121,10 +125,6 @@ {BDAEAB25-991E-46A7-AF1E-4F0E03358DAA} OpenRa.FileFormats - - {2BFC3861-E90E-4F77-B254-8FB8285E43AC} - OpenRa.TechTree - diff --git a/OpenRa.TechTree/Race.cs b/OpenRa.Game/Race.cs similarity index 100% rename from OpenRa.TechTree/Race.cs rename to OpenRa.Game/Race.cs diff --git a/OpenRa.Game/Rules.cs b/OpenRa.Game/Rules.cs index 8d13b2a0dc..8d2cfa64e0 100644 --- a/OpenRa.Game/Rules.cs +++ b/OpenRa.Game/Rules.cs @@ -12,7 +12,7 @@ namespace OpenRa.Game static Rules() { - IniFile rulesIni = new IniFile(FileSystem.Open("rules.ini")); + var rulesIni = SharedResources.Rules; foreach (string line in Util.ReadAllLines(FileSystem.Open("units.txt"))) { diff --git a/OpenRa.Game/SharedResources.cs b/OpenRa.Game/SharedResources.cs new file mode 100644 index 0000000000..a9c82b7322 --- /dev/null +++ b/OpenRa.Game/SharedResources.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; +using OpenRa.FileFormats; +using IjwFramework.Types; + +namespace OpenRa.Game +{ + class SharedResources + { + static Lazy rules = new Lazy( () => new IniFile( FileSystem.Open( "rules.ini" ))); + public static IniFile Rules { get { return rules.Value; } } + } +} diff --git a/OpenRa.Game/Sprite.cs b/OpenRa.Game/Sprite.cs index 857d7d4d09..4c19614c2a 100644 --- a/OpenRa.Game/Sprite.cs +++ b/OpenRa.Game/Sprite.cs @@ -13,6 +13,8 @@ namespace OpenRa.Game public readonly RectangleF uv; public readonly float2 size; + readonly float2[] uvhax; + internal Sprite(Sheet sheet, Rectangle bounds, TextureChannel channel) { this.bounds = bounds; @@ -25,6 +27,14 @@ namespace OpenRa.Game (float)(bounds.Width) / sheet.Size.Width, (float)(bounds.Height) / sheet.Size.Height); + uvhax = new float2[] + { + MapTextureCoords( new float2(0,0) ), + MapTextureCoords( new float2(1,0) ), + MapTextureCoords( new float2(0,1) ), + MapTextureCoords( new float2(1,1) ), + }; + this.size = new float2(bounds.Size); } @@ -35,8 +45,11 @@ namespace OpenRa.Game p.Y > 0 ? uv.Bottom : uv.Top); } - public float2 Size { get { return size; } } - } + public float2 FastMapTextureCoords(int k) + { + return uvhax[k]; + } + } public enum TextureChannel { diff --git a/OpenRa.TechTree/TechTree.cs b/OpenRa.Game/TechTree.cs similarity index 87% rename from OpenRa.TechTree/TechTree.cs rename to OpenRa.Game/TechTree.cs index fae4a26ae3..535b387c32 100644 --- a/OpenRa.TechTree/TechTree.cs +++ b/OpenRa.Game/TechTree.cs @@ -4,14 +4,12 @@ using System.IO; using System.Text; using System.Text.RegularExpressions; using OpenRa.FileFormats; +using OpenRa.Game; namespace OpenRa.TechTree { public class TechTree { - static IniFile rules; - static IniFile Rules { get { return rules ?? (rules = new IniFile(FileSystem.Open("rules.ini"))); } } - Dictionary objects = new Dictionary(); public ICollection built = new List(); @@ -61,8 +59,10 @@ namespace OpenRa.TechTree Lines("buildings.txt", true), Lines("units.txt", false)); + var rules = SharedResources.Rules; + foreach (Tuple p in definitions) - objects.Add(p.a, new Item(p.a, p.b, Rules.GetSection(p.a), p.c)); + objects.Add(p.a, new Item(p.a, p.b, rules.GetSection(p.a), p.c)); } public bool Build(string key, bool force) diff --git a/OpenRa.Game/TerrainCosts.cs b/OpenRa.Game/TerrainCosts.cs index 4080ac9d11..80609506cd 100644 --- a/OpenRa.Game/TerrainCosts.cs +++ b/OpenRa.Game/TerrainCosts.cs @@ -36,7 +36,7 @@ namespace OpenRa.Game static TerrainCosts() { - IniFile file = new IniFile(FileSystem.Open("rules.ini")); + IniFile file = SharedResources.Rules; for( int i = 0 ; i < 10 ; i++ ) { diff --git a/OpenRa.Game/Util.cs b/OpenRa.Game/Util.cs index b26023ffc2..49a2869a66 100644 --- a/OpenRa.Game/Util.cs +++ b/OpenRa.Game/Util.cs @@ -17,17 +17,24 @@ namespace OpenRa.Game return new float2(paletteLine / 16.0f, channelSelect[(int)channel]); } - public static Vertex MakeVertex(float2 o, float2 uv, Sprite r, int palette) + static float2 KLerp(float2 o, float2 d, int k) + { + switch (k) + { + case 0: return o; + case 1: return new float2(o.X + d.X, o.Y); + case 2: return new float2(o.X, o.Y + d.Y); + case 3: return new float2(o.X + d.X, o.Y + d.Y); + default: throw new InvalidOperationException(); + } + } + + static Vertex MakeVertex(float2 o, int k, Sprite r, float2 attrib) { return new Vertex( - float2.Lerp( o, o + r.Size, uv ), - r.MapTextureCoords(uv), - EncodeVertexAttributes(r.channel, palette)); - } - - static float Lerp(float a, float b, float t) - { - return (1 - t) * a + t * b; + KLerp( o, r.size, k ), + r.FastMapTextureCoords(k), + attrib); } public static string[] ReadAllLines(Stream s) @@ -49,28 +56,27 @@ namespace OpenRa.Game return result; } - static float2[] uv = - { - new float2( 0,0 ), - new float2( 1,0 ), - new float2( 0,1 ), - new float2( 1,1 ), - }; - public static void CreateQuad(List vertices, List indices, float2 o, Sprite r, int palette) { ushort offset = (ushort)vertices.Count; + float2 attrib = EncodeVertexAttributes(r.channel, palette); - foreach( float2 p in uv ) - vertices.Add(Util.MakeVertex(o, p, r, palette)); + Vertex[] v = new Vertex[] + { + Util.MakeVertex(o, 0, r, attrib), + Util.MakeVertex(o, 1, r, attrib), + Util.MakeVertex(o, 2, r, attrib), + Util.MakeVertex(o, 3, r, attrib), + }; - indices.Add(offset); - indices.Add((ushort)(offset + 1)); - indices.Add((ushort)(offset + 2)); + vertices.AddRange(v); - indices.Add((ushort)(offset + 1)); - indices.Add((ushort)(offset + 3)); - indices.Add((ushort)(offset + 2)); + ushort[] i = new ushort[] + { + offset, (ushort)(offset + 1), (ushort)(offset + 2), (ushort)(offset + 1), (ushort)(offset + 3), (ushort)(offset + 2) + }; + + indices.AddRange(i); } public static void FastCopyIntoChannel(Sprite dest, byte[] src) diff --git a/OpenRa.TechTree/OpenRa.TechTree.csproj b/OpenRa.TechTree/OpenRa.TechTree.csproj deleted file mode 100644 index 5a213e087f..0000000000 --- a/OpenRa.TechTree/OpenRa.TechTree.csproj +++ /dev/null @@ -1,65 +0,0 @@ - - - Debug - AnyCPU - 8.0.50727 - 2.0 - {2BFC3861-E90E-4F77-B254-8FB8285E43AC} - Library - Properties - OpenRa.TechTree - OpenRa.TechTree - - - 2.0 - - - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - {2F9E7A23-56C0-4286-9C8E-1060A9B2F073} - OpenRa.DataStructures - - - {BDAEAB25-991E-46A7-AF1E-4F0E03358DAA} - OpenRa.FileFormats - - - - - \ No newline at end of file diff --git a/OpenRa.TechTree/Properties/AssemblyInfo.cs b/OpenRa.TechTree/Properties/AssemblyInfo.cs deleted file mode 100644 index f501b1c1a0..0000000000 --- a/OpenRa.TechTree/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -[assembly: AssemblyTitle("OpenRa.TechTree")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("IJW Software")] -[assembly: AssemblyProduct("OpenRa.TechTree")] -[assembly: AssemblyCopyright("Copyright © IJW Software 2007")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] -[assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/OpenRa.sln b/OpenRa.sln index a118323d7d..f0006f696f 100644 --- a/OpenRa.sln +++ b/OpenRa.sln @@ -10,8 +10,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRa.FileFormats", "OpenR EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRa.Game", "OpenRa.Game\OpenRa.Game.csproj", "{0DFB103F-2962-400F-8C6D-E2C28CCBA633}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRa.TechTree", "OpenRa.TechTree\OpenRa.TechTree.csproj", "{2BFC3861-E90E-4F77-B254-8FB8285E43AC}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRa.DataStructures", "OpenRa.DataStructures\OpenRa.DataStructures.csproj", "{2F9E7A23-56C0-4286-9C8E-1060A9B2F073}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaletteUsage", "PaletteUsage\PaletteUsage.csproj", "{54577061-E2D2-4336-90A2-A9A7106A30CD}" @@ -56,16 +54,6 @@ Global {0DFB103F-2962-400F-8C6D-E2C28CCBA633}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {0DFB103F-2962-400F-8C6D-E2C28CCBA633}.Release|Mixed Platforms.Build.0 = Release|Any CPU {0DFB103F-2962-400F-8C6D-E2C28CCBA633}.Release|Win32.ActiveCfg = Release|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Debug|Win32.ActiveCfg = Debug|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Release|Any CPU.Build.0 = Release|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {2BFC3861-E90E-4F77-B254-8FB8285E43AC}.Release|Win32.ActiveCfg = Release|Any CPU {2F9E7A23-56C0-4286-9C8E-1060A9B2F073}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {2F9E7A23-56C0-4286-9C8E-1060A9B2F073}.Debug|Any CPU.Build.0 = Debug|Any CPU {2F9E7A23-56C0-4286-9C8E-1060A9B2F073}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU