Updated ModPreprocessorDefine.csx

This commit is contained in:
MrGadget1024 2023-11-09 07:39:28 -05:00
parent 8893c1d651
commit 300175d4dd

View File

@ -1,137 +1,135 @@
#r "System.IO"
#r "System.Text.RegularExpressions"
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
void Main()
class ModPreprocessorDefine
{
Debug.WriteLine("Debug ModPreprocessorDefine Started");
Console.WriteLine("ModPreprocessorDefine Started");
Console.Out.Flush();
// Redirect console output to a file
var originalConsoleOut = Console.Out;
// Modify the path to use an absolute path
string outputPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "console_output.txt");
// Redirect console output to the absolute file path
using (var writer = new StreamWriter(outputPath))
static void Main()
{
Console.SetOut(writer);
// Print some debug information
Console.WriteLine($"Working Directory: {Environment.CurrentDirectory}");
Console.WriteLine($"Output File Path: {outputPath}");
Console.WriteLine("ModPreprocessorDefine Started");
Console.Out.Flush();
// Define the path to the PreprocessorDefine.cs file
string filePath = "Assets/Mirror/CompilerSymbols/PreprocessorDefine.cs";
// Redirect console output to a file
var originalConsoleOut = Console.Out;
// Read the contents of the file
string fileContents = File.ReadAllText(filePath);
Console.WriteLine("ModPreprocessorDefine File read");
// Modify the path to use an absolute path
string outputPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "console_output.txt");
// Redirect console output to the absolute file path
using (var writer = new StreamWriter(outputPath))
{
Console.SetOut(writer);
// Print some debug information
Console.WriteLine($"Working Directory: {Environment.CurrentDirectory}");
Console.WriteLine($"Output File Path: {outputPath}");
Console.Out.Flush();
// Define the path to the PreprocessorDefine.cs file
string filePath = "Assets/Mirror/CompilerSymbols/PreprocessorDefine.cs";
// Read the contents of the file
string fileContents = File.ReadAllText(filePath);
Console.WriteLine("ModPreprocessorDefine File read");
Console.Out.Flush();
// Find and remove the first entry ending with "_OR_NEWER"
fileContents = RemoveFirstOrNewerEntry(fileContents);
Console.WriteLine("ModPreprocessorDefine Old entry removed");
Console.Out.Flush();
// Find the last entry and capture the version number
string versionNumber = GetLastVersionNumber(fileContents);
Console.WriteLine($"ModPreprocessorDefine current version {versionNumber}");
Console.Out.Flush();
// Append a new entry with the correct indentation and next version number
fileContents = AppendNewEntry(fileContents, versionNumber);
Console.WriteLine("ModPreprocessorDefine New entry appended");
Console.Out.Flush();
// Write the modified contents back to the file
File.WriteAllText(filePath, fileContents);
Console.SetOut(originalConsoleOut);
}
Console.WriteLine("ModPreprocessorDefine Finished");
// Print the contents of the redirected file
Console.WriteLine("Console Output:");
Console.WriteLine(File.ReadAllText("console_output.txt"));
Console.Out.Flush();
// Find and remove the first entry ending with "_OR_NEWER"
fileContents = RemoveFirstOrNewerEntry(fileContents);
Console.WriteLine("ModPreprocessorDefine Old entry removed");
Console.Out.Flush();
// Find the last entry and capture the version number
string versionNumber = GetLastVersionNumber(fileContents);
Console.WriteLine($"ModPreprocessorDefine current version {versionNumber}");
Console.Out.Flush();
// Append a new entry with the correct indentation and next version number
fileContents = AppendNewEntry(fileContents, versionNumber);
Console.WriteLine("ModPreprocessorDefine New entry appended");
Console.Out.Flush();
// Write the modified contents back to the file
File.WriteAllText(filePath, fileContents);
Console.SetOut(originalConsoleOut);
}
Console.WriteLine("ModPreprocessorDefine Finished");
// Print the contents of the redirected file
Console.WriteLine("Console Output:");
Console.WriteLine(File.ReadAllText("console_output.txt"));
Console.Out.Flush();
}
string RemoveFirstOrNewerEntry(string input)
{
// Regex pattern to match the first entry ending with "_OR_NEWER"
string pattern = @"^\s*""[^""]*_OR_NEWER""\s*,\s*$";
// Find the first match
Match match = Regex.Match(input, pattern, RegexOptions.Multiline);
// If a match is found, remove the entire line
if (match.Success)
string RemoveFirstOrNewerEntry(string input)
{
input = input.Remove(match.Index, match.Length);
// Regex pattern to match the first entry ending with "_OR_NEWER"
string pattern = @"^\s*""[^""]*_OR_NEWER""\s*,\s*$";
// Find the first match
Match match = Regex.Match(input, pattern, RegexOptions.Multiline);
// If a match is found, remove the entire line
if (match.Success)
{
input = input.Remove(match.Index, match.Length);
}
return input;
}
return input;
}
string GetLastVersionNumber(string input)
{
// Regex pattern to match the last entry and capture the version number
string pattern = @"^\s*""([^""]*)_OR_NEWER""\s*,\s*$";
// Find all matches
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Multiline);
// Capture the version number from the last match
string versionNumber = matches.Count > 0 ? matches[matches.Count - 1].Groups[1].Value : "";
return versionNumber;
}
string AppendNewEntry(string input, string versionNumber)
{
// Calculate the next version number (increment by 1)
int nextVersion = int.TryParse(versionNumber, out int currentVersion) ? currentVersion + 1 : 1;
// Get the indentation of the "HashSet<string> defines = new HashSet<string>" line
string indentation = GetHashSetIndentation(input);
// Create the new entry with the correct indentation and next version number
string newEntry = indentation + $" \"MIRROR_{nextVersion}_OR_NEWER\"";
Console.WriteLine($"New entry: {newEntry}");
// Find the position of the "defines" HashSet and insert the new entry into it
int definesStartIndex = input.IndexOf("HashSet<string> defines = new HashSet<string>");
int definesEndIndex = input.IndexOf("};", definesStartIndex) + 1;
// Insert the comma and new entry into the "defines" HashSet
input = input.Remove(definesEndIndex - 2, 2); // Remove the trailing "};"
input = input.Insert(definesEndIndex - 2, $",\n{newEntry}\n{indentation}}};");
Console.WriteLine(input);
return input;
}
string GetHashSetIndentation(string input)
{
// Regex pattern to match the indentation of "HashSet<string> defines = new HashSet<string>"
string pattern = @"^\s*HashSet<string> defines = new HashSet<string>";
// Find the first match
Match match = Regex.Match(input, pattern, RegexOptions.Multiline);
// If a match is found, capture the indentation and add 4 spaces
string indentation = match.Success ? Regex.Match(match.Value, @"^\s*").Value : "";
return indentation;
string GetLastVersionNumber(string input)
{
// Regex pattern to match the last entry and capture the version number
string pattern = @"^\s*""([^""]*)_OR_NEWER""\s*,\s*$";
// Find all matches
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Multiline);
// Capture the version number from the last match
string versionNumber = matches.Count > 0 ? matches[matches.Count - 1].Groups[1].Value : "";
return versionNumber;
}
string AppendNewEntry(string input, string versionNumber)
{
// Calculate the next version number (increment by 1)
int nextVersion = int.TryParse(versionNumber, out int currentVersion) ? currentVersion + 1 : 1;
// Get the indentation of the "HashSet<string> defines = new HashSet<string>" line
string indentation = GetHashSetIndentation(input);
// Create the new entry with the correct indentation and next version number
string newEntry = indentation + $" \"MIRROR_{nextVersion}_OR_NEWER\"";
Console.WriteLine($"New entry: {newEntry}");
// Find the position of the "defines" HashSet and insert the new entry into it
int definesStartIndex = input.IndexOf("HashSet<string> defines = new HashSet<string>");
int definesEndIndex = input.IndexOf("};", definesStartIndex) + 1;
// Insert the comma and new entry into the "defines" HashSet
input = input.Remove(definesEndIndex - 2, 2); // Remove the trailing "};"
input = input.Insert(definesEndIndex - 2, $",\n{newEntry}\n{indentation}}};");
Console.WriteLine(input);
return input;
}
string GetHashSetIndentation(string input)
{
// Regex pattern to match the indentation of "HashSet<string> defines = new HashSet<string>"
string pattern = @"^\s*HashSet<string> defines = new HashSet<string>";
// Find the first match
Match match = Regex.Match(input, pattern, RegexOptions.Multiline);
// If a match is found, capture the indentation and add 4 spaces
string indentation = match.Success ? Regex.Match(match.Value, @"^\s*").Value : "";
return indentation;
}
}