예제 #1
0
        public ModData(Manifest mod, InstalledMods mods, bool useLoadScreen = false)
        {
            Languages = new string[0];

            // Take a local copy of the manifest
            Manifest       = new Manifest(mod.Id, mod.Package);
            ObjectCreator  = new ObjectCreator(Manifest, mods);
            PackageLoaders = ObjectCreator.GetLoaders <IPackageLoader>(Manifest.PackageFormats, "package");

            ModFiles = new FS(mods, PackageLoaders);
            ModFiles.LoadFromManifest(Manifest);
            Manifest.LoadCustomData(ObjectCreator);

            if (useLoadScreen)
            {
                LoadScreen = ObjectCreator.CreateObject <ILoadScreen>(Manifest.LoadScreen.Value);
                LoadScreen.Init(this, Manifest.LoadScreen.ToDictionary(my => my.Value));
                LoadScreen.Display();
            }

            WidgetLoader = new WidgetLoader(this);
            MapCache     = new MapCache(this);

            SoundLoaders  = ObjectCreator.GetLoaders <ISoundLoader>(Manifest.SoundFormats, "sound");
            SpriteLoaders = ObjectCreator.GetLoaders <ISpriteLoader>(Manifest.SpriteFormats, "sprite");

            var sequenceFormat = Manifest.Get <SpriteSequenceFormat>();
            var sequenceLoader = ObjectCreator.FindType(sequenceFormat.Type + "Loader");
            var ctor           = sequenceLoader != null?sequenceLoader.GetConstructor(new[] { typeof(ModData) }) : null;

            if (sequenceLoader == null || !sequenceLoader.GetInterfaces().Contains(typeof(ISpriteSequenceLoader)) || ctor == null)
            {
                throw new InvalidOperationException("Unable to find a sequence loader for type '{0}'.".F(sequenceFormat.Type));
            }

            SpriteSequenceLoader = (ISpriteSequenceLoader)ctor.Invoke(new[] { this });
            SpriteSequenceLoader.OnMissingSpriteError = s => Log.Write("debug", s);

            defaultRules    = Exts.Lazy(() => Ruleset.LoadDefaults(this));
            defaultTileSets = Exts.Lazy(() =>
            {
                var items = new Dictionary <string, TileSet>();

                foreach (var file in Manifest.TileSets)
                {
                    var t = new TileSet(DefaultFileSystem, file);
                    items.Add(t.Id, t);
                }

                return((IReadOnlyDictionary <string, TileSet>)(new ReadOnlyDictionary <string, TileSet>(items)));
            });

            defaultSequences = Exts.Lazy(() =>
            {
                var items = DefaultTileSets.ToDictionary(t => t.Key, t => new SequenceProvider(DefaultFileSystem, this, t.Value, null));
                return((IReadOnlyDictionary <string, SequenceProvider>)(new ReadOnlyDictionary <string, SequenceProvider>(items)));
            });

            initialThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
        }
예제 #2
0
        public ModData(string mod, bool useLoadScreen = false)
        {
            Languages     = new string[0];
            Manifest      = new Manifest(mod);
            ObjectCreator = new ObjectCreator(Manifest);
            Manifest.LoadCustomData(ObjectCreator);

            if (useLoadScreen)
            {
                LoadScreen = ObjectCreator.CreateObject <ILoadScreen>(Manifest.LoadScreen.Value);
                LoadScreen.Init(Manifest, Manifest.LoadScreen.ToDictionary(my => my.Value));
                LoadScreen.Display();
            }

            WidgetLoader = new WidgetLoader(this);
            RulesetCache = new RulesetCache(this);
            RulesetCache.LoadingProgress += HandleLoadingProgress;
            MapCache = new MapCache(this);

            var spriteLoaders = new List <ISpriteLoader>();

            foreach (var format in Manifest.SpriteFormats)
            {
                var loader = ObjectCreator.FindType(format + "Loader");
                if (loader == null || !loader.GetInterfaces().Contains(typeof(ISpriteLoader)))
                {
                    throw new InvalidOperationException("Unable to find a sprite loader for type '{0}'.".F(format));
                }

                spriteLoaders.Add((ISpriteLoader)ObjectCreator.CreateBasic(loader));
            }

            SpriteLoaders = spriteLoaders.ToArray();

            var sequenceFormat = Manifest.Get <SpriteSequenceFormat>();
            var sequenceLoader = ObjectCreator.FindType(sequenceFormat.Type + "Loader");
            var ctor           = sequenceLoader != null?sequenceLoader.GetConstructor(new[] { typeof(ModData) }) : null;

            if (sequenceLoader == null || !sequenceLoader.GetInterfaces().Contains(typeof(ISpriteSequenceLoader)) || ctor == null)
            {
                throw new InvalidOperationException("Unable to find a sequence loader for type '{0}'.".F(sequenceFormat.Type));
            }

            SpriteSequenceLoader = (ISpriteSequenceLoader)ctor.Invoke(new[] { this });
            SpriteSequenceLoader.OnMissingSpriteError = s => Log.Write("debug", s);

            // HACK: Mount only local folders so we have a half-working environment for the asset installer
            GlobalFileSystem.UnmountAll();
            foreach (var dir in Manifest.Folders)
            {
                GlobalFileSystem.Mount(dir);
            }

            defaultRules = Exts.Lazy(() => RulesetCache.LoadDefaultRules());

            initialThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
        }
예제 #3
0
파일: ModData.cs 프로젝트: huwpascoe/OpenRA
        public ModData(string mod, bool useLoadScreen = false)
        {
            Languages = new string[0];
            Manifest  = new Manifest(mod);

            // Allow mods to load types from the core Game assembly, and any additional assemblies they specify.
            var assemblies =
                new[] { typeof(Game).Assembly }.Concat(
                Manifest.Assemblies.Select(path => Assembly.LoadFrom(Platform.ResolvePath(path))));

            ObjectCreator = new ObjectCreator(assemblies);
            Manifest.LoadCustomData(ObjectCreator);

            if (useLoadScreen)
            {
                LoadScreen = ObjectCreator.CreateObject <ILoadScreen>(Manifest.LoadScreen.Value);
                LoadScreen.Init(Manifest, Manifest.LoadScreen.ToDictionary(my => my.Value));
                LoadScreen.Display();
            }

            WidgetLoader = new WidgetLoader(this);
            RulesetCache = new RulesetCache(this);
            RulesetCache.LoadingProgress += HandleLoadingProgress;
            MapCache = new MapCache(this);

            SoundLoaders  = GetLoaders <ISoundLoader>(Manifest.SoundFormats, "sound");
            SpriteLoaders = GetLoaders <ISpriteLoader>(Manifest.SpriteFormats, "sprite");

            var sequenceFormat = Manifest.Get <SpriteSequenceFormat>();
            var sequenceLoader = ObjectCreator.FindType(sequenceFormat.Type + "Loader");
            var ctor           = sequenceLoader != null?sequenceLoader.GetConstructor(new[] { typeof(ModData) }) : null;

            if (sequenceLoader == null || !sequenceLoader.GetInterfaces().Contains(typeof(ISpriteSequenceLoader)) || ctor == null)
            {
                throw new InvalidOperationException("Unable to find a sequence loader for type '{0}'.".F(sequenceFormat.Type));
            }

            SpriteSequenceLoader = (ISpriteSequenceLoader)ctor.Invoke(new[] { this });
            SpriteSequenceLoader.OnMissingSpriteError = s => Log.Write("debug", s);

            defaultRules = Exts.Lazy(() => RulesetCache.Load());

            initialThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
        }
예제 #4
0
파일: ModData.cs 프로젝트: Roger-luo/OpenRA
		public ModData(string mod, bool useLoadScreen = false)
		{
			Languages = new string[0];
			Manifest = new Manifest(mod);
			ObjectCreator = new ObjectCreator(Manifest);
			Manifest.LoadCustomData(ObjectCreator);

			if (useLoadScreen)
			{
				LoadScreen = ObjectCreator.CreateObject<ILoadScreen>(Manifest.LoadScreen.Value);
				LoadScreen.Init(Manifest, Manifest.LoadScreen.ToDictionary(my => my.Value));
				LoadScreen.Display();
			}

			WidgetLoader = new WidgetLoader(this);
			RulesetCache = new RulesetCache(this);
			RulesetCache.LoadingProgress += HandleLoadingProgress;
			MapCache = new MapCache(this);

			var spriteLoaders = new List<ISpriteLoader>();
			foreach (var format in Manifest.SpriteFormats)
			{
				var loader = ObjectCreator.FindType(format + "Loader");
				if (loader == null || !loader.GetInterfaces().Contains(typeof(ISpriteLoader)))
					throw new InvalidOperationException("Unable to find a sprite loader for type '{0}'.".F(format));

				spriteLoaders.Add((ISpriteLoader)ObjectCreator.CreateBasic(loader));
			}

			SpriteLoaders = spriteLoaders.ToArray();

			var sequenceFormat = Manifest.Get<SpriteSequenceFormat>();
			var sequenceLoader = ObjectCreator.FindType(sequenceFormat.Type + "Loader");
			var ctor = sequenceLoader != null ? sequenceLoader.GetConstructor(new[] { typeof(ModData) }) : null;
			if (sequenceLoader == null || !sequenceLoader.GetInterfaces().Contains(typeof(ISpriteSequenceLoader)) || ctor == null)
				throw new InvalidOperationException("Unable to find a sequence loader for type '{0}'.".F(sequenceFormat.Type));

			SpriteSequenceLoader = (ISpriteSequenceLoader)ctor.Invoke(new[] { this });
			SpriteSequenceLoader.OnMissingSpriteError = s => Log.Write("debug", s);

			defaultRules = Exts.Lazy(() => RulesetCache.Load());

			initialThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
		}
예제 #5
0
        public DefaultSpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
        {
            Name   = animation;
            Loader = loader;
            //Console.WriteLine("Sequence:" + sequence + "  animation:" + animation);
            var d = info.ToDictionary();

            try
            {
                Start                 = LoadField(d, "Start", 0);
                ShadowStart           = LoadField(d, "ShadowStart", -1);
                ShadowZOffset         = LoadField(d, "ShadowZOffset", DefaultShadowSpriteZOffset).Length;
                ZOffset               = LoadField(d, "ZOffset", WDist.Zero).Length;
                ZRamp                 = LoadField(d, "ZRamp", 0);
                Tick                  = LoadField(d, "Tick", 40);
                transpose             = LoadField(d, "Transpose", false);
                Frames                = LoadField <int[]>(d, "Frames", null);
                useClassicFacingFudge = LoadField(d, "UseClassicFacingFudge", false);

                var flipX = LoadField(d, "FlipX", false);
                var flipY = LoadField(d, "FlipY", false);

                Facings = LoadField(d, "Facings", 1);
                if (Facings < 0)
                {
                    reverseFacings = true;
                    Facings        = -Facings;
                }
                if (useClassicFacingFudge && Facings != 32)
                {
                    throw new InvalidDataException("{0}: Sequence {1}.{2}: UseClassicFacingFudge is only valid for 32 facings".F(info.Nodes[0].Location, sequence, animation));
                }

                var offset    = LoadField(d, "Offset", Vector3.Zero);
                var blendmode = LoadField(d, "BlendMode", BlendMode.Alpha);

                MiniYaml combine;
                if (d.TryGetValue("Combine", out combine))
                {
                    var combined = Enumerable.Empty <Sprite>();
                    foreach (var sub in combine.Nodes)
                    {
                        var sd = sub.Value.ToDictionary();
                        //Allow per-sprite offset,flipping,start,and length
                        var subStart  = LoadField(sd, "Start", 0);
                        var subOffset = LoadField(sd, "Offset", Vector3.Zero);
                        var subFlipX  = LoadField(sd, "FlipX", false);
                        var subFlipY  = LoadField(sd, "FlipY", false);

                        var subSrc = GetSpriteSrc(modData, tileSet, sequence, animation, sub.Key, sd);

                        var subSprites = cache[subSrc].Select(s => new Sprite(s.Sheet, FlipRectangle(s.Bounds, subFlipX, subFlipY),
                                                                              ZRamp, new Vector3(subFlipX?-s.Offset.X:s.Offset.X, subFlipY?-s.Offset.Y:s.Offset.Y, s.Offset.Z) + subOffset + offset, s.Channel, blendmode));

                        var      subLength = 0;
                        MiniYaml subLengthYaml;
                        if (sd.TryGetValue("Length", out subLengthYaml) && subLengthYaml.Value == "*")
                        {
                            subLength = subSprites.Count() - subStart;
                        }
                        else
                        {
                            subLength = LoadField(sd, "Length", 1);
                        }

                        combined = combined.Concat(subSprites.Skip(subStart).Take(subLength));
                    }

                    sprites = combined.ToArray();
                }
                else
                {
                    //Apply offset to each sprite in the sequence
                    //Different sequences may apply different offsets to the same frame.

                    //对序列中的每个子画面应用偏移,不同的序列可以对同一帧应用不同的偏移
                    var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d);
                    sprites = cache[src].Select(s => new Sprite(s.Sheet, FlipRectangle(s.Bounds, flipX, flipY), ZRamp,
                                                                new Vector3(flipX ? -s.Offset.X : s.Offset.X, flipY ? -s.Offset.Y : s.Offset.Y, s.Offset.Z) + offset, s.Channel, blendmode)).ToArray();
                }

                var depthSprite = LoadField <string>(d, "DepthSprite", null);
                if (!string.IsNullOrEmpty(depthSprite))
                {
                    //Console.WriteLine("DepthSprite:" + depthSprite);
                    var depthSpriteFrame = LoadField(d, "DepthSpriteFrame", 0);
                    var depthOffset      = LoadField(d, "DepthSpriteOffset", Vector2.Zero);
                    var depthSprites     = cache.AllCached(depthSprite).Select(s => s[depthSpriteFrame]);

                    sprites = sprites.Select(s =>
                    {
                        //The depth sprite must be live on the same sheet as the main sprite
                        var ds = depthSprites.FirstOrDefault(dss => dss.Sheet == s.Sheet);
                        if (ds == null)
                        {
                            //The sequence has probably overflowed onto a new sheet.
                            //Allocating a new depth sprite on this sheet will almost certainly work
                            //该序列可能已经溢出到新的Sheet上,
                            ds           = cache.Reload(depthSprite)[depthSpriteFrame];
                            depthSprites = cache.AllCached(depthSprite).Select(ss => ss[depthSpriteFrame]);

                            if (ds.Sheet != s.Sheet)
                            {
                                throw new SheetOverflowException("Cross-sheet depth sprite reference:{0}.{1}: {2}");
                            }
                        }

                        var cw = (ds.Bounds.Left + ds.Bounds.Right) / 2 + (int)(s.Offset.X + depthOffset.X);
                        var ch = (ds.Bounds.Top + ds.Bounds.Bottom) / 2 + (int)(s.Offset.Y + depthOffset.Y);

                        var w = s.Bounds.Width / 2;
                        var h = s.Bounds.Height / 2;

                        var r = Rectangle.FromLTRB(cw - w, ch - h, cw + w, ch + h);
                        return(new SpriteWithSecondaryData(s, r, ds.Channel));
                    }).ToArray();
                }

                MiniYaml length;
                if (d.TryGetValue("Length", out length) && length.Value == "*")
                {
                    Length = sprites.Length - Start;
                }
                else
                {
                    Length = LoadField(d, "Length", 1);
                }

                //Plays the animation forwards,and then in reverse
                if (LoadField(d, "Reverses", false))
                {
                    var frames = Frames ?? Exts.MakeArray(Length, i => Start + i);
                    Frames = frames.Concat(frames.Skip(1).Take(frames.Length - 2).Reverse()).ToArray();
                    Length = 2 * Length - 2;
                }

                Stride = LoadField(d, "Stride", Length);

                if (Length > Stride)
                {
                    throw new InvalidOperationException("{0}:Sequence {1}.{2}: Length must be <= Stride".F(info.Nodes[0].Location, sequence, animation));
                }

                if (Frames != null && Length > Frames.Length)
                {
                    throw new InvalidOperationException("{0}:Sequence {1}.{2}: Length must be <=Frames.Length".F(info.Nodes[0].Location, sequence, animation));
                }
                if (Start < 0 || Start + Facings * Stride > sprites.Length)
                {
                    throw new InvalidOperationException("{5}: Sequence {0}.{1} uses frames [{2}...{3}],but only 0..{4} actually exist".F(sequence, animation, Start, Start + Facings * Stride - 1, sprites.Length - 1, info.Nodes[0].Location));
                }

                if (ShadowStart + Facings * Stride > sprites.Length)
                {
                    throw new InvalidOperationException("{5}:Sequence {0}.{1}'s shadow frames use frames [{2}...{3}],but only [0..{4}] actually exist".F(sequence, animation, ShadowStart, ShadowStart + Facings * Stride - 1, sprites.Length - 1, info.Nodes[0].Location));
                }

                var boundSprites = SpriteBounds(sprites, Frames, Start, Facings, Length);
                if (ShadowStart > 0)
                {
                    boundSprites = boundSprites.Concat(SpriteBounds(sprites, Frames, ShadowStart, Facings, Length));
                }

                if (boundSprites.Any())
                {
                    Bounds = boundSprites.First();
                    foreach (var b in boundSprites.Skip(1))
                    {
                        Bounds = Rectangle.Union(Bounds, b);
                    }
                }
            }
            catch (FormatException f)
            {
                throw new FormatException("Failed to parse sequences for {0}.{1} at {2}:\n{3}".F(sequence, animation, info.Nodes[0].Location, f));
            }
        }
예제 #6
0
        public DefaultSpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
        {
            this.sequence = sequence;
            Name          = animation;
            Loader        = loader;
            var d = info.ToDictionary();

            try
            {
                Start         = LoadField(d, "Start", 0);
                ShadowStart   = LoadField(d, "ShadowStart", -1);
                ShadowZOffset = LoadField(d, "ShadowZOffset", DefaultShadowSpriteZOffset).Length;
                ZOffset       = LoadField(d, "ZOffset", WDist.Zero).Length;
                ZRamp         = LoadField(d, "ZRamp", 0);
                Tick          = LoadField(d, "Tick", 40);
                transpose     = LoadField(d, "Transpose", false);
                Frames        = LoadField <int[]>(d, "Frames", null);

                var flipX = LoadField(d, "FlipX", false);
                var flipY = LoadField(d, "FlipY", false);

                Facings = LoadField(d, "Facings", 1);
                if (Facings < 0)
                {
                    reverseFacings = true;
                    Facings        = -Facings;
                }

                var offset    = LoadField(d, "Offset", float3.Zero);
                var blendMode = LoadField(d, "BlendMode", BlendMode.Alpha);

                Func <int, IEnumerable <int> > getUsedFrames = frameCount =>
                {
                    MiniYaml length;
                    if (d.TryGetValue("Length", out length) && length.Value == "*")
                    {
                        Length = Frames != null ? Frames.Length : frameCount - Start;
                    }
                    else
                    {
                        Length = LoadField(d, "Length", 1);
                    }

                    // Plays the animation forwards, and then in reverse
                    if (LoadField(d, "Reverses", false))
                    {
                        var frames = Frames != null?Frames.Skip(Start).Take(Length).ToArray() : Exts.MakeArray(Length, i => Start + i);

                        Frames = frames.Concat(frames.Skip(1).Take(Length - 2).Reverse()).ToArray();
                        Length = 2 * Length - 2;
                        Start  = 0;
                    }

                    Stride = LoadField(d, "Stride", Length);

                    if (Length > Stride)
                    {
                        throw new InvalidOperationException(
                                  "{0}: Sequence {1}.{2}: Length must be <= stride"
                                  .F(info.Nodes[0].Location, sequence, animation));
                    }

                    if (Frames != null && Length > Frames.Length)
                    {
                        throw new InvalidOperationException(
                                  "{0}: Sequence {1}.{2}: Length must be <= Frames.Length"
                                  .F(info.Nodes[0].Location, sequence, animation));
                    }

                    var end = Start + (Facings - 1) * Stride + Length - 1;
                    if (Frames != null)
                    {
                        foreach (var f in Frames)
                        {
                            if (f < 0 || f >= frameCount)
                            {
                                throw new InvalidOperationException(
                                          "{5}: Sequence {0}.{1} defines a Frames override that references frame {4}, but only [{2}..{3}] actually exist"
                                          .F(sequence, animation, Start, end, f, info.Nodes[0].Location));
                            }
                        }

                        if (Start < 0 || end >= Frames.Length)
                        {
                            throw new InvalidOperationException(
                                      "{5}: Sequence {0}.{1} uses indices [{2}..{3}] of the Frames list, but only {4} frames are defined"
                                      .F(sequence, animation, Start, end, Frames.Length, info.Nodes[0].Location));
                        }
                    }
                    else if (Start < 0 || end >= frameCount)
                    {
                        throw new InvalidOperationException(
                                  "{5}: Sequence {0}.{1} uses frames [{2}..{3}], but only 0..{4} actually exist"
                                  .F(sequence, animation, Start, end, frameCount - 1, info.Nodes[0].Location));
                    }

                    if (ShadowStart >= 0 && ShadowStart + (Facings - 1) * Stride + Length > frameCount)
                    {
                        throw new InvalidOperationException(
                                  "{5}: Sequence {0}.{1}'s shadow frames use frames [{2}..{3}], but only [0..{4}] actually exist"
                                  .F(sequence, animation, ShadowStart, ShadowStart + (Facings - 1) * Stride + Length - 1, frameCount - 1,
                                     info.Nodes[0].Location));
                    }

                    var usedFrames = new List <int>();
                    for (var facing = 0; facing < Facings; facing++)
                    {
                        for (var frame = 0; frame < Length; frame++)
                        {
                            var i = transpose ? (frame % Length) * Facings + facing :
                                    (facing * Stride) + (frame % Length);

                            usedFrames.Add(Frames != null ? Frames[i] : Start + i);
                        }
                    }

                    if (ShadowStart >= 0)
                    {
                        return(usedFrames.Concat(usedFrames.Select(i => i + ShadowStart - Start)));
                    }

                    return(usedFrames);
                };

                MiniYaml combine;
                if (d.TryGetValue("Combine", out combine))
                {
                    var combined = Enumerable.Empty <Sprite>();
                    foreach (var sub in combine.Nodes)
                    {
                        var sd = sub.Value.ToDictionary();

                        // Allow per-sprite offset, flipping, start, and length
                        var subStart  = LoadField(sd, "Start", 0);
                        var subOffset = LoadField(sd, "Offset", float3.Zero);
                        var subFlipX  = LoadField(sd, "FlipX", false);
                        var subFlipY  = LoadField(sd, "FlipY", false);
                        var subFrames = LoadField <int[]>(sd, "Frames", null);
                        var subLength = 0;

                        Func <int, IEnumerable <int> > subGetUsedFrames = subFrameCount =>
                        {
                            MiniYaml subLengthYaml;
                            if (sd.TryGetValue("Length", out subLengthYaml) && subLengthYaml.Value == "*")
                            {
                                subLength = subFrames != null ? subFrames.Length : subFrameCount - subStart;
                            }
                            else
                            {
                                subLength = LoadField(sd, "Length", 1);
                            }

                            return(subFrames != null?subFrames.Skip(subStart).Take(subLength) : Enumerable.Range(subStart, subLength));
                        };

                        var subSrc     = GetSpriteSrc(modData, tileSet, sequence, animation, sub.Key, sd);
                        var subSprites = cache[subSrc, subGetUsedFrames].Select(
                            s => s != null ? new Sprite(s.Sheet,
                                                        FlipRectangle(s.Bounds, subFlipX, subFlipY), ZRamp,
                                                        new float3(subFlipX ? -s.Offset.X : s.Offset.X, subFlipY ? -s.Offset.Y : s.Offset.Y, s.Offset.Z) + subOffset + offset,
                                                        s.Channel, blendMode) : null).ToList();

                        var frames = subFrames != null?subFrames.Skip(subStart).Take(subLength).ToArray() : Exts.MakeArray(subLength, i => subStart + i);

                        combined = combined.Concat(frames.Select(i => subSprites[i]));
                    }

                    sprites = combined.ToArray();
                    getUsedFrames(sprites.Length);
                }
                else
                {
                    // Apply offset to each sprite in the sequence
                    // Different sequences may apply different offsets to the same frame
                    var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d);
                    sprites = cache[src, getUsedFrames].Select(
                        s => s != null ? new Sprite(s.Sheet,
                                                    FlipRectangle(s.Bounds, flipX, flipY), ZRamp,
                                                    new float3(flipX ? -s.Offset.X : s.Offset.X, flipY ? -s.Offset.Y : s.Offset.Y, s.Offset.Z) + offset,
                                                    s.Channel, blendMode) : null).ToArray();
                }

                var depthSprite = LoadField <string>(d, "DepthSprite", null);
                if (!string.IsNullOrEmpty(depthSprite))
                {
                    var depthSpriteFrame = LoadField(d, "DepthSpriteFrame", 0);
                    var depthOffset      = LoadField(d, "DepthSpriteOffset", float2.Zero);
                    Func <int, IEnumerable <int> > getDepthFrame = _ => new int[] { depthSpriteFrame };
                    var ds = cache[depthSprite, getDepthFrame][depthSpriteFrame];

                    sprites = sprites.Select(s =>
                    {
                        if (s == null)
                        {
                            return(null);
                        }

                        var cw = (ds.Bounds.Left + ds.Bounds.Right) / 2 + (int)(s.Offset.X + depthOffset.X);
                        var ch = (ds.Bounds.Top + ds.Bounds.Bottom) / 2 + (int)(s.Offset.Y + depthOffset.Y);
                        var w  = s.Bounds.Width / 2;
                        var h  = s.Bounds.Height / 2;

                        var r = Rectangle.FromLTRB(cw - w, ch - h, cw + w, ch + h);
                        return(new SpriteWithSecondaryData(s, ds.Sheet, r, ds.Channel));
                    }).ToArray();
                }

                var exportPalette = LoadField <string>(d, "EmbeddedPalette", null);
                if (exportPalette != null)
                {
                    var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d);

                    var metadata = cache.FrameMetadata(src);
                    var i        = Frames != null ? Frames[0] : Start;
                    var palettes = metadata != null?metadata.GetOrDefault <EmbeddedSpritePalette>() : null;

                    if (palettes == null || !palettes.TryGetPaletteForFrame(i, out EmbeddedPalette))
                    {
                        throw new YamlException("Cannot export palettes from {0}: frame {1} does not define an embedded palette".F(src, i));
                    }
                }

                var boundSprites = SpriteBounds(sprites, Frames, Start, Facings, Length, Stride, transpose);
                if (ShadowStart > 0)
                {
                    boundSprites = boundSprites.Concat(SpriteBounds(sprites, Frames, ShadowStart, Facings, Length, Stride, transpose));
                }

                Bounds = boundSprites.Union();
            }
            catch (FormatException f)
            {
                throw new FormatException("Failed to parse sequences for {0}.{1} at {2}:\n{3}".F(sequence, animation, info.Nodes[0].Location, f));
            }
        }
예제 #7
0
 public Earth2140SpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
     : base(modData, tileSet, cache, loader, sequence, animation, FlipFacings(info))
 {
 }
 public TilesetSpecificSpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
     : base(modData, tileSet, cache, loader, sequence, animation, info)
 {
 }
예제 #9
0
        public DefaultSpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
        {
            Name   = animation;
            Loader = loader;
            var d = info.ToDictionary();

            try
            {
                Start                 = LoadField(d, "Start", 0);
                ShadowStart           = LoadField(d, "ShadowStart", -1);
                ShadowZOffset         = LoadField(d, "ShadowZOffset", DefaultShadowSpriteZOffset).Length;
                ZOffset               = LoadField(d, "ZOffset", WDist.Zero).Length;
                ZRamp                 = LoadField(d, "ZRamp", 0);
                Tick                  = LoadField(d, "Tick", 40);
                transpose             = LoadField(d, "Transpose", false);
                Frames                = LoadField <int[]>(d, "Frames", null);
                useClassicFacingFudge = LoadField(d, "UseClassicFacingFudge", false);

                var flipX = LoadField(d, "FlipX", false);
                var flipY = LoadField(d, "FlipY", false);

                Facings = LoadField(d, "Facings", 1);
                if (Facings < 0)
                {
                    reverseFacings = true;
                    Facings        = -Facings;
                }

                if (useClassicFacingFudge && Facings != 32)
                {
                    throw new InvalidOperationException(
                              "{0}: Sequence {1}.{2}: UseClassicFacingFudge is only valid for 32 facings"
                              .F(info.Nodes[0].Location, sequence, animation));
                }

                var offset    = LoadField(d, "Offset", float3.Zero);
                var blendMode = LoadField(d, "BlendMode", BlendMode.Alpha);

                MiniYaml combine;
                if (d.TryGetValue("Combine", out combine))
                {
                    var combined = Enumerable.Empty <Sprite>();
                    foreach (var sub in combine.Nodes)
                    {
                        var sd = sub.Value.ToDictionary();

                        // Allow per-sprite offset, flipping, start, and length
                        var subStart  = LoadField(sd, "Start", 0);
                        var subOffset = LoadField(sd, "Offset", float2.Zero);
                        var subFlipX  = LoadField(sd, "FlipX", false);
                        var subFlipY  = LoadField(sd, "FlipY", false);

                        var subSrc     = GetSpriteSrc(modData, tileSet, sequence, animation, sub.Key, sd);
                        var subSprites = cache[subSrc].Select(
                            s => new Sprite(s.Sheet,
                                            FlipRectangle(s.Bounds, subFlipX, subFlipY), ZRamp,
                                            new float2(subFlipX ? -s.Offset.X : s.Offset.X, subFlipY ? -s.Offset.Y : s.Offset.Y) + subOffset + offset,
                                            s.Channel, blendMode));

                        var      subLength = 0;
                        MiniYaml subLengthYaml;
                        if (sd.TryGetValue("Length", out subLengthYaml) && subLengthYaml.Value == "*")
                        {
                            subLength = subSprites.Count() - subStart;
                        }
                        else
                        {
                            subLength = LoadField(sd, "Length", 1);
                        }

                        combined = combined.Concat(subSprites.Skip(subStart).Take(subLength));
                    }

                    sprites = combined.ToArray();
                }
                else
                {
                    // Apply offset to each sprite in the sequence
                    // Different sequences may apply different offsets to the same frame
                    var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d);
                    sprites = cache[src].Select(
                        s => new Sprite(s.Sheet,
                                        FlipRectangle(s.Bounds, flipX, flipY), ZRamp,
                                        new float2(flipX ? -s.Offset.X : s.Offset.X, flipY ? -s.Offset.Y : s.Offset.Y) + offset,
                                        s.Channel, blendMode)).ToArray();
                }

                MiniYaml length;
                if (d.TryGetValue("Length", out length) && length.Value == "*")
                {
                    Length = sprites.Length - Start;
                }
                else
                {
                    Length = LoadField(d, "Length", 1);
                }

                // Plays the animation forwards, and then in reverse
                if (LoadField(d, "Reverses", false))
                {
                    var frames = Frames ?? Exts.MakeArray(Length, i => Start + i);
                    Frames = frames.Concat(frames.Skip(1).Take(frames.Length - 2).Reverse()).ToArray();
                    Length = 2 * Length - 2;
                }

                Stride = LoadField(d, "Stride", Length);

                if (Length > Stride)
                {
                    throw new InvalidOperationException(
                              "{0}: Sequence {1}.{2}: Length must be <= stride"
                              .F(info.Nodes[0].Location, sequence, animation));
                }

                if (Start < 0 || Start + Facings * Stride > sprites.Length)
                {
                    throw new InvalidOperationException(
                              "{5}: Sequence {0}.{1} uses frames [{2}..{3}], but only 0..{4} actually exist"
                              .F(sequence, animation, Start, Start + Facings * Stride - 1, sprites.Length - 1,
                                 info.Nodes[0].Location));
                }

                if (ShadowStart + Facings * Stride > sprites.Length)
                {
                    throw new InvalidOperationException(
                              "{5}: Sequence {0}.{1}'s shadow frames use frames [{2}..{3}], but only [0..{4}] actually exist"
                              .F(sequence, animation, ShadowStart, ShadowStart + Facings * Stride - 1, sprites.Length - 1,
                                 info.Nodes[0].Location));
                }
            }
            catch (FormatException f)
            {
                throw new FormatException("Failed to parse sequences for {0}.{1} at {2}:\n{3}".F(sequence, animation, info.Nodes[0].Location, f));
            }
        }
예제 #10
0
        public ClassicSpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
            : base(modData, tileSet, cache, loader, sequence, animation, info)
        {
            var d = info.ToDictionary();

            useClassicFacings = LoadField(d, "UseClassicFacings", false);

            if (useClassicFacings && Facings != 32)
            {
                throw new InvalidOperationException(
                          "{0}: Sequence {1}.{2}: UseClassicFacings is only valid for 32 facings"
                          .F(info.Nodes[0].Location, sequence, animation));
            }
        }
예제 #11
0
        public DefaultSpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
        {
            Name   = animation;
            Loader = loader;
            var d = info.ToDictionary();

            try
            {
                Start         = LoadField <int>(d, "Start", 0);
                ShadowStart   = LoadField <int>(d, "ShadowStart", -1);
                ShadowZOffset = LoadField <WRange>(d, "ShadowZOffset", DefaultShadowSpriteZOffset).Range;
                ZOffset       = LoadField <WRange>(d, "ZOffset", WRange.Zero).Range;
                Tick          = LoadField <int>(d, "Tick", 40);
                transpose     = LoadField <bool>(d, "Transpose", false);
                Frames        = LoadField <int[]>(d, "Frames", null);

                Facings = LoadField <int>(d, "Facings", 1);
                if (Facings < 0)
                {
                    reverseFacings = true;
                    Facings        = -Facings;
                }

                var offset    = LoadField <float2>(d, "Offset", float2.Zero);
                var blendMode = LoadField <BlendMode>(d, "BlendMode", BlendMode.Alpha);

                // Apply offset to each sprite in the sequence
                // Different sequences may apply different offsets to the same frame
                var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d);
                sprites = cache[src].Select(
                    s => new Sprite(s.Sheet, s.Bounds, s.Offset + offset, s.Channel, blendMode)).ToArray();

                MiniYaml length;
                if (d.TryGetValue("Length", out length) && length.Value == "*")
                {
                    Length = sprites.Length - Start;
                }
                else
                {
                    Length = LoadField <int>(d, "Length", 1);
                }

                // Plays the animation forwards, and then in reverse
                if (LoadField <bool>(d, "Reverses", false))
                {
                    var frames = Frames ?? Exts.MakeArray(Length, i => Start + i);
                    Frames = frames.Concat(frames.Skip(1).Take(frames.Length - 2).Reverse()).ToArray();
                    Length = 2 * Length - 2;
                }

                Stride = LoadField <int>(d, "Stride", Length);

                if (Length > Stride)
                {
                    throw new InvalidOperationException(
                              "{0}: Sequence {1}.{2}: Length must be <= stride"
                              .F(info.Nodes[0].Location, sequence, animation));
                }

                if (Start < 0 || Start + Facings * Stride > sprites.Length)
                {
                    throw new InvalidOperationException(
                              "{6}: Sequence {0}.{1} uses frames [{2}..{3}] of SHP `{4}`, but only 0..{5} actually exist"
                              .F(sequence, animation, Start, Start + Facings * Stride - 1, src, sprites.Length - 1,
                                 info.Nodes[0].Location));
                }

                if (ShadowStart + Facings * Stride > sprites.Length)
                {
                    throw new InvalidOperationException(
                              "{6}: Sequence {0}.{1}'s shadow frames use frames [{2}..{3}] of SHP `{4}`, but only [0..{5}] actually exist"
                              .F(sequence, animation, ShadowStart, ShadowStart + Facings * Stride - 1, src, sprites.Length - 1,
                                 info.Nodes[0].Location));
                }
            }
            catch (FormatException f)
            {
                throw new FormatException("Failed to parse sequences for {0}.{1} at {2}:\n{3}".F(sequence, animation, info.Nodes[0].Location, f));
            }
        }
예제 #12
0
        public ModData(Manifest mod, InstalledMods mods, bool useLoadScreen = false)
        {
            Languages = Array.Empty <string>();

            // Take a local copy of the manifest
            Manifest       = new Manifest(mod.Id, mod.Package);
            ObjectCreator  = new ObjectCreator(Manifest, mods);
            PackageLoaders = ObjectCreator.GetLoaders <IPackageLoader>(Manifest.PackageFormats, "package");

            ModFiles = new FS(mod.Id, mods, PackageLoaders);
            ModFiles.LoadFromManifest(Manifest);
            Manifest.LoadCustomData(ObjectCreator);

            if (useLoadScreen)
            {
                LoadScreen = ObjectCreator.CreateObject <ILoadScreen>(Manifest.LoadScreen.Value);
                LoadScreen.Init(this, Manifest.LoadScreen.ToDictionary(my => my.Value));
                LoadScreen.Display();
            }

            WidgetLoader = new WidgetLoader(this);
            MapCache     = new MapCache(this);

            SoundLoaders  = ObjectCreator.GetLoaders <ISoundLoader>(Manifest.SoundFormats, "sound");
            SpriteLoaders = ObjectCreator.GetLoaders <ISpriteLoader>(Manifest.SpriteFormats, "sprite");
            VideoLoaders  = ObjectCreator.GetLoaders <IVideoLoader>(Manifest.VideoFormats, "video");

            var terrainFormat = Manifest.Get <TerrainFormat>();
            var terrainLoader = ObjectCreator.FindType(terrainFormat.Type + "Loader");
            var terrainCtor   = terrainLoader?.GetConstructor(new[] { typeof(ModData) });

            if (terrainLoader == null || !terrainLoader.GetInterfaces().Contains(typeof(ITerrainLoader)) || terrainCtor == null)
            {
                throw new InvalidOperationException($"Unable to find a terrain loader for type '{terrainFormat.Type}'.");
            }

            TerrainLoader = (ITerrainLoader)terrainCtor.Invoke(new[] { this });

            var sequenceFormat = Manifest.Get <SpriteSequenceFormat>();
            var sequenceLoader = ObjectCreator.FindType(sequenceFormat.Type + "Loader");
            var sequenceCtor   = sequenceLoader?.GetConstructor(new[] { typeof(ModData) });

            if (sequenceLoader == null || !sequenceLoader.GetInterfaces().Contains(typeof(ISpriteSequenceLoader)) || sequenceCtor == null)
            {
                throw new InvalidOperationException($"Unable to find a sequence loader for type '{sequenceFormat.Type}'.");
            }

            SpriteSequenceLoader = (ISpriteSequenceLoader)sequenceCtor.Invoke(new[] { this });

            var modelFormat = Manifest.Get <ModelSequenceFormat>();
            var modelLoader = ObjectCreator.FindType(modelFormat.Type + "Loader");
            var modelCtor   = modelLoader?.GetConstructor(new[] { typeof(ModData) });

            if (modelLoader == null || !modelLoader.GetInterfaces().Contains(typeof(IModelSequenceLoader)) || modelCtor == null)
            {
                throw new InvalidOperationException($"Unable to find a model loader for type '{modelFormat.Type}'.");
            }

            ModelSequenceLoader = (IModelSequenceLoader)modelCtor.Invoke(new[] { this });
            ModelSequenceLoader.OnMissingModelError = s => Log.Write("debug", s);

            Hotkeys = new HotkeyManager(ModFiles, Game.Settings.Keys, Manifest);

            Translation = new Translation(Game.Settings.Player.Language, Manifest.Translations, DefaultFileSystem);

            defaultRules       = Exts.Lazy(() => Ruleset.LoadDefaults(this));
            defaultTerrainInfo = Exts.Lazy(() =>
            {
                var items = new Dictionary <string, ITerrainInfo>();

                foreach (var file in Manifest.TileSets)
                {
                    var t = TerrainLoader.ParseTerrain(DefaultFileSystem, file);
                    items.Add(t.Id, t);
                }

                return((IReadOnlyDictionary <string, ITerrainInfo>)(new ReadOnlyDictionary <string, ITerrainInfo>(items)));
            });

            defaultSequences = Exts.Lazy(() =>
            {
                var items = DefaultTerrainInfo.ToDictionary(t => t.Key, t => new SequenceProvider(DefaultFileSystem, this, t.Key, null));
                return((IReadOnlyDictionary <string, SequenceProvider>)(new ReadOnlyDictionary <string, SequenceProvider>(items)));
            });

            initialThreadId = Environment.CurrentManagedThreadId;
        }
		public TilesetSpecificSpriteSequence(ModData modData, TileSet tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
			: base(modData, tileSet, cache, loader, sequence, animation, info) { }
예제 #14
0
        public OffsetsSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
            : base(modData, tileSet, cache, loader, sequence, animation, info)
        {
            if (info.Value.EndsWith(".mobd"))
            {
                var src     = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, info.ToDictionary());
                var offsets = cache.FrameMetadata(src).Get <EmbeddedSpriteOffsets>();

                for (var i = 0; i < sprites.Length; i++)
                {
                    if (sprites[i] == null)
                    {
                        continue;
                    }

                    if (offsets.FrameOffsets != null && offsets.FrameOffsets.ContainsKey(i))
                    {
                        EmbeddedOffsets.Add(sprites[i], offsets.FrameOffsets[i]);
                    }
                }
            }
            else if (info.Value.EndsWith(".png"))
            {
                var src      = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, info.ToDictionary());
                var metadata = cache.FrameMetadata(src).Get <PngSheetMetadata>();

                for (var i = 0; i < sprites.Length; i++)
                {
                    if (sprites[i] == null || !metadata.Metadata.ContainsKey($"Offsets[{i}]"))
                    {
                        continue;
                    }

                    var lines          = metadata.Metadata[$"Offsets[{i}]"].Split('|');
                    var convertOffsets = new Func <string[], Offset>(data => new Offset(int.Parse(data[0]), int.Parse(data[1]), int.Parse(data[2])));
                    EmbeddedOffsets.Add(sprites[i], lines.Select(t => t.Split(',')).Select(convertOffsets).ToArray());
                }
            }
        }