Files
CalamityModPublic/Schematics/SchematicLoader.cs
T

105 lines
5.1 KiB
C#

using CalamityMod.Schematics.AutoGeneratedContent;
using Microsoft.Xna.Framework;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
namespace CalamityMod.Schematics
{
[Serializable]
public struct SerializableColor
{
internal byte Red;
internal byte Green;
internal byte Blue;
internal byte Alpha;
public SerializableColor(byte r, byte g, byte b, byte a)
{
Red = r;
Green = g;
Blue = b;
Alpha = a;
}
}
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)
{
if (!(method.GetCustomAttribute(typeof(GeneratedCodeAttribute)) is GeneratedCodeAttribute generatedCodeAttribute))
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 schematicPath)
{
ColorTileMap map = (ColorTileMap)SeekSchematicLoaderInType(schematicType).Invoke(null, null);
// Forcefully load textures, in spite of multiplayer restrictions.
Stream schematicStream = CalamityMod.Instance.GetFileStream(schematicPath + ".sch", true);
var systemImage = (SerializableColor[,])new BinaryFormatter().Deserialize(schematicStream);
ColorTileCombination[,] schematic = new ColorTileCombination[systemImage.GetLength(0), systemImage.GetLength(1)];
for (int x = 0; x < systemImage.GetLength(0); x++)
{
for (int y = 0; y < systemImage.GetLength(1); y++)
{
var systemColor = systemImage[x, y];
Color xnaColor = new Color(systemColor.Red, systemColor.Green, systemColor.Blue, systemColor.Alpha);
schematic[x, y] = new ColorTileCombination(xnaColor, map[xnaColor].SpecifiedTile);
}
}
return schematic;
}
#endregion
}
}