Beispiel #1
0
        /// <summary>
        /// Parse loopstr like "range:0,123456" to a <see cref="PsbList"/>
        /// </summary>
        /// <param name="loopStr"></param>
        /// <returns></returns>
        public static PsbList ParseLoopStr(string loopStr)
        {
            if (string.IsNullOrWhiteSpace(loopStr) || loopStr == "none")
            {
                return(null);
            }

            if (loopStr.StartsWith("range:"))
            {
                loopStr = loopStr.Substring(6);
                var     loopPoints = loopStr.Split(',');
                PsbList list       = new PsbList();
                foreach (var loopPoint in loopPoints)
                {
                    if (int.TryParse(loopPoint, out var loopInt))
                    {
                        list.Add(new PsbNumber(loopInt));
                    }
                    else if (double.TryParse(loopPoint, out var loopNum))
                    {
                        list.Add(new PsbNumber(loopNum));
                    }
                    else
                    {
                        return(null);
                    }
                }

                return(list);
            }

            return(null);
        }
        /// <summary>
        /// Combine Image texture parts
        /// </summary>
        /// <param name="psb">Image (image) type PSB</param>
        /// <returns></returns>
        public static Dictionary <string, Bitmap> CombineTachie(PSB psb)
        {
            if (psb.Type != PsbType.Tachie)
            {
                throw new NotSupportedException("PSB is not image type");
            }

            Dictionary <string, Bitmap> images = new Dictionary <string, Bitmap>();
            PsbList imageList = psb.Objects["imageList"] as PsbList;

            if (imageList == null)
            {
                return(images);
            }

            foreach (var psbValue in imageList)
            {
                var imageItem = psbValue as PsbDictionary;

                var texture = imageItem?["texture"] as PsbList;
                if (texture == null)
                {
                    continue;
                }

                var height = imageItem["height"].GetInt();
                var width  = imageItem["width"].GetInt();
                var label  = imageItem["label"].ToString();

                Bitmap img = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                using (var f = img.FastLock())
                {
                    foreach (var texObj in texture)
                    {
                        var tex = (PsbDictionary)texObj;
                        var md  = PsbResHelper.GenerateImageMetadata(tex.Children("image") as PsbDictionary);
                        md.Spec = psb.Platform;

                        var left    = tex["left"].GetInt();
                        var top     = tex["top"].GetInt();
                        var tHeight = tex["height"].GetInt();
                        var tWidth  = tex["width"].GetInt();

                        f.CopyRegion(md.ToImage(), new Rectangle(0, 0, md.Width, md.Height), new Rectangle(left, top, tWidth, tHeight));
                    }
                }

                images.Add(label, img);
            }

            return(images);
        }
Beispiel #3
0
        /// <inheritdoc cref="FindByPath(FreeMote.Psb.PsbDictionary,string)"/>
        public static IPsbValue FindByPath(this PsbList psbObj, string path)
        {
            if (psbObj == null)
            {
                return(null);
            }
            if (path.StartsWith("/"))
            {
                path = new string(path.SkipWhile(c => c == '/').ToArray());
            }

            if (path.Contains("/"))
            {
                var       pos        = path.IndexOf('/');
                var       current    = path.Substring(0, pos);
                IPsbValue currentObj = null;
                if (current == "*")
                {
                    currentObj = psbObj.FirstOrDefault();
                }

                if (current.StartsWith("[") && current.EndsWith("]") &&
                    Int32.TryParse(current.Substring(1, current.Length - 2), out var id))
                {
                    currentObj = psbObj[id];
                }

                if (pos == path.Length - 1)
                {
                    return(currentObj);
                }

                if (currentObj is PsbDictionary dictionary)
                {
                    path = path.Substring(pos);
                    return(dictionary.FindByPath(path));
                }

                if (currentObj is PsbList collection)
                {
                    path = path.Substring(pos);
                    return(collection.FindByPath(path));
                }
            }

            return(null);
        }
Beispiel #4
0
        internal IPsbValue ConvertToken(JToken token, Dictionary <string, PsbString> context)
        {
            switch (token.Type)
            {
            case JTokenType.Null:
                return(PsbNull.Null);

            case JTokenType.Integer:
                var l = token.Value <long>();
                if (l > Int32.MaxValue || l < Int32.MinValue)
                {
                    return(new PsbNumber(l));
                }
                return(new PsbNumber(token.Value <int>()));

            case JTokenType.Float:
                //var numberStr = token.Value<string>();
                var d = token.Value <double>();
                if (UseDoubleOnly)
                {
                    return(new PsbNumber(d));
                }
                var f = token.Value <float>();
                if (Math.Abs(f - d) < 1E-08)     //float //pcc: 1E-05
                //if (Math.Abs(f - d) < float.Epsilon)
                {
                    return(new PsbNumber(f));
                }
                //if (d < float.MaxValue && d > float.MinValue)
                //{
                //    return new PsbNumber(token.Value<float>());
                //}
                return(new PsbNumber(d));

            case JTokenType.Boolean:
                return(new PsbBool(token.Value <bool>()));

            case JTokenType.String:
                string str = token.Value <string>();
                if (str.StartsWith(Consts.NumberStringPrefix))
                {
                    var prefixLen = Consts.NumberStringPrefix.Length;
                    if (str.EndsWith("f"))
                    {
                        return(new PsbNumber(int.Parse(str.Substring(prefixLen, 8), NumberStyles.AllowHexSpecifier))
                        {
                            NumberType = PsbNumberType.Float
                        });
                    }
                    if (str.EndsWith("d"))
                    {
                        return(new PsbNumber(long.Parse(str.Substring(prefixLen, 16), NumberStyles.AllowHexSpecifier))
                        {
                            NumberType = PsbNumberType.Double
                        });
                    }
                    return(new PsbNumber(long.Parse(str.Substring(prefixLen), NumberStyles.AllowHexSpecifier)));
                }
                if (str.StartsWith(Consts.ResourceIdentifier))
                {
                    return(new PsbResource(uint.Parse(str.Replace(Consts.ResourceIdentifier, ""))));
                }
                var psbStr = new PsbString(str, (uint)context.Count);
                if (context.ContainsKey(str))
                {
                    return(context[str]);
                }
                else
                {
                    context.Add(str, psbStr);
                }
                return(psbStr);

            case JTokenType.Array:
                var array      = (JArray)token;
                var collection = new PsbList(array.Count);
                foreach (var val in array)
                {
                    var o = ConvertToken(val, context);
                    if (o is IPsbChild c)
                    {
                        c.Parent = collection;
                    }
                    if (o is IPsbSingleton s)
                    {
                        s.Parents.Add(collection);
                    }
                    collection.Add(o);
                }
                return(collection);

            case JTokenType.Object:
                var obj        = (JObject)token;
                var dictionary = new PsbDictionary(obj.Count);
                foreach (var val in obj)
                {
                    var o = ConvertToken(val.Value, context);
                    if (o is IPsbChild c)
                    {
                        c.Parent = dictionary;
                    }
                    if (o is IPsbSingleton s)
                    {
                        s.Parents.Add(dictionary);
                    }
                    dictionary.Add(val.Key, o);
                }
                return(dictionary);

            default:
                throw new FormatException("Unsupported json element");
            }
        }