If any of the recipes seem weird or go against original design plans, please change them.
78 lines
3.5 KiB
C#
78 lines
3.5 KiB
C#
using CalamityMod.Schematics.AutoGeneratedContent;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using System;
|
|
using System.CodeDom.Compiler;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Terraria;
|
|
using Terraria.ModLoader;
|
|
|
|
namespace CalamityMod.Schematics
|
|
{
|
|
public static class SchematicLoader
|
|
{
|
|
#region Fields
|
|
internal static Dictionary<string, Tile[,]> TileMaps = new Dictionary<string, Tile[,]>();
|
|
internal static Dictionary<string, PilePlacementFunction> PilePlacementMaps = new Dictionary<string, PilePlacementFunction>();
|
|
public delegate void PilePlacementFunction(int x, int y, Rectangle placeInArea);
|
|
#endregion
|
|
|
|
#region Loading/Unloading
|
|
internal static void LoadEverything()
|
|
{
|
|
PilePlacementMaps = new Dictionary<string, PilePlacementFunction>();
|
|
TileMaps = new Dictionary<string, Tile[,]>
|
|
{
|
|
// Texture loading does not require a .png at the end of the path string.
|
|
["Workshop"] = LoadSchematicCodeAndTexture(typeof(WorkshopSchematic), "Schematics/AutoGeneratedContent/WorkshopSchematic"),
|
|
["Plague Research Facility"] = LoadSchematicCodeAndTexture(typeof(ResearchFacilitySchematic), "Schematics/AutoGeneratedContent/ResearchFacilitySchematic")
|
|
};
|
|
}
|
|
internal static void UnloadEverything()
|
|
{
|
|
TileMaps = null;
|
|
PilePlacementMaps = null;
|
|
}
|
|
#endregion
|
|
|
|
#region Texture and Code to Tile
|
|
internal static MethodInfo SeekSchematicLoaderInType(Type toSearch)
|
|
{
|
|
// Checks if the method has a GeneratedCode attribute with a specific tool.
|
|
// This should always be the case as it is added by default with the auto-generated code.
|
|
bool isSuitableMethod(MethodInfo method)
|
|
{
|
|
GeneratedCodeAttribute generatedCodeAttribute = method.GetCustomAttribute(typeof(GeneratedCodeAttribute)) as GeneratedCodeAttribute;
|
|
|
|
if (generatedCodeAttribute == null)
|
|
return false;
|
|
return generatedCodeAttribute.Tool.ToLower() == "screencapturetoschematic";
|
|
}
|
|
MethodInfo[] appropriateMethodsFound = toSearch.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
|
|
MethodInfo match = appropriateMethodsFound.First(isSuitableMethod);
|
|
if (match == null)
|
|
throw new NullReferenceException($"Could not load any methods from {toSearch.FullName} that are appropriately marked to be used for schematic loading.");
|
|
return match;
|
|
}
|
|
internal static Tile[,] LoadSchematicCodeAndTexture(Type schematicType, string texturePath)
|
|
{
|
|
ColorTileMap map = (ColorTileMap)SeekSchematicLoaderInType(schematicType).Invoke(null, null);
|
|
Texture2D schemanticTexture = ModContent.GetTexture($"CalamityMod/{texturePath}");
|
|
|
|
Tile[,] tiles = new Tile[schemanticTexture.Width, schemanticTexture.Height];
|
|
Color[,] colors = schemanticTexture.GetColorsFromTexture();
|
|
for (int x = 0; x < schemanticTexture.Width; x++)
|
|
{
|
|
for (int y = 0; y < schemanticTexture.Height; y++)
|
|
{
|
|
tiles[x, y] = map[colors[x, y]].SpecifiedTile;
|
|
}
|
|
}
|
|
return tiles;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|