|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Threading.Tasks;
- using IllusionPlugin;
- using Newtonsoft.Json;
- using Svelto.Tasks;
- using Svelto.Tasks.ExtraLean;
- using TechbloxModdingAPI;
- using TechbloxModdingAPI.App;
- using TechbloxModdingAPI.Blocks;
- using TechbloxModdingAPI.Commands;
- using TechbloxModdingAPI.Tasks;
- using TechbloxModdingAPI.Utility;
- using Unity.Mathematics;
- using UnityEngine;
- using uREPL;
-
- namespace GCMC
- {
- public class GCMCPlugin : IEnhancedPlugin
- {
- public override string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
- public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
-
- private readonly Dictionary<string, BlockType> _mapping = new Dictionary<string, BlockType>(10);
- private readonly List<(Block, SimBody, float3)> _simulationBlocks = new List<(Block, SimBody, float3)>();
- private readonly JsonSerializer _serializer = JsonSerializer.Create();
- private readonly JsonTraceWriter _traceWriter = new JsonTraceWriter();
-
- private async void ImportWorld(string name)
- {
- try
- {
- Log.Output("Reading block mappings...");
- var parser = new IniParser.FileIniDataParser();
- var ini = parser.ReadFile("BlockTypes.ini");
- _mapping.Clear();
- foreach (var section in ini.Sections)
- {
- var mcblocks = section.SectionName.Split(',');
- BlockIDs type;
- if (section.Keys["type"] == null)
- {
- if (section.Keys["ignore"] != "true")
- {
- Log.Warn("Block type not specified for " + section.SectionName);
- continue;
- }
-
- type = BlockIDs.Invalid;
- }
- else if (!Enum.TryParse(section.Keys["type"], out type))
- {
- Log.Warn("Block type specified in ini not found: " + section.Keys["type"]);
- continue;
- }
-
- BlockColors color;
- if (section.Keys["color"] == null)
- color = BlockColors.Default;
- else if (!Enum.TryParse(section.Keys["color"], out color))
- {
- Log.Warn("Block color specified in ini not found: " + section.Keys["color"]);
- continue;
- }
-
- byte darkness;
- if (section.Keys["darkness"] == null)
- darkness = 0;
- else if (!byte.TryParse(section.Keys["darkness"], out darkness) || darkness > 9)
- {
- Log.Warn("Block darkness specified in ini isn't a number between 0 and 9: " +
- section.Keys["darkness"]);
- continue;
- }
-
- foreach (var mcblock in mcblocks)
- {
- _mapping.Add(mcblock.ToUpper(), new BlockType
- {
- Material = mcblock.ToUpper(),
- Type = type,
- Color = new BlockColor(color, darkness)
- });
- }
- }
-
- Log.Output("Reading file...");
- Blocks[] blocksArray = null;
- await Task.Run(() =>
- {
- var fs = File.OpenText(name);
- _traceWriter.FileLength = ((FileStream) fs.BaseStream).Length;
- blocksArray = _serializer.Deserialize<Blocks[]>(new JsonTextReader(fs));
- });
- Log.Output("Placing blocks...");
- int i;
- for (i = 0; i < blocksArray.Length; i++)
- {
- var blocks = blocksArray[i];
- if (!_mapping.TryGetValue(blocks.Material, out var type))
- {
- Console.WriteLine("Unknown block: " + blocks.Material);
- continue;
- }
-
- if (type.Type == BlockIDs.Invalid) continue;
-
- var block = new Block(type.Type, (blocks.Start + blocks.End) / 10 * 3 + new float3(5000, 0, 5000))
- {
- Color = type.Color,
- Scale = (blocks.End - blocks.Start + 1) * 3,
- //Static = true - Doesn't seem to work
- };
- _simulationBlocks.Add((block, null, default));
- }
- Game.Simulate += GameOnSimulate;
-
- Log.Output(i + " blocks placed.");
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- Log.Error(e.Message);
- }
- }
-
- private void GameOnSimulate(object sender, GameEventArgs e)
- {
- Game.Simulate -= GameOnSimulate;
- GameWhileSimulating().RunOn(Scheduler.extraLeanRunner);
- }
-
- private IEnumerator GameWhileSimulating()
- {
- while (!GameState.IsSimulationMode())
- yield return Yield.It;
-
- for (var i = 0; i < _simulationBlocks.Count; i++)
- {
- var (block, body, _) = _simulationBlocks[i];
- if (body is null) body = block.GetSimBody();
- _simulationBlocks[i] = (block, body, body.Position);
- }
-
- while (GameState.IsSimulationMode())
- {
- foreach (var (_, body, pos) in _simulationBlocks)
- {
- body.Velocity = 0;
- body.AngularVelocity = 0;
- body.Position = pos;
- body.Rotation = float3.zero;
- }
-
- yield return Yield.It;
- }
- }
-
- public override void OnApplicationStart()
- {
- TechbloxModdingAPI.Main.Init();
- CommandBuilder.Builder("importWorld", "Imports a Minecraft world.")
- .Action<string>(ImportWorld).Build();
- //_serializer.TraceWriter = _traceWriter;
-
- Debug.Log("GCMC loaded");
- }
-
- public override void OnApplicationQuit()
- {
- TechbloxModdingAPI.Main.Shutdown();
- }
- }
- }
|