Ejemplo n.º 1
0
        /// <summary>
        /// Creates a deep copy of the texture
        /// </summary>
        /// <returns></returns>
        public CompositeTexture Clone()
        {
            CompositeTexture[] alternatesClone = null;

            if (Alternates != null)
            {
                alternatesClone = new CompositeTexture[Alternates.Length];
                for (int i = 0; i < alternatesClone.Length; i++)
                {
                    alternatesClone[i] = Alternates[i].CloneWithoutAlternates();
                }
            }

            CompositeTexture ct = new CompositeTexture()
            {
                Base       = Base.Clone(),
                Alternates = alternatesClone,
                Rotation   = Rotation,
                Alpha      = Alpha
            };

            if (Overlays != null)
            {
                ct.Overlays = new AssetLocation[Overlays.Length];
                for (int i = 0; i < ct.Overlays.Length; i++)
                {
                    ct.Overlays[i] = Overlays[i].Clone();
                }
            }

            return(ct);
        }
Ejemplo n.º 2
0
        public TextureAtlasPosition this[string textureCode]
        {
            get
            {
                AssetLocation textureLoc = null;
                if (curContProps.Textures != null)
                {
                    CompositeTexture compTex;
                    if (curContProps.Textures.TryGetValue(textureCode, out compTex))
                    {
                        textureLoc = compTex.Base;
                    }
                }

                if (textureLoc == null && shapeTextures != null)
                {
                    shapeTextures.TryGetValue(textureCode, out textureLoc);
                }

                if (textureLoc != null)
                {
                    TextureAtlasPosition texPos = capi.BlockTextureAtlas[textureLoc];
                    if (texPos == null)
                    {
                        BitmapRef bmp = capi.Assets.TryGet(textureLoc.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"))?.ToBitmap(capi);
                        if (bmp != null)
                        {
                            capi.BlockTextureAtlas.InsertTextureCached(textureLoc, bmp, out _, out texPos);
                            bmp.Dispose();
                        }
                    }

                    return(texPos);
                }

                ItemStack content = GetContents();
                if (content.Class == EnumItemClass.Item)
                {
                    TextureAtlasPosition texPos;
                    textureLoc = content.Item.Textures[textureCode].Base;
                    BitmapRef bmp = capi.Assets.TryGet(textureLoc.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"))?.ToBitmap(capi);
                    if (bmp != null)
                    {
                        capi.BlockTextureAtlas.InsertTextureCached(textureLoc, bmp, out _, out texPos);
                        bmp.Dispose();
                        return(texPos);
                    }
                }

                return(contentTexSource[textureCode]);
            }
        }
Ejemplo n.º 3
0
        public static void PreFix(ref IList <Block> blocks)
        {
            foreach (Block block in blocks)
            {
                AssetLocation val = null;

                if (PatchingBlocks.Any(a => {
                    if (a.Key.FirstPathPart() == block.FirstCodePart())
                    {
                        val = a.Value;
                        return(true);
                    }
                    return(false);
                }))
                {
                    if (val != null && block.Textures.ContainsKey("all"))
                    {
                        if (val.Path.Contains('{'))
                        {
                            var x = val.Path.Split('{', '}');

                            var r = val.Clone();
                            r.Path = r.Path.Replace('{' + x[1] + '}', block.Variant[x[1]]);

                            block.Textures["all"].Base = r;
                        }
                        else
                        {
                            block.Textures["all"].Base = val;
                        }
                    }
                }
            }
        }
        public TextureAtlasPosition this[string textureCode]
        {
            get
            {
                AssetLocation        texturePath = textureLocation;
                TextureAtlasPosition texpos;
                if (texturePath == null)
                {
                    texpos = this.texPos;
                }
                else
                {
                    texpos = api.BlockTextureAtlas[texturePath];
                }


                if (texpos == null)
                {
                    IAsset texAsset = api.Assets.TryGet(texturePath.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"));
                    if (texAsset != null)
                    {
                        BitmapRef bmp = texAsset.ToBitmap(api);
                        api.BlockTextureAtlas.InsertTextureCached(texturePath, bmp, out _, out texpos);
                    }
                    else
                    {
                        texpos = api.BlockTextureAtlas.UnknownTexturePosition;
                    }
                }

                return(texpos);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Fetches an image surface from a named file.
        /// </summary>
        /// <param name="capi">The Client API</param>
        /// <param name="textureLoc">The name of the text file.</param>
        /// <returns></returns>
        public static ImageSurface getImageSurfaceFromAsset(ICoreClientAPI capi, AssetLocation textureLoc, int mulAlpha = 255)
        {
            byte[] pngdata = capi.Assets.Get(textureLoc.Clone().WithPathPrefixOnce("textures/")).Data;

            BitmapExternal bitmap = capi.Render.BitmapCreateFromPng(pngdata);

            if (mulAlpha != 255)
            {
                bitmap.MulAlpha(mulAlpha);
            }

            BitmapData   bmp_data     = bitmap.bmp.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.bmp.Width, bitmap.bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            ImageSurface imageSurface = new ImageSurface(Format.Argb32, bitmap.bmp.Width, bitmap.bmp.Height);

            unsafe
            {
                uint *destPixels   = (uint *)imageSurface.DataPtr.ToPointer();
                uint *sourcePixels = (uint *)bmp_data.Scan0.ToPointer();

                int size = bitmap.bmp.Width * bitmap.bmp.Height;

                for (int i = 0; i < size; i++)
                {
                    destPixels[i] = sourcePixels[i];
                }
            }

            imageSurface.MarkDirty();


            bitmap.bmp.UnlockBits(bmp_data);
            bitmap.Dispose();

            return(imageSurface);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a copy of this object.
        /// </summary>
        /// <returns></returns>
        public EntityProperties Clone()
        {
            BlockDropItemStack[] DropsCopy;
            if (Drops == null)
            {
                DropsCopy = null;
            }
            else
            {
                DropsCopy = new BlockDropItemStack[Drops.Length];
                for (int i = 0; i < DropsCopy.Length; i++)
                {
                    DropsCopy[i] = Drops[i].Clone();
                }
            }

            Dictionary <string, AssetLocation> csounds = new Dictionary <string, AssetLocation>();

            foreach (var val in Sounds)
            {
                csounds[val.Key] = val.Value.Clone();
            }


            Dictionary <string, AssetLocation[]> cresolvedsounds = new Dictionary <string, AssetLocation[]>();

            foreach (var val in ResolvedSounds)
            {
                AssetLocation[] locs = val.Value;
                cresolvedsounds[val.Key] = new AssetLocation[locs.Length];
                for (int i = 0; i < locs.Length; i++)
                {
                    cresolvedsounds[val.Key][i] = locs[i].Clone();
                }
            }

            return(new EntityProperties()
            {
                Code = Code.Clone(),
                Class = Class,
                Habitat = Habitat,
                HitBoxSize = HitBoxSize.Clone(),
                DeadHitBoxSize = DeadHitBoxSize.Clone(),
                CanClimb = CanClimb,
                CanClimbAnywhere = CanClimbAnywhere,
                FallDamage = FallDamage,
                ClimbTouchDistance = ClimbTouchDistance,
                RotateModelOnClimb = RotateModelOnClimb,
                KnockbackResistance = KnockbackResistance,
                Attributes = Attributes?.Clone(),
                Sounds = new Dictionary <string, AssetLocation>(Sounds),
                IdleSoundChance = IdleSoundChance,
                IdleSoundRange = IdleSoundRange,
                Drops = DropsCopy,
                EyeHeight = EyeHeight,
                Client = Client?.Clone() as EntityClientProperties,
                Server = Server?.Clone() as EntityServerProperties
            });
        }
Ejemplo n.º 7
0
 internal void ResolveCode(AssetLocation baseCode)
 {
     Code = baseCode.Clone();
     foreach (string code in CodeParts.Values)
     {
         Code.Path += "-" + code;
     }
 }
Ejemplo n.º 8
0
 public void ResolveCode(AssetLocation baseCode)
 {
     Code = baseCode.Clone();
     foreach (string code in CodeParts.Values)
     {
         if (code.Length > 0)
         {
             Code.Path += "-" + code;
         }
     }
 }
        private void loadTexture(Shape entityShape, string code, AssetLocation location, int textureWidth, int textureHeight, string shapePathForLogging)
        {
            ICoreClientAPI api = entity.World.Api as ICoreClientAPI;

            CompositeTexture ctex = new CompositeTexture()
            {
                Base = location
            };

            entityShape.TextureSizes[code] = new int[] { textureWidth, textureHeight };

            AssetLocation shapeTexloc = location;

            // Weird backreference to the shaperenderer. Should be refactored.
            var texturesByLoc  = (entity as EntityAgent).extraTextureByLocation;
            var texturesByName = (entity as EntityAgent).extraTexturesByTextureName;

            BakedCompositeTexture bakedCtex;


            if (!texturesByLoc.TryGetValue(shapeTexloc, out bakedCtex))
            {
                int textureSubId = 0;
                TextureAtlasPosition texpos;

                IAsset texAsset = api.Assets.TryGet(location.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"));
                if (texAsset != null)
                {
                    BitmapRef bmp = texAsset.ToBitmap(api);
                    api.EntityTextureAtlas.InsertTexture(bmp, out textureSubId, out texpos, -1);
                }
                else
                {
                    api.World.Logger.Warning("Skin part shape {0} defined texture {1}, no such texture found.", shapePathForLogging, location);
                }

                ctex.Baked = new BakedCompositeTexture()
                {
                    BakedName = location, TextureSubId = textureSubId
                };

                texturesByName[code]       = ctex;
                texturesByLoc[shapeTexloc] = ctex.Baked;
            }
            else
            {
                ctex.Baked           = bakedCtex;
                texturesByName[code] = ctex;
            }
        }
        protected TextureAtlasPosition getOrCreateTexPos(AssetLocation texturePath)
        {
            TextureAtlasPosition texpos = capi.BlockTextureAtlas[texturePath];

            if (texpos == null)
            {
                IAsset texAsset = capi.Assets.TryGet(texturePath.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"));
                if (texAsset != null)
                {
                    BitmapRef bmp = texAsset.ToBitmap(capi);
                    capi.BlockTextureAtlas.InsertTextureCached(texturePath, bmp, out _, out texpos);
                }
                else
                {
                    capi.World.Logger.Warning("For render in block " + Block.Code + ", item {0} defined texture {1}, not no such texture found.", nowTesselatingObj.Code, texturePath);
                }
            }

            return(texpos);
        }
Ejemplo n.º 11
0
        public TextureAtlasPosition this[string textureCode]
        {
            get
            {
                AssetLocation texturePath = crustTextureLoc;
                if (textureCode == "filling")
                {
                    texturePath = fillingTextureLoc;
                }
                if (textureCode == "topcrust")
                {
                    texturePath = topCrustTextureLoc;
                }

                if (texturePath == null)
                {
                    capi.World.Logger.Warning("Missing texture path for pie mesh texture code {0}, seems like a missing texture definition or invalid pie block.", textureCode);
                    return(capi.BlockTextureAtlas.UnknownTexturePosition);
                }

                TextureAtlasPosition texpos = capi.BlockTextureAtlas[texturePath];

                if (texpos == null)
                {
                    IAsset texAsset = capi.Assets.TryGet(texturePath.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"));
                    if (texAsset != null)
                    {
                        BitmapRef bmp = texAsset.ToBitmap(capi);
                        capi.BlockTextureAtlas.InsertTextureCached(texturePath, bmp, out _, out texpos);
                    }
                    else
                    {
                        capi.World.Logger.Warning("Pie mesh texture {1} not found.", nowTesselatingBlock.Code, texturePath);
                        texpos = capi.BlockTextureAtlas.UnknownTexturePosition;
                    }
                }


                return(texpos);
            }
        }
Ejemplo n.º 12
0
        protected TextureAtlasPosition getOrCreateTexPos(AssetLocation texturePath)
        {
            var capi = api as ICoreClientAPI;
            TextureAtlasPosition texpos = curAtlas[texturePath];

            if (texpos == null)
            {
                IAsset texAsset = capi.Assets.TryGet(texturePath.Clone().WithPathPrefixOnce("textures/").WithPathAppendixOnce(".png"));
                if (texAsset != null)
                {
                    BitmapRef bmp = texAsset.ToBitmap(capi);
                    curAtlas.InsertTextureCached(texturePath, bmp, out _, out texpos, 0.1f);
                }
                else
                {
                    capi.World.Logger.Warning("Item {0} defined texture {1}, not no such texture found.", Code, texturePath);
                }
            }

            return(texpos);
        }
Ejemplo n.º 13
0
        internal ResolvedDepositBlock Resolve(string fileForLogging, ICoreServerAPI api, Block inblock, string key, string value)
        {
            AssetLocation oreLoc = Code.Clone();

            oreLoc.Path = oreLoc.Path.Replace("{" + key + "}", value);

            Block[] oreBlocks = api.World.SearchBlocks(oreLoc);

            if (oreBlocks.Length == 0)
            {
                api.World.Logger.Warning("Deposit {0}: No block with code/wildcard '{1}' was found (unresolved code: {2})", fileForLogging, oreLoc, Code);
            }

            if (AllowedVariants != null)
            {
                List <Block> filteredBlocks = new List <Block>();
                for (int i = 0; i < oreBlocks.Length; i++)
                {
                    if (WildcardUtil.Match(oreLoc, oreBlocks[i].Code, AllowedVariants))
                    {
                        filteredBlocks.Add(oreBlocks[i]);
                    }
                }

                if (filteredBlocks.Count == 0)
                {
                    api.World.Logger.Warning("Deposit {0}: AllowedVariants for {1} does not match any block! Please fix", fileForLogging, oreLoc);
                }

                oreBlocks = filteredBlocks.ToArray();

                MaxGrade = AllowedVariants.Length;
            }

            if (AllowedVariantsByInBlock != null)
            {
                List <Block> filteredBlocks = new List <Block>();
                for (int i = 0; i < oreBlocks.Length; i++)
                {
                    if (AllowedVariantsByInBlock[inblock.Code].Contains(WildcardUtil.GetWildcardValue(oreLoc, oreBlocks[i].Code)))
                    {
                        filteredBlocks.Add(oreBlocks[i]);
                    }
                }

                foreach (var val in AllowedVariantsByInBlock)
                {
                    MaxGrade = Math.Max(MaxGrade, val.Value.Length);
                }

                if (filteredBlocks.Count == 0)
                {
                    api.World.Logger.Warning("Deposit {0}: AllowedVariantsByInBlock for {1} does not match any block! Please fix", fileForLogging, oreLoc);
                }

                oreBlocks = filteredBlocks.ToArray();
            }

            return(new ResolvedDepositBlock()
            {
                Blocks = oreBlocks
            });
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Expands a CompositeTexture to a texture atlas friendly version and populates the Baked field
        /// </summary>
        /// <param name="assetManager"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        static BakedCompositeTexture Bake(IAssetManager assetManager, CompositeTexture ct)
        {
            BakedCompositeTexture bct = new BakedCompositeTexture();

            bct.BakedName = ct.Base.Clone();

            if (ct.Base.EndsWithWildCard)
            {
                List <IAsset> assets = assetManager.GetMany("textures/" + bct.BakedName.Path.Substring(0, bct.BakedName.Path.Length - 1), bct.BakedName.Domain);
                if (assets.Count == 0)
                {
                    ct.Base = bct.BakedName = new AssetLocation("unknown");
                }
                else
                if (assets.Count == 1)
                {
                    ct.Base       = assets[0].Location.CloneWithoutPrefixAndEnding("textures/".Length);
                    bct.BakedName = ct.Base.Clone();
                }
                else
                if (assets.Count > 1)
                {
                    int origLength = (ct.Alternates == null ? 0 : ct.Alternates.Length);
                    CompositeTexture[] alternates = new CompositeTexture[origLength + assets.Count - 1];
                    if (ct.Alternates != null)
                    {
                        Array.Copy(ct.Alternates, alternates, ct.Alternates.Length);
                    }

                    for (int i = 0; i < assets.Count; i++)
                    {
                        IAsset        asset       = assets[i];
                        AssetLocation newLocation = asset.Location.CloneWithoutPrefixAndEnding("textures/".Length);

                        if (i == 0)
                        {
                            ct.Base       = newLocation;
                            bct.BakedName = newLocation.Clone();
                        }
                        else
                        {
                            var act = alternates[origLength + i - 1] = new CompositeTexture(newLocation);
                            act.Rotation = ct.Rotation;
                            act.Alpha    = ct.Alpha;
                        }
                    }

                    ct.Alternates = alternates;
                }
            }


            if (ct.Overlays != null)
            {
                bct.TextureFilenames    = new AssetLocation[ct.Overlays.Length + 1];
                bct.TextureFilenames[0] = ct.Base.Clone();

                for (int i = 0; i < ct.Overlays.Length; i++)
                {
                    bct.TextureFilenames[i + 1] = ct.Overlays[i].Clone();
                    bct.BakedName.Path         += "++" + ct.Overlays[i].ToShortString();
                }
            }
            else
            {
                bct.TextureFilenames = new AssetLocation[] { ct.Base.Clone() };
            }

            if (ct.Rotation != 0)
            {
                if (ct.Rotation != 90 && ct.Rotation != 180 && ct.Rotation != 270)
                {
                    throw new Exception("Texture definition " + ct.Base + " has a rotation thats not 0, 90, 180 or 270. These are the only allowed values!");
                }
                bct.BakedName.Path += "@" + ct.Rotation;
            }

            if (ct.Alpha != 255)
            {
                if (ct.Alpha < 0 || ct.Alpha > 255)
                {
                    throw new Exception("Texture definition " + ct.Base + " has a alpha value outside the 0..255 range.");
                }

                bct.BakedName.Path += "å" + ct.Alpha;
            }

            if (ct.Alternates != null)
            {
                bct.BakedVariants    = new BakedCompositeTexture[ct.Alternates.Length + 1];
                bct.BakedVariants[0] = bct;
                for (int i = 0; i < ct.Alternates.Length; i++)
                {
                    bct.BakedVariants[i + 1] = Bake(assetManager, ct.Alternates[i]);
                }
            }

            return(bct);
        }