internal ZScriptActorStructure(ZDTextParser zdparser, DecorateCategoryInfo catinfo, string _classname, string _replacesname, string _parentname) { this.catinfo = catinfo; //mxd parser = (ZScriptParser)zdparser; stream = parser.DataStream; tokenizer = new ZScriptTokenizer(parser.DataReader); parser.tokenizer = tokenizer; classname = _classname; replaceclass = _replacesname; //baseclass = parser.GetArchivedActorByName(_parentname); // this is not guaranteed to work here mixins = new List <string>(); ZScriptToken cls_open = tokenizer.ExpectToken(ZScriptTokenType.OpenCurly); if (cls_open == null || !cls_open.IsValid) { parser.ReportError("Expected {, got " + ((Object)cls_open ?? "<null>").ToString()); return; } // this dict holds temporary user settings per field (function, etc) Dictionary <string, List <string> > var_props = new Dictionary <string, List <string> >(); // in the class definition, we can have the following: // - Defaults block // - States block // - method signature: [native] [action] <type [, type [...]]> <name> (<arguments>); // - method: <method signature (except native)> <block> // - field declaration: [native] <type> <name>; // - enum definition: enum <name> <block>; // we are skipping everything, except Defaults and States. while (true) { var_props.Clear(); while (true) { ZScriptToken tt = tokenizer.ExpectToken(ZScriptTokenType.Whitespace, ZScriptTokenType.BlockComment, ZScriptTokenType.LineComment, ZScriptTokenType.Newline); if (tt == null || !tt.IsValid) { break; } if (tt.Type == ZScriptTokenType.LineComment) { ParseGZDBComment(var_props, tt.Value); } } //tokenizer.SkipWhitespace(); long ocpos = stream.Position; ZScriptToken token = tokenizer.ExpectToken(ZScriptTokenType.Identifier, ZScriptTokenType.CloseCurly); if (token == null || !token.IsValid) { parser.ReportError("Expected identifier, got " + ((Object)cls_open ?? "<null>").ToString()); return; } if (token.Type == ZScriptTokenType.CloseCurly) // end of class { break; } string b_lower = token.Value.ToLowerInvariant(); switch (b_lower) { case "default": if (!ParseDefaultBlock()) { return; } continue; case "states": if (!ParseStatesBlock()) { return; } continue; case "enum": if (!parser.ParseEnum()) { return; } continue; case "const": if (!parser.ParseConst()) { return; } continue; // apparently we can have a struct inside a class, but not another class. case "struct": if (!parser.ParseClassOrStruct(true, false, false, null)) { return; } continue; // new properties syntax case "property": if (!ParseProperty()) { return; } continue; // new flags syntax case "flagdef": if (!ParseFlagdef()) { return; } continue; // mixins case "mixin": if (!ParseMixin()) { return; } continue; default: stream.Position = ocpos; break; } // try to read in a variable/method. bool bmethod = false; string[] availablemodifiers = new string[] { "static", "native", "action", "readonly", "protected", "private", "virtual", "override", "meta", "transient", "deprecated", "final", "play", "ui", "clearscope", "virtualscope", "version", "const" }; string[] versionedmodifiers = new string[] { "version", "deprecated" }; string[] methodmodifiers = new string[] { "action", "virtual", "override", "final" }; HashSet <string> modifiers = new HashSet <string>(); List <string> types = new List <string>(); List <List <int> > typearraylens = new List <List <int> >(); List <string> names = new List <string>(); List <List <int> > arraylens = new List <List <int> >(); List <ZScriptToken> args = null; // this is for the future //List<ZScriptToken> body = null; while (true) { tokenizer.SkipWhitespace(); long cpos = stream.Position; token = tokenizer.ExpectToken(ZScriptTokenType.Identifier); if (token == null || !token.IsValid) { parser.ReportError("Expected modifier or name, got " + ((Object)cls_open ?? "<null>").ToString()); return; } b_lower = token.Value.ToLowerInvariant(); if (availablemodifiers.Contains(b_lower)) { if (modifiers.Contains(b_lower)) { parser.ReportError("Field/method modifier '" + b_lower + "' was specified twice"); return; } if (methodmodifiers.Contains(b_lower)) { bmethod = true; } if (versionedmodifiers.Contains(b_lower)) { string version = ParseVersion(b_lower == "version"); // deprecated doesn't require version string for historical reasons. (compatibility with old gzdoom.pk3) if (version == null && b_lower == "version") { return; } } modifiers.Add(b_lower); } else { stream.Position = cpos; break; } } // read in the type name(s) // type name can be: // - identifier // - identifier<identifier> while (true) { tokenizer.SkipWhitespace(); string typename = ParseTypeName(); if (typename == null) { return; } types.Add(typename.ToLowerInvariant()); typearraylens.Add(null); long cpos = stream.Position; tokenizer.SkipWhitespace(); token = tokenizer.ExpectToken(ZScriptTokenType.Comma, ZScriptTokenType.Identifier, ZScriptTokenType.OpenSquare); if (token != null && !token.IsValid) { parser.ReportError("Expected comma, identifier or array dimensions, got " + ((Object)token ?? "<null>").ToString()); return; } if (token == null || token.Type != ZScriptTokenType.Comma) { stream.Position = cpos; if (token.Type == ZScriptTokenType.OpenSquare) { List <int> typelens = ParseArrayDimensions(); if (typelens == null) // error { return; } typearraylens[typearraylens.Count - 1] = typelens; } break; } } while (true) { string name = null; List <int> lens = null; // read in the method/field name tokenizer.SkipWhitespace(); token = tokenizer.ExpectToken(ZScriptTokenType.Identifier); if (token == null || !token.IsValid) { parser.ReportError("Expected field/method name, got " + ((Object)token ?? "<null>").ToString()); return; } name = token.Value.ToLowerInvariant(); // check the token. if it's a (, then it's a method. if it's a ;, then it's a field, if it's a [ it's an array field. // if it's a field and bmethod=true, report error. tokenizer.SkipWhitespace(); long cpos = stream.Position; token = tokenizer.ExpectToken(ZScriptTokenType.Comma, ZScriptTokenType.OpenParen, ZScriptTokenType.OpenSquare, ZScriptTokenType.Semicolon); if (token == null || !token.IsValid) { parser.ReportError("Expected comma, ;, [, or argument list, got " + ((Object)token ?? "<null>").ToString()); return; } if (token.Type == ZScriptTokenType.OpenParen) { // if we have multiple names if (names.Count > 0) { parser.ReportError("Cannot have multiple names in a method"); return; } bmethod = true; // now, I could parse this properly, but it won't be used anyway, so I'll do it as a fake expression. args = parser.ParseExpression(true); token = tokenizer.ExpectToken(ZScriptTokenType.CloseParen); if (token == null || !token.IsValid) { parser.ReportError("Expected ), got " + ((Object)token ?? "<null>").ToString()); return; } // also get the body block, if any. tokenizer.SkipWhitespace(); cpos = stream.Position; token = tokenizer.ExpectToken(ZScriptTokenType.Semicolon, ZScriptTokenType.OpenCurly, ZScriptTokenType.Identifier); if (token == null || !token.IsValid) { parser.ReportError("Expected 'const', ; or {, got " + ((Object)token ?? "<null>").ToString()); return; } // if (token.Type == ZScriptTokenType.Identifier) { if (token.Value.ToLowerInvariant() != "const") { parser.ReportError("Expected 'const', got " + ((Object)token ?? "<null>").ToString()); return; } tokenizer.SkipWhitespace(); cpos = stream.Position; token = tokenizer.ExpectToken(ZScriptTokenType.Semicolon, ZScriptTokenType.OpenCurly); if (token == null || !token.IsValid) { parser.ReportError("Expected ; or {, got " + ((Object)token ?? "<null>").ToString()); return; } } if (token.Type == ZScriptTokenType.OpenCurly) { stream.Position = cpos; parser.SkipBlock(); //body = parser.ParseBlock(false); } break; // end method parsing } else { if (bmethod) { parser.ReportError("Cannot have virtual, override or action fields"); return; } // array if (token.Type == ZScriptTokenType.OpenSquare) { stream.Position = cpos; lens = ParseArrayDimensions(); if (lens == null) // error { return; } tokenizer.SkipWhitespace(); ZScriptTokenType[] expectTokens; if (modifiers.Contains("static")) { expectTokens = new ZScriptTokenType[] { ZScriptTokenType.Semicolon, ZScriptTokenType.Comma, ZScriptTokenType.OpAssign } } ; else { expectTokens = new ZScriptTokenType[] { ZScriptTokenType.Semicolon, ZScriptTokenType.Comma } }; token = tokenizer.ExpectToken(expectTokens); if (token == null || !token.IsValid) { parser.ReportError("Expected ;, =, or comma, got " + ((Object)token ?? "<null>").ToString()); return; } // "static int A[] = {1, 2, 3};" if (token.Type == ZScriptTokenType.OpAssign) { // read in array data tokenizer.SkipWhitespace(); parser.SkipBlock(false); tokenizer.SkipWhitespace(); token = tokenizer.ExpectToken(ZScriptTokenType.Semicolon, ZScriptTokenType.Comma); if (token == null || !token.IsValid) { parser.ReportError("Expected ; or comma, got " + ((Object)token ?? "<null>").ToString()); return; } } } } names.Add(name); arraylens.Add(lens); if (token.Type != ZScriptTokenType.Comma) // next name { break; } } // validate modifiers here. // protected and private cannot be combined. if (bmethod) { if (modifiers.Contains("protected") && modifiers.Contains("private")) { parser.ReportError("Cannot have protected and private on the same method"); return; } // virtual and override cannot be combined. int cvirtual = modifiers.Contains("virtual") ? 1 : 0; cvirtual += modifiers.Contains("override") ? 1 : 0; cvirtual += modifiers.Contains("final") ? 1 : 0; if (cvirtual > 1) { parser.ReportError("Cannot have virtual, override and final on the same method"); return; } // meta (what the f**k is that?) probably cant be on a method if (modifiers.Contains("meta")) { parser.ReportError("Cannot have meta on a method"); return; } } // finished method or field parsing. /*for (int i = 0; i < names.Count; i++) * { * string name = names[i]; * int arraylen = arraylens[i]; * * string _args = ""; * if (args != null) _args = " (" + ZScriptTokenizer.TokensToString(args) + ")"; * else if (arraylen != -1) _args = " [" + arraylen.ToString() + "]"; * parser.LogWarning(string.Format("{0} {1} {2}{3}", string.Join(" ", modifiers.ToArray()), string.Join(", ", types.ToArray()), name, _args)); * }*/ // update 08.02.17: add user variables from ZScript actors. if (args == null && types.Count == 1) // it's a field { // we support: // - float // - int // - double // - bool string type = types[0]; UniversalType utype; object udefault = null; switch (type) { case "int": case "int8": case "int16": case "uint": case "uint8": case "uint16": utype = UniversalType.Integer; break; case "float": case "double": utype = UniversalType.Float; break; case "bool": utype = UniversalType.Boolean; break; case "string": utype = UniversalType.String; break; // todo test if class names and colors will work default: continue; // go read next field } UniversalType utype_reinterpret = utype; if (var_props.ContainsKey("$userreinterpret")) { string sp = var_props["$userreinterpret"][0].Trim().ToLowerInvariant(); switch (sp) { case "color": if (utype != UniversalType.Integer) { parser.LogWarning("Cannot use $UserReinterpret Color with non-integers"); break; } utype_reinterpret = UniversalType.Color; break; } } if (var_props.ContainsKey("$userdefaultvalue")) { string sp = var_props["$userdefaultvalue"][0]; switch (utype) { case UniversalType.String: if (sp[0] == '"' && sp[sp.Length - 1] == '"') { sp = sp.Substring(1, sp.Length - 2); } udefault = sp; break; case UniversalType.Float: double d; if (!double.TryParse(sp, out d)) { parser.LogWarning("Incorrect float default from string \"" + sp + "\""); break; } udefault = d; break; case UniversalType.Integer: int i; if (!int.TryParse(sp, out i)) { if (utype_reinterpret == UniversalType.Color) { sp = sp.ToLowerInvariant(); Rendering.PixelColor pc; if (!ZDTextParser.GetColorFromString(sp, out pc)) { parser.LogWarning("Incorrect color default from string \"" + sp + "\""); break; } udefault = pc.ToInt() & 0xFFFFFF; break; } } udefault = i; break; case UniversalType.Boolean: sp = sp.ToLowerInvariant(); if (sp == "true") { udefault = true; } else if (sp == "false") { udefault = false; } else { parser.LogWarning("Incorrect boolean default from string \"" + sp + "\""); } break; } } for (int i = 0; i < names.Count; i++) { string name = names[i]; if (arraylens[i] != null || typearraylens[0] != null) { continue; // we don't process arrays } if (!name.StartsWith("user_")) { continue; // we don't process non-user_ fields (because ZScript won't pick them up anyway) } // parent class is not guaranteed to be loaded already, so handle collisions later uservars.Add(name, utype_reinterpret); if (udefault != null) { uservar_defaults.Add(name, udefault); } } } } // parsing done, process thing arguments ParseCustomArguments(); } }
} //mxd #endregion #region ================== Constructor / Disposer // Constructor internal PatchStructure(TexturesParser parser) { // Initialize alpha = 1.0f; renderstyle = TexturePathRenderStyle.COPY; //mxd blendstyle = TexturePathBlendStyle.NONE; //mxd // There should be 3 tokens separated by 2 commas now: // Name, Width, Height // First token is the class name parser.SkipWhitespace(true); name = parser.StripTokenQuotes(parser.ReadToken(false)); //mxd. Don't skip newline if (string.IsNullOrEmpty(name)) { parser.ReportError("Expected patch name"); return; } //mxd. Skip what must be skipped skip = (name.ToUpperInvariant() == IGNORE_SPRITE); //mxd name = name.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); // Now we should find a comma if (!parser.NextTokenIs(",")) { return; //mxd } // Next is the patch width parser.SkipWhitespace(true); string tokenstr = parser.ReadToken(); if (string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, NumberStyles.Integer, CultureInfo.InvariantCulture, out offsetx)) { parser.ReportError("Expected offset in pixels"); return; } // Now we should find a comma again if (!parser.NextTokenIs(",")) { return; //mxd } // Next is the patch height parser.SkipWhitespace(true); tokenstr = parser.ReadToken(); if (string.IsNullOrEmpty(tokenstr) || !int.TryParse(tokenstr, NumberStyles.Integer, CultureInfo.InvariantCulture, out offsety)) { parser.ReportError("Expected offset in pixels"); return; } // Next token is the beginning of the texture scope. If not, then the patch info ends here. if (!parser.NextTokenIs("{", false)) { return; //mxd } // Now parse the contents of texture structure bool done = false; //mxd while (!done && parser.SkipWhitespace(true)) { string token = parser.ReadToken(); token = token.ToLowerInvariant(); switch (token) { case "flipx": flipx = true; break; case "flipy": flipy = true; break; case "alpha": if (!ReadTokenFloat(parser, token, out alpha)) { return; } alpha = General.Clamp(alpha, 0.0f, 1.0f); break; case "rotate": if (!ReadTokenInt(parser, token, out rotation)) { return; } rotation = rotation % 360; //Coalesce multiples if (rotation < 0) { rotation += 360; //Force positive } if (rotation != 0 && rotation != 90 && rotation != 180 && rotation != 270) { parser.LogWarning("Unsupported rotation (" + rotation + ") in patch \"" + name + "\""); rotation = 0; } break; case "style": //mxd string s; if (!ReadTokenString(parser, token, out s)) { return; } int index = Array.IndexOf(renderStyles, s.ToLowerInvariant()); renderstyle = index == -1 ? TexturePathRenderStyle.COPY : (TexturePathRenderStyle)index; break; case "blend": //mxd parser.SkipWhitespace(false); PixelColor color = new PixelColor(); // Blend <string color>[,<float alpha>] block? token = parser.ReadToken(false); if (!parser.ReadByte(token, ref color.r)) { if (!ZDTextParser.GetColorFromString(token, ref color)) { parser.ReportError("Unsupported patch blend definition"); return; } } // That's Blend <int r>,<int g>,<int b>[,<float alpha>] block else { if (!parser.SkipWhitespace(false) || !parser.NextTokenIs(",", false) || !parser.SkipWhitespace(false) || !parser.ReadByte(ref color.g) || !parser.NextTokenIs(",", false) || !parser.SkipWhitespace(false) || !parser.ReadByte(ref color.b)) { parser.ReportError("Unsupported patch blend definition"); return; } } // Alpha block? float blendalpha = -1f; parser.SkipWhitespace(false); if (parser.NextTokenIs(",", false)) { parser.SkipWhitespace(false); if (!ReadTokenFloat(parser, token, out blendalpha)) { parser.ReportError("Unsupported patch blend alpha value"); return; } } // Blend may never be 0 when using the Tint effect if (blendalpha > 0.0f) { color.a = (byte)General.Clamp((int)(blendalpha * 255), 1, 254); blendstyle = TexturePathBlendStyle.TINT; } else if (blendalpha < 0.0f) { color.a = 255; blendstyle = TexturePathBlendStyle.BLEND; } else { // Ignore Blend when alpha == 0 parser.LogWarning("Blend with zero alpha will be ignored by ZDoom"); break; } // Store the color blendcolor = color; break; case "}": // Patch scope ends here, // break out of this parse loop done = true; break; } } }