- beginnings of map loading (INI parser works)

- fixed warnings in mix_decode.h

git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1075 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
chrisf
2007-06-26 08:47:11 +00:00
parent b83b17385d
commit 2002fa4a90
7 changed files with 538 additions and 2 deletions

88
MapViewer/IniFile.cs Normal file
View File

@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace MapViewer
{
class IniFile
{
Dictionary<string, IniSection> sections = new Dictionary<string, IniSection>();
IniSection currentSection;
public IniFile(Stream s)
{
StreamReader reader = new StreamReader(s);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (!ProcessEntry(line))
ProcessSection(line);
}
}
Regex sectionPattern = new Regex(@"\[([^]]*)\]");
Regex entryPattern = new Regex(@"([^=]+)=([^;]*)");
bool ProcessSection(string line)
{
Match m = sectionPattern.Match(line);
if (m == null || !m.Success)
return false;
string sectionName = m.Groups[1].Value;
currentSection = new IniSection(sectionName);
sections.Add(sectionName, currentSection);
return true;
}
bool ProcessEntry(string line)
{
Match m = entryPattern.Match(line);
if (m == null || !m.Success)
return false;
if (currentSection == null)
throw new InvalidOperationException("No current INI section");
string keyName = m.Groups[1].Value;
string keyValue = m.Groups[2].Value;
currentSection.Add(keyName, keyValue);
return true;
}
public IniSection GetSection(string s)
{
IniSection section;
sections.TryGetValue(s, out section);
return section;
}
}
class IniSection
{
string name;
Dictionary<string, string> values = new Dictionary<string, string>();
public IniSection(string name)
{
this.name = name;
}
public void Add(string key, string value)
{
values.Add(key, value);
}
public string GetValue(string key, string defaultValue)
{
string s;
return values.TryGetValue( key, out s ) ? s : defaultValue;
}
}
}

View File

@@ -0,0 +1,50 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{3942F56A-F427-4DE0-928A-89DEA952FF5F}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MapViewer</RootNamespace>
<AssemblyName>MapViewer</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="IniFile.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

43
MapViewer/Program.cs Normal file
View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace MapViewer
{
class Program
{
static Stream GetFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.RestoreDirectory = true;
ofd.Filter = "Map files (*.ini)|*.ini";
return (DialogResult.OK == ofd.ShowDialog()) ? ofd.OpenFile() : null;
}
static void Main(string[] args)
{
Stream s = GetFile();
if (s == null)
{
Console.WriteLine("Fail");
return;
}
IniFile iniFile = new IniFile(s);
Console.WriteLine("Done.");
IniSection basic = iniFile.GetSection("Basic");
Console.WriteLine("Name: {0}", basic.GetValue("Name", "(null)"));
Console.WriteLine("Official: {0}", basic.GetValue("Official", "no"));
IniSection map = iniFile.GetSection("Map");
Console.WriteLine("Theater: {0}", map.GetValue("Theater", "TEMPERATE"));
Console.WriteLine("X: {0} Y: {1} Width: {2} Height: {3}",
map.GetValue("X", "0"), map.GetValue("Y", "0"),
map.GetValue("Width", "0"), map.GetValue("Height", "0"));
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MapViewer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MapViewer")]
[assembly: AssemblyCopyright("Copyright © 2007")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3d06bcba-b9e2-412c-829c-a8458d883fee")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]