file watcher for debugging maps/scripts

This commit is contained in:
nilllzz 2018-02-20 20:21:48 +01:00
parent c4c1d4b62c
commit c570609b39
4 changed files with 94 additions and 0 deletions

View File

@ -12677,6 +12677,7 @@
<Compile Include="World\ActionScript\V2\ScriptV2.vb" />
<Compile Include="World\Badge.vb" />
<Compile Include="World\BaseEntity.vb" />
<Compile Include="Debug\DebugFileWatcher.vb" />
<Compile Include="World\Level.vb" />
<Compile Include="World\LevelLoader.vb" />
<Compile Include="World\Lighting.vb" />

View File

@ -0,0 +1,82 @@
''' <summary>
''' This class watches the maps and scripts folders of the project to automatically
''' copy changed files on map reloads to the bin directory.
''' </summary>
Public Class DebugFileWatcher
Private Shared _changedFiles As List(Of String)
Private Shared _watchers As List(Of FileSystemWatcher)
Private Shared _isWatching As Boolean = False
Shared Sub New()
_changedFiles = New List(Of String)()
_watchers = New List(Of FileSystemWatcher)()
End Sub
Public Shared Sub TriggerReload()
' copy all changed files
SyncLock _changedFiles
Dim projectPath = GetProjectPath()
Dim targetPath = AppDomain.CurrentDomain.BaseDirectory
For Each changedFile In _changedFiles
Dim relativeFile = changedFile.Remove(0, projectPath.Length + 1)
Dim targetFile = Path.Combine(targetPath, relativeFile)
File.Copy(changedFile, targetFile, True)
Next
' clear the changed files afterwards
_changedFiles.Clear()
End SyncLock
End Sub
Private Shared Function GetProjectPath() As String
Return New DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName
End Function
Public Shared Sub StartWatching()
If Not _isWatching Then
_isWatching = True
Dim projectPath = GetProjectPath()
' MAPS
Dim mapsPath = Path.Combine(projectPath, "maps")
Dim mapsWatcher = New FileSystemWatcher With {
.Path = mapsPath,
.NotifyFilter = NotifyFilters.LastWrite,
.IncludeSubdirectories = True
}
AddHandler mapsWatcher.Changed, AddressOf OnChanged
mapsWatcher.EnableRaisingEvents = True
_watchers.Add(mapsWatcher)
' SCRIPTS
Dim scriptsPath = Path.Combine(projectPath, "Scripts")
Dim scriptsWatcher = New FileSystemWatcher With {
.Path = scriptsPath,
.NotifyFilter = NotifyFilters.LastWrite,
.IncludeSubdirectories = True
}
AddHandler scriptsWatcher.Changed, AddressOf OnChanged
scriptsWatcher.EnableRaisingEvents = True
_watchers.Add(scriptsWatcher)
End If
End Sub
Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
SyncLock _changedFiles
Dim file = e.FullPath
If Not _changedFiles.Contains(file) Then
Logger.Debug("File changed: " + file)
_changedFiles.Add(file)
End If
End SyncLock
End Sub
End Class

View File

@ -138,6 +138,11 @@ Public Class OverworldScreen
'Load the particle texture.
Me._particlesTexture = TextureManager.GetTexture("GUI\Overworld\Particles")
' start watching files for changes
#If DEBUG Then
DebugFileWatcher.StartWatching()
#End If
End Sub
''' <summary>

View File

@ -698,6 +698,12 @@ Public Class Level
''' </summary>
''' <param name="Levelpath">The path to load the level from. Start with "|" to prevent loading a levelfile.</param>
Public Sub Load(ByVal Levelpath As String)
' copy all changed files
#If DEBUG Then
DebugFileWatcher.TriggerReload()
#End If
' Create a parameter array to pass over to the LevelLoader:
Dim params As New List(Of Object)
params.AddRange({Levelpath, False, New Vector3(0, 0, 0), 0, New List(Of String)})