some work on decorate

This commit is contained in:
codeimp 2009-01-20 16:18:25 +00:00
parent 70be85aa95
commit b78d9c08ff
5 changed files with 521 additions and 87 deletions

View file

@ -667,7 +667,7 @@
<Compile Include="Data\PK3StructuredReader.cs" />
<Compile Include="Decorate\ActorStructure.cs" />
<Compile Include="Decorate\DecorateParser.cs" />
<Compile Include="Decorate\StatesStructure.cs" />
<Compile Include="Decorate\StateStructure.cs" />
<Compile Include="Editing\EditingManager.cs" />
<EmbeddedResource Include="Resources\Crosshair.png" />
<EmbeddedResource Include="Resources\CrosshairBusy.png" />

View file

@ -34,28 +34,223 @@ namespace CodeImp.DoomBuilder.Decorate
public sealed class ActorStructure
{
#region ================== Constants
#endregion
#region ================== Variables
// Declaration
private string inheritclass;
private string replaceclass;
private int doomednum = -1;
// Flags
private Dictionary<string, bool> flags;
// Properties
// We only parse the properties we know about
// because this format doesn't allow parsing in a generic way
// (properties can have zero, one or two values and there is
// nothing that tells you if it is a value or another property)
private int radius;
private int height;
// States
private List<StateStructure> states;
#endregion
#region ================== Properties
public Dictionary<string, bool> Flags { get { return flags; } }
public int Radius { get { return radius; } }
public int Height { get { return height; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
internal ActorStructure(Stream stream)
internal ActorStructure(DecorateParser parser)
{
// Initialize
flags = new Dictionary<string, bool>();
properties = new Dictionary<string, object>();
states = new List<StateStructure>();
// Parse tokens before entering the actor scope
while(parser.SkipWhitespace(true))
{
string token = parser.ReadToken();
if(!string.IsNullOrEmpty(token))
{
token = token.ToLowerInvariant();
if(token == ":")
{
// The next token must be the class to inherit from
parser.SkipWhitespace(true);
inheritclass = parser.ReadToken();
if(string.IsNullOrEmpty(inheritclass) || parser.IsSpecialToken(inheritclass))
{
parser.ReportError("Expected class name to inherit from");
return;
}
}
else if(replaceclass == "replaces")
{
// The next token must be the class to replace
parser.SkipWhitespace(true);
replaceclass = parser.ReadToken();
if(string.IsNullOrEmpty(replaceclass) || parser.IsSpecialToken(replaceclass))
{
parser.ReportError("Expected class name to replace");
return;
}
}
else if(token == "{")
{
// Actor scope begins here,
// break out of this parse loop
break;
}
else
{
// Check if numeric
if(!int.TryParse(token, out doomednum))
{
// Not numeric!
parser.ReportError("Expected numeric editor thing number");
return;
}
}
}
else
{
parser.ReportError("Unexpected end of structure");
return;
}
}
// Now parse the contents of actor structure
while(parser.SkipWhitespace(true))
{
string token = parser.ReadToken();
token = token.ToLowerInvariant();
if((token == "+") || (token == "-"))
{
// Next token is a flag (option) to set or remove
bool flagvalue = (token == "+");
parser.SkipWhitespace(true);
string flagname = parser.ReadToken();
if(!string.IsNullOrEmpty(flagname))
{
// Add the flag with its value
flagname = flagname.ToLowerInvariant();
flags.Add(flagname, flagvalue);
}
else
{
parser.ReportError("Expected flag name");
return;
}
}
else if(token == "states")
{
// Now parse actor states until we reach the end of the states structure
while(parser.SkipWhitespace(true))
{
string statename = parser.ReadToken();
if(!string.IsNullOrEmpty(statename))
{
// Start of scope?
if(statename == "{")
{
// This is fine
}
// End of scope?
else if(statename == "}")
{
// Done with the states,
// break out of this parse loop
break;
}
else
{
// Next token must be a :
parser.SkipWhitespace(true);
string labeltoken = parser.ReadToken();
if(!string.IsNullOrEmpty(statename))
{
if(labeltoken == ":")
{
// Parse actor state
StateStructure st = new StateStructure(parser);
states.Add(st);
if(parser.HasError) return;
}
else
{
parser.ReportError("Expected state label colon");
return;
}
}
else
{
parser.ReportError("Unexpected end of structure");
return;
}
}
}
else
{
parser.ReportError("Unexpected end of structure");
return;
}
}
}
else if(token == "}")
{
// Actor scope ends here,
// break out of this parse loop
break;
}
else
{
// This must be a property
// Is this a known property?
if((token == "radius") || (token == "height"))
{
// Next token is the property value to set
parser.SkipWhitespace(true);
string value = parser.ReadToken();
if(!string.IsNullOrEmpty(value))
{
// Try parsing as integer value
int intvalue = 0;
int.TryParse(value, out intvalue);
// Set the property
if(token == "radius")
radius = intvalue;
else if(token == "height")
height = intvalue;
}
else
{
// Can't find the property value!
parser.ReportError("Expected a value for property '" + token + "'");
return;
}
}
}
}
}
#endregion
#region ================== Methods
#endregion
}
}

View file

@ -34,30 +34,198 @@ namespace CodeImp.DoomBuilder.Decorate
public sealed class DecorateParser
{
#region ================== Constants
// Parsing
private const string WHITESPACE = "\n \t\r";
private const string SPECIALTOKEN = ":{}+-\n";
#endregion
#region ================== Variables
// Objects
private List<ActorStructure> actors;
// Input data stream
private Stream datastream;
private StreamReader datareader;
// Error report
private int errorline;
private string errordesc;
#endregion
#region ================== Properties
internal Stream DataStream { get { return datastream; } }
internal StreamReader DataReader { get { return datareader; } }
public ICollection<ActorStructure> Actors { get { return actors; } }
public int ErrorLine { get { return errorline; } }
public string ErrorDescription { get { return errordesc; } }
public bool HasError { get { return (errordesc != null); } }
#endregion
#region ================== Constructor / Disposer
// Constructor
internal DecorateParser(Stream stream)
public DecorateParser()
{
// Initialize
actors = new List<ActorStructure>();
errordesc = null;
}
#endregion
#region ================== Methods
// This parses the given decorate stream
// Returns false on errors
public bool Parse(Stream stream)
{
datastream = stream;
datareader = new StreamReader(datastream, Encoding.ASCII);
datastream.Seek(0, SeekOrigin.Begin);
// Continue until at the end of the stream
while(SkipWhitespace(true))
{
// Read a token
string objdeclaration = ReadToken();
if(objdeclaration != null)
{
objdeclaration = objdeclaration.ToLowerInvariant();
if(objdeclaration == "actor")
{
// Read actor structure
ActorStructure actor = new ActorStructure(this);
if(this.HasError) break;
}
else
{
// Unknown structure!
ReportError("Unknown declaration type '" + objdeclaration + "'");
break;
}
}
}
// Return true when no errors occurred
return (errordesc == null);
}
// This returns true if the given character is whitespace
internal bool IsWhitespace(char c)
{
return (WHITESPACE.IndexOf(c) > -1);
}
#endregion
#region ================== Methods
// This returns true if the given character is a special token
internal bool IsSpecialToken(char c)
{
return (SPECIALTOKEN.IndexOf(c) > -1);
}
// This returns true if the given character is a special token
internal bool IsSpecialToken(string s)
{
if(s.Length > 0)
return (SPECIALTOKEN.IndexOf(s[0]) > -1);
else
return false;
}
// This skips whitespace on the stream, placing the read
// position right before the first non-whitespace character
// Returns false when the end of the stream is reached
internal bool SkipWhitespace(bool skipnewline)
{
int offset = skipnewline ? 1 : 0;
int c;
do
{
c = datareader.Read();
if(c == -1) return false;
}
while(WHITESPACE.IndexOf(unchecked((char)c), offset) > -1);
// Go one character back so we can read this non-whitespace character again
datastream.Seek(-1, SeekOrigin.Current);
return true;
}
// This reads a token (all sequential non-whitespace characters or a single character)
// Returns null when the end of the stream has been reached
internal string ReadToken()
{
string token = "";
int c;
// Return null when the end of the stream has been reached
if(datareader.EndOfStream) return null;
// Start reading
c = datareader.Read();
while((c != -1) && !IsWhitespace(unchecked((char)c)))
{
char cc = unchecked((char)c);
// Special token?
if(IsSpecialToken(cc))
{
// Not reading a token yet?
if(token.Length == 0)
{
// This is our whole token
token += cc;
break;
}
else
{
// This is a new token and shouldn't be read now
// Go one character back so we can read this token again
datastream.Seek(-1, SeekOrigin.Current);
break;
}
}
else
{
token += cc;
}
// Next character
c = datareader.Read();
}
return token;
}
// This reports an error
internal void ReportError(string message)
{
long position = datastream.Position;
int linenumber = 1;
// Find the line on which we found this error
datastream.Seek(0, SeekOrigin.Begin);
while(datastream.Position < position)
{
string line = datareader.ReadLine();
if(line == null) break;
linenumber++;
}
// Return to original position
datastream.Seek(position, SeekOrigin.Begin);
// Set error information
errordesc = message;
errorline = linenumber;
}
#endregion
}
}

View file

@ -0,0 +1,134 @@
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Data;
using System.IO;
using System.Diagnostics;
using CodeImp.DoomBuilder.Compilers;
#endregion
namespace CodeImp.DoomBuilder.Decorate
{
public sealed class StateStructure
{
#region ================== Constants
#endregion
#region ================== Variables
// All we care about is the first sprite in the sequence
private string firstsprite;
#endregion
#region ================== Properties
public string FirstSprite { get { return firstsprite; } }
#endregion
#region ================== Constructor / Disposer
// Constructor
internal StateStructure(DecorateParser parser)
{
string lasttoken;
firstsprite = null;
// Skip whitespace
while(parser.SkipWhitespace(true))
{
// Read first token
string token = parser.ReadToken();
token = token.ToLowerInvariant();
// One of the flow control statements?
if((token == "loop") || (token == "stop") || (token == "wait") || (token == "fail"))
{
// Then we leave
break;
}
// Label?
else if(token == ":")
{
// Rewind so that this label can be read again
parser.DataStream.Seek(-(lasttoken.Length + 1), SeekOrigin.Current);
// Done here
return;
}
// End of scope?
else if(token == "}")
{
// Done here
return;
}
else
{
// First part of the sprite name
if(token == null)
{
parser.ReportError("Unexpected end of structure");
return;
}
// Frames of the sprite name
parser.SkipWhitespace(true);
string spriteframes = parser.ReadToken();
if(spriteframes == null)
{
parser.ReportError("Unexpected end of structure");
return;
}
// No first sprite yet?
if((firstsprite == null) && (spriteframes.Length > 0))
{
// Make the sprite name
firstsprite = token + spriteframes[0];
firstsprite = firstsprite.ToUpperInvariant();
}
// Continue until the end of the line
string t = "";
while((t != "\n") && (t != null))
{
parser.SkipWhitespace(false);
t = parser.ReadToken();
}
}
lasttoken = token;
}
}
#endregion
#region ================== Methods
#endregion
}
}

View file

@ -1,63 +0,0 @@
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Data;
using System.IO;
using System.Diagnostics;
using CodeImp.DoomBuilder.Compilers;
#endregion
namespace CodeImp.DoomBuilder.Decorate
{
public sealed class StatesStructure
{
#region ================== Constants
#endregion
#region ================== Variables
private List<StatesStructure> states;
#endregion
#region ================== Properties
#endregion
#region ================== Constructor / Disposer
// Constructor
internal StatesStructure(Stream stream)
{
}
#endregion
#region ================== Methods
#endregion
}
}