public override void Initialize(ModGameAPI dediAPI) { GameAPI = dediAPI; LogLevel = EmpyrionNetAPIDefinitions.LogLevel.Debug; TaskTools.Intervall(Math.Max(1, Program.AppSettings.StructureDataUpdateCheckInSeconds) * 1000, () => BackupStructureData()); Event_Playfield_Loaded += P => ActivePlayfields.TryAdd(P.playfield, P.playfield); Event_Playfield_Unloaded += P => ActivePlayfields.TryRemove(P.playfield, out _); }
private void BackupStructureData() { if (ActivePlayfields.Count == 0) { List <string> playfields = null; try { playfields = Request_Playfield_List().Result?.playfields; } catch (Exception error) { if (LoggedError.TryAdd(error.Message, true)) { Logger?.LogError(error, $"BackupStructureData: Request_Playfield_List"); } return; } if (playfields == null) { var msg = $"Request_Playfield_List: no playfields"; if (LoggedError.TryAdd(msg, true)) { Logger?.LogError(msg, $"BackupStructureData: Request_Playfield_List"); } return; } ActivePlayfields = new ConcurrentDictionary <string, string>(playfields.ToDictionary(P => P)); } if (SavesStructuresDat == null || SavesStructuresDat.Count == 0) { SavesStructuresDat = new Queue <PlayfieldStructureData>(StructureManager.Value.CurrentGlobalStructures .Values .Where(S => S.StructureInfo.factionId > 0 && ActivePlayfields.TryGetValue(S.Playfield, out _))); } int errorCounter = 0; while (SavesStructuresDat.TryDequeue(out var test)) { try { var type = new[] { "Undef", "", "BA", "CV", "SV", "HV", "", "AstVoxel" }[test.StructureInfo.type]; // Entity.GetFromEntityType 'Kommentare der Devs: Set this Undef = 0, BA = 2, CV = 3, SV = 4, HV = 5, AstVoxel = 7 var structurePath = Path.Combine(EmpyrionConfiguration.SaveGamePath, "Shared", $"{test.StructureInfo.id}"); var exportDat = Path.Combine(structurePath, "Export.dat"); if (ActivePlayfields.TryGetValue(test.Playfield, out _) && IsExportDatOutdated(exportDat)) { Request_Entity_Export(new EntityExportInfo() { id = test.StructureInfo.id, playfield = test.Playfield, filePath = exportDat, isForceUnload = false, }).Wait(10000); Thread.Sleep(Program.AppSettings.StructureDataUpdateDelayInSeconds * 1000); return; } } catch (TimeoutException) { } catch (Exception error) { if (LoggedError.TryAdd(error.Message, true)) { Logger?.LogError(error, $"BackupStructureData: Request_Entity_Export[{errorCounter++}] {test.Playfield} -> {test.StructureInfo.id} '{test.StructureInfo.name}'"); } try { ActivePlayfields = new ConcurrentDictionary <string, string>(Request_Playfield_List().Result.playfields.ToDictionary(P => P)); } catch (Exception playfieldListError) { Logger?.LogError(playfieldListError, $"BackupStructureData: Request_Playfield_List {test.Playfield} -> {test.StructureInfo.id} '{test.StructureInfo.name}'"); } Thread.Sleep(Program.AppSettings.StructureDataUpdateDelayInSeconds * 1000); } } }