Version 1.1

This commit is contained in:
Pascal
2018-12-04 09:56:12 +01:00
parent d46caa1fa5
commit b5813eb07b
5 changed files with 477 additions and 303 deletions

View File

@@ -1,9 +1,12 @@
Imports System.IO
Imports System.Globalization
Imports System.IO
Imports DevComponents.DotNetBar
Public Class Form1
Public TempDir As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\SMBX Magic Patcher"
Private SMBXPFX As New SMBXPFX
Private ReadOnly fileSeperatorChar As String = CultureInfo.CurrentCulture.TextInfo.ListSeparator
Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
With New OpenFileDialog
@@ -38,23 +41,38 @@ Public Class Form1
Private Sub ButtonX6_Click(sender As Object, e As EventArgs) Handles ButtonX6.Click
With New OpenFileDialog
.Filter = "SMBX-38A Levels (*.lvl)|*.lvl"
.Multiselect = True
If .ShowDialog = Not DialogResult.OK Then
Exit Sub
End If
TextBoxX_OriginalLevelApply.Text = .FileName
TextBoxX_OriginalLevelApply.Text = GetFileListAsString(.FileNames)
End With
End Sub
Private Sub ButtonX4_Click(sender As Object, e As EventArgs) Handles ButtonX4.Click
With New OpenFileDialog
.Filter = "All supported files (*.plx, *.slp)|*.plx;*.slp"
.Multiselect = True
If .ShowDialog = Not DialogResult.OK Then
Exit Sub
End If
TextBoxX_PatchFileApply.Text = .FileName
TextBoxX_PatchFileApply.Text = GetFileListAsString(.FileNames)
End With
End Sub
Private Function GetFileListAsString(files As String()) As String
Dim str As String = String.Empty
For Each file As String In files
If Not String.IsNullOrEmpty(str) Then
str &= fileSeperatorChar
End If
str &= file
Next
Return str
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button_CreatePatch.Click
CircularProgress_Create.IsRunning = True
Button_CreatePatch.Enabled = False
@@ -73,9 +91,12 @@ Public Class Form1
SwitchButton_Scripts.Value,
SwitchButton_Liquids.Value)
Case 1 : MessageBoxEx.Show("Done", "Patch succesfully created!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 2 : MessageBoxEx.Show("Done", "There is an error at packing!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case Else : MessageBoxEx.Show("Done", "There is an unknown error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 1
MessageBoxEx.Show("Patch succesfully created!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 2
MessageBoxEx.Show("There is an error at packing!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case Else
MessageBoxEx.Show("There is an unknown error!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select
Button_CreatePatch.Enabled = True
@@ -86,18 +107,40 @@ Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button_ApplyPatch.Click
CircularProgress_Apply.IsRunning = True
LabelX14.Visible = True
Button_ApplyPatch.Enabled = True
Button_ApplyPatch.Enabled = False
Select Case SMBXPFX.ApplyPatch(TextBoxX_OriginalLevelApply.Text, TextBoxX_PatchFileApply.Text)
Case 1 : MessageBoxEx.Show("Done", "Patch applied succesfully!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 2 : MessageBoxEx.Show("Done", "There is an error at extracting files! Or maybe this isn't a valid patch file.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 3 : MessageBoxEx.Show("Done", "This isn't a valid patch file.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case Else : MessageBoxEx.Show("Done", "There is an unknown error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select
ApplyPatch(TextBoxX_PatchFileApply.Text.Split(fileSeperatorChar),
TextBoxX_OriginalLevelApply.Text.Split(fileSeperatorChar))
LabelX14.Visible = False
CircularProgress_Apply.IsRunning = False
Button_ApplyPatch.Enabled = False
Button_ApplyPatch.Enabled = True
End Sub
Private Sub ApplyPatch(patchFiles As String(), levelFiles As String())
Dim noErrors As Integer = 0
For Each levelFile As String In levelFiles
Dim msgEnd As String = "<br/><br/>Level file:<br/>" & levelFile
Select Case SMBXPFX.ApplyPatch(levelFile, patchFiles)
Case 1
noErrors += 1
Case 2
MessageBoxEx.Show("There is an error at extracting files! Or maybe this isn't a valid patch file." & msgEnd, "Done", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case 3
MessageBoxEx.Show("This isn't a valid patch file." & msgEnd, "Done", MessageBoxButtons.OK, MessageBoxIcon.Error)
Case Else
MessageBoxEx.Show("There is an unknown error!" & msgEnd, "Done", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select
Next
If noErrors >= levelFiles.Length Then
MessageBoxEx.Show("All patches applied succesfully!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
ElseIf noErrors < levelFiles.Length Then
MessageBoxEx.Show("All patches applied, but there seems to be some errors!", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBoxEx.Show("No patch applied! There are some errors.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub CheckBoxX2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBoxX2.CheckedChanged
@@ -105,21 +148,52 @@ Public Class Form1
TextBoxX_OriginalLevelCreate.Enabled = CheckBoxX2.Checked
End Sub
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
Select Case TabControl1.SelectedIndex
Case 0 : Me.Size = New Point(368, 157)
Case 1 : Me.Size = New Point(368, 354)
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl2.SelectedTabChanged
Select Case TabControl2.SelectedTabIndex
Case 0 : Size = New Point(358, 157)
Case 1 : Size = New Point(358, 354)
End Select
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Not Directory.Exists(TempDir) Then Directory.CreateDirectory(TempDir)
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
CheckAndApplyCommandLinePatches()
End Sub
Private Sub CheckAndApplyCommandLinePatches()
Dim cmds As String() = Environment.GetCommandLineArgs
Dim hasCmds As Boolean = cmds.Length > 1
Dim patchFiles As New List(Of String)
Dim levelFiles As New List(Of String)
If hasCmds Then
For i = 0 To cmds.Length - 1
Dim cmd As String = cmds(i)
Select Case Path.GetExtension(cmd).ToLower
Case ".lvl"
levelFiles.Add(cmd)
Case ".plx", ".slp"
patchFiles.Add(cmd)
End Select
Next
End If
TextBoxX_OriginalLevelApply.Text = GetFileListAsString(levelFiles.ToArray)
TextBoxX_PatchFileApply.Text = GetFileListAsString(patchFiles.ToArray)
If patchFiles.Any AndAlso levelFiles.Any Then
Button_ApplyPatch.PerformClick()
End If
End Sub
End Class
Public Class SMBXPFX
Shared Function CreatePatch(OriginalLevel As String, PatchedLevel As String, PatchFile As String, Optional UseCustomOriginalLevelFile As Boolean = False, Optional CopyBlocksEtc As Boolean = True, Optional CopyGraphics As Boolean = True, Optional CopyConfigurations As Boolean = True, Optional CopyEvents As Boolean = True, Optional CopyLayers As Boolean = True, Optional CopyVariables As Boolean = True, Optional CopyScripts As Boolean = True, Optional CopyLiquids As Boolean = True) As Integer
Public Function CreatePatch(OriginalLevel As String, PatchedLevel As String, PatchFile As String, Optional UseCustomOriginalLevelFile As Boolean = False, Optional CopyBlocksEtc As Boolean = True, Optional CopyGraphics As Boolean = True, Optional CopyConfigurations As Boolean = True, Optional CopyEvents As Boolean = True, Optional CopyLayers As Boolean = True, Optional CopyVariables As Boolean = True, Optional CopyScripts As Boolean = True, Optional CopyLiquids As Boolean = True) As Integer
Dim CheckIfFilesExists = True
If Not UseCustomOriginalLevelFile Then
OriginalLevel = Application.StartupPath & "\Data\emptylevel\emptylevel.dat"
@@ -134,20 +208,20 @@ Public Class SMBXPFX
'Add differences to patch:
For Each l As String In PLevel
If Not l Is "" And Not OLevel.Contains(l) And Not l.StartsWith("SMBXFile") Then
If l.StartsWith("P1|") Or l.StartsWith("P2|") Or l.StartsWith("M|") Then GoTo SkipCheck
If l.StartsWith("B|") And Not CopyBlocksEtc Then GoTo SkipCheck 'Blcks
If l.StartsWith("N|") And Not CopyBlocksEtc Then GoTo SkipCheck 'NPCs
If l.StartsWith("T|") And Not CopyBlocksEtc Then GoTo SkipCheck 'Backgrounds
If l.StartsWith("Q|") And Not CopyLiquids Then GoTo SkipCheck 'Liquids
If l.StartsWith("CB|") And Not CopyConfigurations Then GoTo SkipCheck 'Custom Block Configurations
If l.StartsWith("CT|") And Not CopyConfigurations Then GoTo SkipCheck 'Custom Backgrounds Configurations
If l.StartsWith("L|") And Not CopyLayers Then GoTo SkipCheck 'Layer
If l.StartsWith("E|") And Not CopyEvents Then GoTo SkipCheck 'Events
If l.StartsWith("V|") And Not CopyVariables Then GoTo SkipCheck 'Variables
If l.StartsWith("S|") And Not CopyScripts Then GoTo SkipCheck 'Scripts
If l.StartsWith("Su|") And Not CopyScripts Then GoTo SkipCheck 'Scripts
If l.StartsWith("P1|") Or l.StartsWith("P2|") Or l.StartsWith("M|") Then Continue For
If l.StartsWith("B|") And Not CopyBlocksEtc Then Continue For 'Blcks
If l.StartsWith("N|") And Not CopyBlocksEtc Then Continue For 'NPCs
If l.StartsWith("T|") And Not CopyBlocksEtc Then Continue For 'Backgrounds
If l.StartsWith("Q|") And Not CopyLiquids Then Continue For 'Liquids
If l.StartsWith("CB|") And Not CopyConfigurations Then Continue For 'Custom Block Configurations
If l.StartsWith("CT|") And Not CopyConfigurations Then Continue For 'Custom Backgrounds Configurations
If l.StartsWith("L|") And Not CopyLayers Then Continue For 'Layer
If l.StartsWith("E|") And Not CopyEvents Then Continue For 'Events
If l.StartsWith("V|") And Not CopyVariables Then Continue For 'Variables
If l.StartsWith("S|") And Not CopyScripts Then Continue For 'Scripts
If l.StartsWith("Su|") And Not CopyScripts Then Continue For 'Scripts
If Not PFile.Contains(l) Then PFile.Add(l) 'Finaly add line to patch
SkipCheck: End If
End If
Next
'Check for custom graphics:
@@ -208,148 +282,100 @@ SkipCheck: End If
Return 1
End Function
Shared Function ApplyPatch(OriginalLevel As String, PatchFile As String) As Integer
Public Function ApplyPatch(originalLevel As String, patchFiles As String()) As Integer
Dim PatchTyp As Integer = 0 '0 = plx | 1 = slp
Dim PLevel As List(Of String) = File.ReadAllLines(OriginalLevel).ToList
Dim PLevel As List(Of String) = File.ReadAllLines(originalLevel).ToList
Dim TempLeveldir As String = Form1.TempDir & "\TempLevel" : Dim TempLevelfiledir As String = TempLeveldir & "\data"
Dim oLeveldir As String = Path.GetDirectoryName(OriginalLevel) : Dim oLevelfiledir As String = oLeveldir & "\" & Path.GetFileNameWithoutExtension(OriginalLevel)
Dim oLeveldir As String = Path.GetDirectoryName(originalLevel) : Dim oLevelfiledir As String = oLeveldir & "\" & Path.GetFileNameWithoutExtension(originalLevel)
If Directory.Exists(TempLeveldir) Then Directory.Delete(TempLeveldir, True) : Application.DoEvents()
Directory.CreateDirectory(TempLeveldir) : Application.DoEvents()
'Check Patchtyp:
Select Case Path.GetExtension(PatchFile)
Case ".slp" : PatchTyp = 1
Case ".plx" : PatchTyp = 0
Case Else : Return 3
End Select
For Each patchFile As String In patchFiles
'Check Patchtyp:
Select Case Path.GetExtension(patchFile)
Case ".slp"
PatchTyp = 1
Case ".plx"
PatchTyp = 0
Case Else
Return 3
End Select
'Extract with 7-zip:
If PatchTyp = 0 Then
Dim sevenzip As New Process
If Environment.Is64BitOperatingSystem Then
sevenzip.StartInfo.FileName = Application.StartupPath & "\Data\7-Zip\x64\7z.exe"
Else : sevenzip.StartInfo.FileName = Application.StartupPath & "\Data\7-Zip\x86\7z.exe"
'Extract with 7-zip:
If PatchTyp = 0 Then
Dim sevenzip As New Process
If IntPtr.Size = 8 Then
sevenzip.StartInfo.FileName = Application.StartupPath & "\Data\7-Zip\x64\7z.exe"
Else
sevenzip.StartInfo.FileName = Application.StartupPath & "\Data\7-Zip\x86\7z.exe"
End If
Dim password As String = "SMBXMagicPatcher"
sevenzip.StartInfo.Arguments = String.Format("x ""{1}"" -p{0} -o""{2}"" -r", password, patchFile, TempLeveldir & "\")
sevenzip.StartInfo.UseShellExecute = False
sevenzip.StartInfo.CreateNoWindow = True
sevenzip.Start()
Do Until sevenzip.HasExited = True : Application.DoEvents() : Loop
If sevenzip.ExitCode <> 0 Then
Return 2 'Error at packing
End If
End If
Dim password As String = "SMBXMagicPatcher"
sevenzip.StartInfo.Arguments = String.Format("x ""{1}"" -p{0} -o""{2}"" -r", password, Form1.TextBoxX_PatchFileApply.Text, TempLeveldir & "\")
sevenzip.StartInfo.UseShellExecute = False
sevenzip.StartInfo.CreateNoWindow = True
sevenzip.Start()
Do Until sevenzip.HasExited = True : Application.DoEvents() : Loop
If sevenzip.ExitCode <> 0 Then
Return 2 'Error at packing
'Load patchlevel:
Dim PFile() As String = {}
Select Case PatchTyp
Case 0 : PFile = File.ReadAllLines(TempLeveldir & "\data.dat")
Case 1 : PFile = File.ReadAllLines(patchFile)
End Select
'Apply lines in patch to the level:
For Each l As String In PFile
If Not PLevel.Contains(l) Then PLevel.Add(l)
Next
'Copy and replace files
If PatchTyp = 0 Then
Dim pListFilesInLeveldir() As String = Directory.GetFiles(TempLeveldir)
Dim pListFilesInLevelfiledir() As String = {}
If Directory.Exists(TempLevelfiledir) Then
pListFilesInLevelfiledir = Directory.GetFiles(TempLevelfiledir)
End If
Dim oListFilesInLeveldir() As String = Directory.GetFiles(oLeveldir)
Dim oListFilesInLevelfiledir() As String = {}
If Directory.Exists(oLevelfiledir) Then
oListFilesInLevelfiledir = Directory.GetFiles(oLevelfiledir)
End If
If Not Directory.Exists(oLevelfiledir) And oListFilesInLevelfiledir.Count > 0 Then
Directory.CreateDirectory(oLevelfiledir)
End If
For i As Integer = 0 To pListFilesInLeveldir.Count - 1
If Not pListFilesInLeveldir(i) = TempLeveldir & "\data.dat" Then _
File.Copy(pListFilesInLeveldir(i), oLeveldir & "\" & Path.GetFileName(pListFilesInLeveldir(i)), True)
Application.DoEvents() : Next
For i As Integer = 0 To pListFilesInLevelfiledir.Count - 1
File.Copy(pListFilesInLevelfiledir(i), oLevelfiledir & "\" & Path.GetFileName(pListFilesInLevelfiledir(i)), True)
Application.DoEvents() : Next
End If
End If
'Load patchlevel:
Dim PFile() As String = {}
Select Case PatchTyp
Case 0 : PFile = File.ReadAllLines(TempLeveldir & "\data.dat")
Case 1 : PFile = File.ReadAllLines(PatchFile)
End Select
'Apply lines in patch to the level:
For Each l As String In PFile
If Not PLevel.Contains(l) Then PLevel.Add(l)
Next
'Copy and replace files
If PatchTyp = 0 Then
Dim pListFilesInLeveldir() As String = Directory.GetFiles(TempLeveldir) : Dim pListFilesInLevelfiledir() As String = {} : If Directory.Exists(TempLevelfiledir) Then pListFilesInLevelfiledir = Directory.GetFiles(TempLevelfiledir)
Dim oListFilesInLeveldir() As String = Directory.GetFiles(oLeveldir) : Dim oListFilesInLevelfiledir() As String = {} : If Directory.Exists(oLevelfiledir) Then oListFilesInLevelfiledir = Directory.GetFiles(oLevelfiledir)
If Not Directory.Exists(oLevelfiledir) And oListFilesInLevelfiledir.Count > 0 Then Directory.CreateDirectory(oLevelfiledir)
For i As Integer = 0 To pListFilesInLeveldir.Count - 1
If Not pListFilesInLeveldir(i) = TempLeveldir & "\data.dat" Then
File.Copy(pListFilesInLeveldir(i), oLeveldir & "\" & Path.GetFileName(pListFilesInLeveldir(i)), True) : End If
Application.DoEvents() : Next
For i As Integer = 0 To pListFilesInLevelfiledir.Count - 1
File.Copy(pListFilesInLevelfiledir(i), oLevelfiledir & "\" & Path.GetFileName(pListFilesInLevelfiledir(i)), True)
Application.DoEvents() : Next
End If
'Wirte new level file
Do While PLevel.Contains("")
PLevel.Remove("") : Application.DoEvents() : Loop
File.WriteAllLines(OriginalLevel, PLevel.ToArray)
PLevel.Remove("") : Application.DoEvents()
Loop
File.WriteAllLines(originalLevel, PLevel.ToArray)
Return 1
End Function
Shared Function GetHelp() As String
Return "Not aviable yet!"
End Function
End Class
Public Class PatchSettings
Private _CopyBlocksEtc As Boolean = True
Private _CopyGraphics As Boolean = True
Private _CopyConfigs As Boolean = True
Private _CopyEvents As Boolean = True
Private _CopyLayers As Boolean = True
Private _CopyScripts As Boolean = True
Private _CopyVariables As Boolean = True
Public Property CopyBlocksEtc As Boolean
Get
Return _CopyBlocksEtc
End Get
Set(value As Boolean)
_CopyBlocksEtc = value
End Set
End Property
Public Property CopyGraphics As Boolean
Get
Return _CopyGraphics
End Get
Set(value As Boolean)
_CopyGraphics = value
End Set
End Property
Public Property CopyConfigs As Boolean
Get
Return _CopyConfigs
End Get
Set(value As Boolean)
_CopyConfigs = value
End Set
End Property
Public Property CopyEvents As Boolean
Get
Return _CopyEvents
End Get
Set(value As Boolean)
_CopyEvents = value
End Set
End Property
Public Property CopyLayers As Boolean
Get
Return _CopyLayers
End Get
Set(value As Boolean)
_CopyLayers = value
End Set
End Property
Public Property CopyScripts As Boolean
Get
Return _CopyScripts
End Get
Set(value As Boolean)
_CopyScripts = value
End Set
End Property
Public Property CopyVariables As Boolean
Get
Return _CopyVariables
End Get
Set(value As Boolean)
_CopyVariables = value
End Set
End Property
Public Property CopyBlocksEtc As Boolean = True
Public Property CopyGraphics As Boolean = True
Public Property CopyConfigs As Boolean = True
Public Property CopyEvents As Boolean = True
Public Property CopyLayers As Boolean = True
Public Property CopyScripts As Boolean = True
Public Property CopyVariables As Boolean = True
End Class