Skip to content

Commit 10e5e17

Browse files
authored
Bugfixes
Also Code Cleanup. After all this time, the code is still trash.
1 parent c981df8 commit 10e5e17

File tree

3 files changed

+80
-89
lines changed

3 files changed

+80
-89
lines changed

AutoCrispy/AutoCrispy/Form1.Designer.vb

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AutoCrispy/AutoCrispy/Form1.vb

Lines changed: 62 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Public Class Form1
1212
Dim PyEmbedded As Boolean
1313
Dim ChainedModels As New List(Of Settings)
1414
Dim HandOff As ExtSettings
15-
Dim vbQuote As Char = ControlChars.Quote
1615

1716
Dim CaffePath As String
1817
Dim WaifuNcnnPath As String
@@ -177,28 +176,14 @@ Public Class Form1
177176
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
178177
Application.CurrentCulture = New Globalization.CultureInfo("EN-US")
179178
LoadBindingString()
180-
ChainThumbs.Images.Add(Shrink(My.Resources._0, 64, 64))
181-
ChainThumbs.Images.Add(Shrink(My.Resources._1, 64, 64))
182-
ChainThumbs.Images.Add(Shrink(My.Resources._2, 64, 64))
183-
ChainThumbs.Images.Add(Shrink(My.Resources._3, 64, 64))
184-
ChainThumbs.Images.Add(Shrink(My.Resources._4, 64, 64))
185-
ChainThumbs.Images.Add(Shrink(My.Resources._5, 64, 64))
186-
ChainThumbs.Images.Add(Shrink(My.Resources._6, 64, 64))
179+
PreloadImageList()
187180
StartUpCheckEXE()
188181
StartUpCheckPy()
189-
If PyPaths.Count > 0 Then
190-
ExeComboBox.Items.Add("Python")
191-
PyScript.SelectedIndex = 0
192-
If PyModels.Count > 0 Then
193-
PyModel.SelectedIndex = 0
194-
Else
195-
MsgBox("No ESRGAN Models Found!", MsgBoxStyle.Critical)
196-
End If
197-
End If
198182
If ExeComboBox.Items.Count > 0 Then
199183
ExeComboBox.SelectedIndex = 0
200184
SetSettingsWindow()
201185
End If
186+
TabGroup.Refresh()
202187
WatchDogButton.Select()
203188
End Sub
204189

@@ -258,6 +243,25 @@ Public Class Form1
258243
Next
259244
Next
260245
Next
246+
If PyPaths.Count > 0 Then
247+
ExeComboBox.Items.Add("Python")
248+
PyScript.SelectedIndex = 0
249+
If PyModels.Count > 0 Then
250+
PyModel.SelectedIndex = 0
251+
Else
252+
MsgBox("No ESRGAN Models Found!", MsgBoxStyle.Critical)
253+
End If
254+
End If
255+
End Sub
256+
257+
Private Sub PreloadImageList()
258+
ChainThumbs.Images.Add(Shrink(My.Resources._0, 64, 64))
259+
ChainThumbs.Images.Add(Shrink(My.Resources._1, 64, 64))
260+
ChainThumbs.Images.Add(Shrink(My.Resources._2, 64, 64))
261+
ChainThumbs.Images.Add(Shrink(My.Resources._3, 64, 64))
262+
ChainThumbs.Images.Add(Shrink(My.Resources._4, 64, 64))
263+
ChainThumbs.Images.Add(Shrink(My.Resources._5, 64, 64))
264+
ChainThumbs.Images.Add(Shrink(My.Resources._6, 64, 64))
261265
End Sub
262266

263267
Private Sub SaveBindingString()
@@ -320,7 +324,7 @@ Public Class Form1
320324
Private Sub ChainSave_Click(sender As Object, e As EventArgs) Handles ChainSave.Click
321325
Using SFD As New SaveFileDialog With {.Filter = "XML Files|*.xml|All Files|*.*"}
322326
If SFD.ShowDialog = DialogResult.OK Then
323-
Serialize(ChainedModels, SFD.FileName)
327+
File.WriteAllText(SFD.FileName, Serialize(ChainedModels))
324328
End If
325329
End Using
326330
End Sub
@@ -330,7 +334,7 @@ Public Class Form1
330334
If OFD.ShowDialog = DialogResult.OK Then
331335
ChainPreview.Clear()
332336
ChainedModels.Clear()
333-
ChainedModels = Deserialize(Of List(Of Settings))(OFD.FileName)
337+
ChainedModels = Deserialize(Of List(Of Settings))(File.ReadAllText(OFD.FileName))
334338
For Each Model As Settings In ChainedModels
335339
AddModelToChain(Model.LoadedMode)
336340
Next
@@ -356,10 +360,10 @@ Public Class Form1
356360
Private Sub ChainContextEdit_Click(sender As Object, e As EventArgs) Handles ChainContextEdit.Click
357361
If ChainPreview.SelectedItems.Count > 0 Then
358362
Dim ItemIndex = (ChainPreview.SelectedItems(0).Position.X - 21) / (ChainPreview.SelectedItems(0).Bounds.Width + 1)
359-
Using ECD As New EditChainDialog(SerializeString(ChainedModels(ItemIndex)))
363+
Using ECD As New EditChainDialog(Serialize(ChainedModels(ItemIndex)))
360364
If ECD.ShowDialog = DialogResult.OK Then
361365
Try
362-
Dim NewSettings As Settings = DeserializeString(Of Settings)(ECD.ResultText)
366+
Dim NewSettings As Settings = Deserialize(Of Settings)(ECD.ResultText)
363367
ChainedModels(ItemIndex) = NewSettings
364368
Catch ex As Exception
365369
MsgBox("Error: New settings could not be parsed.")
@@ -382,9 +386,11 @@ Public Class Form1
382386

383387
Private Sub ChainPreview_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles ChainPreview.DragOver
384388
Dim lvi As ListViewItem = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
385-
Dim Offset As Size = Size.Subtract(Cursor.Size, New Size(Cursor.HotSpot.X, Cursor.HotSpot.Y))
386-
lvi.Position = Point.Subtract(ChainPreview.PointToClient(New Point(e.X, e.Y)), Offset)
387-
e.Effect = DragDropEffects.Move
389+
If lvi IsNot Nothing Then
390+
Dim Offset As Size = Size.Subtract(Cursor.Size, New Size(Cursor.HotSpot.X, Cursor.HotSpot.Y))
391+
lvi.Position = Point.Subtract(ChainPreview.PointToClient(New Point(e.X, e.Y)), Offset)
392+
e.Effect = DragDropEffects.Move
393+
End If
388394
End Sub
389395

390396
Private Sub ChainPreview_MouseUp(sender As Object, e As MouseEventArgs) Handles ChainPreview.MouseUp
@@ -501,8 +507,8 @@ Public Class Form1
501507
#Region "Background"
502508

503509
Private Sub WatchDog_Tick(sender As Object, e As EventArgs) Handles WatchDog.Tick
504-
Dim Source As List(Of String) = GetFileNameListNoExtension(InputTextBox.Text)
505-
Dim Dest As List(Of String) = GetFileNameListNoExtension(OutputTextBox.Text)
510+
Dim Source As List(Of String) = GetFileNameList(InputTextBox.Text, False)
511+
Dim Dest As List(Of String) = GetFileNameList(OutputTextBox.Text, False)
506512
Dim FileCheck As Boolean = Dest.SequenceEqual(Source)
507513
If FileCheck = True Or Source.Count = 0 Then
508514
WaitScale = Math.Min(WaitScale + 1, 100)
@@ -578,7 +584,7 @@ Public Class Form1
578584
Next
579585
For Each Model In ChainedModels
580586
Dim NewImages As New List(Of String)
581-
Dim DiffImages = GetFileNameList(ChainPaths(0)).Except(GetFileNameList(HandOff.OutputPath))
587+
Dim DiffImages = GetFileNameList(ChainPaths(0), True).Except(GetFileNameList(HandOff.OutputPath, True))
582588
For Each NewImage As String In DiffImages
583589
Dim AcceptExt As Boolean = Model.LoadedExtensions.Contains(Path.GetExtension(NewImage).ToLower)
584590
If File.Exists(ChainPaths(0) & "\" & NewImage) AndAlso AcceptExt = True Then
@@ -600,7 +606,8 @@ Public Class Form1
600606
Else
601607
For j = 0 To NewImages.Count - 1
602608
Dim NewImage As String = ChainPaths(1) & "\" & Path.GetFileName(NewImages(j))
603-
Dim BuildProcess As ProcessStartInfo = New ProcessStartInfo(Model.LoadedPath, MakeCommand(NewImages(j), NewImage, Model))
609+
Dim BuildProcess As ProcessStartInfo = New ProcessStartInfo(Path.GetFileName(Model.LoadedPath), MakeCommand(NewImages(j), NewImage, Model))
610+
BuildProcess.WorkingDirectory = Directory.GetParent(Model.LoadedPath).FullName
604611
BuildProcess.WindowStyle = ProcessWindowStyle.Hidden
605612
Dim BatchProcess As Process = Process.Start(BuildProcess)
606613
If (j + 1) Mod HandOff.Threads = 0 OrElse (j = NewImages.Count - 1) Then
@@ -624,7 +631,7 @@ Public Class Form1
624631
WorkHorse.ReportProgress(Math.Round(((i * 100) + 1) / Source.Count, 0))
625632
Next
626633
If HandOff.Defringe = True Then
627-
For Each DestImage As String In Source
634+
For Each DestImage As String In Directory.EnumerateFiles(HandOff.OutputPath, "*.png")
628635
Defringe(HandOff.OutputPath & "\" & Path.GetFileName(DestImage))
629636
Next
630637
End If
@@ -781,37 +788,11 @@ Public Class Form1
781788

782789
#Region "XML"
783790

784-
Public Shared Sub Serialize(Of T)(ByVal obj As T, sConfigFilePath As String)
785-
Dim XmlBuddy As New Xml.Serialization.XmlSerializer(GetType(T))
786-
Dim MySettings As New Xml.XmlWriterSettings()
787-
MySettings.Indent = True
788-
MySettings.CloseOutput = True
789-
Dim MyWriter As Xml.XmlWriter = Xml.XmlWriter.Create(sConfigFilePath, MySettings)
790-
XmlBuddy.Serialize(MyWriter, obj)
791-
MyWriter.Flush()
792-
MyWriter.Close()
793-
End Sub
794-
795-
Public Shared Function Deserialize(Of T)(ByVal xml As String) As T
796-
Dim XmlBuddy As New Xml.Serialization.XmlSerializer(GetType(T))
797-
Dim fs As New FileStream(xml, FileMode.Open)
798-
Dim reader As New Xml.XmlTextReader(fs)
799-
If XmlBuddy.CanDeserialize(reader) Then
800-
Dim tempObject As Object = DirectCast(XmlBuddy.Deserialize(reader), T)
801-
reader.Close()
802-
Return tempObject
803-
Else
804-
Return Nothing
805-
End If
806-
End Function
807-
808-
Public Shared Function SerializeString(Of T)(ByVal Source As T) As String
791+
Public Shared Function Serialize(Of T)(ByVal Source As T) As String
809792
Dim Result As String = ""
810793
Using XmlStream As New MemoryStream
811794
Dim XmlSerializer As New Xml.Serialization.XmlSerializer(GetType(T))
812-
Dim XmlSettings As New Xml.XmlWriterSettings()
813-
XmlSettings.Indent = True
814-
XmlSettings.CloseOutput = True
795+
Dim XmlSettings As New Xml.XmlWriterSettings With {.Indent = True, .CloseOutput = True}
815796
Dim XmlWriter As Xml.XmlWriter = Xml.XmlWriter.Create(XmlStream, XmlSettings)
816797
XmlSerializer.Serialize(XmlWriter, Source)
817798
Dim XmlReader As New StreamReader(XmlStream)
@@ -824,21 +805,22 @@ Public Class Form1
824805
Return Result
825806
End Function
826807

827-
Public Shared Function DeserializeString(Of T)(ByVal Xml As String) As T
828-
Dim XmlSerializer As New Xml.Serialization.XmlSerializer(GetType(T))
829-
Dim XmlStream As New MemoryStream
830-
Dim XmlWriter As New StreamWriter(XmlStream)
831-
XmlWriter.Write(Xml)
832-
XmlWriter.Flush()
833-
XmlStream.Position = 0
834-
Dim XmlReader As New Xml.XmlTextReader(XmlStream)
835-
If XmlSerializer.CanDeserialize(XmlReader) Then
836-
Dim tempObject As Object = DirectCast(XmlSerializer.Deserialize(XmlReader), T)
837-
XmlReader.Close()
838-
Return tempObject
839-
Else
840-
Return Nothing
841-
End If
808+
Public Shared Function Deserialize(Of T)(ByVal Xml As String) As T
809+
Dim Result As New Object
810+
Using XmlStream As New MemoryStream
811+
Dim XmlSerializer As New Xml.Serialization.XmlSerializer(GetType(T))
812+
Dim XmlWriter As New StreamWriter(XmlStream)
813+
XmlWriter.Write(Xml)
814+
XmlWriter.Flush()
815+
XmlStream.Position = 0
816+
Dim XmlReader As New Xml.XmlTextReader(XmlStream)
817+
If XmlSerializer.CanDeserialize(XmlReader) Then
818+
Result = DirectCast(XmlSerializer.Deserialize(XmlReader), T)
819+
End If
820+
XmlWriter.Close()
821+
XmlReader.Dispose()
822+
End Using
823+
Return Result
842824
End Function
843825

844826
#End Region
@@ -904,18 +886,14 @@ Public Class Form1
904886
Return ""
905887
End Function
906888

907-
Private Function GetFileNameList(Source As String) As List(Of String)
889+
Private Function GetFileNameList(Source As String, Extension As Boolean) As List(Of String)
908890
Dim Result As New List(Of String)
909-
For Each File As String In Directory.GetFileSystemEntries(Source, "*.*", SearchOption.TopDirectoryOnly).ToList
910-
Result.Add(Path.GetFileName(File))
911-
Next
912-
Return Result
913-
End Function
914-
915-
Private Function GetFileNameListNoExtension(Source As String) As List(Of String)
916-
Dim Result As New List(Of String)
917-
For Each File As String In Directory.GetFileSystemEntries(Source, "*.*", SearchOption.TopDirectoryOnly).ToList
918-
Result.Add(Path.GetFileNameWithoutExtension(File))
891+
For Each File As String In Directory.GetFiles(Source).ToList
892+
If Extension = True Then
893+
Result.Add(Path.GetFileName(File))
894+
Else
895+
Result.Add(Path.GetFileNameWithoutExtension(File))
896+
End If
919897
Next
920898
Return Result
921899
End Function
@@ -992,7 +970,7 @@ Public Class Form1
992970
End Function
993971

994972
Private Function Quote(Source As String) As String
995-
Return vbQuote & Source & vbQuote
973+
Return ControlChars.Quote & Source & ControlChars.Quote
996974
End Function
997975

998976
#End Region

AutoCrispy/AutoCrispy/My Project/AssemblyInfo.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
3131
' by using the '*' as shown below:
3232
' <Assembly: AssemblyVersion("1.0.*")>
3333

34-
<Assembly: AssemblyVersion("1.0.1.8")>
35-
<Assembly: AssemblyFileVersion("1.0.1.8")>
34+
<Assembly: AssemblyVersion("1.0.1.9")>
35+
<Assembly: AssemblyFileVersion("1.0.1.9")>

0 commit comments

Comments
 (0)