private void UpdateThread()
        {
            try
            {
                Logger.Add("Entering update loop..");

                while (true)
                {
                    try
                    {
                        UpdateQueueItem item = _updateQueue.Take(_cancellationTokenSource.Token);

                        if (item.TargetFile.HasChanged())
                        {
                            UpdateFile(item);
                        }
                    }
                    catch (Exception ex) when(ex.IsNot <OperationCanceledException>())
                    {
                        Logger.Add("Error in update loop", ex);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Logger.Add("Update thread terminated");
            }
        }
        private static void UpdateFile(UpdateQueueItem item)
        {
            try
            {
                item.TargetFile.EnableWatcherEvents();

                (bool exists, long fileSize, DateTime modifiedTime) = item.TargetFile.GetLiveProperties();

                if (!exists)
                {
                    return;
                }

                string fileContent = item.TargetFile.ReadContent();
                IDictionary <FileUpdate, int> changes = item.TargetFile.FileUpdates.ToDictionary(x => x, _ => 0);

                string updatedFileContent = item.TargetFile.FileUpdates.Aggregate(fileContent,
                                                                                  (content, fileUpdate) => Regex.Replace(content, fileUpdate.SearchPattern, match =>
                {
                    changes[fileUpdate]++;
                    return(match.Result(fileUpdate.ReplacePattern));
                }, RegexOptions.Multiline));

                int totalChanges = changes.Values.Sum();

                if (totalChanges > 0)
                {
                    item.TargetFile.WriteContent(updatedFileContent);
                }

                string action = item.TargetFile.FileSize < 0 ? "Inspected" : "Identified modification to";
                item.TargetFile.UpdateProperties();

                if (totalChanges > 0)
                {
                    Logger.Add($"{action} \"{item.TargetFile.FilePath}\"" +
                               $" by {item.CheckType.ToString().ToLower()} and made {totalChanges} {"change".Pluralize(totalChanges)}" +
                               $" ({fileSize} bytes modified {modifiedTime} > {item.TargetFile.FileSize} bytes modified {item.TargetFile.ModifiedTime})");

                    foreach (FileUpdate fileUpdate in changes.Keys)
                    {
                        Logger.Add($"Update definition {fileUpdate.Id} resulted in {changes[fileUpdate]} {"change".Pluralize(changes[fileUpdate])}");
                    }
                }
                else
                {
                    Logger.Add($"{action} \"{item.TargetFile.FilePath}\"" +
                               $" by {item.CheckType.ToString().ToLower()} but made no {"change".Pluralize(totalChanges)}");
                }
            }
            catch (Exception ex)
            {
                Logger.Add($"Failed to update \"{item.TargetFile.FilePath}\"", ex);
            }
            finally
            {
                item.TargetFile.DisableWatcherEvents();
            }
        }
Ejemplo n.º 3
0
 public MyClipmap(uint id, MyClipmapScaleEnum scaleGroup, MatrixD worldMatrix, Vector3I sizeLod0, IMyClipmapCellHandler cellProvider)
 {
     m_scaleGroup = scaleGroup;
     m_worldMatrix = worldMatrix;
     MatrixD.Invert(ref m_worldMatrix, out m_invWorldMatrix);
     m_sizeLod0 = sizeLod0;
     m_localAABB = new BoundingBoxD(Vector3D.Zero, new Vector3D(sizeLod0 * MyVoxelConstants.RENDER_CELL_SIZE_IN_METRES));
     for (int lod = 0; lod < m_lodLevels.Length; ++lod)
     {
         m_lodLevels[lod] = new LodLevel(this, lod, ((m_sizeLod0 - 1) >> lod) + 1);
     }
     m_updateQueueItem = new UpdateQueueItem(this);
     m_requestCollector = new RequestCollector(id);
     m_cellHandler = cellProvider;
 }
Ejemplo n.º 4
0
 public MyClipmap(uint id, MyClipmapScaleEnum scaleGroup, MatrixD worldMatrix, Vector3I sizeLod0, IMyClipmapCellHandler cellProvider)
 {
     m_scaleGroup = scaleGroup;
     m_worldMatrix = worldMatrix;
     MatrixD.Invert(ref m_worldMatrix, out m_invWorldMatrix);
     m_sizeLod0 = sizeLod0;
     m_localAABB = new BoundingBoxD(Vector3D.Zero, new Vector3D(sizeLod0 * MyVoxelConstants.RENDER_CELL_SIZE_IN_METRES));
     for (int lod = 0; lod < m_lodLevels.Length; ++lod)
     {
         m_lodLevels[lod] = new LodLevel(this, lod, ((m_sizeLod0 - 1) >> lod) + 1);
     }
     m_updateQueueItem = new UpdateQueueItem(this);
     m_requestCollector = new RequestCollector(id);
     m_cellHandler = cellProvider;
 }
Ejemplo n.º 5
0
 public MyClipmap(uint id, MyClipmapScaleEnum scaleGroup, MatrixD worldMatrix, Vector3I sizeLod0, IMyClipmapCellHandler cellProvider)
 {
     m_scaleGroup  = scaleGroup;
     m_worldMatrix = worldMatrix;
     MatrixD.Invert(ref m_worldMatrix, out m_invWorldMatrix);
     m_sizeLod0  = sizeLod0;
     m_localAABB = new BoundingBoxD(Vector3D.Zero, new Vector3D(sizeLod0 * MyVoxelCoordSystems.RenderCellSizeInMeters(0)));
     for (int lod = 0; lod < m_lodLevels.Length; ++lod)
     {
         var sizeShift = lod + MyVoxelCoordSystems.RenderCellSizeInLodVoxelsShiftDelta(lod);
         m_lodLevels[lod] = new LodLevel(this, lod, ((m_sizeLod0 - 1) >> sizeShift) + 1);
     }
     m_updateQueueItem  = new UpdateQueueItem(this);
     m_requestCollector = new RequestCollector(id);
     m_cellHandler      = cellProvider;
 }
Ejemplo n.º 6
0
 public MyClipmap(uint id, MyClipmapScaleEnum scaleGroup, MatrixD worldMatrix, Vector3I sizeLod0, IMyClipmapCellHandler cellProvider)
 {
     m_scaleGroup = scaleGroup;
     m_worldMatrix = worldMatrix;
     MatrixD.Invert(ref m_worldMatrix, out m_invWorldMatrix);
     m_sizeLod0 = sizeLod0;
     m_localAABB = new BoundingBoxD(Vector3D.Zero, new Vector3D(sizeLod0 * MyVoxelCoordSystems.RenderCellSizeInMeters(0)));
     for (int lod = 0; lod < m_lodLevels.Length; ++lod)
     {
         var sizeShift = lod + MyVoxelCoordSystems.RenderCellSizeInLodVoxelsShiftDelta(lod);
         m_lodLevels[lod] = new LodLevel(this, lod, ((m_sizeLod0 - 1) >> sizeShift) + 1);
     }
     m_updateQueueItem = new UpdateQueueItem(this);
     m_requestCollector = new RequestCollector(id);
     m_cellHandler = cellProvider;
 }
Ejemplo n.º 7
0
        IEnumerator DownloadPluginCoroutine(string tempdir, UpdateQueueItem item)
        {
            var file = Path.Combine(tempdir, item.Name + ".dll");

            using (var req = UnityWebRequest.Get(item.DownloadUri))
            {
                req.downloadHandler = new DownloadHandlerFile(file);
                yield return(req.SendWebRequest());

                if (req.isNetworkError)
                {
                    Logger.log.Error($"Network error while trying to download update for {item.Plugin.Name}");
                    Logger.log.Error(req.error);
                    yield break;
                }
                if (req.isHttpError)
                {
                    Logger.log.Error($"Server returned an error code while trying to download update for {item.Plugin.Name}");
                    Logger.log.Error(req.error);
                    yield break;
                }

                Logger.log.SuperVerbose("Finished download of new file");
            }

            var pluginDir = Path.GetDirectoryName(item.Plugin.FileName);
            var newFile   = Path.Combine(pluginDir, item.Name + ".dll");

            Logger.log.SuperVerbose($"Moving downloaded file to {newFile}");

            File.Delete(item.Plugin.FileName);
            if (File.Exists(newFile))
            {
                File.Delete(newFile);
            }
            File.Move(file, newFile);

            Logger.log.Info($"{item.Plugin.Name} updated to {item.NewVersion}");
        }