Add text block support to RC importer & general tweaks to make that work

This commit is contained in:
NGnius (Graham) 2020-09-27 20:29:02 -04:00
parent 03c429fa72
commit 5aa13c2e82
5 changed files with 121 additions and 3 deletions

View file

@ -20,7 +20,7 @@ namespace Pixi.Common
internal ProcessedVoxelObjectNotation Process() internal ProcessedVoxelObjectNotation Process()
{ {
BlockIDs block = ConversionUtility.BlockIDsToEnum(name); BlockIDs block = ConversionUtility.BlockIDsToEnum(name.Split('\t')[0]);
return new ProcessedVoxelObjectNotation return new ProcessedVoxelObjectNotation
{ {
block = block, block = block,

View file

@ -52,7 +52,7 @@ namespace Pixi.Common
public static int OPTIMISATION_PASSES = 2; public static int OPTIMISATION_PASSES = 2;
public static int GROUP_SIZE = 64; public static int GROUP_SIZE = 32;
// optimisation algorithm constants // optimisation algorithm constants
private static float3[] cornerMultiplicands1 = new float3[8] private static float3[] cornerMultiplicands1 = new float3[8]
@ -217,7 +217,16 @@ namespace Pixi.Common
desc.color.Darkness, 1, desc.scale); desc.color.Darkness, 1, desc.scale);
blocks[i] = b; blocks[i] = b;
} }
#if DEBUG
else
{
Logging.LogWarning($"Found invalid block at index {i}\n\t{optVONsArr[i].ToString()}");
}
#endif
} }
// handle special block parameters
PostProcessSpecialBlocks(ref optVONsArr, ref blocks);
// post processing
magicImporter.PostProcess(name, ref blocks); magicImporter.PostProcess(name, ref blocks);
if (magicImporter.Optimisable && blockCountPreOptimisation > blocks.Length) if (magicImporter.Optimisable && blockCountPreOptimisation > blocks.Length)
{ {
@ -516,6 +525,50 @@ namespace Pixi.Common
return result; return result;
} }
private static void PostProcessSpecialBlocks(ref ProcessedVoxelObjectNotation[] pVONs, ref Block[] blocks)
{
// populate block attributes using metadata field from ProcessedVoxelObjectNotation
for (int i = 0; i < pVONs.Length; i++)
{
switch (pVONs[i].block)
{
case BlockIDs.TextBlock:
string[] textSplit = pVONs[i].metadata.Split('\t');
if (textSplit.Length > 1)
{
TextBlock tb = blocks[i].Specialise<TextBlock>();
tb.Text = textSplit[1];
if (textSplit.Length > 2)
{
tb.TextBlockId = textSplit[2];
}
}
break;
case BlockIDs.ConsoleBlock:
string[] cmdSplit = pVONs[i].metadata.Split('\t');
if (cmdSplit.Length > 1)
{
ConsoleBlock cb = blocks[i].Specialise<ConsoleBlock>();
cb.Command = cmdSplit[1];
if (cmdSplit.Length > 2)
{
cb.Arg1 = cmdSplit[2];
if (cmdSplit.Length > 3)
{
cb.Arg1 = cmdSplit[3];
if (cmdSplit.Length > 4)
{
cb.Arg1 = cmdSplit[4];
}
}
}
}
break;
default: break; // do nothing
}
}
}
private static string float3ArrToString(float3[] arr) private static string float3ArrToString(float3[] arr)
{ {
string result = "["; string result = "[";

View file

@ -53,6 +53,7 @@ namespace Pixi
#if DEBUG #if DEBUG
// Development functionality // Development functionality
RobotCommands.CreatePartDumpCommand(); RobotCommands.CreatePartDumpCommand();
root.Inject(new TestImporter());
#endif #endif
// Audio functionality // Audio functionality
root.Inject(new MidiImporter()); root.Inject(new MidiImporter());

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using Svelto.DataStructures; using Svelto.DataStructures;
using Unity.Mathematics; using Unity.Mathematics;
using UnityEngine; using UnityEngine;
@ -125,6 +125,18 @@ namespace Pixi.Robots
{ {
blocks[i].position += pos; blocks[i].position += pos;
} }
// set textblock colors (replace <color="white"> with <color=#HEX> in textblocks)
Regex pattern = new Regex("<color=(\"white\")|(white)>", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
for (int i = 0; i < blocks.Length; i++)
{
if (blocks[i].block == BlockIDs.TextBlock)
{
// TODO this blindly replaces color tags anywhere in metadata, not just ones that will go in the TextBlock's text field
blocks[i].metadata = pattern.Replace(
blocks[i].metadata,
$"<color=#{ColorUtility.ToHtmlStringRGBA(ColorSpaceUtility.UnquantizeToColor(blocks[i].color))}>");
}
}
} }
public void PostProcess(string name, ref Block[] blocks) public void PostProcess(string name, ref Block[] blocks)

52
Pixi/TestImporter.cs Normal file
View file

@ -0,0 +1,52 @@
using System;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Players;
using Pixi.Common;
using Unity.Mathematics;
namespace Pixi
{
public class TestImporter : Importer
{
public int Priority { get; } = 0;
public bool Optimisable { get; } = false;
public string Name { get; } = "Test~Spell";
public BlueprintProvider BlueprintProvider { get; } = null;
public bool Qualifies(string name)
{
return name.Equals("test", StringComparison.InvariantCultureIgnoreCase);
}
public BlockJsonInfo[] Import(string name)
{
return new[]
{
new BlockJsonInfo
{
name = BlockIDs.TextBlock.ToString() +
"\ttext that is preserved through the whole import process and ends up in the text block\ttextblockIDs_sux",
position = new[] {0f, 0f, 0f},
rotation = new[] {0f, 0f, 0f},
color = new[] {0f, 0f, 0f},
scale = new[] {1f, 1f, 1f},
}
};
}
public void PreProcess(string name, ref ProcessedVoxelObjectNotation[] blocks)
{
Player p = new Player(PlayerType.Local);
float3 pos = p.Position;
for (int i = 0; i < blocks.Length; i++)
{
blocks[i].position += pos;
}
}
public void PostProcess(string name, ref Block[] blocks)
{
// meh
}
}
}