Fixed Single Cry Import

Fixed single cry import to immediatly change loaded cry when a cry is imported. Also, the import all function isn't done for broad use. It skips past the default mons(to index 440), and loads specifically for my rom.
This commit is contained in:
Dreuu 2018-08-03 21:14:41 -04:00
parent 42dcf96745
commit 414b4e680e
18 changed files with 356 additions and 39 deletions

Binary file not shown.

View File

@ -24,18 +24,24 @@ Module CryFunctions
Public Function LoadCry(index As Integer, cryTable As Integer) As Cry
Dim CryToLoad As New Cry
'If ledrom Is Nothing Then
' Return False
'End If
' load cry table entry
'ledrom.Seek(cryTable + index * 12)
'Dim someValue = ledrom.ReadInt32()
'Dim cryOffset = ledrom.ReadPointer()
'Dim cryShape = ledrom.ReadInt32()
Dim CryToLoad As New Cry With {
.Index = index
}
Dim convIndex As Integer = -1
If index > 276 Then
Dim offset As Integer = Int32.Parse((GetString(GetINIFileLocation(), header, "CryConversionTable", "")), System.Globalization.NumberStyles.HexNumber)
convIndex = Int32.Parse((ReverseHEX(ReadHEX(LoadedROM, ((offset)) + ((index - 276) * 2), 2))), System.Globalization.NumberStyles.HexNumber)
End If
Dim someValue = Int32.Parse(ReverseHEX((ReadHEX(LoadedROM, (cryTable) + (index * 12), 4))), System.Globalization.NumberStyles.HexNumber)
Dim cryOffset = (Int32.Parse(ReverseHEX((ReadHEX(LoadedROM, (cryTable) + (index * 12) + 4, 4))), System.Globalization.NumberStyles.HexNumber) - &H8000000)
@ -50,7 +56,14 @@ Module CryFunctions
'ledrom.Seek(cryOffset)
CryToLoad.Offset = cryOffset
CryToLoad.Index = index
If convIndex <> -1 Then
someValue = Int32.Parse(ReverseHEX((ReadHEX(LoadedROM, (cryTable) + (convIndex * 12), 4))), System.Globalization.NumberStyles.HexNumber)
cryOffset = (Int32.Parse(ReverseHEX((ReadHEX(LoadedROM, (cryTable) + (convIndex * 12) + 4, 4))), System.Globalization.NumberStyles.HexNumber) - &H8000000)
cryShape = Int32.Parse(ReverseHEX((ReadHEX(LoadedROM, (cryTable) + (convIndex * 12) + 8, 4))), System.Globalization.NumberStyles.HexNumber)
CryToLoad.Offset = cryOffset
End If
'CryToLoad.Compressed = ledrom.ReadUInt16() = &H1
@ -68,14 +81,22 @@ Module CryFunctions
If Not CryToLoad.Compressed Then
' uncompressed, 1 sample per 1 byte of size
CryToLoad.Data = New SByte(CryToLoad.Size - 1) {}
For g As Integer = 0 To CryToLoad.Size - 1
CryToLoad.Data(g) = ByteToSignedInt("&H" & (ReadHEX(LoadedROM, (cryOffset) + 16 + g, 1)))
Dim cryToLoadTemp As String = ReadHEX(LoadedROM, (cryOffset) + 16, CryToLoad.Size - 1)
For g As Integer = 0 To CryToLoad.Size - 2
CryToLoad.Data(g) = ByteToSignedInt("&H" & cryToLoadTemp.Substring(g * 2, 2))
Next
'For g As Integer = 0 To CryToLoad.Size - 1
' CryToLoad.Data(g) = ByteToSignedInt("&H" & (ReadHEX(LoadedROM, (cryOffset) + 16 + g, 1)))
'Next
Else
' compressed, a bit of a hassle
Dim lookup = New SByte() {0, 1, 4, 9, 16, 25, _
36, 49, -64, -49, -36, -25, _
Dim lookup = New SByte() {0, 1, 4, 9, 16, 25,
36, 49, -64, -49, -36, -25,
-16, -9, -4, -1}
Dim start = (cryOffset) + 16
@ -85,29 +106,47 @@ Module CryFunctions
Dim pcmLevel As SByte = 0
Dim data = New List(Of SByte)()
Dim cryData As String = (ReadHEX(LoadedROM, offtrack, CryToLoad.Size))
offtrack = 0
While True
If alignment = 0 Then
pcmLevel = ByteToSignedInt("&H" & (ReadHEX(LoadedROM, offtrack, 1)))
offtrack = offtrack + 1
pcmLevel = ByteToSignedInt("&H" & cryData.Substring(offtrack, 2)) '(ReadHEX(LoadedROM, offtrack, 1)))
offtrack = offtrack + 2 '1
data.Add(pcmLevel)
alignment = &H20
End If
Dim input As Byte = ("&H" & (ReadHEX(LoadedROM, offtrack, 1)))
offtrack = offtrack + 1
Dim input As Byte = ("&H" & cryData.Substring(offtrack, 2)) '(ReadHEX(LoadedROM, offtrack, 1)))
offtrack = offtrack + 2 '1
If alignment < &H20 Then
' first nybble
pcmLevel += lookup(input >> 4)
Try
pcmLevel += lookup(input >> 4)
Catch
If pcmLevel < 0 And lookup(input >> 4) < 0 Then
pcmLevel = -128
Else
pcmLevel = 127
End If
End Try
data.Add(pcmLevel)
End If
' second nybble
pcmLevel += lookup(input And &HF)
Try
pcmLevel += lookup(input And &HF)
Catch
If pcmLevel < 0 And lookup(input And &HF) < 0 Then
pcmLevel = -128
Else
pcmLevel = 127
End If
End Try
data.Add(pcmLevel)
' exit when currentSize >= cry.Size
@ -121,7 +160,7 @@ Module CryFunctions
CryToLoad.Data = data.ToArray()
' bytes needed to recompress
CryToLoad.Size = offtrack - start
CryToLoad.Size = offtrack / 2 'offtrack - start
End If
Return CryToLoad
@ -199,7 +238,7 @@ Module CryFunctions
Public Sub ExportCry(filename As String, cry As Cry)
If Cry.Offset = 0 Then
If cry.Offset = 0 Then
Return
End If
@ -222,10 +261,10 @@ Module CryFunctions
' format: 1 = wave_format_pcm
writer.Write(CUShort(1))
' channel count
writer.Write(Cry.SampleRate)
writer.Write(cry.SampleRate)
' sample rate
' * 1 * 8 / 8
writer.Write(Cry.SampleRate)
writer.Write(cry.SampleRate)
' SampleRate * NumChannels * BitsPerSample/8
' * 8 / 8
writer.Write(CUShort(1))
@ -235,7 +274,7 @@ Module CryFunctions
' data chunk
writer.Write(Encoding.ASCII.GetBytes("data"))
' chunk ID
writer.Write(Cry.Data.Length)
writer.Write(cry.Data.Length)
' chunk size
For Each sample As SByte In cry.Data
writer.Write(CByte(sample + &H80))
@ -249,7 +288,7 @@ Module CryFunctions
Public Function ImportCry(filename As String, CryToLoad As Cry) As Cry
'If Cry.Offset = 0 Then
'If Cry.Offset = 0 Then
' Return
'End If
@ -320,8 +359,8 @@ Module CryFunctions
Return False
End If
'var lookup = new byte[] { 0x0, 0x1, 0x4, 0x9, 0x10, 0x19, 0x24, 0x31, 0xC0, 0xCF, 0xDC, 0xE7, 0xF0, 0xF7, 0xFC, 0xFF };
Dim lookup = New SByte() {0, 1, 4, 9, 16, 25, _
36, 49, -64, -49, -36, -25, _
Dim lookup = New SByte() {0, 1, 4, 9, 16, 25,
36, 49, -64, -49, -36, -25,
-16, -9, -4, -1}
crytosave.Compressed = False
@ -519,4 +558,173 @@ Module CryFunctions
Return True
End Function
Public Function SaveCryNoPrompt(crytosave As Cry, cryTable As Integer, growlTable As Integer) As Boolean
'If crytosave.Offset = 0 Then
'Return False
'End If
'var lookup = new byte[] { 0x0, 0x1, 0x4, 0x9, 0x10, 0x19, 0x24, 0x31, 0xC0, 0xCF, 0xDC, 0xE7, 0xF0, 0xF7, 0xFC, 0xFF };
Dim lookup = New SByte() {0, 1, 4, 9, 16, 25,
36, 49, -64, -49, -36, -25,
-16, -9, -4, -1}
crytosave.Compressed = False
' copy cry data to be written
Dim data = New List(Of Byte)()
If 0 Then
'MsgBox("This should not be enabled!")
'End
' data is compressed in blocks of 1 + 0x20 bytes at a time
' first byte is normal signed PCM data
' following 0x20 bytes are compressed based on previous value
' (for a value not in lookup table, closest value will be chosen instead)
'Console.WriteLine("compressed");
' each block has 0x40 samples
Dim blockCount = crytosave.Data.Length / &H40
If crytosave.Data.Length Mod &H40 > 0 Then
blockCount += 1
End If
' truncates the length of the last block
' so we don't waste space
Dim lastBlockSize = crytosave.Data.Length - crytosave.Data.Length / &H40 * &H40
If lastBlockSize = 0 Then
lastBlockSize = &H21
Else
lastBlockSize = 1 + (lastBlockSize / 2) + (If(lastBlockSize Mod 2 = 0, 0, 1))
End If
Dim blocks = New Byte(blockCount - 1)() {}
For n As Integer = 0 To blockCount - 1
' create new block
If n < blockCount - 1 Then
blocks(n) = New Byte(32) {}
Else
blocks(n) = New Byte(lastBlockSize - 1) {} ' - Not "- 1"
End If
Dim i As Integer = n * &H40
Dim k As Integer = 0
If i < crytosave.Data.Length Then
' set first value
blocks(n)(k) = BitConverter.GetBytes(crytosave.Data(i))(0)
End If
k = k + 1
Dim pcm As SByte
If i < crytosave.Data.Length Then
pcm = crytosave.Data(i)
End If
i = i + 1
Dim j As Integer = 1
While j < &H40 And i < crytosave.Data.Length
' get current sample
Dim sample As SByte = crytosave.Data(i)
i = i + 1
' difference between previous sample and this
Dim diff As Integer = sample - pcm
' check for a perfect match in lookup table
Dim lookupI = -1
For x As Integer = 0 To 15
If lookup(x) = diff Then
lookupI = x
Exit For
End If
Next
' search for the closest match in the table
If lookupI = -1 Then
Dim bestDiff As Integer = 255
For x As Integer = 0 To 15
If Math.Abs(CInt(lookup(x)) - diff) < bestDiff Then
lookupI = x
bestDiff = Math.Abs(lookup(x) - diff)
End If
Next
End If
' set value in block
' on an odd value, increase position in block
If j Mod 2 = 0 Then
blocks(n)(k) = blocks(n)(k) Or CByte(lookupI << 4)
Else
blocks(n)(k) = blocks(n)(k) Or CByte(lookupI)
k = k + 1
End If
' set previous
pcm = sample
j += 1
End While
Next
For n As Integer = 0 To blockCount - 1
data.AddRange(blocks(n))
Next
Else
' uncompressed, copy directly to data
'Console.WriteLine("uncompressed");
For Each s As SByte In crytosave.Data
data.Add(CByte(s And &HFF))
Next
End If
' set new cry offset
crytosave.Offset = SearchFreeSpaceFourAligned(LoadedROM, &HFF, data.Count, "&H" & GetString(GetINIFileLocation(), header, "StartSearchingForSpaceOffset", "800000"))
' write cry
WriteHEX(LoadedROM, crytosave.Offset, ReverseHEX(VB.Right("0000" & CUShort(If(crytosave.Compressed, 1, 0)), 4)))
WriteHEX(LoadedROM, crytosave.Offset + 2, ReverseHEX(VB.Right("0000" & CUShort(If(crytosave.Looped, &H4000, 0)), 4)))
WriteHEX(LoadedROM, crytosave.Offset + 4, ReverseHEX(VB.Right("00000000" & Hex(crytosave.SampleRate << 10), 8)))
WriteHEX(LoadedROM, crytosave.Offset + 8, ReverseHEX(VB.Right("00000000" & Hex(crytosave.LoopStart), 8)))
WriteHEX(LoadedROM, crytosave.Offset + 12, ReverseHEX(VB.Right("00000000" & Hex(crytosave.Data.Length - 2), 8)))
Dim tempbuff As String = ByteArrayToHexString(data.ToArray)
WriteHEX(LoadedROM, crytosave.Offset + 16, tempbuff)
' write cry table entry
WriteHEX(LoadedROM, cryTable + (crytosave.Index * 12), ReverseHEX(If(crytosave.Compressed, "00003C20", "00003C00")))
WriteHEX(LoadedROM, cryTable + (crytosave.Index * 12) + 4, ReverseHEX(VB.Right("00000000" & Hex(crytosave.Offset + &H8000000), 8)))
WriteHEX(LoadedROM, cryTable + (crytosave.Index * 12) + 8, "FF00FF")
WriteHEX(LoadedROM, growlTable + (crytosave.Index * 12), ReverseHEX(If(crytosave.Compressed, "00003C30", "00003C00")))
WriteHEX(LoadedROM, growlTable + (crytosave.Index * 12) + 4, ReverseHEX(VB.Right("00000000" & Hex(crytosave.Offset + &H8000000), 8)))
WriteHEX(LoadedROM, growlTable + (crytosave.Index * 12) + 8, "FF00FF")
If crytosave.Index > 276 Then
Dim offset As Integer = Int32.Parse((GetString(GetINIFileLocation(), header, "CryConversionTable", "")), System.Globalization.NumberStyles.HexNumber)
WriteHEX(LoadedROM, ((offset)) + ((crytosave.Index - 277) * 2), ReverseHEX(VB.Right("0000" & Hex(crytosave.Index), 4)))
End If
Return True
End Function
End Module

View File

@ -283,6 +283,7 @@ Partial Class Pokemonedit
Me.Button30 = New System.Windows.Forms.Button()
Me.Button38 = New System.Windows.Forms.Button()
Me.Button39 = New System.Windows.Forms.Button()
Me.Button43 = New System.Windows.Forms.Button()
Me.TabControl1.SuspendLayout()
Me.TabPage1.SuspendLayout()
Me.GroupBox26.SuspendLayout()
@ -2543,6 +2544,7 @@ Partial Class Pokemonedit
'
'TabPage6
'
Me.TabPage6.Controls.Add(Me.Button43)
Me.TabPage6.Controls.Add(Me.GroupBox35)
Me.TabPage6.Controls.Add(Me.GroupBox34)
Me.TabPage6.Controls.Add(Me.GroupBox21)
@ -2740,7 +2742,6 @@ Partial Class Pokemonedit
'
'Button33
'
Me.Button33.Enabled = False
Me.Button33.Location = New System.Drawing.Point(336, 264)
Me.Button33.Margin = New System.Windows.Forms.Padding(3, 4, 3, 4)
Me.Button33.Name = "Button33"
@ -3128,6 +3129,16 @@ Partial Class Pokemonedit
Me.Button39.Text = "Import All Cries"
Me.Button39.UseVisualStyleBackColor = True
'
'Button43
'
Me.Button43.Location = New System.Drawing.Point(19, 682)
Me.Button43.Margin = New System.Windows.Forms.Padding(3, 4, 3, 4)
Me.Button43.Name = "Button43"
Me.Button43.Size = New System.Drawing.Size(237, 41)
Me.Button43.TabIndex = 29
Me.Button43.Text = "Import And Repoint All"
Me.Button43.UseVisualStyleBackColor = True
'
'Pokemonedit
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(9.0!, 20.0!)
@ -3513,4 +3524,5 @@ Partial Class Pokemonedit
Friend WithEvents Label59 As System.Windows.Forms.Label
Friend WithEvents TextBox8 As System.Windows.Forms.TextBox
Friend WithEvents Label58 As System.Windows.Forms.Label
Friend WithEvents Button43 As Button
End Class

View File

@ -3943,8 +3943,14 @@ Public Class Pokemonedit
Me.Enabled = False
crynorm = ImportCry(fileOpenDialog.FileName, crynorm)
crynorm.Index += 1
Dim cryGood As Boolean = SaveCryNoPrompt(crynorm, CryTable, CryTable3)
SaveCry(crynorm, CryTable)
If cryGood Then
LoadCryWindow()
End If
Me.Text = "Pokemon Editor"
Me.Enabled = True
@ -3989,14 +3995,6 @@ Public Class Pokemonedit
End If
End Sub
Private Sub Button36_Click(sender As Object, e As EventArgs) Handles Button36.Click
End Sub
Private Sub chkCompressed1_CheckedChanged(sender As Object, e As EventArgs) Handles chkCompressed1.CheckedChanged
End Sub
Private Sub AniSavBttn_Click(sender As Object, e As EventArgs) Handles AniSavBttn.Click
If header2 = "BPE" Then
@ -4136,4 +4134,103 @@ Public Class Pokemonedit
'Me.Enabled = True
End Sub
Private Sub Button43_Click(sender As Object, e As EventArgs) Handles Button43.Click
FolderBrowserDialog.Description = "Select folder to import cries from:"
If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.Text = "Please wait..."
Me.UseWaitCursor = True
ProgressBar.Value = 0
ProgressBar.Visible = True
Dim LoopVar As Integer
LoopVar = 440
Me.Enabled = False
CryTable = Int32.Parse((GetString(GetINIFileLocation(), header, "CryTable", "")), System.Globalization.NumberStyles.HexNumber)
'CryTable2 = Int32.Parse((GetString(GetINIFileLocation(), header, "CryConversionTable", "")), System.Globalization.NumberStyles.HexNumber)
CryTable3 = Int32.Parse((GetString(GetINIFileLocation(), header, "CryTable2", "")), System.Globalization.NumberStyles.HexNumber)
While LoopVar < (GetString(GetINIFileLocation(), header, "NumberOfPokemon", "")) - 1 And 439 < LoopVar And 861 > LoopVar = True
'PKMNames.SelectedIndex = LoopVar
Dim convNum As Integer = LoopVar - 53
Dim validFiles As String() = GetFiles(FolderBrowserDialog.SelectedPath, "*" & convNum & "*")
If validFiles.Count > 0 Then
crynorm = New Cry With {
.Index = LoopVar
}
crynorm = ImportCry(validFiles(0), crynorm)
SaveCryNoPrompt(crynorm, CryTable, CryTable3)
End If
LoopVar = LoopVar + 1
ProgressBar.Value = (LoopVar / (GetString(GetINIFileLocation(), header, "NumberOfPokemon", ""))) * 100
ProgressBar.Invalidate()
ProgressBar.Update()
End While
LoopVar = 880
While LoopVar < (GetString(GetINIFileLocation(), header, "NumberOfPokemon", "")) - 1 And 439 < LoopVar = True
Dim validFiles As String() = GetFiles(FolderBrowserDialog.SelectedPath, "*" & LoopVar & "*")
If validFiles.Count > 0 Then
crynorm = New Cry With {
.Index = LoopVar
}
crynorm = ImportCry(validFiles(0), crynorm)
SaveCryNoPrompt(crynorm, CryTable, CryTable3)
End If
LoopVar = LoopVar + 1
ProgressBar.Value = (LoopVar / (GetString(GetINIFileLocation(), header, "NumberOfPokemon", ""))) * 100
ProgressBar.Invalidate()
ProgressBar.Update()
End While
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim convMons As New List(Of String)
If IO.File.Exists(FolderBrowserDialog.SelectedPath & "\Conversions.txt") Then
convMons.AddRange(IO.File.ReadLines(FolderBrowserDialog.SelectedPath & "\Conversions.txt"))
Dim MonArray As List(Of String()) = New List(Of String())
For Each mon As String In convMons
MonArray.Add(mon.Split(New String() {"="}, StringSplitOptions.None))
Next
Dim offset As Integer = Int32.Parse((GetString(GetINIFileLocation(), header, "CryConversionTable", "")), System.Globalization.NumberStyles.HexNumber)
For Each mon As String() In MonArray
If mon.Count > 1 Then
If mon(1) < 277 Then
WriteHEX(LoadedROM, ((offset)) + ((Int32.Parse(mon(0)) - 277) * 2), ReverseHEX(VB.Right("0000" & Hex(Int32.Parse(mon(1) - 1)), 4)))
Else
WriteHEX(LoadedROM, ((offset)) + ((Int32.Parse(mon(0)) - 277) * 2), ReverseHEX(VB.Right("0000" & Hex(Int32.Parse(mon(1))), 4)))
End If
End If
Next
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
PKMNames.SelectedIndex = 0
Me.Text = "Pokemon Editor"
Me.UseWaitCursor = False
Me.Enabled = True
ProgressBar.Visible = False
Me.BringToFront()
End If
End Sub
End Class

View File

@ -16,7 +16,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>OiPYuKrm3hTRYebjuk/MfqgH2piFKqF6XyRDuz+6gEg=</dsig:DigestValue>
<dsig:DigestValue>3NuNXXjI20ry94cuNrjg+h/qy1PYBF7LwfBw/bMfSlY=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

View File

@ -43,14 +43,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="PokemonGameEditor.exe" size="2087936">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="PokemonGameEditor.exe" size="2092032">
<assemblyIdentity name="PokemonGameEditor" version="3.8.1.0" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>iWch5cpS50wZ/A131ESu9tlN5AKOYAw4wd0tKaVkBjQ=</dsig:DigestValue>
<dsig:DigestValue>ivmk2nhBNmCntFpI39KjDDTzzcvJr/l2z+/V6/KE054=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

View File

@ -16,7 +16,7 @@
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>OiPYuKrm3hTRYebjuk/MfqgH2piFKqF6XyRDuz+6gEg=</dsig:DigestValue>
<dsig:DigestValue>3NuNXXjI20ry94cuNrjg+h/qy1PYBF7LwfBw/bMfSlY=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>

View File

@ -43,14 +43,14 @@
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="PokemonGameEditor.exe" size="2087936">
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="PokemonGameEditor.exe" size="2092032">
<assemblyIdentity name="PokemonGameEditor" version="3.8.1.0" language="neutral" processorArchitecture="msil" />
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
<dsig:DigestValue>iWch5cpS50wZ/A131ESu9tlN5AKOYAw4wd0tKaVkBjQ=</dsig:DigestValue>
<dsig:DigestValue>ivmk2nhBNmCntFpI39KjDDTzzcvJr/l2z+/V6/KE054=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>