-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonFuncs.cs
140 lines (114 loc) · 4.44 KB
/
CommonFuncs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace MO2ExportImport
{
public class CommonFuncs
{
public static List<ModListing> LoadModList(string filePath, bool reverseOrder = true)
{
if (!File.Exists(filePath))
{
return new ();
}
var lines = File.ReadAllLines(filePath).Where(line => !line.StartsWith("#")).ToList();
var modListings = new List<ModListing>();
foreach (var line in lines)
{
modListings.Add(new(line));
}
if (reverseOrder)
{
modListings.Reverse(); // Reverse the list in place if needed
}
return modListings;
}
public static bool SaveModList(string filePath, List<ModListing> modList, out string exceptionStr)
{
List<string> output = modList.Select(mod => mod.GetCurrentEntryString()).ToList();
// Reverse the ProfileModList back to original order before saving
output.Reverse();
// Reinsert the special comment line at the beginning
output.Insert(0, "# This file was automatically generated by Mod Organizer.");
// Save the edited mod list
try
{
File.WriteAllLines(filePath, output);
exceptionStr = string.Empty;
return true;
}
catch (Exception e)
{
exceptionStr = $"Could not save profile information at {filePath}. Error:" + Environment.NewLine + e.Message;
return false;
}
}
public static bool SavePluginList(string filePath, List<PluginListing> pluginList, out string exceptionStr)
{
var output = pluginList.Select(x => x.GetCurrentEntryString()).ToList();
output.Insert(0, "# This file was automatically generated by Mod Organizer.");
// Save the edited plugin lists
try
{
File.WriteAllLines(filePath, output);
exceptionStr = string.Empty;
return true;
}
catch (Exception e)
{
exceptionStr = $"Could not save profile information at {filePath}. Error:" + Environment.NewLine + e.Message;
return false;
}
}
public static List<PluginListing> LoadPluginList(string filePath, bool reverseOrder = false)
{
var pluginListings = new List<PluginListing>();
if (!File.Exists(filePath))
{
return pluginListings;
}
var lines = File.ReadAllLines(filePath).Where(line => !line.StartsWith("#")).ToList();
if (reverseOrder)
{
lines.Reverse(); // Reverse the list in place if needed
}
foreach (var line in lines)
{
pluginListings.Add(new(line));
}
return pluginListings;
}
public static bool IsNoDelete(string modName, out string matchedPrefix)
{
matchedPrefix = string.Empty;
// Define a case-insensitive regex pattern
string pattern = @"^\[NoDelete.*?\]";
// Match the pattern at the start of the string
var match = Regex.Match(modName, pattern, RegexOptions.IgnoreCase);
// Check if a match is found at the start of the string
if (match.Success && match.Index == 0)
{
matchedPrefix = match.Value;
return true;
}
return false;
}
public static string RemoveNoDeletePrefix(string modName) // expects mod name without the activation status
{
if (IsNoDelete(modName, out string matchedPrefix))
{
// Remove the matched substring and trim the result
modName = modName.Substring(matchedPrefix.Length).Trim();
return modName;
}
// Return the original string if no match
return modName;
}
}
}