' Save File System Inline-Function for VoiceAttack - Save Variables to Text File (or Delete)
' by SemlerPDX Apr2020
' VETERANS-GAMING.COM
Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Linq
Imports System.Xml.Linq
Imports System.Threading.Tasks
Public Class VAInline
dim entryDeleted as boolean
dim entryChanged as boolean
dim entryAdded as boolean
dim deleteVariable as boolean
dim requestVarName as string
dim requestVarValue as string
dim contents() as string
dim entry() as string
dim entryFinal as string
dim saveFinal as string
dim contentsFinal as new List(of string)
Public Sub Main()
'Check and Set Delete Parameter Variables
if VA.GetBoolean("AVCS_SFS_DELETE_saved") IsNot nothing
deleteVariable = VA.GetBoolean("AVCS_SFS_DELETE_saved")
else
deleteVariable = false
end if
'Check and Set Requested Name Parameter Variables
if VA.GetText("~change_request_var_name") IsNot nothing
requestVarName = VA.GetText("~change_request_var_name")
'VA.WriteToLog(requestVarName)
end if
'Check and Set Requested Value Parameter Variables
if VA.GetText("~change_request_var_value") IsNot nothing
requestVarValue = VA.GetText("~change_request_var_value")
'VA.WriteToLog(requestVarValue)
end if
if VA.GetText("~save_file") IsNot nothing
if VA.GetText("~save_file").Contains(requestVarName)
'Split contents of save file into single lines separated by the newline
if VA.GetText("~save_file").Contains(vbNewLine)
contents = VA.GetText("~save_file").Split(new string() {Environment.NewLine},StringSplitOptions.None)
'File contains this request somewhere, go through each line to find it
for each line as string in contents
if not(line = "")
if (deleteVariable)
'Add any lines not containing delete request to new list
if (line.Contains(requestVarName))
entryDeleted = true
else
contentsFinal.Add(line)
end if
else
'If this line contains the requested variable
if(line.Contains (requestVarName))
'Split this line and compare value to change request
entry = line.Split("=")
'if ((not(entry(1).Contains(requestVarValue))) or (not(entry(1).StartsWith(requestVarValue))))
if ((not(entry(1).Contains(requestVarValue))) or (not(entry(1).StartsWith(requestVarValue))) or (not(entry(1).EndsWith(requestVarValue))))
entry(1) = entry(1).Replace(entry(1),requestVarValue)
entryChanged = true
end if
'Add each entry to new list if changed or not
entryFinal = entry(0) + "=" + entry(1)
contentsFinal.Add(entryFinal)
else
contentsFinal.Add(line)
end if
end if
end if
next 'loop around to next line
else
'If the save file is one line containing this entry
dim line as string = VA.GetText("~save_file")
if not(line = "")
'Split this line and compare value to change request
entry = line.Split("=")
if not(entry(1).Contains(requestVarValue))
entry(1) = entry(1).Replace(entry(1),requestVarValue)
entryChanged = true
end if
'Add entry to new list if changed or not
entryFinal = entry(0) + "=" + entry(1)
contentsFinal.Add(entryFinal)
end if
end if
'------------------------------------------------------------------------------------------
'--end of check where file contains requested var already and has one or more lines-----------
'------------------------------------------------------------------------------------------
else
'Else (if) file does NOT contain RequestVarName
if not(deleteVariable)
'Split contents of save file into single lines separated by the newline
if VA.GetText("~save_file").Contains(vbNewLine)
contents = VA.GetText("~save_file").Split(new string() {Environment.NewLine},StringSplitOptions.None)
'Add each line to the final list, and the new request at the end
for each line as string in contents
if not(line = "")
contentsFinal.Add(line)
end if
next
'Moved this out of the above if not .Add portion above, otherwise many duplicates added?
entryFinal = requestVarName + "=" + requestVarValue
contentsFinal.Add(entryFinal)
'entryChanged = true (disabled June)
entryAdded = true
else
'If the save file is one line
dim line as string = VA.GetText("~save_file")
if not(line = "")
'If that one line has contents (not containing requested var name)
contentsFinal.Add(line)
end if
'Enter new requested save variable to contents
entryFinal = requestVarName + "=" + requestVarValue
contentsFinal.Add(entryFinal)
'entryChanged = true (disabled June - changed to Added)
entryAdded = true
end if
end if
'------------------------------------------------------------------------------------------
'--end of check where file does not contain requested var already and has one or more lines---
'------------------------------------------------------------------------------------------
end if
else
'If ~save_file is 'nothing' then add the request
if not (deleteVariable)
entryFinal = requestVarName + "=" + requestVarValue
contentsFinal.Add(entryFinal)
entryChanged = true
end if
'----------------------------------------------------------------------------------------------------
'--end of redundant check where file does not contain any lines yet or is somehow non-existent---------
'----------------------------------------------------------------------------------------------------
end if
'Tranform new list back into string with newline placeholders for VA.proxy SetText
if not(contentsFinal.ToString() = "")
'Remove duplicate lines added through the line-by-line re-assembly above
contentsFinal = contentsFinal.Distinct().ToList()
saveFinal = ""
for each newline as string in contentsFinal
saveFinal = newline + "(/n)" + saveFinal
next
else
if VA.GetText("~save_file") IsNot nothing
saveFinal = VA.GetText("~save_file")
else
saveFinal = ""
end if
end if
VA.SetText("~save_file", saveFinal)
if(entryDeleted)
VA.SetBoolean("~entry_deleted", true)
end if
if(entryChanged)
VA.SetBoolean("~entry_changed", true)
end if
if(entryAdded)
VA.SetBoolean("~entry_added", true)
end if
End Sub
End Class
Comments