Using async instead of extra vars
But they don't connect...
This commit is contained in:
parent
ec07a429c3
commit
b27858607c
4 changed files with 98 additions and 51 deletions
|
@ -4,45 +4,38 @@ using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using GamecraftModdingAPI;
|
using GamecraftModdingAPI;
|
||||||
using GamecraftModdingAPI.Blocks;
|
using GamecraftModdingAPI.Blocks;
|
||||||
using HarmonyLib;
|
using GamecraftModdingAPI.Engines;
|
||||||
|
using GamecraftModdingAPI.Utility;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using RobocraftX.Common.Input;
|
using RobocraftX.Common;
|
||||||
using RobocraftX.Common.Utilities;
|
|
||||||
using RobocraftX.StateSync;
|
|
||||||
using Svelto.ECS;
|
using Svelto.ECS;
|
||||||
using Unity.Jobs;
|
|
||||||
using Unity.Mathematics;
|
using Unity.Mathematics;
|
||||||
using uREPL;
|
using uREPL;
|
||||||
|
|
||||||
namespace GCMC
|
namespace GCMC
|
||||||
{
|
{
|
||||||
public class CubePlacerEngine : IQueryingEntitiesEngine, IDeterministicTimeStopped
|
public class CubePlacerEngine : IApiEngine
|
||||||
{
|
{
|
||||||
public void Ready()
|
public void Ready()
|
||||||
{
|
{
|
||||||
RuntimeCommands.Register<string>("importWorld", ImportWorld, "Imports a Minecraft world.");
|
RuntimeCommands.Register<string>("importWorld", ImportWorld, "Imports a Minecraft world.");
|
||||||
|
_serializer.TraceWriter = _traceWriter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntitiesDB entitiesDB { get; set; }
|
public EntitiesDB entitiesDB { get; set; }
|
||||||
|
|
||||||
private Dictionary<string, BlockType> mapping = new Dictionary<string, BlockType>(10);
|
private readonly Dictionary<string, BlockType> mapping = new Dictionary<string, BlockType>(10);
|
||||||
private Blocks[] blocksArray;
|
private JsonSerializer _serializer = JsonSerializer.Create();
|
||||||
private int C;
|
private JsonTraceWriter _traceWriter = new JsonTraceWriter();
|
||||||
private bool state;
|
|
||||||
private string filename;
|
|
||||||
|
|
||||||
private void ImportWorld(string name)
|
private async void ImportWorld(string name)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
filename = name;
|
|
||||||
Log.Output("Reading block mappings...");
|
Log.Output("Reading block mappings...");
|
||||||
var parser = new IniParser.FileIniDataParser();
|
var parser = new IniParser.FileIniDataParser();
|
||||||
var ini = parser.ReadFile("BlockTypes.ini");
|
var ini = parser.ReadFile("BlockTypes.ini");
|
||||||
mapping.Clear();
|
mapping.Clear();
|
||||||
C = 0;
|
|
||||||
blocksArray = null;
|
|
||||||
state = false;
|
|
||||||
foreach (var section in ini.Sections)
|
foreach (var section in ini.Sections)
|
||||||
{
|
{
|
||||||
var mcblocks = section.SectionName.Split(',');
|
var mcblocks = section.SectionName.Split(',');
|
||||||
|
@ -93,12 +86,54 @@ namespace GCMC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.Output("Starting...");
|
Log.Output("Reading file...");
|
||||||
Task.Run(() =>
|
Blocks[] blocksArray = null;
|
||||||
|
//Console.WriteLine("Outside thread: " + Thread.CurrentThread.ManagedThreadId);
|
||||||
|
//Console.WriteLine("Outside context: " + SynchronizationContext.Current);
|
||||||
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
blocksArray = JsonSerializer.Create()
|
//Console.WriteLine("Inside thread: " + Thread.CurrentThread.Name);
|
||||||
.Deserialize<Blocks[]>(new JsonTextReader(File.OpenText(filename)));
|
var fs = File.OpenText(name);
|
||||||
|
_traceWriter.FileLength = ((FileStream) fs.BaseStream).Length;
|
||||||
|
blocksArray = _serializer.Deserialize<Blocks[]>(new JsonTextReader(fs));
|
||||||
});
|
});
|
||||||
|
//Console.WriteLine("After thread: " + Thread.CurrentThread.ManagedThreadId);
|
||||||
|
Log.Output("Placing blocks...");
|
||||||
|
int i;
|
||||||
|
uint start = BlockIdentifiers.LatestBlockID + 1;
|
||||||
|
uint end = start;
|
||||||
|
for (i = 0; i < blocksArray.Length; i++)
|
||||||
|
{
|
||||||
|
//if (i % 5000 == 0) await AsyncUtils.WaitForSubmission();
|
||||||
|
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;
|
||||||
|
|
||||||
|
end = Block.PlaceNew(type.Type, (blocks.Start + blocks.End) / 10 * 3, color: type.Color.Color,
|
||||||
|
darkness: type.Color.Darkness, scale: (blocks.End - blocks.Start + 1) * 3,
|
||||||
|
rotation: float3.zero).Id.entityID;
|
||||||
|
}
|
||||||
|
|
||||||
|
await AsyncUtils.WaitForSubmission();
|
||||||
|
var conns = entitiesDB.QueryEntities<GridConnectionsEntityStruct>(CommonExclusiveGroups
|
||||||
|
.OWNED_BLOCKS_GROUP);
|
||||||
|
Log.Output("Start: " + start + " - End: " + end);
|
||||||
|
Log.Output("conn: " + conns[start].isIsolator + " " + conns[start].isProcessed + " " +
|
||||||
|
conns[start].areConnectionsAssigned + " " + conns[start].ID + " " +
|
||||||
|
conns[start].machineRigidBodyId);
|
||||||
|
for (uint j = start; j <= end; j++)
|
||||||
|
{
|
||||||
|
conns[j].isProcessed = false;
|
||||||
|
conns[j].areConnectionsAssigned = false;
|
||||||
|
conns[j].isIsolator = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.Output(i + " blocks placed.");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -107,35 +142,11 @@ namespace GCMC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JobHandle SimulatePhysicsStep(in float deltaTime, in PhysicsUtility utility,
|
public void Dispose()
|
||||||
in PlayerInput[] playerInputs)
|
|
||||||
{
|
{
|
||||||
state = !state;
|
|
||||||
if (blocksArray == null || C >= blocksArray.Length
|
|
||||||
|| state) return default;
|
|
||||||
int i;
|
|
||||||
for (i = C; i < C + 10000 && 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;
|
|
||||||
|
|
||||||
Block.PlaceNew(type.Type, (blocks.Start + blocks.End) / 10 * 3, color: type.Color.Color,
|
|
||||||
darkness: type.Color.Darkness, scale: (blocks.End - blocks.Start + 1) * 3,
|
|
||||||
rotation: float3.zero);
|
|
||||||
}
|
|
||||||
|
|
||||||
AccessTools.Method(typeof(Block), "Sync").Invoke(null, new object[0]);
|
|
||||||
C = i;
|
|
||||||
Log.Output(C + " blocks placed.");
|
|
||||||
return new JobHandle();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string name { get; } = "Cube placer engine";
|
public string Name { get; } = "GCMCCubePlacerEngine";
|
||||||
|
public bool isRemovable { get; } = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using GamecraftModdingAPI.Utility;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using IllusionPlugin;
|
using IllusionPlugin;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
@ -14,11 +15,12 @@ namespace GCMC
|
||||||
|
|
||||||
public void OnApplicationStart()
|
public void OnApplicationStart()
|
||||||
{
|
{
|
||||||
if (harmony == null)
|
/*if (harmony == null)
|
||||||
{
|
{
|
||||||
harmony = new Harmony(HarmonyID);
|
harmony = new Harmony(HarmonyID);
|
||||||
harmony.PatchAll(Assembly.GetExecutingAssembly());
|
harmony.PatchAll(Assembly.GetExecutingAssembly());
|
||||||
}
|
}*/
|
||||||
|
GameEngineManager.AddGameEngine(new CubePlacerEngine());
|
||||||
|
|
||||||
Debug.Log("GCMC loaded");
|
Debug.Log("GCMC loaded");
|
||||||
}
|
}
|
||||||
|
|
34
GCMC/JsonTraceWriter.cs
Normal file
34
GCMC/JsonTraceWriter.cs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
using uREPL;
|
||||||
|
|
||||||
|
namespace GCMC
|
||||||
|
{
|
||||||
|
public class JsonTraceWriter : ITraceWriter
|
||||||
|
{
|
||||||
|
private long lastpercent;
|
||||||
|
public void Trace(TraceLevel level, string message, Exception ex)
|
||||||
|
{
|
||||||
|
//Log.Output(message + "\n" + ex, lvl, new StackFrame(1, true));
|
||||||
|
int i = message.IndexOf("position ", StringComparison.Ordinal);
|
||||||
|
int j = message.IndexOf(".", i + 1, StringComparison.Ordinal);
|
||||||
|
if (i > -1 && j > -1)
|
||||||
|
{
|
||||||
|
i += "position ".Length;
|
||||||
|
string str = message.Substring(i, j - i);
|
||||||
|
long pos = long.Parse(str);
|
||||||
|
byte percent = (byte) (pos / (double) FileLength * 100);
|
||||||
|
if (lastpercent / 10 == percent / 10) return;
|
||||||
|
Log.Output("Deserialization " + percent + "% complete");
|
||||||
|
lastpercent = percent;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Console.WriteLine(message + "\n" + ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TraceLevel LevelFilter { get; } = TraceLevel.Info;
|
||||||
|
|
||||||
|
public long FileLength { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ using UnityEngine;
|
||||||
|
|
||||||
namespace GCMC
|
namespace GCMC
|
||||||
{
|
{
|
||||||
[HarmonyPatch]
|
/*[HarmonyPatch]
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public class PlaceBlockPatch
|
public class PlaceBlockPatch
|
||||||
{
|
{
|
||||||
|
@ -31,5 +31,5 @@ namespace GCMC
|
||||||
return typeof(MainEditingCompositionRoot).GetMethod("Compose",
|
return typeof(MainEditingCompositionRoot).GetMethod("Compose",
|
||||||
BindingFlags.Public | BindingFlags.Static);
|
BindingFlags.Public | BindingFlags.Static);
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
}
|
}
|
Loading…
Reference in a new issue