P3D-Legacy/P3D/HelperClasses/Localization.vb
JappaWakka 9def5642e8 Lots of QoL changes
- You can now run up and down a slideblock.
- NPC movement type Straight is now less random and more smooth.
- When renaming the rival, the skin visible at the top changes to whatever skin is set as the rivalskin. This way, the rival is not stuck with one skin. This can be useful when the rival's skin changes based on the player's gender for example.
- Text messages that contain <token.tokenname>, where tokenname is a string from the localizations files, have the part between the <> replaced with that string now.
- Text messages that contain <button.buttonname>, where buttonname is one of the controls defined in "Saves\Keyboard.dat", will now have the part between the <> replaced with the key assigned to that control.
- Players can now be genderless instead of only either male or female in scripts.
- Which skins are available in the intro is now defined in the GameMode's definition file instead of being hard-coded
- Old New Game intro now uses the player skin data from the GameMode definition file
- The Pokémon in the old New Game now doesn't fall too far down anymore
- Scripts adjusted to match the new gender functionality
2021-10-01 15:19:04 +02:00

240 lines
10 KiB
VB.net

Public Class Localization
Public Shared LanguageSuffix As String = "en"
Public Shared LocalizationTokens As Dictionary(Of String, Token) = New Dictionary(Of String, Token)
Public Shared Sub Load(ByVal LanguageSuffix As String)
LocalizationTokens.Clear()
Localization.LanguageSuffix = LanguageSuffix
Logger.Debug("Loaded language [" & LanguageSuffix & "]")
LoadTokenFile(GameMode.DefaultLocalizationsPath, False)
If GameModeManager.GameModeCount > 0 Then
Dim GameModeLocalizationPath As String = GameModeManager.ActiveGameMode.LocalizationsPath
If GameModeLocalizationPath <> GameMode.DefaultLocalizationsPath Then
LoadTokenFile(GameModeLocalizationPath, True)
End If
End If
End Sub
Public Shared Sub ReloadGameModeTokens()
For i = 0 To LocalizationTokens.Count - 1
If i <= LocalizationTokens.Count - 1 Then
Dim Token As Token = LocalizationTokens.Values(i)
If Token.IsGameModeToken = True Then
LocalizationTokens.Remove(LocalizationTokens.Keys(i))
i -= 1
End If
End If
Next
If GameModeManager.GameModeCount > 0 Then
Dim GameModeLocalizationPath As String = GameModeManager.ActiveGameMode.LocalizationsPath
LoadTokenFile(GameModeLocalizationPath, True)
End If
Logger.Debug("---Reloaded GameMode Tokens---")
End Sub
Private Shared Sub LoadTokenFile(ByVal path As String, ByVal IsGameModeFile As Boolean)
Dim fullpath As String = GameController.GamePath & path
Dim tokenFullpath As String = fullpath & "Tokens_" & LanguageSuffix & ".dat"
Logger.Debug("Token filepath: " & tokenFullpath)
If System.IO.Directory.GetFiles(fullpath).Count > 0 Then
If System.IO.File.Exists(tokenFullpath) = False Then
Logger.Debug("Did NOT find token file for suffix: " & LanguageSuffix)
LanguageSuffix = "en"
End If
If System.IO.File.Exists(tokenFullpath) = True Then
Logger.Debug("Found token file for suffix: " & LanguageSuffix)
Dim TokensFile() As String = System.IO.File.ReadAllLines(fullpath & "Tokens_" & LanguageSuffix & ".dat")
Dim splitIdx As Integer = 0
For Each TokenLine As String In TokensFile
If TokenLine.Contains(",") = True Then
splitIdx = TokenLine.IndexOf(",")
Dim TokenName As String = TokenLine.Substring(0, splitIdx)
Dim TokenContent As String = ""
If TokenLine.Length > TokenName.Length + 1 Then
TokenContent = TokenLine.Substring(splitIdx + 1, TokenLine.Length - splitIdx - 1)
End If
If LocalizationTokens.ContainsKey(TokenName) = False Then
LocalizationTokens.Add(TokenName, New Token(TokenContent, LanguageSuffix, IsGameModeFile))
Else
LocalizationTokens.Remove(TokenName)
LocalizationTokens.Add(TokenName, New Token(TokenContent, LanguageSuffix, IsGameModeFile))
End If
End If
Next
End If
If Not LanguageSuffix = "en" Then
If System.IO.File.Exists(fullpath & "Tokens_en.dat") Then
Dim FallbackTokensFile() As String = System.IO.File.ReadAllLines(fullpath & "Tokens_en.dat")
Dim splitIdx As Integer = 0
For Each TokenLine As String In FallbackTokensFile
If TokenLine.Contains(",") = True Then
splitIdx = TokenLine.IndexOf(",")
Dim TokenName As String = TokenLine.Substring(0, splitIdx)
Dim TokenContent As String = ""
If TokenLine.Length > TokenName.Length + 1 Then
TokenContent = TokenLine.Substring(splitIdx + 1, TokenLine.Length - splitIdx - 1)
End If
If LocalizationTokens.ContainsKey(TokenName) = False Then
LocalizationTokens.Add(TokenName, New Token(TokenContent, "en", IsGameModeFile))
Else
LocalizationTokens.Remove(TokenName)
LocalizationTokens.Add(TokenName, New Token(TokenContent, "en", IsGameModeFile))
End If
End If
Next
End If
End If
End If
End Sub
Public Shared Function GetString(ByVal s As String, Optional ByVal DefaultValue As String = "") As String
Dim resultToken As Token = Nothing
If LocalizationTokens.ContainsKey(s) = True Then
If LocalizationTokens.TryGetValue(s, resultToken) = False Then
Return s
Else
Dim result As String = resultToken.TokenContent
If Core.Player IsNot Nothing Then
result = result.Replace("<playername>", Core.Player.Name)
result = result.Replace("<player.name>", Core.Player.Name)
result = result.Replace("<rivalname>", Core.Player.RivalName)
result = result.Replace("<player.name>", Core.Player.RivalName)
End If
Dim tokenSearchBuffer As String() = result.Split("<button.")
Dim tokenEndIdx As Integer = 0
Dim validToken As String = ""
For Each possibleToken As String In tokenSearchBuffer
tokenEndIdx = possibleToken.IndexOf(">")
If Not tokenEndIdx = -1 Then
Dim key As Keys
validToken = possibleToken.Substring(0, tokenEndIdx)
Select Case validToken.ToLower()
Case "moveforward"
key = KeyBindings.ForwardMoveKey
Case "moveleft"
key = KeyBindings.LeftMoveKey
Case "movebackward"
key = KeyBindings.BackwardMoveKey
Case "moveright"
key = KeyBindings.RightMoveKey
Case "openmenu"
key = KeyBindings.OpenInventoryKey
Case "chat"
key = KeyBindings.ChatKey
Case "special", "phone"
key = KeyBindings.SpecialKey
Case "muteaudio"
key = KeyBindings.MuteAudioKey
Case "cameraleft"
key = KeyBindings.LeftKey
Case "cameraright"
key = KeyBindings.RightKey
Case "cameraup"
key = KeyBindings.UpKey
Case "cameradown"
key = KeyBindings.DownKey
Case "cameralock"
key = KeyBindings.CameraLockKey
Case "guicontrol"
key = KeyBindings.GUIControlKey
Case "screenshot"
key = KeyBindings.ScreenshotKey
Case "debugcontrol"
key = KeyBindings.DebugKey
Case "perspectiveswitch"
key = KeyBindings.PerspectiveSwitchKey
Case "fullscreen"
key = KeyBindings.FullScreenKey
Case "enter1"
key = KeyBindings.EnterKey1
Case "enter2"
key = KeyBindings.EnterKey2
Case "back1"
key = KeyBindings.BackKey1
Case "back2"
key = KeyBindings.BackKey2
Case "escape", "esc"
key = KeyBindings.EscapeKey
Case "onlinestatus"
key = KeyBindings.OnlineStatusKey
Case "lighting"
key = KeyBindings.LightKey
End Select
result = result.Replace("<button." & validToken & ">", KeyBindings.GetKeyName(key))
End If
Next
Return result
End If
Else
If DefaultValue = "" Then
Return s
Else
Return DefaultValue
End If
End If
End Function
Public Shared Function TokenExists(ByVal TokenName As String) As Boolean
Return LocalizationTokens.ContainsKey(TokenName)
End Function
End Class
Public Class Token
Private _TokenContent As String = ""
Private _TokenLanguageSuffix As String = "en"
Private _IsGameModeToken As Boolean = False
Public Sub New(ByVal TokenContent As String, ByVal TokenLanguageSuffix As String, ByVal IsGameModeToken As Boolean)
Me._IsGameModeToken = IsGameModeToken
Me._TokenContent = TokenContent
Me._TokenLanguageSuffix = TokenLanguageSuffix
End Sub
Public Property TokenContent() As String
Get
Return Me._TokenContent
End Get
Set(value As String)
Me._TokenContent = value
End Set
End Property
Public Property TokenLanguageSuffix() As String
Get
Return Me._TokenLanguageSuffix
End Get
Set(value As String)
Me._TokenLanguageSuffix = value
End Set
End Property
Public Property IsGameModeToken() As Boolean
Get
Return Me._IsGameModeToken
End Get
Set(value As Boolean)
Me._IsGameModeToken = value
End Set
End Property
End Class