84 lines
4.5 KiB
C#
84 lines
4.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.ModLoader;
|
|
|
|
namespace CalamityMod.Schematics
|
|
{
|
|
public static class SchematicLoader
|
|
{
|
|
#region Fields
|
|
internal static Dictionary<string, ColorTileCombination[,]> TileMaps = new Dictionary<string, ColorTileCombination[,]>();
|
|
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, ColorTileCombination[,]>
|
|
{
|
|
// Texture loading does not require a .png at the end of the path string.
|
|
["Workshop"] = LoadSchematicCodeAndTexture(typeof(WorkshopSchematic), "Schematics/AutoGeneratedContent/WorkshopSchematic"),
|
|
["Research Facility"] = LoadSchematicCodeAndTexture(typeof(ResearchFacilitySchematic), "Schematics/AutoGeneratedContent/ResearchFacilitySchematic"),
|
|
["Hell Laboratory"] = LoadSchematicCodeAndTexture(typeof(HellLaboratorySchematic), "Schematics/AutoGeneratedContent/HellLaboratorySchematic"),
|
|
["Sunken Sea Laboratory"] = LoadSchematicCodeAndTexture(typeof(SunkenSeaLaboratorySchematic), "Schematics/AutoGeneratedContent/SunkenSeaLaboratorySchematic"),
|
|
["Ice Laboratory"] = LoadSchematicCodeAndTexture(typeof(IceLaboratorySchematic), "Schematics/AutoGeneratedContent/IceLaboratorySchematic"),
|
|
["Plague Laboratory"] = LoadSchematicCodeAndTexture(typeof(PlagueLaboratorySchematic), "Schematics/AutoGeneratedContent/PlagueLaboratorySchematic"),
|
|
["Planetoid Laboratory"] = LoadSchematicCodeAndTexture(typeof(PlanetoidLaboratorySchematic), "Schematics/AutoGeneratedContent/PlanetoidLaboratorySchematic"),
|
|
|
|
["Astral Beacon"] = LoadSchematicCodeAndTexture(typeof(AstralBeaconSchematic), "Schematics/AutoGeneratedContent/AstralBeaconSchematic")
|
|
};
|
|
}
|
|
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 ColorTileCombination[,] LoadSchematicCodeAndTexture(Type schematicType, string texturePath)
|
|
{
|
|
ColorTileMap map = (ColorTileMap)SeekSchematicLoaderInType(schematicType).Invoke(null, null);
|
|
Texture2D schemanticTexture = ModContent.GetTexture($"CalamityMod/{texturePath}");
|
|
|
|
ColorTileCombination[,] schematic = new ColorTileCombination[schemanticTexture.Width, schemanticTexture.Height];
|
|
Color[,] colors = schemanticTexture.GetColorsFromTexture();
|
|
for (int x = 0; x < schemanticTexture.Width; x++)
|
|
{
|
|
for (int y = 0; y < schemanticTexture.Height; y++)
|
|
{
|
|
schematic[x, y] = new ColorTileCombination(colors[x, y], map[colors[x, y]].SpecifiedTile);
|
|
}
|
|
}
|
|
return schematic;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|